use crate::dsp::dsp::WEBP_FILTER_TYPE;
pub type WebPFilterFunc = fn(&[u8], i32, i32, i32, &mut [u8]);
pub type WebPUnfilterFunc = fn(Option<&[u8]>, &[u8], &mut [u8], i32);
fn predict_line_c(src: &[u8], pred: &[u8], dst: &mut [u8], length: usize) {
for i in 0..length {
dst[i] = src[i].wrapping_sub(pred[i]);
}
}
#[must_use]
fn gradient_predictor_c(a: u8, b: u8, c: u8) -> i32 {
let g = i32::from(a) + i32::from(b) - i32::from(c);
if (g & !0xff) == 0 {
g
} else if g < 0 {
0
} else {
255
}
}
#[must_use]
fn predictor_byte(pred: i32) -> u8 {
u8::try_from(pred).expect("the gradient predictor is a byte")
}
fn do_horizontal_filter_c(input: &[u8], width: i32, height: i32, stride: i32, out: &mut [u8]) {
let width = usize::try_from(width).expect("the width is positive");
let height = usize::try_from(height).expect("the height is positive");
let stride = usize::try_from(stride).expect("the stride is not negative");
out[0] = input[0];
predict_line_c(&input[1..], &input[0..], &mut out[1..], width - 1);
for row in 1..height {
let cur = row * stride;
let above = (row - 1) * stride;
predict_line_c(&input[cur..], &input[above..], &mut out[cur..], 1);
predict_line_c(
&input[cur + 1..],
&input[cur..],
&mut out[cur + 1..],
width - 1,
);
}
}
fn do_vertical_filter_c(input: &[u8], width: i32, height: i32, stride: i32, out: &mut [u8]) {
let width = usize::try_from(width).expect("the width is positive");
let height = usize::try_from(height).expect("the height is positive");
let stride = usize::try_from(stride).expect("the stride is not negative");
out[0] = input[0];
predict_line_c(&input[1..], &input[0..], &mut out[1..], width - 1);
for row in 1..height {
let cur = row * stride;
let above = (row - 1) * stride;
predict_line_c(&input[cur..], &input[above..], &mut out[cur..], width);
}
}
fn do_gradient_filter_c(input: &[u8], width: i32, height: i32, stride: i32, out: &mut [u8]) {
let width = usize::try_from(width).expect("the width is positive");
let height = usize::try_from(height).expect("the height is positive");
let stride = usize::try_from(stride).expect("the stride is not negative");
out[0] = input[0];
predict_line_c(&input[1..], &input[0..], &mut out[1..], width - 1);
for row in 1..height {
let cur = row * stride;
let above = (row - 1) * stride;
predict_line_c(&input[cur..], &input[above..], &mut out[cur..], 1);
for w in 1..width {
let pred =
gradient_predictor_c(input[cur + w - 1], input[above + w], input[above + w - 1]);
out[cur + w] = input[cur + w].wrapping_sub(predictor_byte(pred));
}
}
}
pub fn horizontal_filter_c(
data: &[u8],
width: i32,
height: i32,
stride: i32,
filtered_data: &mut [u8],
) {
do_horizontal_filter_c(data, width, height, stride, filtered_data);
}
pub fn vertical_filter_c(
data: &[u8],
width: i32,
height: i32,
stride: i32,
filtered_data: &mut [u8],
) {
do_vertical_filter_c(data, width, height, stride, filtered_data);
}
pub fn gradient_filter_c(
data: &[u8],
width: i32,
height: i32,
stride: i32,
filtered_data: &mut [u8],
) {
do_gradient_filter_c(data, width, height, stride, filtered_data);
}
fn none_unfilter_c(_prev: Option<&[u8]>, input: &[u8], out: &mut [u8], width: i32) {
let width = usize::try_from(width).unwrap_or(0);
out[..width].copy_from_slice(&input[..width]);
}
#[expect(
clippy::similar_names,
reason = "the C names prev and pred of the reconstruction"
)]
pub fn horizontal_unfilter_c(prev: Option<&[u8]>, input: &[u8], out: &mut [u8], width: i32) {
let width = usize::try_from(width).unwrap_or(0);
let mut pred = prev.map_or(0, |prev| prev[0]);
for i in 0..width {
out[i] = pred.wrapping_add(input[i]);
pred = out[i];
}
}
pub fn vertical_unfilter_c(prev: Option<&[u8]>, input: &[u8], out: &mut [u8], width: i32) {
match prev {
None => horizontal_unfilter_c(None, input, out, width),
Some(prev) => {
let width = usize::try_from(width).unwrap_or(0);
for i in 0..width {
out[i] = prev[i].wrapping_add(input[i]);
}
}
}
}
fn gradient_unfilter_c(prev: Option<&[u8]>, input: &[u8], out: &mut [u8], width: i32) {
match prev {
None => horizontal_unfilter_c(None, input, out, width),
Some(prev) => {
let width = usize::try_from(width).unwrap_or(0);
let mut top = prev[0];
let mut top_left = top;
let mut left = top;
for i in 0..width {
top = prev[i]; left = input[i]
.wrapping_add(predictor_byte(gradient_predictor_c(left, top, top_left)));
top_left = top;
out[i] = left;
}
}
}
}
static WEBP_FILTERS: [Option<WebPFilterFunc>; 4] = [
None, Some(horizontal_filter_c), Some(vertical_filter_c), Some(gradient_filter_c), ];
static WEBP_UNFILTERS: [Option<WebPUnfilterFunc>; 4] = [
Some(none_unfilter_c), Some(horizontal_unfilter_c), Some(vertical_unfilter_c), Some(gradient_unfilter_c), ];
pub fn webp_filters(
filter: WEBP_FILTER_TYPE,
data: &[u8],
width: i32,
height: i32,
stride: i32,
filtered_data: &mut [u8],
) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
use crate::dsp::filters_sse2;
match filter {
WEBP_FILTER_TYPE::WEBP_FILTER_HORIZONTAL => {
return filters_sse2::horizontal_filter(
data,
width,
height,
stride,
filtered_data,
);
}
WEBP_FILTER_TYPE::WEBP_FILTER_VERTICAL => {
return filters_sse2::vertical_filter(
data,
width,
height,
stride,
filtered_data,
);
}
WEBP_FILTER_TYPE::WEBP_FILTER_GRADIENT => {
return filters_sse2::gradient_filter(
data,
width,
height,
stride,
filtered_data,
);
}
_ => {}
}
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
if let Some(filter_fn) = crate::dsp::filters_neon::filters(filter) {
return filter_fn(data, width, height, stride, filtered_data);
}
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
let filter_fn =
WEBP_FILTERS[filter.0 as usize].expect("WEBP_FILTER_NONE has no filter function");
filter_fn(data, width, height, stride, filtered_data);
}
fn overlap(a: &[u8], b: &[u8]) -> bool {
let (a, b) = (a.as_ptr_range(), b.as_ptr_range());
a.start < b.end && b.start < a.end
}
pub fn webp_unfilters(
filter: WEBP_FILTER_TYPE,
prev: Option<&[u8]>,
input: &[u8],
out: &mut [u8],
width: i32,
) {
debug_assert!(
prev.is_none_or(|prev| !overlap(prev, out)),
"the previous row overlaps the output row"
);
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
use crate::dsp::filters_sse2;
match filter {
WEBP_FILTER_TYPE::WEBP_FILTER_HORIZONTAL => {
return filters_sse2::horizontal_unfilter(prev, input, out, width);
}
WEBP_FILTER_TYPE::WEBP_FILTER_VERTICAL => {
return filters_sse2::vertical_unfilter(prev, input, out, width);
}
WEBP_FILTER_TYPE::WEBP_FILTER_GRADIENT => {
return filters_sse2::gradient_unfilter(prev, input, out, width);
}
_ => {}
}
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
if let Some(unfilter_fn) = crate::dsp::filters_neon::unfilters(filter) {
return unfilter_fn(prev, input, out, width);
}
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
let unfilter_fn =
WEBP_UNFILTERS[filter.0 as usize].expect("every filter has an unfilter function");
unfilter_fn(prev, input, out, width);
}