From 0133157783ee17cf5c887867fa511b1dad4618d3 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 17 Nov 2026 14:09:49 -0800 Subject: [PATCH 1/7] PSD import: a layer with a knockout merges with the layers below it in its group Opening a PSD or PSB file with its layers as objects now applies a layer's Knockout setting instead of only counting it in a warning. The import options help page says that layers using a Knockout option are merged with the underlying layers: https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html A layer or group inside a group whose `knko` value is 1 is read as a shallow knockout and merged with the visible layers below it in that group into one image, named after them with " + ". Where it has pixels (shaped by its layer mask), the layers below are cleared to transparency and it is drawn there at its fill opacity; its opacity then fades the result back toward the layers below. In a pass-through group the cut shows what is outside the group, and the knockout is applied only when it and the visible layers below it are in the Normal mode, so that no layer stops blending with what is outside the group. The knockouts of top-level layers, clipped layers, layers that others are clipped to, other `knko` values, and merges that would need more memory than allowed are not applied: those layers come in as before and the existing warning counts only them. A hidden knockout that would need the merge is left out, like other hidden layers that need a merge. A knockout that draws nothing, or has nothing visible below it, merges nothing and comes in as before. --- crates/engine/src/cmd/fileio/psdlayers.rs | 334 ++++++++++++++---- .../engine/src/cmd/fileio/tests_psdlayers.rs | 128 +++++++ docs/target-app-parity.md | 2 +- 3 files changed, 395 insertions(+), 69 deletions(-) diff --git a/crates/engine/src/cmd/fileio/psdlayers.rs b/crates/engine/src/cmd/fileio/psdlayers.rs index cbfaa434d5..ded5288c69 100644 --- a/crates/engine/src/cmd/fileio/psdlayers.rs +++ b/crates/engine/src/cmd/fileio/psdlayers.rs @@ -24,14 +24,20 @@ //! Light, Pin Light, Hard Mix, Subtract, Divide, Darker Color, Lighter Color) is merged with the //! layers below it in its group. A Dissolve layer's partly transparent pixels are merged as opaque //! or transparent, opaque at the share of its pixels their transparency gives, in a fixed pattern -//! that differs from layer to layer. When a merge's canvases would need more memory than allowed, -//! the layer comes in alone: in the Normal mode, or unclipped. -//! Hidden layers are left out unless `hidden` asks for them. A hidden layer that would need a merge -//! (one in a mode VectorCraft lacks, one that layers are clipped to, one clipped to another, or -//! one below a merged layer) is always left out, with the layers clipped to it. Adjustment layers, -//! fill layers that store no pixels and whose fill isn't read or whose shape is a vector mask, -//! layer effects that are switched on, knockouts and vector masks aren't applied; the warnings -//! count them and the hidden layers left out. +//! that differs from layer to layer. A layer or group in a group whose Knockout setting is 1 (read +//! as a shallow knockout) is merged with the layers below it in its group too, as the import option +//! describes: where it has pixels, they are cleared to transparency and it is drawn there at its fill +//! opacity, and its opacity fades that back toward them. In a pass-through group this is done only +//! when it and the visible layers below it are in the Normal mode. When a merge's canvases would need more +//! memory than allowed, the layer comes in alone: in the Normal mode, unclipped, or without its +//! knockout. Hidden layers are left out unless `hidden` asks for them. A hidden layer that would need +//! a merge (one in a mode VectorCraft lacks, one with a knockout in a group, one that layers are +//! clipped to, one clipped to another, or one below a merged layer) is always left out, with the +//! layers clipped to it. Adjustment layers, fill layers that store no pixels and whose fill isn't read +//! or whose shape is a vector mask, layer effects that are switched on, vector masks, and the +//! knockouts of top-level layers, of clipped layers, of layers that others are clipped to and of +//! layers in a pass-through group with a layer in another mode, and other Knockout values, aren't +//! applied; the warnings count them and the hidden layers left out. //! //! The file is untrusted: every count, length and size is checked before anything is allocated, //! groups nest at most [`MAX_DEPTH`] deep (layers deeper than a document may nest merge into the @@ -227,7 +233,9 @@ struct Record<'a> { content: Content, effects: bool, vector_mask: bool, - knockout: bool, + /// The Knockout setting's byte (`knko`): 0 none, 1 a shallow knockout. Any other value isn't + /// applied. + knockout: u8, } /// The layer records of `info` (the layer count, the records, then their channels' data). @@ -285,7 +293,7 @@ fn record<'a>(r: &mut Reader<'a>, psb: bool) -> Result<(Record<'a>, Vec<(i16, us content: Content::Pixels, effects: false, vector_mask: false, - knockout: false, + knockout: 0, }; rec.read_extra(extra, psb)?; Ok((rec, lengths)) @@ -345,8 +353,7 @@ impl Record<'_> { b"lfx2" | b"lmfx" => effects = Some(effects == Some(true) || effects_on(data)), b"lrFX" => old_effects = true, b"vmsk" | b"vsms" => self.vector_mask = true, - // Shallow or deep knockout. - b"knko" => self.knockout = data.first().is_some_and(|&k| k != 0), + b"knko" => self.knockout = data.first().copied().unwrap_or(0), k if ADJUSTMENTS.contains(&k) => self.content = Content::Adjustment, _ => {} } @@ -646,6 +653,27 @@ enum Body<'i> { Merged(Canvas), } +/// Where a layer list is. +#[derive(Clone, Copy, PartialEq)] +enum Level { + /// The file's top level. + Top, + /// An isolated group, or a group drawn on its own. + Group, + /// A pass-through group whose layers blend with what is below it. + Pass, +} + +/// What a knockout's merge with the layers below it gives. +enum Knocked { + /// The merged canvas. + Merged(Canvas), + /// Nothing below it draws, or it draws nothing: it comes in as a layer of its own. + Nothing, + /// The canvases would need more memory than allowed. + TooLarge, +} + /// A unit's body drawn alone. enum Drawn { /// It draws nothing. @@ -663,6 +691,7 @@ struct Notes { fills: usize, effects: usize, vector_masks: usize, + /// Layers whose knockout wasn't applied. knockouts: usize, /// Hidden layers left out, and layers clipped to them. hidden: usize, @@ -678,6 +707,8 @@ struct Notes { #[derive(Clone, Copy)] struct Counts { skipped: usize, + /// The knockouts counted as not applied. + unknocked: usize, merged: usize, unmerged: usize, unclipped: usize, @@ -745,8 +776,8 @@ impl Notes { out } - fn counts(&self, skipped: usize) -> Counts { - Counts { skipped, merged: self.merged.len(), unmerged: self.unmerged, unclipped: self.unclipped, clipped_out: self.clipped_out } + fn counts(&self, skipped: usize, unknocked: usize) -> Counts { + Counts { skipped, unknocked, merged: self.merged.len(), unmerged: self.unmerged, unclipped: self.unclipped, clipped_out: self.clipped_out } } } @@ -771,6 +802,10 @@ struct Builder<'a, F> { skipped: Vec, /// Those records, in the order they were counted. skip_log: Vec, + /// The records counted as knockouts not applied. + unknocked: Vec, + /// Those records, in the order they were counted. + unknock_log: Vec, /// Bytes of layer and mask pixels decoded, and of canvases the merges made, so far. spent: Cell, } @@ -920,6 +955,10 @@ impl [f32; 3]> Builder<'_, F> { Some(b) => { // Counted even when it draws nothing (an adjustment layer). self.note(i)?; + // A clipped layer's knockout isn't applied. + if self.record(i)?.knockout != 0 { + self.unknock(i); + } if !unclipped && self.clip(&mut out, b, &body, i, &name, blend, opacity)? { continue; } @@ -1048,9 +1087,9 @@ impl [f32; 3]> Builder<'_, F> { Some(n) => *n = true, None => {} } - let (content, empty, effects, vector_mask, knockout) = { + let (content, empty, effects, vector_mask) = { let rec = self.record(i)?; - (rec.content, rec.stores_nothing(), rec.effects, rec.vector_mask, rec.knockout) + (rec.content, rec.stores_nothing(), rec.effects, rec.vector_mask) }; match content { Content::Type => self.notes.types += 1, @@ -1062,10 +1101,39 @@ impl [f32; 3]> Builder<'_, F> { } self.notes.effects += usize::from(effects); self.notes.vector_masks += usize::from(vector_mask); - self.notes.knockouts += usize::from(knockout); Ok(()) } + /// Count record `i` as a layer whose knockout isn't applied (once). + fn unknock(&mut self, i: usize) { + if let Some(s) = self.unknocked.get_mut(i) + && !*s + { + *s = true; + self.notes.knockouts += 1; + self.unknock_log.push(i); + } + } + + /// Does unit `u`, at `level` above the units `below` it, take a shallow knockout this import + /// applies? Only a layer or group in a group, neither clipped nor a base of clipped layers, does; + /// in a pass-through group, only when it and the visible units below it are in the Normal mode, so + /// that none of them blends with what is below the group. Any other knockout is counted as not + /// applied. + fn knocks(&mut self, u: &Unit<'_>, below: &[Unit<'_>], level: Level) -> Result { + let rec = self.record(u.record)?; + if rec.knockout == 0 { + return Ok(false); + } + let normal = |u: &Unit<'_>| u.blend == Blend::Mode(BlendMode::Normal); + let blends_out = level == Level::Pass && !(normal(u) && below.iter().filter(|b| b.visible).all(normal)); + let applies = rec.knockout == 1 && level != Level::Top && !blends_out && !rec.clipped && !matches!(u.body, Body::Merged(_)); + if !applies { + self.unknock(u.record); + } + Ok(applies) + } + /// Count record `i` as a hidden layer left out (once). fn skip(&mut self, i: usize) { if let Some(s) = self.skipped.get_mut(i) @@ -1077,10 +1145,36 @@ impl [f32; 3]> Builder<'_, F> { } } - /// `items`' units with the layers in blend modes VectorCraft lacks merged with those below. - fn merged<'i>(&mut self, items: &'i [Item], keep_hidden: bool) -> Result>, String> { + /// `items`' units, at `level`, with the layers in blend modes VectorCraft lacks, and in a group + /// the layers with a shallow knockout, merged with those below. + fn merged<'i>(&mut self, items: &'i [Item], keep_hidden: bool, level: Level) -> Result>, String> { let mut out: Vec> = vec![]; for mut u in self.units(items, keep_hidden)? { + if self.knocks(&u, &out, level)? { + // A hidden layer that would need the merge is left out. + if !u.visible { + self.skip(u.record); + continue; + } + let saved = self.notes.counts(self.skip_log.len(), self.unknock_log.len()); + match self.knock(&out, &u)? { + Knocked::Merged(canvas) => { + self.take_merge(&mut out, &u, canvas); + if let Blend::Other(other) = u.blend + && !self.notes.merged.contains(&other.label()) + { + self.notes.merged.push(other.label()); + } + continue; + } + // Nothing below it, or nothing of its own, to knock out: it comes in as a layer. + Knocked::Nothing => {} + Knocked::TooLarge => { + self.restore(saved); + self.unknock(u.record); + } + } + } let Blend::Other(other) = u.blend else { out.push(u); continue; @@ -1091,7 +1185,7 @@ impl [f32; 3]> Builder<'_, F> { continue; } // A merge that can't be made counts nothing its groups would have counted. - let saved = self.notes.counts(self.skip_log.len()); + let saved = self.notes.counts(self.skip_log.len(), self.unknock_log.len()); let made = match out.as_mut_slice() { // Onto the previous merge alone: composited onto its canvas, in place. [prev] if prev.is_merge() => { @@ -1113,34 +1207,7 @@ impl [f32; 3]> Builder<'_, F> { } _ => match self.merge(&out, &u)? { Some(canvas) => { - let mut name = String::new(); - for (k, b) in out.iter_mut().filter(|b| b.visible).enumerate() { - if k == 0 { - name = std::mem::take(&mut b.name); - } else { - name.push_str(" + "); - name.push_str(&b.name); - } - } - if out.iter().any(|b| b.visible) { - name.push_str(" + "); - } - name.push_str(&u.name); - // Hidden layers below are left out with the rest. - for b in out.iter().filter(|b| !b.visible).map(|b| b.record).collect::>() { - self.skip(b); - } - out.clear(); - out.push(Unit { - name, - visible: true, - opacity: 1.0, - blend: Blend::Mode(BlendMode::Normal), - mask_of: None, - record: u.record, - body: Body::Merged(canvas), - clip: None, - }); + self.take_merge(&mut out, &u, canvas); true } None => false, @@ -1168,6 +1235,12 @@ impl [f32; 3]> Builder<'_, F> { self.notes.hidden = self.notes.hidden.saturating_sub(1); } } + for i in self.unknock_log.split_off(c.unknocked) { + if let Some(s) = self.unknocked.get_mut(i) { + *s = false; + self.notes.knockouts = self.notes.knockouts.saturating_sub(1); + } + } self.notes.merged.truncate(c.merged); self.notes.unmerged = c.unmerged; self.notes.unclipped = c.unclipped; @@ -1186,6 +1259,82 @@ impl [f32; 3]> Builder<'_, F> { Ok(Some(acc.unwrap_or_default())) } + /// `out` replaced by one layer: `canvas`, the merge of its visible units with `top`, named after + /// them, bottom first. Its hidden units are left out. + fn take_merge<'i>(&mut self, out: &mut Vec>, top: &Unit<'_>, canvas: Canvas) { + let mut name = String::new(); + for (k, b) in out.iter_mut().filter(|b| b.visible).enumerate() { + if k == 0 { + name = std::mem::take(&mut b.name); + } else { + name.push_str(" + "); + name.push_str(&b.name); + } + } + if out.iter().any(|b| b.visible) { + name.push_str(" + "); + } + name.push_str(&top.name); + // Hidden layers below are left out with the rest. + for b in out.iter().filter(|b| !b.visible).map(|b| b.record).collect::>() { + self.skip(b); + } + out.clear(); + out.push(Unit { + name, + visible: true, + opacity: 1.0, + blend: Blend::Mode(BlendMode::Normal), + mask_of: None, + record: top.record, + body: Body::Merged(canvas), + clip: None, + }); + } + + /// The visible units `below` with `top`, a layer with a shallow knockout, knocking them out: where + /// `top` has pixels, what is below it in its group is cleared to transparency (the group's own + /// backdrop) and `top` is drawn there at its fill opacity; its opacity then fades all of that + /// back toward the units below as they were. Its layer mask shapes the cut. + fn knock(&mut self, below: &[Unit<'_>], top: &Unit<'_>) -> Result { + if !below.iter().any(|u| u.visible) { + return Ok(Knocked::Nothing); + } + let (fill, opacity) = { + let rec = self.record(top.record)?; + (rec.fill, rec.opacity) + }; + let k = match self.draw_body(&top.body, top.mask_of)? { + Drawn::Nothing => return Ok(Knocked::Nothing), + Drawn::TooLarge => return Ok(Knocked::TooLarge), + Drawn::Canvas(c) if c.px.iter().all(|p| p[3] <= 0.0) => return Ok(Knocked::Nothing), + Drawn::Canvas(c) => c, + }; + // The layer's canvas is held while the units below are drawn. + let bytes = 16 * k.px.len() as u64; + self.held = self.held.saturating_add(bytes); + let mut acc: Option = None; + let mut drawn = Ok(true); + for u in below.iter().filter(|u| u.visible) { + drawn = self.composite(&mut acc, u); + if !matches!(drawn, Ok(true)) { + break; + } + } + self.held = self.held.saturating_sub(bytes); + if !drawn? { + return Ok(Knocked::TooLarge); + } + // Nothing below draws. + if acc.is_none() { + return Ok(Knocked::Nothing); + } + if !knock_onto(&mut acc, &k, top.blend, fill, opacity, top.record, self.room(), &|n| self.spend(n))? { + return Ok(Knocked::TooLarge); + } + Ok(acc.map_or(Knocked::Nothing, Knocked::Merged)) + } + /// The bytes a merge step may still take: the memory allowed, less the copies of what is below /// the pass-through groups being merged. fn room(&self) -> u64 { @@ -1203,7 +1352,7 @@ impl [f32; 3]> Builder<'_, F> { _ => None, }; if u.opacity >= 1.0 && mask.is_none() { - return self.lay(acc, items); + return self.lay(acc, items, Level::Pass); } let bytes = acc.as_ref().map_or(0, |c| 16 * c.px.len() as u64); if bytes > self.room() { @@ -1212,7 +1361,7 @@ impl [f32; 3]> Builder<'_, F> { self.spend(bytes)?; let below = acc.clone(); self.held = self.held.saturating_add(bytes); - let laid = self.lay(acc, items); + let laid = self.lay(acc, items, Level::Pass); self.held = self.held.saturating_sub(bytes); if !laid? { return Ok(false); @@ -1230,10 +1379,10 @@ impl [f32; 3]> Builder<'_, F> { blend_onto(acc, &c, u.blend, u.opacity, u.record, self.room(), &|n| self.spend(n)) } - /// `items`' layers composited onto `acc` one by one; `false` when a canvas would need more - /// memory than allowed. - fn lay(&mut self, acc: &mut Option, items: &[Item]) -> Result { - for c in self.merged(items, false)? { + /// `items`' layers, the layers of a group at `level`, composited onto `acc` one by one; `false` + /// when a canvas would need more memory than allowed. + fn lay(&mut self, acc: &mut Option, items: &[Item], level: Level) -> Result { + for c in self.merged(items, false, level)? { if !self.composite(acc, &c)? { return Ok(false); } @@ -1273,7 +1422,7 @@ impl [f32; 3]> Builder<'_, F> { }, Body::Group(items) => { let mut acc: Option = None; - if !self.lay(&mut acc, items)? { + if !self.lay(&mut acc, items, Level::Group)? { return Ok(Drawn::TooLarge); } let Some(acc) = acc else { return Ok(Drawn::Nothing) }; @@ -1306,10 +1455,10 @@ impl [f32; 3]> Builder<'_, F> { Ok(n) } - /// Layers of the document for `items`, bottom first. - fn list(&mut self, items: &[Item]) -> Result>, String> { + /// Layers of the document for `items`, the layers at `level`, bottom first. + fn list(&mut self, items: &[Item], level: Level) -> Result>, String> { let mut out = vec![]; - for u in self.merged(items, self.hidden)? { + for u in self.merged(items, self.hidden, level)? { if let Some(n) = self.node(u)? { out.push(Arc::new(n)); } @@ -1329,7 +1478,7 @@ impl [f32; 3]> Builder<'_, F> { Some((left, top, img)) => vec![Arc::new(self.image_node(&u.name, left, top, img)?)], None => return Ok(None), }, - Body::Group(items) => self.list(items)?, + Body::Group(items) => self.list(items, if u.blend == Blend::Pass { Level::Pass } else { Level::Group })?, }; let mut layer = self.layer(&u.name); layer.visible = u.visible; @@ -1443,6 +1592,61 @@ fn blend_onto( max_alloc: u64, spend: &dyn Fn(u64) -> Result<(), String>, ) -> Result { + if !cover(acc, src, max_alloc, spend)? { + return Ok(false); + } + let Some(out) = acc.as_mut() else { return Ok(false) }; + // Only the source's rows and columns change. + let (sw, x0, y0) = (src.width.max(1), (src.left - out.left) as usize, (src.top - out.top) as usize); + for (k, s) in src.px.iter().enumerate() { + let Some(p) = out.px.get_mut((y0 + k / sw) * out.width + x0 + k % sw) else { continue }; + let a = coverage(blend, s[3] * opacity, src.left + (k % sw) as i64, src.top + (k / sw) as i64, record); + *p = over(blend, *p, [s[0], s[1], s[2], a]); + } + Ok(true) +} + +/// `knock` (of record `record`, in `blend`, with its `fill` and `opacity`), a layer with a shallow +/// knockout, knocking out `acc`, on a canvas covering both, as [`blend_onto`] makes it. Where +/// `knock` has pixels, its transparency is the share of `acc` cleared and replaced by its color at +/// `fill`; `opacity` then fades that back toward `acc`. `false`, with `acc` as it was, when the +/// canvases would take more than `max_alloc` bytes. +#[allow(clippy::too_many_arguments)] +fn knock_onto( + acc: &mut Option, + knock: &Canvas, + blend: Blend, + fill: f32, + opacity: f32, + record: usize, + max_alloc: u64, + spend: &dyn Fn(u64) -> Result<(), String>, +) -> Result { + if !cover(acc, knock, max_alloc, spend)? { + return Ok(false); + } + let Some(out) = acc.as_mut() else { return Ok(false) }; + let (fill, opacity) = (fill.clamp(0.0, 1.0), opacity.clamp(0.0, 1.0)); + let (kw, x0, y0) = (knock.width.max(1), (knock.left - out.left) as usize, (knock.top - out.top) as usize); + for (k, s) in knock.px.iter().enumerate() { + let Some(p) = out.px.get_mut((y0 + k / kw) * out.width + x0 + k % kw) else { continue }; + let t = opacity * coverage(blend, s[3].clamp(0.0, 1.0), knock.left + (k % kw) as i64, knock.top + (k / kw) as i64, record); + let below = p[3].clamp(0.0, 1.0); + let alpha = (1.0 - t) * below + t * fill; + if alpha <= 0.0 { + *p = [0.0; 4]; + continue; + } + let mix = |i: usize| ((1.0 - t) * below * p[i] + t * fill * s[i]) / alpha; + *p = [mix(0), mix(1), mix(2), alpha]; + } + Ok(true) +} + +/// `acc` grown, when it doesn't already, to a canvas covering itself and `src` (a new one counted +/// with `spend`). `false`, with `acc` as it was, when the canvases held, `acc` and `src` and a +/// larger canvas when one is needed, would take more than `max_alloc` bytes. +fn cover(acc: &mut Option, src: &Canvas, max_alloc: u64, spend: &dyn Fn(u64) -> Result<(), String>) -> Result { let (mut left, mut top, mut right, mut bottom) = (src.left, src.top, src.left + src.width as i64, src.top + src.height as i64); if let Some(a) = acc.as_ref() { left = left.min(a.left); @@ -1475,14 +1679,6 @@ fn blend_onto( } *acc = Some(out); } - let Some(out) = acc.as_mut() else { return Ok(false) }; - // Only the source's rows and columns change. - let (sw, x0, y0) = (src.width.max(1), (src.left - left) as usize, (src.top - top) as usize); - for (k, s) in src.px.iter().enumerate() { - let Some(p) = out.px.get_mut((y0 + k / sw) * width + x0 + k % sw) else { continue }; - let a = coverage(blend, s[3] * opacity, src.left + (k % sw) as i64, src.top + (k / sw) as i64, record); - *p = over(blend, *p, [s[0], s[1], s[2], a]); - } Ok(true) } @@ -1523,9 +1719,11 @@ pub(super) fn import( noted: vec![false; records.len()], skipped: vec![false; records.len()], skip_log: vec![], + unknocked: vec![false; records.len()], + unknock_log: vec![], spent: Cell::new(0), }; - let layers = b.list(&items)?; + let layers = b.list(&items, Level::Top)?; let Builder { mut doc, notes, .. } = b; if !layers.is_empty() { doc.layers = layers; diff --git a/crates/engine/src/cmd/fileio/tests_psdlayers.rs b/crates/engine/src/cmd/fileio/tests_psdlayers.rs index 6c227c0e17..61db4b86fc 100644 --- a/crates/engine/src/cmd/fileio/tests_psdlayers.rs +++ b/crates/engine/src/cmd/fileio/tests_psdlayers.rs @@ -646,6 +646,134 @@ fn dissolve_layers_merge_as_opaque_or_transparent_pixels() { assert!((32..=96).contains(&red), "{red} of 256 pixels show the lower layer: {r}"); } +/// A layer with a Knockout value of `knko` and a fill opacity of `fill`. +fn knockout(l: L, knko: u8, fill: u8) -> L { + let mut l = l; + l.blocks.push((*b"knko", vec![knko, 0, 0, 0])); + l.blocks.push((*b"iOpa", vec![fill])); + l +} + +/// A green layer under a group of `group_blend` holding a red layer and `cut` above it. +fn knockout_stack(group_blend: &'static [u8; 4], cut: L) -> Vec { + vec![ + layer("Outside", [0, 0, 4, 4], [0, 255, 0, 255]), + group_end(), + layer("Lower", [0, 0, 4, 4], [255, 0, 0, 255]), + cut, + group("Group", group_blend), + ] +} + +/// The names of the document's layers, and of the layers in its second one. +fn knockout_names(s: &Session) -> (Vec, Vec) { + let d = &s.doc().unwrap().doc; + let names = |ls: &[std::sync::Arc]| ls.iter().map(|l| name(l).to_string()).collect::>(); + (names(&d.layers), d.layers.get(1).and_then(|g| g.children()).map_or(vec![], |c| names(c))) +} + +/// A layer with a shallow knockout in a group merges with the layers below it in the group: its +/// shape clears them to transparency, it draws there at its fill opacity, and its opacity fades +/// that. What is outside the group stays a layer of its own and shows through. +#[test] +fn knockout_layers_merge_with_the_layers_below_them_in_their_group() { + let cut = |fill: u8, opacity: u8| knockout(L { opacity, ..layer("Cut", [0, 0, 2, 2], [0, 0, 255, 255]) }, 1, fill); + for (fill, opacity, at_cut) in [(0, 255, [0, 0, 0, 0]), (128, 255, [0, 0, 255, 128]), (0, 128, [255, 0, 0, 127]), (255, 255, [0, 0, 255, 255])] { + for group_blend in [b"norm", b"pass"] { + let (s, r) = open(&file(4, 4, &knockout_stack(group_blend, cut(fill, opacity)), 8, false, false), json!({})); + assert_eq!(r["warnings"], json!([]), "{r}"); + let (top, inner) = knockout_names(&s); + assert_eq!((top, inner), (vec!["Outside".to_string(), "Group".into()], vec!["Lower + Cut".to_string()]), "{fill} {opacity}"); + let d = &s.doc().unwrap().doc; + assert_eq!(d.layers[1].isolate, group_blend == b"norm"); + let ((x, y), px) = image_of(&s, &d.layers[1].children().unwrap()[0]); + assert_eq!(((x, y), px.dimensions()), ((0.0, 0.0), (4, 4))); + assert_eq!((px.get_pixel(1, 1).0, px.get_pixel(3, 3).0), (at_cut, [255, 0, 0, 255]), "{fill} {opacity}"); + } + } + // A layer mask shapes the cut: where it hides the layer, nothing below is cleared. + let mut masked = cut(0, 255); + masked.mask = Some(([0, 0, 1, 1], 255, 0, vec![0])); + let (s, r) = open(&file(4, 4, &knockout_stack(b"norm", masked), 8, false, false), json!({})); + let px = image_of(&s, &s.doc().unwrap().doc.layers[1].children().unwrap()[0]).1; + assert_eq!((px.get_pixel(0, 0).0, px.get_pixel(1, 1).0), ([255, 0, 0, 255], [0, 0, 0, 0]), "{r}"); + // Without a knockout (0), the layers stay apart. + let (s, r) = + open(&file(4, 4, &knockout_stack(b"norm", knockout(layer("Cut", [0, 0, 2, 2], [0, 0, 255, 255]), 0, 0)), 8, false, false), json!({})); + assert_eq!((knockout_names(&s).1, &r["warnings"]), (vec!["Lower".to_string(), "Cut".into()], &json!([])), "{r}"); +} + +/// The knockouts this import doesn't apply come in as layers, counted in a warning: at the top level, +/// on a clipped layer, and with a Knockout value other than 1. Hidden ones that would need the merge +/// are left out; one that draws nothing, or nothing below it, merges nothing. +#[test] +fn knockouts_that_arent_applied_are_said() { + const UNAPPLIED: &str = "the knockout of 1 layer wasn't applied"; + let cut = |l: L, knko: u8| knockout(l, knko, 0); + let blue = || layer("Cut", [0, 0, 2, 2], [0, 0, 255, 255]); + // At the top level. + let layers = [layer("Lower", [0, 0, 4, 4], [255, 0, 0, 255]), cut(blue(), 1)]; + let (s, r) = open(&file(4, 4, &layers, 8, false, false), json!({})); + assert_eq!((knockout_names(&s).0, &r["warnings"]), (vec!["Lower".to_string(), "Cut".into()], &json!([UNAPPLIED])), "{r}"); + // Another Knockout value, and a clipped layer, in a group. + let (s, r) = open(&file(4, 4, &knockout_stack(b"norm", cut(blue(), 2)), 8, false, false), json!({})); + assert_eq!((knockout_names(&s).1, &r["warnings"]), (vec!["Lower".to_string(), "Cut".into()], &json!([UNAPPLIED])), "{r}"); + let (s, r) = open(&file(4, 4, &knockout_stack(b"norm", cut(L { clipped: true, ..blue() }, 1)), 8, false, false), json!({})); + assert_eq!((knockout_names(&s).1, &r["warnings"]), (vec!["Lower + Cut".to_string()], &json!([UNAPPLIED])), "{r}"); + // In a pass-through group, a layer below it, or the knockout itself, in another mode blends with + // what is outside the group: the layers stay apart, and the lower one still multiplies the green + // outside. In an isolated group they merge. + let mut multiply = knockout_stack(b"pass", cut(blue(), 1)); + multiply[2].blend = b"mul "; + let (s, r) = open(&file(4, 4, &multiply, 8, false, false), json!({})); + assert_eq!((knockout_names(&s).1, &r["warnings"]), (vec!["Lower".to_string(), "Cut".into()], &json!([UNAPPLIED])), "{r}"); + assert_eq!(s.doc().unwrap().doc.layers[1].children().unwrap()[0].blend, BlendMode::Multiply); + let (s, r) = open(&file(4, 4, &knockout_stack(b"pass", cut(L { blend: b"mul ", ..blue() }, 1)), 8, false, false), json!({})); + assert_eq!((knockout_names(&s).1, &r["warnings"]), (vec!["Lower".to_string(), "Cut".into()], &json!([UNAPPLIED])), "{r}"); + multiply[4] = group("Group", b"norm"); + let (s, r) = open(&file(4, 4, &multiply, 8, false, false), json!({})); + assert_eq!((knockout_names(&s).1, &r["warnings"]), (vec!["Lower + Cut".to_string()], &json!([])), "{r}"); + // A hidden knockout would need the merge: left out, hidden layers asked for or not. + for hidden in [false, true] { + let (s, r) = + open(&file(4, 4, &knockout_stack(b"norm", cut(L { hidden: true, ..blue() }, 1)), 8, false, false), json!({"hiddenLayers": hidden})); + assert_eq!((knockout_names(&s).1, &r["warnings"]), (vec!["Lower".to_string()], &json!([HIDDEN_1])), "{hidden}: {r}"); + } + // One that stores no pixels or only transparent ones cuts nothing, and merges nothing. + for (l, inner) in [(L { bare: true, ..blue() }, vec!["Lower"]), (layer("Cut", [0, 0, 2, 2], [0, 0, 255, 0]), vec!["Lower", "Cut"])] { + let (s, r) = open(&file(4, 4, &knockout_stack(b"norm", cut(l, 1)), 8, false, false), json!({})); + assert_eq!((knockout_names(&s).1, &r["warnings"]), (inner.iter().map(|n| n.to_string()).collect(), &json!([])), "{r}"); + } + // Nothing below it in its group: it comes in as a layer. + let layers = [group_end(), cut(blue(), 1), group("Group", b"norm"), layer("Over", [0, 0, 1, 1], [9, 9, 9, 255])]; + let (s, r) = open(&file(4, 4, &layers, 8, false, false), json!({})); + let d = &s.doc().unwrap().doc; + assert_eq!(d.layers[0].children().unwrap().iter().map(|c| name(c)).collect::>(), ["Cut"], "{r}"); + assert_eq!(r["warnings"], json!([]), "{r}"); +} + +/// A knockout's merge whose canvases would need more memory than allowed leaves the layers apart, +/// with a warning. +#[test] +fn a_knockout_merge_past_the_memory_allowed_leaves_the_layers_apart() { + let cms = |c: [f32; 4]| [c[0], c[1], c[2]]; + let layers = [ + group_end(), + layer("Lower", [0, 0, 2, 2], [255, 0, 0, 255]), + knockout(layer("Cut", [2, 2, 4, 4], [0, 0, 255, 255]), 1, 0), + group("Group", b"norm"), + ]; + let f = file(4, 4, &layers, 8, false, false); + // Each 2 × 2 layer fits 128 bytes (16 a pixel), but not the canvas of the layer below with the + // knockout's canvas held beside it. + let (doc, warnings) = psdlayers::import(&f, false, 128, cms).unwrap().unwrap(); + assert_eq!(doc.layers[0].children().unwrap().iter().map(|c| name(c)).collect::>(), ["Lower", "Cut"]); + assert_eq!(warnings, ["the knockout of 1 layer wasn't applied"]); + let (doc, warnings) = psdlayers::import(&f, false, 1 << 20, cms).unwrap().unwrap(); + assert_eq!(doc.layers[0].children().unwrap().iter().map(|c| name(c)).collect::>(), ["Lower + Cut"]); + assert!(warnings.is_empty(), "{warnings:?}"); +} + #[test] fn what_doesnt_come_in_is_said() { let mut text = layer("Words", [0, 0, 2, 2], [0, 0, 0, 255]); diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 900dd99aab..e4bbf9dbbb 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -2040,7 +2040,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur EPS export and EPS Save make each file's PNG thumbnail from its TIFF preview, averaging the preview's pixels down to the thumbnail's size on white, instead of drawing the document a second time; a file without a preview still draws its thumbnail (`crates/engine/src/cmd/fileio/tests_eps.rs`). - Targa files (`.tga`, `.vda`, `.icb`, `.vst`) open, place linked or embedded, relink and update at their pixel size: color-mapped, true-color and black-and-white, raw or run-length encoded, in any of the four row orders. The image descriptor's attribute bits and the extension area's Attributes Type decide the transparency: a file with no attribute bits, or with attribute data the Attributes Type calls undefined, opens opaque; with one bit, a 16-bit true-color pixel takes that bit as its alpha, and a color-mapped pixel takes the bit of the 16-bit color map entry it indexes; premultiplied colors are divided by their alpha. Open and Place recognize a Targa file by its extension, and `clipboard.importImage` and Glass's Load Texture… by its header; a Targa file copied in a file manager pastes as a bitmap (`crates/engine/src/cmd/fileio/tests_tiffbmp.rs`, `crates/engine/src/tests/links.rs`). - BMP files named `.dib` or `.rle` (run-length encoded bitmaps included), Photoshop files named `.pdd` and PNG files named `.pns` open and place, linked or embedded, as BMP, PSD and PNG files, in any letter case: the Open and Place dialogs list them with their format, the Links panel names their format, and Edit Original opens them. Exports write only `.bmp`, `.psd` and `.png` (`crates/engine/src/cmd/fileio/tests_tiffbmp.rs`, `crates/engine/src/cmd/fileio/tests_psdread.rs`, `crates/engine/src/tests/links.rs`). - - File › Open converts a PSD or PSB file's layers to objects, keeping the layer structure, merging clipped layers into one layer and leaving out hidden layers that would need a merge, as [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for the option that converts layers to objects; hidden layers come in with `hiddenLayers`, as Import Hidden Layers brings them in, and `layers: false` opens the merged image. VectorCraft's own rules: each layer becomes a layer holding its pixels with its name, visibility, opacity and blend mode, groups become layers of sublayers, layers in Dissolve, Linear Burn, Linear Dodge, Vivid Light, Linear Light, Pin Light, Hard Mix, Subtract, Divide, Darker Color and Lighter Color are merged with the layers below, layer masks become opacity masks, fill opacity multiplies the opacity (in a layer that others are clipped to, it fades only that layer's own pixels), and adjustment layers, layer effects, knockouts and vector masks aren't applied and are counted in the warnings. 8-, 16- and 32-bit files, PSB, raw, PackBits and zipped channels (`crates/engine/src/cmd/fileio/tests_psdlayers.rs`). + - File › Open converts a PSD or PSB file's layers to objects, keeping the layer structure, merging clipped layers into one layer, merging a layer that uses a Knockout option with the layers below it, and leaving out hidden layers that would need a merge, as [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for the option that converts layers to objects; hidden layers come in with `hiddenLayers`, as Import Hidden Layers brings them in, and `layers: false` opens the merged image. VectorCraft's own rules: each layer becomes a layer holding its pixels with its name, visibility, opacity and blend mode, groups become layers of sublayers, layers in Dissolve, Linear Burn, Linear Dodge, Vivid Light, Linear Light, Pin Light, Hard Mix, Subtract, Divide, Darker Color and Lighter Color are merged with the layers below, layer masks become opacity masks, fill opacity multiplies the opacity (in a layer that others are clipped to, it fades only that layer's own pixels), a Knockout value of 1 is read as a shallow knockout and applied to a layer or group inside a group, which is merged with the layers below it in that group (its pixels clear them to transparency, it is drawn there at its fill opacity, and its opacity fades the result; in a pass-through group only when it and the layers below it are in the Normal mode), and adjustment layers, layer effects, vector masks and the other knockouts (top-level layers, clipped layers, layers that others are clipped to, pass-through groups with layers in other modes, other Knockout values) aren't applied and are counted in the warnings. 8-, 16- and 32-bit files, PSB, raw, PackBits and zipped channels (`crates/engine/src/cmd/fileio/tests_psdlayers.rs`). - File › Open of a PSD or PSB file with two or more layers, and File › Place of one without Link, show PSD Import Options: Convert Layers to Objects with Import Hidden Layers, or Flatten Layers to a Single Image, the options [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for a file that is opened or embedded. A file placed with Link shows its flat image and isn't asked about. `file.place` takes `layers` and `hiddenLayers`, and `document.psdInfo` counts a file's layers without opening it. VectorCraft's own rules: placed layers come in as one group named after the file, a layer as an image and a layer group as a group, at the place the merged image would take and clipped to the canvas when layers reach past it; the dialog asks from two layers up and starts from the choices made last. Layer comps aren't read (`crates/engine/src/cmd/fileio/tests_psdlayers.rs`, `crates/ui-egui/src/dialogs/psd_import.rs`). - A PSD or PSB file's clipping path, the saved path that image resource 2999 names in the [PSD file format specification](https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/), clips its merged image on File › Place and on File › Open of the merged image, as [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for a file imported as one flattened image. The image comes in a clip group whose clipping path has no paint and uses the even-odd rule, and the path can be edited or released. `links.update` and `links.relink` give the clip group the clipping path the new file names, or leave a plain group when it names none, and files that name no clipping path come in as before (`crates/engine/src/cmd/fileio/tests_psdread.rs`). - Import Slices: File › Open of a PSD or PSB file with slices, and File › Place of one without Link, bring its slices in as user slices, as [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for a file that is opened or embedded; PSD Import Options offers Import Slices for such a file. `document.open` and `file.place` take `importSlices`, and `document.psdInfo` counts the slices. The slices are read from image resource 1050 in version 6 and in the descriptor of versions 7 and 8, as the [Photoshop File Formats Specification](https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/) describes it. VectorCraft's own rules: Import Slices applies to Convert Layers to Objects and to Flatten Layers to a Single Image alike, and makes the dialog ask for a file with fewer than two layers; placed slices move and scale with the art in the same undo step; each slice keeps its name, type, link, target, message, alternative text, cell text and background color; slices the file generated automatically, and slices wholly outside the canvas, are left out; slices that can't be read are left out with a warning while the art comes in; with Show Import Options off, a PSD file is placed with the PSD Import Options chosen last, and on, each PSD file of the place cursor asks for them (`crates/engine/src/cmd/fileio/tests_psdslices.rs`, `crates/ui-egui/src/dialogs/psd_import.rs`). From 313d889a7f513ea0157ab0fd4bc736e2ab97851f Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 17 Nov 2026 16:54:12 -0800 Subject: [PATCH 2/7] PSD import: a group that draws nothing is drawn once, however deep it nests A group with a knockout was drawn for the knockout and, when it drew nothing, drawn again as a layer of its own. Nested inside each other, such groups were drawn twice over for every level, so opening a crafted PSD file could take exponential time; layers without pixels charge nothing against the import's memory allowance, so it did not stop it. The import now keeps what a group gave when drawn without reading any layer's pixels (nothing, or a canvas without pixels) and gives it again without walking the group's layers. A pass-through group that laid nothing onto an empty canvas without reading any pixels is not laid there again; its layers count the same warnings each time. A merge that can't be made forgets the groups found during it, with the notes it gives back. A knockout group that comes in as a layer is also listed after being drawn, and its layers clipped to a fill that isn't drawn, or left unclipped by a merge past the memory allowed, were counted twice in the warnings. Each is now counted once. The knockout of a layer that others are clipped to was applied when the clip merge needed more memory than allowed and the layers came in apart. It is now left unapplied and counted, as it is when the clip merge is made. A Dissolve knockout at part opacity cleared every pixel below it in part. It now clears each pixel fully or not at all, at the share its transparency times its opacity gives, as other Dissolve merges do. --- crates/engine/src/cmd/fileio/psdlayers.rs | 198 +++++++++++++++--- .../engine/src/cmd/fileio/tests_psdlayers.rs | 137 ++++++++++++ 2 files changed, 308 insertions(+), 27 deletions(-) diff --git a/crates/engine/src/cmd/fileio/psdlayers.rs b/crates/engine/src/cmd/fileio/psdlayers.rs index ded5288c69..c2f0dd5ff2 100644 --- a/crates/engine/src/cmd/fileio/psdlayers.rs +++ b/crates/engine/src/cmd/fileio/psdlayers.rs @@ -72,6 +72,13 @@ const MAX_CHANNELS: usize = 56; const MIN_RECORD: usize = 34; const DAMAGED: &str = "the PSD file's layer records are damaged"; +#[cfg(test)] +thread_local! { + /// The layer lists imports on this thread have walked, and the most a test lets them walk + /// before the import stops with an error. + pub(super) static WALKS: Cell<(usize, usize)> = const { Cell::new((0, usize::MAX)) }; +} + /// The keys of the tagged blocks that make a layer an adjustment layer. const ADJUSTMENTS: [&[u8; 4]; 18] = [ b"levl", b"curv", b"brit", b"blnc", b"hue ", b"hue2", b"selc", b"mixr", b"grdm", b"phfl", b"expA", b"vibA", b"thrs", b"nvrt", b"post", b"clrL", @@ -619,6 +626,8 @@ struct Unit<'i> { body: Body<'i>, /// While layers are clipped to it: how its merged canvas is shaped. clip: Option, + /// Layers are clipped to it, merged into it or not. + base: bool, } impl Unit<'_> { @@ -647,8 +656,8 @@ enum Clip { enum Body<'i> { /// A layer record's pixels. Layer(usize), - /// A group's layers. - Group(&'i [Item]), + /// A group's record and its layers. + Group(usize, &'i [Item]), /// Layers merged into one image. Merged(Canvas), } @@ -675,6 +684,7 @@ enum Knocked { } /// A unit's body drawn alone. +#[derive(Clone)] enum Drawn { /// It draws nothing. Nothing, @@ -713,6 +723,8 @@ struct Counts { unmerged: usize, unclipped: usize, clipped_out: usize, + /// The groups found to read no pixels. + blank: usize, } /// Adds the warning for `n` things to `out`: `one` when `n` is 1, `many` with the count in place @@ -776,8 +788,16 @@ impl Notes { out } - fn counts(&self, skipped: usize, unknocked: usize) -> Counts { - Counts { skipped, unknocked, merged: self.merged.len(), unmerged: self.unmerged, unclipped: self.unclipped, clipped_out: self.clipped_out } + fn counts(&self, skipped: usize, unknocked: usize, blank: usize) -> Counts { + Counts { + skipped, + unknocked, + merged: self.merged.len(), + unmerged: self.unmerged, + unclipped: self.unclipped, + clipped_out: self.clipped_out, + blank, + } } } @@ -806,8 +826,26 @@ struct Builder<'a, F> { unknocked: Vec, /// Those records, in the order they were counted. unknock_log: Vec, + /// The records counted as layers clipped to a fill that isn't drawn. + clipped_out: Vec, + /// Those records, in the order they were counted. + clipped_out_log: Vec, + /// The records counted as clipped layers left unclipped. + unclipped: Vec, + /// Those records, in the order they were counted. + unclip_log: Vec, /// Bytes of layer and mask pixels decoded, and of canvases the merges made, so far. spent: Cell, + /// The layers whose pixels were read so far (each time they were). + painted: Cell, + /// What each group gave when drawn alone without reading any layer's pixels (nothing, or a + /// canvas without pixels, whatever the memory left): drawing it again gives that without + /// walking its layers. + blank: Vec>, + /// The pass-through groups that laid nothing onto nothing (see [`Self::laid_nothing`]). + laid_blank: Vec, + /// The groups of both, in the order they were found. + blank_log: Vec, } impl [f32; 3]> Builder<'_, F> { @@ -849,6 +887,7 @@ impl [f32; 3]> Builder<'_, F> { Paint::Cmyk(ink) => (self.cmyk)(ink), }; let px = image::Rgba([byte(rgb[0]), byte(rgb[1]), byte(rgb[2]), 255]); + self.painted.set(self.painted.get() + 1); return Ok(Some((0, 0, RgbaImage::from_pixel(h.width, h.height, px)))); } Content::Pixels | Content::Type | Content::Solid(_) | Content::Fill => rec.bounds.size(), @@ -880,6 +919,7 @@ impl [f32; 3]> Builder<'_, F> { let rgb = to_rgb(h, |c| planes.get(c).map_or(0.0, |p| at(p, linear, 0.0)), &self.cmyk); image::Rgba([byte(rgb[0]), byte(rgb[1]), byte(rgb[2]), byte(at(&alpha, false, 1.0))]) }); + self.painted.set(self.painted.get() + 1); Ok(Some((i64::from(rec.bounds.left), i64::from(rec.bounds.top), img))) } @@ -932,7 +972,7 @@ impl [f32; 3]> Builder<'_, F> { for item in items { let (i, body) = match item { Item::Layer(i) => (*i, Body::Layer(*i)), - Item::Group(i, children) => (*i, Body::Group(children)), + Item::Group(i, children) => (*i, Body::Group(*i, children)), }; let rec = self.record(i)?; let (name, hidden, clipped, blend) = (rec.name.clone(), rec.hidden, rec.clipped, rec.blend); @@ -959,14 +999,17 @@ impl [f32; 3]> Builder<'_, F> { if self.record(i)?.knockout != 0 { self.unknock(i); } + if let Some(u) = out.get_mut(b) { + u.base = true; + } if !unclipped && self.clip(&mut out, b, &body, i, &name, blend, opacity)? { continue; } // The merge doesn't fit: this layer and the next ones clipped to the same // base come in as layers of their own. unclipped = true; - self.notes.unclipped += 1; - out.push(Unit { name, visible: true, opacity, blend, mask_of: Some(i), record: i, body, clip: None }); + self.unclip(i); + out.push(Unit { name, visible: true, opacity, blend, mask_of: Some(i), record: i, body, clip: None, base: false }); } } continue; @@ -978,7 +1021,7 @@ impl [f32; 3]> Builder<'_, F> { continue; } self.note(i)?; - out.push(Unit { name, visible: !hidden, opacity, blend, mask_of: Some(i), record: i, body, clip: None }); + out.push(Unit { name, visible: !hidden, opacity, blend, mask_of: Some(i), record: i, body, clip: None, base: false }); base = Some(Some(out.len() - 1)); } for u in &mut out { @@ -1001,7 +1044,7 @@ impl [f32; 3]> Builder<'_, F> { }; // A fill whose shape is a vector mask isn't drawn, and nothing clipped to it is either. if undrawn && u.clip.is_none() { - self.notes.clipped_out += 1; + self.clip_out(i); return Ok(true); } let layer = match self.draw_body(body, Some(i))? { @@ -1127,7 +1170,7 @@ impl [f32; 3]> Builder<'_, F> { } let normal = |u: &Unit<'_>| u.blend == Blend::Mode(BlendMode::Normal); let blends_out = level == Level::Pass && !(normal(u) && below.iter().filter(|b| b.visible).all(normal)); - let applies = rec.knockout == 1 && level != Level::Top && !blends_out && !rec.clipped && !matches!(u.body, Body::Merged(_)); + let applies = rec.knockout == 1 && level != Level::Top && !blends_out && !rec.clipped && !u.base && !matches!(u.body, Body::Merged(_)); if !applies { self.unknock(u.record); } @@ -1145,9 +1188,39 @@ impl [f32; 3]> Builder<'_, F> { } } + /// Count record `i` as a layer clipped to a fill that isn't drawn (once). + fn clip_out(&mut self, i: usize) { + if let Some(s) = self.clipped_out.get_mut(i) + && !*s + { + *s = true; + self.notes.clipped_out += 1; + self.clipped_out_log.push(i); + } + } + + /// Count record `i` as a clipped layer left unclipped (once). + fn unclip(&mut self, i: usize) { + if let Some(s) = self.unclipped.get_mut(i) + && !*s + { + *s = true; + self.notes.unclipped += 1; + self.unclip_log.push(i); + } + } + /// `items`' units, at `level`, with the layers in blend modes VectorCraft lacks, and in a group /// the layers with a shallow knockout, merged with those below. fn merged<'i>(&mut self, items: &'i [Item], keep_hidden: bool, level: Level) -> Result>, String> { + #[cfg(test)] + { + let (walked, most) = WALKS.get(); + if walked >= most { + return Err("the import walked more layer lists than the test allows".into()); + } + WALKS.set((walked + 1, most)); + } let mut out: Vec> = vec![]; for mut u in self.units(items, keep_hidden)? { if self.knocks(&u, &out, level)? { @@ -1156,7 +1229,7 @@ impl [f32; 3]> Builder<'_, F> { self.skip(u.record); continue; } - let saved = self.notes.counts(self.skip_log.len(), self.unknock_log.len()); + let saved = self.notes.counts(self.skip_log.len(), self.unknock_log.len(), self.blank_log.len()); match self.knock(&out, &u)? { Knocked::Merged(canvas) => { self.take_merge(&mut out, &u, canvas); @@ -1185,7 +1258,7 @@ impl [f32; 3]> Builder<'_, F> { continue; } // A merge that can't be made counts nothing its groups would have counted. - let saved = self.notes.counts(self.skip_log.len(), self.unknock_log.len()); + let saved = self.notes.counts(self.skip_log.len(), self.unknock_log.len(), self.blank_log.len()); let made = match out.as_mut_slice() { // Onto the previous merge alone: composited onto its canvas, in place. [prev] if prev.is_merge() => { @@ -1241,10 +1314,29 @@ impl [f32; 3]> Builder<'_, F> { self.notes.knockouts = self.notes.knockouts.saturating_sub(1); } } + // The groups found since: what their drawing counted is given back, so they are drawn again. + for g in self.blank_log.split_off(c.blank) { + if let Some(b) = self.blank.get_mut(g) { + *b = None; + } + if let Some(b) = self.laid_blank.get_mut(g) { + *b = false; + } + } + for i in self.unclip_log.split_off(c.unclipped) { + if let Some(s) = self.unclipped.get_mut(i) { + *s = false; + self.notes.unclipped = self.notes.unclipped.saturating_sub(1); + } + } + for i in self.clipped_out_log.split_off(c.clipped_out) { + if let Some(s) = self.clipped_out.get_mut(i) { + *s = false; + self.notes.clipped_out = self.notes.clipped_out.saturating_sub(1); + } + } self.notes.merged.truncate(c.merged); self.notes.unmerged = c.unmerged; - self.notes.unclipped = c.unclipped; - self.notes.clipped_out = c.clipped_out; } /// The visible units `below` with `top` composited over them; `None` when the canvas would need @@ -1289,6 +1381,7 @@ impl [f32; 3]> Builder<'_, F> { record: top.record, body: Body::Merged(canvas), clip: None, + base: false, }); } @@ -1341,18 +1434,41 @@ impl [f32; 3]> Builder<'_, F> { self.max_alloc.saturating_sub(self.held) } + /// Note pass-through group `g` as laying nothing onto nothing when `acc`, nothing before, still + /// is and no layer's pixels were read since `painted`: laying its layers onto nothing again + /// gives nothing, with the same notes. + fn laid_nothing(&mut self, g: usize, acc: &Option, painted: usize) { + if acc.is_none() + && self.painted.get() == painted + && let Some(b) = self.laid_blank.get_mut(g) + && !*b + { + *b = true; + self.blank_log.push(g); + } + } + /// Unit `u` composited onto `acc`. A pass-through group lays its layers onto `acc` one by one, /// so that they blend with what is below it; at less than full opacity or with a layer mask, the /// result is then faded back toward `acc` as it was by the opacity and the mask. Anything else /// is drawn alone first. `false` when the canvas would need more memory than allowed. fn composite(&mut self, acc: &mut Option, u: &Unit<'_>) -> Result { - if let (Body::Group(items), Blend::Pass) = (&u.body, u.blend) { + if let (Body::Group(g, items), Blend::Pass) = (&u.body, u.blend) { let mask = match u.mask_of { Some(i) if self.has_mask(Some(i))? => self.mask(i, false)?, _ => None, }; + // Onto nothing, a group that laid nothing onto nothing before lays nothing again. + let (fresh, painted) = (acc.is_none(), self.painted.get()); + if fresh && self.laid_blank.get(*g) == Some(&true) { + return Ok(true); + } if u.opacity >= 1.0 && mask.is_none() { - return self.lay(acc, items, Level::Pass); + let laid = self.lay(acc, items, Level::Pass)?; + if laid && fresh { + self.laid_nothing(*g, acc, painted); + } + return Ok(laid); } let bytes = acc.as_ref().map_or(0, |c| 16 * c.px.len() as u64); if bytes > self.room() { @@ -1366,6 +1482,9 @@ impl [f32; 3]> Builder<'_, F> { if !laid? { return Ok(false); } + if fresh { + self.laid_nothing(*g, acc, painted); + } if let Some(after) = acc.as_mut() { fade(after, below.as_ref(), u.opacity, mask.as_ref()); } @@ -1420,13 +1539,29 @@ impl [f32; 3]> Builder<'_, F> { } None => return Ok(Drawn::Nothing), }, - Body::Group(items) => { - let mut acc: Option = None; - if !self.lay(&mut acc, items, Level::Group)? { - return Ok(Drawn::TooLarge); + Body::Group(g, items) => { + let drawn = match self.blank.get(*g).cloned().flatten() { + Some(d) => d, + None => { + let painted = self.painted.get(); + let mut acc: Option = None; + if !self.lay(&mut acc, items, Level::Group)? { + return Ok(Drawn::TooLarge); + } + let d = acc.map_or(Drawn::Nothing, Drawn::Canvas); + if self.painted.get() == painted + && let Some(b) = self.blank.get_mut(*g) + { + *b = Some(d.clone()); + self.blank_log.push(*g); + } + d + } + }; + match drawn { + Drawn::Canvas(c) => c, + other => return Ok(other), } - let Some(acc) = acc else { return Ok(Drawn::Nothing) }; - acc } }; if let Some(i) = mask_of { @@ -1478,13 +1613,13 @@ impl [f32; 3]> Builder<'_, F> { Some((left, top, img)) => vec![Arc::new(self.image_node(&u.name, left, top, img)?)], None => return Ok(None), }, - Body::Group(items) => self.list(items, if u.blend == Blend::Pass { Level::Pass } else { Level::Group })?, + Body::Group(_, items) => self.list(items, if u.blend == Blend::Pass { Level::Pass } else { Level::Group })?, }; let mut layer = self.layer(&u.name); layer.visible = u.visible; layer.opacity = u.opacity.clamp(0.0, 1.0); (layer.blend, layer.isolate) = match u.blend { - Blend::Mode(m) => (m, matches!(u.body, Body::Group(_))), + Blend::Mode(m) => (m, matches!(u.body, Body::Group(..))), Blend::Pass | Blend::Other(_) => (BlendMode::Normal, false), }; if let Some(c) = layer.children_mut() { @@ -1609,8 +1744,9 @@ fn blend_onto( /// `knock` (of record `record`, in `blend`, with its `fill` and `opacity`), a layer with a shallow /// knockout, knocking out `acc`, on a canvas covering both, as [`blend_onto`] makes it. Where /// `knock` has pixels, its transparency is the share of `acc` cleared and replaced by its color at -/// `fill`; `opacity` then fades that back toward `acc`. `false`, with `acc` as it was, when the -/// canvases would take more than `max_alloc` bytes. +/// `fill`; `opacity` then fades that back toward `acc` (in Dissolve, each pixel is cleared fully +/// or not at all, at the share its transparency times `opacity` gives). `false`, with `acc` as it +/// was, when the canvases would take more than `max_alloc` bytes. #[allow(clippy::too_many_arguments)] fn knock_onto( acc: &mut Option, @@ -1630,7 +1766,7 @@ fn knock_onto( let (kw, x0, y0) = (knock.width.max(1), (knock.left - out.left) as usize, (knock.top - out.top) as usize); for (k, s) in knock.px.iter().enumerate() { let Some(p) = out.px.get_mut((y0 + k / kw) * out.width + x0 + k % kw) else { continue }; - let t = opacity * coverage(blend, s[3].clamp(0.0, 1.0), knock.left + (k % kw) as i64, knock.top + (k / kw) as i64, record); + let t = coverage(blend, s[3].clamp(0.0, 1.0) * opacity, knock.left + (k % kw) as i64, knock.top + (k / kw) as i64, record); let below = p[3].clamp(0.0, 1.0); let alpha = (1.0 - t) * below + t * fill; if alpha <= 0.0 { @@ -1721,7 +1857,15 @@ pub(super) fn import( skip_log: vec![], unknocked: vec![false; records.len()], unknock_log: vec![], + clipped_out: vec![false; records.len()], + clipped_out_log: vec![], + unclipped: vec![false; records.len()], + unclip_log: vec![], spent: Cell::new(0), + painted: Cell::new(0), + blank: vec![None; records.len()], + laid_blank: vec![false; records.len()], + blank_log: vec![], }; let layers = b.list(&items, Level::Top)?; let Builder { mut doc, notes, .. } = b; diff --git a/crates/engine/src/cmd/fileio/tests_psdlayers.rs b/crates/engine/src/cmd/fileio/tests_psdlayers.rs index 61db4b86fc..6c2f8b8ff2 100644 --- a/crates/engine/src/cmd/fileio/tests_psdlayers.rs +++ b/crates/engine/src/cmd/fileio/tests_psdlayers.rs @@ -774,6 +774,143 @@ fn a_knockout_merge_past_the_memory_allowed_leaves_the_layers_apart() { assert!(warnings.is_empty(), "{warnings:?}"); } +/// A Dissolve knockout at part opacity clears each pixel below it fully or not at all, at the share +/// its opacity gives, as a Dissolve layer merges. +#[test] +fn a_dissolve_knockout_at_part_opacity_clears_whole_pixels() { + let cut = knockout(L { blend: b"diss", opacity: 128, ..layer("Cut", [0, 0, 16, 16], [0, 0, 255, 255]) }, 1, 0); + let layers = [group_end(), layer("Lower", [0, 0, 16, 16], [255, 0, 0, 255]), cut, group("Group", b"norm")]; + let (s, r) = open(&file(16, 16, &layers, 8, false, false), json!({})); + let d = &s.doc().unwrap().doc; + let (_, px) = image_of(&s, &d.layers[0].children().unwrap()[0]); + assert!(px.pixels().all(|p| p.0 == [255, 0, 0, 255] || p.0[3] == 0), "{r}"); + let red = px.pixels().filter(|p| p.0 == [255, 0, 0, 255]).count(); + assert!((32..=224).contains(&red), "{red} of 256 pixels keep the lower layer: {r}"); +} + +/// The knockout of a layer that another is clipped to isn't applied, also when the clipped layer's +/// merge with it needs more memory than allowed and the two come in apart. +#[test] +fn a_clipping_base_knockout_isnt_applied_when_its_clip_doesnt_fit() { + let cms = |c: [f32; 4]| [c[0], c[1], c[2]]; + // The 4 × 4 clipped layer and its 2 × 2 base don't fit 320 bytes together for the clip; the + // base's knockout merge with the 2 × 2 layer below would. + let layers = [ + group_end(), + layer("Lower", [0, 0, 2, 2], [255, 0, 0, 255]), + knockout(layer("Base", [0, 0, 2, 2], [0, 0, 255, 255]), 1, 0), + L { clipped: true, ..layer("Clipped", [0, 0, 4, 4], [0, 255, 0, 255]) }, + group("Group", b"norm"), + ]; + let (doc, warnings) = psdlayers::import(&file(4, 4, &layers, 8, false, false), false, 320, cms).unwrap().unwrap(); + assert_eq!(doc.layers[0].children().unwrap().iter().map(|c| name(c)).collect::>(), ["Lower", "Base", "Clipped"], "{warnings:?}"); + assert_eq!( + warnings, + ["the knockout of 1 layer wasn't applied", "1 layer clipped to the layer below came in unclipped: the merge needs more memory than allowed"] + ); +} + +/// A knockout group that comes in as a layer counts each layer clipped in it once in the warnings, +/// though its layers are also drawn for the knockout: one clipped to a fill that isn't drawn, and +/// one that a merge past the memory allowed leaves unclipped. +#[test] +fn a_knockout_group_that_comes_in_as_a_layer_counts_its_clipped_layers_once() { + let cms = |c: [f32; 4]| [c[0], c[1], c[2]]; + let empty = L { bare: true, ..layer("Empty", [0, 0, 2, 2], [0, 0, 0, 0]) }; + // A fill whose shape is a vector mask, with a layer clipped to it: neither is drawn. + let mut soco = vec![0; 8]; + for (key, v) in [(b"Rd doub", 255.0f64), (b"Grn doub", 0.0), (b"Bl doub", 51.0)] { + soco.extend(key); + soco.extend(v.to_be_bytes()); + } + let mut outline = layer("Outline", [0, 0, 0, 0], [0; 4]); + outline.blocks.push((*b"SoCo", soco)); + outline.blocks.push((*b"vsms", vec![0; 8])); + let on_outline = L { clipped: true, ..layer("On outline", [0, 0, 2, 2], [1, 1, 1, 255]) }; + // Each 2 × 2 layer fits 128 bytes, and so do the two drawn together, but not the merge of the + // clipped one with the one below. + let base = layer("Base", [0, 0, 2, 2], [255, 0, 0, 255]); + let clipped = L { clipped: true, ..layer("Clipped", [0, 0, 2, 2], [0, 0, 255, 255]) }; + for (inner, said) in [ + (vec![outline, on_outline], "1 layer clipped to a fill layer that wasn't drawn was left out"), + (vec![base, clipped], "1 layer clipped to the layer below came in unclipped: the merge needs more memory than allowed"), + ] { + let mut layers = vec![layer("Outside", [0, 0, 4, 4], [0, 255, 0, 255]), group_end(), empty.clone(), group_end()]; + layers.extend(inner); + layers.extend([knockout(group("Cut", b"norm"), 1, 255), group("Group", b"norm")]); + let (doc, warnings) = psdlayers::import(&file(4, 4, &layers, 8, false, false), false, 128, cms).unwrap().unwrap(); + assert_eq!(doc.layers.iter().map(|l| name(l)).collect::>(), ["Outside", "Group"]); + assert!(warnings.iter().any(|w| w == said), "{said}: {warnings:?}"); + } +} + +/// A pass-through group that draws nothing still counts what its layers count in it, also when it +/// is drawn again inside a group that merges: a knockout above a Multiply layer isn't applied in a +/// pass-through group, though drawn alone it would be. +#[test] +fn a_pass_through_group_that_draws_nothing_still_counts_its_unapplied_knockouts() { + let cms = |c: [f32; 4]| [c[0], c[1], c[2]]; + let empty = |name: &'static str, blend: &'static [u8; 4]| L { bare: true, blend, ..layer(name, [0, 0, 2, 2], [0, 0, 0, 0]) }; + let layers = [ + layer("Lower", [0, 0, 4, 4], [255, 0, 0, 255]), + group_end(), + empty("Empty", b"norm"), + group_end(), + empty("Multiplied", b"mul "), + knockout(empty("Cut", b"norm"), 1, 255), + knockout(group("Through", b"pass"), 1, 255), + group("Dissolved", b"diss"), + ]; + let (doc, warnings) = psdlayers::import(&file(4, 4, &layers, 8, false, false), false, 1 << 20, cms).unwrap().unwrap(); + assert_eq!(doc.layers.iter().map(|l| name(l)).collect::>(), ["Lower + Dissolved"], "{warnings:?}"); + assert_eq!(warnings, ["the knockout of 1 layer wasn't applied".to_string(), psdlayers::MERGED_WARNING.replace("{modes}", "Dissolve")]); +} + +/// A group that draws nothing is drawn once, however deep the groups like it nest: here each holds +/// a layer that stores no pixels and, above it, the next group, with a knockout. The import walks +/// at most as many layer lists as the file has records, with isolated groups, with groups that +/// alternate with pass-through ones, with pass-through groups inside the outermost one (whose +/// knockouts aren't applied), and with a layer in a mode VectorCraft lacks in each (a merge of +/// nothing, a canvas without pixels). +#[test] +fn groups_that_draw_nothing_are_drawn_once_however_deep_they_nest() { + const LEVELS: usize = 48; + let cms = |c: [f32; 4]| [c[0], c[1], c[2]]; + let empty = |name: &'static str, blend: &'static [u8; 4]| L { bare: true, blend, ..layer(name, [0, 0, 2, 2], [0, 0, 0, 0]) }; + // Which levels are pass-through groups. + let none: fn(usize) -> bool = |_| false; + let odd: fn(usize) -> bool = |k| k % 2 == 1; + let all: fn(usize) -> bool = |k| k > 0; + for (case, pass, dissolve, warned) in [ + ("isolated", none, false, vec![]), + ("alternating", odd, false, vec![]), + ("pass-through", all, false, vec![format!("the knockout of {} layers wasn't applied", LEVELS - 1)]), + ("dissolved", none, true, vec![psdlayers::MERGED_WARNING.replace("{modes}", "Dissolve")]), + ] { + let mut layers = vec![layer("Outside", [0, 0, 4, 4], [0, 255, 0, 255])]; + for _ in 0..=LEVELS { + layers.push(group_end()); + if dissolve { + layers.push(empty("Dissolved", b"diss")); + } + layers.push(empty("Empty", b"norm")); + } + for k in (0..=LEVELS).rev() { + let g = group("Group", if pass(k) { b"pass" } else { b"norm" }); + layers.push(if k == 0 { g } else { knockout(g, 1, 255) }); + } + psdlayers::WALKS.set((0, layers.len())); + let imported = psdlayers::import(&file(4, 4, &layers, 8, false, false), false, 1 << 20, cms); + let (walked, _) = psdlayers::WALKS.replace((0, usize::MAX)); + let (doc, warnings) = imported.unwrap().unwrap(); + assert!(walked <= layers.len(), "{case}: {walked} walks for {} records", layers.len()); + assert_eq!(doc.layers.iter().map(|l| name(l)).collect::>(), ["Outside", "Group"], "{case}"); + let mut expected = warned; + expected.push(vectorcraft_doc::depth::merged_note()); + assert_eq!(warnings, expected, "{case}"); + } +} + #[test] fn what_doesnt_come_in_is_said() { let mut text = layer("Words", [0, 0, 2, 2], [0, 0, 0, 255]); From 9c364510e9249082a3cf1335e1ccc18cb8fd835a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 17 Nov 2026 19:32:03 -0800 Subject: [PATCH 3/7] PSD import: knockouts that change nothing below them leave the layers apart A shallow knockout at 0 opacity, or at 0 fill over layers that are all transparent, is no longer merged with the layers below it, so hidden layers below it come in with hiddenLayers. A hidden knockout at 0 opacity, or with nothing visible below it, comes in with hiddenLayers instead of being left out. The count of hidden layers left out no longer includes hidden layers that came in after a group was drawn for a knockout or a clip that wasn't made. docs/mcp.md describes the knockouts document.open applies. --- crates/engine/src/cmd/fileio/psdlayers.rs | 35 ++++++++-- .../engine/src/cmd/fileio/tests_psdlayers.rs | 68 +++++++++++++++++++ docs/mcp.md | 14 +++- docs/target-app-parity.md | 2 +- 4 files changed, 108 insertions(+), 11 deletions(-) diff --git a/crates/engine/src/cmd/fileio/psdlayers.rs b/crates/engine/src/cmd/fileio/psdlayers.rs index c2f0dd5ff2..4c3e05cb48 100644 --- a/crates/engine/src/cmd/fileio/psdlayers.rs +++ b/crates/engine/src/cmd/fileio/psdlayers.rs @@ -677,7 +677,8 @@ enum Level { enum Knocked { /// The merged canvas. Merged(Canvas), - /// Nothing below it draws, or it draws nothing: it comes in as a layer of its own. + /// Nothing below it draws, it draws nothing, or what is below it and the merge are both + /// transparent: it comes in as a layer of its own. Nothing, /// The canvases would need more memory than allowed. TooLarge, @@ -834,6 +835,9 @@ struct Builder<'a, F> { unclipped: Vec, /// Those records, in the order they were counted. unclip_log: Vec, + /// The records that came in as layers of the document (hidden ones too, with `hidden`): not left + /// out, though a drawing for a merge that wasn't made counted them. + kept: Vec, /// Bytes of layer and mask pixels decoded, and of canvases the merges made, so far. spent: Cell, /// The layers whose pixels were read so far (each time they were). @@ -1162,7 +1166,8 @@ impl [f32; 3]> Builder<'_, F> { /// applies? Only a layer or group in a group, neither clipped nor a base of clipped layers, does; /// in a pass-through group, only when it and the visible units below it are in the Normal mode, so /// that none of them blends with what is below the group. Any other knockout is counted as not - /// applied. + /// applied. One at 0 opacity, or with no visible unit below it, changes nothing below it: it needs + /// no merge, and a hidden one isn't left out for it. fn knocks(&mut self, u: &Unit<'_>, below: &[Unit<'_>], level: Level) -> Result { let rec = self.record(u.record)?; if rec.knockout == 0 { @@ -1171,10 +1176,12 @@ impl [f32; 3]> Builder<'_, F> { let normal = |u: &Unit<'_>| u.blend == Blend::Mode(BlendMode::Normal); let blends_out = level == Level::Pass && !(normal(u) && below.iter().filter(|b| b.visible).all(normal)); let applies = rec.knockout == 1 && level != Level::Top && !blends_out && !rec.clipped && !u.base && !matches!(u.body, Body::Merged(_)); + // Its fill opacity isn't checked: at 0 fill it still clears what is below it. + let changes = rec.opacity > 0.0 && below.iter().any(|b| b.visible); if !applies { self.unknock(u.record); } - Ok(applies) + Ok(applies && changes) } /// Count record `i` as a hidden layer left out (once). @@ -1240,7 +1247,8 @@ impl [f32; 3]> Builder<'_, F> { } continue; } - // Nothing below it, or nothing of its own, to knock out: it comes in as a layer. + // Nothing below it, or nothing of its own, to knock out: it comes in as a layer, + // with the hidden layers its drawing left out (see `kept`). Knocked::Nothing => {} Knocked::TooLarge => { self.restore(saved); @@ -1390,9 +1398,6 @@ impl [f32; 3]> Builder<'_, F> { /// backdrop) and `top` is drawn there at its fill opacity; its opacity then fades all of that /// back toward the units below as they were. Its layer mask shapes the cut. fn knock(&mut self, below: &[Unit<'_>], top: &Unit<'_>) -> Result { - if !below.iter().any(|u| u.visible) { - return Ok(Knocked::Nothing); - } let (fill, opacity) = { let rec = self.record(top.record)?; (rec.fill, rec.opacity) @@ -1422,9 +1427,19 @@ impl [f32; 3]> Builder<'_, F> { if acc.is_none() { return Ok(Knocked::Nothing); } + let clear = |c: &Canvas| c.px.iter().all(|p| p[3] <= 0.0); + let below_clear = acc.as_ref().is_some_and(clear); + // Nothing below to cut, and nothing of its own drawn at 0 fill: no canvas for the merge is needed. + if below_clear && fill <= 0.0 { + return Ok(Knocked::Nothing); + } if !knock_onto(&mut acc, &k, top.blend, fill, opacity, top.record, self.room(), &|n| self.spend(n))? { return Ok(Knocked::TooLarge); } + // Nothing below to cut, and nothing of its own drawn at its fill opacity. + if below_clear && acc.as_ref().is_none_or(clear) { + return Ok(Knocked::Nothing); + } Ok(acc.map_or(Knocked::Nothing, Knocked::Merged)) } @@ -1604,6 +1619,9 @@ impl [f32; 3]> Builder<'_, F> { /// The layer of the document unit `u` becomes; `None` for a layer that draws nothing (an /// adjustment layer, or one without pixels). fn node(&mut self, u: Unit<'_>) -> Result, String> { + if let Some(k) = self.kept.get_mut(u.record) { + *k = true; + } let children = match &u.body { Body::Layer(i) => match self.image(*i)? { Some((left, top, img)) => vec![Arc::new(self.image_node(&u.name, left, top, img)?)], @@ -1861,6 +1879,7 @@ pub(super) fn import( clipped_out_log: vec![], unclipped: vec![false; records.len()], unclip_log: vec![], + kept: vec![false; records.len()], spent: Cell::new(0), painted: Cell::new(0), blank: vec![None; records.len()], @@ -1868,6 +1887,8 @@ pub(super) fn import( blank_log: vec![], }; let layers = b.list(&items, Level::Top)?; + // A drawing for a knockout or a clip leaves hidden layers out; those that came in anyway aren't. + b.notes.hidden = b.skipped.iter().zip(&b.kept).filter(|&(s, k)| *s && !*k).count(); let Builder { mut doc, notes, .. } = b; if !layers.is_empty() { doc.layers = layers; diff --git a/crates/engine/src/cmd/fileio/tests_psdlayers.rs b/crates/engine/src/cmd/fileio/tests_psdlayers.rs index 6c2f8b8ff2..6324ffcce5 100644 --- a/crates/engine/src/cmd/fileio/tests_psdlayers.rs +++ b/crates/engine/src/cmd/fileio/tests_psdlayers.rs @@ -752,6 +752,74 @@ fn knockouts_that_arent_applied_are_said() { assert_eq!(r["warnings"], json!([]), "{r}"); } +/// A knockout that changes nothing below it leaves the layers apart, hidden ones included: one at 0 +/// opacity, and one at 0 fill over layers that are transparent. At 0 fill over painted layers, and +/// at a fill over transparent ones, it still merges. A knockout at 0 fill over a transparent layer +/// stays apart without a warning when their merge would need more memory than allowed. +#[test] +fn knockouts_that_change_nothing_keep_the_layers_apart() { + let cms = |c: [f32; 4]| [c[0], c[1], c[2]]; + let layers = [ + group_end(), + layer("Lower", [0, 0, 2, 2], [255, 0, 0, 0]), + knockout(layer("Cut", [2, 2, 4, 4], [0, 0, 255, 255]), 1, 0), + group("Group", b"norm"), + ]; + // The two 2 × 2 layers drawn together fit 256 bytes, but not the 4 × 4 canvas covering both. + let (doc, warnings) = psdlayers::import(&file(4, 4, &layers, 8, false, false), false, 256, cms).unwrap().unwrap(); + assert_eq!(doc.layers[0].children().unwrap().iter().map(|c| name(c)).collect::>(), ["Lower", "Cut"]); + assert!(warnings.is_empty(), "{warnings:?}"); + let lower = |alpha: u8| layer("Lower", [0, 0, 4, 4], [255, 0, 0, alpha]); + let hidden = L { hidden: true, ..layer("Hidden", [0, 0, 4, 4], [0, 255, 0, 255]) }; + let cut = |fill: u8, opacity: u8| knockout(L { opacity, ..layer("Cut", [0, 0, 2, 2], [0, 0, 255, 255]) }, 1, fill); + let inner = |s: &Session| s.doc().unwrap().doc.layers[0].children().map_or(vec![], |c| c.iter().map(|l| name(l).to_string()).collect::>()); + for (alpha, fill, opacity, apart) in [(255, 0, 0, true), (0, 0, 255, true), (255, 0, 255, false), (0, 128, 255, false)] { + let layers = [group_end(), lower(alpha), hidden.clone(), cut(fill, opacity), group("Group", b"norm")]; + let (s, r) = open(&file(4, 4, &layers, 8, false, false), json!({"hiddenLayers": true})); + let (names, warnings) = if apart { (vec!["Lower", "Hidden", "Cut"], json!([])) } else { (vec!["Lower + Cut"], json!([HIDDEN_1])) }; + assert_eq!((inner(&s), &r["warnings"]), (names.iter().map(|n| n.to_string()).collect(), &warnings), "{alpha} {fill} {opacity}: {r}"); + } + // A hidden knockout that needs no merge comes in with hidden layers: at 0 opacity, and with + // nothing below it in its group. + let hidden_cut = |opacity: u8| L { hidden: true, ..cut(0, opacity) }; + for (layers, names) in [ + (vec![group_end(), lower(255), hidden_cut(0), group("Group", b"norm")], vec!["Lower", "Cut"]), + (vec![group_end(), hidden_cut(255), group("Group", b"norm")], vec!["Cut"]), + ] { + let (s, r) = open(&file(4, 4, &layers, 8, false, false), json!({"hiddenLayers": true})); + assert_eq!((inner(&s), &r["warnings"]), (names.iter().map(|n| n.to_string()).collect(), &json!([])), "{r}"); + } +} + +/// With hidden layers asked for, the hidden layers of a group drawn for a knockout or a clip that +/// isn't made come in, and aren't counted as left out: a knockout group over a layer that stores no +/// pixels, and a clipped group whose merge needs more memory than allowed. +#[test] +fn hidden_layers_of_a_group_drawn_for_a_merge_not_made_come_in() { + let cms = |c: [f32; 4]| [c[0], c[1], c[2]]; + let children = |l: &Node| l.children().map_or(vec![], |c| c.iter().map(|l| (name(l).to_string(), l.visible)).collect::>()); + let shown_and_hidden = + || [group_end(), layer("Shown", [0, 0, 2, 2], [0, 0, 255, 255]), L { hidden: true, ..layer("Hidden", [2, 2, 4, 4], [0, 255, 0, 255]) }]; + let both = vec![("Shown".to_string(), true), ("Hidden".to_string(), false)]; + // A knockout group over a layer that stores no pixels. + let mut layers = vec![group_end(), L { bare: true, ..layer("Lower", [0, 0, 2, 2], [0; 4]) }]; + layers.extend(shown_and_hidden()); + layers.extend([knockout(group("Cut", b"norm"), 1, 255), group("Group", b"norm")]); + let (s, r) = open(&file(4, 4, &layers, 8, false, false), json!({"hiddenLayers": true})); + let d = &s.doc().unwrap().doc; + let cut = &d.layers[0].children().unwrap()[0]; + assert_eq!((name(cut), children(cut), &r["warnings"]), ("Cut", both.clone(), &json!([])), "{r}"); + // A clipped group whose merge with its base doesn't fit 320 bytes. + let mut layers = vec![layer("Base", [0, 0, 2, 2], [255, 0, 0, 255])]; + layers.extend(shown_and_hidden()); + layers[2].rect = [0, 0, 4, 4]; + layers.push(L { clipped: true, ..group("Clipped", b"norm") }); + let (doc, warnings) = psdlayers::import(&file(4, 4, &layers, 8, false, false), true, 320, cms).unwrap().unwrap(); + let clipped = &doc.layers[1]; + assert_eq!((name(clipped), children(clipped)), ("Clipped", both), "{warnings:?}"); + assert_eq!(warnings, ["1 layer clipped to the layer below came in unclipped: the merge needs more memory than allowed"]); +} + /// A knockout's merge whose canvases would need more memory than allowed leaves the layers apart, /// with a warning. #[test] diff --git a/docs/mcp.md b/docs/mcp.md index 4c1b661f42..06fc2fe242 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -9312,8 +9312,8 @@ clipping mask into one layer and discards hidden layers that require merging; th imports one image, and Import Hidden Layers brings hidden layers in. `document.open` does the same, with `layers: false` for the single image and `hiddenLayers: true` for the hidden layers. The page also merges adjustment layers, layers with a knockout option, and layers that hold transparent pixels in some blend modes with -the layers below them. `document.open` applies neither adjustments nor knockouts, and it merges a layer by its blend -mode alone, as the rules below say. +the layers below them. `document.open` does not apply adjustments, applies only shallow knockouts inside groups, and +otherwise merges a layer by its blend mode alone, as the rules below say. The rest are VectorCraft's own rules: @@ -9333,6 +9333,12 @@ The rest are VectorCraft's own rules: mask. - A Dissolve layer's partly transparent pixels are merged as opaque or transparent, opaque at the share of its pixels their transparency gives, in a fixed pattern that differs from layer to layer. +- A layer or group inside a group with a Knockout value of 1 (shallow) is merged with the visible layers below it + in that group: where it has pixels, shaped by its layer mask, they are cleared to transparency, it is drawn there + at its fill opacity, and its opacity fades the result back toward them. In a Pass Through group this applies only + when it and the visible layers below it are in Normal mode. A knockout at 0 opacity, one that draws nothing, one + with nothing visible below it, and one at 0 fill over layers that are all transparent are not merged for the + knockout; one in a mode VectorCraft lacks is still merged by its mode. - A merged layer is named after the layers it holds, joined with ` + `. - A layer name the file stores only as a one-byte Pascal string, without its Unicode name, is read as Mac Roman. - A layer's opacity is multiplied by its fill opacity, in every blend mode. A layer that others are clipped to keeps @@ -9344,7 +9350,9 @@ The rest are VectorCraft's own rules: (RGB, CMYK, Grayscale, Lab or HSB) over the canvas. One whose shape is a vector mask isn't drawn, and the layers clipped to it are left out. - Adjustment layers, fill layers that store no pixels and whose fill isn't read, layer effects that are switched on, - knockouts and vector masks are not applied; the `warnings` count them. + the other knockouts (top-level layers, clipped layers, layers that others are clipped to, Pass Through groups with + layers in other modes, other Knockout values, and merges that need more memory than allowed) and vector masks are + not applied; the `warnings` count them. - Layer channels may be raw, PackBits or zipped (with or without prediction) at 8, 16 and 32 bits. - A file without layers opens as its merged image. A file whose layers can't be read opens as its merged image with a warning, and when the merged image can't be read either, the file is refused with an error. diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index e4bbf9dbbb..36b53e6f6f 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -2040,7 +2040,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur EPS export and EPS Save make each file's PNG thumbnail from its TIFF preview, averaging the preview's pixels down to the thumbnail's size on white, instead of drawing the document a second time; a file without a preview still draws its thumbnail (`crates/engine/src/cmd/fileio/tests_eps.rs`). - Targa files (`.tga`, `.vda`, `.icb`, `.vst`) open, place linked or embedded, relink and update at their pixel size: color-mapped, true-color and black-and-white, raw or run-length encoded, in any of the four row orders. The image descriptor's attribute bits and the extension area's Attributes Type decide the transparency: a file with no attribute bits, or with attribute data the Attributes Type calls undefined, opens opaque; with one bit, a 16-bit true-color pixel takes that bit as its alpha, and a color-mapped pixel takes the bit of the 16-bit color map entry it indexes; premultiplied colors are divided by their alpha. Open and Place recognize a Targa file by its extension, and `clipboard.importImage` and Glass's Load Texture… by its header; a Targa file copied in a file manager pastes as a bitmap (`crates/engine/src/cmd/fileio/tests_tiffbmp.rs`, `crates/engine/src/tests/links.rs`). - BMP files named `.dib` or `.rle` (run-length encoded bitmaps included), Photoshop files named `.pdd` and PNG files named `.pns` open and place, linked or embedded, as BMP, PSD and PNG files, in any letter case: the Open and Place dialogs list them with their format, the Links panel names their format, and Edit Original opens them. Exports write only `.bmp`, `.psd` and `.png` (`crates/engine/src/cmd/fileio/tests_tiffbmp.rs`, `crates/engine/src/cmd/fileio/tests_psdread.rs`, `crates/engine/src/tests/links.rs`). - - File › Open converts a PSD or PSB file's layers to objects, keeping the layer structure, merging clipped layers into one layer, merging a layer that uses a Knockout option with the layers below it, and leaving out hidden layers that would need a merge, as [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for the option that converts layers to objects; hidden layers come in with `hiddenLayers`, as Import Hidden Layers brings them in, and `layers: false` opens the merged image. VectorCraft's own rules: each layer becomes a layer holding its pixels with its name, visibility, opacity and blend mode, groups become layers of sublayers, layers in Dissolve, Linear Burn, Linear Dodge, Vivid Light, Linear Light, Pin Light, Hard Mix, Subtract, Divide, Darker Color and Lighter Color are merged with the layers below, layer masks become opacity masks, fill opacity multiplies the opacity (in a layer that others are clipped to, it fades only that layer's own pixels), a Knockout value of 1 is read as a shallow knockout and applied to a layer or group inside a group, which is merged with the layers below it in that group (its pixels clear them to transparency, it is drawn there at its fill opacity, and its opacity fades the result; in a pass-through group only when it and the layers below it are in the Normal mode), and adjustment layers, layer effects, vector masks and the other knockouts (top-level layers, clipped layers, layers that others are clipped to, pass-through groups with layers in other modes, other Knockout values) aren't applied and are counted in the warnings. 8-, 16- and 32-bit files, PSB, raw, PackBits and zipped channels (`crates/engine/src/cmd/fileio/tests_psdlayers.rs`). + - File › Open converts a PSD or PSB file's layers to objects, keeping the layer structure, merging clipped layers into one layer, merging a layer that uses a Knockout option with the layers below it, and leaving out hidden layers that would need a merge, as [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for the option that converts layers to objects; hidden layers come in with `hiddenLayers`, as Import Hidden Layers brings them in, and `layers: false` opens the merged image. VectorCraft's own rules: each layer becomes a layer holding its pixels with its name, visibility, opacity and blend mode, groups become layers of sublayers, layers in Dissolve, Linear Burn, Linear Dodge, Vivid Light, Linear Light, Pin Light, Hard Mix, Subtract, Divide, Darker Color and Lighter Color are merged with the layers below, layer masks become opacity masks, fill opacity multiplies the opacity (in a layer that others are clipped to, it fades only that layer's own pixels), a Knockout value of 1 is read as a shallow knockout and applied to a layer or group inside a group, which is merged with the layers below it in that group (its pixels clear them to transparency, it is drawn there at its fill opacity, and its opacity fades the result; in a pass-through group only when it and the layers below it are in the Normal mode; a knockout at 0 opacity, or at 0 fill over layers that are all transparent, isn't merged for the knockout), and adjustment layers, layer effects, vector masks and the other knockouts (top-level layers, clipped layers, layers that others are clipped to, pass-through groups with layers in other modes, other Knockout values) aren't applied and are counted in the warnings. 8-, 16- and 32-bit files, PSB, raw, PackBits and zipped channels (`crates/engine/src/cmd/fileio/tests_psdlayers.rs`). - File › Open of a PSD or PSB file with two or more layers, and File › Place of one without Link, show PSD Import Options: Convert Layers to Objects with Import Hidden Layers, or Flatten Layers to a Single Image, the options [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for a file that is opened or embedded. A file placed with Link shows its flat image and isn't asked about. `file.place` takes `layers` and `hiddenLayers`, and `document.psdInfo` counts a file's layers without opening it. VectorCraft's own rules: placed layers come in as one group named after the file, a layer as an image and a layer group as a group, at the place the merged image would take and clipped to the canvas when layers reach past it; the dialog asks from two layers up and starts from the choices made last. Layer comps aren't read (`crates/engine/src/cmd/fileio/tests_psdlayers.rs`, `crates/ui-egui/src/dialogs/psd_import.rs`). - A PSD or PSB file's clipping path, the saved path that image resource 2999 names in the [PSD file format specification](https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/), clips its merged image on File › Place and on File › Open of the merged image, as [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for a file imported as one flattened image. The image comes in a clip group whose clipping path has no paint and uses the even-odd rule, and the path can be edited or released. `links.update` and `links.relink` give the clip group the clipping path the new file names, or leave a plain group when it names none, and files that name no clipping path come in as before (`crates/engine/src/cmd/fileio/tests_psdread.rs`). - Import Slices: File › Open of a PSD or PSB file with slices, and File › Place of one without Link, bring its slices in as user slices, as [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for a file that is opened or embedded; PSD Import Options offers Import Slices for such a file. `document.open` and `file.place` take `importSlices`, and `document.psdInfo` counts the slices. The slices are read from image resource 1050 in version 6 and in the descriptor of versions 7 and 8, as the [Photoshop File Formats Specification](https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/) describes it. VectorCraft's own rules: Import Slices applies to Convert Layers to Objects and to Flatten Layers to a Single Image alike, and makes the dialog ask for a file with fewer than two layers; placed slices move and scale with the art in the same undo step; each slice keeps its name, type, link, target, message, alternative text, cell text and background color; slices the file generated automatically, and slices wholly outside the canvas, are left out; slices that can't be read are left out with a warning while the art comes in; with Show Import Options off, a PSD file is placed with the PSD Import Options chosen last, and on, each PSD file of the place cursor asks for them (`crates/engine/src/cmd/fileio/tests_psdslices.rs`, `crates/ui-egui/src/dialogs/psd_import.rs`). From 157469f6af49137de4e0f06afce9ad0ec02e1dfc Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 17 Nov 2026 19:56:29 -0800 Subject: [PATCH 4/7] i18n: the Greek PSD knockout warnings use the catalog's knockout term MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit main now checks that every Greek row naming a knockout says διάτρηση. --- crates/ui-egui/src/i18n/el.tsv | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/i18n/el.tsv b/crates/ui-egui/src/i18n/el.tsv index acd85f7742..43abb58be7 100644 --- a/crates/ui-egui/src/i18n/el.tsv +++ b/crates/ui-egui/src/i18n/el.tsv @@ -5717,8 +5717,8 @@ variable-list-end {0}, {1} {0} και {1} @msg the layer effects of {n} layers weren't drawn τα εφέ επιπέδου {n} επιπέδων δεν σχεδιάστηκαν @msg the vector masks of 1 layer weren't applied οι διανυσματικές μάσκες 1 επιπέδου δεν εφαρμόστηκαν @msg the vector masks of {n} layers weren't applied οι διανυσματικές μάσκες {n} επιπέδων δεν εφαρμόστηκαν -@msg the knockout of 1 layer wasn't applied η εκκένωση 1 επιπέδου δεν εφαρμόστηκε -@msg the knockout of {n} layers wasn't applied η εκκένωση {n} επιπέδων δεν εφαρμόστηκε +@msg the knockout of 1 layer wasn't applied η διάτρηση 1 επιπέδου δεν εφαρμόστηκε +@msg the knockout of {n} layers wasn't applied η διάτρηση {n} επιπέδων δεν εφαρμόστηκε @msg 1 layer was left out (hidden layers and the layers clipped to them) 1 επίπεδο παραλείφθηκε (κρυφά επίπεδα και τα επίπεδα που αποκόπτονται σε αυτά) @msg {n} layers were left out (hidden layers and the layers clipped to them) {n} επίπεδα παραλείφθηκαν (κρυφά επίπεδα και τα επίπεδα που αποκόπτονται σε αυτά) @msg 1 layer clipped to a fill layer that wasn't drawn was left out 1 επίπεδο που αποκόπτεται σε επίπεδο γεμίσματος που δεν σχεδιάστηκε παραλείφθηκε From a6d4729bf246c0645a1a809abb249ed45d395a3f Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 18 Nov 2026 06:49:40 -0800 Subject: [PATCH 5/7] PSD import: knockouts that cut no paint stay apart, and failed merges keep room to read the layers - A knockout at 0 fill whose pixels cover none of the painted pixels below it is no longer merged with them. - A hidden knockout is left out only when its merge would be made or is too large, so one whose layer mask hides all of it comes in with hiddenLayers. What drawing it counted is forgotten when it is left out. - The pixels and canvases of a merge that isn't made are counted against a separate limit of the same size, so the layers can still be read apart afterward. - docs: the memory-limit fallback for knockouts is listed with the knockouts that aren't applied. --- crates/engine/src/cmd/fileio/psdlayers.rs | 125 ++++++++++++++---- .../engine/src/cmd/fileio/tests_psdlayers.rs | 74 +++++++++-- docs/mcp.md | 9 +- docs/target-app-parity.md | 2 +- 4 files changed, 163 insertions(+), 47 deletions(-) diff --git a/crates/engine/src/cmd/fileio/psdlayers.rs b/crates/engine/src/cmd/fileio/psdlayers.rs index 4c3e05cb48..598f75ff04 100644 --- a/crates/engine/src/cmd/fileio/psdlayers.rs +++ b/crates/engine/src/cmd/fileio/psdlayers.rs @@ -31,19 +31,20 @@ //! when it and the visible layers below it are in the Normal mode. When a merge's canvases would need more //! memory than allowed, the layer comes in alone: in the Normal mode, unclipped, or without its //! knockout. Hidden layers are left out unless `hidden` asks for them. A hidden layer that would need -//! a merge (one in a mode VectorCraft lacks, one with a knockout in a group, one that layers are -//! clipped to, one clipped to another, or one below a merged layer) is always left out, with the -//! layers clipped to it. Adjustment layers, fill layers that store no pixels and whose fill isn't read -//! or whose shape is a vector mask, layer effects that are switched on, vector masks, and the -//! knockouts of top-level layers, of clipped layers, of layers that others are clipped to and of -//! layers in a pass-through group with a layer in another mode, and other Knockout values, aren't -//! applied; the warnings count them and the hidden layers left out. +//! a merge (one in a mode VectorCraft lacks, one with a knockout in a group that changes the layers +//! below it, one that layers are clipped to, one clipped to another, or one below a merged layer) is +//! always left out, with the layers clipped to it. Adjustment layers, fill layers that store no +//! pixels and whose fill isn't read or whose shape is a vector mask, layer effects that are switched +//! on, vector masks, and the knockouts of top-level layers, of clipped layers, of layers that others +//! are clipped to and of layers in a pass-through group with a layer in another mode, and other +//! Knockout values, aren't applied; the warnings count them and the hidden layers left out. //! //! The file is untrusted: every count, length and size is checked before anything is allocated, //! groups nest at most [`MAX_DEPTH`] deep (layers deeper than a document may nest merge into the //! group around them, with a warning), each layer's pixels, like the canvases one step of a //! merge holds together, must fit the memory allowed, and all the pixels decoded and the canvases -//! the merges make must fit [`TOTAL_DECODED`] times that. +//! the merges make must fit [`TOTAL_DECODED`] times that. What the merges that can't be made, or +//! that a hidden layer would need, decoded and made is counted apart, against the same limit. use std::cell::Cell; use std::sync::Arc; @@ -677,8 +678,8 @@ enum Level { enum Knocked { /// The merged canvas. Merged(Canvas), - /// Nothing below it draws, it draws nothing, or what is below it and the merge are both - /// transparent: it comes in as a layer of its own. + /// Nothing below it draws, it draws nothing, it is at 0 fill and cuts no painted pixel below + /// it, or what is below it and the merge are both transparent: it comes in as a layer of its own. Nothing, /// The canvases would need more memory than allowed. TooLarge, @@ -717,6 +718,8 @@ struct Notes { /// The counts a merge that can't be made gives back. #[derive(Clone, Copy)] struct Counts { + /// The bytes spent so far (see [`Builder::spent`]). + spent: u64, skipped: usize, /// The knockouts counted as not applied. unknocked: usize, @@ -789,8 +792,9 @@ impl Notes { out } - fn counts(&self, skipped: usize, unknocked: usize, blank: usize) -> Counts { + fn counts(&self, spent: u64, skipped: usize, unknocked: usize, blank: usize) -> Counts { Counts { + spent, skipped, unknocked, merged: self.merged.len(), @@ -819,6 +823,8 @@ struct Builder<'a, F> { notes: Notes, /// The records [`Notes`] counted. noted: Vec, + /// Those records, in the order they were counted. + noted_log: Vec, /// The records counted as hidden layers left out. skipped: Vec, /// Those records, in the order they were counted. @@ -840,6 +846,9 @@ struct Builder<'a, F> { kept: Vec, /// Bytes of layer and mask pixels decoded, and of canvases the merges made, so far. spent: Cell, + /// Those bytes that merges not made, and merges a hidden layer would need, took: given back to + /// `spent` and counted here instead, against the same limit. + wasted: Cell, /// The layers whose pixels were read so far (each time they were). painted: Cell, /// What each group gave when drawn alone without reading any layer's pixels (nothing, or a @@ -873,6 +882,20 @@ impl [f32; 3]> Builder<'_, F> { Ok(()) } + /// Move the bytes spent since `spent` was the count to [`Self::wasted`], which has an allowance + /// of its own as large: what a merge that isn't made decoded doesn't take the room for reading + /// the layers apart. + fn refund(&self, spent: u64) -> Result<(), String> { + let since = self.spent.get().saturating_sub(spent); + self.spent.set(self.spent.get().min(spent)); + let total = self.wasted.get().saturating_add(since); + self.wasted.set(total); + if total > self.max_alloc.saturating_mul(TOTAL_DECODED) { + return Err("the PSD file's layers take more memory to read than allowed".into()); + } + Ok(()) + } + /// Record `i`'s pixels as straight RGBA, at their place on the canvas; `None` when it has none. fn image(&self, i: usize) -> Result, String> { let rec = self.record(i)?; @@ -1006,9 +1029,11 @@ impl [f32; 3]> Builder<'_, F> { if let Some(u) = out.get_mut(b) { u.base = true; } + let spent = self.spent.get(); if !unclipped && self.clip(&mut out, b, &body, i, &name, blend, opacity)? { continue; } + self.refund(spent)?; // The merge doesn't fit: this layer and the next ones clipped to the same // base come in as layers of their own. unclipped = true; @@ -1131,7 +1156,10 @@ impl [f32; 3]> Builder<'_, F> { // Once, though a merge may draw it again. match self.noted.get_mut(i) { Some(true) => return Ok(()), - Some(n) => *n = true, + Some(n) => { + *n = true; + self.noted_log.push(i); + } None => {} } let (content, empty, effects, vector_mask) = { @@ -1151,6 +1179,23 @@ impl [f32; 3]> Builder<'_, F> { Ok(()) } + /// What [`Self::note`] has counted so far: the length of its log, and the counts. + fn noted_so_far(&self) -> (usize, [usize; 5]) { + let n = &self.notes; + (self.noted_log.len(), [n.types, n.adjustments, n.fills, n.effects, n.vector_masks]) + } + + /// Forget what [`Self::note`] counted since `so_far`, for layers that are left out. + fn unnote(&mut self, (logged, counts): (usize, [usize; 5])) { + for i in self.noted_log.split_off(logged) { + if let Some(n) = self.noted.get_mut(i) { + *n = false; + } + } + let n = &mut self.notes; + [n.types, n.adjustments, n.fills, n.effects, n.vector_masks] = counts; + } + /// Count record `i` as a layer whose knockout isn't applied (once). fn unknock(&mut self, i: usize) { if let Some(s) = self.unknocked.get_mut(i) @@ -1231,13 +1276,17 @@ impl [f32; 3]> Builder<'_, F> { let mut out: Vec> = vec![]; for mut u in self.units(items, keep_hidden)? { if self.knocks(&u, &out, level)? { - // A hidden layer that would need the merge is left out. - if !u.visible { - self.skip(u.record); - continue; - } - let saved = self.notes.counts(self.skip_log.len(), self.unknock_log.len(), self.blank_log.len()); + let saved = self.saved(); + let noted = self.noted_so_far(); match self.knock(&out, &u)? { + // A hidden layer that would need the merge is left out, and nothing its drawing + // counted is counted. + Knocked::Merged(_) | Knocked::TooLarge if !u.visible => { + self.restore(saved)?; + self.unnote(noted); + self.skip(u.record); + continue; + } Knocked::Merged(canvas) => { self.take_merge(&mut out, &u, canvas); if let Blend::Other(other) = u.blend @@ -1251,7 +1300,7 @@ impl [f32; 3]> Builder<'_, F> { // with the hidden layers its drawing left out (see `kept`). Knocked::Nothing => {} Knocked::TooLarge => { - self.restore(saved); + self.restore(saved)?; self.unknock(u.record); } } @@ -1266,7 +1315,7 @@ impl [f32; 3]> Builder<'_, F> { continue; } // A merge that can't be made counts nothing its groups would have counted. - let saved = self.notes.counts(self.skip_log.len(), self.unknock_log.len(), self.blank_log.len()); + let saved = self.saved(); let made = match out.as_mut_slice() { // Onto the previous merge alone: composited onto its canvas, in place. [prev] if prev.is_merge() => { @@ -1299,7 +1348,7 @@ impl [f32; 3]> Builder<'_, F> { self.notes.merged.push(other.label()); } } else { - self.restore(saved); + self.restore(saved)?; self.notes.unmerged += 1; u.blend = Blend::Mode(BlendMode::Normal); out.push(u); @@ -1308,8 +1357,15 @@ impl [f32; 3]> Builder<'_, F> { Ok(out) } - /// The counts as they were before a merge that couldn't be made. - fn restore(&mut self, c: Counts) { + /// The counts as they are, for [`Self::restore`]. + fn saved(&self) -> Counts { + self.notes.counts(self.spent.get(), self.skip_log.len(), self.unknock_log.len(), self.blank_log.len()) + } + + /// The counts as they were before a merge that couldn't be made, the bytes it spent counted as + /// wasted (see [`Self::refund`]). + fn restore(&mut self, c: Counts) -> Result<(), String> { + self.refund(c.spent)?; for i in self.skip_log.split_off(c.skipped) { if let Some(s) = self.skipped.get_mut(i) { *s = false; @@ -1345,6 +1401,7 @@ impl [f32; 3]> Builder<'_, F> { } self.notes.merged.truncate(c.merged); self.notes.unmerged = c.unmerged; + Ok(()) } /// The visible units `below` with `top` composited over them; `None` when the canvas would need @@ -1424,15 +1481,14 @@ impl [f32; 3]> Builder<'_, F> { return Ok(Knocked::TooLarge); } // Nothing below draws. - if acc.is_none() { + let Some(below) = acc.as_ref() else { return Ok(Knocked::Nothing) }; + // At 0 fill it draws nothing of its own: where it cuts no painted pixel below, no canvas for + // the merge is needed. + if fill <= 0.0 && !cuts_paint(below, &k, top.blend, opacity, top.record) { return Ok(Knocked::Nothing); } let clear = |c: &Canvas| c.px.iter().all(|p| p[3] <= 0.0); - let below_clear = acc.as_ref().is_some_and(clear); - // Nothing below to cut, and nothing of its own drawn at 0 fill: no canvas for the merge is needed. - if below_clear && fill <= 0.0 { - return Ok(Knocked::Nothing); - } + let below_clear = clear(below); if !knock_onto(&mut acc, &k, top.blend, fill, opacity, top.record, self.room(), &|n| self.spend(n))? { return Ok(Knocked::TooLarge); } @@ -1797,6 +1853,15 @@ fn knock_onto( Ok(true) } +/// Does `knock`, as [`knock_onto`] takes it, clear any of a pixel of `below` that has paint? +fn cuts_paint(below: &Canvas, knock: &Canvas, blend: Blend, opacity: f32, record: usize) -> bool { + let (kw, opacity) = (knock.width.max(1), opacity.clamp(0.0, 1.0)); + knock.px.iter().enumerate().any(|(k, s)| { + let (x, y) = (knock.left + (k % kw) as i64, knock.top + (k / kw) as i64); + below.get(x, y).is_some_and(|b| b[3] > 0.0) && coverage(blend, s[3].clamp(0.0, 1.0) * opacity, x, y, record) > 0.0 + }) +} + /// `acc` grown, when it doesn't already, to a canvas covering itself and `src` (a new one counted /// with `spend`). `false`, with `acc` as it was, when the canvases held, `acc` and `src` and a /// larger canvas when one is needed, would take more than `max_alloc` bytes. @@ -1871,6 +1936,7 @@ pub(super) fn import( layers: 0, notes: Notes::default(), noted: vec![false; records.len()], + noted_log: vec![], skipped: vec![false; records.len()], skip_log: vec![], unknocked: vec![false; records.len()], @@ -1881,6 +1947,7 @@ pub(super) fn import( unclip_log: vec![], kept: vec![false; records.len()], spent: Cell::new(0), + wasted: Cell::new(0), painted: Cell::new(0), blank: vec![None; records.len()], laid_blank: vec![false; records.len()], diff --git a/crates/engine/src/cmd/fileio/tests_psdlayers.rs b/crates/engine/src/cmd/fileio/tests_psdlayers.rs index 6324ffcce5..c5be312153 100644 --- a/crates/engine/src/cmd/fileio/tests_psdlayers.rs +++ b/crates/engine/src/cmd/fileio/tests_psdlayers.rs @@ -753,7 +753,8 @@ fn knockouts_that_arent_applied_are_said() { } /// A knockout that changes nothing below it leaves the layers apart, hidden ones included: one at 0 -/// opacity, and one at 0 fill over layers that are transparent. At 0 fill over painted layers, and +/// opacity, one at 0 fill over layers that are transparent or beside their painted pixels, and one +/// whose layer mask hides all of it. At 0 fill over painted layers, and /// at a fill over transparent ones, it still merges. A knockout at 0 fill over a transparent layer /// stays apart without a warning when their merge would need more memory than allowed. #[test] @@ -779,16 +780,40 @@ fn knockouts_that_change_nothing_keep_the_layers_apart() { let (names, warnings) = if apart { (vec!["Lower", "Hidden", "Cut"], json!([])) } else { (vec!["Lower + Cut"], json!([HIDDEN_1])) }; assert_eq!((inner(&s), &r["warnings"]), (names.iter().map(|n| n.to_string()).collect(), &warnings), "{alpha} {fill} {opacity}: {r}"); } - // A hidden knockout that needs no merge comes in with hidden layers: at 0 opacity, and with - // nothing below it in its group. + // A hidden knockout that needs no merge comes in with hidden layers: at 0 opacity, with nothing + // below it in its group, at 0 fill beside the painted pixels below it, and with a layer mask that + // hides all of it. let hidden_cut = |opacity: u8| L { hidden: true, ..cut(0, opacity) }; + let beside = L { hidden: true, ..knockout(layer("Cut", [0, 0, 2, 2], [0, 0, 255, 255]), 1, 0) }; + let masked = L { hidden: true, mask: Some(([0, 0, 2, 2], 0, 0, vec![0; 4])), ..cut(255, 255) }; for (layers, names) in [ (vec![group_end(), lower(255), hidden_cut(0), group("Group", b"norm")], vec!["Lower", "Cut"]), (vec![group_end(), hidden_cut(255), group("Group", b"norm")], vec!["Cut"]), + (vec![group_end(), layer("Lower", [2, 2, 4, 4], [255, 0, 0, 255]), beside, group("Group", b"norm")], vec!["Lower", "Cut"]), + (vec![group_end(), lower(255), masked.clone(), group("Group", b"norm")], vec!["Lower", "Cut"]), ] { let (s, r) = open(&file(4, 4, &layers, 8, false, false), json!({"hiddenLayers": true})); assert_eq!((inner(&s), &r["warnings"]), (names.iter().map(|n| n.to_string()).collect(), &json!([])), "{r}"); } + // With the mask switched off it would need the merge, and is left out. + let unmasked = L { mask: Some(([0, 0, 2, 2], 0, 2, vec![0; 4])), ..masked }; + let (s, r) = open(&file(4, 4, &[group_end(), lower(255), unmasked, group("Group", b"norm")], 8, false, false), json!({"hiddenLayers": true})); + assert_eq!((inner(&s), &r["warnings"]), (vec!["Lower".to_string()], &json!([HIDDEN_1])), "{r}"); + // A hidden knockout group that would need the merge is counted once, as a hidden layer left out: + // its type layer and its own hidden layer aren't counted. + let mut words = layer("Words", [0, 0, 2, 2], [0, 0, 0, 255]); + words.blocks.push((*b"TySh", vec![0; 4])); + let layers = [ + group_end(), + lower(255), + group_end(), + words, + L { hidden: true, ..layer("Inner", [0, 0, 2, 2], [0, 0, 255, 255]) }, + L { hidden: true, ..knockout(group("Cut", b"norm"), 1, 255) }, + group("Group", b"norm"), + ]; + let (s, r) = open(&file(4, 4, &layers, 8, false, false), json!({"hiddenLayers": true})); + assert_eq!((inner(&s), &r["warnings"]), (vec!["Lower".to_string()], &json!([HIDDEN_1])), "{r}"); } /// With hidden layers asked for, the hidden layers of a group drawn for a knockout or a clip that @@ -821,25 +846,48 @@ fn hidden_layers_of_a_group_drawn_for_a_merge_not_made_come_in() { } /// A knockout's merge whose canvases would need more memory than allowed leaves the layers apart, -/// with a warning. +/// with a warning. One at 0 fill beside the painted pixels below it cuts nothing, and stays apart +/// whatever the memory. #[test] fn a_knockout_merge_past_the_memory_allowed_leaves_the_layers_apart() { let cms = |c: [f32; 4]| [c[0], c[1], c[2]]; - let layers = [ - group_end(), - layer("Lower", [0, 0, 2, 2], [255, 0, 0, 255]), - knockout(layer("Cut", [2, 2, 4, 4], [0, 0, 255, 255]), 1, 0), - group("Group", b"norm"), - ]; - let f = file(4, 4, &layers, 8, false, false); + let stack = |cut: [i32; 4]| { + let layers = [ + group_end(), + layer("Lower", [0, 0, 2, 2], [255, 0, 0, 255]), + knockout(layer("Cut", cut, [0, 0, 255, 255]), 1, 0), + group("Group", b"norm"), + ]; + file(4, 4, &layers, 8, false, false) + }; + let names = |doc: &vectorcraft_doc::Document| doc.layers[0].children().unwrap().iter().map(|c| name(c).to_string()).collect::>(); + let f = stack([1, 1, 3, 3]); // Each 2 × 2 layer fits 128 bytes (16 a pixel), but not the canvas of the layer below with the // knockout's canvas held beside it. let (doc, warnings) = psdlayers::import(&f, false, 128, cms).unwrap().unwrap(); - assert_eq!(doc.layers[0].children().unwrap().iter().map(|c| name(c)).collect::>(), ["Lower", "Cut"]); + assert_eq!(names(&doc), ["Lower", "Cut"]); assert_eq!(warnings, ["the knockout of 1 layer wasn't applied"]); let (doc, warnings) = psdlayers::import(&f, false, 1 << 20, cms).unwrap().unwrap(); - assert_eq!(doc.layers[0].children().unwrap().iter().map(|c| name(c)).collect::>(), ["Lower + Cut"]); + assert_eq!(names(&doc), ["Lower + Cut"]); assert!(warnings.is_empty(), "{warnings:?}"); + let (doc, warnings) = psdlayers::import(&stack([2, 2, 4, 4]), false, 1 << 20, cms).unwrap().unwrap(); + assert_eq!(names(&doc), ["Lower", "Cut"]); + assert!(warnings.is_empty(), "{warnings:?}"); +} + +/// A knockout merge that can't be made leaves the import the whole allowance for reading the layers +/// apart: here the merge decodes 22 layers over one another before the layer beside them makes its +/// canvas too large, which used up nearly all of eight times 64 KiB. +#[test] +fn a_knockout_merge_not_made_leaves_room_to_read_the_layers() { + let cms = |c: [f32; 4]| [c[0], c[1], c[2]]; + let mut layers = vec![group_end()]; + layers.extend((0..22).map(|_| layer("Lower", [0, 0, 32, 32], [255, 0, 0, 255]))); + layers.push(layer("Beside", [0, 64, 32, 96], [0, 255, 0, 255])); + layers.extend([knockout(layer("Cut", [0, 0, 32, 32], [0, 0, 255, 255]), 1, 0), group("Group", b"norm")]); + let (doc, warnings) = psdlayers::import(&file(96, 32, &layers, 8, false, false), false, 64 << 10, cms).unwrap().unwrap(); + assert_eq!(doc.layers[0].children().unwrap().len(), 24, "{warnings:?}"); + assert_eq!(warnings, ["the knockout of 1 layer wasn't applied"]); } /// A Dissolve knockout at part opacity clears each pixel below it fully or not at all, at the share diff --git a/docs/mcp.md b/docs/mcp.md index 048fc5f557..e641490062 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -9906,7 +9906,7 @@ The rest are VectorCraft's own rules: in that group: where it has pixels, shaped by its layer mask, they are cleared to transparency, it is drawn there at its fill opacity, and its opacity fades the result back toward them. In a Pass Through group this applies only when it and the visible layers below it are in Normal mode. A knockout at 0 opacity, one that draws nothing, one - with nothing visible below it, and one at 0 fill over layers that are all transparent are not merged for the + with nothing visible below it, and one at 0 fill that covers none of their painted pixels are not merged for the knockout; one in a mode VectorCraft lacks is still merged by its mode. - A merged layer is named after the layers it holds, joined with ` + `. - A layer name the file stores only as a one-byte Pascal string, without its Unicode name, is read as Mac Roman. @@ -9928,9 +9928,10 @@ The rest are VectorCraft's own rules: Each layer's pixels must fit the memory allowed for one image, and the pixels of all the layers and masks, with the canvases the merges make, must together fit eight times that; a file past either limit opens as its merged image, -with a warning. The canvases one step of a merge holds at once must also fit the memory allowed for one image; when -they don't, the layer that needed the merge comes in alone, with a warning: in the Normal mode, or as a layer of its -own instead of clipped. +with a warning. The pixels and canvases of merges that aren't made count against a second limit of the same size. The +canvases one step of a merge holds at once must also fit the memory allowed for one image; when they don't, the +layer that needed the merge comes in alone, with a warning: in the Normal mode, as a layer of its own instead of +clipped, or without its knockout. ```json {"name":"run_command","arguments":{"command":"document.open","params":{"path":"/tmp/poster.psd","hiddenLayers":true}}} diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index c7e1723f05..14bf39612f 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -2141,7 +2141,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur EPS export and EPS Save make each file's PNG thumbnail from its TIFF preview, averaging the preview's pixels down to the thumbnail's size on white, instead of drawing the document a second time; a file without a preview still draws its thumbnail (`crates/engine/src/cmd/fileio/tests_eps.rs`). - Targa files (`.tga`, `.vda`, `.icb`, `.vst`) open, place linked or embedded, relink and update at their pixel size: color-mapped, true-color and black-and-white, raw or run-length encoded, in any of the four row orders. The image descriptor's attribute bits and the extension area's Attributes Type decide the transparency: a file with no attribute bits, or with attribute data the Attributes Type calls undefined, opens opaque; with one bit, a 16-bit true-color pixel takes that bit as its alpha, and a color-mapped pixel takes the bit of the 16-bit color map entry it indexes; premultiplied colors are divided by their alpha. Open and Place recognize a Targa file by its extension, and `clipboard.importImage` and Glass's Load Texture… by its header; a Targa file copied in a file manager pastes as a bitmap (`crates/engine/src/cmd/fileio/tests_tiffbmp.rs`, `crates/engine/src/tests/links.rs`). - BMP files named `.dib` or `.rle` (run-length encoded bitmaps included), Photoshop files named `.pdd` and PNG files named `.pns` open and place, linked or embedded, as BMP, PSD and PNG files, in any letter case: the Open and Place dialogs list them with their format, the Links panel names their format, and Edit Original opens them. Exports write only `.bmp`, `.psd` and `.png` (`crates/engine/src/cmd/fileio/tests_tiffbmp.rs`, `crates/engine/src/cmd/fileio/tests_psdread.rs`, `crates/engine/src/tests/links.rs`). - - File › Open converts a PSD or PSB file's layers to objects, keeping the layer structure, merging clipped layers into one layer, merging a layer that uses a Knockout option with the layers below it, and leaving out hidden layers that would need a merge, as [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for the option that converts layers to objects; hidden layers come in with `hiddenLayers`, as Import Hidden Layers brings them in, and `layers: false` opens the merged image. VectorCraft's own rules: each layer becomes a layer holding its pixels with its name, visibility, opacity and blend mode, groups become layers of sublayers, layers in Dissolve, Linear Burn, Linear Dodge, Vivid Light, Linear Light, Pin Light, Hard Mix, Subtract, Divide, Darker Color and Lighter Color are merged with the layers below, layer masks become opacity masks, fill opacity multiplies the opacity (in a layer that others are clipped to, it fades only that layer's own pixels), a Knockout value of 1 is read as a shallow knockout and applied to a layer or group inside a group, which is merged with the layers below it in that group (its pixels clear them to transparency, it is drawn there at its fill opacity, and its opacity fades the result; in a pass-through group only when it and the layers below it are in the Normal mode; a knockout at 0 opacity, or at 0 fill over layers that are all transparent, isn't merged for the knockout), and adjustment layers, layer effects, vector masks and the other knockouts (top-level layers, clipped layers, layers that others are clipped to, pass-through groups with layers in other modes, other Knockout values) aren't applied and are counted in the warnings. 8-, 16- and 32-bit files, PSB, raw, PackBits and zipped channels (`crates/engine/src/cmd/fileio/tests_psdlayers.rs`). + - File › Open converts a PSD or PSB file's layers to objects, keeping the layer structure, merging clipped layers into one layer, merging a layer that uses a Knockout option with the layers below it, and leaving out hidden layers that would need a merge, as [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for the option that converts layers to objects; hidden layers come in with `hiddenLayers`, as Import Hidden Layers brings them in, and `layers: false` opens the merged image. VectorCraft's own rules: each layer becomes a layer holding its pixels with its name, visibility, opacity and blend mode, groups become layers of sublayers, layers in Dissolve, Linear Burn, Linear Dodge, Vivid Light, Linear Light, Pin Light, Hard Mix, Subtract, Divide, Darker Color and Lighter Color are merged with the layers below, layer masks become opacity masks, fill opacity multiplies the opacity (in a layer that others are clipped to, it fades only that layer's own pixels), a Knockout value of 1 is read as a shallow knockout and applied to a layer or group inside a group, which is merged with the layers below it in that group (its pixels clear them to transparency, it is drawn there at its fill opacity, and its opacity fades the result; in a pass-through group only when it and the layers below it are in the Normal mode; a knockout at 0 opacity, or at 0 fill where it covers none of their painted pixels, isn't merged for the knockout), and adjustment layers, layer effects, vector masks and the other knockouts (top-level layers, clipped layers, layers that others are clipped to, pass-through groups with layers in other modes, other Knockout values, and merges that need more memory than allowed) aren't applied and are counted in the warnings. 8-, 16- and 32-bit files, PSB, raw, PackBits and zipped channels (`crates/engine/src/cmd/fileio/tests_psdlayers.rs`). - File › Open of a PSD or PSB file with two or more layers, and File › Place of one without Link, show PSD Import Options: Convert Layers to Objects with Import Hidden Layers, or Flatten Layers to a Single Image, the options [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for a file that is opened or embedded. A file placed with Link shows its flat image and isn't asked about. `file.place` takes `layers` and `hiddenLayers`, and `document.psdInfo` counts a file's layers without opening it. VectorCraft's own rules: placed layers come in as one group named after the file, a layer as an image and a layer group as a group, at the place the merged image would take and clipped to the canvas when layers reach past it; the dialog asks from two layers up and starts from the choices made last. Layer comps aren't read (`crates/engine/src/cmd/fileio/tests_psdlayers.rs`, `crates/ui-egui/src/dialogs/psd_import.rs`). - A PSD or PSB file's clipping path, the saved path that image resource 2999 names in the [PSD file format specification](https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/), clips its merged image on File › Place and on File › Open of the merged image, as [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for a file imported as one flattened image. The image comes in a clip group whose clipping path has no paint and uses the even-odd rule, and the path can be edited or released. `links.update` and `links.relink` give the clip group the clipping path the new file names, or leave a plain group when it names none, and files that name no clipping path come in as before (`crates/engine/src/cmd/fileio/tests_psdread.rs`). - Import Slices: File › Open of a PSD or PSB file with slices, and File › Place of one without Link, bring its slices in as user slices, as [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for a file that is opened or embedded; PSD Import Options offers Import Slices for such a file. `document.open` and `file.place` take `importSlices`, and `document.psdInfo` counts the slices. The slices are read from image resource 1050 in version 6 and in the descriptor of versions 7 and 8, as the [Photoshop File Formats Specification](https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/) describes it. VectorCraft's own rules: Import Slices applies to Convert Layers to Objects and to Flatten Layers to a Single Image alike, and makes the dialog ask for a file with fewer than two layers; placed slices move and scale with the art in the same undo step; each slice keeps its name, type, link, target, message, alternative text, cell text and background color; slices the file generated automatically, and slices wholly outside the canvas, are left out; slices that can't be read are left out with a warning while the art comes in; with Show Import Options off, a PSD file is placed with the PSD Import Options chosen last, and on, each PSD file of the place cursor asks for them (`crates/engine/src/cmd/fileio/tests_psdslices.rs`, `crates/ui-egui/src/dialogs/psd_import.rs`). From bc1c1131c0ea43cf22da20d05697532d1db2c5ef Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 18 Nov 2026 08:01:32 -0800 Subject: [PATCH 6/7] PSD import: merges being tried get their own allowance, and knockouts that cut nothing give it back - A knockout, clip or blend-mode merge being tried may use the whole import allowance beyond what was read before it. A merge that is made must still fit the allowance with everything read before it. - A knockout that turns out to cut nothing moves what its drawing read out of the allowance, as a merge that isn't made does. A hidden one also forgets what its drawing counted, so a later merge that leaves it out reports only the hidden layer. - A hidden layer in a mode VectorCraft lacks is left out before any knockout drawing, as before the knockout merge. - A knockout at 0 fill whose painted pixels lie outside the bounds of every layer below it is kept apart before their canvas is made. --- crates/engine/src/cmd/fileio/psdlayers.rs | 99 ++++++++++++++++--- .../engine/src/cmd/fileio/tests_psdlayers.rs | 54 +++++++++- docs/mcp.md | 4 +- docs/target-app-parity.md | 2 +- 4 files changed, 139 insertions(+), 20 deletions(-) diff --git a/crates/engine/src/cmd/fileio/psdlayers.rs b/crates/engine/src/cmd/fileio/psdlayers.rs index 598f75ff04..3779b72745 100644 --- a/crates/engine/src/cmd/fileio/psdlayers.rs +++ b/crates/engine/src/cmd/fileio/psdlayers.rs @@ -43,8 +43,9 @@ //! groups nest at most [`MAX_DEPTH`] deep (layers deeper than a document may nest merge into the //! group around them, with a warning), each layer's pixels, like the canvases one step of a //! merge holds together, must fit the memory allowed, and all the pixels decoded and the canvases -//! the merges make must fit [`TOTAL_DECODED`] times that. What the merges that can't be made, or -//! that a hidden layer would need, decoded and made is counted apart, against the same limit. +//! the merges make must fit [`TOTAL_DECODED`] times that. A merge being tried may take that much +//! past what was read before it. What the merges that aren't made, or that a hidden layer would +//! need, decoded and made is counted apart, against the same limit. use std::cell::Cell; use std::sync::Arc; @@ -849,6 +850,9 @@ struct Builder<'a, F> { /// Those bytes that merges not made, and merges a hidden layer would need, took: given back to /// `spent` and counted here instead, against the same limit. wasted: Cell, + /// `spent` when the innermost merge being tried began: until it is made or given up, it may + /// spend the whole allowance past that (see [`Self::spend`]). + trial: Cell, /// The layers whose pixels were read so far (each time they were). painted: Cell, /// What each group gave when drawn alone without reading any layer's pixels (nothing, or a @@ -872,16 +876,30 @@ impl [f32; 3]> Builder<'_, F> { } /// Count `bytes` of decoded pixels, or of a canvas a merge makes, against the whole import's - /// allowance, [`TOTAL_DECODED`] times the memory allowed for one image. + /// allowance, [`TOTAL_DECODED`] times the memory allowed for one image. While a merge is tried + /// (see [`Self::try_from_here`]), only what it spent counts against the allowance. fn spend(&self, bytes: u64) -> Result<(), String> { let total = self.spent.get().saturating_add(bytes); self.spent.set(total); - if total > self.max_alloc.saturating_mul(TOTAL_DECODED) { + if total.saturating_sub(self.trial.get()) > self.max_alloc.saturating_mul(TOTAL_DECODED) { return Err("the PSD file's layers take more memory to read than allowed".into()); } Ok(()) } + /// Start trying a merge that may not be made: what it spends counts against the whole + /// allowance, however much was spent before it. Gives what [`Self::tried`] takes back. + fn try_from_here(&self) -> u64 { + self.trial.replace(self.spent.get()) + } + + /// End the merge tried since [`Self::try_from_here`] gave `outer`. A merge that is `made` keeps + /// what it spent, which must then fit the allowance with what was spent before it. + fn tried(&self, outer: u64, made: bool) -> Result<(), String> { + self.trial.set(outer); + if made { self.spend(0) } else { Ok(()) } + } + /// Move the bytes spent since `spent` was the count to [`Self::wasted`], which has an allowance /// of its own as large: what a merge that isn't made decoded doesn't take the room for reading /// the layers apart. @@ -1030,7 +1048,10 @@ impl [f32; 3]> Builder<'_, F> { u.base = true; } let spent = self.spent.get(); - if !unclipped && self.clip(&mut out, b, &body, i, &name, blend, opacity)? { + let outer = self.try_from_here(); + let made = !unclipped && self.clip(&mut out, b, &body, i, &name, blend, opacity)?; + self.tried(outer, made)?; + if made { continue; } self.refund(spent)?; @@ -1275,10 +1296,19 @@ impl [f32; 3]> Builder<'_, F> { } let mut out: Vec> = vec![]; for mut u in self.units(items, keep_hidden)? { + // A hidden layer in a mode VectorCraft lacks would need the merge for its mode: it is left + // out without being drawn. + if !u.visible && matches!(u.blend, Blend::Other(_)) { + self.skip(u.record); + continue; + } if self.knocks(&u, &out, level)? { let saved = self.saved(); let noted = self.noted_so_far(); - match self.knock(&out, &u)? { + let outer = self.try_from_here(); + let knocked = self.knock(&out, &u)?; + self.tried(outer, u.visible && matches!(knocked, Knocked::Merged(_)))?; + match knocked { // A hidden layer that would need the merge is left out, and nothing its drawing // counted is counted. Knocked::Merged(_) | Knocked::TooLarge if !u.visible => { @@ -1297,8 +1327,14 @@ impl [f32; 3]> Builder<'_, F> { continue; } // Nothing below it, or nothing of its own, to knock out: it comes in as a layer, - // with the hidden layers its drawing left out (see `kept`). - Knocked::Nothing => {} + // with the hidden layers its drawing left out (see `kept`). What its drawing + // read is read again when it comes in. A hidden one may still be left out by a + // later merge: what its drawing counted is counted when it comes in instead. + Knocked::Nothing if !u.visible => { + self.restore(saved)?; + self.unnote(noted); + } + Knocked::Nothing => self.refund(saved.spent)?, Knocked::TooLarge => { self.restore(saved)?; self.unknock(u.record); @@ -1309,13 +1345,9 @@ impl [f32; 3]> Builder<'_, F> { out.push(u); continue; }; - // A hidden layer that would need the merge is left out. - if !u.visible { - self.skip(u.record); - continue; - } // A merge that can't be made counts nothing its groups would have counted. let saved = self.saved(); + let outer = self.try_from_here(); let made = match out.as_mut_slice() { // Onto the previous merge alone: composited onto its canvas, in place. [prev] if prev.is_merge() => { @@ -1343,6 +1375,7 @@ impl [f32; 3]> Builder<'_, F> { None => false, }, }; + self.tried(outer, made)?; if made { if !self.notes.merged.contains(&other.label()) { self.notes.merged.push(other.label()); @@ -1465,6 +1498,10 @@ impl [f32; 3]> Builder<'_, F> { Drawn::Canvas(c) if c.px.iter().all(|p| p[3] <= 0.0) => return Ok(Knocked::Nothing), Drawn::Canvas(c) => c, }; + // At 0 fill, beside every layer below it, it cuts nothing: no canvas for them is needed. + if fill <= 0.0 && self.beside(below, &k)? { + return Ok(Knocked::Nothing); + } // The layer's canvas is held while the units below are drawn. let bytes = 16 * k.px.len() as u64; self.held = self.held.saturating_add(bytes); @@ -1499,6 +1536,41 @@ impl [f32; 3]> Builder<'_, F> { Ok(acc.map_or(Knocked::Nothing, Knocked::Merged)) } + /// Do the pixels `knock` paints lie outside the bounds of every visible unit `below`? Only the + /// bounds of layers and of merged canvases are known without drawing them: with a group below, + /// `false`. + fn beside(&self, below: &[Unit<'_>], knock: &Canvas) -> Result { + let width = knock.width.max(1); + let mut painted: Option<[i64; 4]> = None; + for (k, _) in knock.px.iter().enumerate().filter(|(_, p)| p[3] > 0.0) { + let (x, y) = (knock.left + (k % width) as i64, knock.top + (k / width) as i64); + painted = Some(painted.map_or([x, y, x + 1, y + 1], |[l, t, r, b]| [l.min(x), t.min(y), r.max(x + 1), b.max(y + 1)])); + } + let Some([left, top, right, bottom]) = painted else { return Ok(true) }; + for u in below.iter().filter(|u| u.visible) { + let [l, t, r, b] = match &u.body { + Body::Merged(c) => [c.left, c.top, c.left + c.width as i64, c.top + c.height as i64], + Body::Layer(i) => { + let rec = self.record(*i)?; + match rec.content { + Content::Adjustment => continue, + Content::Solid(Some(_)) if rec.color_over_canvas() => [0, 0, i64::from(self.h.width), i64::from(self.h.height)], + _ => { + let (w, h) = rec.bounds.size(); + let (l, t) = (i64::from(rec.bounds.left), i64::from(rec.bounds.top)); + [l, t, l + w as i64, t + h as i64] + } + } + } + Body::Group(..) => return Ok(false), + }; + if l < right && left < r && t < bottom && top < b { + return Ok(false); + } + } + Ok(true) + } + /// The bytes a merge step may still take: the memory allowed, less the copies of what is below /// the pass-through groups being merged. fn room(&self) -> u64 { @@ -1948,6 +2020,7 @@ pub(super) fn import( kept: vec![false; records.len()], spent: Cell::new(0), wasted: Cell::new(0), + trial: Cell::new(0), painted: Cell::new(0), blank: vec![None; records.len()], laid_blank: vec![false; records.len()], diff --git a/crates/engine/src/cmd/fileio/tests_psdlayers.rs b/crates/engine/src/cmd/fileio/tests_psdlayers.rs index c5be312153..d197658c20 100644 --- a/crates/engine/src/cmd/fileio/tests_psdlayers.rs +++ b/crates/engine/src/cmd/fileio/tests_psdlayers.rs @@ -807,13 +807,26 @@ fn knockouts_that_change_nothing_keep_the_layers_apart() { group_end(), lower(255), group_end(), - words, + words.clone(), L { hidden: true, ..layer("Inner", [0, 0, 2, 2], [0, 0, 255, 255]) }, L { hidden: true, ..knockout(group("Cut", b"norm"), 1, 255) }, group("Group", b"norm"), ]; let (s, r) = open(&file(4, 4, &layers, 8, false, false), json!({"hiddenLayers": true})); assert_eq!((inner(&s), &r["warnings"]), (vec!["Lower".to_string()], &json!([HIDDEN_1])), "{r}"); + // One whose layer mask hides all of it needs no merge, but a knockout above it merges the layers + // and leaves it out: it is counted once, without its type layer. + let layers = [ + group_end(), + lower(255), + group_end(), + words, + L { hidden: true, mask: Some(([0, 0, 2, 2], 0, 0, vec![0; 4])), ..knockout(group("Masked", b"norm"), 1, 255) }, + cut(0, 255), + group("Group", b"norm"), + ]; + let (s, r) = open(&file(4, 4, &layers, 8, false, false), json!({"hiddenLayers": true})); + assert_eq!((inner(&s), &r["warnings"]), (vec!["Lower + Cut".to_string()], &json!([HIDDEN_1])), "{r}"); } /// With hidden layers asked for, the hidden layers of a group drawn for a knockout or a clip that @@ -870,14 +883,18 @@ fn a_knockout_merge_past_the_memory_allowed_leaves_the_layers_apart() { let (doc, warnings) = psdlayers::import(&f, false, 1 << 20, cms).unwrap().unwrap(); assert_eq!(names(&doc), ["Lower + Cut"]); assert!(warnings.is_empty(), "{warnings:?}"); - let (doc, warnings) = psdlayers::import(&stack([2, 2, 4, 4]), false, 1 << 20, cms).unwrap().unwrap(); - assert_eq!(names(&doc), ["Lower", "Cut"]); - assert!(warnings.is_empty(), "{warnings:?}"); + for max_alloc in [128, 1 << 20] { + let (doc, warnings) = psdlayers::import(&stack([2, 2, 4, 4]), false, max_alloc, cms).unwrap().unwrap(); + assert_eq!(names(&doc), ["Lower", "Cut"], "{max_alloc}"); + assert!(warnings.is_empty(), "{max_alloc}: {warnings:?}"); + } } /// A knockout merge that can't be made leaves the import the whole allowance for reading the layers /// apart: here the merge decodes 22 layers over one another before the layer beside them makes its -/// canvas too large, which used up nearly all of eight times 64 KiB. +/// canvas too large, which used up nearly all of eight times 64 KiB. The merge has that allowance +/// also after five layers below the group were read, and a knockout at 0 fill over 22 transparent +/// layers, which cuts no paint after they are drawn, leaves the same room. #[test] fn a_knockout_merge_not_made_leaves_room_to_read_the_layers() { let cms = |c: [f32; 4]| [c[0], c[1], c[2]]; @@ -888,6 +905,33 @@ fn a_knockout_merge_not_made_leaves_room_to_read_the_layers() { let (doc, warnings) = psdlayers::import(&file(96, 32, &layers, 8, false, false), false, 64 << 10, cms).unwrap().unwrap(); assert_eq!(doc.layers[0].children().unwrap().len(), 24, "{warnings:?}"); assert_eq!(warnings, ["the knockout of 1 layer wasn't applied"]); + let mut after_five = vec![layer("Outside", [0, 0, 32, 32], [0, 0, 0, 255]); 5]; + after_five.extend(layers); + let (doc, warnings) = psdlayers::import(&file(96, 32, &after_five, 8, false, false), false, 64 << 10, cms).unwrap().unwrap(); + assert_eq!((doc.layers.len(), doc.layers[5].children().unwrap().len()), (6, 24), "{warnings:?}"); + assert_eq!(warnings, ["the knockout of 1 layer wasn't applied"]); + let mut layers = vec![group_end()]; + layers.extend((0..22).map(|_| layer("Lower", [0, 0, 32, 32], [255, 0, 0, 0]))); + layers.extend([knockout(layer("Cut", [0, 0, 32, 32], [0, 0, 255, 255]), 1, 0), group("Group", b"norm")]); + let (doc, warnings) = psdlayers::import(&file(32, 32, &layers, 8, false, false), false, 64 << 10, cms).unwrap().unwrap(); + assert_eq!(doc.layers[0].children().unwrap().len(), 23, "{warnings:?}"); + assert!(warnings.is_empty(), "{warnings:?}"); +} + +/// A hidden knockout in a mode VectorCraft lacks is left out without being drawn, hidden layers +/// asked for: here its pixels are larger than the memory allowed for one image. +#[test] +fn a_hidden_knockout_in_a_mode_vectorcraft_lacks_is_left_out_unread() { + let cms = |c: [f32; 4]| [c[0], c[1], c[2]]; + let layers = [ + group_end(), + layer("Lower", [0, 0, 2, 2], [255, 0, 0, 255]), + L { hidden: true, blend: b"diss", ..knockout(layer("Cut", [0, 0, 8, 8], [0, 0, 255, 255]), 1, 255) }, + group("Group", b"norm"), + ]; + let (doc, warnings) = psdlayers::import(&file(8, 8, &layers, 8, false, false), true, 128, cms).unwrap().unwrap(); + assert_eq!(doc.layers[0].children().unwrap().iter().map(|c| name(c)).collect::>(), ["Lower"]); + assert_eq!(warnings, [HIDDEN_1]); } /// A Dissolve knockout at part opacity clears each pixel below it fully or not at all, at the share diff --git a/docs/mcp.md b/docs/mcp.md index e641490062..a71a4d6943 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -9928,7 +9928,9 @@ The rest are VectorCraft's own rules: Each layer's pixels must fit the memory allowed for one image, and the pixels of all the layers and masks, with the canvases the merges make, must together fit eight times that; a file past either limit opens as its merged image, -with a warning. The pixels and canvases of merges that aren't made count against a second limit of the same size. The +with a warning. A merge being tried may use the whole of that limit beyond what was read before it. The pixels and +canvases of merges that aren't made, including knockouts that turn out to cut nothing, then count against a second +limit of the same size instead. The canvases one step of a merge holds at once must also fit the memory allowed for one image; when they don't, the layer that needed the merge comes in alone, with a warning: in the Normal mode, as a layer of its own instead of clipped, or without its knockout. diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 14bf39612f..ca57638462 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -2141,7 +2141,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur EPS export and EPS Save make each file's PNG thumbnail from its TIFF preview, averaging the preview's pixels down to the thumbnail's size on white, instead of drawing the document a second time; a file without a preview still draws its thumbnail (`crates/engine/src/cmd/fileio/tests_eps.rs`). - Targa files (`.tga`, `.vda`, `.icb`, `.vst`) open, place linked or embedded, relink and update at their pixel size: color-mapped, true-color and black-and-white, raw or run-length encoded, in any of the four row orders. The image descriptor's attribute bits and the extension area's Attributes Type decide the transparency: a file with no attribute bits, or with attribute data the Attributes Type calls undefined, opens opaque; with one bit, a 16-bit true-color pixel takes that bit as its alpha, and a color-mapped pixel takes the bit of the 16-bit color map entry it indexes; premultiplied colors are divided by their alpha. Open and Place recognize a Targa file by its extension, and `clipboard.importImage` and Glass's Load Texture… by its header; a Targa file copied in a file manager pastes as a bitmap (`crates/engine/src/cmd/fileio/tests_tiffbmp.rs`, `crates/engine/src/tests/links.rs`). - BMP files named `.dib` or `.rle` (run-length encoded bitmaps included), Photoshop files named `.pdd` and PNG files named `.pns` open and place, linked or embedded, as BMP, PSD and PNG files, in any letter case: the Open and Place dialogs list them with their format, the Links panel names their format, and Edit Original opens them. Exports write only `.bmp`, `.psd` and `.png` (`crates/engine/src/cmd/fileio/tests_tiffbmp.rs`, `crates/engine/src/cmd/fileio/tests_psdread.rs`, `crates/engine/src/tests/links.rs`). - - File › Open converts a PSD or PSB file's layers to objects, keeping the layer structure, merging clipped layers into one layer, merging a layer that uses a Knockout option with the layers below it, and leaving out hidden layers that would need a merge, as [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for the option that converts layers to objects; hidden layers come in with `hiddenLayers`, as Import Hidden Layers brings them in, and `layers: false` opens the merged image. VectorCraft's own rules: each layer becomes a layer holding its pixels with its name, visibility, opacity and blend mode, groups become layers of sublayers, layers in Dissolve, Linear Burn, Linear Dodge, Vivid Light, Linear Light, Pin Light, Hard Mix, Subtract, Divide, Darker Color and Lighter Color are merged with the layers below, layer masks become opacity masks, fill opacity multiplies the opacity (in a layer that others are clipped to, it fades only that layer's own pixels), a Knockout value of 1 is read as a shallow knockout and applied to a layer or group inside a group, which is merged with the layers below it in that group (its pixels clear them to transparency, it is drawn there at its fill opacity, and its opacity fades the result; in a pass-through group only when it and the layers below it are in the Normal mode; a knockout at 0 opacity, or at 0 fill where it covers none of their painted pixels, isn't merged for the knockout), and adjustment layers, layer effects, vector masks and the other knockouts (top-level layers, clipped layers, layers that others are clipped to, pass-through groups with layers in other modes, other Knockout values, and merges that need more memory than allowed) aren't applied and are counted in the warnings. 8-, 16- and 32-bit files, PSB, raw, PackBits and zipped channels (`crates/engine/src/cmd/fileio/tests_psdlayers.rs`). + - File › Open converts a PSD or PSB file's layers to objects, keeping the layer structure, merging clipped layers into one layer, merging a layer that uses a Knockout option with the layers below it, and leaving out hidden layers that would need a merge, as [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for the option that converts layers to objects; hidden layers come in with `hiddenLayers`, as Import Hidden Layers brings them in, and `layers: false` opens the merged image. VectorCraft's own rules: each layer becomes a layer holding its pixels with its name, visibility, opacity and blend mode, groups become layers of sublayers, layers in Dissolve, Linear Burn, Linear Dodge, Vivid Light, Linear Light, Pin Light, Hard Mix, Subtract, Divide, Darker Color and Lighter Color are merged with the layers below, layer masks become opacity masks, fill opacity multiplies the opacity (in a layer that others are clipped to, it fades only that layer's own pixels), a Knockout value of 1 is read as a shallow knockout and applied to a layer or group inside a group, which is merged with the visible layers below it in that group (its pixels clear them to transparency, it is drawn there at its fill opacity, and its opacity fades the result; in a pass-through group only when it and the visible layers below it are in the Normal mode; a knockout at 0 opacity, or at 0 fill where it covers none of their painted pixels, isn't merged for the knockout), and adjustment layers, layer effects, vector masks and the other knockouts (top-level layers, clipped layers, layers that others are clipped to, pass-through groups with layers in other modes, other Knockout values, and merges that need more memory than allowed) aren't applied and are counted in the warnings. 8-, 16- and 32-bit files, PSB, raw, PackBits and zipped channels (`crates/engine/src/cmd/fileio/tests_psdlayers.rs`). - File › Open of a PSD or PSB file with two or more layers, and File › Place of one without Link, show PSD Import Options: Convert Layers to Objects with Import Hidden Layers, or Flatten Layers to a Single Image, the options [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for a file that is opened or embedded. A file placed with Link shows its flat image and isn't asked about. `file.place` takes `layers` and `hiddenLayers`, and `document.psdInfo` counts a file's layers without opening it. VectorCraft's own rules: placed layers come in as one group named after the file, a layer as an image and a layer group as a group, at the place the merged image would take and clipped to the canvas when layers reach past it; the dialog asks from two layers up and starts from the choices made last. Layer comps aren't read (`crates/engine/src/cmd/fileio/tests_psdlayers.rs`, `crates/ui-egui/src/dialogs/psd_import.rs`). - A PSD or PSB file's clipping path, the saved path that image resource 2999 names in the [PSD file format specification](https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/), clips its merged image on File › Place and on File › Open of the merged image, as [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for a file imported as one flattened image. The image comes in a clip group whose clipping path has no paint and uses the even-odd rule, and the path can be edited or released. `links.update` and `links.relink` give the clip group the clipping path the new file names, or leave a plain group when it names none, and files that name no clipping path come in as before (`crates/engine/src/cmd/fileio/tests_psdread.rs`). - Import Slices: File › Open of a PSD or PSB file with slices, and File › Place of one without Link, bring its slices in as user slices, as [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for a file that is opened or embedded; PSD Import Options offers Import Slices for such a file. `document.open` and `file.place` take `importSlices`, and `document.psdInfo` counts the slices. The slices are read from image resource 1050 in version 6 and in the descriptor of versions 7 and 8, as the [Photoshop File Formats Specification](https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/) describes it. VectorCraft's own rules: Import Slices applies to Convert Layers to Objects and to Flatten Layers to a Single Image alike, and makes the dialog ask for a file with fewer than two layers; placed slices move and scale with the art in the same undo step; each slice keeps its name, type, link, target, message, alternative text, cell text and background color; slices the file generated automatically, and slices wholly outside the canvas, are left out; slices that can't be read are left out with a warning while the art comes in; with Show Import Options off, a PSD file is placed with the PSD Import Options chosen last, and on, each PSD file of the place cursor asks for them (`crates/engine/src/cmd/fileio/tests_psdslices.rs`, `crates/ui-egui/src/dialogs/psd_import.rs`). From 67c446b31345e91074265c3ff7358b1662b24e1a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 18 Nov 2026 18:16:47 -0800 Subject: [PATCH 7/7] PSD import: a knockout that cuts nothing gives back the warnings of a merge it left short of memory A visible knockout whose trial merge cut nothing kept the warnings that drawing the layers below it recorded while its canvas was held. A merge below it that only failed for want of that memory then came in merged, with a warning that it hadn't been applied. Such a trial now gives back what it counted, and the layers count again when they come in. The PSD catalog test compares the whole numbers of each warning and its translation, and checks that each complete catalog puts the translated reason inside the warnings that carry one. --- crates/engine/src/cmd/fileio/psdlayers.rs | 11 ++++++++++ .../engine/src/cmd/fileio/tests_psdlayers.rs | 22 +++++++++++++++++++ crates/ui-egui/src/i18n/tests.rs | 11 ++++++---- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/cmd/fileio/psdlayers.rs b/crates/engine/src/cmd/fileio/psdlayers.rs index 3779b72745..17e6c3f814 100644 --- a/crates/engine/src/cmd/fileio/psdlayers.rs +++ b/crates/engine/src/cmd/fileio/psdlayers.rs @@ -863,6 +863,8 @@ struct Builder<'a, F> { laid_blank: Vec, /// The groups of both, in the order they were found. blank_log: Vec, + /// How many times a merge was counted as not made because it needed more memory than allowed. + squeezed: usize, } impl [f32; 3]> Builder<'_, F> { @@ -1274,6 +1276,7 @@ impl [f32; 3]> Builder<'_, F> { /// Count record `i` as a clipped layer left unclipped (once). fn unclip(&mut self, i: usize) { + self.squeezed += 1; if let Some(s) = self.unclipped.get_mut(i) && !*s { @@ -1305,6 +1308,7 @@ impl [f32; 3]> Builder<'_, F> { if self.knocks(&u, &out, level)? { let saved = self.saved(); let noted = self.noted_so_far(); + let squeezed = self.squeezed; let outer = self.try_from_here(); let knocked = self.knock(&out, &u)?; self.tried(outer, u.visible && matches!(knocked, Knocked::Merged(_)))?; @@ -1334,9 +1338,14 @@ impl [f32; 3]> Builder<'_, F> { self.restore(saved)?; self.unnote(noted); } + // A merge below it that its held canvas left without the memory it needed may + // be made when they come in without it: what drawing them counted is given back + // and counted again then. + Knocked::Nothing if self.squeezed != squeezed => self.restore(saved)?, Knocked::Nothing => self.refund(saved.spent)?, Knocked::TooLarge => { self.restore(saved)?; + self.squeezed += 1; self.unknock(u.record); } } @@ -1382,6 +1391,7 @@ impl [f32; 3]> Builder<'_, F> { } } else { self.restore(saved)?; + self.squeezed += 1; self.notes.unmerged += 1; u.blend = Blend::Mode(BlendMode::Normal); out.push(u); @@ -2025,6 +2035,7 @@ pub(super) fn import( blank: vec![None; records.len()], laid_blank: vec![false; records.len()], blank_log: vec![], + squeezed: 0, }; let layers = b.list(&items, Level::Top)?; // A drawing for a knockout or a clip leaves hidden layers out; those that came in anyway aren't. diff --git a/crates/engine/src/cmd/fileio/tests_psdlayers.rs b/crates/engine/src/cmd/fileio/tests_psdlayers.rs index d197658c20..d5dccfd662 100644 --- a/crates/engine/src/cmd/fileio/tests_psdlayers.rs +++ b/crates/engine/src/cmd/fileio/tests_psdlayers.rs @@ -890,6 +890,28 @@ fn a_knockout_merge_past_the_memory_allowed_leaves_the_layers_apart() { } } +/// A knockout that cuts nothing gives back what drawing the layers below it counted: here a +/// knockout at 0 fill beside a group, whose own knockout doesn't fit 192 bytes while the outer +/// knockout's canvas is held, but merges when the group comes in. +#[test] +fn a_knockout_that_cuts_nothing_doesnt_keep_what_drawing_below_it_counted() { + let cms = |c: [f32; 4]| [c[0], c[1], c[2]]; + let layers = [ + group_end(), + group_end(), + layer("Opaque", [0, 0, 2, 2], [255, 0, 0, 255]), + knockout(layer("Inner cut", [0, 0, 1, 2], [0, 0, 255, 255]), 1, 0), + group("Inner", b"norm"), + knockout(layer("Outer cut", [2, 2, 4, 4], [0, 0, 255, 255]), 1, 0), + group("Outer", b"norm"), + ]; + let (doc, warnings) = psdlayers::import(&file(4, 4, &layers, 8, false, false), false, 192, cms).unwrap().unwrap(); + let outer = doc.layers[0].children().unwrap(); + assert_eq!(outer.iter().map(|c| name(c)).collect::>(), ["Inner", "Outer cut"], "{warnings:?}"); + assert_eq!(outer[0].children().unwrap().iter().map(|c| name(c)).collect::>(), ["Opaque + Inner cut"], "{warnings:?}"); + assert!(warnings.is_empty(), "{warnings:?}"); +} + /// A knockout merge that can't be made leaves the import the whole allowance for reading the layers /// apart: here the merge decodes 22 layers over one another before the layer beside them makes its /// canvas too large, which used up nearly all of eight times 64 KiB. The merge has that allowance diff --git a/crates/ui-egui/src/i18n/tests.rs b/crates/ui-egui/src/i18n/tests.rs index 8cdd1a490e..34d4c3f9f1 100644 --- a/crates/ui-egui/src/i18n/tests.rs +++ b/crates/ui-egui/src/i18n/tests.rs @@ -625,12 +625,15 @@ fn psd_import_strings_are_in_complete_catalogs() { for s in &shown { let tr = message(lang, s); assert_ne!(&tr, s, "{code}"); - for n in numbers(s) { - assert!(tr.contains(&n), "{code}: {tr} lost {n}"); - } + let (mut source, mut translated) = (numbers(s), numbers(&tr)); + source.sort(); + translated.sort(); + assert_eq!(source, translated, "{code}: {tr}"); } for (s, inner) in [(unread_slices, nested), (unread_layers, damaged)] { - assert!(!message(lang, s).contains(inner), "{code}: {s}"); + let reason = message(lang, inner); + assert_ne!(reason, inner, "{code}"); + assert!(message(lang, s).contains(&reason), "{code}: {s}"); } assert!(message(lang, &merged).contains("Hue, Luminosity"), "{code}"); assert!(message(lang, &opened_with_notes(1, &one)).ends_with(&message(lang, &one)), "{code}");