servo/components/pixels
Andrei Volykhin d8c552f3ab
pixels: Multiply by alpha with less loss of precision (#37503)
Integer color components representation has performance and memory
storage benefits
but suffers from precision loss after multiple consequence alpha
(un)premultiply operations.

Rounding any fractional bits (to the nearest integer) during alpha
multiplication should reduce the loss of precision.
Expensive division will be replaced by multiplication and bits shift.

https://research.swtch.com/divmult

https://docs.google.com/document/d/1tNrMWShq55rfltcZxAx1N-6f82Dt7MWLDHm-5GQVEnE

Other browsers and graphics libraries have the similar approach:

- Chromium (Skia):
https://github.com/google/skia/blob/main/include/private/base/SkMath.h#L73
- Firefox:
https://github.com/mozilla/gecko-dev/blob/master/gfx/2d/Swizzle.cpp#L276
- Servo (Raqote):
https://github.com/jrmuizel/sw-composite/blob/master/src/lib.rs#L878

Testing: Improvements in the following WPT test
-
html/canvas/element/manual/imagebitmap/createImageBitmap-premultiplyAlpha.html

Signed-off-by: Andrei Volykhin <andrei.volykhin@gmail.com>
2025-06-18 06:52:42 +00:00
..
tests imagebitmap: Crop bitmap data with formatting (#37397) 2025-06-16 12:09:04 +00:00
benches.rs Update pixels::unmultiply_inplace to support RB swap and use it in canvas_state (#35313) 2025-02-06 05:02:49 +00:00
Cargo.toml Organize component Cargo.toml dependencies (#36224) 2025-03-30 19:03:54 +00:00
lib.rs pixels: Multiply by alpha with less loss of precision (#37503) 2025-06-18 06:52:42 +00:00