1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-21 04:52:26 -05:00

image_ops takes responsibility of treat alpha channel

This commit is contained in:
Hajime-san 2024-09-29 13:53:19 +09:00
parent 8d8d726e00
commit fc4064e0d7
2 changed files with 19 additions and 39 deletions

View file

@ -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<DynamicImage, AnyError>,
) -> Result<DynamicImage, AnyError> {
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<DynamicImage, AnyError>,
) -> Result<DynamicImage, AnyError> {
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),
}
}

View file

@ -260,37 +260,24 @@ fn apply_premultiply_alpha(
image_bitmap_source: &ImageBitmapSource,
premultiply_alpha: &PremultiplyAlpha,
) -> Result<DynamicImage, AnyError> {
let color = image.color();
if !color.has_alpha() {
Ok(image)
} else {
fn unmatch_color_handler(
_: ColorType,
image: DynamicImage,
) -> Result<DynamicImage, AnyError> {
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)
}
}
}