Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add a new constructor that takes a ColorType #1988

Merged
merged 1 commit into from
Aug 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/dynimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ macro_rules! dynamic_map(
);

impl DynamicImage {
/// Creates a dynamic image backed by a buffer depending on
/// the color type given.
pub fn new(w: u32, h: u32, color: color::ColorType) -> DynamicImage {
use color::ColorType::*;
match color {
L8 => Self::new_luma8(w, h),
La8 => Self::new_luma_a8(w, h),
Rgb8 => Self::new_rgb8(w, h),
Rgba8 => Self::new_rgba8(w, h),
L16 => Self::new_luma16(w, h),
La16 => Self::new_luma_a16(w, h),
Rgb16 => Self::new_rgb16(w, h),
Rgba16 => Self::new_rgba16(w, h),
Rgb32F => Self::new_rgb32f(w, h),
Rgba32F => Self::new_rgba32f(w, h),
}
}

/// Creates a dynamic image backed by a buffer of gray pixels.
pub fn new_luma8(w: u32, h: u32) -> DynamicImage {
DynamicImage::ImageLuma8(ImageBuffer::new(w, h))
Expand Down Expand Up @@ -1234,6 +1252,8 @@ mod bench {

#[cfg(test)]
mod test {
use crate::color::ColorType;

#[test]
fn test_empty_file() {
assert!(super::load_from_memory(b"").is_err());
Expand Down Expand Up @@ -1276,51 +1296,61 @@ mod test {
#[test]
fn test_grayscale_luma8() {
test_grayscale_alpha_discarded(super::DynamicImage::new_luma8(1, 1));
test_grayscale_alpha_discarded(super::DynamicImage::new(1, 1, ColorType::L8));
}

#[test]
fn test_grayscale_luma_a8() {
test_grayscale_alpha_preserved(super::DynamicImage::new_luma_a8(1, 1));
test_grayscale_alpha_preserved(super::DynamicImage::new(1, 1, ColorType::La8));
}

#[test]
fn test_grayscale_rgb8() {
test_grayscale_alpha_discarded(super::DynamicImage::new_rgb8(1, 1));
test_grayscale_alpha_discarded(super::DynamicImage::new(1, 1, ColorType::Rgb8));
}

#[test]
fn test_grayscale_rgba8() {
test_grayscale_alpha_preserved(super::DynamicImage::new_rgba8(1, 1));
test_grayscale_alpha_preserved(super::DynamicImage::new(1, 1, ColorType::Rgba8));
}

#[test]
fn test_grayscale_luma16() {
test_grayscale_alpha_discarded(super::DynamicImage::new_luma16(1, 1));
test_grayscale_alpha_discarded(super::DynamicImage::new(1, 1, ColorType::L16));
}

#[test]
fn test_grayscale_luma_a16() {
test_grayscale_alpha_preserved(super::DynamicImage::new_luma_a16(1, 1));
test_grayscale_alpha_preserved(super::DynamicImage::new(1, 1, ColorType::La16));
}

#[test]
fn test_grayscale_rgb16() {
test_grayscale_alpha_discarded(super::DynamicImage::new_rgb16(1, 1));
test_grayscale_alpha_discarded(super::DynamicImage::new(1, 1, ColorType::Rgb16));
}

#[test]
fn test_grayscale_rgba16() {
test_grayscale_alpha_preserved(super::DynamicImage::new_rgba16(1, 1));
test_grayscale_alpha_preserved(super::DynamicImage::new(1, 1, ColorType::Rgba16));
}

#[test]
fn test_grayscale_rgb32f() {
test_grayscale_alpha_discarded(super::DynamicImage::new_rgb32f(1, 1));
test_grayscale_alpha_discarded(super::DynamicImage::new(1, 1, ColorType::Rgb32F));
}

#[test]
fn test_grayscale_rgba32f() {
test_grayscale_alpha_preserved(super::DynamicImage::new_rgba32f(1, 1));
test_grayscale_alpha_preserved(super::DynamicImage::new(1, 1, ColorType::Rgba32F));
}

#[test]
Expand Down
Loading