From 69c2b1e2417399ea08a13463218c1ef34f64ab25 Mon Sep 17 00:00:00 2001 From: rusty Date: Wed, 24 Feb 2021 21:12:04 +0900 Subject: [PATCH] feat: Implement TryFrom trait for RgbImage and RgbaImage --- src/image.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/image.rs b/src/image.rs index 8bf0306b0..fc24ec656 100644 --- a/src/image.rs +++ b/src/image.rs @@ -106,6 +106,26 @@ impl TryFrom for Image { } } +impl TryFrom for Image { + type Error = ImageError; + + fn try_from(img: image::RgbImage) -> Result { + let (width, height) = img.dimensions(); + let image_data = img.into_raw(); + Image::from_rgb(width as i32, height as i32, image_data) + } +} + +impl TryFrom for Image { + type Error = ImageError; + + fn try_from(img: image::RgbaImage) -> Result { + 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 {