Skip to content

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

fix(engine): Camera Raw clamps the noise amounts to their range (#707) (#839)

bfenty1ee12eb90de1
diff --git a/crates/engine/src/lens_cmds.rs b/crates/engine/src/lens_cmds.rs
index 954c543ae..b7bbc2a06 100644
--- a/crates/engine/src/lens_cmds.rs
+++ b/crates/engine/src/lens_cmds.rs
@@ -139,7 +139,15 @@ pub fn raw_params(cmd: &str, p: &Value) -> Result<CameraRaw> {
if let Value::Object(m) = &mut q {
m.remove("layer");
}
- serde_json::from_value(q).map_err(|e| bad(cmd, format!("bad params: {e}")))
+ let mut cr: CameraRaw = serde_json::from_value(q).map_err(|e| bad(cmd, format!("bad params: {e}")))?;
+ // The noise stages turn these straight into a guided-filter radius and a
+ // blur sigma, so an out-of-range value means unbounded work. Clamp to the
+ // documented 0..100 (part of #707).
+ cr.noise_luminance = cr.noise_luminance.clamp(0.0, 100.0);
+ cr.noise_luminance_detail = cr.noise_luminance_detail.clamp(0.0, 100.0);
+ cr.noise_color = cr.noise_color.clamp(0.0, 100.0);
+ cr.noise_color_detail = cr.noise_color_detail.clamp(0.0, 100.0);
+ Ok(cr)
}
// ---------- pixels ----------
diff --git a/crates/engine/tests/camera_raw_noise_clamp.rs b/crates/engine/tests/camera_raw_noise_clamp.rs
new file mode 100644
index 000000000..53de97cb0
--- /dev/null
+++ b/crates/engine/tests/camera_raw_noise_clamp.rs
@@ -0,0 +1,38 @@
+//! #707: `filter.cameraRaw` must clamp the noise-reduction amounts to their
+//! documented 0..100 range. Before the fix, `raw_params` deserialized the
+//! params verbatim, so `noiseLuminance: 1e9` reached the guided filter as a
+//! radius of ~3e9 and the command never finished.
+
+use serde_json::json;
+
+#[test]
+fn camera_raw_clamps_noise_amounts() {
+ let p = photocraft_engine::lens_cmds::raw_params("filter.cameraRaw", &json!({"noiseLuminance": 1e9, "noiseColor": 1e9})).expect("params parse");
+ assert!((p.noise_luminance - 100.0).abs() < f32::EPSILON, "noiseLuminance 1e9 must clamp to 100, got {}", p.noise_luminance);
+ assert!((p.noise_color - 100.0).abs() < f32::EPSILON, "noiseColor 1e9 must clamp to 100, got {}", p.noise_color);
+}
+
+#[test]
+fn camera_raw_clamps_negative_noise_and_details() {
+ let p = photocraft_engine::lens_cmds::raw_params(
+ "filter.cameraRaw",
+ &json!({
+ "noiseLuminance": -5.0,
+ "noiseColor": -5.0,
+ "noiseLuminanceDetail": 1e9,
+ "noiseColorDetail": -1.0
+ }),
+ )
+ .expect("params parse");
+ assert_eq!(p.noise_luminance, 0.0, "negative noiseLuminance must clamp to 0");
+ assert_eq!(p.noise_color, 0.0, "negative noiseColor must clamp to 0");
+ assert_eq!(p.noise_luminance_detail, 100.0, "detail must clamp to 100");
+ assert_eq!(p.noise_color_detail, 0.0, "negative detail must clamp to 0");
+}
+
+#[test]
+fn camera_raw_keeps_in_range_noise_untouched() {
+ let p = photocraft_engine::lens_cmds::raw_params("filter.cameraRaw", &json!({"noiseLuminance": 25.0, "noiseColor": 40.0})).expect("params parse");
+ assert_eq!(p.noise_luminance, 25.0);
+ assert_eq!(p.noise_color, 40.0);
+}