#![cfg_attr(not(any(feature = "yuv-planar", feature = "rgb")), allow(dead_code))]
use std::vec::Vec;
use derive_more::{IsVariant, TryUnwrap, Unwrap};
use thiserror::Error;
mod filter;
use filter::FilterAxis;
pub use filter::{
BlackmanSinc, CatmullRom, CubicBSpline, FilterKernel, Gaussian, InvalidFilterSupport, Lanczos3,
Lanczos4, Mitchell, OpenCvCubic, Spline16, Spline36, Spline64, SwscaleBicubic, Triangle,
};
#[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"
))]
#[cfg_attr(not(any(feature = "rgb", feature = "gray")), allow(unused_imports))]
pub(crate) use filter::{FilterSample, FilterStream};
mod strategy;
#[cfg(feature = "yuv-planar")]
pub(crate) use strategy::transfer::bt2020_oetf;
pub use strategy::{AveragingDomain, FilterSpec, LinearMode, ResampleStrategy, TransferFunction};
#[cfg_attr(not(feature = "yuv-planar"), allow(unused_imports))]
pub(crate) use strategy::{InsertionContext, InsertionPoint, select_insertion_point};
pub trait Resampler: sealed::Sealed {
fn plan(&self, src_w: usize, src_h: usize) -> Result<Option<ResamplePlan>, ResampleError>;
}
mod sealed {
pub trait Sealed {}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct NoopResampler;
impl sealed::Sealed for NoopResampler {}
impl Resampler for NoopResampler {
#[cfg_attr(not(tarpaulin), inline(always))]
fn plan(&self, _src_w: usize, _src_h: usize) -> Result<Option<ResamplePlan>, ResampleError> {
Ok(None)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AreaResampler {
out_w: usize,
out_h: usize,
}
impl AreaResampler {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn to(out_w: usize, out_h: usize) -> Self {
Self { out_w, out_h }
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn out_w(&self) -> usize {
self.out_w
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn out_h(&self) -> usize {
self.out_h
}
}
impl sealed::Sealed for AreaResampler {}
impl Resampler for AreaResampler {
fn plan(&self, src_w: usize, src_h: usize) -> Result<Option<ResamplePlan>, ResampleError> {
if self.out_w == 0 || self.out_h == 0 {
return Err(ResampleError::ZeroOutputDimension(
ZeroOutputDimension::new(self.out_w, self.out_h),
));
}
if self.out_w > src_w || self.out_h > src_h {
return Err(ResampleError::UpscaleUnsupported(UpscaleUnsupported::new(
src_w, src_h, self.out_w, self.out_h,
)));
}
if self.out_w == src_w && self.out_h == src_h {
return Ok(None);
}
ResamplePlan::area(src_w, src_h, self.out_w, self.out_h).map(Some)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FilteredResampler<K: FilterKernel> {
out_w: usize,
out_h: usize,
kernel: K,
}
impl<K: FilterKernel> FilteredResampler<K> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new(out_w: usize, out_h: usize, kernel: K) -> Self {
Self {
out_w,
out_h,
kernel,
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn out_w(&self) -> usize {
self.out_w
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn out_h(&self) -> usize {
self.out_h
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn kernel(&self) -> &K {
&self.kernel
}
}
impl<K: FilterKernel> sealed::Sealed for FilteredResampler<K> {}
impl<K: FilterKernel> Resampler for FilteredResampler<K> {
fn plan(&self, src_w: usize, src_h: usize) -> Result<Option<ResamplePlan>, ResampleError> {
if self.out_w == 0 || self.out_h == 0 {
return Err(ResampleError::ZeroOutputDimension(
ZeroOutputDimension::new(self.out_w, self.out_h),
));
}
if self.out_w == src_w && self.out_h == src_h {
return Ok(None);
}
ResamplePlan::filter(src_w, src_h, self.out_w, self.out_h, &self.kernel).map(Some)
}
}
#[cfg(feature = "yuv-planar")]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Bicublin {
out_w: usize,
out_h: usize,
}
#[cfg(feature = "yuv-planar")]
impl Bicublin {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn to(out_w: usize, out_h: usize) -> Self {
Self { out_w, out_h }
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn out_w(&self) -> usize {
self.out_w
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn out_h(&self) -> usize {
self.out_h
}
}
#[cfg(feature = "yuv-planar")]
impl sealed::Sealed for Bicublin {}
#[cfg(feature = "yuv-planar")]
impl Resampler for Bicublin {
fn plan(&self, src_w: usize, src_h: usize) -> Result<Option<ResamplePlan>, ResampleError> {
if self.out_w == 0 || self.out_h == 0 {
return Err(ResampleError::ZeroOutputDimension(
ZeroOutputDimension::new(self.out_w, self.out_h),
));
}
let chroma_w = src_w / 2;
let chroma_h = src_h.div_ceil(2);
ResamplePlan::bicublin(
src_w,
src_h,
chroma_w,
chroma_h,
self.out_w,
self.out_h,
&SwscaleBicubic,
&Triangle,
0.0,
0.0,
)
.map(Some)
}
}
#[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) fn try_zeroed<T: Clone + Default>(
n: usize,
) -> Result<Vec<T>, std::collections::TryReserveError> {
let mut buf = Vec::new();
buf.try_reserve_exact(n)?;
buf.resize(n, T::default());
Ok(buf)
}
fn gcd(mut a: usize, mut b: usize) -> usize {
while b != 0 {
(a, b) = (b, a % b);
}
a
}
#[derive(Debug)]
enum AxisError {
Overflow,
Alloc,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct AxisSpans {
starts: Vec<usize>,
offsets: Vec<usize>,
weights: Vec<usize>,
}
impl AxisSpans {
fn area_taps(src: usize, out: usize) -> Option<usize> {
(src - gcd(src, out)).checked_add(out)
}
#[cfg_attr(not(any(feature = "yuv-planar", feature = "rgb")), allow(dead_code))]
fn empty() -> Self {
Self {
starts: Vec::new(),
offsets: Vec::new(),
weights: Vec::new(),
}
}
fn area(src: usize, out: usize) -> Result<Self, AxisError> {
let src64 = src as u64;
let out64 = out as u64;
src64.checked_mul(out64).ok_or(AxisError::Overflow)?;
let taps = Self::area_taps(src, out).ok_or(AxisError::Overflow)?;
let offsets_len = out.checked_add(1).ok_or(AxisError::Overflow)?;
let mut starts = Vec::new();
starts
.try_reserve_exact(out)
.map_err(|_| AxisError::Alloc)?;
let mut offsets = Vec::new();
offsets
.try_reserve_exact(offsets_len)
.map_err(|_| AxisError::Alloc)?;
let mut weights = Vec::new();
weights
.try_reserve_exact(taps)
.map_err(|_| AxisError::Alloc)?;
offsets.push(0);
for j in 0..out64 {
let lo = j * src64;
let hi = lo + src64;
let start = lo / out64;
starts.push(start as usize);
for i in start..hi.div_ceil(out64) {
let cell_lo = i * out64;
let cell_hi = cell_lo + out64;
weights.push((cell_hi.min(hi) - cell_lo.max(lo)) as usize);
}
offsets.push(weights.len());
}
debug_assert_eq!(weights.len(), taps);
Ok(Self {
starts,
offsets,
weights,
})
}
#[cfg_attr(not(feature = "yuv-planar"), allow(dead_code))]
fn area_subsampled(src_full: usize, out: usize, factor: usize) -> Result<Self, AxisError> {
let src64 = src_full as u64;
let out64 = out as u64;
let f = factor as u64;
src64.checked_mul(out64).ok_or(AxisError::Overflow)?;
let cells = src_full.div_ceil(factor);
let taps = Self::area_taps(src_full, out).ok_or(AxisError::Overflow)?;
let offsets_len = out.checked_add(1).ok_or(AxisError::Overflow)?;
let mut starts = Vec::new();
starts
.try_reserve_exact(out)
.map_err(|_| AxisError::Alloc)?;
let mut offsets = Vec::new();
offsets
.try_reserve_exact(offsets_len)
.map_err(|_| AxisError::Alloc)?;
let mut weights = Vec::new();
weights
.try_reserve_exact(taps)
.map_err(|_| AxisError::Alloc)?;
offsets.push(0);
for j in 0..out64 {
let lo = j * src64;
let hi = lo + src64;
let start = ((lo / out64) / f) as usize;
starts.push(start);
let mut c = start as u64;
loop {
let cell_lo = (f * c) * out64;
let cell_hi = ((f * c + f).min(src64)) * out64;
if cell_lo >= hi {
break;
}
let w = cell_hi.min(hi) - cell_lo.max(lo);
if w == 0 {
break;
}
weights.push(w as usize);
if cell_hi >= hi || c as usize + 1 >= cells {
break;
}
c += 1;
}
offsets.push(weights.len());
}
Ok(Self {
starts,
offsets,
weights,
})
}
#[cfg_attr(not(feature = "yuv-planar"), allow(dead_code))]
fn area_halved(src_full: usize, out: usize) -> Result<Self, AxisError> {
Self::area_subsampled(src_full, out, 2)
}
#[cfg(feature = "yuv-planar")]
fn area_chroma_phased_h_centered(chroma_w: usize, out: usize) -> Result<Self, AxisError> {
let full_w = chroma_w.checked_mul(2).ok_or(AxisError::Overflow)?;
full_w.checked_mul(4).ok_or(AxisError::Overflow)?;
let box_spans = Self::area(full_w, out)?;
let first_chroma = |x: usize| -> usize {
if x.is_multiple_of(2) {
(x / 2).saturating_sub(1)
} else {
(x - 1) / 2
}
};
let last_chroma = |x: usize| -> usize {
if x.is_multiple_of(2) {
x / 2
} else {
((x - 1) / 2 + 1).min(chroma_w - 1)
}
};
let offsets_len = out.checked_add(1).ok_or(AxisError::Overflow)?;
let mut starts = Vec::new();
starts
.try_reserve_exact(out)
.map_err(|_| AxisError::Alloc)?;
let mut offsets = Vec::new();
offsets
.try_reserve_exact(offsets_len)
.map_err(|_| AxisError::Alloc)?;
let mut total = 0usize;
let mut max_span = 0usize;
for o in 0..out {
let (xstart, wbins) = box_spans.span(o);
let jf = first_chroma(xstart);
let jl = last_chroma(xstart + wbins.len() - 1);
starts.push(jf);
let span = jl - jf + 1;
max_span = max_span.max(span);
total = total.checked_add(span).ok_or(AxisError::Overflow)?;
}
let mut weights = Vec::new();
weights
.try_reserve_exact(total)
.map_err(|_| AxisError::Alloc)?;
let mut local: Vec<u64> = Vec::new();
local
.try_reserve_exact(max_span)
.map_err(|_| AxisError::Alloc)?;
offsets.push(0);
for (o, &jf) in starts.iter().enumerate() {
let (xstart, wbins) = box_spans.span(o);
let jl = last_chroma(xstart + wbins.len() - 1);
local.clear();
local.resize(jl - jf + 1, 0);
for (k, &wb) in wbins.iter().enumerate() {
let x = xstart + k;
let wb = wb as u64;
if x.is_multiple_of(2) {
let j = x / 2;
if j == 0 {
local[0] += 4 * wb; } else {
local[j - 1 - jf] += wb; local[j - jf] += 3 * wb; }
} else {
let j = (x - 1) / 2;
if j == chroma_w - 1 {
local[j - jf] += 4 * wb; } else {
local[j - jf] += 3 * wb; local[j + 1 - jf] += wb; }
}
}
for &wv in &local {
weights.push(wv as usize);
}
offsets.push(weights.len());
}
debug_assert_eq!(weights.len(), total);
Ok(Self {
starts,
offsets,
weights,
})
}
#[cfg(feature = "yuv-planar")]
fn area_chroma_phased_v(luma_h: usize, out: usize) -> Result<Self, AxisError> {
luma_h.checked_mul(2).ok_or(AxisError::Overflow)?;
let box_spans = Self::area(luma_h, out)?;
let first_chroma = |r: usize| -> usize {
if r.is_multiple_of(2) {
(r / 2).saturating_sub(1)
} else {
r / 2
}
};
let last_chroma = |r: usize| -> usize { r / 2 };
let offsets_len = out.checked_add(1).ok_or(AxisError::Overflow)?;
let mut starts = Vec::new();
starts
.try_reserve_exact(out)
.map_err(|_| AxisError::Alloc)?;
let mut offsets = Vec::new();
offsets
.try_reserve_exact(offsets_len)
.map_err(|_| AxisError::Alloc)?;
let mut total = 0usize;
let mut max_span = 0usize;
for o in 0..out {
let (rstart, wbins) = box_spans.span(o);
let i_f = first_chroma(rstart);
let i_l = last_chroma(rstart + wbins.len() - 1);
starts.push(i_f);
let span = i_l - i_f + 1;
max_span = max_span.max(span);
total = total.checked_add(span).ok_or(AxisError::Overflow)?;
}
let mut weights = Vec::new();
weights
.try_reserve_exact(total)
.map_err(|_| AxisError::Alloc)?;
let mut local: Vec<u64> = Vec::new();
local
.try_reserve_exact(max_span)
.map_err(|_| AxisError::Alloc)?;
offsets.push(0);
for (o, &i_f) in starts.iter().enumerate() {
let (rstart, wbins) = box_spans.span(o);
let i_l = last_chroma(rstart + wbins.len() - 1);
local.clear();
local.resize(i_l - i_f + 1, 0);
for (k, &wb) in wbins.iter().enumerate() {
let r = rstart + k;
let wb = wb as u64;
let i = r / 2;
if r.is_multiple_of(2) {
if i == 0 {
local[0] += 2 * wb; } else {
local[i - 1 - i_f] += wb; local[i - i_f] += wb; }
} else {
local[i - i_f] += 2 * wb; }
}
for &wv in &local {
weights.push(wv as usize);
}
offsets.push(weights.len());
}
debug_assert_eq!(weights.len(), total);
Ok(Self {
starts,
offsets,
weights,
})
}
#[cfg(feature = "yuv-planar")]
fn area_chroma_phased_v_top(luma_h: usize, out: usize) -> Result<Self, AxisError> {
luma_h.checked_mul(2).ok_or(AxisError::Overflow)?;
let box_spans = Self::area(luma_h, out)?;
let chroma_h = luma_h.div_ceil(2);
let last_valid = chroma_h - 1;
let first_chroma = |r: usize| -> usize { r / 2 };
let last_chroma = |r: usize| -> usize {
if r.is_multiple_of(2) {
r / 2
} else {
(r / 2 + 1).min(last_valid)
}
};
let offsets_len = out.checked_add(1).ok_or(AxisError::Overflow)?;
let mut starts = Vec::new();
starts
.try_reserve_exact(out)
.map_err(|_| AxisError::Alloc)?;
let mut offsets = Vec::new();
offsets
.try_reserve_exact(offsets_len)
.map_err(|_| AxisError::Alloc)?;
let mut total = 0usize;
let mut max_span = 0usize;
for o in 0..out {
let (rstart, wbins) = box_spans.span(o);
let i_f = first_chroma(rstart);
let i_l = last_chroma(rstart + wbins.len() - 1);
starts.push(i_f);
let span = i_l - i_f + 1;
max_span = max_span.max(span);
total = total.checked_add(span).ok_or(AxisError::Overflow)?;
}
let mut weights = Vec::new();
weights
.try_reserve_exact(total)
.map_err(|_| AxisError::Alloc)?;
let mut local: Vec<u64> = Vec::new();
local
.try_reserve_exact(max_span)
.map_err(|_| AxisError::Alloc)?;
offsets.push(0);
for (o, &i_f) in starts.iter().enumerate() {
let (rstart, wbins) = box_spans.span(o);
let i_l = last_chroma(rstart + wbins.len() - 1);
local.clear();
local.resize(i_l - i_f + 1, 0);
for (k, &wb) in wbins.iter().enumerate() {
let r = rstart + k;
let wb = wb as u64;
let i = r / 2;
if r.is_multiple_of(2) {
local[i - i_f] += 2 * wb; } else if i + 1 > last_valid {
local[i - i_f] += 2 * wb; } else {
local[i - i_f] += wb; local[i + 1 - i_f] += wb; }
}
for &wv in &local {
weights.push(wv as usize);
}
offsets.push(weights.len());
}
debug_assert_eq!(weights.len(), total);
Ok(Self {
starts,
offsets,
weights,
})
}
#[cfg(feature = "yuv-planar")]
fn area_chroma_phased_h_centered_4(
luma_w: usize,
chroma_w: usize,
out: usize,
) -> Result<Self, AxisError> {
luma_w.checked_mul(8).ok_or(AxisError::Overflow)?;
let box_spans = Self::area(luma_w, out)?;
let first_chroma = |x: usize| -> usize {
let j = x / 4;
match x % 4 {
0 | 1 => j.saturating_sub(1),
_ => j,
}
};
let last_chroma = |x: usize| -> usize {
let j = x / 4;
match x % 4 {
0 | 1 => j,
_ => (j + 1).min(chroma_w - 1),
}
};
let offsets_len = out.checked_add(1).ok_or(AxisError::Overflow)?;
let mut starts = Vec::new();
starts
.try_reserve_exact(out)
.map_err(|_| AxisError::Alloc)?;
let mut offsets = Vec::new();
offsets
.try_reserve_exact(offsets_len)
.map_err(|_| AxisError::Alloc)?;
let mut total = 0usize;
let mut max_span = 0usize;
for o in 0..out {
let (xstart, wbins) = box_spans.span(o);
let jf = first_chroma(xstart);
let jl = last_chroma(xstart + wbins.len() - 1);
starts.push(jf);
let span = jl - jf + 1;
max_span = max_span.max(span);
total = total.checked_add(span).ok_or(AxisError::Overflow)?;
}
let mut weights = Vec::new();
weights
.try_reserve_exact(total)
.map_err(|_| AxisError::Alloc)?;
let mut local: Vec<u64> = Vec::new();
local
.try_reserve_exact(max_span)
.map_err(|_| AxisError::Alloc)?;
offsets.push(0);
for (o, &jf) in starts.iter().enumerate() {
let (xstart, wbins) = box_spans.span(o);
let jl = last_chroma(xstart + wbins.len() - 1);
local.clear();
local.resize(jl - jf + 1, 0);
for (k, &wb) in wbins.iter().enumerate() {
let x = xstart + k;
let wb = wb as u64;
let j = x / 4;
match x % 4 {
0 => {
if j == 0 {
local[0] += 8 * wb; } else {
local[j - 1 - jf] += 3 * wb; local[j - jf] += 5 * wb; }
}
1 => {
if j == 0 {
local[0] += 8 * wb; } else {
local[j - 1 - jf] += wb; local[j - jf] += 7 * wb; }
}
2 => {
if j == chroma_w - 1 {
local[j - jf] += 8 * wb; } else {
local[j - jf] += 7 * wb; local[j + 1 - jf] += wb; }
}
_ => {
if j == chroma_w - 1 {
local[j - jf] += 8 * wb; } else {
local[j - jf] += 5 * wb; local[j + 1 - jf] += 3 * wb; }
}
}
}
for &wv in &local {
weights.push(wv as usize);
}
offsets.push(weights.len());
}
debug_assert_eq!(weights.len(), total);
Ok(Self {
starts,
offsets,
weights,
})
}
#[cfg_attr(not(any(feature = "yuv-planar", feature = "rgb")), allow(dead_code))]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn out_len(&self) -> usize {
self.starts.len()
}
#[cfg_attr(not(any(feature = "yuv-planar", feature = "rgb")), allow(dead_code))]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn span(&self, j: usize) -> (usize, &[usize]) {
(
self.starts[j],
&self.weights[self.offsets[j]..self.offsets[j + 1]],
)
}
#[cfg_attr(not(any(feature = "yuv-planar", feature = "rgb")), allow(dead_code))]
pub(crate) fn window_depth(&self) -> usize {
let mut depth = 0;
for o in 0..self.out_len() {
let (s, w) = self.span(o);
let last = s + w.len() - 1;
let mut open = 1;
for o2 in (o + 1)..self.out_len() {
if self.span(o2).0 < last {
open += 1;
} else {
break;
}
}
depth = depth.max(open);
}
depth
}
#[cfg_attr(not(any(feature = "yuv-planar", feature = "rgb")), allow(dead_code))]
fn try_clone(&self) -> Result<Self, AxisError> {
fn copy<T: Copy>(src: &[T]) -> Result<Vec<T>, AxisError> {
let mut v = Vec::new();
v.try_reserve_exact(src.len())
.map_err(|_| AxisError::Alloc)?;
v.extend_from_slice(src);
Ok(v)
}
Ok(Self {
starts: copy(&self.starts)?,
offsets: copy(&self.offsets)?,
weights: copy(&self.weights)?,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, IsVariant)]
pub enum SpanKind {
Area,
Filter,
}
impl SpanKind {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn as_str(&self) -> &'static str {
match self {
Self::Area => "area",
Self::Filter => "filter",
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ResamplePlan {
src_w: usize,
src_h: usize,
out_w: usize,
out_h: usize,
kind: SpanKind,
h: AxisSpans,
v: AxisSpans,
filter_h: Option<FilterAxis>,
filter_v: Option<FilterAxis>,
filter_h_chroma: Option<FilterAxis>,
filter_v_chroma: Option<FilterAxis>,
h_phase: f64,
v_phase: f64,
v_top: bool,
}
impl ResamplePlan {
pub(crate) fn area(
src_w: usize,
src_h: usize,
out_w: usize,
out_h: usize,
) -> Result<Self, ResampleError> {
let fail = |e: AxisError| match e {
AxisError::Overflow => ResampleError::Overflow(PlanGeometry::new(src_w, src_h, out_w, out_h)),
AxisError::Alloc => {
ResampleError::AllocationFailed(PlanGeometry::new(src_w, src_h, out_w, out_h))
}
};
let h = AxisSpans::area(src_w, out_w).map_err(fail)?;
let v = AxisSpans::area(src_h, out_h).map_err(fail)?;
Ok(Self {
src_w,
src_h,
out_w,
out_h,
kind: SpanKind::Area,
h,
v,
filter_h: None,
filter_v: None,
filter_h_chroma: None,
filter_v_chroma: None,
h_phase: 0.0,
v_phase: 0.0,
v_top: false,
})
}
pub(crate) fn filter(
src_w: usize,
src_h: usize,
out_w: usize,
out_h: usize,
kernel: &dyn FilterKernel,
) -> Result<Self, ResampleError> {
let filter_h = FilterAxis::build(src_w, out_w, kernel, 0.0)?;
let filter_v = FilterAxis::build(src_h, out_h, kernel, 0.0)?;
Ok(Self {
src_w,
src_h,
out_w,
out_h,
kind: SpanKind::Filter,
h: AxisSpans::empty(),
v: AxisSpans::empty(),
filter_h: Some(filter_h),
filter_v: Some(filter_v),
filter_h_chroma: None,
filter_v_chroma: None,
h_phase: 0.0,
v_phase: 0.0,
v_top: false,
})
}
#[cfg(feature = "yuv-planar")]
#[allow(clippy::too_many_arguments)]
pub(crate) fn bicublin(
src_w: usize,
src_h: usize,
chroma_w: usize,
chroma_h: usize,
out_w: usize,
out_h: usize,
luma_kernel: &dyn FilterKernel,
chroma_kernel: &dyn FilterKernel,
h_phase: f64,
v_phase: f64,
) -> Result<Self, ResampleError> {
let filter_h = FilterAxis::build(src_w, out_w, luma_kernel, 0.0)?;
let filter_v = FilterAxis::build(src_h, out_h, luma_kernel, 0.0)?;
let filter_h_chroma = FilterAxis::build(chroma_w, out_w, chroma_kernel, h_phase)?;
let filter_v_chroma = FilterAxis::build(chroma_h, out_h, chroma_kernel, v_phase)?;
Ok(Self {
src_w,
src_h,
out_w,
out_h,
kind: SpanKind::Filter,
h: AxisSpans::empty(),
v: AxisSpans::empty(),
filter_h: Some(filter_h),
filter_v: Some(filter_v),
filter_h_chroma: Some(filter_h_chroma),
filter_v_chroma: Some(filter_v_chroma),
h_phase,
v_phase,
v_top: false,
})
}
#[cfg(feature = "yuv-planar")]
pub(crate) fn area_chroma_420(
chroma_w: usize,
luma_h: usize,
out_w: usize,
out_h: usize,
h_phase: f64,
v_phase: f64,
v_top: bool,
) -> Result<Self, ResampleError> {
let fail_overflow =
|| ResampleError::Overflow(PlanGeometry::new(chroma_w, luma_h, out_w, out_h));
let fail_alloc =
|| ResampleError::AllocationFailed(PlanGeometry::new(chroma_w, luma_h, out_w, out_h));
let fail = |e: AxisError| match e {
AxisError::Overflow => fail_overflow(),
AxisError::Alloc => fail_alloc(),
};
let (v, denom_h) = if v_top {
let denom_h = luma_h.checked_mul(2).ok_or_else(fail_overflow)?;
(
AxisSpans::area_chroma_phased_v_top(luma_h, out_h).map_err(fail)?,
denom_h,
)
} else if v_phase == 0.0 {
(AxisSpans::area_halved(luma_h, out_h).map_err(fail)?, luma_h)
} else {
let denom_h = luma_h.checked_mul(2).ok_or_else(fail_overflow)?;
(
AxisSpans::area_chroma_phased_v(luma_h, out_h).map_err(fail)?,
denom_h,
)
};
let (h, denom_w) = if h_phase == 0.0 {
(AxisSpans::area(chroma_w, out_w).map_err(fail)?, chroma_w)
} else {
let denom_w = chroma_w.checked_mul(8).ok_or_else(fail_overflow)?;
(
AxisSpans::area_chroma_phased_h_centered(chroma_w, out_w).map_err(fail)?,
denom_w,
)
};
Ok(Self {
src_w: denom_w,
src_h: denom_h,
out_w,
out_h,
kind: SpanKind::Area,
h,
v,
filter_h: None,
filter_v: None,
filter_h_chroma: None,
filter_v_chroma: None,
h_phase,
v_phase,
v_top,
})
}
#[cfg(feature = "yuv-planar")]
pub(crate) fn area_chroma_440(
frame_w: usize,
luma_h: usize,
out_w: usize,
out_h: usize,
h_phase: f64,
v_phase: f64,
v_top: bool,
) -> Result<Self, ResampleError> {
let fail_overflow =
|| ResampleError::Overflow(PlanGeometry::new(frame_w, luma_h, out_w, out_h));
let fail_alloc =
|| ResampleError::AllocationFailed(PlanGeometry::new(frame_w, luma_h, out_w, out_h));
let fail = |e: AxisError| match e {
AxisError::Overflow => fail_overflow(),
AxisError::Alloc => fail_alloc(),
};
let h = AxisSpans::area(frame_w, out_w).map_err(fail)?;
let (v, denom_h) = if v_top {
let denom_h = luma_h.checked_mul(2).ok_or_else(fail_overflow)?;
(
AxisSpans::area_chroma_phased_v_top(luma_h, out_h).map_err(fail)?,
denom_h,
)
} else if v_phase == 0.0 {
(AxisSpans::area_halved(luma_h, out_h).map_err(fail)?, luma_h)
} else {
let denom_h = luma_h.checked_mul(2).ok_or_else(fail_overflow)?;
(
AxisSpans::area_chroma_phased_v(luma_h, out_h).map_err(fail)?,
denom_h,
)
};
Ok(Self {
src_w: frame_w,
src_h: denom_h,
out_w,
out_h,
kind: SpanKind::Area,
h,
v,
filter_h: None,
filter_v: None,
filter_h_chroma: None,
filter_v_chroma: None,
h_phase,
v_phase,
v_top,
})
}
#[cfg(feature = "yuv-planar")]
pub(crate) fn area_chroma_410(
luma_w: usize,
luma_h: usize,
out_w: usize,
out_h: usize,
h_phase: f64,
v_phase: f64,
) -> Result<Self, ResampleError> {
let fail_overflow = || ResampleError::Overflow(PlanGeometry::new(luma_w, luma_h, out_w, out_h));
let fail = |e: AxisError| match e {
AxisError::Overflow => fail_overflow(),
AxisError::Alloc => {
ResampleError::AllocationFailed(PlanGeometry::new(luma_w, luma_h, out_w, out_h))
}
};
let chroma_w = luma_w.div_ceil(4);
let (h, denom_w) = if h_phase == 0.0 {
(
AxisSpans::area_subsampled(luma_w, out_w, 4).map_err(fail)?,
luma_w,
)
} else {
let denom_w = luma_w.checked_mul(8).ok_or_else(fail_overflow)?;
(
AxisSpans::area_chroma_phased_h_centered_4(luma_w, chroma_w, out_w).map_err(fail)?,
denom_w,
)
};
let v = AxisSpans::area_subsampled(luma_h, out_h, 4).map_err(fail)?;
Ok(Self {
src_w: denom_w,
src_h: luma_h,
out_w,
out_h,
kind: SpanKind::Area,
h,
v,
filter_h: None,
filter_v: None,
filter_h_chroma: None,
filter_v_chroma: None,
h_phase,
v_phase,
v_top: false,
})
}
#[cfg(feature = "yuv-planar")]
pub(crate) fn area_chroma_411(
luma_w: usize,
frame_h: usize,
out_w: usize,
out_h: usize,
h_phase: f64,
v_phase: f64,
) -> Result<Self, ResampleError> {
let fail_overflow =
|| ResampleError::Overflow(PlanGeometry::new(luma_w, frame_h, out_w, out_h));
let fail = |e: AxisError| match e {
AxisError::Overflow => fail_overflow(),
AxisError::Alloc => {
ResampleError::AllocationFailed(PlanGeometry::new(luma_w, frame_h, out_w, out_h))
}
};
let chroma_w = luma_w.div_ceil(4);
let (h, denom_w) = if h_phase == 0.0 {
(
AxisSpans::area_subsampled(luma_w, out_w, 4).map_err(fail)?,
luma_w,
)
} else {
let denom_w = luma_w.checked_mul(8).ok_or_else(fail_overflow)?;
(
AxisSpans::area_chroma_phased_h_centered_4(luma_w, chroma_w, out_w).map_err(fail)?,
denom_w,
)
};
let v = AxisSpans::area(frame_h, out_h).map_err(fail)?;
Ok(Self {
src_w: denom_w,
src_h: frame_h,
out_w,
out_h,
kind: SpanKind::Area,
h,
v,
filter_h: None,
filter_v: None,
filter_h_chroma: None,
filter_v_chroma: None,
h_phase,
v_phase,
v_top: false,
})
}
#[cfg(feature = "yuv-planar")]
pub(crate) fn area_chroma_422(
chroma_w: usize,
luma_h: usize,
out_w: usize,
out_h: usize,
h_phase: f64,
v_phase: f64,
) -> Result<Self, ResampleError> {
debug_assert_eq!(v_phase, 0.0, "4:2:2 carries no vertical chroma phase");
if h_phase == 0.0 {
return Self::area(chroma_w, luma_h, out_w, out_h);
}
let geometry = || PlanGeometry::new(chroma_w, luma_h, out_w, out_h);
let fail = |e: AxisError| match e {
AxisError::Overflow => ResampleError::Overflow(geometry()),
AxisError::Alloc => ResampleError::AllocationFailed(geometry()),
};
let denom_w = chroma_w
.checked_mul(8)
.ok_or_else(|| ResampleError::Overflow(geometry()))?;
let h = AxisSpans::area_chroma_phased_h_centered(chroma_w, out_w).map_err(fail)?;
let v = AxisSpans::area(luma_h, out_h).map_err(fail)?;
Ok(Self {
src_w: denom_w,
src_h: luma_h,
out_w,
out_h,
kind: SpanKind::Area,
h,
v,
filter_h: None,
filter_v: None,
filter_h_chroma: None,
filter_v_chroma: None,
h_phase,
v_phase,
v_top: false,
})
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn src_w(&self) -> usize {
self.src_w
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn src_h(&self) -> usize {
self.src_h
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn kind(&self) -> SpanKind {
self.kind
}
#[cfg(feature = "yuv-planar")]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) const fn has_chroma_phase(&self) -> bool {
self.h_phase != 0.0 || self.v_phase != 0.0
}
#[cfg(feature = "yuv-planar")]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) const fn has_chroma_v_phase(&self) -> bool {
self.v_phase != 0.0
}
#[cfg(feature = "yuv-planar")]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) const fn has_chroma_v_top(&self) -> bool {
self.v_top
}
#[cfg(feature = "yuv-planar")]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) const fn has_chroma_h_phase(&self) -> bool {
self.h_phase != 0.0
}
#[allow(dead_code)]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) const fn unsupported_filter(&self) -> ResampleError {
ResampleError::UnsupportedFilter(PlanGeometry::new(
self.src_w, self.src_h, self.out_w, self.out_h,
))
}
#[allow(dead_code)]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) const fn linear_domain_unsupported(&self) -> ResampleError {
ResampleError::LinearDomainUnsupported(PlanGeometry::new(
self.src_w, self.src_h, self.out_w, self.out_h,
))
}
#[allow(dead_code)]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) const fn premultiplied_domain_unsupported(&self) -> ResampleError {
ResampleError::PremultipliedDomainUnsupported(PlanGeometry::new(
self.src_w, self.src_h, self.out_w, self.out_h,
))
}
#[cfg_attr(not(any(feature = "yuv-planar", feature = "rgb")), allow(dead_code))]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn filter_h(&self) -> Option<&FilterAxis> {
self.filter_h.as_ref()
}
#[cfg_attr(not(any(feature = "yuv-planar", feature = "rgb")), allow(dead_code))]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn filter_v(&self) -> Option<&FilterAxis> {
self.filter_v.as_ref()
}
#[cfg(feature = "yuv-planar")]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn filter_h_chroma(&self) -> Option<&FilterAxis> {
self.filter_h_chroma.as_ref()
}
#[cfg(feature = "yuv-planar")]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn filter_v_chroma(&self) -> Option<&FilterAxis> {
self.filter_v_chroma.as_ref()
}
#[allow(dead_code)]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) const fn is_bicublin(&self) -> bool {
self.filter_h_chroma.is_some()
}
#[allow(dead_code)]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn ensure_single_kernel_filter(&self) -> Result<(), ResampleError> {
if self.is_bicublin() {
return Err(self.unsupported_filter());
}
Ok(())
}
#[cfg_attr(not(any(feature = "yuv-planar", feature = "rgb")), allow(dead_code))]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) const fn h(&self) -> &AxisSpans {
&self.h
}
#[cfg_attr(not(any(feature = "yuv-planar", feature = "rgb")), allow(dead_code))]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) const fn v(&self) -> &AxisSpans {
&self.v
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn out_w(&self) -> usize {
self.out_w
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn out_h(&self) -> usize {
self.out_h
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn out_dims(&self) -> (usize, usize) {
(self.out_w, self.out_h)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct UpscaleUnsupported {
src_w: usize,
src_h: usize,
out_w: usize,
out_h: usize,
}
impl UpscaleUnsupported {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new(src_w: usize, src_h: usize, out_w: usize, out_h: usize) -> Self {
Self {
src_w,
src_h,
out_w,
out_h,
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn src_w(&self) -> usize {
self.src_w
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn src_h(&self) -> usize {
self.src_h
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn out_w(&self) -> usize {
self.out_w
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn out_h(&self) -> usize {
self.out_h
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ZeroOutputDimension {
out_w: usize,
out_h: usize,
}
impl ZeroOutputDimension {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new(out_w: usize, out_h: usize) -> Self {
Self { out_w, out_h }
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn out_w(&self) -> usize {
self.out_w
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn out_h(&self) -> usize {
self.out_h
}
}
#[cfg_attr(not(any(feature = "yuv-planar", feature = "rgb")), allow(dead_code))]
fn round_div_half_up(a: u64, d: u64) -> u64 {
let q = a / d;
let r = a % d;
q + u64::from(r >= d - d / 2)
}
#[cfg_attr(not(feature = "gray"), allow(dead_code))]
fn round_div_half_up_u128(a: u128, d: u128) -> u128 {
let q = a / d;
let r = a % d;
q + u128::from(r >= d - d / 2)
}
#[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) trait AreaSample: Copy + Default {
const NEEDS_SIMD_STAGING: bool = true;
type HSum: Copy + Default;
type VAcc: Copy + Default;
fn h_sum_fits(src_w: u64) -> bool;
fn denom_fits(denom: u64) -> bool;
fn h_reduce(
row: &[Self],
channels: usize,
h: &AxisSpans,
padded: Option<&crate::row::PaddedSpans>,
h_tmp: &mut [Self::HSum],
use_simd: bool,
);
fn v_accumulate(acc: &mut [Self::VAcc], h_tmp: &[Self::HSum], w: u64, use_simd: bool);
fn finalize(acc: Self::VAcc, denom: u64) -> Self;
}
#[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"
))]
impl AreaSample for u8 {
type HSum = u32;
type VAcc = u64;
#[cfg_attr(not(tarpaulin), inline(always))]
fn h_sum_fits(src_w: u64) -> bool {
src_w <= u64::from(u32::MAX) / 255
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn denom_fits(denom: u64) -> bool {
denom <= u64::MAX / 255
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn h_reduce(
row: &[u8],
channels: usize,
h: &AxisSpans,
padded: Option<&crate::row::PaddedSpans>,
h_tmp: &mut [u32],
use_simd: bool,
) {
crate::row::area_h_reduce_row(
row, channels, &h.starts, &h.offsets, &h.weights, padded, h_tmp, use_simd,
);
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn v_accumulate(acc: &mut [u64], h_tmp: &[u32], w: u64, use_simd: bool) {
crate::row::area_v_accumulate(acc, h_tmp, w, use_simd);
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn finalize(acc: u64, denom: u64) -> u8 {
round_div_half_up(acc, denom) as u8
}
}
#[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"
))]
#[cfg_attr(not(feature = "rgb"), allow(dead_code))]
impl AreaSample for u16 {
type HSum = u64;
type VAcc = u64;
#[cfg_attr(not(tarpaulin), inline(always))]
fn h_sum_fits(src_w: u64) -> bool {
src_w <= u64::MAX / 65535
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn denom_fits(denom: u64) -> bool {
denom <= u64::MAX / 65535
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn h_reduce(
row: &[u16],
channels: usize,
h: &AxisSpans,
padded: Option<&crate::row::PaddedSpans>,
h_tmp: &mut [u64],
use_simd: bool,
) {
crate::row::area_h_reduce_row_u16(
row, channels, &h.starts, &h.offsets, &h.weights, padded, h_tmp, use_simd,
);
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn v_accumulate(acc: &mut [u64], h_tmp: &[u64], w: u64, use_simd: bool) {
crate::row::area_v_accumulate_u16(acc, h_tmp, w, use_simd);
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn finalize(acc: u64, denom: u64) -> u16 {
round_div_half_up(acc, denom) as u16
}
}
#[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"
))]
#[allow(dead_code)]
impl AreaSample for f32 {
type HSum = f64;
type VAcc = f64;
#[cfg_attr(not(tarpaulin), inline(always))]
fn h_sum_fits(_src_w: u64) -> bool {
true
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn denom_fits(_denom: u64) -> bool {
true
}
fn h_reduce(
row: &[f32],
channels: usize,
h: &AxisSpans,
padded: Option<&crate::row::PaddedSpans>,
h_tmp: &mut [f64],
use_simd: bool,
) {
crate::row::area_h_reduce_row_f32(
row, channels, &h.starts, &h.offsets, &h.weights, padded, h_tmp, use_simd,
);
}
fn v_accumulate(acc: &mut [f64], h_tmp: &[f64], w: u64, use_simd: bool) {
crate::row::area_v_accumulate_f32(acc, h_tmp, w as f64, use_simd);
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn finalize(acc: f64, denom: u64) -> f32 {
(acc / denom as f64) as f32
}
}
#[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"
))]
#[cfg_attr(not(feature = "gray"), allow(dead_code))]
impl AreaSample for u32 {
const NEEDS_SIMD_STAGING: bool = false;
type HSum = u128;
type VAcc = u128;
#[cfg_attr(not(tarpaulin), inline(always))]
fn h_sum_fits(src_w: u64) -> bool {
u128::from(src_w) <= u128::MAX / u128::from(u32::MAX)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn denom_fits(denom: u64) -> bool {
u128::from(denom) <= u128::MAX / u128::from(u32::MAX)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn h_reduce(
row: &[u32],
channels: usize,
h: &AxisSpans,
_padded: Option<&crate::row::PaddedSpans>,
h_tmp: &mut [u128],
_use_simd: bool,
) {
crate::row::area_h_reduce_row_u32(row, channels, &h.starts, &h.offsets, &h.weights, h_tmp);
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn v_accumulate(acc: &mut [u128], h_tmp: &[u128], w: u64, _use_simd: bool) {
crate::row::area_v_accumulate_u32(acc, h_tmp, u128::from(w));
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn finalize(acc: u128, denom: u64) -> u32 {
round_div_half_up_u128(acc, u128::from(denom)) as u32
}
}
#[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"
))]
#[derive(Debug)]
pub(crate) struct AreaStream<S: AreaSample> {
h: AxisSpans,
v: AxisSpans,
channels: usize,
denom: u64,
h_tmp: Vec<S::HSum>,
acc: Vec<S::VAcc>,
acc_next: Vec<S::VAcc>,
out_tmp: Vec<S>,
h_padded: Option<crate::row::PaddedSpans>,
cur_out: usize,
next_open: bool,
next_y: usize,
}
#[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"
))]
impl<S: AreaSample> AreaStream<S> {
pub(crate) fn new(
h: &AxisSpans,
v: &AxisSpans,
src_w: usize,
src_h: usize,
channels: usize,
) -> Result<Self, ResampleError> {
let geometry = || PlanGeometry::new(src_w, src_h, h.out_len(), v.out_len());
let window = v.window_depth();
if window > 2 {
return Err(ResampleError::UpscaleUnsupported(UpscaleUnsupported::new(
src_w,
src_h,
h.out_len(),
v.out_len(),
)));
}
if !S::h_sum_fits(src_w as u64) {
return Err(ResampleError::Overflow(geometry()));
}
let denom = (src_w as u64)
.checked_mul(src_h as u64)
.filter(|d| S::denom_fits(*d))
.ok_or_else(|| ResampleError::Overflow(geometry()))?;
let n = h
.out_len()
.checked_mul(channels)
.ok_or_else(|| ResampleError::Overflow(geometry()))?;
let alloc = |_| ResampleError::AllocationFailed(geometry());
let h = h
.try_clone()
.map_err(|_| ResampleError::AllocationFailed(geometry()))?;
let v = v
.try_clone()
.map_err(|_| ResampleError::AllocationFailed(geometry()))?;
let h_padded = if S::NEEDS_SIMD_STAGING {
crate::row::PaddedSpans::build(&h.starts, &h.offsets, &h.weights)
} else {
None
};
Ok(Self {
h,
v,
channels,
denom,
h_tmp: try_zeroed(n).map_err(alloc)?,
acc: try_zeroed(n).map_err(alloc)?,
acc_next: if window >= 2 {
try_zeroed(n).map_err(alloc)?
} else {
Vec::new()
},
out_tmp: try_zeroed(n).map_err(alloc)?,
h_padded,
cur_out: 0,
next_open: false,
next_y: 0,
})
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) const fn next_y(&self) -> usize {
self.next_y
}
pub(crate) fn reset(&mut self) {
self.acc.fill(S::VAcc::default());
self.acc_next.fill(S::VAcc::default());
self.cur_out = 0;
self.next_open = false;
self.next_y = 0;
}
pub(crate) fn feed_row(
&mut self,
y: usize,
row: &[S],
use_simd: bool,
mut emit: impl FnMut(usize, &[S]),
) -> Result<(), ResampleError> {
if y != self.next_y {
return Err(ResampleError::OutOfSequenceRow(OutOfSequenceRow::new(
self.next_y,
y,
)));
}
self.next_y += 1;
if self.cur_out >= self.v.out_len() {
return Ok(());
}
S::h_reduce(
row,
self.channels,
&self.h,
self.h_padded.as_ref(),
&mut self.h_tmp,
use_simd,
);
loop {
let (start, weights) = self.v.span(self.cur_out);
let Some(idx) = y.checked_sub(start) else {
return Ok(());
};
let Some(&w) = weights.get(idx) else {
return Ok(());
};
let last = idx + 1 == weights.len();
S::v_accumulate(&mut self.acc, &self.h_tmp, w as u64, use_simd);
if !last {
self.fold_lookahead(y, use_simd);
return Ok(());
}
for (o, a) in self.out_tmp.iter_mut().zip(self.acc.iter_mut()) {
*o = S::finalize(*a, self.denom);
*a = S::VAcc::default();
}
emit(self.cur_out, &self.out_tmp);
self.cur_out += 1;
if self.next_open {
core::mem::swap(&mut self.acc, &mut self.acc_next);
self.next_open = false;
if self.cur_out >= self.v.out_len() {
return Ok(());
}
continue;
}
if self.cur_out >= self.v.out_len() || self.v.span(self.cur_out).0 != y {
return Ok(());
}
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn fold_lookahead(&mut self, y: usize, use_simd: bool) {
let nxt = self.cur_out + 1;
if nxt >= self.v.out_len() {
return;
}
let (start, weights) = self.v.span(nxt);
let Some(idx) = y.checked_sub(start) else {
return;
};
let Some(&w) = weights.get(idx) else {
return;
};
debug_assert!(
nxt + 1 >= self.v.out_len() || self.v.span(nxt + 1).0 > y,
"phased-V window exceeded two open output rows"
);
S::v_accumulate(&mut self.acc_next, &self.h_tmp, w as u64, use_simd);
self.next_open = true;
}
}
#[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) trait RowResampler<S> {
fn feed_row<F: FnMut(usize, &[S])>(
&mut self,
y: usize,
row: &[S],
use_simd: bool,
emit: F,
) -> Result<(), ResampleError>;
}
#[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"
))]
impl<S: AreaSample> RowResampler<S> for AreaStream<S> {
#[cfg_attr(not(tarpaulin), inline(always))]
fn feed_row<F: FnMut(usize, &[S])>(
&mut self,
y: usize,
row: &[S],
use_simd: bool,
emit: F,
) -> Result<(), ResampleError> {
AreaStream::feed_row(self, y, row, use_simd, emit)
}
}
#[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"
))]
impl<S: FilterSample> RowResampler<S> for FilterStream<S> {
#[cfg_attr(not(tarpaulin), inline(always))]
fn feed_row<F: FnMut(usize, &[S])>(
&mut self,
y: usize,
row: &[S],
use_simd: bool,
emit: F,
) -> Result<(), ResampleError> {
FilterStream::feed_row(self, y, row, use_simd, emit)
}
}
#[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"
))]
impl<S, R: RowResampler<S> + ?Sized> RowResampler<S> for std::boxed::Box<R> {
#[cfg_attr(not(tarpaulin), inline(always))]
fn feed_row<F: FnMut(usize, &[S])>(
&mut self,
y: usize,
row: &[S],
use_simd: bool,
emit: F,
) -> Result<(), ResampleError> {
(**self).feed_row(y, row, use_simd, emit)
}
}
#[cfg(all(test, feature = "std", any(feature = "yuv-planar", feature = "rgb")))]
std::thread_local! {
static FORCE_BOX_FAILURE: core::cell::Cell<bool> = const { core::cell::Cell::new(false) };
}
#[cfg(all(test, feature = "std", any(feature = "yuv-planar", feature = "rgb")))]
pub(crate) fn arm_box_failure() {
FORCE_BOX_FAILURE.with(|f| f.set(true));
}
#[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) fn try_box<T>(
value: T,
) -> Result<std::boxed::Box<T>, std::collections::TryReserveError> {
#[cfg(all(test, feature = "std", any(feature = "yuv-planar", feature = "rgb")))]
if FORCE_BOX_FAILURE.with(|f| f.take()) {
let mut probe = std::vec::Vec::<T>::new();
probe.try_reserve_exact(usize::MAX)?;
}
let mut backing = std::vec::Vec::with_capacity(0);
backing.try_reserve_exact(1)?;
backing.push(value);
let boxed_slice: std::boxed::Box<[T]> = backing.into_boxed_slice();
Ok(unsafe { std::boxed::Box::from_raw(std::boxed::Box::into_raw(boxed_slice).cast::<T>()) })
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct OutOfSequenceRow {
expected: usize,
got: usize,
}
impl OutOfSequenceRow {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new(expected: usize, got: usize) -> Self {
Self { expected, got }
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn expected(&self) -> usize {
self.expected
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn got(&self) -> usize {
self.got
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PlanGeometry {
src_w: usize,
src_h: usize,
out_w: usize,
out_h: usize,
}
impl PlanGeometry {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new(src_w: usize, src_h: usize, out_w: usize, out_h: usize) -> Self {
Self {
src_w,
src_h,
out_w,
out_h,
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn src_w(&self) -> usize {
self.src_w
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn src_h(&self) -> usize {
self.src_h
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn out_w(&self) -> usize {
self.out_w
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn out_h(&self) -> usize {
self.out_h
}
}
#[derive(Debug, Clone, Copy, PartialEq, IsVariant, TryUnwrap, Unwrap, Error)]
#[non_exhaustive]
pub enum ResampleError {
#[error(
"resample output {}x{} exceeds source {}x{} (upscaling is unsupported)",
.0.out_w(), .0.out_h(), .0.src_w(), .0.src_h()
)]
UpscaleUnsupported(UpscaleUnsupported),
#[error(
"resample output dimensions must be nonzero, got {}x{}",
.0.out_w(), .0.out_h()
)]
ZeroOutputDimension(ZeroOutputDimension),
#[error(
"resample plan geometry overflows usize: source {}x{}, output {}x{}",
.0.src_w(), .0.src_h(), .0.out_w(), .0.out_h()
)]
Overflow(PlanGeometry),
#[error(
"resample plan allocation failed: source {}x{}, output {}x{}",
.0.src_w(), .0.src_h(), .0.out_w(), .0.out_h()
)]
AllocationFailed(PlanGeometry),
#[error(
"resample stream fed source row {}, expected row {}",
.0.got(), .0.expected()
)]
OutOfSequenceRow(OutOfSequenceRow),
#[error(
"filter kernel support {} is invalid for source extent {} (must be finite, > 0, and bounded)",
.0.support(), .0.in_size()
)]
InvalidFilterSupport(InvalidFilterSupport),
#[error(
"filter resampling is unsupported for this source format (output {}x{} from source {}x{}); \
route it through Rgb24, Rgb48, or Grayf32",
.0.out_w(), .0.out_h(), .0.src_w(), .0.src_h()
)]
UnsupportedFilter(PlanGeometry),
#[error(
"the Linear averaging domain needs the `rgb` feature for its RGB decode \
(output {}x{} from source {}x{}); enable `rgb` or use AveragingDomain::Encoded",
.0.out_w(), .0.out_h(), .0.src_w(), .0.src_h()
)]
LinearDomainUnsupported(PlanGeometry),
#[error(
"the Premultiplied averaging domain is unsupported for this format \
(output {}x{} from source {}x{}): it has no alpha channel; \
use AveragingDomain::Encoded",
.0.out_w(), .0.out_h(), .0.src_w(), .0.src_h()
)]
PremultipliedDomainUnsupported(PlanGeometry),
}
#[cfg(all(
test,
feature = "std",
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 cv2_goldens;
#[cfg(all(
test,
feature = "std",
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 pil_goldens;
#[cfg(all(test, feature = "std"))]
mod tests;