Skip to content

Commit

Permalink
image_ops takes responsibility of treat alpha channel
Browse files Browse the repository at this point in the history
  • Loading branch information
Hajime-san committed Sep 29, 2024
1 parent 8d8d726 commit fc4064e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 41 deletions.
17 changes: 5 additions & 12 deletions ext/canvas/image_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
))),
Expand All @@ -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),
}
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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),
}
}

Expand Down
45 changes: 16 additions & 29 deletions ext/canvas/op_create_image_bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),

// https://html.spec.whatwg.org/multipage/canvas.html#convert-from-premultiplied

// 2.
PremultiplyAlpha::Premultiply => {
process_premultiply_alpha(image, unmatch_color_handler)
match premultiply_alpha {
// 1.
PremultiplyAlpha::Default => Ok(image),

// https://html.spec.whatwg.org/multipage/canvas.html#convert-from-premultiplied

// 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)
}
}
}
Expand Down

0 comments on commit fc4064e

Please sign in to comment.