use std::vec::Vec;
use super::{PlanGeometry, ResampleError};
#[cfg(all(test, feature = "std"))]
std::thread_local! {
static FORCE_FILTER_AXIS_ALLOC_FAILURE: core::cell::Cell<bool> =
const { core::cell::Cell::new(false) };
}
#[cfg(all(test, feature = "std"))]
pub(crate) fn arm_filter_axis_alloc_failure() {
FORCE_FILTER_AXIS_ALLOC_FAILURE.with(|f| f.set(true));
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn next_up_f64(x: f64) -> f64 {
f64::from_bits(x.to_bits() + 1)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn support_absorbable_at_max_center(scale: f64, support: f64, out_size: usize) -> bool {
let c_max = (out_size as f64 - 0.5) * scale;
let ulp = next_up_f64(c_max) - c_max;
support < ulp
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn floor_f64(x: f64) -> f64 {
#[cfg(feature = "std")]
{
f64::floor(x)
}
#[cfg(all(not(feature = "std"), feature = "alloc"))]
{
libm::floor(x)
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn ceil_f64(x: f64) -> f64 {
#[cfg(feature = "std")]
{
f64::ceil(x)
}
#[cfg(all(not(feature = "std"), feature = "alloc"))]
{
libm::ceil(x)
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn round_f64(x: f64) -> f64 {
#[cfg(feature = "std")]
{
f64::round(x)
}
#[cfg(all(not(feature = "std"), feature = "alloc"))]
{
libm::round(x)
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn sin_f64(x: f64) -> f64 {
#[cfg(feature = "std")]
{
f64::sin(x)
}
#[cfg(all(not(feature = "std"), feature = "alloc"))]
{
libm::sin(x)
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn cos_f64(x: f64) -> f64 {
#[cfg(feature = "std")]
{
f64::cos(x)
}
#[cfg(all(not(feature = "std"), feature = "alloc"))]
{
libm::cos(x)
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn exp2_f64(x: f64) -> f64 {
#[cfg(feature = "std")]
{
f64::exp2(x)
}
#[cfg(all(not(feature = "std"), feature = "alloc"))]
{
libm::exp2(x)
}
}
pub trait FilterKernel {
fn support(&self) -> f64;
fn weight(&self, x: f64) -> f64;
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Triangle;
impl FilterKernel for Triangle {
#[cfg_attr(not(tarpaulin), inline(always))]
fn support(&self) -> f64 {
1.0
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn weight(&self, x: f64) -> f64 {
let x = x.abs();
if x < 1.0 { 1.0 - x } else { 0.0 }
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct CatmullRom;
impl CatmullRom {
const A: f64 = -0.5;
}
impl FilterKernel for CatmullRom {
#[cfg_attr(not(tarpaulin), inline(always))]
fn support(&self) -> f64 {
2.0
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn weight(&self, x: f64) -> f64 {
let a = Self::A;
let t = x.abs();
if t < 1.0 {
((a + 2.0) * t - (a + 3.0)) * t * t + 1.0
} else if t < 2.0 {
(((t - 5.0) * t + 8.0) * t - 4.0) * a
} else {
0.0
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Lanczos3;
impl Lanczos3 {
#[cfg_attr(not(tarpaulin), inline(always))]
fn sinc(t: f64) -> f64 {
if t == 0.0 {
1.0
} else {
let pt = core::f64::consts::PI * t;
sin_f64(pt) / pt
}
}
}
impl FilterKernel for Lanczos3 {
#[cfg_attr(not(tarpaulin), inline(always))]
fn support(&self) -> f64 {
3.0
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn weight(&self, x: f64) -> f64 {
if x > -3.0 && x < 3.0 {
Self::sinc(x) * Self::sinc(x / 3.0)
} else {
0.0
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Mitchell;
impl Mitchell {
const B: f64 = 1.0 / 3.0;
const C: f64 = 1.0 / 3.0;
}
impl FilterKernel for Mitchell {
#[cfg_attr(not(tarpaulin), inline(always))]
fn support(&self) -> f64 {
2.0
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn weight(&self, x: f64) -> f64 {
let (b, c) = (Self::B, Self::C);
let t = x.abs();
if t < 1.0 {
(((12.0 - 9.0 * b - 6.0 * c) * t + (-18.0 + 12.0 * b + 6.0 * c)) * t * t + (6.0 - 2.0 * b))
/ 6.0
} else if t < 2.0 {
((((-b - 6.0 * c) * t + (6.0 * b + 30.0 * c)) * t + (-12.0 * b - 48.0 * c)) * t
+ (8.0 * b + 24.0 * c))
/ 6.0
} else {
0.0
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct OpenCvCubic;
impl OpenCvCubic {
const A: f64 = -0.75;
}
impl FilterKernel for OpenCvCubic {
#[cfg_attr(not(tarpaulin), inline(always))]
fn support(&self) -> f64 {
2.0
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn weight(&self, x: f64) -> f64 {
let a = Self::A;
let t = x.abs();
if t < 1.0 {
((a + 2.0) * t - (a + 3.0)) * t * t + 1.0
} else if t < 2.0 {
(((t - 5.0) * t + 8.0) * t - 4.0) * a
} else {
0.0
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct SwscaleBicubic;
impl SwscaleBicubic {
const A: f64 = -0.6;
}
impl FilterKernel for SwscaleBicubic {
#[cfg_attr(not(tarpaulin), inline(always))]
fn support(&self) -> f64 {
2.0
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn weight(&self, x: f64) -> f64 {
let a = Self::A;
let t = x.abs();
if t < 1.0 {
((a + 2.0) * t - (a + 3.0)) * t * t + 1.0
} else if t < 2.0 {
(((t - 5.0) * t + 8.0) * t - 4.0) * a
} else {
0.0
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Lanczos4;
impl Lanczos4 {
#[cfg_attr(not(tarpaulin), inline(always))]
fn sinc(t: f64) -> f64 {
if t == 0.0 {
1.0
} else {
let pt = core::f64::consts::PI * t;
sin_f64(pt) / pt
}
}
}
impl FilterKernel for Lanczos4 {
#[cfg_attr(not(tarpaulin), inline(always))]
fn support(&self) -> f64 {
4.0
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn weight(&self, x: f64) -> f64 {
if x > -4.0 && x < 4.0 {
Self::sinc(x) * Self::sinc(x / 4.0)
} else {
0.0
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Gaussian;
impl Gaussian {
const P: f64 = 3.0;
const SUPPORT: f64 = 3.0;
}
impl FilterKernel for Gaussian {
#[cfg_attr(not(tarpaulin), inline(always))]
fn support(&self) -> f64 {
Self::SUPPORT
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn weight(&self, x: f64) -> f64 {
if x.abs() < Self::SUPPORT {
exp2_f64(-Self::P * x * x)
} else {
0.0
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct BlackmanSinc;
impl BlackmanSinc {
const A: f64 = 3.0;
#[cfg_attr(not(tarpaulin), inline(always))]
fn sinc(t: f64) -> f64 {
if t == 0.0 {
1.0
} else {
let pt = core::f64::consts::PI * t;
sin_f64(pt) / pt
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn window(x: f64) -> f64 {
let u = core::f64::consts::PI * x / Self::A;
0.42 + 0.5 * cos_f64(u) + 0.08 * cos_f64(2.0 * u)
}
}
impl FilterKernel for BlackmanSinc {
#[cfg_attr(not(tarpaulin), inline(always))]
fn support(&self) -> f64 {
Self::A
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn weight(&self, x: f64) -> f64 {
if x > -Self::A && x < Self::A {
Self::sinc(x) * Self::window(x)
} else {
0.0
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct CubicBSpline;
impl FilterKernel for CubicBSpline {
#[cfg_attr(not(tarpaulin), inline(always))]
fn support(&self) -> f64 {
2.0
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn weight(&self, x: f64) -> f64 {
let t = x.abs();
if t < 1.0 {
(((3.0 * t - 6.0) * t) * t + 4.0) / 6.0
} else if t < 2.0 {
let u = 2.0 - t;
u * u * u / 6.0
} else {
0.0
}
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn spline_poly3(t: f64, c0: f64, c1: f64, c2: f64, c3: f64) -> f64 {
c0 + t * (c1 + t * (c2 + t * c3))
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Spline16;
impl FilterKernel for Spline16 {
#[cfg_attr(not(tarpaulin), inline(always))]
fn support(&self) -> f64 {
2.0
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn weight(&self, x: f64) -> f64 {
let t = x.abs();
if t < 1.0 {
spline_poly3(t, 1.0, -1.0 / 5.0, -9.0 / 5.0, 1.0)
} else if t < 2.0 {
spline_poly3(t - 1.0, 0.0, -7.0 / 15.0, 4.0 / 5.0, -1.0 / 3.0)
} else {
0.0
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Spline36;
impl FilterKernel for Spline36 {
#[cfg_attr(not(tarpaulin), inline(always))]
fn support(&self) -> f64 {
3.0
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn weight(&self, x: f64) -> f64 {
let t = x.abs();
if t < 1.0 {
spline_poly3(t, 1.0, -3.0 / 209.0, -453.0 / 209.0, 13.0 / 11.0)
} else if t < 2.0 {
spline_poly3(t - 1.0, 0.0, -156.0 / 209.0, 270.0 / 209.0, -6.0 / 11.0)
} else if t < 3.0 {
spline_poly3(t - 2.0, 0.0, 26.0 / 209.0, -45.0 / 209.0, 1.0 / 11.0)
} else {
0.0
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Spline64;
impl FilterKernel for Spline64 {
#[cfg_attr(not(tarpaulin), inline(always))]
fn support(&self) -> f64 {
4.0
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn weight(&self, x: f64) -> f64 {
let t = x.abs();
if t < 1.0 {
spline_poly3(t, 1.0, -3.0 / 2911.0, -6387.0 / 2911.0, 49.0 / 41.0)
} else if t < 2.0 {
spline_poly3(
t - 1.0,
0.0,
-2328.0 / 2911.0,
4032.0 / 2911.0,
-24.0 / 41.0,
)
} else if t < 3.0 {
spline_poly3(t - 2.0, 0.0, 582.0 / 2911.0, -1008.0 / 2911.0, 6.0 / 41.0)
} else if t < 4.0 {
spline_poly3(t - 3.0, 0.0, -97.0 / 2911.0, 168.0 / 2911.0, -1.0 / 41.0)
} else {
0.0
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct FilterAxis {
starts: Vec<usize>,
offsets: Vec<usize>,
coeffs: Vec<f32>,
coeffs_q8: Vec<f32>,
max_overlap: usize,
}
const PRECISION_BITS: u32 = 22;
impl FilterAxis {
pub(crate) fn build(
in_size: usize,
out_size: usize,
kernel: &dyn FilterKernel,
phase: f64,
) -> Result<Self, ResampleError> {
debug_assert!(in_size > 0 && out_size > 0);
let support_unit = kernel.support();
if !support_unit.is_finite() || support_unit <= 0.0 {
return Err(ResampleError::InvalidFilterSupport(
InvalidFilterSupport::new(support_unit, in_size),
));
}
let scale = in_size as f64 / out_size as f64;
let filterscale = if scale < 1.0 { 1.0 } else { scale };
let support = support_unit * filterscale;
let geometry = || PlanGeometry::new(in_size, 1, out_size, 1);
let offsets_len = out_size
.checked_add(1)
.ok_or_else(|| ResampleError::Overflow(geometry()))?;
offsets_len
.checked_mul(core::mem::size_of::<usize>())
.filter(|&b| b <= isize::MAX as usize)
.ok_or_else(|| ResampleError::Overflow(geometry()))?;
if support_absorbable_at_max_center(scale, support, out_size) {
return Err(ResampleError::InvalidFilterSupport(
InvalidFilterSupport::new(support_unit, in_size),
));
}
let last_center = ((out_size - 1) as f64 + 0.5) * scale;
let last_lo = floor_f64(last_center - support);
let last_xmin = if last_lo < 0.0 { 0 } else { last_lo as usize };
let last_xmax = (ceil_f64(last_center + support) as usize).min(in_size);
if last_xmax <= last_xmin {
return Err(ResampleError::InvalidFilterSupport(
InvalidFilterSupport::new(support_unit, in_size),
));
}
let mut starts = Vec::new();
#[cfg(all(test, feature = "std"))]
if FORCE_FILTER_AXIS_ALLOC_FAILURE.with(|f| f.take()) {
return Err(ResampleError::AllocationFailed(geometry()));
}
starts
.try_reserve_exact(out_size)
.map_err(|_| ResampleError::AllocationFailed(geometry()))?;
let mut ksize = Vec::new();
ksize
.try_reserve_exact(out_size)
.map_err(|_| ResampleError::AllocationFailed(geometry()))?;
let mut offsets = Vec::new();
offsets
.try_reserve_exact(offsets_len)
.map_err(|_| ResampleError::AllocationFailed(geometry()))?;
let mut coeffs: Vec<f32> = Vec::new();
let mut coeffs_q8: Vec<f32> = Vec::new();
let mut w_f64: Vec<f64> = Vec::new();
offsets.push(0);
let q8_scale_f64 = f64::from(1u32 << PRECISION_BITS);
let q8_scale_f32 = (1u32 << PRECISION_BITS) as f32;
for xx in 0..out_size {
let center = (xx as f64 + 0.5) * scale + phase;
let lo = floor_f64(center - support);
let xmin = if lo < 0.0 { 0 } else { lo as usize };
let hi = ceil_f64(center + support);
let xmax = (hi as usize).min(in_size);
let Some(n) = xmax.checked_sub(xmin).filter(|&n| n > 0) else {
return Err(ResampleError::InvalidFilterSupport(
InvalidFilterSupport::new(support_unit, in_size),
));
};
coeffs
.try_reserve(n)
.map_err(|_| ResampleError::AllocationFailed(geometry()))?;
coeffs_q8
.try_reserve(n)
.map_err(|_| ResampleError::AllocationFailed(geometry()))?;
w_f64.clear();
w_f64
.try_reserve(n)
.map_err(|_| ResampleError::AllocationFailed(geometry()))?;
let mut ww = 0.0f64;
let base = coeffs.len();
for k in 0..n {
let x = (xmin + k) as f64 + 0.5 - center;
let w = kernel.weight(x / filterscale);
coeffs.push(w as f32);
w_f64.push(w);
ww += w;
}
let inv = if ww != 0.0 { 1.0 / ww } else { 1.0 };
if ww != 0.0 {
for c in &mut coeffs[base..base + n] {
*c = (f64::from(*c) * inv) as f32;
}
}
for &w in &w_f64 {
let k = round_f64(w * inv * q8_scale_f64);
coeffs_q8.push((k as f32) / q8_scale_f32);
}
starts.push(xmin);
ksize.push(n);
offsets.push(coeffs.len());
}
let mut max_overlap = 0usize;
let mut lo = 0usize;
for j in 0..starts.len() {
while lo < j && starts[lo] + ksize[lo] <= starts[j] {
lo += 1;
}
let open = j - lo + 1;
if open > max_overlap {
max_overlap = open;
}
}
Ok(Self {
starts,
offsets,
coeffs,
coeffs_q8,
max_overlap,
})
}
#[cfg_attr(not(any(feature = "rgb", feature = "gray")), allow(dead_code))]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn out_len(&self) -> usize {
self.starts.len()
}
#[cfg_attr(not(any(feature = "rgb", feature = "gray")), allow(dead_code))]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn max_overlap(&self) -> usize {
self.max_overlap
}
#[cfg_attr(not(any(feature = "rgb", feature = "gray")), allow(dead_code))]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn span(&self, j: usize) -> (usize, &[f32]) {
(
self.starts[j],
&self.coeffs[self.offsets[j]..self.offsets[j + 1]],
)
}
#[cfg_attr(not(any(feature = "rgb", feature = "gray")), allow(dead_code))]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn span_q8(&self, j: usize) -> (usize, &[f32]) {
(
self.starts[j],
&self.coeffs_q8[self.offsets[j]..self.offsets[j + 1]],
)
}
#[cfg_attr(not(any(feature = "rgb", feature = "gray")), allow(dead_code))]
fn try_clone(&self) -> Result<Self, ResampleError> {
fn copy<T: Copy>(src: &[T]) -> Result<Vec<T>, ResampleError> {
let mut v = Vec::new();
v.try_reserve_exact(src.len())
.map_err(|_| ResampleError::AllocationFailed(PlanGeometry::new(0, 0, 0, 0)))?;
v.extend_from_slice(src);
Ok(v)
}
Ok(Self {
starts: copy(&self.starts)?,
offsets: copy(&self.offsets)?,
coeffs: copy(&self.coeffs)?,
coeffs_q8: copy(&self.coeffs_q8)?,
max_overlap: self.max_overlap,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct InvalidFilterSupport {
support: f64,
in_size: usize,
}
impl InvalidFilterSupport {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new(support: f64, in_size: usize) -> Self {
Self { support, in_size }
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn support(&self) -> f64 {
self.support
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn in_size(&self) -> usize {
self.in_size
}
}
#[cfg(any(
feature = "yuv-planar",
feature = "rgb",
feature = "gbr",
feature = "gray",
feature = "xyz",
feature = "bayer",
feature = "mono",
feature = "yuv-semi-planar",
feature = "yuv-packed",
feature = "yuv-444-packed",
feature = "y2xx",
feature = "v210",
feature = "rgb-legacy"
))]
mod stream;
#[cfg(any(
feature = "yuv-planar",
feature = "rgb",
feature = "gbr",
feature = "gray",
feature = "xyz",
feature = "bayer",
feature = "mono",
feature = "yuv-semi-planar",
feature = "yuv-packed",
feature = "yuv-444-packed",
feature = "y2xx",
feature = "v210",
feature = "rgb-legacy"
))]
pub(crate) use stream::{FilterSample, FilterStream};
#[cfg(all(test, feature = "std"))]
mod tests;