PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Layers: toggling a clipped adjustment damages only its base (#6535)
David Mann145acbda8bc5
diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rsindex 3cf2f1dc1..6ee2b6a2c 100644--- a/crates/compose/src/lib.rs+++ b/crates/compose/src/lib.rs@@ -1168,6 +1168,23 @@ pub fn change_bounds(layer: &Layer, canvas: Rect) -> Option<Rect> {composite_bounds(layer, canvas)}+/// [`change_bounds`] of `layers[k]` in its sibling list (bottom→top), which also bounds a clipped+/// adjustment layer: it changes only its clip base's pixels, so it reaches as far as showing,+/// hiding or restyling that base does (#6535). Still `None` when there is no base below it (the+/// compositor then draws it as an ordinary adjustment) or the base is an adjustment itself+/// (layers clipped to one composite atop the adjusted backdrop, everywhere).+pub fn change_bounds_in(layers: &[Layer], k: usize, canvas: Rect) -> Option<Rect> {+ let layer = layers.get(k)?;+ if layer.clipped && matches!(layer.content, LayerContent::Adjustment(_)) {+ let base = layers.get(..k)?.iter().rev().find(|l| !l.clipped)?;+ if matches!(base.content, LayerContent::Adjustment(_)) {+ return None;+ }+ return change_bounds(base, canvas);+ }+ change_bounds(layer, canvas)+}+/// Whether the layer's content is transparent outside its bounds (every surface it draws from/// has a transparent default pixel), so its effects can't change pixels beyond its bounds grown/// by their reach.diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rsindex ddd487154..42bbea272 100644--- a/crates/compose/src/tests.rs+++ b/crates/compose/src/tests.rs@@ -448,6 +448,58 @@ fn clipped_adjustment_only_affects_base() {assert!(close4(px(&d, 3, 0), [1.0; 4]), "background untouched (white)");}+/// #6535: showing or hiding a clipped adjustment changes only its base's pixels, so its change+/// bounds are the base's; it still reaches everywhere without a base, or clipped to an+/// adjustment, or to a pass-through group whose adjustment it isolates.+#[test]+fn clipped_adjustment_changes_only_its_base() {+ let canvas = Rect::new(0, 0, 8, 6);+ let base_rect = Rect::new(2, 1, 5, 4);+ let mut d = doc_white(8, 6);+ d.layers.push(solid_layer("base", base_rect, [0.2, 0.4, 0.6, 1.0]));+ let mut over = solid_layer("over", Rect::new(3, 2, 4, 3), [0.9, 0.1, 0.1, 1.0]);+ over.clipped = true;+ d.layers.push(over);+ let mut adj = Layer::new("inv", LayerContent::Adjustment(Adjustment::Invert));+ adj.clipped = true;+ d.layers.push(adj);+ assert_eq!(change_bounds(&d.layers[3], canvas), None);+ assert_eq!(change_bounds_in(&d.layers, 3, canvas), Some(base_rect));+ // Every pixel the toggle changes lies inside those bounds.+ let shown = render(&d, canvas);+ d.layers[3].visible = false;+ let hidden = render(&d, canvas);+ let mut changed = Rect::EMPTY;+ for (i, (a, b)) in shown.px.iter().zip(&hidden.px).enumerate() {+ if !close4(*a, *b) {+ let (x, y) = ((i % 8) as i32, (i / 8) as i32);+ assert!(base_rect.contains(x, y), "pixel {x},{y} changed outside the base");+ let p = Rect::from_xywh(x, y, 1, 1);+ changed = if changed.is_empty() { p } else { changed.union(&p) };+ }+ }+ assert_eq!(changed, base_rect);+ // Other layers keep their own bounds.+ assert_eq!(change_bounds_in(&d.layers, 2, canvas), Some(Rect::new(3, 2, 4, 3)));+ assert_eq!(change_bounds_in(&d.layers, 9, canvas), None);++ // Unclipped, or with no base beneath it, it reaches everything.+ let mut free = d.layers.clone();+ free[3].clipped = false;+ assert_eq!(change_bounds_in(&free, 3, canvas), None);+ let orphan = vec![d.layers[3].clone()];+ assert_eq!(change_bounds_in(&orphan, 0, canvas), None);+ // Clipped layers on an adjustment composite atop the adjusted backdrop.+ let on_adj = vec![d.layers[0].clone(), Layer::new("lv", LayerContent::Adjustment(Adjustment::Invert)), d.layers[3].clone()];+ assert_eq!(change_bounds_in(&on_adj, 2, canvas), None);+ // A pass-through group holding an adjustment is isolated only while it has visible clipped+ // layers, so they change whether its adjustment reaches the layers beneath.+ let mut g = Layer::group("g", vec![solid_layer("in", base_rect, [0.2, 0.4, 0.6, 1.0]), Layer::new("inv", LayerContent::Adjustment(Adjustment::Invert))]);+ g.blend = BlendMode::PassThrough;+ let on_group = vec![d.layers[0].clone(), g, d.layers[3].clone()];+ assert_eq!(change_bounds_in(&on_group, 2, canvas), None);+}+#[test]fn threshold_posterize_levels_curves() {let mut buf = Buffer::filled(Rect::new(0, 0, 1, 1), [0.3, 0.3, 0.3, 1.0]);diff --git a/crates/engine/src/layer_multi_cmds.rs b/crates/engine/src/layer_multi_cmds.rsindex 1b71e966a..3a67f8278 100644--- a/crates/engine/src/layer_multi_cmds.rs+++ b/crates/engine/src/layer_multi_cmds.rs@@ -502,7 +502,8 @@ pub fn layers_damage(before: &Document, after: &Document, ids: &[LayerId]) -> Oplet canvas = d.bounds();for id in ids {// A layer gone from one side (never for a move) changes who knows what.- let b = photocraft_compose::change_bounds(d.layer(*id)?, canvas)?;+ let (siblings, k) = siblings_of(d, *id)?;+ let b = photocraft_compose::change_bounds_in(siblings, k, canvas)?;if !b.is_empty() {out = if out.is_empty() { b } else { out.union(&b) };}@@ -511,6 +512,14 @@ pub fn layers_damage(before: &Document, after: &Document, ids: &[LayerId]) -> OpSome(out)}+/// The sibling list holding layer `id` in `doc` (bottom→top) and its index there.+fn siblings_of(doc: &Document, id: LayerId) -> Option<(&[Layer], usize)> {+ let path = doc.path_of(id)?;+ let (&k, parent) = path.split_last()?;+ let siblings = if parent.is_empty() { doc.layers.as_slice() } else { doc.layer_at(parent)?.children()? };+ Some((siblings, k))+}+/// Report [`layers_damage`] as the active document's last change, so the canvas recomposites/// only that (grown by the effect reach around it) instead of everything.pub(crate) fn note_damage(s: &mut Session, before: &Document, ids: &[LayerId]) {@@ -801,6 +810,7 @@ pub fn step_damage(a: &Document, b: &Document) -> Option<Rect> {// Both sides have the layer, in another order._ => return false,};+ let (xk, yk) = (i, j);i += 1;j += 1;if x.video.is_some() || y.video.is_some() {@@ -813,7 +823,9 @@ pub fn step_damage(a: &Document, b: &Document) -> Option<Rect> {}if !layer_pixels_equal(x, y) {// Both sides' extents: a move changes pixels where it left and where it landed.- let (Some(bx), Some(by)) = (photocraft_compose::change_bounds(x, a.bounds()), photocraft_compose::change_bounds(y, b.bounds())) else {+ // A clipped adjustment reaches as far as its base (#6535).+ let (Some(bx), Some(by)) = (photocraft_compose::change_bounds_in(xs, xk, a.bounds()), photocraft_compose::change_bounds_in(ys, yk, b.bounds()))+ else {return false;};*out = if out.is_empty() { bx.union(&by) } else { out.union(&bx).union(&by) };@@ -834,10 +846,11 @@ pub fn step_damage(a: &Document, b: &Document) -> Option<Rect> {}/// The pixels adding or removing `layers[k]` can change, when that is bounded: its change bounds-/// (`photocraft_compose::change_bounds`: not for an adjustment, or a pass-through group holding-/// one), provided it isn't the clipping base of clipped layers above it (they would change base),-/// and neither it nor anything inside it is a video layer or excludes channels (both reach-/// outside the layer's bounds, see [`step_damage`]).+/// (`photocraft_compose::change_bounds_in`: not for an unclipped adjustment, or a pass-through+/// group holding one; a clipped adjustment's are its base's), provided it isn't the clipping base+/// of clipped layers above it (they would change base), and neither it nor anything inside it is+/// a video layer or excludes channels (both reach outside the layer's bounds, see+/// [`step_damage`]).fn one_sided_bounds(layers: &[Layer], k: usize, canvas: Rect) -> Option<Rect> {fn contained(l: &Layer, depth: usize) -> bool {depth < 256 && l.video.is_none() && l.excluded_channels == 0 && l.children().is_none_or(|c| c.iter().all(|c| contained(c, depth + 1)))@@ -847,7 +860,7 @@ fn one_sided_bounds(layers: &[Layer], k: usize, canvas: Rect) -> Option<Rect> {if new_base || !contained(l, 0) {return None;}- photocraft_compose::change_bounds(l, canvas)+ photocraft_compose::change_bounds_in(layers, k, canvas)}/// Whether `layers[k]` draws nothing wherever it sits, so adding or removing it leaves the@@ -1780,6 +1793,71 @@ mod tests {assert_eq!(step_damage(&plain, &before), Some(Rect::EMPTY));}+ /// #6535: hiding or showing an adjustment clipped to a layer recomposites only that base's+ /// area, also on undo and redo and inside a group, and recompositing just that area gives+ /// the same canvas as a full recomposite.+ #[test]+ fn toggling_a_clipped_adjustment_damages_only_its_base() {+ let all = |s: &Session| photocraft_compose::render(doc(s), doc(s).bounds());+ // Recomposite only the reported damage over the previous canvas, as the view does.+ let patched = |s: &Session, prev: &photocraft_compose::Buffer| {+ let r = damage(s).expect("bounded damage");+ let mut out = prev.clone();+ if r.is_empty() {+ return out;+ }+ let part = photocraft_compose::render(doc(s), r);+ let w = out.rect.width() as usize;+ for y in r.y0..r.y1 {+ for x in r.x0..r.x1 {+ let i = (y - out.rect.y0) as usize * w + (x - out.rect.x0) as usize;+ let k = (y - r.y0) as usize * r.width() as usize + (x - r.x0) as usize;+ out.px[i] = part.px[k];+ }+ }+ out+ };+ for grouped in [false, true] {+ let mut s = session(16);+ let base_rect = Rect::new(10, 10, 30, 25);+ let base = rect_layer(&mut s, base_rect);+ let adj = LayerId(s.execute("layer.newAdjustmentLayer.invert", json!({})).unwrap()["layer"].as_u64().unwrap());+ s.execute("layer.createClippingMask", json!({"layer": adj.0})).unwrap();+ assert!(doc(&s).layer(adj).unwrap().clipped);+ if grouped {+ s.execute("layer.select", json!({"layer": base.0})).unwrap();+ s.execute("layer.select", json!({"layer": adj.0, "mode": "toggle"})).unwrap();+ s.execute("layer.groupLayers", json!({})).unwrap();+ assert!(doc(&s).path_of(adj).unwrap().len() == 2, "the pair sits in a group");+ }+ let shown = all(&s);+ s.execute("layer.hideLayers", json!({"layer": adj.0})).unwrap();+ assert!(!doc(&s).layer(adj).unwrap().visible);+ assert_eq!(damage(&s), Some(base_rect), "grouped: {grouped}");+ let hidden = all(&s);+ assert_ne!(hidden, shown);+ assert_eq!(patched(&s, &shown), hidden);+ s.execute("edit.undo", json!({})).unwrap();+ assert_eq!(damage(&s), Some(base_rect));+ assert_eq!(patched(&s, &hidden), shown);+ s.execute("edit.redo", json!({})).unwrap();+ assert_eq!(damage(&s), Some(base_rect));+ assert_eq!(patched(&s, &shown), hidden);+ s.execute("layer.showLayers", json!({"layer": adj.0})).unwrap();+ assert_eq!(damage(&s), Some(base_rect));+ assert_eq!(patched(&s, &hidden), shown);+ // layer.setProps reports its own damage.+ s.execute("layer.setProps", json!({"layer": adj.0, "visible": false})).unwrap();+ assert_eq!(damage(&s), Some(base_rect));+ assert_eq!(patched(&s, &shown), hidden);+ // Unclipped, the adjustment still reaches the whole canvas.+ s.execute("layer.setProps", json!({"layer": adj.0, "visible": true})).unwrap();+ s.execute("layer.releaseClippingMask", json!({"layer": adj.0})).unwrap();+ s.execute("layer.hideLayers", json!({"layer": adj.0})).unwrap();+ assert_eq!(damage(&s), None);+ }+ }+/// #2883 (P8/P9): a duplicate shares the source's tiles (no pixel copy) and recomposites only/// where it draws; undo brings back exactly the old pixels. A copy that becomes the clipping/// base of the layers above still recomposites everything.