use crate::dsp::rescaler::{webp_rescaler_export_row, webp_rescaler_import_row};
use crate::utils::utils::check_size_overflow;
pub const WEBP_RESCALER_RFIX: u32 = 32;
pub const WEBP_RESCALER_ONE: u64 = 1 << WEBP_RESCALER_RFIX;
#[expect(non_camel_case_types, reason = "C type name")]
pub type rescaler_t = u32;
#[must_use]
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "the C macro converts its numerator to uint64_t and keeps the low 32 bits"
)]
pub fn webp_rescaler_frac(x: i32, y: i32) -> u32 {
let x = x as u64;
((x << WEBP_RESCALER_RFIX) / u64::try_from(y).expect("the divisor is positive")) as u32
}
pub struct WebPRescaler {
pub x_expand: bool,
pub y_expand: bool,
pub num_channels: i32,
pub fx_scale: u32,
pub fy_scale: u32,
pub fxy_scale: u32,
pub y_accum: i32,
pub y_add: i32,
pub y_sub: i32,
pub x_add: i32,
pub x_sub: i32,
pub src_width: i32,
pub src_height: i32,
pub dst_width: i32,
pub dst_height: i32,
pub src_y: i32,
pub dst_y: i32,
pub dst_pos: usize,
pub dst_stride: i32,
pub(crate) irow: Vec<rescaler_t>,
pub(crate) frow: Vec<rescaler_t>,
}
impl WebPRescaler {
pub const C_SIZE: usize = {
let pointer = size_of::<usize>();
let after_dst = (17 * 4_usize).next_multiple_of(pointer) + pointer + 4;
(after_dst.next_multiple_of(pointer) + 2 * pointer).next_multiple_of(pointer)
};
}
#[expect(
clippy::similar_names,
reason = "the C names fxy_scale, fy_scale and fx_scale of the scaling factors"
)]
#[must_use]
pub fn webp_rescaler_init(
src_width: i32,
src_height: i32,
dst_width: i32,
dst_height: i32,
dst_stride: i32,
num_channels: i32,
work: Vec<rescaler_t>,
) -> Option<WebPRescaler> {
let x_add = src_width;
let x_sub = dst_width;
let y_add = src_height;
let y_sub = dst_height;
let row_len = usize::try_from(num_channels * dst_width).expect("the row length is positive");
let total_size = 2_u64
* u64::try_from(dst_width).expect("dst_width is positive")
* u64::try_from(num_channels).expect("the channels are positive")
* 4;
if !check_size_overflow(total_size) {
return None;
}
assert!(
work.len() == 2 * row_len,
"the work holds {} elements, and the two side rows need {}",
work.len(),
2 * row_len
);
let x_expand = src_width < dst_width;
let y_expand = src_height < dst_height;
let x_add_out = if x_expand { x_sub - 1 } else { x_add };
let x_sub_out = if x_expand { x_add - 1 } else { x_sub };
let fx_scale = if x_expand {
0
} else {
webp_rescaler_frac(1, x_sub_out)
};
let y_add_out = if y_expand { y_add - 1 } else { y_add };
let y_sub_out = if y_expand { y_sub - 1 } else { y_sub };
let y_accum = if y_expand { y_sub_out } else { y_add_out };
let (fy_scale, fxy_scale) = if y_expand {
(webp_rescaler_frac(1, x_add_out), 0)
} else {
let num = u64::try_from(dst_height).expect("dst_height is positive") * WEBP_RESCALER_ONE;
let den = u64::try_from(x_add_out).expect("x_add is positive")
* u64::try_from(y_add).expect("src_height is positive");
let ratio = num / den;
let fxy_ratio = u32::try_from(ratio).unwrap_or(0);
(webp_rescaler_frac(1, y_sub_out), fxy_ratio)
};
let mut irow = work;
let mut frow = irow.split_off(row_len);
irow.fill(0);
frow.fill(0);
Some(WebPRescaler {
x_expand,
y_expand,
num_channels,
fx_scale,
fy_scale,
fxy_scale,
y_accum,
y_add: y_add_out,
y_sub: y_sub_out,
x_add: x_add_out,
x_sub: x_sub_out,
src_width,
src_height,
dst_width,
dst_height,
src_y: 0,
dst_y: 0,
dst_pos: 0,
dst_stride,
irow,
frow,
})
}
#[must_use]
#[expect(
clippy::cast_possible_truncation,
reason = "the C casts of the scaled dimensions keep the low 32 bits"
)]
#[expect(
clippy::cast_sign_loss,
reason = "the C code converts the signed dimensions to uint64_t"
)]
pub fn webp_rescaler_get_scaled_dimensions(
src_width: i32,
src_height: i32,
scaled_width: &mut i32,
scaled_height: &mut i32,
) -> bool {
let mut width = *scaled_width;
let mut height = *scaled_height;
let max_size = i32::MAX / 2;
if width == 0 && src_height > 0 {
width = ((((src_width as u64).wrapping_mul(height as u64))
.wrapping_add(src_height as u64)
.wrapping_sub(1))
/ (src_height as u64)) as i32;
}
if height == 0 && src_width > 0 {
height = ((((src_height as u64).wrapping_mul(width as u64))
.wrapping_add(src_width as u64)
.wrapping_sub(1))
/ (src_width as u64)) as i32;
}
if width <= 0 || height <= 0 || width > max_size || height > max_size {
return false;
}
*scaled_width = width;
*scaled_height = height;
true
}
#[must_use]
pub fn webp_rescale_needed_lines(rescaler: &WebPRescaler, max_num_lines: i32) -> i32 {
let num_lines = (rescaler.y_accum + rescaler.y_sub - 1) / rescaler.y_sub;
if num_lines > max_num_lines {
max_num_lines
} else {
num_lines
}
}
pub fn webp_rescaler_import(
rescaler: &mut WebPRescaler,
num_lines: i32,
src: &[u8],
src_stride: i32,
) -> i32 {
let mut total_imported = 0_i32;
let mut src_pos = 0_usize;
let row_len = usize::try_from(rescaler.num_channels * rescaler.dst_width)
.expect("the row length is positive");
while total_imported < num_lines && !webp_rescaler_has_pending_output(rescaler) {
if rescaler.y_expand {
std::mem::swap(&mut rescaler.irow, &mut rescaler.frow);
}
webp_rescaler_import_row(rescaler, &src[src_pos..]);
if !rescaler.y_expand {
for x in 0..row_len {
rescaler.irow[x] = rescaler.irow[x].wrapping_add(rescaler.frow[x]);
}
}
rescaler.src_y += 1;
src_pos = src_pos
.checked_add_signed(isize::try_from(src_stride).expect("the stride fits in isize"))
.expect("the source row starts inside the buffer");
total_imported += 1;
rescaler.y_accum -= rescaler.y_sub;
}
total_imported
}
pub fn webp_rescaler_export(rescaler: &mut WebPRescaler, dst: &mut [u8]) -> i32 {
let mut total_exported = 0_i32;
while webp_rescaler_has_pending_output(rescaler) {
webp_rescaler_export_row(rescaler, dst);
total_exported += 1;
}
total_exported
}
#[must_use]
pub fn webp_rescaler_input_done(rescaler: &WebPRescaler) -> bool {
rescaler.src_y >= rescaler.src_height
}
#[must_use]
pub fn webp_rescaler_output_done(rescaler: &WebPRescaler) -> bool {
rescaler.dst_y >= rescaler.dst_height
}
#[must_use]
pub fn webp_rescaler_has_pending_output(rescaler: &WebPRescaler) -> bool {
!webp_rescaler_output_done(rescaler) && rescaler.y_accum <= 0
}