fix(algo): hold raw filter parameters to their ranges (Water Paper, Box Blur, Lanczos, Camera Raw noise) (#11199)

This commit is contained in:
Dylan M. Taylor authored and GitHub committed 2026-11-11 16:27:11 -05:00
1 parent 556382bab8
commit 3b43cb451a
5 files changed
+44 -9

No files matched your search

+8 -2
View File
@@ -447,7 +447,7 @@ pub(crate) fn effect_reach(e: &GalleryEffect) -> i32 {
F::Plaster => gs(0.5 + g("smoothness") * 0.8) + gs(1.5) + 2,
F::Stamp => gs(0.3 + g("smoothness") * 0.3),
F::TornEdges => gs(1.0),
F::WaterPaper => 2 * (g("fiberLength") / 6.0) as i32 + 2,
F::WaterPaper => 2 * (fiber_length(e) / 6.0) as i32 + 2,
F::GlowingEdges => gs(0.3 + g("smoothness") * 0.25) + gs(g("edgeWidth") * 0.4) + 2,
F::Craquelure => 0,
F::Patchwork => patch_size(e).ceil() as i32 + 2,
@@ -518,6 +518,12 @@ fn plastic_sigma(e: &GalleryEffect) -> f32 {
fn watercolor_radius(e: &GalleryEffect) -> usize {
1 + ((15.0 - e.get("brushDetail")) / 2.5) as usize
}
/// Water Paper's fiber length within its parameter range (3..=50): raw gallery values bypass the
/// setter's clamp, and the reach estimate and the blur radius must agree on one bounded value.
fn fiber_length(e: &GalleryEffect) -> f32 {
e.get("fiberLength").clamp(3.0, 50.0)
}
fn basrelief_sigma(e: &GalleryEffect) -> f32 {
0.3 + e.get("smoothness") * 0.5 + (15.0 - e.get("detail")) * 0.12
}
@@ -1178,7 +1184,7 @@ fn apply_sketch(e: &GalleryEffect, win: &mut Win, ctx: &Ctx) -> bool {
}
}
F::WaterPaper => {
let fl = g("fiberLength");
let fl = fiber_length(e);
let (br, co) = (g("brightness") / 100.0, g("contrast") / 100.0);
let r = (fl / 6.0) as usize;
line_blur3(&mut win.c, w, h, (1, 0), r);
+6 -1
View File
@@ -168,13 +168,18 @@ fn boxes_for_gauss(sigma: f32, n: usize) -> Vec<usize> {
(0..n).map(|i| if i < m { wl as usize } else { wu as usize }).collect()
}
/// Largest box-blur radius run (far beyond any real use).
const MAX_BOX_RADIUS: f32 = 16384.0;
/// The box widths (odd) a blur runs as running-sum passes along each axis, or `None` when it uses a
/// direct kernel (small radii, where the kernel is quicker).
pub(crate) fn box_widths(params: &FilterParams) -> Option<Vec<usize>> {
match *params {
FilterParams::GaussianBlur { radius } if radius > 4.0 => Some(boxes_for_gauss(radius, 3)),
FilterParams::BoxBlur { radius } => {
let r = radius.max(0.0).round() as usize;
// Capped: the line scratch grows with the radius, and `2 * r + 1` must not overflow
// (Photoshop's largest Box Blur radius is 999).
let r = radius.clamp(0.0, MAX_BOX_RADIUS).round() as usize;
(r > 4).then(|| vec![2 * r + 1])
}
_ => None,
+14 -6
View File
@@ -1029,23 +1029,25 @@ fn dehaze(px: &mut [[f32; 4]], w: usize, h: usize, d: &Dehaze) {
}
fn detail(px: &mut [[f32; 4]], w: usize, h: usize, p: &CameraRaw, ps: f32) {
// The controls' documented 0..=100 range: raw values drive the filter radii below.
let [nl, nld, nc, ncd] = [p.noise_luminance, p.noise_luminance_detail, p.noise_color, p.noise_color_detail].map(|v| v.clamp(0.0, 100.0));
// Work in Y + chroma (BT.709 luma, B−Y, R−Y).
let mut y: Vec<f32> = px.iter().map(|q| luma([q[0], q[1], q[2]])).collect();
let mut cb: Vec<f32> = px.iter().zip(&y).map(|(q, l)| q[2] - l).collect();
let mut cr: Vec<f32> = px.iter().zip(&y).map(|(q, l)| q[0] - l).collect();
if p.noise_luminance > 0.0 {
let n = p.noise_luminance / 100.0;
if nl > 0.0 {
let n = nl / 100.0;
let r = ((1.0 + 3.0 * n) * ps).round().max(1.0) as usize;
let smooth = guided(&y, &y, w, h, r, (n * 0.06).powi(2) + 1e-6);
let keep = p.noise_luminance_detail / 100.0 * 0.5;
let keep = nld / 100.0 * 0.5;
for i in 0..w * h {
y[i] = smooth[i] + (y[i] - smooth[i]) * keep;
}
}
if p.noise_color > 0.0 {
let sigma = (p.noise_color / 100.0 * 6.0 * ps).max(0.3);
if nc > 0.0 {
let sigma = (nc / 100.0 * 6.0 * ps).max(0.3);
let (bcb, bcr) = (gauss(&cb, w, h, sigma), gauss(&cr, w, h, sigma));
let keep = p.noise_color_detail / 100.0 * 0.3;
let keep = ncd / 100.0 * 0.3;
for i in 0..w * h {
cb[i] = bcb[i] + (cb[i] - bcb[i]) * keep;
cr[i] = bcr[i] + (cr[i] - bcr[i]) * keep;
@@ -1306,6 +1308,12 @@ mod tests {
let mut nr = noisy.clone();
develop(&mut nr, w, h, &CameraRaw { noise_luminance: 80.0, ..Default::default() }, false);
assert!(var(&nr) < var(&noisy) * 0.5);
// Out-of-range noise settings are held to their 0..=100 range, not turned into huge radii.
let mut wild = noisy.clone();
let hostile =
CameraRaw { noise_luminance: 1.0e30, noise_color: 1.0e30, noise_luminance_detail: 1.0e30, noise_color_detail: 1.0e30, ..Default::default() };
develop(&mut wild, w, h, &hostile, false);
assert!(wild.iter().all(|q| q[0].is_finite()));
let mut sh = noisy.clone();
develop(&mut sh, w, h, &CameraRaw { sharpen_amount: 100.0, ..Default::default() }, false);
assert!(var(&sh) > var(&noisy));
+4
View File
@@ -233,6 +233,10 @@ fn weights(o: i32, scale: f64, kernel: Kernel, edge: Option<(i32, i32)>, content
let sum: f64 = w.iter().sum();
(lo, w.into_iter().map(|v| v / sum).collect())
}
// Past a 4096x reduction Lanczos would build a weight per covered source sample (tens of
// thousands per output sample); the box average that folds the beyond-edge weights is
// indistinguishable at that scale.
Kernel::Lanczos if scale < 1.0 / 4096.0 => weights(o, scale, Kernel::Bilinear, edge, content),
Kernel::Lanczos => {
let stretch = if scale < 1.0 { 1.0 / scale } else { 1.0 };
let u = (o as f64 + 0.5) / scale - 0.5;
+12
View File
@@ -401,6 +401,18 @@ fn nearest_neighbor_reduction_does_not_average() {
}
}
/// A reduction beyond 4096x by Lanczos must not build a weight per covered source sample.
#[test]
fn a_tiny_lanczos_scale_keeps_its_weights_small() {
let scale = 1.0e-7;
for o in [0, 1] {
let (_, w) = weights(o, scale, Kernel::Lanczos, None, (0, 63));
assert!(w.len() < 1024, "{} weights", w.len());
let sum: f64 = w.iter().sum();
assert!((sum - 1.0).abs() < 1e-6, "{sum}");
}
}
/// #9212: a bicubic shrink of both axes stores the image between its passes with one axis at the
/// old size, larger than the result; single-pass filters and growing axes store nothing larger.
#[test]