From 3b7c10acc4fab5bfa037193479f1f02e83a9a33c Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 21 Nov 2026 10:56:48 -0800 Subject: [PATCH] PSD import: an adjustment layer in a nested group over drawn layers merges applied A pass-through group's adjustment layers count as kept to change what is below the outer pass-through group only when nothing below that group in the outer one draws, as for adjustment layers directly in the outer group. Before, a layer merged above them for its blend mode merged them applied but the import counted them as not applied. docs/mcp.md now says when a layer's mask hides all of it. --- crates/engine/src/cmd/fileio/psdlayers.rs | 53 ++++++++++++------- .../engine/src/cmd/fileio/tests_psdlayers.rs | 21 ++++++++ docs/mcp.md | 3 +- 3 files changed, 56 insertions(+), 21 deletions(-) diff --git a/crates/engine/src/cmd/fileio/psdlayers.rs b/crates/engine/src/cmd/fileio/psdlayers.rs index 59ead541d6..75c303cc36 100644 --- a/crates/engine/src/cmd/fileio/psdlayers.rs +++ b/crates/engine/src/cmd/fileio/psdlayers.rs @@ -1592,9 +1592,10 @@ impl [f32; 3]> Builder<'_, F> { /// The records of the adjustment layers kept in `out` at `level` (in a pass-through group, to /// change what is below the group), those kept in the visible pass-through groups of `out` - /// included, except groups whose layer mask hides all of them. + /// included: from the first one with nothing that draws below it in `out`, as [`Self::leading`] + /// keeps them. A group whose layer mask hides all of it keeps none. fn pending(&self, out: &[Unit<'_>], level: Level) -> Result, String> { - let mut records = vec![]; + let (mut records, mut draws) = (vec![], false); if level != Level::Pass { return Ok(records); } @@ -1602,15 +1603,41 @@ impl [f32; 3]> Builder<'_, F> { for u in out.iter().filter(|u| u.visible && u.opacity > 0.0) { if self.adjustment(&u.body)?.is_some() { records.push(u.record); - } else if let (Body::Group(_, items), Blend::Pass) = (&u.body, u.blend) + continue; + } + if let (Body::Group(_, items), Blend::Pass) = (&u.body, u.blend) + && (!draws || !records.is_empty()) && !u.mask_of.map(|g| self.masked_out(g, true)).transpose()?.unwrap_or(false) { - records.extend(self.leading(items)?.0); + let (inner, inner_draws) = self.leading(items)?; + records.extend(inner); + draws |= inner_draws; + continue; } + draws = draws || self.unit_draws(u)?; } Ok(records) } + /// Does unit `u` draw? An adjustment layer, a layer that draws nothing or whose pixels are all + /// transparent, a group of such layers, and a unit whose layer mask hides all of it, don't. + fn unit_draws(&self, u: &Unit<'_>) -> Result { + let draws = match &u.body { + Body::Layer(i) => !self.record(*i)?.draws_nothing() && !self.clear(*i)?, + Body::Group(_, items) => self.draws(items)?, + // Clipped layers merged with a base that has no pixels have none either. + Body::Merged(c) => c.px.iter().any(|p| p[3] > 0.0), + }; + // A base merged with the layers clipped to it isn't checked: its canvas may reach past the + // base's own pixels. + let hidden = |m: usize| match &u.body { + Body::Layer(_) => self.masked_out(m, false), + Body::Group(..) => self.masked_out(m, true), + Body::Merged(_) => Ok(false), + }; + Ok(draws && !u.mask_of.map(hidden).transpose()?.unwrap_or(false)) + } + /// The adjustment layers the import applies that a pass-through group with the layers `items` /// keeps to change what is below it: from the first one with nothing that draws below it in the /// group (hidden and clipped layers, other adjustment layers, layers and groups at 0 opacity, and @@ -1899,24 +1926,10 @@ impl [f32; 3]> Builder<'_, F> { if u.opacity <= 0.0 { return Ok(()); } + // A unit that draws nothing has nothing to change. let mut below = false; for b in out.iter().filter(|b| b.visible && b.opacity > 0.0) { - let draws = match &b.body { - // An adjustment layer, a layer that draws nothing or whose pixels are all - // transparent, or a group of such layers, has nothing to change. - Body::Layer(i) => !self.record(*i)?.draws_nothing() && !self.clear(*i)?, - Body::Group(_, items) => self.draws(items)?, - // Clipped layers merged with a base that has no pixels have none either. - Body::Merged(c) => c.px.iter().any(|p| p[3] > 0.0), - }; - // One whose layer mask hides all of it has nothing to change either. A base merged with - // the layers clipped to it isn't checked: its canvas may reach past the base's own pixels. - let hidden = |m: usize| match &b.body { - Body::Layer(_) => self.masked_out(m, false), - Body::Group(..) => self.masked_out(m, true), - Body::Merged(_) => Ok(false), - }; - below = below || (draws && !b.mask_of.map(hidden).transpose()?.unwrap_or(false)); + below = below || self.unit_draws(b)?; } // Kept below it in the group, or in a pass-through group below it there. let pending = !self.pending(out, level)?.is_empty(); diff --git a/crates/engine/src/cmd/fileio/tests_psdlayers.rs b/crates/engine/src/cmd/fileio/tests_psdlayers.rs index 4f1777c323..56322787da 100644 --- a/crates/engine/src/cmd/fileio/tests_psdlayers.rs +++ b/crates/engine/src/cmd/fileio/tests_psdlayers.rs @@ -1785,6 +1785,27 @@ fn an_adjustment_layer_above_a_group_whose_kept_ones_merged_is_applied() { } } +/// An adjustment layer in a pass-through group, over a layer drawn below that group in an outer +/// pass-through group, isn't kept to change what is below the outer group: a layer merged above +/// them for its mode merges it applied, and the import doesn't count it as not applied. The gray +/// 100 inverts to 155. +#[test] +fn an_adjustment_layer_in_a_group_over_a_drawn_layer_merges_applied() { + let layers = [ + group_end(), + layer("Photo", [0, 0, 1, 1], [100, 100, 100, 255]), + group_end(), + adjustment("Invert", b"nvrt", &[]), + group("Inner", b"pass"), + L { blend: b"lddg", ..layer("Top", [0, 0, 1, 1], [0, 0, 0, 255]) }, + group("Folder", b"pass"), + ]; + let (s, r) = open(&file(1, 1, &layers, 8, false, false), json!({})); + let d = &s.doc().unwrap().doc; + assert_eq!(row_of(&s, sublayer(&d.layers[0], "Top")), [[155, 155, 155, 255]], "{r}"); + assert!(!r["warnings"].to_string().contains("adjustment"), "{r}"); +} + /// A layer whose pixels are all transparent draws nothing: an adjustment layer above it in a /// pass-through group, directly or in a pass-through group inside it, still changes what is below /// the group. diff --git a/docs/mcp.md b/docs/mcp.md index d1e8f49dd7..504eab27ae 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -10919,7 +10919,8 @@ The rest are VectorCraft's own rules: transparency is kept. A hidden one is left out. One with nothing visible below it in its group that draws (a layer that stores no pixels, other than a solid color fill drawn over the canvas, a layer whose pixels are all transparent, or one at 0 opacity or 0 fill with nothing clipped to it doesn't, nor does a group of only such - layers, nor a layer or group whose layer mask hides all of it) changes nothing, and so does one at 0 opacity or 0 fill; in a Pass Through group it changes what is below the group only when the group is merged, and is otherwise not + layers, nor a layer or group whose layer mask hides all of it, for a layer a mask of 0 over each of its pixels that + isn't fully transparent) changes nothing, and so does one at 0 opacity or 0 fill; in a Pass Through group it changes what is below the group only when the group is merged, and is otherwise not applied. One whose layer mask needs more memory than allowed beside the canvases it would change is not applied, and a clip, or a merge for another layer, that it is part of goes on without it. - Other adjustment layers, Invert, Threshold and Posterize layers that others are clipped to, that are clipped to