use crate::dsp::lossless_common::vp8l_near_lossless_bits;
use crate::enc::backward_references_enc::to_index;
use crate::enc::picture_enc::argb_samples;
use crate::utils::utils::webp_safe_malloc;
use crate::webp::encode::WebPPicture;
const MIN_DIM_FOR_NEAR_LOSSLESS: i32 = 64;
const MAX_LIMIT_BITS: i32 = 5;
fn find_closest_discretized(a: u32, bits: i32) -> u32 {
let mask = (1_u32 << bits) - 1;
let biased = a + (mask >> 1) + ((a >> bits) & 1);
debug_assert!(bits > 0);
if biased > 0xff {
return 0xff;
}
biased & !mask
}
fn closest_discretized_argb(a: u32, bits: i32) -> u32 {
(find_closest_discretized(a >> 24, bits) << 24)
| (find_closest_discretized((a >> 16) & 0xff, bits) << 16)
| (find_closest_discretized((a >> 8) & 0xff, bits) << 8)
| find_closest_discretized(a & 0xff, bits)
}
#[expect(clippy::cast_possible_wrap, reason = "the channels are below 256")]
fn is_near(a: u32, b: u32, limit: i32) -> bool {
for k in 0..4 {
let delta = ((a >> (k * 8)) & 0xff) as i32 - ((b >> (k * 8)) & 0xff) as i32;
if delta >= limit || delta <= -limit {
return false;
}
}
true
}
fn is_smooth(
rows: &[u32],
prev_row: usize,
curr_row: usize,
next_row: usize,
ix: usize,
limit: i32,
) -> bool {
is_near(rows[curr_row + ix], rows[curr_row + ix - 1], limit)
&& is_near(rows[curr_row + ix], rows[curr_row + ix + 1], limit)
&& is_near(rows[curr_row + ix], rows[prev_row + ix], limit)
&& is_near(rows[curr_row + ix], rows[next_row + ix], limit)
}
fn source<'s>(argb_src: Option<&'s [u32]>, argb_dst: &'s [u32]) -> &'s [u32] {
argb_src.unwrap_or(argb_dst)
}
fn near_lossless(
xsize: i32,
ysize: i32,
argb_src: Option<&[u32]>,
stride: i32,
limit_bits: i32,
copy_buffer: &mut [u32],
argb_dst: &mut [u32],
) {
let width = to_index(xsize);
let stride = to_index(stride);
let limit = 1 << limit_bits;
let mut prev_row: usize = 0;
let mut curr_row: usize = width;
let mut next_row: usize = 2 * width;
copy_buffer[curr_row..curr_row + width].copy_from_slice(&source(argb_src, argb_dst)[..width]);
copy_buffer[next_row..next_row + width]
.copy_from_slice(&source(argb_src, argb_dst)[stride..stride + width]);
for y in 0..to_index(ysize) {
let src = y * stride;
let dst = y * width;
if y == 0 || y == to_index(ysize) - 1 {
if let Some(argb_src) = argb_src {
argb_dst[dst..dst + width].copy_from_slice(&argb_src[src..src + width]);
}
} else {
let next = src + stride;
copy_buffer[next_row..next_row + width]
.copy_from_slice(&source(argb_src, argb_dst)[next..next + width]);
if let Some(argb_src) = argb_src {
argb_dst[dst] = argb_src[src];
argb_dst[dst + width - 1] = argb_src[src + width - 1];
}
for x in 1..width - 1 {
argb_dst[dst + x] =
if is_smooth(copy_buffer, prev_row, curr_row, next_row, x, limit) {
copy_buffer[curr_row + x]
} else {
closest_discretized_argb(copy_buffer[curr_row + x], limit_bits)
};
}
}
let temp = prev_row;
prev_row = curr_row;
curr_row = next_row;
next_row = temp;
}
}
#[must_use]
pub fn vp8_apply_near_lossless(
picture: &WebPPicture<'_>,
quality: i32,
argb_dst: &mut [u32],
) -> bool {
let xsize = picture.width;
let ysize = picture.height;
let stride = picture.argb_stride;
let Some(mut copy_buffer) = webp_safe_malloc::<u32>(i64::from(xsize * 3).cast_unsigned(), 4)
else {
return false;
};
copy_buffer.resize(to_index(xsize * 3), 0);
let limit_bits = vp8l_near_lossless_bits(quality);
debug_assert!(limit_bits > 0);
debug_assert!(limit_bits <= MAX_LIMIT_BITS);
let argb = argb_samples(
&picture.argb_storage,
picture.argb.expect("the picture has an ARGB plane"),
);
if (xsize < MIN_DIM_FOR_NEAR_LOSSLESS && ysize < MIN_DIM_FOR_NEAR_LOSSLESS) || ysize < 3 {
let width = to_index(xsize);
for i in 0..to_index(ysize) {
let src = i * to_index(picture.argb_stride);
argb_dst[i * width..(i + 1) * width].copy_from_slice(&argb[src..src + width]);
}
return true;
}
near_lossless(
xsize,
ysize,
Some(argb),
stride,
limit_bits,
&mut copy_buffer,
argb_dst,
);
let mut i = limit_bits - 1;
while i != 0 {
near_lossless(xsize, ysize, None, xsize, i, &mut copy_buffer, argb_dst);
i -= 1;
}
true
}