diff --git a/components/pixels/lib.rs b/components/pixels/lib.rs index 8b002e16190..c6f85348df1 100644 --- a/components/pixels/lib.rs +++ b/components/pixels/lib.rs @@ -195,9 +195,13 @@ pub fn rgba8_premultiply_inplace(pixels: &mut [u8]) -> bool { is_opaque } +/// Returns a*b/255, rounding any fractional bits to nearest integer +/// to reduce the loss of precision after multiple consequence alpha +/// (un)premultiply operations. #[inline(always)] pub fn multiply_u8_color(a: u8, b: u8) -> u8 { - (a as u32 * b as u32 / 255) as u8 + let c = a as u32 * b as u32 + 128; + ((c + (c >> 8)) >> 8) as u8 } pub fn clip( diff --git a/tests/wpt/meta/html/canvas/element/manual/imagebitmap/createImageBitmap-premultiplyAlpha.html.ini b/tests/wpt/meta/html/canvas/element/manual/imagebitmap/createImageBitmap-premultiplyAlpha.html.ini deleted file mode 100644 index a6e6c78a2ad..00000000000 --- a/tests/wpt/meta/html/canvas/element/manual/imagebitmap/createImageBitmap-premultiplyAlpha.html.ini +++ /dev/null @@ -1,9 +0,0 @@ -[createImageBitmap-premultiplyAlpha.html] - [createImageBitmap: from Canvas2D, unpremultiplied, drawn to canvas] - expected: FAIL - - [createImageBitmap: from Canvas2D willReadFrequently:true, unpremultiplied, drawn to canvas] - expected: FAIL - - [createImageBitmap: from Canvas2D willReadFrequently:false, unpremultiplied, drawn to canvas] - expected: FAIL