Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(algo): blur small sigmas instead of silently no-oping (#706) (#783)

bfentyf15fb4ee6e7c
diff --git a/crates/algo/src/fxutil.rs b/crates/algo/src/fxutil.rs
index dec6d7294..21e3b809a 100644
--- a/crates/algo/src/fxutil.rs
+++ b/crates/algo/src/fxutil.rs
@@ -172,7 +172,17 @@ pub(crate) fn gauss_box_radii(sigma: f32) -> [usize; 3] {
let wl = wl.max(1);
let wu = wl + 2;
let m = ((12.0 * sigma * sigma - n * (wl * wl) as f32 - 4.0 * n * wl as f32 - 3.0 * n) / (-4.0 * wl as f32 - 4.0)).round().clamp(0.0, n) as usize;
- std::array::from_fn(|i| if i < m { (wl as usize - 1) / 2 } else { (wu as usize - 1) / 2 })
+ let mut radii = std::array::from_fn(|i| if i < m { (wl as usize - 1) / 2 } else { (wu as usize - 1) / 2 });
+ // Kovesi's rounded triple degenerates below sigma ~ 0.577: `wl` floors to 1 and every
+ // radius becomes 0, so the passes would be no-ops and callers with documented small
+ // sigmas (Camera Raw `sharpenRadius` 0.5, colour noise, HDR Toning `radius` 1) would
+ // silently change nothing (issue #706). One radius-1 pass is the smallest real blur;
+ // it only engages where the ideal widths are already at their floor, leaving larger
+ // sigmas exactly as the paper prescribes.
+ if radii.iter().all(|&r| r == 0) {
+ radii[0] = 1;
+ }
+ radii
}
/// Approximate Gaussian blur (three box passes) of an interleaved buffer.
diff --git a/crates/engine/tests/gauss_small_sigma.rs b/crates/engine/tests/gauss_small_sigma.rs
new file mode 100644
index 000000000..0b0d267ba
--- /dev/null
+++ b/crates/engine/tests/gauss_small_sigma.rs
@@ -0,0 +1,35 @@
+//! Regression tests for issue #706: sigmas in [0.3, ~0.577) must blur, not silently no-op.
+use photocraft_engine::Session;
+use serde_json::json;
+
+fn px(s: &mut Session, x: i32, y: i32) -> Vec<f32> {
+ serde_json::from_value(s.execute("document.pixel", json!({"x": x, "y": y})).unwrap()).unwrap()
+}
+
+fn edge_doc() -> Session {
+ let mut s = Session::new();
+ // Mid-tone, slightly chromatic halves so sharpening is not clipped away.
+ s.execute("file.new", json!({"width": 32, "height": 32, "background": "#602020"})).unwrap();
+ s.execute("select.rect", json!({"x": 16, "y": 0, "width": 16, "height": 32})).unwrap();
+ s.execute("edit.fill", json!({"contents": "color", "color": "#206020"})).unwrap();
+ s.execute("select.deselect", json!({})).unwrap();
+ s
+}
+
+fn row(s: &mut Session) -> Vec<Vec<f32>> {
+ (12..20).map(|x| px(s, x, 16)).collect()
+}
+
+#[test]
+fn camera_raw_sharpen_radius_half_noop() {
+ let mut base_s = edge_doc();
+ let base = row(&mut base_s);
+ let mut ctl_s = edge_doc();
+ ctl_s.execute("filter.cameraRaw", json!({"sharpenAmount":150,"sharpenRadius":3.0,"sharpenDetail":100})).unwrap();
+ let ctl = row(&mut ctl_s);
+ let mut half_s = edge_doc();
+ half_s.execute("filter.cameraRaw", json!({"sharpenAmount":150,"sharpenRadius":0.5,"sharpenDetail":100})).unwrap();
+ let half = row(&mut half_s);
+ assert_ne!(base, ctl, "control: sharpenRadius 3.0 must change pixels");
+ assert_ne!(base, half, "BUG: sharpenRadius 0.5 changed nothing");
+}