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.
This commit is contained in:
dotCooCoo committed 2026-11-18 06:49:40 -08:00
1 parent c4f19c9207
commit a6d4729bf2
4 files changed
+163 -47

No files matched your search

+96 -29
View File
@@ -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<bool>,
/// Those records, in the order they were counted.
noted_log: Vec<usize>,
/// The records counted as hidden layers left out.
skipped: Vec<bool>,
/// Those records, in the order they were counted.
@@ -840,6 +846,9 @@ struct Builder<'a, F> {
kept: Vec<bool>,
/// Bytes of layer and mask pixels decoded, and of canvases the merges made, so far.
spent: Cell<u64>,
/// 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<u64>,
/// The layers whose pixels were read so far (each time they were).
painted: Cell<usize>,
/// What each group gave when drawn alone without reading any layer's pixels (nothing, or a
@@ -873,6 +882,20 @@ impl<F: Fn([f32; 4]) -> [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<Option<(i64, i64, RgbaImage)>, String> {
let rec = self.record(i)?;
@@ -1006,9 +1029,11 @@ impl<F: Fn([f32; 4]) -> [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<F: Fn([f32; 4]) -> [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<F: Fn([f32; 4]) -> [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<F: Fn([f32; 4]) -> [f32; 3]> Builder<'_, F> {
let mut out: Vec<Unit<'i>> = 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<F: Fn([f32; 4]) -> [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<F: Fn([f32; 4]) -> [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<F: Fn([f32; 4]) -> [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<F: Fn([f32; 4]) -> [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<F: Fn([f32; 4]) -> [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<F: Fn([f32; 4]) -> [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()],
+61 -13
View File
@@ -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::<Vec<_>>();
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::<Vec<_>>(), ["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::<Vec<_>>(), ["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
+5 -4
View File
@@ -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}}}
+1 -1
View File
@@ -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`).