PSD import: a knockout that cuts nothing gives back the warnings of a merge it left short of memory

A visible knockout whose trial merge cut nothing kept the warnings that
drawing the layers below it recorded while its canvas was held. A merge
below it that only failed for want of that memory then came in merged,
with a warning that it hadn't been applied. Such a trial now gives back
what it counted, and the layers count again when they come in.

The PSD catalog test compares the whole numbers of each warning and its
translation, and checks that each complete catalog puts the translated
reason inside the warnings that carry one.
This commit is contained in:
dotCooCoo
2026-11-18 18:16:47 -08:00
parent 762182367c
commit 67c446b313
3 changed files with 40 additions and 4 deletions
+11
View File
@@ -863,6 +863,8 @@ struct Builder<'a, F> {
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> {
@@ -1274,6 +1276,7 @@ impl<F: Fn([f32; 4]) -> [f32; 3]> Builder<'_, F> {
/// Count record `i` as a clipped layer left unclipped (once).
fn unclip(&mut self, i: usize) {
self.squeezed += 1;
if let Some(s) = self.unclipped.get_mut(i)
&& !*s
{
@@ -1305,6 +1308,7 @@ impl<F: Fn([f32; 4]) -> [f32; 3]> Builder<'_, F> {
if self.knocks(&u, &out, level)? {
let saved = self.saved();
let noted = self.noted_so_far();
let squeezed = self.squeezed;
let outer = self.try_from_here();
let knocked = self.knock(&out, &u)?;
self.tried(outer, u.visible && matches!(knocked, Knocked::Merged(_)))?;
@@ -1334,9 +1338,14 @@ impl<F: Fn([f32; 4]) -> [f32; 3]> Builder<'_, F> {
self.restore(saved)?;
self.unnote(noted);
}
// A merge below it that its held canvas left without the memory it needed may
// be made when they come in without it: what drawing them counted is given back
// and counted again then.
Knocked::Nothing if self.squeezed != squeezed => self.restore(saved)?,
Knocked::Nothing => self.refund(saved.spent)?,
Knocked::TooLarge => {
self.restore(saved)?;
self.squeezed += 1;
self.unknock(u.record);
}
}
@@ -1382,6 +1391,7 @@ impl<F: Fn([f32; 4]) -> [f32; 3]> Builder<'_, F> {
}
} else {
self.restore(saved)?;
self.squeezed += 1;
self.notes.unmerged += 1;
u.blend = Blend::Mode(BlendMode::Normal);
out.push(u);
@@ -2025,6 +2035,7 @@ pub(super) fn import(
blank: vec![None; records.len()],
laid_blank: vec![false; records.len()],
blank_log: vec![],
squeezed: 0,
};
let layers = b.list(&items, Level::Top)?;
// A drawing for a knockout or a clip leaves hidden layers out; those that came in anyway aren't.
@@ -890,6 +890,28 @@ fn a_knockout_merge_past_the_memory_allowed_leaves_the_layers_apart() {
}
}
/// A knockout that cuts nothing gives back what drawing the layers below it counted: here a
/// knockout at 0 fill beside a group, whose own knockout doesn't fit 192 bytes while the outer
/// knockout's canvas is held, but merges when the group comes in.
#[test]
fn a_knockout_that_cuts_nothing_doesnt_keep_what_drawing_below_it_counted() {
let cms = |c: [f32; 4]| [c[0], c[1], c[2]];
let layers = [
group_end(),
group_end(),
layer("Opaque", [0, 0, 2, 2], [255, 0, 0, 255]),
knockout(layer("Inner cut", [0, 0, 1, 2], [0, 0, 255, 255]), 1, 0),
group("Inner", b"norm"),
knockout(layer("Outer cut", [2, 2, 4, 4], [0, 0, 255, 255]), 1, 0),
group("Outer", b"norm"),
];
let (doc, warnings) = psdlayers::import(&file(4, 4, &layers, 8, false, false), false, 192, cms).unwrap().unwrap();
let outer = doc.layers[0].children().unwrap();
assert_eq!(outer.iter().map(|c| name(c)).collect::<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
+7 -4
View File
@@ -625,12 +625,15 @@ fn psd_import_strings_are_in_complete_catalogs() {
for s in &shown {
let tr = message(lang, s);
assert_ne!(&tr, s, "{code}");
for n in numbers(s) {
assert!(tr.contains(&n), "{code}: {tr} lost {n}");
}
let (mut source, mut translated) = (numbers(s), numbers(&tr));
source.sort();
translated.sort();
assert_eq!(source, translated, "{code}: {tr}");
}
for (s, inner) in [(unread_slices, nested), (unread_layers, damaged)] {
assert!(!message(lang, s).contains(inner), "{code}: {s}");
let reason = message(lang, inner);
assert_ne!(reason, inner, "{code}");
assert!(message(lang, s).contains(&reason), "{code}: {s}");
}
assert!(message(lang, &merged).contains("Hue, Luminosity"), "{code}");
assert!(message(lang, &opened_with_notes(1, &one)).ends_with(&message(lang, &one)), "{code}");