Skip to content

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

Retouch tools: test the stroke-point bound on all eleven commands (#1784)

kayrockscreenprintingdd41598d4c60
diff --git a/crates/engine/src/brush_cmds.rs b/crates/engine/src/brush_cmds.rs
index 665c4b63b..49a752369 100644
--- a/crates/engine/src/brush_cmds.rs
+++ b/crates/engine/src/brush_cmds.rs
@@ -74,11 +74,17 @@ pub fn parse_points(p: &Value, cmd: &str) -> Result<Vec<StrokePoint>> {
if pts.is_empty() {
return Err(bad(cmd, "`points` is empty"));
}
- // A stroke runs dab by dab along its length: an absurd coordinate would mean billions of dabs.
+ check_coords(&pts, cmd)?;
+ Ok(pts)
+}
+
+/// A stroke runs dab by dab along its length: an absurd coordinate would mean billions of dabs.
+/// Shared by the painting and the retouch tools' `points` (#976).
+pub(crate) fn check_coords(pts: &[StrokePoint], cmd: &str) -> Result<()> {
if pts.iter().any(|q| !(q.x.abs() <= MAX_COORD && q.y.abs() <= MAX_COORD)) {
return Err(bad(cmd, format!("point coordinates must be finite and within ±{MAX_COORD}")));
}
- Ok(pts)
+ Ok(())
}
/// Deep-merge `patch` into `base` (objects merge key by key; anything else replaces).
diff --git a/crates/engine/src/retouch_cmds.rs b/crates/engine/src/retouch_cmds.rs
index 3eed2901a..348ce94a7 100644
--- a/crates/engine/src/retouch_cmds.rs
+++ b/crates/engine/src/retouch_cmds.rs
@@ -81,10 +81,7 @@ fn parse_brush(s: &Session, p: &Value, cmd: &str) -> Result<(Stroke, Option<Laye
if pts.is_empty() {
return Err(bad(cmd, "`points` is empty"));
}
- // A stroke runs dab by dab along its length: an absurd coordinate would mean billions of dabs.
- if pts.iter().any(|q| !(q.x.abs() <= crate::brush_cmds::MAX_COORD && q.y.abs() <= crate::brush_cmds::MAX_COORD)) {
- return Err(bad(cmd, format!("point coordinates must be finite and within ±{}", crate::brush_cmds::MAX_COORD)));
- }
+ crate::brush_cmds::check_coords(&pts, cmd)?;
let base = s.tools.brush.clone();
let pct = |k: &str, d: f32, lo: f32, hi: f32| num(p, k, d).clamp(lo, hi) / 100.0;
let brush = BrushSettings {
diff --git a/crates/engine/src/retouch_cmds/tests.rs b/crates/engine/src/retouch_cmds/tests.rs
index 1b6ddd13c..87f3e77e9 100644
--- a/crates/engine/src/retouch_cmds/tests.rs
+++ b/crates/engine/src/retouch_cmds/tests.rs
@@ -1164,3 +1164,40 @@ fn live_dodge_matches_the_commit_across_cells() {
assert!(changed);
assert!(worst <= 1e-4, "live and commit differ by {worst}");
}
+
+#[test]
+fn retouch_strokes_refuse_absurd_point_coordinates() {
+ // #976: a segment to (1e300, 0) has an infinite length and the dab walker would never stop, so
+ // every retouch tool's `points` must stay within ±1e6, as the painting tools' do.
+ let cmds = [
+ "paint.dodge",
+ "paint.burn",
+ "paint.sponge",
+ "paint.blur",
+ "paint.sharpen",
+ "paint.smudge",
+ "paint.cloneStamp",
+ "paint.healingBrush",
+ "paint.spotHealing",
+ "paint.historyBrush",
+ "paint.patternStamp",
+ ];
+ // A single far point first: cheap even without the bound, so a regression fails fast.
+ let far = [json!([[1_000_001, 0]]), json!([[0, 0], [0, -2_000_000]]), json!([[0, 0], [1e300, 0]]), json!([[1e300, 1e300]])];
+ for depth in DEPTHS {
+ let mut s = session(32, 32, depth, "rgb");
+ paint_layer(&mut s, |x, y| [x as f32 / 32.0, y as f32 / 32.0, 0.25, 1.0]);
+ s.execute("cloneSource.set", json!({"source": [8, 8]})).unwrap();
+ let (before, past) = (rgba(&s, 16, 16), s.active().unwrap().history.past_len());
+ for cmd in cmds {
+ for points in &far {
+ let err = s.execute(cmd, json!({ "points": points })).unwrap_err();
+ assert!(err.to_string().contains("within ±"), "{cmd} {points} at {depth}: {err}");
+ }
+ }
+ assert_eq!((rgba(&s, 16, 16), s.active().unwrap().history.past_len()), (before, past), "{depth}: nothing changed");
+ // Control: a stroke on the canvas still retouches.
+ s.execute("paint.dodge", json!({"points": [[4, 16], [28, 16]], "size": 8})).unwrap();
+ assert_ne!(rgba(&s, 16, 16), before, "{depth}");
+ }
+}