diff --git a/crates/pipeline/src/geometry.rs b/crates/pipeline/src/geometry.rs index cc2b11ca72..32516eaae2 100644 --- a/crates/pipeline/src/geometry.rs +++ b/crates/pipeline/src/geometry.rs @@ -316,7 +316,9 @@ impl Frame { pub fn sample_plan(&self, src_w: usize, src_h: usize, w: usize, h: usize) -> SamplePlan { let (ow, oh) = if self.orient.swaps_axes() { (src_h, src_w) } else { (src_w, src_h) }; let crop_px = self.crop.rect_px(self.ow, self.oh); - let k = crop_px.width() / w as f64; + // the larger of the two axes' reductions: the prefilter then takes every axis at least as small + // as its output needs, so the bilinear pass after it never minifies (and aliases) either one + let k = (crop_px.width() / w as f64).max(crop_px.height() / h as f64); // Sharp exports use their selected filter for every downscale; keep smooth previews' threshold. let threshold = if self.downscale == Filter::Lanczos3 { 1.0 } else { 1.25 }; let prefilter = (k > threshold).then(|| (((ow as f64 / k).round() as usize).max(1), ((oh as f64 / k).round() as usize).max(1))); diff --git a/crates/pipeline/tests/sharp_downscale.rs b/crates/pipeline/tests/sharp_downscale.rs index 8d81593c69..da4fb57e09 100644 --- a/crates/pipeline/tests/sharp_downscale.rs +++ b/crates/pipeline/tests/sharp_downscale.rs @@ -21,3 +21,22 @@ fn mild_sharp_reductions_suppress_above_target_nyquist_aliases() { assert!(sharp < bilinear * 0.95, "{w}: residual alias power {sharp} vs unfiltered {bilinear}"); } } + +#[test] +fn a_height_only_reduction_is_prefiltered_too() { + // the width stays 1:1, the height shrinks 1000 → 900: the vertical carrier must not fold in + let source = Rgb32f::from_fn(100, 1000, |_, y| [0.5 + 0.25 * (std::f32::consts::TAU * 0.49 * y as f32).sin(); 3]); + let power = |image: &Rgb32f| { + let values: Vec = (12..image.height - 12).map(|y| f64::from(image.get(image.width / 2, y)[0])).collect(); + let mean = values.iter().sum::() / values.len() as f64; + values.iter().map(|v| (v - mean).powi(2)).sum::() / values.len() as f64 + }; + let mut frame = Frame::new(100, 1000, &DevelopSettings::default(), true); + frame.downscale = Filter::Lanczos3; + let (w, h) = (100, 900); + assert!(frame.sample_plan(100, 1000, w, h).prefilter.is_some(), "a reduction on either axis is prefiltered"); + let k = 1000.0 / h as f64; + let unfiltered = Rgb32f::from_fn(w, h, |x, y| source.sample_bilinear(x as f32 + 0.5, ((y as f64 + 0.5) * k) as f32)); + let (sharp, bilinear) = (power(&frame.sample(&source, w, h)), power(&unfiltered)); + assert!(sharp < bilinear * 0.95, "residual alias power {sharp} vs unfiltered {bilinear}"); +}