Skip to content

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

Address CodeRabbit review: bound the soft shape frame scan

David Mann4bc06b906b08
diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs
index 42b389264..2e63844c1 100644
--- a/crates/compose/src/lib.rs
+++ b/crates/compose/src/lib.rs
@@ -1167,19 +1167,59 @@ pub fn soft_shape_frame(sh: &photocraft_doc::ShapeLayer, format: photocraft_colo
path.inverted = false;
let hard = photocraft_doc::ShapeLayer { density: 1.0, feather: 0.0, path, fill: sh.fill.clone(), stroke: sh.stroke.clone(), ..Default::default() };
let shape = photocraft_vector::CompiledShape::new(&hard, photocraft_vector::DEFAULT_TOLERANCE, canvas);
- let mut r = shape.bounds()?.intersect(&canvas);
- let painted = |strip: Rect| !shape.render(format, strip).content_bounds().is_empty();
- while !r.is_empty() && !painted(Rect::new(r.x0, r.y0, r.x1, r.y0 + 1)) {
- r.y0 += 1;
+ let r = shape.bounds()?.intersect(&canvas);
+ trim_to_painted(r, |strip| shape.render(format, strip).content_bounds())
+}
+
+/// `r` shrunk to the pixels `painted` reports (the painted bounds within a strip of `r`, empty if
+/// none), looking in from each edge through strips that double in thickness: a few renders per
+/// edge however wide the empty margin (a subtracted or intersected subpath can leave most of the
+/// rasterizers' bounds empty), and none of `r`'s inside rendered at once.
+fn trim_to_painted(mut r: Rect, mut painted: impl FnMut(Rect) -> Rect) -> Option<Rect> {
+ // Top: the first strip holding paint gives the first painted row.
+ let mut t = 1;
+ while !r.is_empty() {
+ let strip = Rect::new(r.x0, r.y0, r.x1, r.y0.saturating_add(t).min(r.y1));
+ let p = painted(strip).intersect(&strip);
+ if !p.is_empty() {
+ r.y0 = p.y0;
+ break;
+ }
+ r.y0 = strip.y1;
+ t = t.saturating_mul(2);
}
- while !r.is_empty() && !painted(Rect::new(r.x0, r.y1 - 1, r.x1, r.y1)) {
- r.y1 -= 1;
+ let mut t = 1;
+ while !r.is_empty() {
+ let strip = Rect::new(r.x0, r.y1.saturating_sub(t).max(r.y0), r.x1, r.y1);
+ let p = painted(strip).intersect(&strip);
+ if !p.is_empty() {
+ r.y1 = p.y1;
+ break;
+ }
+ r.y1 = strip.y0;
+ t = t.saturating_mul(2);
}
- while !r.is_empty() && !painted(Rect::new(r.x0, r.y0, r.x0 + 1, r.y1)) {
- r.x0 += 1;
+ let mut t = 1;
+ while !r.is_empty() {
+ let strip = Rect::new(r.x0, r.y0, r.x0.saturating_add(t).min(r.x1), r.y1);
+ let p = painted(strip).intersect(&strip);
+ if !p.is_empty() {
+ r.x0 = p.x0;
+ break;
+ }
+ r.x0 = strip.x1;
+ t = t.saturating_mul(2);
}
- while !r.is_empty() && !painted(Rect::new(r.x1 - 1, r.y0, r.x1, r.y1)) {
- r.x1 -= 1;
+ let mut t = 1;
+ while !r.is_empty() {
+ let strip = Rect::new(r.x1.saturating_sub(t).max(r.x0), r.y0, r.x1, r.y1);
+ let p = painted(strip).intersect(&strip);
+ if !p.is_empty() {
+ r.x1 = p.x1;
+ break;
+ }
+ r.x1 = strip.x0;
+ t = t.saturating_mul(2);
}
(!r.is_empty()).then_some(r)
}
diff --git a/crates/compose/src/soft_shape_tests.rs b/crates/compose/src/soft_shape_tests.rs
index 38bd8fa9a..222b3f3e9 100644
--- a/crates/compose/src/soft_shape_tests.rs
+++ b/crates/compose/src/soft_shape_tests.rs
@@ -158,3 +158,42 @@ fn soft_shape_bounds_cover_the_fill() {
assert_eq!(layer_bounds(&dense.layers[1], canvas), canvas);
assert!(effect_outline(&dense.layers[1]).is_none(), "a soft fill's alpha is its outline");
}
+
+/// A soft shape's frame is the hard shape's painted pixels, also where a subtracted subpath
+/// leaves most of the rasterizers' bounds empty (#5846), at every depth.
+#[test]
+fn soft_shape_frame_skips_a_subtracted_margin() {
+ let canvas = Rect::new(0, 0, 200, 200);
+ let mut cut = Subpath::polygon(&[(40.0, 40.0), (160.0, 40.0), (160.0, 140.0), (40.0, 140.0)]);
+ cut.op = photocraft_doc::vector::PathOp::Subtract;
+ let path = Path::new(vec![Subpath::polygon(&[(50.0, 50.0), (150.0, 50.0), (150.0, 150.0), (50.0, 150.0)]), cut]);
+ for depth in [SampleType::U8, SampleType::U16, SampleType::F32] {
+ let sh = ShapeLayer { path: path.clone(), fill: Some(Fill::Solid(Color::BLACK)), density: 0.5, feather: 4.0, ..ShapeLayer::default() };
+ assert_eq!(soft_shape_frame(&sh, doc(depth).pixel_format(), canvas), Some(Rect::new(50, 140, 150, 150)), "{depth:?}");
+ }
+}
+
+/// The frame scan renders a few strips per edge, not one per empty row or column: a 2-px
+/// painted spot in a 4000 × 3000 bounds takes a handful of renders and is found exactly.
+#[test]
+fn soft_shape_frame_scan_is_bounded() {
+ let spot = Rect::new(2500, 1700, 2502, 1702);
+ let mut calls = 0;
+ let got = trim_to_painted(Rect::new(0, 0, 4000, 3000), |strip| {
+ calls += 1;
+ strip.intersect(&spot)
+ });
+ assert_eq!(got, Some(spot));
+ assert!(calls <= 4 * 14, "{calls} renders");
+ let mut calls = 0;
+ assert_eq!(
+ trim_to_painted(Rect::new(0, 0, 4000, 3000), |_| {
+ calls += 1;
+ Rect::EMPTY
+ }),
+ None
+ );
+ assert!(calls <= 14, "an empty shape stops after the top edge: {calls} renders");
+ assert_eq!(trim_to_painted(Rect::EMPTY, |r| r), None);
+ assert_eq!(trim_to_painted(Rect::new(3, 4, 9, 7), |r| r), Some(Rect::new(3, 4, 9, 7)), "a full rect keeps its edges");
+}