From b8749ac66544e2a28d802a362ad9dbc683133318 Mon Sep 17 00:00:00 2001 From: Andrew Cassidy Date: Sun, 24 Dec 2023 17:59:42 -0800 Subject: [PATCH] implement From for all image types (#2076) --- src/buffer.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/buffer.rs b/src/buffer.rs index 046bdebe76..bfb6e66c14 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -14,6 +14,7 @@ use crate::image::{GenericImage, GenericImageView, ImageEncoder, ImageFormat, Im use crate::math::Rect; use crate::traits::{EncodableLayout, Pixel, PixelWithColorType}; use crate::utils::expand_packed; +use crate::DynamicImage; /// Iterate over pixel refs. pub struct Pixels<'a, P: Pixel + 'a> @@ -1427,6 +1428,60 @@ pub type Rgb32FImage = ImageBuffer, Vec>; /// where the backing container is a flattened vector of floats. pub type Rgba32FImage = ImageBuffer, Vec>; +impl From for RgbImage { + fn from(value: DynamicImage) -> Self { + value.into_rgb8() + } +} + +impl From for RgbaImage { + fn from(value: DynamicImage) -> Self { + value.into_rgba8() + } +} + +impl From for GrayImage { + fn from(value: DynamicImage) -> Self { + value.into_luma8() + } +} + +impl From for GrayAlphaImage { + fn from(value: DynamicImage) -> Self { + value.into_luma_alpha8() + } +} + +impl From for Rgb16Image { + fn from(value: DynamicImage) -> Self { + value.into_rgb16() + } +} + +impl From for Rgba16Image { + fn from(value: DynamicImage) -> Self { + value.into_rgba16() + } +} + +impl From for Gray16Image { + fn from(value: DynamicImage) -> Self { + value.into_luma16() + } +} + +impl From for GrayAlpha16Image { + fn from(value: DynamicImage) -> Self { + value.into_luma_alpha16() + } +} + +impl From for Rgba32FImage { + fn from(value: DynamicImage) -> Self { + value.into_rgba32f() + } +} + #[cfg(test)] mod test { use super::{GrayImage, ImageBuffer, ImageOutputFormat, RgbImage};