Skip to content

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

History Brush: bound the stroke before its dense buffers (#4961)

Muhammad Saad205c9c173721
diff --git a/crates/engine/src/retouch_cmds.rs b/crates/engine/src/retouch_cmds.rs
index 9ea7fd597..972617206 100644
--- a/crates/engine/src/retouch_cmds.rs
+++ b/crates/engine/src/retouch_cmds.rs
@@ -35,6 +35,52 @@ fn bad(cmd: &str, msg: impl Into<String>) -> EngineError {
EngineError::BadParams { cmd: cmd.into(), msg: msg.into() }
}
+/// A single stroke's accumulated coverage must stay at or under this many pixels (8192², so a
+/// stroke corner to corner across a 24–36 MP canvas fits). Larger footprints return an error
+/// instead of allocating.
+const MAX_STROKE_PIXELS: u64 = 67_108_864;
+
+/// Refuse a stroke whose footprint would not fit the coverage budget, from the points and brush
+/// size alone, before the renderer allocates anything for it.
+fn reject_huge_stroke(cmd: &str, stroke: &Stroke) -> Result<()> {
+ let pad = f64::from(stroke.brush.size.max(1.0)).mul_add(0.5, 4.0);
+ let mut min_x = f64::INFINITY;
+ let mut min_y = f64::INFINITY;
+ let mut max_x = f64::NEG_INFINITY;
+ let mut max_y = f64::NEG_INFINITY;
+ for q in &stroke.points {
+ min_x = min_x.min(q.x);
+ min_y = min_y.min(q.y);
+ max_x = max_x.max(q.x);
+ max_y = max_y.max(q.y);
+ }
+ if !min_x.is_finite() || !min_y.is_finite() || !max_x.is_finite() || !max_y.is_finite() {
+ return Err(bad(cmd, "point coordinates must be finite"));
+ }
+ let w = (max_x - min_x + pad * 2.0).ceil().max(1.0);
+ let h = (max_y - min_y + pad * 2.0).ceil().max(1.0);
+ if !w.is_finite() || !h.is_finite() || w > 1.0e9 || h > 1.0e9 {
+ return Err(bad(cmd, format!("stroke too large (at most {MAX_STROKE_PIXELS} px)")));
+ }
+ let area = (w as u64).saturating_mul(h as u64);
+ if area > MAX_STROKE_PIXELS {
+ return Err(bad(cmd, format!("stroke too large ({w:.0}×{h:.0} px, at most {MAX_STROKE_PIXELS})")));
+ }
+ Ok(())
+}
+
+fn reject_bounds(cmd: &str, bounds: Rect) -> Result<()> {
+ if bounds.is_empty() {
+ return Ok(());
+ }
+ let (w, h) = (u64::from(bounds.width()), u64::from(bounds.height()));
+ let area = w.saturating_mul(h);
+ if area > MAX_STROKE_PIXELS {
+ return Err(bad(cmd, format!("stroke too large ({w}×{h} px, at most {MAX_STROKE_PIXELS})")));
+ }
+ Ok(())
+}
+
/// A pixel layer, a layer with a mask, or a targeted alpha channel / Quick Mask (as the Brush).
fn has_pixel_layer(s: &Session) -> std::result::Result<(), String> {
if crate::channel_cmds::edits_channel(s) {
@@ -631,6 +677,10 @@ fn history_brush(s: &mut Session, p: &Value) -> Result<Value> {
const CMD: &str = "paint.historyBrush";
let (stroke, id) = parse_brush(s, p, CMD)?;
let mode = blend_param(p, CMD)?;
+ // The coverage and the source region below are both dense over the stroke's bounds, so a
+ // thin stroke across a large canvas would still ask for the whole box. Refuse it from the
+ // points and brush size, before the source surface is cloned or anything is rendered (#4961).
+ reject_huge_stroke(CMD, &stroke)?;
let live = s.live_commit.take().filter(|l| l.matches(s, CMD, p, &stroke.points));
let from_live = live.is_some();
let src_surface = history_source(s, id, p, CMD)?;
@@ -642,6 +692,7 @@ fn history_brush(s: &mut Session, p: &Value) -> Result<Value> {
let fmt = surf.format();
let src = if src_surface.format() == fmt { src_surface } else { src_surface.convert(fmt) };
let (bounds, cov) = stroke_coverage(&stroke);
+ reject_bounds(CMD, bounds)?;
let paint = Region::read(&src, bounds);
Ok(apply_coverage(surf, bounds, &cov, stroke.brush.opacity, sel, lock, &paint, mode))
})?;
diff --git a/crates/engine/src/retouch_cmds/pattern_stamp.rs b/crates/engine/src/retouch_cmds/pattern_stamp.rs
index 0dc9f102a..3c90c6d2a 100644
--- a/crates/engine/src/retouch_cmds/pattern_stamp.rs
+++ b/crates/engine/src/retouch_cmds/pattern_stamp.rs
@@ -13,14 +13,9 @@ use photocraft_paint::{Stroke, StrokePoint};
use photocraft_raster::{Surface, from_rgba_into, to_rgba};
use serde_json::{Value, json};
-use super::{bad, blend_param, damage_json, flag, has_pixel_layer, parse_brush};
+use super::{bad, blend_param, damage_json, flag, has_pixel_layer, parse_brush, reject_bounds, reject_huge_stroke};
use crate::{Result, Session};
-/// A single stroke's accumulated coverage must stay at or under this many pixels (8192², so a
-/// stroke corner to corner across a 24–36 MP canvas fits). Larger footprints return an error
-/// instead of allocating.
-const MAX_STROKE_PIXELS: u64 = 67_108_864;
-
const STREAM_PHASE_X: u64 = 101;
const STREAM_PHASE_Y: u64 = 102;
@@ -123,45 +118,6 @@ fn stamp_phase(p: &Value, cmd: &str, aligned: bool, first: &StrokePoint) -> Resu
}
}
-fn reject_huge_stroke(cmd: &str, stroke: &Stroke) -> Result<()> {
- let pad = f64::from(stroke.brush.size.max(1.0)).mul_add(0.5, 4.0);
- let mut min_x = f64::INFINITY;
- let mut min_y = f64::INFINITY;
- let mut max_x = f64::NEG_INFINITY;
- let mut max_y = f64::NEG_INFINITY;
- for q in &stroke.points {
- min_x = min_x.min(q.x);
- min_y = min_y.min(q.y);
- max_x = max_x.max(q.x);
- max_y = max_y.max(q.y);
- }
- if !min_x.is_finite() || !min_y.is_finite() || !max_x.is_finite() || !max_y.is_finite() {
- return Err(bad(cmd, "point coordinates must be finite"));
- }
- let w = (max_x - min_x + pad * 2.0).ceil().max(1.0);
- let h = (max_y - min_y + pad * 2.0).ceil().max(1.0);
- if !w.is_finite() || !h.is_finite() || w > 1.0e9 || h > 1.0e9 {
- return Err(bad(cmd, format!("stroke too large (at most {MAX_STROKE_PIXELS} px)")));
- }
- let area = (w as u64).saturating_mul(h as u64);
- if area > MAX_STROKE_PIXELS {
- return Err(bad(cmd, format!("stroke too large ({w:.0}×{h:.0} px, at most {MAX_STROKE_PIXELS})")));
- }
- Ok(())
-}
-
-fn reject_bounds(cmd: &str, bounds: Rect) -> Result<()> {
- if bounds.is_empty() {
- return Ok(());
- }
- let (w, h) = (u64::from(bounds.width()), u64::from(bounds.height()));
- let area = w.saturating_mul(h);
- if area > MAX_STROKE_PIXELS {
- return Err(bad(cmd, format!("stroke too large ({w}×{h} px, at most {MAX_STROKE_PIXELS})")));
- }
- Ok(())
-}
-
fn placement(phase: (f32, f32), scale: f32, angle: f32) -> Placement {
Placement::new(Rect::EMPTY, false, phase, scale, angle)
}
diff --git a/crates/engine/src/retouch_cmds/tests.rs b/crates/engine/src/retouch_cmds/tests.rs
index e20706857..d72924c43 100644
--- a/crates/engine/src/retouch_cmds/tests.rs
+++ b/crates/engine/src/retouch_cmds/tests.rs
@@ -544,6 +544,27 @@ fn history_brush_restores_earlier_pixels() {
}
}
+/// #4961: the History Brush's coverage and its source region are both dense over the stroke's
+/// bounds, so a stroke whose box dwarfs the canvas is refused from the points and brush size
+/// alone, before the source surface is cloned or any coverage is rendered. The document is left
+/// as it was, and ordinary strokes still paint at every depth.
+#[test]
+fn history_brush_refuses_a_stroke_too_large_to_render() {
+ for depth in DEPTHS {
+ let mut s = session(40, 20, depth, "rgb");
+ s.execute("paint.stroke", json!({"points": [[0, 10], [40, 10]], "size": 12, "hardness": 1.0, "color": "#ff0000"})).unwrap();
+ let before = rgba(&s, 20, 10);
+
+ let err = s.execute("paint.historyBrush", json!({"points": [[0, 0], [60000, 60000]], "size": 6, "hardness": 100})).unwrap_err();
+ assert!(err.to_string().contains("stroke too large"), "depth {depth}: {err}");
+ assert_eq!(rgba(&s, 20, 10), before, "depth {depth}: a refused stroke leaves the pixels alone");
+
+ // The guard is sized for real work: a stroke corner to corner across the canvas still runs.
+ s.execute("paint.historyBrush", json!({"points": [[0, 0], [40, 20]], "size": 6, "hardness": 100})).unwrap();
+ assert!(rgba(&s, 20, 10).iter().all(|v| *v > 0.99), "depth {depth}: the Open snapshot is white");
+ }
+}
+
#[test]
fn history_brush_reads_a_source_when_its_pixels_were_locked() {
// Given a white layer locked in history state 1, then unlocked and filled red.