Skip to content

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

fix(engine): Clone Stamp and Healing Brush refuse a far source instead of slicing out of bounds (#1111) (#1165)

wstockaf3c99d81760
diff --git a/crates/engine/src/retouch_cmds.rs b/crates/engine/src/retouch_cmds.rs
index 611f0053a..7e422ce62 100644
--- a/crates/engine/src/retouch_cmds.rs
+++ b/crates/engine/src/retouch_cmds.rs
@@ -195,15 +195,29 @@ fn clone_mapping(s: &mut Session, p: &Value, stroke: &Stroke, cmd: &str) -> Resu
/// Source pixels for destination `rect`: a translated read, or a bilinear resample when the
/// clone source is scaled, rotated or flipped.
-fn clone_sample(pre: &Document, id: Option<LayerId>, surf: &Surface, which: SampleLayers, rect: Rect, map: &crate::presets::clone_source::Mapping) -> Region {
+fn clone_sample(
+ pre: &Document,
+ id: Option<LayerId>,
+ surf: &Surface,
+ which: SampleLayers,
+ rect: Rect,
+ map: &crate::presets::clone_source::Mapping,
+ cmd: &str,
+) -> Result<Region> {
if map.is_translation() {
let off = map.offset();
- let mut r = sample(pre, id, surf, which, rect.translate(off.0, off.1));
+ let src = rect.translate(off.0, off.1);
+ // `translate` saturates at the i32 edges, so a far source (issue #1111) yields a window
+ // smaller than `rect`; relabelling that as `rect` would index past the sampled pixels.
+ if src.width() != rect.width() || src.height() != rect.height() {
+ return Err(bad(cmd, format!("source offset [{}, {}] is too far: the sample window leaves the coordinate range", off.0, off.1)));
+ }
+ let mut r = sample(pre, id, surf, which, src);
r.rect = rect;
- return r;
+ return Ok(r);
}
let src = sample(pre, id, surf, which, map.source_rect(rect));
- crate::presets::clone_source::resample(&src, rect, map, alpha_index(&surf.format()))
+ Ok(crate::presets::clone_source::resample(&src, rect, map, alpha_index(&surf.format())))
}
/// Result JSON shared by Clone Stamp and Healing Brush: the offset used and where the next stroke
@@ -251,7 +265,7 @@ fn clone_stamp(s: &mut Session, p: &Value) -> Result<Value> {
let dmg = run_stroke(s, "Clone Stamp", id, p, |pre, surf, sel, lock| {
let (bounds, cov) = stroke_coverage(&stroke);
// Samples come from the pre-stroke state, so pixels painted earlier in the stroke are never re-cloned.
- let paint = clone_sample(pre, id, surf, which, bounds, &map);
+ let paint = clone_sample(pre, id, surf, which, bounds, &map, CMD)?;
Ok(apply_coverage(surf, bounds, &cov, stroke.brush.opacity, sel, lock, &paint, mode))
})?;
Ok(clone_result(dmg, off, aligned, p, &stroke))
@@ -307,7 +321,7 @@ fn healing_brush(s: &mut Session, p: &Value) -> Result<Value> {
let (bounds, cov) = stroke_coverage(&stroke);
let (g, cov) = pad_coverage(bounds, &cov, 2);
let fmt = surf.format();
- let src = clone_sample(pre, id, surf, which, g, &map);
+ let src = clone_sample(pre, id, surf, which, g, &map, CMD)?;
// The destination the texture is fitted to: the layer itself, or what the user sees when
// sampling several layers (so healing onto an empty layer matches the composite).
let dst = if which == SampleLayers::Current { Region::read(surf, g) } else { composite_region(pre, id, which, g, fmt) };
diff --git a/crates/engine/src/retouch_cmds/tests.rs b/crates/engine/src/retouch_cmds/tests.rs
index 36d004578..72fd9112a 100644
--- a/crates/engine/src/retouch_cmds/tests.rs
+++ b/crates/engine/src/retouch_cmds/tests.rs
@@ -874,3 +874,31 @@ fn content_aware_move_fails_gracefully() {
}
assert!(s.execute("paint.contentAwareMove", json!({"offset": [10, 0], "structure": 7.0, "color": null})).is_ok());
}
+
+#[test]
+fn clone_and_heal_with_a_far_source_are_plain_errors() {
+ // Issue #1111: a translation whose offset saturates `Rect::translate` used to sample a
+ // narrower window, relabel it full-size and index past the buffer (a panic the dispatch
+ // guard turned into an internal error). It is a bad-params error, and the layer is untouched.
+ for cmd in ["paint.cloneStamp", "paint.healingBrush"] {
+ let mut s = session(40, 30, 8, "rgb");
+ paint_layer(&mut s, texture);
+ let before = rgba(&s, 10, 10);
+ let steps = s.active().unwrap().history.past_len();
+ for p in [
+ json!({"points": [[10, 10]], "source": [3e9, 0]}),
+ json!({"points": [[10, 10]], "source": [0, -3e9]}),
+ json!({"points": [[10, 10]], "offset": [2147483647, 0]}),
+ json!({"points": [[10, 10], [30, 10]], "source": [2147483640, 0]}),
+ ] {
+ let err = s.execute(cmd, p.clone()).expect_err(&format!("{cmd} {p}"));
+ assert!(matches!(err, EngineError::BadParams { .. }), "{cmd} {p}: {err:?}");
+ }
+ assert_eq!(rgba(&s, 10, 10), before, "{cmd}: a refused stroke changed the layer");
+ assert_eq!(s.active().unwrap().history.past_len(), steps, "{cmd}: a refused stroke recorded a history step");
+ // A source far outside the canvas but inside the coordinate range is still fine: it clones
+ // transparency.
+ s.execute(cmd, json!({"points": [[10, 10]], "source": [1e6, 1e6]})).unwrap();
+ s.execute(cmd, json!({"points": [[10, 10], [30, 10]], "source": [2147483000, 0]})).unwrap();
+ }
+}