pixels: improve performance of unmultiply using precomputed table

Signed-off-by: Euclid Ye <yezhizhenjiakang@gmail.com>
This commit is contained in:
Euclid Ye 2025-02-06 18:00:08 +08:00
parent e7a6691628
commit cb3d46c772
2 changed files with 4113 additions and 18 deletions

View file

@ -11,6 +11,8 @@ use ipc_channel::ipc::IpcSharedMemory;
use log::debug; use log::debug;
use malloc_size_of_derive::MallocSizeOf; use malloc_size_of_derive::MallocSizeOf;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
mod unpremultiplytable;
use unpremultiplytable::UNPREMULTIPLY_TABLE;
use webrender_api::ImageKey; use webrender_api::ImageKey;
#[derive(Clone, Copy, Debug, Deserialize, Eq, MallocSizeOf, PartialEq, Serialize)] #[derive(Clone, Copy, Debug, Deserialize, Eq, MallocSizeOf, PartialEq, Serialize)]
@ -199,25 +201,16 @@ pub fn detect_image_format(buffer: &[u8]) -> Result<ImageFormat, &str> {
pub fn unmultiply_inplace<const SWAP_RB: bool>(pixels: &mut [u8]) { pub fn unmultiply_inplace<const SWAP_RB: bool>(pixels: &mut [u8]) {
for rgba in pixels.chunks_mut(4) { for rgba in pixels.chunks_mut(4) {
let a = rgba[3] as u32; let a = rgba[3] as usize;
let mut b = rgba[2] as u32;
let mut g = rgba[1] as u32;
let mut r = rgba[0] as u32;
if a > 0 { if SWAP_RB {
r = r * 255 / a; rgba[0] = UNPREMULTIPLY_TABLE[256 * a + rgba[2] as usize];
g = g * 255 / a; rgba[1] = UNPREMULTIPLY_TABLE[256 * a + rgba[1] as usize];
b = b * 255 / a; rgba[2] = UNPREMULTIPLY_TABLE[256 * a + rgba[0] as usize];
} else {
if SWAP_RB { rgba[0] = UNPREMULTIPLY_TABLE[256 * a + rgba[0] as usize];
rgba[2] = r as u8; rgba[1] = UNPREMULTIPLY_TABLE[256 * a + rgba[1] as usize];
rgba[1] = g as u8; rgba[2] = UNPREMULTIPLY_TABLE[256 * a + rgba[2] as usize];
rgba[0] = b as u8;
} else {
rgba[2] = b as u8;
rgba[1] = g as u8;
rgba[0] = r as u8;
}
} }
} }
} }

File diff suppressed because it is too large Load diff