mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Patch Destination: the rectangle check is capped and scans in bands (#12119)
Cause: is_hard_rect read selection coverage over the selection's own content bounds in one buffer. After a transform those bounds can reach far past the canvas, while the patch-size guard only measures the on-canvas part, so a huge mostly-off-canvas selection could stall or exhaust memory. Fix: is_hard_rect now calls is_hard_rect_within with MAX_PIXELS (the patch's own cap); bounds over the cap are not a hard rectangle and the selection stays in place. Otherwise coverage is scanned in 64-row bands, stopping at the first pixel below 1.0. Verified: new test covers a limit below and at the pixel count and a hole in the third band; patch (25) and retouch (126) lib tests pass, clippy -D warnings and fmt clean. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Model-Effort: Opus 5.5 drove and briefed; Sonnet 5.5 wrote at medium
This commit is contained in:
1 parent
1483d5193d
commit
76ad52bf47
2 files changed
+44
-2
No files matched your search
@@ -214,11 +214,34 @@ fn patch_surface(surf: &mut Surface, sel: &Surface, plan: &Plan, lock: bool, ste
|
||||
apply_coverage(surf, g, &cov, 1.0, None, lock, &healed, BlendMode::Normal)
|
||||
}
|
||||
|
||||
/// Rows scanned per band by [`is_hard_rect_within`], so the coverage buffer stays small.
|
||||
const RECT_BAND_ROWS: i32 = 64;
|
||||
|
||||
/// Whether `sel` is a hard-edged rectangle: nothing selected outside its own `bounds` and every pixel
|
||||
/// inside fully selected (the only Destination selection measured to move with the patch). The
|
||||
/// bounds may reach past the canvas; the patched area is then only the part on the canvas.
|
||||
/// bounds may reach past the canvas; the patched area is then only the part on the canvas. Bounds
|
||||
/// of more than [`MAX_PIXELS`] pixels (the patch's own cap) are never classified as a hard
|
||||
/// rectangle, so a huge selection moved mostly off the canvas is not scanned and stays in place.
|
||||
fn is_hard_rect(sel: &Surface, bounds: Rect) -> bool {
|
||||
sel.default_pixel().first().copied().unwrap_or(0.0) <= 0.0 && coverage(sel, bounds).iter().all(|c| *c >= 1.0)
|
||||
is_hard_rect_within(sel, bounds, MAX_PIXELS)
|
||||
}
|
||||
|
||||
/// [`is_hard_rect`] with the pixel `limit` given. Scans `bounds` in bands of [`RECT_BAND_ROWS`] rows
|
||||
/// and stops at the first pixel that is not fully selected.
|
||||
fn is_hard_rect_within(sel: &Surface, bounds: Rect, limit: u64) -> bool {
|
||||
let (w, h) = ((i64::from(bounds.x1) - i64::from(bounds.x0)).max(0) as u64, (i64::from(bounds.y1) - i64::from(bounds.y0)).max(0) as u64);
|
||||
if w.saturating_mul(h) > limit || sel.default_pixel().first().copied().unwrap_or(0.0) > 0.0 {
|
||||
return false;
|
||||
}
|
||||
let mut y = bounds.y0;
|
||||
while y < bounds.y1 {
|
||||
let y1 = y.saturating_add(RECT_BAND_ROWS).min(bounds.y1);
|
||||
if !coverage(sel, Rect::new(bounds.x0, y, bounds.x1, y1)).iter().all(|c| *c >= 1.0) {
|
||||
return false;
|
||||
}
|
||||
y = y1;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
pub(super) fn patch(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
@@ -182,3 +182,22 @@ fn destination_moves_a_hard_rectangle_that_extends_past_the_canvas() {
|
||||
assert_eq!(bounds(&mut s), sel_before);
|
||||
assert_eq!(px(&s, 480, 400), before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hard_rect_check_is_capped_and_scans_every_band() {
|
||||
let mut s = session();
|
||||
// 40x150 rectangle: three bands of 64 rows (64 + 64 + 22).
|
||||
s.execute("select.rect", json!({"x": 100, "y": 100, "width": 40, "height": 150})).unwrap();
|
||||
let d = s.active().unwrap();
|
||||
let sel = d.doc.selection.as_ref().unwrap();
|
||||
let b = sel.content_bounds();
|
||||
assert_eq!(b, Rect::new(100, 100, 140, 250));
|
||||
assert!(!is_hard_rect_within(sel, b, 5999), "over the limit is never a hard rectangle");
|
||||
assert!(is_hard_rect_within(sel, b, 6000), "at the limit it is checked as usual");
|
||||
assert!(is_hard_rect_within(sel, b, u64::MAX));
|
||||
// A hole in the third band must still be found.
|
||||
let mut holed = sel.clone();
|
||||
let fmt = holed.format();
|
||||
holed.write_region(Rect::new(120, 240, 121, 241), &photocraft_raster::from_rgba(&fmt, [0.0, 0.0, 0.0, 0.0]));
|
||||
assert!(!is_hard_rect_within(&holed, b, u64::MAX), "a hole past the first band fails the check");
|
||||
}
|
||||
Reference in new issue
Block a user