Files
craft-libs/crates/psd/src/lazy.rs
T
f462bb8128 craft-psd: deferred global blocks and a merged-image preview (ported from PhotoCraft) (#1037)
* craft-psd: Photoshop PSD/PSB reader and writer, moved from PhotoCraft

PhotoCraft's `photocraft-psd` crate (crates/psd at storytold/photocraft
63b641b55e54bf8f5c83efd977fdb16a2c255431) as `craft-psd`: the lossless
PSD/PSB model, reader, writer and builder, descriptors, the Photoshop
resource files that share its primitives (.abr, .asl, .aco, .act, .ase,
.grd, .pat, .atn, .csh, .shc) and the Photoshop blocks of a layered TIFF.
The crate had no dependency on any other PhotoCraft crate.

Code is unchanged apart from the crate name, craft-libs' rustfmt width,
comments that named other PhotoCraft crates, issue references (now
`photocraft#N`), and the corpus tests' directory (`CRAFT_PSD_CORPUS` or
`corpus/` at the workspace root). Tests, proptests, fuzz targets and
fixtures move with it.

Part of #778, #786.

* craft-psd: engine_data, the text engine document of type layers

The EngineData reader and writer from PhotoCraft's photocraft-text
(crates/text/src/engine_data.rs at storytold/photocraft 63b641b5): a
tree of values that is written back in Photoshop's layout, with strings
stored as plain bytes kept as plain bytes.

From EffectCraft's effectcraft-psd (crates/psd/src/engine_data.rs at
storytold/effectcraft 072e1af9), ported onto that tree: `first_style`,
the first style run as a typed `TextStyle` with bounded sizes and
spacings, and `minimal`, a one-run document for writers and fixtures.
`Value::index` is new.

Both apps had their own parser for the same syntax.

* craft-psd: engine_data::parse_lossy reads what it can of a damaged document

EffectCraft's EngineData reader was best effort: a truncated or damaged
document still gave the text and style it held. `parse_lossy` does that
on the same parser: a truncated dictionary, array or string keeps what
it has, a byte that starts no key is skipped, a token that is no number
becomes a name, and garbage gives an empty dictionary. `parse` stays
strict and is unchanged in what it accepts.

The parser's `value` is split into one function per kind of value, and
reads with `get` instead of indexing.

* craft-psd: typed views of type layers, smart objects and adjustments

Read-only views EffectCraft's effectcraft-psd had over the blocks this
crate keeps raw (storytold/effectcraft f334bcf3, crates/psd/src/parse.rs
and placed.rs). The blocks stay raw and write back unchanged.

- `text`: `TypeLayer` from `TySh`: text, transform (the identity when not
  finite), bounds, the first style run (read with
  `engine_data::parse_lossy`) and the text descriptor.
  `LayerRecord::type_layer`.
- `smart`: `SmartObject` from `SoLd`/`SoLE` (id, quad, non-affine
  transform, size, warp) and `PlLd`; `Warp` with the named styles and the
  custom quilt mesh; the quad geometry (`map`, `placed_bounds`,
  `placed_size`, `bake_scale`, `placement`). `LinkedFile` and
  `linked_files` for `lnk2`/`lnkD`/`lnk3`/`lnkE`, `PsdFile::linked_files`
  and `PsdFile::linked_data`. `LayerRecord::smart_object`.
- `adjust`: `Adjustment` from `nvrt`, `brit`, `CgEd`, `hue2`/`hue `,
  `levl`, `expA`, and the other adjustment keys by name;
  `LayerRecord::adjustment`, `solid_color` (`SoCo`) and `effects` (`lfx2`).
- `Descriptor`: `object`, `number`, `numbers`, `boolean`, `text`,
  `enumerated`, `raw_data`, `color`; `Value::as_number`.

Linked-file data is a range of the block's data, checked against the
record, instead of a range of the whole file.

* craft-psd: shared descriptor lookups

Descriptor::get_bool, get_number, get_text, get_enum and get_object,
used by asl, grd and slices in place of their own copies.

Ported from storytold/photocraft#9552 (its crates/psd part).

* craft-psd: the typed views use the shared descriptor lookups

One set of Descriptor accessors: the get_* lookups from photocraft#9552.
numbers, raw_data and color move next to them as get_numbers,
get_raw_data and get_color.

* craft-psd: global blocks can stay in the source, and a preview of the merged image

- `PsdFile::from_source_deferring` leaves the data of each global block a
  callback picks in the source (`TaggedBlock::stored`), so an embedded
  smart-object file that is most of a PSD is not read on open.
  `TaggedBlock::encoded`, `encoded_data_len` and `load` give the data
  wherever it is; writing copies it from the source; equality compares the
  bytes, and the same range of the same source without reading it.
  `parsed` refuses a block still in the source.
- `FileSource` fails a read after the file changed in place (length or
  modification time), instead of mixing old offsets with new bytes.
- `PsdFile::merged_preview(max_side)` samples every k-th row and pixel of
  a raw or RLE RGB/gray merged image, 8/16/32-bit (32-bit encoded to sRGB),
  reading a few megabytes of a large file. `ImageData::row_decoder` decodes
  the merged image's rows on their own.

Ported from storytold/photocraft#11586, which contains #6589 and #6616's
crates/psd changes (photocraft#5106).

* craft-psd: linked files read deferred blocks, and the preview size is capped

- `PsdFile::linked_files` and `linked_data` read a linked-layer block
  through `TaggedBlock::encoded`, so a block left in the source is found;
  they return `Result`, and `linked_data` a `Cow` borrowed from a block in
  memory.
- `merged_preview` refuses an output over `MAX_DECODED_BYTES` before it
  decodes or allocates (the size is the caller's), and a failed allocation
  is `OutOfMemory`.

---------

Co-authored-by: Omar Saidahmed <osaidahm@uwaterloo.ca>
Co-authored-by: Phil Trinh <ptrinh@me.com>
2026-11-11 20:56:55 -05:00

539 lines
23 KiB
Rust

//! Parsing from a [`Source`] without reading the whole file: [`PsdFile::from_source`].
//!
//! The structure (header, resources, layer records, global blocks) is read into memory as
//! [`PsdFile::from_bytes`] would hold it. The bulk of a layered file is left in the source: every
//! channel's encoded bytes and the merged image become [`Stored`] ranges, read when they are
//! decoded. Apart from those ranges, the model is the one `from_bytes` builds from the same bytes
//! (the parse tests hold the two to that), with one difference: a merged image the caller does
//! not ask for is not read, so it is not checked either.
use crate::compression::Compression;
use crate::error::{PsdError, Result};
use crate::file::{GlobalLayerMask, LayerInfoPlacement, PsdFile};
use crate::header::{Header, Version};
use crate::image_data::ImageData;
use crate::io::Reader;
use crate::layer::{LayerInfo, LayerRecord};
use crate::resources::ImageResource;
use crate::source::{Source, Stored};
use crate::tagged::{TaggedBlock, is_sig, uses_long_length};
/// Bytes read per attempt at the layer records; doubled until they all fit. (Small in tests, so
/// they grow it.)
const RECORDS_CHUNK: u64 = if cfg!(test) { 64 } else { 1 << 20 };
/// A bounds-checked cursor over `start..end` of a source.
struct Cursor<'a> {
src: &'a Source,
pos: u64,
end: u64,
}
impl<'a> Cursor<'a> {
fn new(src: &'a Source) -> Self {
Cursor { src, pos: 0, end: src.get().len() }
}
fn remaining(&self) -> u64 {
self.end.saturating_sub(self.pos)
}
fn is_empty(&self) -> bool {
self.remaining() == 0
}
fn eof(&self, needed: u64) -> PsdError {
PsdError::UnexpectedEof {
offset: usize::try_from(self.pos).unwrap_or(usize::MAX),
needed: usize::try_from(needed.saturating_sub(self.remaining())).unwrap_or(usize::MAX),
}
}
fn need(&self, n: u64) -> Result<()> {
if n > self.remaining() { Err(self.eof(n)) } else { Ok(()) }
}
/// Reads `n` bytes.
fn bytes(&mut self, n: u64) -> Result<Vec<u8>> {
self.need(n)?;
let v = self.src.read_vec(self.pos, n)?;
self.pos += n;
Ok(v)
}
/// Up to `n` bytes from the current position, without consuming.
fn peek(&self, n: u64) -> Result<Vec<u8>> {
self.src.read_vec(self.pos, n.min(self.remaining()))
}
fn array<const N: usize>(&mut self) -> Result<[u8; N]> {
self.need(N as u64)?;
let mut b = [0u8; N];
self.src.get().read_at(self.pos, &mut b)?;
self.pos += N as u64;
Ok(b)
}
fn u16(&mut self) -> Result<u16> {
self.array().map(u16::from_be_bytes)
}
fn i16(&mut self) -> Result<i16> {
self.array().map(i16::from_be_bytes)
}
fn u32(&mut self) -> Result<u32> {
self.array().map(u32::from_be_bytes)
}
fn len_field(&mut self, long: bool) -> Result<u64> {
if long { self.array().map(u64::from_be_bytes) } else { self.u32().map(u64::from) }
}
/// The next `n` bytes as a cursor of their own; this one moves past them.
fn sub(&mut self, n: u64) -> Result<Cursor<'a>> {
self.need(n)?;
let sub = Cursor { src: self.src, pos: self.pos, end: self.pos + n };
self.pos += n;
Ok(sub)
}
fn skip(&mut self, n: u64) -> Result<()> {
self.need(n)?;
self.pos += n;
Ok(())
}
fn rest(&mut self) -> Result<Vec<u8>> {
self.bytes(self.remaining())
}
/// The rest as a stored range; the cursor moves to the end.
fn stored_rest(&mut self) -> Stored {
let s = Stored { source: self.src.clone(), offset: self.pos, len: self.remaining() };
self.pos = self.end;
s
}
}
impl PsdFile {
/// Parses a file from `source`, leaving channel data and the merged image in it: see
/// [`crate::source`]. Decoding a channel then reads its bytes from the source, which must
/// stay unchanged while the model is in use.
pub fn from_source(source: Source) -> Result<Self> {
Self::from_source_and_merged_recovering(source, |_| false, |_| false).map(|(file, _)| file)
}
/// [`Self::from_bytes_and_merged_recovering`] for a [`Source`]: the merged image is read only
/// when `want` asks for it (and is then decoded as there, with the same recovery); otherwise
/// it stays in the source, unread and unchecked.
pub fn from_source_and_merged_recovering(
source: Source,
want: impl FnOnce(&PsdFile) -> bool,
recover: impl FnOnce(&PsdFile) -> bool,
) -> Result<(Self, Option<Result<Vec<u8>>>)> {
Self::from_source_and_merged_preflight(source, |file| Ok(want(file)), recover)
}
/// Parses with a fallible pixel preflight after layer records are available and before
/// the merged image is read, validated or decoded. A preflight error is always fatal;
/// `recover` only handles damage encountered after an accepted preflight.
pub fn from_source_and_merged_preflight(
source: Source,
want: impl FnOnce(&PsdFile) -> Result<bool>,
recover: impl FnOnce(&PsdFile) -> bool,
) -> Result<(Self, Option<Result<Vec<u8>>>)> {
Self::from_source_and_merged_preflight_info(source, |file, _, _| want(file), recover)
}
/// [`Self::from_bytes_and_merged_preflight_info`] for a [`Source`]. Only the two-byte
/// compression field is inspected before preflight, never the encoded pixels. A missing
/// field or failed read is reported as `None`; a read error is returned after preflight
/// unless the callback refuses the import first.
pub fn from_source_and_merged_preflight_info(
source: Source,
want: impl FnOnce(&PsdFile, Option<Compression>, u64) -> Result<bool>,
recover: impl FnOnce(&PsdFile) -> bool,
) -> Result<(Self, Option<Result<Vec<u8>>>)> {
Self::from_source_deferring_info(source, |_, _| false, want, recover)
}
/// [`Self::from_source_and_merged_preflight`] that also leaves the data of each global block
/// `defer(key, length)` accepts in the source ([`TaggedBlock::stored`]): an embedded
/// smart-object block (`lnk2`) can be most of a file. Defer only blocks the reader loads
/// before use (the linked-file blocks `lnk2`/`lnk3`/`lnkD`): a deferred block's `data` is
/// empty, and [`TaggedBlock::parsed`] refuses it.
pub fn from_source_deferring(
source: Source,
defer: impl Fn(&[u8; 4], u64) -> bool,
want: impl FnOnce(&PsdFile) -> Result<bool>,
recover: impl FnOnce(&PsdFile) -> bool,
) -> Result<(Self, Option<Result<Vec<u8>>>)> {
Self::from_source_deferring_info(source, defer, |file, _, _| want(file), recover)
}
/// [`Self::from_source_deferring`] whose preflight also sees the merged image's compression
/// and encoded length, as [`Self::from_source_and_merged_preflight_info`] gives them.
pub fn from_source_deferring_info(
source: Source,
defer: impl Fn(&[u8; 4], u64) -> bool,
want: impl FnOnce(&PsdFile, Option<Compression>, u64) -> Result<bool>,
recover: impl FnOnce(&PsdFile) -> bool,
) -> Result<(Self, Option<Result<Vec<u8>>>)> {
let mut c = Cursor::new(&source);
let header = Header::read(&mut Reader::new(&c.bytes(26)?))?;
let version = header.version;
let psb = version.is_psb();
let cmd_len = c.u32()?;
let color_mode_data = c.bytes(u64::from(cmd_len))?;
let res_len = c.u32()?;
let res = c.bytes(u64::from(res_len))?;
let mut rr = Reader::new(&res);
let mut resources = Vec::new();
while !rr.is_empty() {
resources.push(ImageResource::read(&mut rr)?);
}
let lm_len = c.len_field(psb)?;
let mut s = c.sub(lm_len)?;
let mut layer_info = None;
let mut global_layer_mask = None;
let mut global_blocks = Vec::new();
let mut layer_mask_trailing = Vec::new();
let mut layer_info_placement = LayerInfoPlacement::Section;
if !s.is_empty() {
let li_len = s.len_field(psb)?;
if li_len > 0 {
layer_info = Some(read_layer_info(s.sub(li_len)?, version)?);
}
if s.remaining() >= 4 {
let n = s.u32()?;
global_layer_mask = Some(GlobalLayerMask { data: s.bytes(u64::from(n))? });
let target_key = match header.depth {
16 => Some(b"Lr16"),
32 => Some(b"Lr32"),
_ => None,
};
let mut candidate_seen = match target_key {
Some(_) => false,
None => layer_info.as_ref().is_some_and(|i: &LayerInfo| !i.layers.is_empty()),
};
let trailing = read_blocks(
&mut s,
version,
|index, signature, key, data, padding| {
let is_candidate = match target_key {
Some(k) => &key == k,
None => matches!(&key, b"Layr" | b"Lr16" | b"Lr32"),
};
if candidate_seen || !is_candidate {
return Ok(None);
}
candidate_seen = true;
let info = match read_layer_info(data, version) {
Ok(info) => info,
// A failed read of the source is not damage: fail the parse, as
// reading the whole file would.
Err(e @ PsdError::Read(_)) => return Err(e),
Err(_) => {
if target_key.is_some() {
layer_info = None;
}
return Ok(None);
}
};
layer_info = Some(info);
layer_info_placement = LayerInfoPlacement::GlobalBlock { index, signature, key, padding: padding.clone() };
Ok(Some(()))
},
&defer,
&mut global_blocks,
)?;
layer_mask_trailing = trailing;
} else {
layer_mask_trailing = s.rest()?;
}
}
let mut file = PsdFile {
header,
color_mode_data,
resources,
layer_info,
layer_info_placement,
global_layer_mask,
global_blocks,
layer_mask_trailing,
image_data: ImageData::new(Compression::Raw, Vec::new()),
write_as_psb: false,
};
let compression = c.peek(2).map(|bytes| Reader::new(&bytes).u16().ok().map(Compression::from_u16));
let want = want(&file, compression.as_ref().ok().copied().flatten(), c.remaining().saturating_sub(2))?;
compression?;
if !want {
let compression = Compression::from_u16(c.u16()?);
file.image_data = ImageData { compression, data: Vec::new(), stored: Some(c.stored_rest()) };
return Ok((file, None));
}
// The merged image is wanted: read it and decode it as `from_bytes` does.
let rest = c.rest()?;
let merged = file.read_merged_recovering(&rest, recover)?;
Ok((file, merged))
}
}
/// Reads tagged blocks as [`crate::tagged::read_blocks_with`] does, from a cursor: `consume`
/// sees each block's data as a cursor and returns `Some` when it took the block; the others are
/// read into memory and pushed to `blocks`. Returns the trailing bytes.
fn read_blocks<'a>(
r: &mut Cursor<'a>,
version: Version,
mut consume: impl FnMut(usize, [u8; 4], [u8; 4], Cursor<'a>, &Option<Vec<u8>>) -> Result<Option<()>>,
defer: &dyn Fn(&[u8; 4], u64) -> bool,
blocks: &mut Vec<TaggedBlock>,
) -> Result<Vec<u8>> {
const ALIGNMENT: usize = 4;
loop {
let head = r.peek(12)?;
if head.len() < 12 || !is_sig(&head) {
return r.rest();
}
let signature = r.array::<4>()?;
let key = r.array::<4>()?;
let len = r.len_field(uses_long_length(version, &key))?;
let data = r.sub(len)?;
// Padding as `read_blocks_with` detects it: the smallest k in 0..=3 such that the next
// signature (or the end of the region) follows k bytes later.
let rest = r.peek(7)?;
let mut pad = None;
for k in 0..=3usize {
if k == rest.len() || rest.get(k..).is_some_and(is_sig) {
pad = Some(k);
break;
}
}
let default = TaggedBlock::default_padding((len % ALIGNMENT as u64) as usize, ALIGNMENT);
let pad = pad.unwrap_or_else(|| if rest.get(..default).is_some_and(|p| p.iter().all(|&b| b == 0)) { default } else { 0 });
let pad_bytes = rest.get(..pad).map(<[u8]>::to_vec).unwrap_or_default();
r.skip(pad as u64)?;
let padding = if pad_bytes.len() == default && pad_bytes.iter().all(|&b| b == 0) { None } else { Some(pad_bytes) };
let (start, end) = (data.pos, data.end);
if consume(blocks.len(), signature, key, data, &padding)?.is_none() {
if defer(&key, end - start) {
let stored = Stored { source: r.src.clone(), offset: start, len: end - start };
blocks.push(TaggedBlock { signature, key, data: Vec::new(), padding, stored: Some(stored) });
} else {
let data = Cursor { src: r.src, pos: start, end }.rest()?;
blocks.push(TaggedBlock { signature, key, data, padding, stored: None });
}
}
}
}
/// [`LayerInfo::read_body`] from a cursor over the body: the records are read into memory, and
/// each channel's encoded bytes become a [`Stored`] range after its compression field.
fn read_layer_info(mut r: Cursor<'_>, version: Version) -> Result<LayerInfo> {
let body_start = r.pos;
let count = r.i16()?;
let merged_alpha = count < 0;
let n = count.unsigned_abs();
// Minimum record size, as `read_body` checks it.
if u64::from(n) * 34 > r.remaining() {
return Err(r.eof(u64::from(n) * 34));
}
// The records' total size is only known once they are parsed: read a chunk and grow it until
// they all fit (or the body is exhausted, when their error is the body's).
let mut chunk = RECORDS_CHUNK.min(r.remaining());
let (layers, all_lens, used) = loop {
let buf = r.peek(chunk)?;
let mut rr = Reader::new(&buf);
let mut layers = Vec::with_capacity(usize::from(n));
let mut all_lens = Vec::with_capacity(usize::from(n));
let parsed = (0..n).try_for_each(|_| {
let (rec, lens) = LayerRecord::read(&mut rr, version)?;
layers.push(rec);
all_lens.push(lens);
Ok::<_, PsdError>(())
});
match parsed {
Ok(()) => break (layers, all_lens, rr.pos() as u64),
Err(PsdError::UnexpectedEof { .. }) if chunk < r.remaining() => chunk = chunk.saturating_mul(2).min(r.remaining()),
Err(e) => return Err(e),
}
};
r.skip(used)?;
let mut layers = layers;
for (rec, lens) in layers.iter_mut().zip(all_lens) {
for (ch, len) in rec.channels.iter_mut().zip(lens) {
if len == 0 {
continue;
}
if len == 1 {
return Err(PsdError::invalid("channel data length 1"));
}
ch.compression = Some(Compression::from_u16(r.u16()?));
let data = r.sub(len - 2)?;
ch.stored = Some(Stored { source: data.src.clone(), offset: data.pos, len: data.remaining() });
}
}
let body_len = r.pos - body_start;
let rest = r.rest()?;
let padding = if rest.len() as u64 == body_len % 2 && rest.iter().all(|&b| b == 0) { None } else { Some(rest) };
Ok(LayerInfo { merged_alpha, layers, padding })
}
#[cfg(test)]
mod tests {
use super::*;
use crate::testgen;
/// Every generated file, plus the layered 16/32-bit ones with their layers in a global block.
fn samples() -> Vec<Vec<u8>> {
let mut v = Vec::new();
for case in testgen::all_cases() {
let mut file = case.file;
v.push(file.to_bytes().unwrap());
let key = match file.header.depth {
16 => *b"Lr16",
32 => *b"Lr32",
_ => continue,
};
if file.layer_info.as_ref().is_some_and(|i| !i.layers.is_empty()) {
file.global_layer_mask.get_or_insert_with(GlobalLayerMask::default);
file.layer_info_placement = LayerInfoPlacement::GlobalBlock { index: 0, signature: *b"8BIM", key, padding: None };
v.push(file.to_bytes().unwrap());
}
}
v
}
/// The preflight sees the merged image's compression and encoded length, from bytes and from
/// a source alike, before the image is read.
#[test]
fn preflight_info_reports_the_merged_compression_and_length() {
for c in Compression::ALL {
let file = testgen::small(Version::Psd, c);
let bytes = file.to_bytes().unwrap();
let want = Some((Some(c), file.image_data.encoded_len()));
let mut eager = None;
PsdFile::from_bytes_and_merged_preflight_info(&bytes, |_, comp, len| Ok(eager.replace((comp, len)).is_some()), |_| false).unwrap();
let mut lazy = None;
PsdFile::from_source_and_merged_preflight_info(Source::new(bytes), |_, comp, len| Ok(lazy.replace((comp, len)).is_some()), |_| false)
.unwrap();
assert_eq!((eager, lazy), (want, want), "{c:?}");
}
// No compression field: the preflight sees `None`, and its refusal wins over the missing field.
let file = testgen::small(Version::Psd, Compression::Raw);
let bytes = file.to_bytes().unwrap();
let cut = &bytes[..bytes.len() - file.image_data.encoded_len() as usize - 2];
let refuse = |_: &PsdFile, comp: Option<Compression>, _: u64| {
assert_eq!(comp, None);
Err(PsdError::LimitExceeded("refused"))
};
assert_eq!(PsdFile::from_bytes_and_merged_preflight_info(cut, refuse, |_| false).unwrap_err(), PsdError::LimitExceeded("refused"));
assert_eq!(
PsdFile::from_source_and_merged_preflight_info(Source::new(cut.to_vec()), refuse, |_| false).unwrap_err(),
PsdError::LimitExceeded("refused")
);
// Accepted, the missing field is the error it always was.
assert!(PsdFile::from_source_and_merged_preflight_info(Source::new(cut.to_vec()), |_, _, _| Ok(true), |_| false).is_err());
}
fn same(bytes: Vec<u8>) {
let eager = PsdFile::from_bytes(&bytes).unwrap();
let lazy = PsdFile::from_source(Source::new(bytes.clone())).unwrap();
assert_eq!(lazy, eager);
assert_eq!(lazy.to_bytes().unwrap(), bytes);
assert!(lazy.layers().iter().flat_map(|l| &l.channels).all(|c| c.data.is_empty()));
assert!(lazy.image_data.data.is_empty() && lazy.image_data.stored.is_some());
}
#[test]
fn lazy_parse_matches_the_eager_one() {
for bytes in samples() {
same(bytes);
}
}
/// A PSD read lazily and written as PSB widens the RLE count tables of channels still in the
/// source, byte for byte as an eager read does (main's PSB promotion, photocraft#5285, on photocraft#6583's reader).
#[test]
fn a_lazy_psd_promotes_to_the_same_psb_as_an_eager_one() {
let bytes = testgen::layered(Version::Psd, crate::ColorMode::Rgb, 8, Compression::Rle).to_bytes().unwrap();
let mut eager = PsdFile::from_bytes(&bytes).unwrap();
let mut lazy = PsdFile::from_source(Source::new(bytes)).unwrap();
eager.promote_to_psb().unwrap();
lazy.promote_to_psb().unwrap();
let written = eager.to_bytes().unwrap();
assert_eq!(lazy.encoded_len().unwrap(), written.len() as u64);
assert!(lazy.to_bytes().unwrap() == written, "the lazily read PSD writes another PSB");
}
#[test]
fn deferred_blocks_read_back_the_same() {
for bytes in samples() {
let eager = PsdFile::from_bytes(&bytes).unwrap();
let (lazy, _) = PsdFile::from_source_deferring(Source::new(bytes.clone()), |_, _| true, |_| Ok(false), |_| false).unwrap();
assert!(lazy.global_blocks.iter().all(|b| b.data.is_empty() && b.stored.is_some()));
assert_eq!(lazy, eager);
assert_eq!(lazy.to_bytes().unwrap(), bytes);
}
}
#[test]
fn lazy_parse_fails_where_the_eager_one_does() {
for bytes in samples() {
for cut in [1, 2, 7, 26, 40, bytes.len() / 3, bytes.len() / 2, bytes.len() - 3] {
let Some(b) = bytes.get(..cut) else { continue };
let eager = PsdFile::from_bytes(b);
let lazy = PsdFile::from_source(Source::new(b.to_vec()));
// A truncated merged image is only noticed when it is read.
match eager {
Ok(eager) => assert_eq!(lazy.unwrap(), eager, "cut at {cut}"),
Err(e) => {
let records_ok = PsdFile::from_bytes_and_merged_recovering(b, |_| false, |_| true).is_ok();
assert!(lazy.is_err() || records_ok, "cut at {cut}: eager {e:?}");
}
}
}
}
}
/// A source whose `fail_at`-th read fails once.
struct FailOnce {
bytes: Vec<u8>,
fail_at: usize,
reads: std::sync::atomic::AtomicUsize,
}
impl crate::source::ReadAt for FailOnce {
fn len(&self) -> u64 {
self.bytes.len() as u64
}
fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<()> {
if self.reads.fetch_add(1, std::sync::atomic::Ordering::SeqCst) == self.fail_at {
return Err(PsdError::Read("injected".into()));
}
self.bytes.read_at(offset, buf)
}
}
#[test]
fn a_failed_read_fails_the_parse_instead_of_dropping_the_layers() {
for bytes in samples() {
let eager = PsdFile::from_bytes(&bytes).unwrap();
let clean = std::sync::Arc::new(FailOnce { bytes: bytes.clone(), fail_at: usize::MAX, reads: 0.into() });
PsdFile::from_source(Source::from_arc(clean.clone())).unwrap();
let reads = clean.reads.load(std::sync::atomic::Ordering::SeqCst);
for fail_at in 0..reads {
let src = FailOnce { bytes: bytes.clone(), fail_at, reads: 0.into() };
match PsdFile::from_source(Source::new(src)) {
Err(PsdError::Read(_)) => {}
Ok(lazy) => assert_eq!(lazy, eager, "read {fail_at} failed silently"),
Err(e) => panic!("read {fail_at}: {e:?}"),
}
}
}
}
}