mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-22 18:34:42 +01:00
Merge pull request #8464 from dotCooCoo/fix/psd-knockout-layers-merge
PSD import: a layer with a knockout merges with the layers below it in its group
This commit is contained in:
4 files changed
+1075
-103
No files matched your search
@@ -24,20 +24,28 @@
|
||||
//! 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 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. A merge being tried may take that much
|
||||
//! past what was read before it. What the merges that aren't made, or that a hidden layer would
|
||||
//! need, decoded and made is counted apart, against the same limit.
|
||||
|
||||
use std::cell::Cell;
|
||||
use std::sync::Arc;
|
||||
@@ -66,6 +74,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 +242,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 +302,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 +362,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 +628,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 +658,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, 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,
|
||||
}
|
||||
|
||||
/// A unit's body drawn alone.
|
||||
#[derive(Clone)]
|
||||
enum Drawn {
|
||||
/// It draws nothing.
|
||||
Nothing,
|
||||
@@ -663,6 +704,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,
|
||||
@@ -677,11 +719,17 @@ 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,
|
||||
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 +793,17 @@ 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, spent: u64, skipped: usize, unknocked: usize, blank: usize) -> Counts {
|
||||
Counts {
|
||||
spent,
|
||||
skipped,
|
||||
unknocked,
|
||||
merged: self.merged.len(),
|
||||
unmerged: self.unmerged,
|
||||
unclipped: self.unclipped,
|
||||
clipped_out: self.clipped_out,
|
||||
blank,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -767,12 +824,47 @@ 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.
|
||||
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>,
|
||||
/// 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>,
|
||||
/// `spent` when the innermost merge being tried began: until it is made or given up, it may
|
||||
/// spend the whole allowance past that (see [`Self::spend`]).
|
||||
trial: Cell<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>,
|
||||
/// How many times a merge was counted as not made because it needed more memory than allowed.
|
||||
squeezed: usize,
|
||||
}
|
||||
|
||||
impl<F: Fn([f32; 4]) -> [f32; 3]> Builder<'_, F> {
|
||||
@@ -786,10 +878,38 @@ impl<F: Fn([f32; 4]) -> [f32; 3]> Builder<'_, F> {
|
||||
}
|
||||
|
||||
/// Count `bytes` of decoded pixels, or of a canvas a merge makes, against the whole import's
|
||||
/// allowance, [`TOTAL_DECODED`] times the memory allowed for one image.
|
||||
/// allowance, [`TOTAL_DECODED`] times the memory allowed for one image. While a merge is tried
|
||||
/// (see [`Self::try_from_here`]), only what it spent counts against the allowance.
|
||||
fn spend(&self, bytes: u64) -> Result<(), String> {
|
||||
let total = self.spent.get().saturating_add(bytes);
|
||||
self.spent.set(total);
|
||||
if total.saturating_sub(self.trial.get()) > self.max_alloc.saturating_mul(TOTAL_DECODED) {
|
||||
return Err("the PSD file's layers take more memory to read than allowed".into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Start trying a merge that may not be made: what it spends counts against the whole
|
||||
/// allowance, however much was spent before it. Gives what [`Self::tried`] takes back.
|
||||
fn try_from_here(&self) -> u64 {
|
||||
self.trial.replace(self.spent.get())
|
||||
}
|
||||
|
||||
/// End the merge tried since [`Self::try_from_here`] gave `outer`. A merge that is `made` keeps
|
||||
/// what it spent, which must then fit the allowance with what was spent before it.
|
||||
fn tried(&self, outer: u64, made: bool) -> Result<(), String> {
|
||||
self.trial.set(outer);
|
||||
if made { self.spend(0) } else { Ok(()) }
|
||||
}
|
||||
|
||||
/// Move the bytes spent since `spent` was the count to [`Self::wasted`], which has an allowance
|
||||
/// of its own as large: what a merge that isn't made decoded doesn't take the room for reading
|
||||
/// the layers apart.
|
||||
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());
|
||||
}
|
||||
@@ -814,6 +934,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 +966,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 +1019,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 +1042,26 @@ impl<F: Fn([f32; 4]) -> [f32; 3]> Builder<'_, F> {
|
||||
Some(b) => {
|
||||
// Counted even when it draws nothing (an adjustment layer).
|
||||
self.note(i)?;
|
||||
if !unclipped && self.clip(&mut out, b, &body, i, &name, blend, opacity)? {
|
||||
// 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;
|
||||
}
|
||||
let spent = self.spent.get();
|
||||
let outer = self.try_from_here();
|
||||
let made = !unclipped && self.clip(&mut out, b, &body, i, &name, blend, opacity)?;
|
||||
self.tried(outer, made)?;
|
||||
if made {
|
||||
continue;
|
||||
}
|
||||
self.refund(spent)?;
|
||||
// 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 +1073,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 +1096,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))? {
|
||||
@@ -1045,12 +1179,15 @@ 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, 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 +1199,59 @@ 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(())
|
||||
}
|
||||
|
||||
/// 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)
|
||||
&& !*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,21 +1263,100 @@ 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) {
|
||||
self.squeezed += 1;
|
||||
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)? {
|
||||
// A hidden layer in a mode VectorCraft lacks would need the merge for its mode: it is left
|
||||
// out without being drawn.
|
||||
if !u.visible && matches!(u.blend, Blend::Other(_)) {
|
||||
self.skip(u.record);
|
||||
continue;
|
||||
}
|
||||
if self.knocks(&u, &out, level)? {
|
||||
let saved = self.saved();
|
||||
let noted = self.noted_so_far();
|
||||
let squeezed = self.squeezed;
|
||||
let outer = self.try_from_here();
|
||||
let knocked = self.knock(&out, &u)?;
|
||||
self.tried(outer, u.visible && matches!(knocked, Knocked::Merged(_)))?;
|
||||
match knocked {
|
||||
// A hidden layer that would need the merge is left out, and nothing its drawing
|
||||
// counted is counted.
|
||||
Knocked::Merged(_) | Knocked::TooLarge if !u.visible => {
|
||||
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
|
||||
&& !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`). What its drawing
|
||||
// read is read again when it comes in. A hidden one may still be left out by a
|
||||
// later merge: what its drawing counted is counted when it comes in instead.
|
||||
Knocked::Nothing if !u.visible => {
|
||||
self.restore(saved)?;
|
||||
self.unnote(noted);
|
||||
}
|
||||
// A merge below it that its held canvas left without the memory it needed may
|
||||
// be made when they come in without it: what drawing them counted is given back
|
||||
// and counted again then.
|
||||
Knocked::Nothing if self.squeezed != squeezed => self.restore(saved)?,
|
||||
Knocked::Nothing => self.refund(saved.spent)?,
|
||||
Knocked::TooLarge => {
|
||||
self.restore(saved)?;
|
||||
self.squeezed += 1;
|
||||
self.unknock(u.record);
|
||||
}
|
||||
}
|
||||
}
|
||||
let Blend::Other(other) = u.blend else {
|
||||
out.push(u);
|
||||
continue;
|
||||
};
|
||||
// A hidden layer that would need the merge is left out.
|
||||
if !u.visible {
|
||||
self.skip(u.record);
|
||||
continue;
|
||||
}
|
||||
// A merge that can't be made counts nothing its groups would have counted.
|
||||
let saved = self.notes.counts(self.skip_log.len());
|
||||
let saved = self.saved();
|
||||
let outer = self.try_from_here();
|
||||
let made = match out.as_mut_slice() {
|
||||
// Onto the previous merge alone: composited onto its canvas, in place.
|
||||
[prev] if prev.is_merge() => {
|
||||
@@ -1113,45 +1378,20 @@ 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,
|
||||
},
|
||||
};
|
||||
self.tried(outer, made)?;
|
||||
if made {
|
||||
if !self.notes.merged.contains(&other.label()) {
|
||||
self.notes.merged.push(other.label());
|
||||
}
|
||||
} else {
|
||||
self.restore(saved);
|
||||
self.restore(saved)?;
|
||||
self.squeezed += 1;
|
||||
self.notes.unmerged += 1;
|
||||
u.blend = Blend::Mode(BlendMode::Normal);
|
||||
out.push(u);
|
||||
@@ -1160,18 +1400,51 @@ 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;
|
||||
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;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The visible units `below` with `top` composited over them; `None` when the canvas would need
|
||||
@@ -1186,24 +1459,169 @@ 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,
|
||||
};
|
||||
// At 0 fill, beside every layer below it, it cuts nothing: no canvas for them is needed.
|
||||
if fill <= 0.0 && self.beside(below, &k)? {
|
||||
return Ok(Knocked::Nothing);
|
||||
}
|
||||
// The layer's canvas is held while the units below are drawn.
|
||||
let bytes = 16 * k.px.len() as u64;
|
||||
self.held = self.held.saturating_add(bytes);
|
||||
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.
|
||||
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 = clear(below);
|
||||
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))
|
||||
}
|
||||
|
||||
/// Do the pixels `knock` paints lie outside the bounds of every visible unit `below`? Only the
|
||||
/// bounds of layers and of merged canvases are known without drawing them: with a group below,
|
||||
/// `false`.
|
||||
fn beside(&self, below: &[Unit<'_>], knock: &Canvas) -> Result<bool, String> {
|
||||
let width = knock.width.max(1);
|
||||
let mut painted: Option<[i64; 4]> = None;
|
||||
for (k, _) in knock.px.iter().enumerate().filter(|(_, p)| p[3] > 0.0) {
|
||||
let (x, y) = (knock.left + (k % width) as i64, knock.top + (k / width) as i64);
|
||||
painted = Some(painted.map_or([x, y, x + 1, y + 1], |[l, t, r, b]| [l.min(x), t.min(y), r.max(x + 1), b.max(y + 1)]));
|
||||
}
|
||||
let Some([left, top, right, bottom]) = painted else { return Ok(true) };
|
||||
for u in below.iter().filter(|u| u.visible) {
|
||||
let [l, t, r, b] = match &u.body {
|
||||
Body::Merged(c) => [c.left, c.top, c.left + c.width as i64, c.top + c.height as i64],
|
||||
Body::Layer(i) => {
|
||||
let rec = self.record(*i)?;
|
||||
match rec.content {
|
||||
Content::Adjustment => continue,
|
||||
Content::Solid(Some(_)) if rec.color_over_canvas() => [0, 0, i64::from(self.h.width), i64::from(self.h.height)],
|
||||
_ => {
|
||||
let (w, h) = rec.bounds.size();
|
||||
let (l, t) = (i64::from(rec.bounds.left), i64::from(rec.bounds.top));
|
||||
[l, t, l + w as i64, t + h as i64]
|
||||
}
|
||||
}
|
||||
}
|
||||
Body::Group(..) => return Ok(false),
|
||||
};
|
||||
if l < right && left < r && t < bottom && top < b {
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// The bytes a merge step may still take: the memory allowed, less the copies of what is below
|
||||
/// the pass-through groups being merged.
|
||||
fn room(&self) -> u64 {
|
||||
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 +1630,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 +1651,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 +1692,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 +1743,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 +1757,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 +1769,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 +1883,71 @@ 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)
|
||||
}
|
||||
|
||||
/// 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.
|
||||
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 +1980,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)
|
||||
}
|
||||
|
||||
@@ -1521,11 +2018,28 @@ 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()],
|
||||
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),
|
||||
wasted: Cell::new(0),
|
||||
trial: Cell::new(0),
|
||||
painted: Cell::new(0),
|
||||
blank: vec![None; records.len()],
|
||||
laid_blank: vec![false; records.len()],
|
||||
blank_log: vec![],
|
||||
squeezed: 0,
|
||||
};
|
||||
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,453 @@ 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, 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]
|
||||
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, 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.clone(),
|
||||
L { hidden: true, ..layer("Inner", [0, 0, 2, 2], [0, 0, 255, 255]) },
|
||||
L { hidden: true, ..knockout(group("Cut", b"norm"), 1, 255) },
|
||||
group("Group", b"norm"),
|
||||
];
|
||||
let (s, r) = open(&file(4, 4, &layers, 8, false, false), json!({"hiddenLayers": true}));
|
||||
assert_eq!((inner(&s), &r["warnings"]), (vec!["Lower".to_string()], &json!([HIDDEN_1])), "{r}");
|
||||
// One whose layer mask hides all of it needs no merge, but a knockout above it merges the layers
|
||||
// and leaves it out: it is counted once, without its type layer.
|
||||
let layers = [
|
||||
group_end(),
|
||||
lower(255),
|
||||
group_end(),
|
||||
words,
|
||||
L { hidden: true, mask: Some(([0, 0, 2, 2], 0, 0, vec![0; 4])), ..knockout(group("Masked", b"norm"), 1, 255) },
|
||||
cut(0, 255),
|
||||
group("Group", b"norm"),
|
||||
];
|
||||
let (s, r) = open(&file(4, 4, &layers, 8, false, false), json!({"hiddenLayers": true}));
|
||||
assert_eq!((inner(&s), &r["warnings"]), (vec!["Lower + Cut".to_string()], &json!([HIDDEN_1])), "{r}");
|
||||
}
|
||||
|
||||
/// With hidden layers asked for, the hidden layers of a group drawn for a knockout or a clip that
|
||||
/// 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. 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 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!(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!(names(&doc), ["Lower + Cut"]);
|
||||
assert!(warnings.is_empty(), "{warnings:?}");
|
||||
for max_alloc in [128, 1 << 20] {
|
||||
let (doc, warnings) = psdlayers::import(&stack([2, 2, 4, 4]), false, max_alloc, cms).unwrap().unwrap();
|
||||
assert_eq!(names(&doc), ["Lower", "Cut"], "{max_alloc}");
|
||||
assert!(warnings.is_empty(), "{max_alloc}: {warnings:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A knockout that cuts nothing gives back what drawing the layers below it counted: here a
|
||||
/// knockout at 0 fill beside a group, whose own knockout doesn't fit 192 bytes while the outer
|
||||
/// knockout's canvas is held, but merges when the group comes in.
|
||||
#[test]
|
||||
fn a_knockout_that_cuts_nothing_doesnt_keep_what_drawing_below_it_counted() {
|
||||
let cms = |c: [f32; 4]| [c[0], c[1], c[2]];
|
||||
let layers = [
|
||||
group_end(),
|
||||
group_end(),
|
||||
layer("Opaque", [0, 0, 2, 2], [255, 0, 0, 255]),
|
||||
knockout(layer("Inner cut", [0, 0, 1, 2], [0, 0, 255, 255]), 1, 0),
|
||||
group("Inner", b"norm"),
|
||||
knockout(layer("Outer cut", [2, 2, 4, 4], [0, 0, 255, 255]), 1, 0),
|
||||
group("Outer", b"norm"),
|
||||
];
|
||||
let (doc, warnings) = psdlayers::import(&file(4, 4, &layers, 8, false, false), false, 192, cms).unwrap().unwrap();
|
||||
let outer = doc.layers[0].children().unwrap();
|
||||
assert_eq!(outer.iter().map(|c| name(c)).collect::<Vec<_>>(), ["Inner", "Outer cut"], "{warnings:?}");
|
||||
assert_eq!(outer[0].children().unwrap().iter().map(|c| name(c)).collect::<Vec<_>>(), ["Opaque + Inner cut"], "{warnings:?}");
|
||||
assert!(warnings.is_empty(), "{warnings:?}");
|
||||
}
|
||||
|
||||
/// A knockout merge that can't be made leaves the import the whole allowance for reading the layers
|
||||
/// apart: here the merge decodes 22 layers over one another before the layer beside them makes its
|
||||
/// canvas too large, which used up nearly all of eight times 64 KiB. The merge has that allowance
|
||||
/// also after five layers below the group were read, and a knockout at 0 fill over 22 transparent
|
||||
/// layers, which cuts no paint after they are drawn, leaves the same room.
|
||||
#[test]
|
||||
fn a_knockout_merge_not_made_leaves_room_to_read_the_layers() {
|
||||
let cms = |c: [f32; 4]| [c[0], c[1], c[2]];
|
||||
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"]);
|
||||
let mut after_five = vec![layer("Outside", [0, 0, 32, 32], [0, 0, 0, 255]); 5];
|
||||
after_five.extend(layers);
|
||||
let (doc, warnings) = psdlayers::import(&file(96, 32, &after_five, 8, false, false), false, 64 << 10, cms).unwrap().unwrap();
|
||||
assert_eq!((doc.layers.len(), doc.layers[5].children().unwrap().len()), (6, 24), "{warnings:?}");
|
||||
assert_eq!(warnings, ["the knockout of 1 layer wasn't applied"]);
|
||||
let mut layers = vec![group_end()];
|
||||
layers.extend((0..22).map(|_| layer("Lower", [0, 0, 32, 32], [255, 0, 0, 0])));
|
||||
layers.extend([knockout(layer("Cut", [0, 0, 32, 32], [0, 0, 255, 255]), 1, 0), group("Group", b"norm")]);
|
||||
let (doc, warnings) = psdlayers::import(&file(32, 32, &layers, 8, false, false), false, 64 << 10, cms).unwrap().unwrap();
|
||||
assert_eq!(doc.layers[0].children().unwrap().len(), 23, "{warnings:?}");
|
||||
assert!(warnings.is_empty(), "{warnings:?}");
|
||||
}
|
||||
|
||||
/// A hidden knockout in a mode VectorCraft lacks is left out without being drawn, hidden layers
|
||||
/// asked for: here its pixels are larger than the memory allowed for one image.
|
||||
#[test]
|
||||
fn a_hidden_knockout_in_a_mode_vectorcraft_lacks_is_left_out_unread() {
|
||||
let cms = |c: [f32; 4]| [c[0], c[1], c[2]];
|
||||
let layers = [
|
||||
group_end(),
|
||||
layer("Lower", [0, 0, 2, 2], [255, 0, 0, 255]),
|
||||
L { hidden: true, blend: b"diss", ..knockout(layer("Cut", [0, 0, 8, 8], [0, 0, 255, 255]), 1, 255) },
|
||||
group("Group", b"norm"),
|
||||
];
|
||||
let (doc, warnings) = psdlayers::import(&file(8, 8, &layers, 8, false, false), true, 128, cms).unwrap().unwrap();
|
||||
assert_eq!(doc.layers[0].children().unwrap().iter().map(|c| name(c)).collect::<Vec<_>>(), ["Lower"]);
|
||||
assert_eq!(warnings, [HIDDEN_1]);
|
||||
}
|
||||
|
||||
/// A Dissolve knockout at part opacity clears each pixel below it fully or not at all, at the share
|
||||
/// 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]);
|
||||
|
||||
Reference in new issue
Block a user