This commit is contained in:
bflatastic committed 2026-11-18 15:03:50 +07:00
commit bf59b08bbf
4 files changed
+792 -88

No files matched your search

+447 -84
View File
@@ -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
@@ -66,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",
@@ -227,7 +240,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 +300,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 +360,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,
_ => {}
}
@@ -612,6 +626,8 @@ struct Unit<'i> {
body: Body<'i>,
/// While layers are clipped to it: how its merged canvas is shaped.
clip: Option<Clip>,
/// Layers are clipped to it, merged into it or not.
base: bool,
}
impl Unit<'_> {
@@ -640,13 +656,36 @@ 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),
}
/// 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, 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,
}
/// A unit's body drawn alone.
#[derive(Clone)]
enum Drawn {
/// It draws nothing.
Nothing,
@@ -663,6 +702,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,10 +718,14 @@ struct Notes {
#[derive(Clone, Copy)]
struct Counts {
skipped: usize,
/// The knockouts counted as not applied.
unknocked: usize,
merged: usize,
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
@@ -745,8 +789,16 @@ 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, blank: usize) -> Counts {
Counts {
skipped,
unknocked,
merged: self.merged.len(),
unmerged: self.unmerged,
unclipped: self.unclipped,
clipped_out: self.clipped_out,
blank,
}
}
}
@@ -771,8 +823,33 @@ struct Builder<'a, F> {
skipped: Vec<bool>,
/// Those records, in the order they were counted.
skip_log: Vec<usize>,
/// The records counted as knockouts not applied.
unknocked: Vec<bool>,
/// Those records, in the order they were counted.
unknock_log: Vec<usize>,
/// The records counted as layers clipped to a fill that isn't drawn.
clipped_out: Vec<bool>,
/// Those records, in the order they were counted.
clipped_out_log: Vec<usize>,
/// The records counted as clipped layers left unclipped.
unclipped: Vec<bool>,
/// Those records, in the order they were counted.
unclip_log: Vec<usize>,
/// 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<bool>,
/// Bytes of layer and mask pixels decoded, and of canvases the merges made, so far.
spent: 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
/// canvas without pixels, whatever the memory left): drawing it again gives that without
/// walking its layers.
blank: Vec<Option<Drawn>>,
/// The pass-through groups that laid nothing onto nothing (see [`Self::laid_nothing`]).
laid_blank: Vec<bool>,
/// The groups of both, in the order they were found.
blank_log: Vec<usize>,
}
impl<F: Fn([f32; 4]) -> [f32; 3]> Builder<'_, F> {
@@ -814,6 +891,7 @@ impl<F: Fn([f32; 4]) -> [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(),
@@ -845,6 +923,7 @@ impl<F: Fn([f32; 4]) -> [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)))
}
@@ -897,7 +976,7 @@ impl<F: Fn([f32; 4]) -> [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);
@@ -920,14 +999,21 @@ impl<F: Fn([f32; 4]) -> [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 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;
@@ -939,7 +1025,7 @@ impl<F: Fn([f32; 4]) -> [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 {
@@ -962,7 +1048,7 @@ impl<F: Fn([f32; 4]) -> [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))? {
@@ -1048,9 +1134,9 @@ impl<F: Fn([f32; 4]) -> [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 +1148,42 @@ impl<F: Fn([f32; 4]) -> [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. 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<bool, String> {
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 && !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 && changes)
}
/// 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 +1195,67 @@ impl<F: Fn([f32; 4]) -> [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<Vec<Unit<'i>>, String> {
/// 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<Vec<Unit<'i>>, 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<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());
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,
// with the hidden layers its drawing left out (see `kept`).
Knocked::Nothing => {}
Knocked::TooLarge => {
self.restore(saved);
self.unknock(u.record);
}
}
}
let Blend::Other(other) = u.blend else {
out.push(u);
continue;
@@ -1091,7 +1266,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());
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() => {
@@ -1113,34 +1288,7 @@ impl<F: Fn([f32; 4]) -> [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::<Vec<_>>() {
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,10 +1316,35 @@ impl<F: Fn([f32; 4]) -> [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);
}
}
// 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
@@ -1186,24 +1359,131 @@ impl<F: Fn([f32; 4]) -> [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<Unit<'i>>, 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::<Vec<_>>() {
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,
base: false,
});
}
/// 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<Knocked, String> {
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<Canvas> = 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);
}
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))
}
/// 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 {
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<Canvas>, 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<Canvas>, u: &Unit<'_>) -> Result<bool, String> {
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);
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() {
@@ -1212,11 +1492,14 @@ impl<F: Fn([f32; 4]) -> [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);
}
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());
}
@@ -1230,10 +1513,10 @@ impl<F: Fn([f32; 4]) -> [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<Canvas>, items: &[Item]) -> Result<bool, String> {
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<Canvas>, items: &[Item], level: Level) -> Result<bool, String> {
for c in self.merged(items, false, level)? {
if !self.composite(acc, &c)? {
return Ok(false);
}
@@ -1271,13 +1554,29 @@ impl<F: Fn([f32; 4]) -> [f32; 3]> Builder<'_, F> {
}
None => return Ok(Drawn::Nothing),
},
Body::Group(items) => {
let mut acc: Option<Canvas> = None;
if !self.lay(&mut acc, items)? {
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<Canvas> = 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 {
@@ -1306,10 +1605,10 @@ impl<F: Fn([f32; 4]) -> [f32; 3]> Builder<'_, F> {
Ok(n)
}
/// Layers of the document for `items`, bottom first.
fn list(&mut self, items: &[Item]) -> Result<Vec<Arc<Node>>, String> {
/// Layers of the document for `items`, the layers at `level`, bottom first.
fn list(&mut self, items: &[Item], level: Level) -> Result<Vec<Arc<Node>>, 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));
}
@@ -1320,6 +1619,9 @@ impl<F: Fn([f32; 4]) -> [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<Option<Node>, 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)?)],
@@ -1329,13 +1631,13 @@ impl<F: Fn([f32; 4]) -> [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;
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() {
@@ -1443,6 +1745,62 @@ fn blend_onto(
max_alloc: u64,
spend: &dyn Fn(u64) -> Result<(), String>,
) -> Result<bool, String> {
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` (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<Canvas>,
knock: &Canvas,
blend: Blend,
fill: f32,
opacity: f32,
record: usize,
max_alloc: u64,
spend: &dyn Fn(u64) -> Result<(), String>,
) -> Result<bool, String> {
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 = 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 {
*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<Canvas>, src: &Canvas, max_alloc: u64, spend: &dyn Fn(u64) -> Result<(), String>) -> Result<bool, String> {
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 +1833,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 +1873,22 @@ 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![],
clipped_out: vec![false; records.len()],
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()],
laid_blank: vec![false; records.len()],
blank_log: vec![],
};
let layers = b.list(&items)?;
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;
@@ -646,6 +646,339 @@ 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<L> {
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<String>, Vec<String>) {
let d = &s.doc().unwrap().doc;
let names = |ls: &[std::sync::Arc<Node>]| ls.iter().map(|l| name(l).to_string()).collect::<Vec<_>>();
(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::<Vec<_>>(), ["Cut"], "{r}");
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::<Vec<_>>(), ["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::<Vec<_>>());
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::<Vec<_>>());
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]
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::<Vec<_>>(), ["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!(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::<Vec<_>>(), ["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::<Vec<_>>(), ["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::<Vec<_>>(), ["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::<Vec<_>>(), ["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]);
+11 -3
View File
@@ -10027,8 +10027,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:
@@ -10048,6 +10048,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
@@ -10059,7 +10065,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.
+1 -1
View File
@@ -2160,7 +2160,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; 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`).