const MFIX: u32 = 24;
const HALF: u32 = (1 << MFIX) >> 1;
const KINV_255: u32 = (1 << MFIX) / 255;
const MULTIPLIER_SCALE: u32 = 32897;
#[expect(
clippy::cast_possible_truncation,
reason = "a C conversion to uint8_t keeps the low eight bits"
)]
#[must_use]
fn low_byte(value: u32) -> u8 {
value as u8
}
fn mult(x: u8, scale: u32) -> u32 {
let v = (u32::from(x).wrapping_mul(scale).wrapping_add(HALF)) >> MFIX;
debug_assert!(v <= 255); v
}
#[must_use]
fn get_scale(a: u32, inverse: bool) -> u32 {
if inverse {
(255 << MFIX) / a
} else {
a * KINV_255
}
}
pub fn webp_mult_argb_row_c(ptr: &mut [u32], width: i32, inverse: bool) {
let width = usize::try_from(width).unwrap_or(0);
for pixel in &mut ptr[..width] {
let argb = *pixel;
if argb < 0xff00_0000 {
if argb <= 0x00ff_ffff {
*pixel = 0;
} else {
let alpha = (argb >> 24) & 0xff;
let scale = get_scale(alpha, inverse);
let mut out = argb & 0xff00_0000;
out |= mult(low_byte(argb), scale);
out |= mult(low_byte(argb >> 8), scale) << 8;
out |= mult(low_byte(argb >> 16), scale) << 16;
*pixel = out;
}
}
}
}
pub fn webp_mult_row_c(ptr: &mut [u8], alpha: &[u8], width: i32, inverse: bool) {
let width = usize::try_from(width).unwrap_or(0);
for x in 0..width {
let a = u32::from(alpha[x]);
if a != 255 {
if a == 0 {
ptr[x] = 0;
} else {
let scale = get_scale(a, inverse);
ptr[x] = low_byte(mult(ptr[x], scale));
}
}
}
}
pub fn webp_mult_argb_row(ptr: &mut [u32], width: i32, inverse: bool) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::alpha_processing_sse2::mult_argb_row(ptr, width, inverse);
}
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
webp_mult_argb_row_c(ptr, width, inverse);
}
pub fn webp_mult_row(ptr: &mut [u8], alpha: &[u8], width: i32, inverse: bool) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::alpha_processing_sse2::mult_row(ptr, alpha, width, inverse);
}
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
webp_mult_row_c(ptr, alpha, width, inverse);
}
pub fn webp_mult_argb_rows(ptr: &mut [u32], stride: i32, width: i32, num_rows: i32, inverse: bool) {
let stride = usize::try_from(stride).expect("the stride is not negative");
for n in 0..usize::try_from(num_rows).unwrap_or(0) {
webp_mult_argb_row(&mut ptr[n * stride..], width, inverse);
}
}
pub fn webp_mult_rows(
ptr: &mut [u8],
stride: i32,
alpha: &[u8],
alpha_stride: i32,
width: i32,
num_rows: i32,
inverse: bool,
) {
let stride = usize::try_from(stride).expect("the stride is not negative");
let alpha_stride = usize::try_from(alpha_stride).expect("the stride is not negative");
for n in 0..usize::try_from(num_rows).unwrap_or(0) {
webp_mult_row(
&mut ptr[n * stride..],
&alpha[n * alpha_stride..],
width,
inverse,
);
}
}
#[must_use]
fn premultiply(x: u8, m: u32) -> u8 {
low_byte((u32::from(x) * m) >> 23)
}
pub fn apply_alpha_multiply_c(rgba: &mut [u8], alpha_first: bool, w: i32, h: i32, stride: i32) {
let w = usize::try_from(w).unwrap_or(0);
let stride = usize::try_from(stride).expect("the stride is not negative");
let rgb_offset = usize::from(alpha_first);
let alpha_offset = if alpha_first { 0 } else { 3 };
for row in 0..usize::try_from(h).unwrap_or(0) {
let base = row * stride;
for i in 0..w {
let a = u32::from(rgba[base + alpha_offset + 4 * i]);
if a != 0xff {
let mult = a * MULTIPLIER_SCALE;
let rgb = base + rgb_offset + 4 * i;
rgba[rgb] = premultiply(rgba[rgb], mult);
rgba[rgb + 1] = premultiply(rgba[rgb + 1], mult);
rgba[rgb + 2] = premultiply(rgba[rgb + 2], mult);
}
}
}
}
pub fn webp_apply_alpha_multiply(rgba: &mut [u8], alpha_first: bool, w: i32, h: i32, stride: i32) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::alpha_processing_sse2::apply_alpha_multiply(
rgba,
alpha_first,
w,
h,
stride,
);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::alpha_processing_neon::apply_alpha_multiply(
rgba,
alpha_first,
w,
h,
stride,
);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
apply_alpha_multiply_c(rgba, alpha_first, w, h, stride);
}
#[must_use]
fn dither_hi(x: u8) -> u8 {
(x & 0xf0) | (x >> 4)
}
#[must_use]
fn dither_lo(x: u8) -> u8 {
(x & 0x0f) | (x << 4)
}
#[must_use]
fn multiply(x: u8, m: u32) -> u8 {
low_byte((u32::from(x) * m) >> 16)
}
#[expect(
clippy::many_single_char_names,
reason = "the C names w, h, r, g, b and a of the pixel channels"
)]
fn apply_alpha_multiply4444_c(
rgba4444: &mut [u8],
w: i32,
h: i32,
stride: i32,
rg_byte_pos: usize,
) {
let w = usize::try_from(w).unwrap_or(0);
let stride = usize::try_from(stride).expect("the stride is not negative");
for row in 0..usize::try_from(h).unwrap_or(0) {
let base = row * stride;
for i in 0..w {
let rg_index = base + 2 * i + rg_byte_pos;
let ba_index = base + 2 * i + (rg_byte_pos ^ 1);
let rg = rgba4444[rg_index];
let ba = rgba4444[ba_index];
let a = ba & 0x0f;
let mult = u32::from(a) * 0x1111; let r = multiply(dither_hi(rg), mult);
let g = multiply(dither_lo(rg), mult);
let b = multiply(dither_hi(ba), mult);
rgba4444[rg_index] = (r & 0xf0) | ((g >> 4) & 0x0f);
rgba4444[ba_index] = (b & 0xf0) | a;
}
}
}
fn apply_alpha_multiply_16b_c(rgba4444: &mut [u8], w: i32, h: i32, stride: i32) {
apply_alpha_multiply4444_c(rgba4444, w, h, stride, 0);
}
pub fn webp_apply_alpha_multiply4444(rgba4444: &mut [u8], w: i32, h: i32, stride: i32) {
apply_alpha_multiply_16b_c(rgba4444, w, h, stride);
}
pub fn dispatch_alpha_c(
alpha: &[u8],
alpha_stride: i32,
width: i32,
height: i32,
dst: &mut [u8],
dst_stride: i32,
) -> bool {
let width = usize::try_from(width).unwrap_or(0);
let alpha_stride = usize::try_from(alpha_stride).expect("the stride is not negative");
let dst_stride = usize::try_from(dst_stride).expect("the stride is not negative");
let mut alpha_mask = 0xff_u8;
for j in 0..usize::try_from(height).unwrap_or(0) {
for i in 0..width {
let alpha_value = alpha[j * alpha_stride + i];
dst[j * dst_stride + 4 * i] = alpha_value;
alpha_mask &= alpha_value;
}
}
alpha_mask != 0xff
}
pub fn webp_dispatch_alpha(
alpha: &[u8],
alpha_stride: i32,
width: i32,
height: i32,
dst: &mut [u8],
dst_stride: i32,
) -> bool {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::alpha_processing_sse2::dispatch_alpha(
alpha,
alpha_stride,
width,
height,
dst,
dst_stride,
);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::alpha_processing_neon::dispatch_alpha(
alpha,
alpha_stride,
width,
height,
dst,
dst_stride,
);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
dispatch_alpha_c(alpha, alpha_stride, width, height, dst, dst_stride)
}
pub fn dispatch_alpha_to_green_c(
alpha: &[u8],
alpha_stride: i32,
width: i32,
height: i32,
dst: &mut [u32],
dst_stride: i32,
) {
let width = usize::try_from(width).unwrap_or(0);
let alpha_stride = usize::try_from(alpha_stride).expect("the stride is not negative");
let dst_stride = usize::try_from(dst_stride).expect("the stride is not negative");
for j in 0..usize::try_from(height).unwrap_or(0) {
for i in 0..width {
dst[j * dst_stride + i] = u32::from(alpha[j * alpha_stride + i]) << 8; }
}
}
pub fn webp_dispatch_alpha_to_green(
alpha: &[u8],
alpha_stride: i32,
width: i32,
height: i32,
dst: &mut [u32],
dst_stride: i32,
) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::alpha_processing_sse2::dispatch_alpha_to_green(
alpha,
alpha_stride,
width,
height,
dst,
dst_stride,
);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::alpha_processing_neon::dispatch_alpha_to_green(
alpha,
alpha_stride,
width,
height,
dst,
dst_stride,
);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
dispatch_alpha_to_green_c(alpha, alpha_stride, width, height, dst, dst_stride);
}
pub fn extract_alpha_c(
argb: &[u8],
argb_stride: i32,
width: i32,
height: i32,
alpha: &mut [u8],
alpha_stride: i32,
) -> bool {
let width = usize::try_from(width).unwrap_or(0);
let argb_stride = usize::try_from(argb_stride).expect("the stride is not negative");
let alpha_stride = usize::try_from(alpha_stride).expect("the stride is not negative");
let mut alpha_mask = 0xff_u8;
for j in 0..usize::try_from(height).unwrap_or(0) {
for i in 0..width {
let alpha_value = argb[j * argb_stride + 4 * i];
alpha[j * alpha_stride + i] = alpha_value;
alpha_mask &= alpha_value;
}
}
alpha_mask == 0xff
}
pub fn webp_extract_alpha(
argb: &[u8],
argb_stride: i32,
width: i32,
height: i32,
alpha: &mut [u8],
alpha_stride: i32,
) -> bool {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 && features.sse4_1 {
return crate::dsp::alpha_processing_sse41::extract_alpha(
argb,
argb_stride,
width,
height,
alpha,
alpha_stride,
);
}
if features.sse2 {
return crate::dsp::alpha_processing_sse2::extract_alpha(
argb,
argb_stride,
width,
height,
alpha,
alpha_stride,
);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::alpha_processing_neon::extract_alpha(
argb,
argb_stride,
width,
height,
alpha,
alpha_stride,
);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
extract_alpha_c(argb, argb_stride, width, height, alpha, alpha_stride)
}
pub fn extract_green_c(argb: &[u32], alpha: &mut [u8], size: i32) {
let size = usize::try_from(size).unwrap_or(0);
for i in 0..size {
alpha[i] = low_byte(argb[i] >> 8);
}
}
pub fn webp_extract_green(argb: &[u32], alpha: &mut [u8], size: i32) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::alpha_processing_sse2::extract_green(argb, alpha, size);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::alpha_processing_neon::extract_green(argb, alpha, size);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
extract_green_c(argb, alpha, size);
}
#[must_use]
fn make_argb32(a: u32, r: u32, g: u32, b: u32) -> u32 {
(a << 24) | (r << 16) | (g << 8) | b
}
pub fn pack_rgb_c(r: &[u8], g: &[u8], b: &[u8], len: i32, step: i32, out: &mut [u32]) {
let len = usize::try_from(len).unwrap_or(0);
let step = usize::try_from(step).expect("the step is not negative");
let mut offset = 0_usize;
for pixel in &mut out[..len] {
*pixel = make_argb32(
0xff,
u32::from(r[offset]),
u32::from(g[offset]),
u32::from(b[offset]),
);
offset += step;
}
}
pub fn webp_pack_rgb(r: &[u8], g: &[u8], b: &[u8], len: i32, step: i32, out: &mut [u32]) {
pack_rgb_c(r, g, b, len, step, out);
}
fn has_alpha8b_c(src: &[u8], length: i32) -> bool {
let length = usize::try_from(length).unwrap_or(0);
src[..length].iter().any(|&value| value != 0xff)
}
#[must_use]
pub fn webp_has_alpha8b(src: &[u8], length: i32) -> bool {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::alpha_processing_sse2::has_alpha8b(src, length);
}
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
has_alpha8b_c(src, length)
}
fn has_alpha32b_c(src: &[u8], length: i32) -> bool {
let length = usize::try_from(length).unwrap_or(0);
(0..length).any(|x| src[4 * x] != 0xff)
}
#[must_use]
pub fn webp_has_alpha32b(src: &[u8], length: i32) -> bool {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::alpha_processing_sse2::has_alpha32b(src, length);
}
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
has_alpha32b_c(src, length)
}
fn alpha_replace_c(src: &mut [u32], length: i32, color: u32) {
let length = usize::try_from(length).unwrap_or(0);
for pixel in &mut src[..length] {
if *pixel >> 24 == 0 {
*pixel = color;
}
}
}
pub fn webp_alpha_replace(src: &mut [u32], length: i32, color: u32) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::alpha_processing_sse2::alpha_replace(src, length, color);
}
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
alpha_replace_c(src, length, color);
}