Files
photocraft/crates/psd/src/error.rs
T
Phil TrinhandClaude Opus 5.5 4915f0970e PSD/PSB open without reading the whole file: channel data read as it is decoded, in bands straight into tiles (#5106)
Opening a 2.14 GB PSD (2000×2000, 109 layers, 821 MB of embedded smart objects) peaked at
7.28 GB and took 6.8 s; it now peaks at 3.00 GB and takes 3.6 s (macOS peak memory footprint,
`cargo run --release -p photocraft-io --example psd_open_mem`).

- photocraft-psd: `PsdFile::from_source` parses from a `ReadAt` source (`FileSource`: pread /
  seek_read in bounded reads). Channel data and the merged image become `Stored` ranges read
  when decoded; the model equals `from_bytes`'s and writes back the same bytes (tests compare
  the two on every generated file, including Lr16/Lr32 layer blocks and truncated files).
- `ChannelData::row_decoder` decodes any band of rows: RLE via its row table (a stored channel
  reads only the band's bytes), raw by offset, ZIP inflated once.
- photocraft-io: `import_file` opens a path; PSD/PSB through `from_source`, other formats read
  whole. Layers decode in bands of whole tile rows (~32 MB) into tiles: no whole-layer plane or
  interleaved buffer. Global blocks move into the document instead of being copied; finding an
  embedded smart object no longer copies every file in `lnk2`.
- photocraft-raster: `Surface::share_uniform_tiles` + `TileInterner`; the PSD import shares
  single-value tiles (backgrounds, fill caches, masks) across layers.
- The background open job and the CLI/MCP (`automation::files::open`) use `import_file`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 02:37:20 +08:00

56 lines
1.9 KiB
Rust

//! Error type for every fallible operation in this crate.
use thiserror::Error;
/// Errors produced while parsing, decoding, encoding or writing PSD/PSB data.
///
/// Parsers never panic on malformed input; every failure is reported through
/// this type.
#[derive(Debug, Error, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum PsdError {
/// The input ended before a structure was complete.
#[error("unexpected end of data at offset {offset} (needed {needed} more bytes)")]
UnexpectedEof {
/// Offset (relative to the current section) where the read started.
offset: usize,
/// Number of bytes that were requested but not available.
needed: usize,
},
/// A four-byte signature did not match.
#[error("invalid signature {found:?}, expected {expected}")]
InvalidSignature {
/// Human-readable description of the expected signature(s).
expected: &'static str,
/// The bytes that were found.
found: [u8; 4],
},
/// Header version is neither 1 (PSD) nor 2 (PSB).
#[error("unsupported file version {0}")]
UnsupportedVersion(u16),
/// A dimension, count or allocation exceeded the configured safety limits.
#[error("limit exceeded: {0}")]
LimitExceeded(&'static str),
/// Structurally invalid data.
#[error("invalid data: {0}")]
Invalid(String),
/// A compressed stream could not be decoded.
#[error("decompression failed: {0}")]
Decompress(String),
/// Reading from a [`crate::source::ReadAt`] source failed.
#[error("read failed: {0}")]
Read(String),
/// An operation is not supported for this data (e.g. unknown compression).
#[error("unsupported: {0}")]
Unsupported(String),
}
impl PsdError {
pub(crate) fn invalid(msg: impl Into<String>) -> Self {
PsdError::Invalid(msg.into())
}
}
/// Convenience alias.
pub type Result<T, E = PsdError> = std::result::Result<T, E>;