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] 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:?}"); +}