diff --git a/crates/algo/src/artistic_fx.rs b/crates/algo/src/artistic_fx.rs index a2c98e3c39..edd42a0591 100644 --- a/crates/algo/src/artistic_fx.rs +++ b/crates/algo/src/artistic_fx.rs @@ -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); diff --git a/crates/algo/src/blur.rs b/crates/algo/src/blur.rs index 7ca9d34f99..e9e64aff9b 100644 --- a/crates/algo/src/blur.rs +++ b/crates/algo/src/blur.rs @@ -168,13 +168,18 @@ fn boxes_for_gauss(sigma: f32, n: usize) -> Vec { (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> { 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, diff --git a/crates/algo/src/camera_raw.rs b/crates/algo/src/camera_raw.rs index 1a16612cc1..b783f39a54 100644 --- a/crates/algo/src/camera_raw.rs +++ b/crates/algo/src/camera_raw.rs @@ -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 = px.iter().map(|q| luma([q[0], q[1], q[2]])).collect(); let mut cb: Vec = px.iter().zip(&y).map(|(q, l)| q[2] - l).collect(); let mut cr: Vec = 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)); diff --git a/crates/algo/src/resample.rs b/crates/algo/src/resample.rs index 635a5a3079..d153b78cb0 100644 --- a/crates/algo/src/resample.rs +++ b/crates/algo/src/resample.rs @@ -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; diff --git a/crates/algo/src/resample/tests.rs b/crates/algo/src/resample/tests.rs index 6e410f1c5d..f4406da0d2 100644 --- a/crates/algo/src/resample/tests.rs +++ b/crates/algo/src/resample/tests.rs @@ -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]