diff --git a/ext/canvas/image_ops.rs b/ext/canvas/image_ops.rs index 2d5017c233..cbe8b63174 100644 --- a/ext/canvas/image_ops.rs +++ b/ext/canvas/image_ops.rs @@ -89,13 +89,8 @@ where /// Premultiply the alpha channel of the image. pub(crate) fn premultiply_alpha( image: DynamicImage, - unmatch_color_handler: fn( - ColorType, - DynamicImage, - ) -> Result, ) -> Result { - let color = image.color(); - match color { + match image.color() { ColorType::La8 => Ok(DynamicImage::ImageLumaA8(process_premultiply_alpha( image.as_luma_alpha8().unwrap(), ))), @@ -108,7 +103,8 @@ pub(crate) fn premultiply_alpha( ColorType::Rgba16 => Ok(DynamicImage::ImageRgba16( process_premultiply_alpha(image.as_rgba16().unwrap()), )), - x => unmatch_color_handler(x, image), + // If the image does not have an alpha channel, return the image as is. + _ => Ok(image), } } @@ -218,10 +214,6 @@ where /// Invert the premultiplied alpha channel of the image. pub(crate) fn unpremultiply_alpha( image: DynamicImage, - unmatch_color_handler: fn( - ColorType, - DynamicImage, - ) -> Result, ) -> Result { match image.color() { ColorType::La8 => Ok(DynamicImage::ImageLumaA8( @@ -252,7 +244,8 @@ pub(crate) fn unpremultiply_alpha( image.into_rgba16() }, )), - x => unmatch_color_handler(x, image), + // If the image does not have an alpha channel, return the image as is. + _ => Ok(image), } } diff --git a/ext/canvas/op_create_image_bitmap.rs b/ext/canvas/op_create_image_bitmap.rs index 3ce5d13b9e..c11fb518d8 100644 --- a/ext/canvas/op_create_image_bitmap.rs +++ b/ext/canvas/op_create_image_bitmap.rs @@ -260,37 +260,24 @@ fn apply_premultiply_alpha( image_bitmap_source: &ImageBitmapSource, premultiply_alpha: &PremultiplyAlpha, ) -> Result { - let color = image.color(); - if !color.has_alpha() { - Ok(image) - } else { - fn unmatch_color_handler( - _: ColorType, - image: DynamicImage, - ) -> Result { - Ok(image) - } - match premultiply_alpha { - // 1. - PremultiplyAlpha::Default => Ok(image), + match premultiply_alpha { + // 1. + PremultiplyAlpha::Default => Ok(image), - // https://html.spec.whatwg.org/multipage/canvas.html#convert-from-premultiplied + // https://html.spec.whatwg.org/multipage/canvas.html#convert-from-premultiplied - // 2. - PremultiplyAlpha::Premultiply => { - process_premultiply_alpha(image, unmatch_color_handler) + // 2. + PremultiplyAlpha::Premultiply => process_premultiply_alpha(image), + // 3. + PremultiplyAlpha::None => { + // NOTE: It's not clear how to handle the case of ImageData. + // https://issues.chromium.org/issues/339759426 + // https://github.com/whatwg/html/issues/5365 + if *image_bitmap_source == ImageBitmapSource::ImageData { + return Ok(image); } - // 3. - PremultiplyAlpha::None => { - // NOTE: It's not clear how to handle the case of ImageData. - // https://issues.chromium.org/issues/339759426 - // https://github.com/whatwg/html/issues/5365 - if *image_bitmap_source == ImageBitmapSource::ImageData { - return Ok(image); - } - unpremultiply_alpha(image, unmatch_color_handler) - } + unpremultiply_alpha(image) } } }