Skip to content

Commit

Permalink
feat: Implement TryFrom trait for RgbImage and RgbaImage
Browse files Browse the repository at this point in the history
  • Loading branch information
rusty committed Feb 26, 2021
1 parent d25ab47 commit 69c2b1e
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,26 @@ impl TryFrom<DynamicImage> for Image {
}
}

impl TryFrom<image::RgbImage> for Image {
type Error = ImageError;

fn try_from(img: image::RgbImage) -> Result<Self, Self::Error> {
let (width, height) = img.dimensions();
let image_data = img.into_raw();
Image::from_rgb(width as i32, height as i32, image_data)
}
}

impl TryFrom<image::RgbaImage> for Image {
type Error = ImageError;

fn try_from(img: image::RgbaImage) -> Result<Self, Self::Error> {
let (width, height) = img.dimensions();
let image_data = img.into_raw();
Image::from_rgba(width as i32, height as i32, image_data)
}
}

/// Errors that can occur when creating an Image
#[derive(Debug)]
pub enum ImageError {
Expand Down

0 comments on commit 69c2b1e

Please sign in to comment.