From 0d272049445a08835527e63b364d553ba4b10b2b Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 21 Nov 2026 06:11:57 -0800 Subject: [PATCH] PSD import: keep clips and merges around adjustments that aren't applied - An adjustment layer's mask is checked against the memory allowed beside the canvases held while it is read (the merged canvas, the base's shape and the copies kept for pass-through groups). - A clipped adjustment layer whose mask is too large to read is left out without unclipping the layers clipped above it to the same base. - An adjustment layer in a pass-through group whose mask is too large to read is left out without stopping the merge of the layer above the group; its own merge with the layers below it still isn't made. - A layer whose stored pixels are all transparent draws nothing, so an adjustment layer above it in a pass-through group stays kept to change what is below the group. - A layer merged in a pass-through group for its blend mode or its knockout takes the adjustment layers kept below it with it, so an adjustment layer above that group in an outer pass-through group merges with it instead of being dropped. --- crates/engine/src/cmd/fileio/psdlayers.rs | 136 ++++++++++++++++-- .../engine/src/cmd/fileio/tests_psdlayers.rs | 101 +++++++++++++ docs/mcp.md | 6 +- docs/target-app-parity.md | 2 +- 4 files changed, 227 insertions(+), 18 deletions(-) diff --git a/crates/engine/src/cmd/fileio/psdlayers.rs b/crates/engine/src/cmd/fileio/psdlayers.rs index 5fc6916c5e..dae505e247 100644 --- a/crates/engine/src/cmd/fileio/psdlayers.rs +++ b/crates/engine/src/cmd/fileio/psdlayers.rs @@ -45,11 +45,14 @@ //! otherwise, and Posterize maps each channel to the nearest of its evenly spaced levels, the higher //! one on a tie. The adjusted color is composited over the color below in the layer's blend mode, //! at its opacity times its fill opacity, through its layer mask; transparency stays as it was. One -//! with nothing visible below it in its group that draws (a layer that stores no pixels or is at 0 +//! with nothing visible below it in its group that draws (a layer that stores no pixels, other than +//! a solid color fill drawn over the canvas, a layer whose pixels are all transparent, or one at 0 //! opacity or 0 fill with nothing clipped to it doesn't, nor does a group of only such layers) //! changes nothing; in a pass-through group it changes //! what is below the group when the group is merged, and otherwise isn't applied. One that layers -//! are clipped to, and one clipped to another adjustment layer, isn't applied. +//! are clipped to, and one clipped to another adjustment layer, isn't applied. One whose layer mask +//! needs more memory than allowed beside the canvases it would change isn't applied, and a clip, or +//! a merge for another layer, that it is part of goes on without it. //! //! Other adjustment layers, Invert, Threshold and Posterize data that can't be read (a level out of //! range, too short, or a second adjustment block on the layer), fill layers that store no pixels and @@ -969,6 +972,12 @@ struct Builder<'a, F> { blank_log: Vec, /// How many times a merge was counted as not made because it needed more memory than allowed. squeezed: usize, + /// The adjustment layer whose own merge with the layers below it is being made: a layer mask of + /// its too large to read stops that merge, where one of another adjustment layer drawn in it + /// leaves that one out. + adjusting: Option, + /// Whether each record's stored transparency is 0 everywhere, once [`Self::clear`] has read it. + clear: Vec>>, } impl [f32; 3]> Builder<'_, F> { @@ -1098,8 +1107,10 @@ impl [f32; 3]> Builder<'_, F> { } /// Does record `i`'s layer mask, switched on, fit the memory allowed for reading it with - /// [`Self::mask`]? `true` when it has none, and when its size is damaged (reading it says so). - fn mask_fits(&self, i: usize) -> Result { + /// [`Self::mask`] beside `live` bytes of canvases held while it is read, and the copies of what + /// is below the pass-through groups being merged? `true` when it has none, and when its size is + /// damaged (reading it says so). + fn mask_fits(&self, i: usize, live: u64) -> Result { let rec = self.record(i)?; let Some(m) = rec.mask.filter(|m| !m.disabled) else { return Ok(true) }; if !rec.channels.iter().any(|(c, _)| *c == -2) { @@ -1110,7 +1121,42 @@ impl [f32; 3]> Builder<'_, F> { if w > max_side || rows > max_side { return Ok(true); } - Ok(self.fits((row_bytes(w, self.h.depth) as u64 + w as u64).saturating_mul(rows as u64)).is_ok()) + let bytes = (row_bytes(w, self.h.depth) as u64 + w as u64).saturating_mul(rows as u64); + Ok(bytes.saturating_add(live) <= self.room()) + } + + /// Does record `i` store pixels that are all fully transparent, so that it paints nothing? + /// Its transparency is read once. `false` when it stores none, or has no transparency, or its + /// transparency can't be read within the memory allowed or is damaged (drawing it says so). + fn clear(&self, i: usize) -> Result { + if let Some(clear) = self.clear.get(i).and_then(Cell::get) { + return Ok(clear); + } + let rec = self.record(i)?; + let (w, rows) = rec.bounds.size(); + let max_side = if self.h.psb { 300_000 } else { 30_000 }; + let alpha = rec.channels.iter().find(|(c, _)| *c == -1).map(|(_, data)| data); + let clear = match alpha { + Some(data) if !rec.draws_nothing() && !rec.color_over_canvas() && w <= max_side && rows <= max_side => { + let (depth, row) = (self.h.depth, row_bytes(w, self.h.depth)); + let bytes = (row as u64).saturating_mul(rows as u64); + if bytes > self.room() { + false + } else { + self.spend(bytes)?; + match layer_channel(data, rows, row, self.h.psb, depth) { + Ok(plane) => (0..rows) + .all(|y| plane.get(y * row..(y + 1) * row).is_some_and(|r| (0..w).all(|x| byte(sample(r, x, depth, false)) == 0))), + Err(_) => false, + } + } + } + _ => false, + }; + if let Some(c) = self.clear.get(i) { + c.set(Some(clear)); + } + Ok(clear) } /// Multiply `c`'s transparency by record `i`'s layer mask. @@ -1229,9 +1275,12 @@ impl [f32; 3]> Builder<'_, F> { } // A clipped adjustment layer changes the merged canvas in place, through its layer mask. let adjust = self.adjustment(body)?; - // Its layer mask too large to read needs more memory than allowed. - if adjust.is_some() && !base_adjustment && !self.mask_fits(i)? { - return Ok(false); + // Its layer mask too large to read needs more memory than allowed: it isn't applied, and the + // layers clipped above it still clip to the base. + if adjust.is_some() && !base_adjustment && !self.mask_fits(i, 0)? { + self.squeezed += 1; + self.unadjust(i); + return Ok(true); } let layer = match adjust { Some(_) => Canvas::default(), @@ -1282,6 +1331,16 @@ impl [f32; 3]> Builder<'_, F> { self.unadjust(i); return Ok(true); } + // The mask is read beside the merged canvas and the base's shape. + let shape = match &u.clip { + Some(Clip::Shape(s)) => 4 * s.len() as u64, + _ => 0, + }; + if !self.mask_fits(i, (16 * c.px.len() as u64).saturating_add(shape))? { + self.squeezed += 1; + self.unadjust(i); + return Ok(true); + } let mask = self.mask(i, false)?; adjust_onto(c, a, blend, opacity, mask.as_ref(), i); u.name.push_str(" + "); @@ -1497,6 +1556,12 @@ impl [f32; 3]> Builder<'_, F> { continue; } let (clips, drawn) = self.base_draws(items, k)?; + // One in a mode VectorCraft lacks (an adjustment layer aside), or with a shallow knockout + // that cuts what is below it, is merged with the layers below it in the group, which + // takes the ones kept so far with them (see `merged`). + if (matches!(r.blend, Blend::Other(_)) && (children.is_some() || r.adjust().is_none())) || self.cuts(items, k, clips, drawn && draws)? { + records.clear(); + } // One that layers are clipped to isn't kept, and a pass-through group they are clipped to // is drawn alone (see `clip`). One at 0 opacity changes nothing (see `adjust_below`). let keeps = !clips && r.opacity * r.fill > 0.0 && (!draws || !records.is_empty()); @@ -1517,6 +1582,33 @@ impl [f32; 3]> Builder<'_, F> { Ok((records, draws)) } + /// Does item `k` of `items`, a pass-through group's layers, take a shallow knockout that is merged + /// with the layers below it (see [`Self::knocks`])? Only one that `clips` nothing, is in the + /// Normal mode over visible layers in the Normal mode back to the last one merged for its mode, + /// and where it and something below it draw (`draws`). + fn cuts(&self, items: &[Item], k: usize, clips: bool, draws: bool) -> Result { + let record = |item: &Item| match item { + Item::Layer(i) | Item::Group(i, _) => self.record(*i), + }; + let Some(r) = items.get(k).map(record).transpose()? else { return Ok(false) }; + let normal = Blend::Mode(BlendMode::Normal); + if r.knockout != 1 || clips || !draws || r.blend != normal { + return Ok(false); + } + for below in items.get(..k).unwrap_or_default().iter().rev() { + let b = record(below)?; + if b.hidden || b.clipped { + continue; + } + match b.blend { + Blend::Other(_) if matches!(below, Item::Group(..)) || b.adjust().is_none() => break, + blend if blend != normal => return Ok(false), + _ => {} + } + } + Ok(true) + } + /// Does anything in `items` draw: a visible layer or group that isn't clipped and draws (see /// [`Self::base_draws`])? fn draws(&self, items: &[Item]) -> Result { @@ -1567,7 +1659,7 @@ impl [f32; 3]> Builder<'_, F> { Ok(match item { Item::Layer(i) => { let r = self.record(*i)?; - (r, !r.draws_nothing()) + (r, !r.draws_nothing() && !self.clear(*i)?) } Item::Group(g, children) => (self.record(*g)?, self.draws(children)?), }) @@ -1732,9 +1824,9 @@ impl [f32; 3]> Builder<'_, F> { let mut below = false; for b in out.iter().filter(|b| b.visible && b.opacity > 0.0) { below |= match &b.body { - // An adjustment layer, a layer that draws nothing, or a group of such layers, has - // nothing to change. - Body::Layer(i) => !self.record(*i)?.draws_nothing(), + // An adjustment layer, a layer that draws nothing or whose pixels are all + // transparent, or a group of such layers, has nothing to change. + Body::Layer(i) => !self.record(*i)?.draws_nothing() && !self.clear(*i)?, Body::Group(_, items) => self.draws(items)?, // Clipped layers merged with a base that has no pixels have none either. Body::Merged(c) => c.px.iter().any(|p| p[3] > 0.0), @@ -1751,7 +1843,10 @@ impl [f32; 3]> Builder<'_, F> { } let saved = self.saved(); let outer = self.try_from_here(); - let made = self.merge_into(out, &u)?; + let adjusting = self.adjusting.replace(u.record); + let made = self.merge_into(out, &u); + self.adjusting = adjusting; + let made = made?; self.tried(outer, made)?; if !made { self.restore(saved)?; @@ -2018,8 +2113,17 @@ impl [f32; 3]> Builder<'_, F> { if let Some(a) = self.adjustment(&u.body)? { if let Some(c) = acc.as_mut() { let mask = match u.mask_of { - // A layer mask too large to read needs more memory than allowed. - Some(i) if !self.mask_fits(i)? => return Ok(false), + // A layer mask too large to read beside the canvas needs more memory than + // allowed: the adjustment isn't applied, and the rest of the merge goes on. + Some(i) if !self.mask_fits(i, 16 * c.px.len() as u64)? => { + // The adjustment's own merge isn't made then. + if self.adjusting == Some(u.record) { + return Ok(false); + } + self.squeezed += 1; + self.unadjust(u.record); + return Ok(true); + } Some(i) => self.mask(i, false)?, None => None, }; @@ -2454,6 +2558,8 @@ pub(super) fn import( laid_blank: vec![false; records.len()], blank_log: vec![], squeezed: 0, + adjusting: None, + clear: vec![Cell::new(None); records.len()], }; 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 8ebc3a9f24..cda458d2aa 100644 --- a/crates/engine/src/cmd/fileio/tests_psdlayers.rs +++ b/crates/engine/src/cmd/fileio/tests_psdlayers.rs @@ -1707,6 +1707,107 @@ fn an_adjustment_mask_past_the_memory_allowed_isnt_applied() { } } +/// An adjustment layer's mask is read beside the canvases it changes: a 100 × 1 mask fits 256 bytes +/// alone, but not beside a 6 × 1 canvas, so the layer isn't applied. With the memory for both, it is. +#[test] +fn an_adjustment_mask_counts_the_canvas_it_changes() { + let cms = |c: [f32; 4]| [c[0], c[1], c[2]]; + let masked = |clipped: bool| L { clipped, mask: Some(([0, 0, 1, 100], 255, 0, vec![255; 100])), ..adjustment("Invert", b"nvrt", &[]) }; + for clipped in [false, true] { + let f = file(6, 1, &[layer("Under", [0, 0, 1, 6], [10, 20, 30, 255]), masked(clipped)], 8, false, false); + let (doc, warnings) = psdlayers::import(&f, false, 256, cms).unwrap_or_else(|e| panic!("{clipped}: {e}")).unwrap(); + assert_eq!(doc.layers.iter().map(|l| name(l)).collect::>(), ["Under"], "{clipped}: {warnings:?}"); + assert_eq!(warnings, ["1 adjustment layer was not applied"], "{clipped}"); + let (doc, warnings) = psdlayers::import(&f, false, 1 << 20, cms).unwrap().unwrap(); + assert_eq!(doc.layers.iter().map(|l| name(l)).collect::>(), ["Under + Invert"], "{clipped}: {warnings:?}"); + assert!(warnings.is_empty(), "{clipped}: {warnings:?}"); + } +} + +/// An adjustment layer whose mask is too large to read is left out without stopping the clip or the +/// merge around it: a layer clipped above it still clips to the base, and a layer in a mode +/// VectorCraft lacks above a pass-through group holding it still merges in its mode. +#[test] +fn an_adjustment_mask_too_large_leaves_the_clip_and_merge_around_it() { + let cms = |c: [f32; 4]| [c[0], c[1], c[2]]; + let masked = |clipped: bool| L { clipped, mask: Some(([0, 0, 100, 100], 255, 0, vec![255; 10_000])), ..adjustment("Invert", b"nvrt", &[]) }; + let layers = [layer("Base", [0, 0, 1, 1], [200, 0, 0, 255]), masked(true), L { clipped: true, ..layer("Photo", [0, 0, 1, 2], [0, 0, 255, 255]) }]; + let (doc, warnings) = psdlayers::import(&file(2, 1, &layers, 8, false, false), false, 128, cms).unwrap().unwrap(); + assert_eq!(doc.layers.iter().map(|l| name(l)).collect::>(), ["Base + Photo"], "{warnings:?}"); + assert_eq!(warnings, ["1 adjustment layer was not applied"]); + // Gray 100 under Linear Dodge's gray 20 gives gray 120. + let layers = [ + layer("Under", [0, 0, 1, 1], [100, 100, 100, 255]), + group_end(), + masked(false), + group("Folder", b"pass"), + L { blend: b"lddg", ..layer("Dodge", [0, 0, 1, 1], [20, 20, 20, 255]) }, + ]; + let (doc, warnings) = psdlayers::import(&file(1, 1, &layers, 8, false, false), false, 128, cms).unwrap().unwrap(); + assert_eq!(doc.layers.iter().map(|l| name(l)).collect::>(), ["Under + Folder + Dodge"], "{warnings:?}"); + let merged = "layers in blend modes VectorCraft doesn't have (Linear Dodge) were merged with the layers below them"; + assert_eq!(warnings, ["1 adjustment layer was not applied", merged]); + let Some([c]) = doc.layers[0].children().map(Vec::as_slice) else { panic!("the merged layer holds one image") }; + let NodeKind::Image(img) = &c.kind else { panic!("the merged layer holds an image") }; + let px = craft_image_codecs::decode(&doc.images[&img.key].bytes).unwrap().to_rgba8(); + assert_eq!(px, [120, 120, 120, 255]); +} + +/// A layer merged in a pass-through group for its mode or its knockout takes the adjustment layers +/// kept below it with it: an adjustment layer above that group in an outer pass-through group then +/// merges with it, and the gray 100 posterizes to 85. +#[test] +fn an_adjustment_layer_above_a_group_whose_kept_ones_merged_is_applied() { + let tops = [ + ("Linear Dodge", vec![L { blend: b"lddg", ..layer("Top", [0, 0, 1, 1], [100, 100, 100, 255]) }]), + ("knockout", vec![layer("Photo", [0, 0, 1, 1], [50, 50, 50, 255]), knockout(layer("Top", [0, 0, 1, 1], [100, 100, 100, 255]), 1, 255)]), + ]; + for (what, top) in tops { + let layers = [ + &[group_end(), group_end(), adjustment("Invert", b"nvrt", &[])][..], + &top, + &[group("Inner", b"pass"), adjustment("Posterize", b"post", &[0, 4, 0, 0]), group("Folder", b"pass")], + ] + .concat(); + let (s, r) = open(&file(1, 1, &layers, 8, false, false), json!({})); + let d = &s.doc().unwrap().doc; + assert_eq!(row_of(&s, sublayer(&d.layers[0], "Posterize")), [[85, 85, 85, 255]], "{what}: {r}"); + assert!(r["warnings"].to_string().contains("1 adjustment layer was not applied"), "{what}: {r}"); + } +} + +/// A layer whose pixels are all transparent draws nothing: an adjustment layer above it in a +/// pass-through group, directly or in a pass-through group inside it, still changes what is below +/// the group. +#[test] +fn an_adjustment_layer_over_transparent_pixels_changes_what_is_below_the_group() { + let clear = || layer("Clear", [0, 0, 1, 1], [50, 50, 50, 0]); + let dodge = || L { blend: b"lddg", ..layer("Dodge", [0, 0, 1, 1], [0, 0, 0, 255]) }; + let direct = [ + layer("Under", [0, 0, 1, 1], [10, 20, 30, 255]), + group_end(), + clear(), + adjustment("Invert", b"nvrt", &[]), + group("Folder", b"pass"), + dodge(), + ]; + let (s, r) = open(&file(1, 1, &direct, 8, false, false), json!({})); + assert_eq!(row_of(&s, &s.doc().unwrap().doc.layers[0]), [[245, 235, 225, 255]], "{r}"); + let nested = [ + layer("Under", [0, 0, 1, 1], [10, 20, 30, 255]), + group_end(), + group_end(), + clear(), + adjustment("Invert", b"nvrt", &[]), + group("Inner", b"pass"), + adjustment("Posterize", b"post", &[0, 4, 0, 0]), + group("Folder", b"pass"), + dodge(), + ]; + let (s, r) = open(&file(1, 1, &nested, 8, false, false), json!({})); + assert_eq!(row_of(&s, &s.doc().unwrap().doc.layers[0]), [[255, 255, 255, 255]], "{r}"); +} + /// Damaged Invert, Threshold and Posterize layers, masked and clipped, open without a panic. #[test] fn damaged_adjustment_layers_never_panic() { diff --git a/docs/mcp.md b/docs/mcp.md index 2194a45351..04ce0fe200 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -10917,9 +10917,11 @@ The rest are VectorCraft's own rules: nearest of its 2 to 255 evenly spaced levels, the higher one on a tie. The adjusted color is composited over the color below in the layer's blend mode, at its opacity times its fill opacity, through its layer mask, and transparency is kept. A hidden one is left out. One with nothing visible below it in its group that draws (a layer - that stores no pixels or is at 0 opacity or 0 fill with nothing clipped to it doesn't, nor does a group of only such + that stores no pixels, other than a solid color fill drawn over the canvas, a layer whose pixels are all + transparent, or one at 0 opacity or 0 fill with nothing clipped to it doesn't, nor does a group of only such layers) changes nothing, and so does one at 0 opacity or 0 fill; in a Pass Through group it changes what is below the group only when the group is merged, and is otherwise not - applied. + applied. One whose layer mask needs more memory than allowed beside the canvases it would change is not applied, + and a clip, or a merge for another layer, that it is part of goes on without it. - Other adjustment layers, Invert, Threshold and Posterize layers that others are clipped to, that are clipped to another adjustment layer, whose data can't be read (a level out of range, too short, or two adjustment blocks on one layer) or whose merge, its layer mask included, needs more memory than allowed, fill layers that store no pixels and whose fill isn't diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index b42ce17573..0046922018 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -2324,7 +2324,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - 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 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 other than Invert, Threshold and Posterize, 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 with a PSD or PSB file's layers as objects applies Invert, Threshold and Posterize adjustment layers (the `nvrt`, `thrs` and `post` blocks of the [PSD file format specification](https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/)): each is merged with the visible layers below it in its group, which it changes, as [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for adjustment layers; clipped to a layer, it changes only that layer and the layers clipped to it. Each adjustment works on the colors as [the help page](https://helpx.adobe.com/photoshop/desktop/adjust-color/color-effects-techniques/apply-special-color-effects-to-images.html) describes them. VectorCraft's own rules: each channel is taken as an 8-bit sRGB value; Invert turns `v` into `255 − v`; Threshold compares the Rec. 709 luminance of linear sRGB, encoded back to 0..255, with its level (1 to 255), white above it and black at or below it; Posterize maps each channel to the nearest of its 2 to 255 evenly spaced levels, the higher one on a tie; the adjusted color is composited over the color below in the layer's blend mode at its opacity and fill opacity, through its layer mask, and transparency is kept. A hidden one is left out; one with nothing visible below it in its group that draws (a layer that stores no pixels or is at 0 opacity or 0 fill with nothing clipped to it doesn't, nor does a group of only such layers) changes nothing, and so does one at 0 opacity or 0 fill; in a pass-through group it changes what is below the group only when the group is merged; one that layers are clipped to, one clipped to another adjustment layer, one whose data can't be read (a level out of range, too short, or two adjustment blocks on one layer), and one whose merge, its layer mask included, needs more memory than allowed aren't applied and are counted in the warnings (`crates/engine/src/cmd/fileio/tests_psdlayers.rs`). + - File › Open with a PSD or PSB file's layers as objects applies Invert, Threshold and Posterize adjustment layers (the `nvrt`, `thrs` and `post` blocks of the [PSD file format specification](https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/)): each is merged with the visible layers below it in its group, which it changes, as [PSD import options](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-from-other-apps/photoshop-import-options.html) describes for adjustment layers; clipped to a layer, it changes only that layer and the layers clipped to it. Each adjustment works on the colors as [the help page](https://helpx.adobe.com/photoshop/desktop/adjust-color/color-effects-techniques/apply-special-color-effects-to-images.html) describes them. VectorCraft's own rules: each channel is taken as an 8-bit sRGB value; Invert turns `v` into `255 − v`; Threshold compares the Rec. 709 luminance of linear sRGB, encoded back to 0..255, with its level (1 to 255), white above it and black at or below it; Posterize maps each channel to the nearest of its 2 to 255 evenly spaced levels, the higher one on a tie; the adjusted color is composited over the color below in the layer's blend mode at its opacity and fill opacity, through its layer mask, and transparency is kept. A hidden one is left out; one with nothing visible below it in its group that draws (a layer that stores no pixels, other than a solid color fill drawn over the canvas, a layer whose pixels are all transparent, or one at 0 opacity or 0 fill with nothing clipped to it doesn't, nor does a group of only such layers) changes nothing, and so does one at 0 opacity or 0 fill; in a pass-through group it changes what is below the group only when the group is merged; one that layers are clipped to, one clipped to another adjustment layer, one whose data can't be read (a level out of range, too short, or two adjustment blocks on one layer), and one whose merge, its layer mask included, needs more memory than allowed aren't applied and are counted in the warnings; a clip, or a merge for another layer, that one with a layer mask too large to read is part of goes on without it (`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; `links.update` leaves a distorted image's clipping as it is. When Placement Options do not clip the image, a clip group whose clipping path was changed in the document (an anchor or handle moved by a thousandth of a point or more, anchors added or removed, a subpath opened or closed, or its fill rule changed) and a clipping mask made in the document are kept: a new file that names no clipping path leaves the mask clipping the image, and the clip group of a new file that names one goes inside the mask. Files that name no clipping path come in as before. `links.unembed` keeps the clip group, and the PNG or TIFF it writes is read with the path on File › Open, File › Place, `links.relink` and `links.update` (`crates/engine/src/cmd/fileio/tests_psdread.rs`, `crates/engine/src/tests/dcs.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`).