Skip to content

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

fix(algo): Path Blur clamps per-path speeds (#708) (#840)

bfenty83cfc3d9ef18
diff --git a/crates/engine/src/filters_ext.rs b/crates/engine/src/filters_ext.rs
index b3157d395..1f25aff52 100644
--- a/crates/engine/src/filters_ext.rs
+++ b/crates/engine/src/filters_ext.rs
@@ -341,13 +341,23 @@ pub fn params_for(id: &str, p: &Value) -> Option<FilterParams> {
}),
},
"filter.blurGallery.pathBlur" => FilterParams::PathBlur {
- paths: list::<BlurPath>(p, "paths").unwrap_or_else(|| {
- vec![BlurPath {
- points: vec![[f(p, "startX", 0.2), f(p, "startY", 0.5)], [f(p, "endX", 0.8), f(p, "endY", 0.5)]],
- speed: f(p, "speed", 50.0).clamp(0.0, 500.0),
- taper: f(p, "taper", 0.0).clamp(0.0, 100.0),
- }]
- }),
+ paths: list::<BlurPath>(p, "paths")
+ .map(|mut v| {
+ // Entry speeds bypass the scalar clamp above; the halo
+ // casts one to i32 and adds, so an unclamped value
+ // overflows (#708).
+ for b in &mut v {
+ b.speed = b.speed.clamp(0.0, 500.0);
+ }
+ v
+ })
+ .unwrap_or_else(|| {
+ vec![BlurPath {
+ points: vec![[f(p, "startX", 0.2), f(p, "startY", 0.5)], [f(p, "endX", 0.8), f(p, "endY", 0.5)]],
+ speed: f(p, "speed", 50.0).clamp(0.0, 500.0),
+ taper: f(p, "taper", 0.0).clamp(0.0, 100.0),
+ }]
+ }),
},
// ---- Other ----
"filter.other.custom" => FilterParams::Custom {
diff --git a/crates/engine/tests/path_blur_speed_clamp.rs b/crates/engine/tests/path_blur_speed_clamp.rs
new file mode 100644
index 000000000..79c9d8ec9
--- /dev/null
+++ b/crates/engine/tests/path_blur_speed_clamp.rs
@@ -0,0 +1,37 @@
+//! Regression tests for issue #708: `filter.blurGallery.pathBlur` clamps the
+//! scalar `speed`, but each entry of the documented `paths` array is
+//! deserialized without a clamp. A huge finite speed saturates the
+//! float-to-i32 cast in the halo and the `+ 1` overflows, which the dispatch
+//! turns into an internal-error panic. The command must clamp like the scalar
+//! path does and blur normally.
+
+use photocraft_engine::Session;
+use serde_json::json;
+
+fn path_blur(speed: f64) -> Result<serde_json::Value, photocraft_engine::EngineError> {
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 16, "height": 16, "background": "#808080"})).unwrap();
+ s.execute("filter.blurGallery.pathBlur", json!({"paths": [{"points": [[0.2, 0.5], [0.8, 0.5]], "speed": speed}]}))
+}
+
+#[test]
+fn path_blur_huge_entry_speed_does_not_panic() {
+ // On main this comes back as a caught panic ("internal error" style)
+ // from overflow in the halo radius. After the clamp it must succeed.
+ let r = path_blur(1.0e20);
+ assert!(r.is_ok(), "huge entry speed must clamp, got error: {r:?}");
+}
+
+#[test]
+fn path_blur_negative_entry_speed_does_not_panic() {
+ let r = path_blur(-1.0e20);
+ assert!(r.is_ok(), "huge negative entry speed must clamp, got error: {r:?}");
+}
+
+#[test]
+fn path_blur_normal_entry_speed_still_blurs() {
+ // Control: a documented in-range speed keeps working.
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 16, "height": 16, "background": "#808080"})).unwrap();
+ s.execute("filter.blurGallery.pathBlur", json!({"paths": [{"points": [[0.2, 0.5], [0.8, 0.5]], "speed": 120.0}]})).expect("in-range speed must succeed");
+}