From 8d8d726e00e8ea9f1c914a9ef96aa45ee2f804eb Mon Sep 17 00:00:00 2001 From: Hajime-san <41257923+Hajime-san@users.noreply.github.com> Date: Sun, 29 Sep 2024 13:42:13 +0900 Subject: [PATCH] avoid copying vector --- ext/canvas/image_ops.rs | 100 +++++++++++++++++++++++----------------- 1 file changed, 58 insertions(+), 42 deletions(-) diff --git a/ext/canvas/image_ops.rs b/ext/canvas/image_ops.rs index 4db7243195..2d5017c233 100644 --- a/ext/canvas/image_ops.rs +++ b/ext/canvas/image_ops.rs @@ -68,7 +68,6 @@ impl PremultiplyAlpha for Rgba { } } -// make public if needed fn process_premultiply_alpha(image: &I) -> ImageBuffer> where I: GenericImageView, @@ -98,16 +97,16 @@ pub(crate) fn premultiply_alpha( let color = image.color(); match color { ColorType::La8 => Ok(DynamicImage::ImageLumaA8(process_premultiply_alpha( - &image.to_luma_alpha8(), + image.as_luma_alpha8().unwrap(), ))), - ColorType::Rgba8 => Ok(DynamicImage::ImageRgba8( - process_premultiply_alpha(&image.to_rgba8()), - )), ColorType::La16 => Ok(DynamicImage::ImageLumaA16( - process_premultiply_alpha(&image.to_luma_alpha16()), + process_premultiply_alpha(image.as_luma_alpha16().unwrap()), + )), + ColorType::Rgba8 => Ok(DynamicImage::ImageRgba8( + process_premultiply_alpha(image.as_rgba8().unwrap()), )), ColorType::Rgba16 => Ok(DynamicImage::ImageRgba16( - process_premultiply_alpha(&image.to_rgba16()), + process_premultiply_alpha(image.as_rgba16().unwrap()), )), x => unmatch_color_handler(x, image), } @@ -187,7 +186,17 @@ impl UnpremultiplyAlpha for LumaA { } } -// make public if needed +fn is_premultiplied_alpha(image: &I) -> bool +where + I: GenericImageView, + P: Pixel + UnpremultiplyAlpha + 'static, + S: Primitive + 'static, +{ + image + .pixels() + .any(|(_, _, pixel)| pixel.is_premultiplied_alpha()) +} + fn process_unpremultiply_alpha(image: &I) -> ImageBuffer> where I: GenericImageView, @@ -197,17 +206,8 @@ where let (width, height) = image.dimensions(); let mut out = ImageBuffer::new(width, height); - let is_premultiplied_alpha = image - .pixels() - .any(|(_, _, pixel)| pixel.is_premultiplied_alpha()); - for (x, y, pixel) in image.pixels() { - let pixel = if is_premultiplied_alpha { - pixel.unpremultiply_alpha() - } else { - // return the original - pixel - }; + let pixel = pixel.unpremultiply_alpha(); out.put_pixel(x, y, pixel); } @@ -225,16 +225,32 @@ pub(crate) fn unpremultiply_alpha( ) -> Result { match image.color() { ColorType::La8 => Ok(DynamicImage::ImageLumaA8( - process_unpremultiply_alpha(&image.to_luma_alpha8()), - )), - ColorType::Rgba8 => Ok(DynamicImage::ImageRgba8( - process_unpremultiply_alpha(&image.to_rgba8()), + if is_premultiplied_alpha(image.as_luma_alpha8().unwrap()) { + process_unpremultiply_alpha(image.as_luma_alpha8().unwrap()) + } else { + image.into_luma_alpha8() + }, )), ColorType::La16 => Ok(DynamicImage::ImageLumaA16( - process_unpremultiply_alpha(&image.to_luma_alpha16()), + if is_premultiplied_alpha(image.as_luma_alpha16().unwrap()) { + process_unpremultiply_alpha(image.as_luma_alpha16().unwrap()) + } else { + image.into_luma_alpha16() + }, + )), + ColorType::Rgba8 => Ok(DynamicImage::ImageRgba8( + if is_premultiplied_alpha(image.as_rgba8().unwrap()) { + process_unpremultiply_alpha(image.as_rgba8().unwrap()) + } else { + image.into_rgba8() + }, )), ColorType::Rgba16 => Ok(DynamicImage::ImageRgba16( - process_unpremultiply_alpha(&image.to_rgba16()), + if is_premultiplied_alpha(image.as_rgba16().unwrap()) { + process_unpremultiply_alpha(image.as_rgba16().unwrap()) + } else { + image.into_rgba16() + }, )), x => unmatch_color_handler(x, image), } @@ -319,7 +335,6 @@ impl_transform_color_profile!(Rgb); impl_transform_color_profile!(Rgba); impl_transform_color_profile!(Rgba); -// make public if needed fn process_icc_profile_conversion( image: &I, color: ColorType, @@ -392,7 +407,7 @@ pub(crate) fn to_srgb_from_icc_profile( match color { ColorType::L8 => { Ok(DynamicImage::ImageLuma8(process_icc_profile_conversion( - &image.to_luma8(), + image.as_luma8().unwrap(), color, icc_profile, srgb_icc_profile, @@ -400,7 +415,7 @@ pub(crate) fn to_srgb_from_icc_profile( } ColorType::L16 => { Ok(DynamicImage::ImageLuma16(process_icc_profile_conversion( - &image.to_luma16(), + image.as_luma16().unwrap(), color, icc_profile, srgb_icc_profile, @@ -408,7 +423,7 @@ pub(crate) fn to_srgb_from_icc_profile( } ColorType::La8 => { Ok(DynamicImage::ImageLumaA8(process_icc_profile_conversion( - &image.to_luma_alpha8(), + image.as_luma_alpha8().unwrap(), color, icc_profile, srgb_icc_profile, @@ -416,7 +431,7 @@ pub(crate) fn to_srgb_from_icc_profile( } ColorType::La16 => { Ok(DynamicImage::ImageLumaA16(process_icc_profile_conversion( - &image.to_luma_alpha16(), + image.as_luma_alpha16().unwrap(), color, icc_profile, srgb_icc_profile, @@ -424,7 +439,7 @@ pub(crate) fn to_srgb_from_icc_profile( } ColorType::Rgb8 => { Ok(DynamicImage::ImageRgb8(process_icc_profile_conversion( - &image.to_rgb8(), + image.as_rgb8().unwrap(), color, icc_profile, srgb_icc_profile, @@ -432,7 +447,7 @@ pub(crate) fn to_srgb_from_icc_profile( } ColorType::Rgb16 => { Ok(DynamicImage::ImageRgb16(process_icc_profile_conversion( - &image.to_rgb16(), + image.as_rgb16().unwrap(), color, icc_profile, srgb_icc_profile, @@ -440,7 +455,7 @@ pub(crate) fn to_srgb_from_icc_profile( } ColorType::Rgba8 => { Ok(DynamicImage::ImageRgba8(process_icc_profile_conversion( - &image.to_rgba8(), + image.as_rgba8().unwrap(), color, icc_profile, srgb_icc_profile, @@ -448,7 +463,7 @@ pub(crate) fn to_srgb_from_icc_profile( } ColorType::Rgba16 => { Ok(DynamicImage::ImageRgba16(process_icc_profile_conversion( - &image.to_rgba16(), + image.as_rgba16().unwrap(), color, icc_profile, srgb_icc_profile, @@ -591,7 +606,6 @@ pub(crate) fn to_srgb_from_icc_profile( // } // } -// // make public if needed // fn process_srgb_to_display_p3(image: &I) -> ImageBuffer> // where // I: GenericImageView, @@ -620,21 +634,23 @@ pub(crate) fn to_srgb_from_icc_profile( // ) -> Result { // match image.color() { // // The conversion of the lumincance color types to the display-p3 color space is meaningless. -// ColorType::L8 => Ok(DynamicImage::ImageLuma8(image.to_luma8())), -// ColorType::L16 => Ok(DynamicImage::ImageLuma16(image.to_luma16())), -// ColorType::La8 => Ok(DynamicImage::ImageLumaA8(image.to_luma_alpha8())), -// ColorType::La16 => Ok(DynamicImage::ImageLumaA16(image.to_luma_alpha16())), +// ColorType::L8 => Ok(DynamicImage::ImageLuma8(image.into_luma8())), +// ColorType::L16 => Ok(DynamicImage::ImageLuma16(image.into_luma16())), +// ColorType::La8 => Ok(DynamicImage::ImageLumaA8(image.into_luma_alpha8())), +// ColorType::La16 => { +// Ok(DynamicImage::ImageLumaA16(image.into_luma_alpha16())) +// } // ColorType::Rgb8 => Ok(DynamicImage::ImageRgb8(process_srgb_to_display_p3( -// &image.to_rgb8(), +// image.as_rgb8().unwrap(), // ))), // ColorType::Rgb16 => Ok(DynamicImage::ImageRgb16( -// process_srgb_to_display_p3(&image.to_rgb16()), +// process_srgb_to_display_p3(image.as_rgb16().unwrap()), // )), // ColorType::Rgba8 => Ok(DynamicImage::ImageRgba8( -// process_srgb_to_display_p3(&image.to_rgba8()), +// process_srgb_to_display_p3(image.as_rgba8().unwrap()), // )), // ColorType::Rgba16 => Ok(DynamicImage::ImageRgba16( -// process_srgb_to_display_p3(&image.to_rgba16()), +// process_srgb_to_display_p3(image.as_rgba16().unwrap()), // )), // x => unmatch_color_handler(x, image), // }