From 022a95a2cd55617538f2dc841de3a76ef12ad8d9 Mon Sep 17 00:00:00 2001 From: Shalvin Date: Mon, 30 Sep 2024 21:39:27 +0200 Subject: [PATCH] Optimized unmultiply_inplace to remove fp ops (#33582) Signed-off-by: Shalvin Deo --- components/pixels/lib.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/components/pixels/lib.rs b/components/pixels/lib.rs index e90df8e32ea..339b0f9222e 100644 --- a/components/pixels/lib.rs +++ b/components/pixels/lib.rs @@ -199,10 +199,20 @@ pub fn detect_image_format(buffer: &[u8]) -> Result { pub fn unmultiply_inplace(pixels: &mut [u8]) { for rgba in pixels.chunks_mut(4) { - let a = (rgba[3] as f32) / 255.0; - rgba[0] = (rgba[0] as f32 / a) as u8; - rgba[1] = (rgba[1] as f32 / a) as u8; - rgba[2] = (rgba[2] as f32 / a) as u8; + let a = rgba[3] as u32; + let mut b = rgba[2] as u32; + let mut g = rgba[1] as u32; + let mut r = rgba[0] as u32; + + if a > 0 { + r = r * 255 / a; + g = g * 255 / a; + b = b * 255 / a; + + rgba[2] = b as u8; + rgba[1] = g as u8; + rgba[0] = r as u8; + } } }