From 248972cfa343ef46cee8f06ea16173310b2abba2 Mon Sep 17 00:00:00 2001 From: David Mann Date: Sun, 25 Oct 2026 14:52:51 +0100 Subject: [PATCH] Integration: clip VP clone copy-back to the canvas, paste in the stamp preview (#3281) A newLayer clone copied back the union of every dab's box: one point far off the canvas (e.g. [9e6, 9e6], accepted by the params) made it read a region of petabytes and abort. The command and the stamp preview now copy back and report damage only inside the canvas. With an image on the clipboard (Paste is on by default), OK pastes first and the strokes paint over and sample the paste, but the preview painted the strokes on the unpasted layer and drew the paste on top. The preview now pastes first too (shared paste_onto), repaints when the paste moves, and the overlay is only drawn when no current preview shows the paste. While the paste is dragged the baked preview is hidden so only one copy follows the pointer; a plane drag freezes preview and paste until release. Deleting the last plane now drops the stroke preview. Co-authored-by: Valentin Klein <271991477+vangel76@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/vp_cmds.rs | 49 ++++++-- crates/ui-egui/src/vp_ui.rs | 222 +++++++++++++++++++++++++++++++++-- 2 files changed, 250 insertions(+), 21 deletions(-) diff --git a/crates/engine/src/vp_cmds.rs b/crates/engine/src/vp_cmds.rs index ee9a4c6d7f..565d1f4965 100644 --- a/crates/engine/src/vp_cmds.rs +++ b/crates/engine/src/vp_cmds.rs @@ -137,15 +137,17 @@ pub fn clone_specs(p: &Value) -> Result> { } /// Paints clone stroke `c` on `surf`, or, with `work` (the image it samples), on `work` and copies -/// the pixels it changed to `surf`. Returns the dabs painted. -pub fn clone_into(sc: &Scene, surf: &mut Surface, work: Option<&mut Surface>, c: &CloneSpec) -> usize { +/// the pixels it changed inside `canvas` to `surf`. Returns the dabs painted. +pub fn clone_into(sc: &Scene, surf: &mut Surface, work: Option<&mut Surface>, c: &CloneSpec, canvas: Rect) -> usize { let Some(first) = c.points.first() else { return 0 }; let Some(work) = work else { return clone_dabs(sc, surf, c.source, *first, &c.points, c.size, c.hardness, c.opacity as f32).0; }; let before = work.clone(); let (dabs, area) = clone_dabs(sc, work, c.source, *first, &c.points, c.size, c.hardness, c.opacity as f32); - copy_changed(surf, &before, work, area); + // The dabs' boxes can span far off the canvas (points up to 1e7 px apart): reading their + // union back would allocate without bound. + copy_changed(surf, &before, work, area.intersect(&canvas)); dabs } @@ -178,8 +180,16 @@ fn has_layer(s: &Session) -> std::result::Result<(), String> { } } +/// Pastes `src` (content in `rect`) onto plane `plane` of `sc` over `surf`, as the command's +/// `"paste"` does. Returns the area the paste covers. +pub fn paste_onto(sc: &Scene, surf: &mut Surface, plane: usize, src: &Surface, rect: Rect, at: [f64; 2], width: f64) -> Result { + let Some(out) = paste(sc, plane, src, rect, at, width)? else { return Ok(Rect::EMPTY) }; + over(surf, &out); + Ok(out.content_bounds()) +} + /// Composites `top` over `dst` (premultiplied source-over), same format. -fn over(dst: &mut Surface, top: &Surface) { +pub fn over(dst: &mut Surface, top: &Surface) { let b = top.content_bounds(); if b.is_empty() { return; @@ -249,9 +259,7 @@ fn vanishing_point(s: &mut Session, p: &Value) -> Result { } let surf = l.surface_mut().ok_or_else(|| EngineError::Other("Vanishing Point needs a pixel layer".into()))?; for (plane, src, rect, at, width) in &pastes { - if let Some(out) = paste(&sc, *plane, src, *rect, *at, *width)? { - over(surf, &out); - } + paste_onto(&sc, surf, *plane, src, *rect, *at, *width)?; } // A new layer samples the visible image (merged with what's been pasted) and takes only // the pixels the strokes change; otherwise the strokes paint the layer itself. @@ -260,7 +268,7 @@ fn vanishing_point(s: &mut Session, p: &Value) -> Result { w }); for c in &clones { - dabs += clone_into(&sc, surf, work.as_mut(), c); + dabs += clone_into(&sc, surf, work.as_mut(), c, canvas); } surf.prune(); Ok(id) @@ -393,4 +401,29 @@ mod tests { assert_eq!(r["planes"].as_array().unwrap().len(), 2); assert!(r["dabs"].as_u64().unwrap() >= 1, "{r}"); } + + #[test] + fn new_layer_clone_stays_on_the_canvas() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 200, "height": 200})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("edit.fill", json!({"color": "#40c040"})).unwrap(); + // A red stripe where the first dab samples, so cloning it changes pixels. + s.edit("stripe", |doc, active| { + doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(140, 80, 160, 120), &[1.0, 0.0, 0.0, 1.0]); + Ok(()) + }) + .unwrap(); + let planes = json!([{"corners": [[0, 0], [200, 0], [200, 200], [0, 200]]}]); + // The second dab lies left of the canvas and samples the canvas 50 px to its right; the + // third is far off it (its box joined with the first's once asked for petabytes). + for points in [json!([[100, 100], [-30, 100]]), json!([[100, 100], [-30, 100], [9_000_000, 9_000_000]])] { + let r = s.execute(VP, json!({"planes": planes, "newLayer": true, "clone": [{"source": [150, 100], "points": points, "size": 20}]})).unwrap(); + let l = s.active().unwrap().doc.layer(LayerId(r["layer"].as_u64().unwrap())).unwrap().surface().unwrap(); + let b = l.content_bounds(); + assert!(!b.is_empty() && Rect::new(0, 0, 200, 200).contains_rect(&b), "{b:?}"); + assert!(l.rgba(100, 100)[0] > 0.9, "{:?}", l.rgba(100, 100)); + s.undo(); + } + } } diff --git a/crates/ui-egui/src/vp_ui.rs b/crates/ui-egui/src/vp_ui.rs index 65c629e590..fc4476dca3 100644 --- a/crates/ui-egui/src/vp_ui.rs +++ b/crates/ui-egui/src/vp_ui.rs @@ -20,7 +20,7 @@ use egui::{Color32, Pos2, Stroke, TextureHandle, pos2}; use photocraft_algo::transform::Homography; use photocraft_algo::vanishing::{Edge, PlaneCheck, Scene, VpPlane, clone_dabs_from, clone_source}; use photocraft_doc::{DocId, Document, Layer, LayerContent, LayerId}; -use photocraft_engine::vp_cmds::{CloneSpec, MAX_CLONE_DABS, MAX_CLONE_STROKES, VP, copy_changed, visible_image}; +use photocraft_engine::vp_cmds::{CloneSpec, MAX_CLONE_DABS, MAX_CLONE_STROKES, VP, copy_changed, over, paste_onto, visible_image}; use photocraft_geom::Rect; use photocraft_raster::Surface; use serde_json::{Value, json}; @@ -92,9 +92,13 @@ enum Drag { /// The clone strokes painted onto the target layer, shown on the canvas while stamping. struct StampPreview { - /// The session's `rev` and New Layer it was painted with (either changing repaints it). + /// The session's `rev`, New Layer and paste it was painted with (any changing repaints it). rev: u64, new_layer: bool, + /// The paste (plane, top-left, width) baked in under the strokes, as OK pastes before it clones. + paste: Option<(usize, [f64; 2], f64)>, + /// The paste landed in `out` (it can fail, or miss the plane). + baked: bool, /// On a new layer: the visible image the strokes sample, with the strokes so far. work: Option, /// The sampled image before the current stroke, retained across frame batches. @@ -111,6 +115,9 @@ struct StampPreview { /// The clipboard image shown pasted onto the plane. struct Clip { + /// The clipboard pixels and their content bounds, which the preview pastes like OK does. + surface: Surface, + rect: Rect, texture: TextureHandle, width: f64, height: f64, @@ -251,6 +258,28 @@ impl VpSession { self.selected.filter(|i| *i < self.planes.len()).or_else(|| (!self.planes.is_empty()).then_some(0)) } + /// What OK pastes: the plane, the top-left and the width. + fn paste_key(&self) -> Option<(usize, [f64; 2], f64)> { + self.paste_plane().map(|i| (i, self.paste_at, self.paste_width)) + } + + /// The stamp preview is current and already shows the paste under the strokes. + fn preview_has_paste(&self) -> bool { + self.preview.as_ref().is_some_and(|p| p.baked && p.rev == self.rev && p.paste == self.paste_key()) + } + + /// The paste is dragged while the stamp preview holds it in its old place. + fn moving_baked_paste(&self) -> bool { + matches!(self.drag, Some(Drag::Paste { .. })) && self.preview.as_ref().is_some_and(|p| p.baked) + } + + /// [`draw_content`] draws the paste overlay: not when the stamp preview shows the paste, and + /// not while a plane drag freezes that preview (as it freezes the strokes) until release. + fn paste_overlay(&self) -> bool { + let frozen = matches!(self.drag, Some(Drag::Corner { .. } | Drag::Edge { .. } | Drag::Torn { .. })) && self.preview.as_ref().is_some_and(|p| p.baked); + !self.preview_has_paste() && !frozen + } + /// Size of the pasted image in plane coordinates (the engine keeps its metric aspect). fn paste_extent(&self, lu: f64, lv: f64) -> Option<[f64; 2]> { let c = self.clip.as_ref()?; @@ -493,17 +522,46 @@ impl VpSession { self.preview = None; return had; } - let Some(sc) = self.scene() else { return false }; - let stale = self.preview.as_ref().is_none_or(|p| p.rev != self.rev || p.new_layer != self.new_layer); + let Some(sc) = self.scene() else { + // No planes left: nothing to show the strokes on. + return self.preview.take().is_some(); + }; + let paste = self.paste_key(); + let canvas = self.canvas; + let stale = self.preview.as_ref().is_none_or(|p| p.rev != self.rev || p.new_layer != self.new_layer || p.paste != paste); let mut area = Rect::EMPTY; + let mut baked = false; if stale { let Some(layer) = self.base.layer(self.layer).and_then(Layer::surface) else { return false }; - let (out, work) = if self.new_layer { (Surface::new(layer.format()), Some(visible_image(&self.base))) } else { (layer.clone(), None) }; + let mut out = if self.new_layer { Surface::new(layer.format()) } else { layer.clone() }; + // OK pastes first and the strokes then paint over (and sample) the pasted pixels. + if let (Some((i, at, width)), Some(c)) = (paste, self.clip.as_ref()) + && self.planes.get(i).is_some_and(|p| p.check(self.center()) != PlaneCheck::Invalid) + && let Ok(r) = paste_onto(&sc, &mut out, i, &c.surface, c.rect, at, width) + { + area = r.intersect(&canvas); + baked = !r.is_empty(); + } + let work = self.new_layer.then(|| { + let mut w = visible_image(&self.base); + over(&mut w, &out); + w + }); // The old preview's area changes back too. let keys = self.preview.take().map(|p| p.keys).unwrap_or_default(); - area = keys.iter().fold(Rect::EMPTY, |a, k| union(a, k.1)); - self.preview = - Some(StampPreview { rev: self.rev, new_layer: self.new_layer, work, source: None, out, done: (0, 0), keys, shown: self.base.clone() }); + area = keys.iter().fold(area, |a, k| union(a, k.1)); + self.preview = Some(StampPreview { + rev: self.rev, + new_layer: self.new_layer, + paste, + baked, + work, + source: None, + out, + done: (0, 0), + keys, + shown: self.base.clone(), + }); } let Some(pv) = self.preview.as_mut() else { return false }; let (from_stroke, from_point) = pv.done; @@ -522,10 +580,12 @@ impl VpSession { Some(work) => { let before = work.clone(); let (_, a) = clone_dabs_from(&sc, work, source, c.source, *anchor, points, c.size, hard, opacity); + // As OK does: only the canvas (the dabs' boxes can span far off it). + let a = a.intersect(&canvas); copy_changed(&mut pv.out, &before, work, a); a } - None => clone_dabs_from(&sc, &mut pv.out, source, c.source, *anchor, points, c.size, hard, opacity).1, + None => clone_dabs_from(&sc, &mut pv.out, source, c.source, *anchor, points, c.size, hard, opacity).1.intersect(&canvas), }; area = union(area, changed); } @@ -546,9 +606,14 @@ impl VpSession { true } - /// The document with the strokes painted, and its preview key. + /// The document with the strokes painted, and its preview key. Not while the paste is + /// dragged out of its baked-in place: the overlay follows the pointer instead, and the + /// strokes repaint over it on release. pub(crate) fn shown(&self) -> Option<(Arc, u64)> { let p = self.preview.as_ref()?; + if self.moving_baked_paste() { + return None; + } Some((p.shown.clone(), PREVIEW_KEY + p.keys.last()?.0)) } @@ -707,6 +772,8 @@ pub fn begin(app: &mut PhotocraftApp, ctx: &egui::Context) -> Result<(), String> let clip = app.session.clipboard.as_ref().and_then(|c| { let r = c.surface.content_bounds(); (!r.is_empty()).then(|| Clip { + surface: c.surface.clone(), + rect: r, texture: ctx.load_texture(format!("vp-paste-{key}"), crate::distort_ui::surface_image(&c.surface, r, 1024), egui::TextureOptions::LINEAR), width: f64::from(r.width()), height: f64::from(r.height()), @@ -896,7 +963,7 @@ fn parse_planes(v: &Value) -> Result<(Vec, Vec>), String> /// changed, once no plane is being dragged). pub(crate) fn sync_preview(app: &mut PhotocraftApp) { let Some(s) = app.distort.vanishing.as_mut() else { return }; - if matches!(s.drag, Some(Drag::Corner { .. } | Drag::Edge { .. } | Drag::Torn { .. })) { + if matches!(s.drag, Some(Drag::Corner { .. } | Drag::Edge { .. } | Drag::Torn { .. } | Drag::Paste { .. })) { return; } let ui = &mut app.ui; @@ -967,10 +1034,12 @@ pub fn escape(app: &mut PhotocraftApp) { } /// The paste on its plane: image content, drawn under the grid and guides like the document so a -/// guide stays visible across it (#7781). The planes and their grids are [`draw`]. +/// guide stays visible across it (#7781). The planes and their grids are [`draw`]. A current +/// stamp preview already shows the paste under its strokes. pub fn draw_content(s: &VpSession, painter: &egui::Painter, xf: &ViewXform) { let scr = |p: [f64; 2]| xf.to_screen(p[0] as f32, p[1] as f32); - if let Some(i) = s.paste_plane() + if s.paste_overlay() + && let Some(i) = s.paste_plane() && s.planes.get(i).is_some_and(|p| p.check(s.center()) != PlaneCheck::Invalid) && let (Some((h, lu, lv)), Some(c)) = (s.frame(i), s.clip.as_ref()) && let Some(ext) = s.paste_extent(lu, lv) @@ -1183,6 +1252,8 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { if let Some(s) = app.distort.vanishing.as_mut() { s.settle(ui.input(|i| i.pointer.any_down())); } + // A paste Width or Paste change shows in this frame's canvas, the paste under the strokes. + sync_preview(app); } #[cfg(test)] @@ -1375,4 +1446,129 @@ mod tests { assert_eq!(photocraft_compose::flatten(&app.session.active().unwrap().doc).px, expected, "newLayer={new_layer}"); } } + + /// A red square on the clipboard (copied from the layer's top-left corner). + fn copy_red_square(app: &mut PhotocraftApp) { + app.session.execute("select.rect", json!({"x": 0, "y": 0, "width": 40, "height": 40})).unwrap(); + app.session.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.session.execute("edit.copy", json!({})).unwrap(); + app.session.execute("select.deselect", json!({})).unwrap(); + } + + #[test] + fn stamp_preview_paints_over_the_paste_like_ok() { + for new_layer in [false, true] { + let (ctx, mut app) = app_with_layer(); + copy_red_square(&mut app); + crate::distort_ui::menu(&mut app, &ctx, VP, &json!({})).unwrap().unwrap(); + control(&mut app, &json!({"planes": [{"corners": [[10, 10], [230, 10], [230, 150], [10, 150]]}], "newLayer": new_layer})).unwrap(); + let s = app.distort.vanishing.as_ref().unwrap(); + assert!(s.paste && s.paste_plane() == Some(0), "the clipboard pastes by default"); + // The paste covers x 65..175 from y 45 down. One stroke clones it onto the base + // layer, the other paints across its right edge. + control( + &mut app, + &json!({"clone": [ + {"source": [100, 60], "points": [[40, 120], [48, 120], [56, 120]], "size": 16, "hardness": 80}, + {"source": [40, 130], "points": [[150, 60], [170, 60], [190, 60]], "size": 16, "hardness": 80}, + ]}), + ) + .unwrap(); + let s = app.distort.vanishing.as_ref().unwrap(); + assert!(s.preview_has_paste(), "the preview shows the paste, so the overlay isn't drawn over the strokes"); + let (shown, _) = s.shown().unwrap(); + let expected = photocraft_compose::flatten(&shown).px; + commit(&mut app).unwrap(); + assert_eq!(photocraft_compose::flatten(&app.session.active().unwrap().doc).px, expected, "newLayer={new_layer}"); + } + } + + #[test] + fn moving_the_paste_repaints_the_stamp_preview() { + let (ctx, mut app) = app_with_layer(); + copy_red_square(&mut app); + crate::distort_ui::menu(&mut app, &ctx, VP, &json!({})).unwrap().unwrap(); + control(&mut app, &json!({"planes": [{"corners": [[10, 10], [230, 10], [230, 150], [10, 150]]}]})).unwrap(); + control(&mut app, &json!({"clone": [{"source": [200, 130], "points": [[30, 130]], "size": 8}]})).unwrap(); + let red = |app: &PhotocraftApp, x: i32, y: i32| { + let (shown, _) = app.distort.vanishing.as_ref().unwrap().shown().unwrap(); + let l = app.session.active().unwrap().active_layer.unwrap(); + shown.layer(l).unwrap().surface().unwrap().rgba(x, y)[0] + }; + assert!(red(&app, 100, 60) > 0.9); + control(&mut app, &json!({"pasteAt": [0.0, 0.5]})).unwrap(); + assert!(red(&app, 100, 60) < 0.5, "the paste left its old place"); + assert!(red(&app, 30, 100) > 0.9, "and shows at the new one"); + control(&mut app, &json!({"paste": false})).unwrap(); + assert!(red(&app, 30, 100) < 0.5, "paste off: gone from the preview"); + assert!(!app.distort.vanishing.as_ref().unwrap().preview_has_paste()); + } + + #[test] + fn dragging_the_paste_shows_it_once() { + let (ctx, mut app) = app_with_layer(); + copy_red_square(&mut app); + crate::distort_ui::menu(&mut app, &ctx, VP, &json!({})).unwrap().unwrap(); + control(&mut app, &json!({"planes": [{"corners": [[10, 10], [230, 10], [230, 150], [10, 150]]}], "tool": "edit"})).unwrap(); + control(&mut app, &json!({"clone": [{"source": [200, 130], "points": [[30, 130]], "size": 8}]})).unwrap(); + let layer = app.session.active().unwrap().active_layer.unwrap(); + let red = |app: &PhotocraftApp, x: i32, y: i32| { + let (shown, _) = crate::distort_ui::display_doc(app, 0)?; + Some(shown.layer(layer).unwrap().surface().unwrap().rgba(x, y)[0]) + }; + assert!(red(&app, 100, 60).unwrap() > 0.9, "the paste is in the preview"); + let ev = |app: &mut PhotocraftApp, e: ToolEvent| crate::distort_ui::pointer(app, e, egui::Modifiers::NONE); + ev(&mut app, ToolEvent::Down { x: 100.0, y: 60.0, pressure: 1.0 }); + ev(&mut app, ToolEvent::Move { x: 60.0, y: 100.0, pressure: 1.0 }); + // Mid-drag: no copy left behind at the old place, only the overlay at the pointer. + assert!(red(&app, 100, 60).is_none_or(|r| r < 0.5), "{:?}", red(&app, 100, 60)); + assert!(app.distort.vanishing.as_ref().unwrap().paste_overlay(), "the overlay draws the moving paste"); + ev(&mut app, ToolEvent::Up { x: 60.0, y: 100.0 }); + assert!(red(&app, 100, 60).unwrap() < 0.5); + assert!(red(&app, 60, 100).unwrap() > 0.9, "released: the preview pastes at the new place"); + assert!(!app.distort.vanishing.as_ref().unwrap().paste_overlay()); + // A plane corner drag freezes the preview, paste included, until release: one copy. + ev(&mut app, ToolEvent::Down { x: 230.0, y: 150.0, pressure: 1.0 }); + ev(&mut app, ToolEvent::Move { x: 236.0, y: 156.0, pressure: 1.0 }); + let s = app.distort.vanishing.as_ref().unwrap(); + assert!(matches!(s.drag, Some(Drag::Corner { .. }))); + assert!(red(&app, 60, 100).unwrap() > 0.9 && !s.paste_overlay(), "frozen preview, no overlay on top"); + ev(&mut app, ToolEvent::Up { x: 236.0, y: 156.0 }); + let s = app.distort.vanishing.as_ref().unwrap(); + assert!(s.preview_has_paste() && !s.paste_overlay()); + } + + #[test] + fn new_layer_stamp_preview_stays_on_the_canvas() { + let (ctx, mut app) = app_with_layer(); + // A red stripe where the first dab samples, so cloning it changes pixels. + app.session.execute("select.rect", json!({"x": 130, "y": 70, "width": 20, "height": 20})).unwrap(); + app.session.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.session.execute("select.deselect", json!({})).unwrap(); + crate::distort_ui::menu(&mut app, &ctx, VP, &json!({})).unwrap().unwrap(); + control(&mut app, &json!({"planes": [{"corners": [[10, 10], [230, 10], [230, 150], [10, 150]]}], "newLayer": true})).unwrap(); + // The second dab lies left of the canvas and samples the canvas 100 px to its right. + control(&mut app, &json!({"clone": [{"source": [140, 80], "points": [[40, 80], [-30, 80]], "size": 16}]})).unwrap(); + let s = app.distort.vanishing.as_ref().unwrap(); + let out = s.preview.as_ref().unwrap().out.content_bounds(); + assert!(!out.is_empty() && s.canvas.contains_rect(&out), "{out:?}"); + let st = app.session.active().unwrap(); + let area = damage(&app, st.doc.id, st.revision, 0, s.shown().unwrap().1).unwrap(); + assert!(s.canvas.contains_rect(&area), "{area:?}"); + } + + #[test] + fn deleting_the_last_plane_drops_the_stamp_preview() { + let (ctx, mut app) = app_with_layer(); + crate::distort_ui::menu(&mut app, &ctx, VP, &json!({})).unwrap().unwrap(); + control(&mut app, &json!({"planes": [{"corners": [[70, 60], [170, 60], [220, 150], [20, 150]]}]})).unwrap(); + control(&mut app, &json!({"clone": [{"source": [120, 139], "points": [[118, 100], [126, 100]], "size": 12}]})).unwrap(); + assert!(crate::distort_ui::display_doc(&app, 0).is_some()); + let s = app.distort.vanishing.as_mut().unwrap(); + s.selected = Some(0); + s.delete_selected(); + sync_preview(&mut app); + assert!(app.distort.vanishing.as_ref().unwrap().planes.is_empty()); + assert!(crate::distort_ui::display_doc(&app, 0).is_none(), "no strokes shown without a plane"); + } }