2988: prefilter scale from both axes

Frame::sample_plan took the prefilter scale k from the width alone, so
a reduction on the height only (a 100x1000 source to 100x900) had k = 1,
skipped the prefilter and bilinear-minified the height, which aliases.

k is now the larger of the two axes' reductions. The prefilter shrinks
the source isotropically by k, and the bilinear pass after it then
reduces each axis by k_axis / k. With the larger k that ratio is at most
1 on both axes, so the bilinear pass never minifies; the smaller k would
leave the more-reduced axis minified by bilinear and aliasing. The cost
of the larger k is a slight bilinear upsample on the less-reduced axis,
which only differs from 1 when the crop and output aspects differ (by
rounding, or a stretched size). Fits with matching aspects keep the
same prefilter size as before.

A test covers a height-only Lanczos-3 reduction: it now prefilters and
suppresses a vertical carrier above the output's Nyquist limit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Fnx9Mvap24RDADn7A3Wnwq
This commit is contained in:
radialmonsterandClaude Opus 5.5 committed 2026-11-07 04:21:04 -05:00
1 parent f8e6222f45
commit cd275006e5
2 files changed
+22 -1

No files matched your search

+3 -1
View File
@@ -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)));
+19
View File
@@ -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<f64> = (12..image.height - 12).map(|y| f64::from(image.get(image.width / 2, y)[0])).collect();
let mean = values.iter().sum::<f64>() / values.len() as f64;
values.iter().map(|v| (v - mean).powi(2)).sum::<f64>() / 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}");
}