From 05182cfa8aca025348c5e47d76a1e22a4143da62 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Sat, 21 Nov 2026 15:25:49 +0100 Subject: [PATCH 1/5] Auto-Blend Stack Images: the bottom layer shows everywhere (#14212) In a Stack Images run the bottom selected layer now gets a reveal-all mask; the layers above keep their focus-weight masks, so the composite is unchanged. Measured with two layers, Seamless Tones off and no masks before; three or more layers, Seamless Tones on and existing masks keep the complementary masks, as does Panorama. --- crates/engine/src/cmd/align_cmds.rs | 20 +++- .../auto_blend_stack_bottom_mask_tests.rs | 112 ++++++++++++++++++ 2 files changed, 128 insertions(+), 4 deletions(-) create mode 100644 crates/engine/src/cmd/align_cmds/auto_blend_stack_bottom_mask_tests.rs diff --git a/crates/engine/src/cmd/align_cmds.rs b/crates/engine/src/cmd/align_cmds.rs index 8e283e53936..e2a34d7bf87 100644 --- a/crates/engine/src/cmd/align_cmds.rs +++ b/crates/engine/src/cmd/align_cmds.rs @@ -328,8 +328,12 @@ fn auto_blend(s: &mut Session, p: &Value) -> Result { } } } + // Stack Images leaves the bottom layer showing everywhere, under the sharp parts of the + // layers above (#14212). Measured with two layers, Seamless Tones off and no masks before; + // the cases not measured keep complementary masks. + let reveal_bottom = method == "stack" && !seamless && ids.len() == 2 && ids.iter().all(|id| doc.layer(*id).is_some_and(|l| l.mask.is_none())); let (new_layer, depth_layer) = s.edit(label, |doc, active| { - for (id, wt) in ids.iter().zip(&weights) { + for (i, (id, wt)) in ids.iter().zip(&weights).enumerate() { if doc.is_background(*id) { if keep_background { continue; @@ -337,9 +341,14 @@ fn auto_blend(s: &mut Session, p: &Value) -> Result { crate::extra_cmds::background_to_layer_for_mask(doc, *id); } let l = doc.layer_mut(*id).ok_or(EngineError::NoLayer(*id))?; - let mut mask = LayerMask::hide_all(); - mask.surface.write_region(area, wt); - mask.surface.prune(); + let mask = if reveal_bottom && i == 0 { + LayerMask::reveal_all() + } else { + let mut mask = LayerMask::hide_all(); + mask.surface.write_region(area, wt); + mask.surface.prune(); + mask + }; l.mask = Some(mask); } let mut below = top; @@ -399,3 +408,6 @@ mod tests; #[cfg(test)] mod auto_blend_background_tests; + +#[cfg(test)] +mod auto_blend_stack_bottom_mask_tests; diff --git a/crates/engine/src/cmd/align_cmds/auto_blend_stack_bottom_mask_tests.rs b/crates/engine/src/cmd/align_cmds/auto_blend_stack_bottom_mask_tests.rs new file mode 100644 index 00000000000..291c9bedef8 --- /dev/null +++ b/crates/engine/src/cmd/align_cmds/auto_blend_stack_bottom_mask_tests.rs @@ -0,0 +1,112 @@ +//! #14212: Stack Images leaves the bottom layer's mask white everywhere; only the layers above +//! are masked to their sharp parts. + +use super::*; + +const W: i32 = 400; +const H: i32 = 300; + +/// The detail both layers share: a 2 px checker, the "sharp" picture. +fn detail(x: i32, y: i32) -> f32 { + if (x / 2 + y / 2) % 2 == 0 { 0.2 } else { 0.8 } +} + +/// A layer that is sharp over x `sharp` and flat grey (the checker's mean, "blurred") elsewhere. +fn focus_layer(s: &mut Session, name: &str, sharp: std::ops::Range) -> LayerId { + s.execute("layer.new.layer", json!({"name": name})).unwrap(); + s.edit(name, |doc, active| { + let surf = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap(); + for y in 0..H { + for x in 0..W { + let v = if sharp.contains(&x) { detail(x, y) } else { 0.5 }; + surf.fill_rect(Rect::new(x, y, x + 1, y + 1), &[v, v, v, 1.0]); + } + } + Ok(()) + }) + .unwrap(); + s.active().unwrap().active_layer.unwrap() +} + +fn select(s: &mut Session, ids: &[LayerId]) { + s.execute("layer.select", json!({"layer": ids[0].0})).unwrap(); + for id in &ids[1..] { + s.execute("layer.select", json!({"layer": id.0, "mode": "add"})).unwrap(); + } +} + +/// The issue's focus stack: FocusL (bottom) sharp on the left half, FocusR (top) on the right. +fn focus_pair() -> (Session, LayerId, LayerId) { + let mut s = Session::new(); + s.execute("file.new", json!({"width": W, "height": H, "background": "transparent"})).unwrap(); + let l = focus_layer(&mut s, "FocusL", 0..W / 2); + let r = focus_layer(&mut s, "FocusR", W / 2..W); + select(&mut s, &[l, r]); + (s, l, r) +} + +fn pixels() -> impl Iterator { + (0..H).flat_map(|y| (0..W).map(move |x| (x, y))) +} + +#[test] +fn a_stack_shows_the_bottom_layer_everywhere() { + let (mut s, l, r) = focus_pair(); + s.execute("edit.autoBlendLayers", json!({"method": "stack", "seamlessTones": false})).unwrap(); + let doc = &s.active().unwrap().doc; + let ml = doc.layer(l).unwrap().mask.as_ref().expect("FocusL gets a mask"); + assert!(pixels().all(|(x, y)| ml.value(x, y) == 1.0), "FocusL's mask is white everywhere"); + let mr = doc.layer(r).unwrap().mask.as_ref().expect("FocusR gets a mask"); + assert!(mr.value(3 * W / 4, H / 2) > 0.5 && mr.value(W / 4, H / 2) < 0.5, "FocusR shows only its sharp half"); + // The sharp half of each layer shows, away from the seam. + let img = photocraft_compose::render(doc, doc.bounds()); + let off = pixels().filter(|&(x, _)| !(W / 2 - 20..W / 2 + 20).contains(&x)).find(|&(x, y)| { + let px = img.px[(y * W + x) as usize]; + (px[0] - detail(x, y)).abs() > 0.02 || px[3] < 0.98 + }); + assert_eq!(off, None, "the composite keeps the sharp half of each layer"); +} + +/// Prepares one case on the focus pair (FocusL, FocusR) and returns its Auto-Blend params. +type Setup = fn(&mut Session, &[LayerId]) -> Value; + +/// Panorama, and the Stack Images cases the issue did not measure, keep complementary masks: +/// the bottom layer stays hidden where the layer above takes over. +#[test] +fn other_runs_keep_the_bottom_layer_masked() { + let cases: [(&str, Setup); 4] = [ + ("panorama", |s, ids| { + // A panorama pair: FocusL over x 0..240, FocusR over x 160..400. + let (l, r) = (ids[0], ids[1]); + s.edit("trim", |doc, _| { + doc.layer_mut(l).unwrap().surface_mut().unwrap().fill_rect(Rect::new(240, 0, W, H), &[0.0; 4]); + doc.layer_mut(r).unwrap().surface_mut().unwrap().fill_rect(Rect::new(0, 0, 160, H), &[0.0; 4]); + Ok(()) + }) + .unwrap(); + json!({"method": "panorama", "seamlessTones": false}) + }), + ("stack with Seamless Tones", |_, _| json!({"method": "stack", "seamlessTones": true})), + ("stack of three layers", |s, ids| { + let flat = focus_layer(s, "Flat", 0..0); + select(s, &[ids[0], ids[1], flat]); + json!({"method": "stack", "seamlessTones": false}) + }), + ("stack over an existing mask", |s, ids| { + let top = ids[1]; + s.edit("mask", |doc, _| { + doc.layer_mut(top).unwrap().mask = Some(LayerMask::reveal_all()); + Ok(()) + }) + .unwrap(); + json!({"method": "stack", "seamlessTones": false}) + }), + ]; + for (name, setup) in cases { + let (mut s, l, r) = focus_pair(); + let params = setup(&mut s, &[l, r]); + s.execute("edit.autoBlendLayers", params).unwrap(); + let ml = s.active().unwrap().doc.layer(l).unwrap().mask.clone().expect("FocusL gets a mask"); + assert!(pixels().any(|(x, y)| ml.value(x, y) < 0.5), "{name}: FocusL is hidden where another layer takes over"); + } +} From 8ae2b56a0f2af6c4ebf3a3662ec750c58a3fefb6 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Mon, 23 Nov 2026 16:52:01 +0100 Subject: [PATCH 2/5] Auto-Blend Stack Images: keep the bottom masked when revealing it would change the picture (#14212) A top pixel at alpha 0.75 can still win the focus mask. Under the complementary mask the bottom stays hidden there and the pixel stays partly transparent; a reveal-all bottom made it opaque and mixed in the bottom's colour. The bottom now gets the reveal-all mask only when every bottom pixel it uncovers sits under an opaque, fully weighted top pixel drawn normally at full opacity, the bottom has no effects and no pixels outside the blended area. Otherwise both layers keep their old masks. The tests' session now comes from crate::session(), since engine-core's Session::new() installs no commands. --- crates/engine-core/src/cmd/align_cmds.rs | 36 +++++++++++++++++-- .../auto_blend_stack_bottom_mask_tests.rs | 24 ++++++++++++- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/crates/engine-core/src/cmd/align_cmds.rs b/crates/engine-core/src/cmd/align_cmds.rs index e2a34d7bf87..3234927a051 100644 --- a/crates/engine-core/src/cmd/align_cmds.rs +++ b/crates/engine-core/src/cmd/align_cmds.rs @@ -330,8 +330,13 @@ fn auto_blend(s: &mut Session, p: &Value) -> Result { } // Stack Images leaves the bottom layer showing everywhere, under the sharp parts of the // layers above (#14212). Measured with two layers, Seamless Tones off and no masks before; - // the cases not measured keep complementary masks. - let reveal_bottom = method == "stack" && !seamless && ids.len() == 2 && ids.iter().all(|id| doc.layer(*id).is_some_and(|l| l.mask.is_none())); + // the cases not measured keep complementary masks. The reveal must leave the composite as it + // was, so it is kept only where the layer above hides every bottom pixel it uncovers. + let reveal_bottom = method == "stack" + && !seamless + && ids.len() == 2 + && ids.iter().all(|id| doc.layer(*id).is_some_and(|l| l.mask.is_none())) + && revealing_the_bottom_keeps_the_composite(&doc, &ids, area, &alpha, &weights); let (new_layer, depth_layer) = s.edit(label, |doc, active| { for (i, (id, wt)) in ids.iter().zip(&weights).enumerate() { if doc.is_background(*id) { @@ -380,6 +385,33 @@ fn auto_blend(s: &mut Session, p: &Value) -> Result { Ok(json!({"method": method, "layers": ids.iter().map(|i| i.0).collect::>(), "blended": new_layer.map(|l| l.0), "depth": depth_layer.map(|l| l.0)})) } +/// Whether a reveal-all mask on the bottom of two stacked layers draws the same composite as its +/// focus-weight mask (#14212). The reveal uncovers bottom pixels wherever that weight is below 1; +/// each one must sit under an opaque, fully weighted pixel of the top layer, drawn normally at +/// full opacity. The bottom must have no effects, which would trace the wider mask, and no pixels +/// outside `area`, which its old mask hid. +fn revealing_the_bottom_keeps_the_composite(doc: &Document, ids: &[LayerId], area: Rect, alpha: &[Vec], weights: &[Vec]) -> bool { + let (Some(bottom), Some(top)) = (doc.layer(ids[0]), doc.layer(ids[1])) else { return false }; + let bottom_inside = bottom.surface().is_some_and(|s| { + let b = s.content_bounds(); + b.is_empty() || b.intersect(&area) == b + }); + let no_effects = |l: &Layer| !l.effects.enabled || l.effects.items.iter().all(|e| !e.enabled()); + let top_plain = top.visible + && top.blend == photocraft_color::BlendMode::Normal + && top.opacity >= 1.0 + && top.fill_opacity >= 1.0 + && top.vector_mask.is_none() + && top.excluded_channels == 0 + && no_effects(top); + if !bottom_inside || !no_effects(bottom) || !top_plain { + return false; + } + // Masks are 8-bit: a weight that rounds to 255 shows the pixel in full. + let full = |v: f32| v >= 1.0 - 0.5 / 255.0; + (0..alpha[0].len()).all(|i| alpha[0][i] <= 0.0 || full(weights[0][i]) || (full(alpha[1][i]) && full(weights[1][i]))) +} + macro_rules! spec { ($id:literal, $label:literal, $params:literal, $run:expr) => { CommandSpec { id: $id, label: $label, menu: &["Edit"], shortcut: None, params: $params, enabled: two_layers, run: $run, journal: true } diff --git a/crates/engine-core/src/cmd/align_cmds/auto_blend_stack_bottom_mask_tests.rs b/crates/engine-core/src/cmd/align_cmds/auto_blend_stack_bottom_mask_tests.rs index 291c9bedef8..d9236acedc7 100644 --- a/crates/engine-core/src/cmd/align_cmds/auto_blend_stack_bottom_mask_tests.rs +++ b/crates/engine-core/src/cmd/align_cmds/auto_blend_stack_bottom_mask_tests.rs @@ -37,7 +37,7 @@ fn select(s: &mut Session, ids: &[LayerId]) { /// The issue's focus stack: FocusL (bottom) sharp on the left half, FocusR (top) on the right. fn focus_pair() -> (Session, LayerId, LayerId) { - let mut s = Session::new(); + let mut s = crate::session(); s.execute("file.new", json!({"width": W, "height": H, "background": "transparent"})).unwrap(); let l = focus_layer(&mut s, "FocusL", 0..W / 2); let r = focus_layer(&mut s, "FocusR", W / 2..W); @@ -67,6 +67,28 @@ fn a_stack_shows_the_bottom_layer_everywhere() { assert_eq!(off, None, "the composite keeps the sharp half of each layer"); } +/// A top pixel at alpha 0.75 still wins the mask where it is sharp. Under the old mask the +/// bottom stays hidden there and the pixel stays partly transparent; revealing the bottom would +/// make it opaque and mix in the bottom's colour, so the old masks are kept. +#[test] +fn a_semi_transparent_winning_pixel_keeps_the_bottom_masked() { + let (mut s, l, r) = focus_pair(); + let (x, y) = (3 * W / 4, H / 2); + s.edit("soften", |doc, _| { + let v = detail(x, y); + doc.layer_mut(r).unwrap().surface_mut().unwrap().fill_rect(Rect::new(x, y, x + 1, y + 1), &[v, v, v, 0.75]); + Ok(()) + }) + .unwrap(); + s.execute("edit.autoBlendLayers", json!({"method": "stack", "seamlessTones": false})).unwrap(); + let doc = &s.active().unwrap().doc; + assert!(doc.layer(r).unwrap().mask.as_ref().unwrap().value(x, y) > 0.5, "the soft pixel wins the mask"); + assert!(doc.layer(l).unwrap().mask.as_ref().unwrap().value(x, y) < 0.5, "FocusL stays hidden under it"); + let img = photocraft_compose::render(doc, doc.bounds()); + let a = img.px[(y * W + x) as usize][3]; + assert!(a < 0.9, "the composite keeps the pixel partly transparent, alpha {a}"); +} + /// Prepares one case on the focus pair (FocusL, FocusR) and returns its Auto-Blend params. type Setup = fn(&mut Session, &[LayerId]) -> Value; From f3ea52b6c63f5c99123aa0759b1ef4f43e066b36 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:20:27 +0100 Subject: [PATCH 3/5] Auto-Blend Stack Images: keep the bottom masked under a clipped or not quite opaque top (#14212) A clipped top takes its coverage from the bottom's mask, so revealing the bottom shows more of it. A float top pixel at alpha 0.999 passed the 8-bit mask tolerance, and revealing the bottom under it made it opaque. The reveal check now refuses a clipped top and needs exactly opaque top pixels; the mask tolerance still applies to the focus weights. --- crates/engine-core/src/cmd/align_cmds.rs | 11 ++++-- .../auto_blend_stack_bottom_mask_tests.rs | 38 ++++++++++++++++++- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/crates/engine-core/src/cmd/align_cmds.rs b/crates/engine-core/src/cmd/align_cmds.rs index 3234927a051..b47c36d47c1 100644 --- a/crates/engine-core/src/cmd/align_cmds.rs +++ b/crates/engine-core/src/cmd/align_cmds.rs @@ -388,8 +388,9 @@ fn auto_blend(s: &mut Session, p: &Value) -> Result { /// Whether a reveal-all mask on the bottom of two stacked layers draws the same composite as its /// focus-weight mask (#14212). The reveal uncovers bottom pixels wherever that weight is below 1; /// each one must sit under an opaque, fully weighted pixel of the top layer, drawn normally at -/// full opacity. The bottom must have no effects, which would trace the wider mask, and no pixels -/// outside `area`, which its old mask hid. +/// full opacity and not clipped, since a clipped top takes its coverage from the bottom's mask. +/// The bottom must have no effects, which would trace the wider mask, and no pixels outside +/// `area`, which its old mask hid. fn revealing_the_bottom_keeps_the_composite(doc: &Document, ids: &[LayerId], area: Rect, alpha: &[Vec], weights: &[Vec]) -> bool { let (Some(bottom), Some(top)) = (doc.layer(ids[0]), doc.layer(ids[1])) else { return false }; let bottom_inside = bottom.surface().is_some_and(|s| { @@ -401,15 +402,17 @@ fn revealing_the_bottom_keeps_the_composite(doc: &Document, ids: &[LayerId], are && top.blend == photocraft_color::BlendMode::Normal && top.opacity >= 1.0 && top.fill_opacity >= 1.0 + && !top.clipped && top.vector_mask.is_none() && top.excluded_channels == 0 && no_effects(top); if !bottom_inside || !no_effects(bottom) || !top_plain { return false; } - // Masks are 8-bit: a weight that rounds to 255 shows the pixel in full. + // Masks are 8-bit: a weight that rounds to 255 shows the pixel in full. Pixel alpha keeps the + // document's depth, so the top must be exactly opaque. let full = |v: f32| v >= 1.0 - 0.5 / 255.0; - (0..alpha[0].len()).all(|i| alpha[0][i] <= 0.0 || full(weights[0][i]) || (full(alpha[1][i]) && full(weights[1][i]))) + (0..alpha[0].len()).all(|i| alpha[0][i] <= 0.0 || full(weights[0][i]) || (alpha[1][i] >= 1.0 && full(weights[1][i]))) } macro_rules! spec { diff --git a/crates/engine-core/src/cmd/align_cmds/auto_blend_stack_bottom_mask_tests.rs b/crates/engine-core/src/cmd/align_cmds/auto_blend_stack_bottom_mask_tests.rs index d9236acedc7..f9c71addfde 100644 --- a/crates/engine-core/src/cmd/align_cmds/auto_blend_stack_bottom_mask_tests.rs +++ b/crates/engine-core/src/cmd/align_cmds/auto_blend_stack_bottom_mask_tests.rs @@ -37,8 +37,12 @@ fn select(s: &mut Session, ids: &[LayerId]) { /// The issue's focus stack: FocusL (bottom) sharp on the left half, FocusR (top) on the right. fn focus_pair() -> (Session, LayerId, LayerId) { + focus_pair_at(8) +} + +fn focus_pair_at(depth: u32) -> (Session, LayerId, LayerId) { let mut s = crate::session(); - s.execute("file.new", json!({"width": W, "height": H, "background": "transparent"})).unwrap(); + s.execute("file.new", json!({"width": W, "height": H, "depth": depth, "background": "transparent"})).unwrap(); let l = focus_layer(&mut s, "FocusL", 0..W / 2); let r = focus_layer(&mut s, "FocusR", W / 2..W); select(&mut s, &[l, r]); @@ -89,6 +93,26 @@ fn a_semi_transparent_winning_pixel_keeps_the_bottom_masked() { assert!(a < 0.9, "the composite keeps the pixel partly transparent, alpha {a}"); } +/// In a 32-bit document a top pixel at alpha 0.999 is not opaque, though a mask would round that +/// weight to full. Revealing the bottom under it would make it opaque, so the old masks are kept. +#[test] +fn a_nearly_opaque_float_pixel_keeps_the_bottom_masked() { + let (mut s, l, r) = focus_pair_at(32); + let (x, y) = (3 * W / 4, H / 2); + s.edit("soften", |doc, _| { + let v = detail(x, y); + doc.layer_mut(r).unwrap().surface_mut().unwrap().fill_rect(Rect::new(x, y, x + 1, y + 1), &[v, v, v, 0.999]); + Ok(()) + }) + .unwrap(); + s.execute("edit.autoBlendLayers", json!({"method": "stack", "seamlessTones": false})).unwrap(); + let doc = &s.active().unwrap().doc; + assert!(doc.layer(l).unwrap().mask.as_ref().unwrap().value(x, y) < 0.5, "FocusL stays hidden under it"); + let img = photocraft_compose::render(doc, doc.bounds()); + let a = img.px[(y * W + x) as usize][3]; + assert!(a < 0.9995, "the composite keeps the pixel's own alpha, {a}"); +} + /// Prepares one case on the focus pair (FocusL, FocusR) and returns its Auto-Blend params. type Setup = fn(&mut Session, &[LayerId]) -> Value; @@ -96,7 +120,7 @@ type Setup = fn(&mut Session, &[LayerId]) -> Value; /// the bottom layer stays hidden where the layer above takes over. #[test] fn other_runs_keep_the_bottom_layer_masked() { - let cases: [(&str, Setup); 4] = [ + let cases: [(&str, Setup); 5] = [ ("panorama", |s, ids| { // A panorama pair: FocusL over x 0..240, FocusR over x 160..400. let (l, r) = (ids[0], ids[1]); @@ -114,6 +138,16 @@ fn other_runs_keep_the_bottom_layer_masked() { select(s, &[ids[0], ids[1], flat]); json!({"method": "stack", "seamlessTones": false}) }), + ("stack under a clipped top", |s, ids| { + // A clipped top shows only where the bottom does, so revealing the bottom shows more of it. + let top = ids[1]; + s.edit("clip", |doc, _| { + doc.layer_mut(top).unwrap().clipped = true; + Ok(()) + }) + .unwrap(); + json!({"method": "stack", "seamlessTones": false}) + }), ("stack over an existing mask", |s, ids| { let top = ids[1]; s.edit("mask", |doc, _| { From 169d5de47be990bcd107636f4a1ad8932274a323 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Mon, 23 Nov 2026 20:31:26 +0100 Subject: [PATCH 4/5] Auto-Blend Stack Images: keep the bottom masked when the top sits in a group of its own (#14212) A group holding only the top layer (its opacity, fill, blend or mask) thins the top's coverage over the bottom, so revealing all of the bottom would change the composite. The reveal now needs both layers in the same group. --- crates/engine-core/src/cmd/align_cmds.rs | 9 +++++++-- .../auto_blend_stack_bottom_mask_tests.rs | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/crates/engine-core/src/cmd/align_cmds.rs b/crates/engine-core/src/cmd/align_cmds.rs index b47c36d47c1..f62ab323d50 100644 --- a/crates/engine-core/src/cmd/align_cmds.rs +++ b/crates/engine-core/src/cmd/align_cmds.rs @@ -389,10 +389,15 @@ fn auto_blend(s: &mut Session, p: &Value) -> Result { /// focus-weight mask (#14212). The reveal uncovers bottom pixels wherever that weight is below 1; /// each one must sit under an opaque, fully weighted pixel of the top layer, drawn normally at /// full opacity and not clipped, since a clipped top takes its coverage from the bottom's mask. -/// The bottom must have no effects, which would trace the wider mask, and no pixels outside -/// `area`, which its old mask hid. +/// Both must sit in the same group: a group holding only the top (its opacity, fill, blend or +/// mask) would thin the top's coverage over the bottom. The bottom must have no effects, which +/// would trace the wider mask, and no pixels outside `area`, which its old mask hid. fn revealing_the_bottom_keeps_the_composite(doc: &Document, ids: &[LayerId], area: Rect, alpha: &[Vec], weights: &[Vec]) -> bool { let (Some(bottom), Some(top)) = (doc.layer(ids[0]), doc.layer(ids[1])) else { return false }; + let index = doc.layers.index(); + if index.parent(ids[0]) != index.parent(ids[1]) { + return false; + } let bottom_inside = bottom.surface().is_some_and(|s| { let b = s.content_bounds(); b.is_empty() || b.intersect(&area) == b diff --git a/crates/engine-core/src/cmd/align_cmds/auto_blend_stack_bottom_mask_tests.rs b/crates/engine-core/src/cmd/align_cmds/auto_blend_stack_bottom_mask_tests.rs index f9c71addfde..6e86f95c21a 100644 --- a/crates/engine-core/src/cmd/align_cmds/auto_blend_stack_bottom_mask_tests.rs +++ b/crates/engine-core/src/cmd/align_cmds/auto_blend_stack_bottom_mask_tests.rs @@ -120,7 +120,7 @@ type Setup = fn(&mut Session, &[LayerId]) -> Value; /// the bottom layer stays hidden where the layer above takes over. #[test] fn other_runs_keep_the_bottom_layer_masked() { - let cases: [(&str, Setup); 5] = [ + let cases: [(&str, Setup); 6] = [ ("panorama", |s, ids| { // A panorama pair: FocusL over x 0..240, FocusR over x 160..400. let (l, r) = (ids[0], ids[1]); @@ -148,6 +148,20 @@ fn other_runs_keep_the_bottom_layer_masked() { .unwrap(); json!({"method": "stack", "seamlessTones": false}) }), + ("stack with the top in a half-opaque group", |s, ids| { + // The group thins the top's coverage, so the bottom shows through even where the + // top is opaque and sharp; revealing all of the bottom would show more of it. + s.execute("layer.select", json!({"layer": ids[1].0})).unwrap(); + s.execute("layer.groupLayers", json!({})).unwrap(); + let group = s.active().unwrap().active_layer.unwrap(); + s.edit("fade", |doc, _| { + doc.layer_mut(group).unwrap().opacity = 0.5; + Ok(()) + }) + .unwrap(); + select(s, ids); + json!({"method": "stack", "seamlessTones": false}) + }), ("stack over an existing mask", |s, ids| { let top = ids[1]; s.edit("mask", |doc, _| { From 834f641fd56b8cd5f59701ed79e887589541eeab Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:53:23 +0100 Subject: [PATCH 5/5] Auto-Blend Stack Images: keep the bottom masked under Blend If, a shapeless knockout or an opaque default Revealing the bottom must leave the composite as the complementary masks drew it. Three more cases broke that: - Blend If on the top hides top pixels the weights count as covering; the reveal showed the bottom there. - A bottom whose Advanced Blending acts (a deep knockout with Transparency Shapes Layer off) knocks out wherever its mask shows, so a reveal-all mask cleared the layers beneath past the blended area. - content_bounds leaves out pixels equal to the surface's default, so an opaque default showed past the blended area once the mask revealed it. Each now keeps the old masks, with a regression that fails without the check. --- crates/engine-vector/src/cmd/align_cmds.rs | 12 ++- .../auto_blend_stack_bottom_mask_tests.rs | 83 ++++++++++++++++++- 2 files changed, 90 insertions(+), 5 deletions(-) diff --git a/crates/engine-vector/src/cmd/align_cmds.rs b/crates/engine-vector/src/cmd/align_cmds.rs index f62ab323d50..a006c8cf632 100644 --- a/crates/engine-vector/src/cmd/align_cmds.rs +++ b/crates/engine-vector/src/cmd/align_cmds.rs @@ -390,8 +390,11 @@ fn auto_blend(s: &mut Session, p: &Value) -> Result { /// each one must sit under an opaque, fully weighted pixel of the top layer, drawn normally at /// full opacity and not clipped, since a clipped top takes its coverage from the bottom's mask. /// Both must sit in the same group: a group holding only the top (its opacity, fill, blend or -/// mask) would thin the top's coverage over the bottom. The bottom must have no effects, which -/// would trace the wider mask, and no pixels outside `area`, which its old mask hid. +/// mask) would thin the top's coverage over the bottom, and so would Blend If on the top. The +/// bottom must have no effects, which would trace the wider mask, no Advanced Blending that acts +/// (a knockout or blend that ignores its transparency covers all the mask reveals), and no pixels +/// outside `area`, which its old mask hid: neither content nor an opaque default pixel, which +/// `content_bounds` leaves out. fn revealing_the_bottom_keeps_the_composite(doc: &Document, ids: &[LayerId], area: Rect, alpha: &[Vec], weights: &[Vec]) -> bool { let (Some(bottom), Some(top)) = (doc.layer(ids[0]), doc.layer(ids[1])) else { return false }; let index = doc.layers.index(); @@ -400,7 +403,7 @@ fn revealing_the_bottom_keeps_the_composite(doc: &Document, ids: &[LayerId], are } let bottom_inside = bottom.surface().is_some_and(|s| { let b = s.content_bounds(); - b.is_empty() || b.intersect(&area) == b + to_rgba(&s.format(), &s.default_pixel())[3] <= 0.0 && (b.is_empty() || b.intersect(&area) == b) }); let no_effects = |l: &Layer| !l.effects.enabled || l.effects.items.iter().all(|e| !e.enabled()); let top_plain = top.visible @@ -410,8 +413,9 @@ fn revealing_the_bottom_keeps_the_composite(doc: &Document, ids: &[LayerId], are && !top.clipped && top.vector_mask.is_none() && top.excluded_channels == 0 + && !photocraft_compose::blend_if_active(top, doc.mode) && no_effects(top); - if !bottom_inside || !no_effects(bottom) || !top_plain { + if !bottom_inside || !no_effects(bottom) || photocraft_compose::advanced_active(bottom, &[]) || !top_plain { return false; } // Masks are 8-bit: a weight that rounds to 255 shows the pixel in full. Pixel alpha keeps the diff --git a/crates/engine-vector/src/cmd/align_cmds/auto_blend_stack_bottom_mask_tests.rs b/crates/engine-vector/src/cmd/align_cmds/auto_blend_stack_bottom_mask_tests.rs index 6e86f95c21a..0c7e238a736 100644 --- a/crates/engine-vector/src/cmd/align_cmds/auto_blend_stack_bottom_mask_tests.rs +++ b/crates/engine-vector/src/cmd/align_cmds/auto_blend_stack_bottom_mask_tests.rs @@ -120,7 +120,7 @@ type Setup = fn(&mut Session, &[LayerId]) -> Value; /// the bottom layer stays hidden where the layer above takes over. #[test] fn other_runs_keep_the_bottom_layer_masked() { - let cases: [(&str, Setup); 6] = [ + let cases: [(&str, Setup); 7] = [ ("panorama", |s, ids| { // A panorama pair: FocusL over x 0..240, FocusR over x 160..400. let (l, r) = (ids[0], ids[1]); @@ -162,6 +162,18 @@ fn other_runs_keep_the_bottom_layer_masked() { select(s, ids); json!({"method": "stack", "seamlessTones": false}) }), + ("stack under a top that Blend If hides", |s, ids| { + // This Layer's white point at 0 hides every top pixel, so the complementary masks + // leave its winning half empty; revealing the bottom would fill it. + let top = ids[1]; + s.edit("blend if", |doc, _| { + let hide = photocraft_doc::BlendRange { black: [0, 0], white: [0, 0] }; + doc.layer_mut(top).unwrap().blend_if.set(0, [hide, photocraft_doc::BlendRange::FULL]); + Ok(()) + }) + .unwrap(); + json!({"method": "stack", "seamlessTones": false}) + }), ("stack over an existing mask", |s, ids| { let top = ids[1]; s.edit("mask", |doc, _| { @@ -180,3 +192,72 @@ fn other_runs_keep_the_bottom_layer_masked() { assert!(pixels().any(|(x, y)| ml.value(x, y) < 0.5), "{name}: FocusL is hidden where another layer takes over"); } } + +/// The middle of the canvas, where the patch pairs keep their pixels. +fn patch() -> Rect { + Rect::new(100, 50, 300, 250) +} + +/// Keeps `id`'s pixels inside `patch()` only; everywhere else the surface holds `default`. +fn keep_to_patch(s: &mut Session, id: LayerId, default: [f32; 4]) { + s.edit("patch", |doc, _| { + let surf = doc.layer_mut(id).unwrap().surface_mut().unwrap(); + let mut kept = Surface::with_default(surf.format(), &default[..surf.format().channels()]); + kept.write_region(patch(), &surf.read_region(patch())); + kept.prune(); + *surf = kept; + Ok(()) + }) + .unwrap(); +} + +/// A bottom layer whose default pixel is opaque white (as an imported layer can have) shows that +/// white past its changed pixels. The old mask hides it outside the blended area, so revealing +/// the bottom would paint the rest of the canvas white; the old masks are kept. +#[test] +fn an_opaque_default_pixel_keeps_the_bottom_masked() { + let (mut s, l, r) = focus_pair(); + keep_to_patch(&mut s, l, [1.0; 4]); + keep_to_patch(&mut s, r, [0.0; 4]); + s.execute("edit.autoBlendLayers", json!({"method": "stack", "seamlessTones": false})).unwrap(); + let doc = &s.active().unwrap().doc; + let outside = (10, 10); + assert!(doc.layer(l).unwrap().mask.as_ref().unwrap().value(outside.0, outside.1) < 0.5, "FocusL stays hidden past the patch"); + let img = photocraft_compose::render(doc, doc.bounds()); + let a = img.px[(outside.1 * W + outside.0) as usize][3]; + assert!(a < 0.02, "the canvas past the patch stays empty, alpha {a}"); +} + +/// A bottom layer with a deep knockout and Transparency Shapes Layer off knocks out wherever its +/// mask shows, not only under its pixels. Revealing it would clear the layer beneath past the +/// patch, where the old mask kept the knockout away; the old masks are kept. +#[test] +fn a_shapeless_knockout_keeps_the_bottom_masked() { + let mut s = crate::session(); + s.execute("file.new", json!({"width": W, "height": H, "depth": 8, "background": "transparent"})).unwrap(); + s.execute("layer.new.layer", json!({"name": "Under"})).unwrap(); + s.edit("red", |doc, active| { + doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(0, 0, W, H), &[1.0, 0.0, 0.0, 1.0]); + Ok(()) + }) + .unwrap(); + let l = focus_layer(&mut s, "FocusL", 0..W / 2); + let r = focus_layer(&mut s, "FocusR", W / 2..W); + keep_to_patch(&mut s, l, [0.0; 4]); + keep_to_patch(&mut s, r, [0.0; 4]); + s.edit("knockout", |doc, _| { + let advanced = &mut doc.layer_mut(l).unwrap().advanced; + advanced.knockout = photocraft_doc::Knockout::Deep; + advanced.transparency_shapes = false; + Ok(()) + }) + .unwrap(); + select(&mut s, &[l, r]); + s.execute("edit.autoBlendLayers", json!({"method": "stack", "seamlessTones": false})).unwrap(); + let doc = &s.active().unwrap().doc; + let outside = (10, 10); + assert!(doc.layer(l).unwrap().mask.as_ref().unwrap().value(outside.0, outside.1) < 0.5, "FocusL stays hidden past the patch"); + let img = photocraft_compose::render(doc, doc.bounds()); + let px = img.px[(outside.1 * W + outside.0) as usize]; + assert!(px[3] > 0.98 && px[0] > 0.98, "the red layer beneath shows past the patch: {px:?}"); +}