Skip to content

Commit

Permalink
implement From<DynamicImage> for all image types (#2076)
Browse files Browse the repository at this point in the history
  • Loading branch information
drewcassidy authored Dec 25, 2023
1 parent 7197841 commit b8749ac
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -1427,6 +1428,60 @@ pub type Rgb32FImage = ImageBuffer<Rgb<f32>, Vec<f32>>;
/// where the backing container is a flattened vector of floats.
pub type Rgba32FImage = ImageBuffer<Rgba<f32>, Vec<f32>>;

impl From<DynamicImage> for RgbImage {
fn from(value: DynamicImage) -> Self {
value.into_rgb8()
}
}

impl From<DynamicImage> for RgbaImage {
fn from(value: DynamicImage) -> Self {
value.into_rgba8()
}
}

impl From<DynamicImage> for GrayImage {
fn from(value: DynamicImage) -> Self {
value.into_luma8()
}
}

impl From<DynamicImage> for GrayAlphaImage {
fn from(value: DynamicImage) -> Self {
value.into_luma_alpha8()
}
}

impl From<DynamicImage> for Rgb16Image {
fn from(value: DynamicImage) -> Self {
value.into_rgb16()
}
}

impl From<DynamicImage> for Rgba16Image {
fn from(value: DynamicImage) -> Self {
value.into_rgba16()
}
}

impl From<DynamicImage> for Gray16Image {
fn from(value: DynamicImage) -> Self {
value.into_luma16()
}
}

impl From<DynamicImage> for GrayAlpha16Image {
fn from(value: DynamicImage) -> Self {
value.into_luma_alpha16()
}
}

impl From<DynamicImage> for Rgba32FImage {
fn from(value: DynamicImage) -> Self {
value.into_rgba32f()
}
}

#[cfg(test)]
mod test {
use super::{GrayImage, ImageBuffer, ImageOutputFormat, RgbImage};
Expand Down

0 comments on commit b8749ac

Please sign in to comment.