use crate::utils::utils::webp_safe_malloc;
const FIX: u32 = 16;
const LFIX: i32 = 2;
const LUT_SIZE: i32 = (1 << (8 + LFIX)) - 1;
const DFIX: i32 = 0;
#[expect(
clippy::cast_possible_wrap,
reason = "the C mask is an int made from an unsigned shift"
)]
const CLIP_8B_MASK: i32 = (!0_u32 << (8 + DFIX)) as i32;
#[must_use]
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "the C conversion keeps the low eight bits of the descaled value"
)]
fn clip_8b(v: i32) -> u8 {
if (v & CLIP_8B_MASK) == 0 {
(v >> DFIX) as u8
} else if v < 0 {
0
} else {
255
}
}
#[must_use]
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "the C conversion keeps the low 16 bits, as two's complement for negative values"
)]
fn wrap16(value: i32) -> u16 {
value as u16
}
#[must_use]
#[expect(
clippy::cast_possible_truncation,
reason = "the C conversion keeps the low 16 bits"
)]
fn low16(value: u32) -> u16 {
value as u16
}
struct SmoothParams<'a> {
width: usize,
height: usize,
stride: usize,
row: i32,
data: &'a mut [u8],
src: usize,
dst: usize,
radius: i32,
scale: u32,
start: Vec<u16>,
cur: usize,
top: usize,
end: usize,
average: Vec<u16>,
correction: Vec<i16>,
num_levels: i32,
min: i32,
max: i32,
min_level_dist: i32,
}
fn v_filter(p: &mut SmoothParams<'_>) {
let w = p.width;
let mut sum: u16 = 0; for x in 0..w {
sum = sum.wrapping_add(u16::from(p.data[p.src + x]));
let new_value = p.start[p.top + x].wrapping_add(sum);
let out = p.end + x;
p.start[out] = new_value.wrapping_sub(p.start[p.cur + x]); p.start[p.cur + x] = new_value;
}
p.top = p.cur;
p.cur += w;
if p.cur == p.end {
p.cur = 0; }
if p.row >= 0 && p.row < i32::try_from(p.height).expect("the height fits in int") - 1 {
p.src += p.stride;
}
}
#[expect(
clippy::many_single_char_names,
reason = "the C names x, r, w and p of the horizontal filter"
)]
fn h_filter(p: &mut SmoothParams<'_>) {
let input = p.end;
let scale = p.scale;
let w = p.width;
let r = usize::try_from(p.radius).expect("the radius is positive");
let mut x = 0_usize;
while x <= r {
let delta = p.start[input + x + r - 1].wrapping_add(p.start[input + r - x]);
p.average[x] = low16((u32::from(delta).wrapping_mul(scale)) >> FIX);
x += 1;
}
while x + r < w {
let delta = p.start[input + x + r].wrapping_sub(p.start[input + x - r - 1]);
p.average[x] = low16((u32::from(delta).wrapping_mul(scale)) >> FIX);
x += 1;
}
while x < w {
let a = 2 * i32::from(p.start[input + w - 1]);
let b = i32::from(p.start[input + 2 * w - 2 - r - x]);
let c = i32::from(p.start[input + x - r - 1]);
let delta = wrap16(a - b - c);
p.average[x] = low16((u32::from(delta).wrapping_mul(scale)) >> FIX);
x += 1;
}
}
fn apply_filter(p: &mut SmoothParams<'_>) {
let w = p.width;
let dst = p.dst;
for x in 0..w {
let v = i32::from(p.data[dst + x]);
if v < p.max && v > p.min {
let index = i32::from(p.average[x]) - (v << LFIX);
let table =
usize::try_from(LUT_SIZE + index).expect("the correction index is in the table");
let c = (v << DFIX) + i32::from(p.correction[table]);
p.data[dst + x] = clip_8b(c);
}
}
p.dst += p.stride; }
fn init_correction_lut(lut: &mut [i16], min_dist: i32) {
let threshold1 = min_dist << LFIX;
let threshold2 = (3 * threshold1) >> 2;
let max_threshold = threshold2 << DFIX;
let delta = threshold1 - threshold2;
for i in 1..=LUT_SIZE {
let c = if i <= threshold2 {
i << DFIX
} else if i < threshold1 {
max_threshold * (threshold1 - i) / delta
} else {
0
};
let c = c >> LFIX;
let plus = usize::try_from(LUT_SIZE + i).expect("the table index is positive");
let minus = usize::try_from(LUT_SIZE - i).expect("the table index is positive");
lut[plus] = i16::try_from(c).expect("a correction fits in 16 bits");
lut[minus] = i16::try_from(-c).expect("a correction fits in 16 bits");
}
lut[usize::try_from(LUT_SIZE).expect("the table centre is positive")] = 0;
}
fn count_levels(p: &mut SmoothParams<'_>) {
let mut used_levels = [false; 256];
p.min = 255;
p.max = 0;
for j in 0..p.height {
let row = p.src + j * p.stride;
for i in 0..p.width {
let v = i32::from(p.data[row + i]);
if v < p.min {
p.min = v;
}
if v > p.max {
p.max = v;
}
used_levels[p.data[row + i] as usize] = true;
}
}
p.min_level_dist = p.max - p.min;
let mut last_level = -1_i32;
for (level, &used) in used_levels.iter().enumerate() {
let i = i32::try_from(level).expect("a level fits in int");
if used {
p.num_levels += 1;
if last_level >= 0 {
let level_dist = i - last_level;
if level_dist < p.min_level_dist {
p.min_level_dist = level_dist;
}
}
last_level = i;
}
}
}
fn init_params(
data: &mut [u8],
width: i32,
height: i32,
stride: i32,
radius: i32,
) -> Option<SmoothParams<'_>> {
let r_size = 2 * radius + 1; let width_u = usize::try_from(width).expect("the width is positive");
let size_scratch_m = usize::try_from(r_size + 1).expect("the kernel is positive") * width_u * 2;
let size_m = width_u * 2;
let size_lut = (1 + 2 * usize::try_from(LUT_SIZE).expect("the table size is positive")) * 2;
let total_size = size_scratch_m + size_m + size_lut;
webp_safe_malloc::<u8>(1, total_size)?;
let rows = usize::try_from(r_size).expect("the kernel is positive");
let mut p = SmoothParams {
width: width_u,
height: usize::try_from(height).expect("the height is positive"),
stride: usize::try_from(stride).expect("the stride is positive"),
row: -radius,
data,
src: 0,
dst: 0,
radius,
scale: u32::try_from(
(1_i32 << (i32::try_from(FIX).expect("FIX fits in int") + LFIX)) / (r_size * r_size),
)
.expect("the normalization is positive"),
start: vec![0_u16; (rows + 1) * width_u],
cur: 0,
top: 0,
end: rows * width_u,
average: vec![0_u16; width_u],
correction: vec![
0_i16;
1 + 2 * usize::try_from(LUT_SIZE).expect("the table size is positive")
],
num_levels: 0,
min: 0,
max: 0,
min_level_dist: 0,
};
p.top = p.end - width_u;
count_levels(&mut p);
init_correction_lut(&mut p.correction, p.min_level_dist);
Some(p)
}
#[must_use]
pub fn webp_dequantize_levels(
data: Option<&mut [u8]>,
width: i32,
height: i32,
stride: i32,
strength: i32,
) -> bool {
let mut radius = 4 * strength / 100;
if !(0..=100).contains(&strength) {
return false;
}
let Some(data) = data else {
return false;
};
if width <= 0 || height <= 0 {
return false; }
if 2 * radius + 1 > width {
radius = (width - 1) >> 1;
}
if 2 * radius + 1 > height {
radius = (height - 1) >> 1;
}
if radius > 0 {
let Some(mut p) = init_params(data, width, height, stride, radius) else {
return false;
};
if p.num_levels > 2 {
while p.row < i32::try_from(p.height).expect("the height fits in int") {
v_filter(&mut p); if p.row >= p.radius {
h_filter(&mut p);
apply_filter(&mut p);
}
p.row += 1;
}
}
}
true
}