psd: read the document structure with craft_psd

Psd::parse reads the header, colour mode data, layer records with their
additional information and the global blocks with
craft_psd::PsdFile::from_source. Channel data and the merged image stay in
the document as ranges, and the linked-file blocks are left in the source.
A damaged layer section still opens as the merged image. The local 8BPS
section, record and block walkers are removed (storytold/craft-libs#786).
This commit is contained in:
FredApps committed 2026-11-16 11:33:33 +01:00
1 parent 3d84810b43
commit 1cc80e7443
6 files changed
+174 -188

No files matched your search

+3
View File
@@ -12,3 +12,6 @@ workspace = true
[dependencies]
craft-psd = { workspace = true }
miniz_oxide = "0.9"
[dev-dependencies]
craft-psd = { workspace = true, features = ["testgen"] }
+2 -2
View File
@@ -1,8 +1,8 @@
//! Photoshop document (PSD / PSB) reader, implemented from Adobe's public *Photoshop File Formats
//! Specification* (see README.md), and a minimal writer ([`mod@write`]) for fixtures.
//!
//! [`Psd::parse`] reads the structure (header, layer records and their additional information,
//! the layer tree) without decoding pixels; [`Psd::composite`] decodes the merged image and
//! [`Psd::parse`] reads the structure with `craft_psd` (header, layer records and their
//! additional information, the layer tree) without decoding pixels; [`Psd::composite`] decodes the merged image and
//! [`Psd::layer_pixels`] one layer (with its layer mask applied to alpha) on demand. Pixels come
//! out as straight (unpremultiplied) RGBA `f32` in 0..1, display-encoded (32-bit documents,
//! which are linear, are encoded to sRGB).
+106 -185
View File
@@ -11,102 +11,106 @@ pub fn is_psd(bytes: &[u8]) -> bool {
}
impl Psd {
/// Reads the structure with [`craft_psd::PsdFile::from_source`]: header, colour mode data,
/// layer records with their additional information and the global blocks. Channel data and
/// the merged image stay in `data` (as ranges), decoded on demand.
pub fn parse(data: impl Into<Arc<[u8]>>) -> Result<Psd> {
let data: Arc<[u8]> = data.into();
let mut r = Reader::new(&data);
if r.bytes(4).ok() != Some(b"8BPS") {
if !is_psd(&data) {
return Err(Error::NotPsd);
}
let version = r.u16()?;
let psb = match version {
1 => false,
2 => true,
_ => return Err(Error::NotPsd),
let source = craft_psd::source::Source::new(data.clone());
// Linked-file blocks can be most of a file: leave them in the source, read in place.
let linked = |key: &[u8; 4], _len: u64| matches!(key, b"lnk2" | b"lnkD" | b"lnk3" | b"lnkE");
let (head, file, warnings) = match craft_psd::PsdFile::from_source_deferring(source, linked, |_| Ok(false), |_| false) {
Ok((file, _)) => {
// The compression field before the merged image's data.
let merged = file.image_data.stored.as_ref().map_or(data.len(), |s| (s.offset as usize).saturating_sub(2));
((file.header.clone(), file.color_mode_data.clone(), merged), Some(file), vec![])
}
// A damaged layer section must not discard the document: keep the header and the
// merged image, drop the layers and say so. Without a merged image there is nothing to
// fall back to, so the original error stands.
Err(e) => match flattened(&data) {
Some(head) => (head, None, vec![format!("Layers could not be read ({}); only the flattened image is available", error(&e))]),
None => return Err(error(&e)),
},
};
r.skip(6)?;
let channels = r.u16()?;
let height = r.u32()?;
let width = r.u32()?;
let depth = r.u16()?;
let mode_v = r.u16()?;
let (h, palette, merged_offset) = head;
let mode_v = h.color_mode.as_u16();
let mode = ColorMode::from_u16(mode_v).ok_or_else(|| Error::Unsupported(format!("colour mode {mode_v}")))?;
if !matches!(depth, 1 | 8 | 16 | 32) {
return Err(Error::Unsupported(format!("{depth}-bit")));
if !matches!(h.depth, 1 | 8 | 16 | 32) {
return Err(Error::Unsupported(format!("{}-bit", h.depth)));
}
if width == 0 || height == 0 || width > 300_000 || height > 300_000 {
return Err(Error::Invalid(format!("size {width}x{height}")));
if h.width == 0 || h.height == 0 || h.width > 300_000 || h.height > 300_000 {
return Err(Error::Invalid(format!("size {}x{}", h.width, h.height)));
}
// Colour mode data.
let n = r.u32()? as usize;
let palette = r.bytes(n)?.to_vec();
// Image resources.
let n = r.u32()? as usize;
r.skip(n)?;
// Layer and mask information.
let mut psd = Psd {
psb,
width,
height,
channels,
depth,
psb: h.version.is_psb(),
width: h.width,
height: h.height,
channels: h.channels,
depth: h.depth,
mode,
layers: vec![],
merged_alpha: false,
palette,
linked: vec![],
warnings: vec![],
warnings,
data: data.clone(),
merged_offset: 0,
merged_offset,
};
let lm_len = r.length(psb)?;
let lm_end = r.pos.checked_add(lm_len).filter(|&e| e <= data.len()).ok_or(Error::Truncated)?;
if lm_len > 0 {
let li_len = r.length(psb)?;
let li_end = r.pos.checked_add(li_len).filter(|&e| e <= lm_end).ok_or(Error::Truncated)?;
if li_len > 0
&& let Err(e) = psd.layer_info(&data, r.pos, li_end)
{
// A damaged layer section must not discard the document: keep the header and the
// merged image, drop the layers and say so. Without a merged image there is
// nothing to fall back to, so the original error stands.
if lm_end.checked_add(2).is_none_or(|n| n > data.len()) {
return Err(e);
}
psd.layers.clear();
psd.warnings.push(format!("Layers could not be read ({e}); only the flattened image is available"));
}
r.pos = li_end;
if r.pos + 4 <= lm_end {
let g = r.u32()? as usize;
r.pos = (r.pos + g).min(lm_end);
}
// Global additional layer information (16/32-bit layer info lives here).
while let Some((key, start, end)) = next_block(&data, &mut r.pos, lm_end, psb) {
if matches!(&key, b"lnk2" | b"lnkD" | b"lnk3" | b"lnkE") {
psd.linked_files(&data, start, end);
}
if matches!(&key, b"Lr16" | b"Lr32" | b"Layr") && psd.layers.is_empty() && end > start {
// The block holds the layer info, normally without its length field; accept
// both layouts.
if psd.layer_info(&data, start, end).is_err() || psd.layers.is_empty() {
psd.layers.clear();
let skip = if psb { 8 } else { 4 };
if start + skip < end && psd.layer_info(&data, start + skip, end).is_err() {
psd.layers.clear();
}
}
}
r.pos = end;
let Some(file) = file else { return Ok(psd) };
if let Some(info) = &file.layer_info {
psd.merged_alpha = info.merged_alpha;
psd.layers = info.layers.iter().enumerate().map(|(i, rec)| psd.layer(rec, i)).collect();
}
for b in &file.global_blocks {
if let Some(stored) = b.stored.as_ref().filter(|_| linked(&b.key, 0)) {
let start = stored.offset as usize;
psd.linked_files(&data, start, start.saturating_add(stored.len as usize).min(data.len()));
}
}
if !psd.layers.is_empty() {
// A 16/32-bit layer block recovered the layers.
psd.warnings.clear();
}
psd.merged_offset = lm_end;
Ok(psd)
}
/// One layer record of the model, its additional information read into [`Layer`].
fn layer(&self, rec: &craft_psd::LayerRecord, index: usize) -> Layer {
let channels = rec
.channels
.iter()
.map(|c| match &c.stored {
// `ChannelInfo` covers the compression field and the data after it.
Some(s) => ChannelInfo { id: c.id, offset: (s.offset as usize).saturating_sub(2), len: s.len as usize + 2 },
None => ChannelInfo { id: c.id, offset: 0, len: 0 },
})
.collect();
let r = rec.rect;
let mut l = Layer {
index,
rect: Rect { top: r.top, left: r.left, bottom: r.bottom, right: r.right },
blend: rec.blend_mode.key(),
opacity: rec.opacity,
fill_opacity: 255,
clipping: rec.clipping != 0,
hidden: rec.flags.hidden(),
protect_transparency: rec.flags.transparency_protected(),
channels,
name: mac_roman_string(&rec.name),
..Default::default()
};
if let craft_psd::MaskData::Mask(m) = &rec.mask {
let rect = Rect { top: m.rect.top, left: m.rect.left, bottom: m.rect.bottom, right: m.rect.right };
l.mask = Some(LayerMask { rect, default_color: m.default_color, disabled: m.flags & 2 != 0, inverted: m.flags & 4 != 0 });
}
for b in &rec.blocks {
l.keys.push(b.key);
// A malformed block only loses that block's information.
let _ = self.layer_block(&mut l, &b.key, &b.data);
}
l
}
/// The embedded bytes of linked file `uuid`.
pub fn linked_data(&self, uuid: &str) -> Option<&[u8]> {
let f = self.linked.iter().find(|f| f.uuid == uuid)?;
@@ -168,88 +172,6 @@ impl Psd {
}
}
fn layer_info(&mut self, data: &[u8], start: usize, end: usize) -> Result<()> {
let mut r = Reader::at(&data[..end], start);
let count = r.i16()?;
self.merged_alpha = count < 0;
let n = count.unsigned_abs() as usize;
let mut layers = Vec::with_capacity(n);
for i in 0..n {
layers.push(self.layer_record(&mut r, i)?);
}
// Channel image data follows in record order.
for l in &mut layers {
for c in &mut l.channels {
c.offset = r.pos;
r.skip(c.len)?;
}
}
self.layers = layers;
Ok(())
}
fn layer_record(&self, r: &mut Reader, index: usize) -> Result<Layer> {
let psb = self.psb;
let (top, left, bottom, right) = (r.i32()?, r.i32()?, r.i32()?, r.i32()?);
let nch = r.u16()? as usize;
if nch > 64 {
return Err(Error::Invalid("channel count".into()));
}
let mut channels = Vec::with_capacity(nch);
for _ in 0..nch {
let id = r.i16()?;
let len = r.length(psb)?;
channels.push(ChannelInfo { id, offset: 0, len });
}
if r.bytes(4)? != b"8BIM" {
return Err(Error::Invalid("layer record signature".into()));
}
let blend = r.tag()?;
let opacity = r.u8()?;
let clipping = r.u8()? != 0;
let flags = r.u8()?;
r.skip(1)?;
let extra = r.u32()? as usize;
let extra_end = r.pos.checked_add(extra).filter(|&e| e <= r.data.len()).ok_or(Error::Truncated)?;
let mut l = Layer {
index,
rect: Rect { top, left, bottom, right },
blend,
opacity,
fill_opacity: 255,
clipping,
hidden: flags & 2 != 0,
protect_transparency: flags & 1 != 0,
channels,
..Default::default()
};
// Layer mask data.
let mlen = r.u32()? as usize;
let mstart = r.pos;
if mlen >= 18 {
let rect = Rect { top: r.i32()?, left: r.i32()?, bottom: r.i32()?, right: r.i32()? };
let default_color = r.u8()?;
let mflags = r.u8()?;
l.mask = Some(LayerMask { rect, default_color, disabled: mflags & 2 != 0, inverted: mflags & 4 != 0 });
}
r.pos = mstart + mlen;
// Blending ranges.
let blen = r.u32()? as usize;
r.skip(blen)?;
l.name = r.pascal(4)?;
let mut pos = r.pos;
let data = r.data;
while let Some((key, start, end)) = next_block(data, &mut pos, extra_end, psb) {
l.keys.push(key);
let block = &data[start..end];
// A malformed block only loses that block's information.
let _ = self.layer_block(&mut l, &key, block);
pos = end;
}
r.pos = extra_end;
Ok(l)
}
fn layer_block(&self, l: &mut Layer, key: &[u8; 4], b: &[u8]) -> Result<()> {
let mut r = Reader::new(b);
match key {
@@ -482,33 +404,32 @@ impl Psd {
}
}
/// The next additional-information block at `*pos` (signature `8BIM`/`8B64`, key, length):
/// returns (key, data start, data end). Skips up to 3 padding bytes before a signature.
fn next_block(data: &[u8], pos: &mut usize, end: usize, psb: bool) -> Option<([u8; 4], usize, usize)> {
let mut p = *pos;
for _ in 0..4 {
if p + 12 > end {
return None;
}
if &data[p..p + 4] == b"8BIM" || &data[p..p + 4] == b"8B64" {
break;
}
p += 1;
/// The header, colour mode data and merged image offset of a document whose layer and mask
/// section cannot be read: `None` when the merged image cannot be reached either.
fn flattened(data: &[u8]) -> Option<(craft_psd::Header, Vec<u8>, usize)> {
let head = craft_psd::head::HeadView::from_bytes(data).ok()?;
let at = usize::try_from(head.layer_section_offset).ok()?;
let len = if head.header.version.is_psb() {
u64::from_be_bytes(data.get(at..at + 8)?.try_into().ok()?)
} else {
u64::from(u32::from_be_bytes(data.get(at..at + 4)?.try_into().ok()?))
};
let field = if head.header.version.is_psb() { 8 } else { 4 };
let merged = at.checked_add(field)?.checked_add(usize::try_from(len).ok()?)?;
(merged.checked_add(2)? <= data.len()).then(|| (head.header.clone(), head.color_mode_data.to_vec(), merged))
}
/// This crate's error for one from craft-psd.
fn error(e: &craft_psd::PsdError) -> Error {
match e {
craft_psd::PsdError::UnexpectedEof { .. } => Error::Truncated,
craft_psd::PsdError::InvalidSignature { .. } | craft_psd::PsdError::UnsupportedVersion(_) => Error::NotPsd,
craft_psd::PsdError::Unsupported(s) => Error::Unsupported(s.clone()),
other => Error::Invalid(other.to_string()),
}
if p + 12 > end || !(&data[p..p + 4] == b"8BIM" || &data[p..p + 4] == b"8B64") {
return None;
}
let mut key = [0u8; 4];
key.copy_from_slice(&data[p + 4..p + 8]);
let wide =
psb && matches!(&key, b"LMsk" | b"Lr16" | b"Lr32" | b"Layr" | b"Mt16" | b"Mt32" | b"Mtrn" | b"Alph" | b"FMsk" | b"lnk2" | b"FEid" | b"FXid" | b"PxSD");
let mut r = Reader::at(&data[..end], p + 8);
let len = r.length(wide).ok()?;
let start = r.pos;
let stop = start.checked_add(len)?;
if stop > end {
return None;
}
*pos = stop;
Some((key, start, stop))
}
/// A Pascal string's bytes in the system (Mac Roman) encoding; ASCII covers the common case.
fn mac_roman_string(b: &[u8]) -> String {
b.iter().map(|&b| if b < 0x80 { b as char } else { super::read::mac_roman(b) }).collect()
}
+1 -1
View File
@@ -85,7 +85,7 @@ impl<'a> Reader<'a> {
}
/// Mac OS Roman upper half (0x80..=0xFF), as published by the Unicode Consortium mapping table.
fn mac_roman(b: u8) -> char {
pub(crate) fn mac_roman(b: u8) -> char {
const T: &str = "ÄÅÇÉÑÖÜáàâäãåçéèêëíìîïñóòôöõúùûü†°¢£§•¶ß®©™´¨≠ÆØ∞±≤≥¥µ∂∑∏π∫ªºΩæø¿¡¬√ƒ≈∆«»… ÀÃÕŒœ–—“”‘’÷◊ÿŸ⁄€‹›fifl‡·‚„‰ÂÊÁËÈÍÎÏÌÓÔ\u{F8FF}ÒÚÛÙıˆ˜¯˘˙˚¸˝˛ˇ";
T.chars().nth((b - 0x80) as usize).unwrap_or('?')
}
+1
View File
@@ -3,6 +3,7 @@ use super::*;
mod integration;
mod opt;
mod shared_corpus;
fn gradient(w: u32, h: u32) -> Vec<[f32; 4]> {
(0..w * h).map(|i| [(i % w) as f32 / (w - 1).max(1) as f32, (i / w) as f32 / (h - 1).max(1) as f32, 0.25, 1.0]).collect()
+61
View File
@@ -0,0 +1,61 @@
//! The reader over craft-psd's generated corpus (every colour mode, depth, compression and PSB):
//! the model matches the shared reader's records and its channel ranges hold the stored bytes.
use crate::*;
#[test]
fn every_generated_case_reads_like_craft_psd() {
let mut decoded = 0;
for (i, case) in craft_psd::testgen::all_cases().into_iter().enumerate() {
let bytes = case.file.to_bytes().expect("write");
let shared = craft_psd::PsdFile::from_bytes(&bytes).expect("shared parse");
let p = match Psd::parse(bytes.clone()) {
Ok(p) => p,
// Modes this reader does not show are refused cleanly, never half-read.
Err(Error::Unsupported(_)) => continue,
Err(e) => panic!("case {i}: {e}"),
};
assert_eq!(
(p.width, p.height, p.depth, p.psb),
(shared.header.width, shared.header.height, shared.header.depth, shared.header.version.is_psb()),
"case {i}"
);
let records = shared.layer_info.as_ref().map_or(&[][..], |l| &l.layers[..]);
assert_eq!(p.layers.len(), records.len(), "case {i}");
for (l, rec) in p.layers.iter().zip(records) {
assert_eq!(l.blend, rec.blend_mode.key(), "case {i}");
assert_eq!(l.channels.len(), rec.channels.len(), "case {i}");
for (c, rc) in l.channels.iter().zip(&rec.channels) {
assert_eq!(c.id, rc.id, "case {i}");
// The range covers the compression field and the stored bytes after it.
assert!(c.len == 0 || c.len == rc.data.len() + 2, "case {i}");
assert!(c.len == 0 || bytes[c.offset + 2..c.offset + c.len] == rc.data[..], "case {i}");
}
}
// Decoding reads those ranges: it ends in pixels or a clean error, as before the cutover.
if p.composite().is_ok() {
decoded += 1;
}
for li in 0..p.layers.len() {
let _ = p.layer_pixels(li, false);
}
}
assert!(decoded > 100, "{decoded} composites decoded");
}
#[test]
fn a_damaged_layer_section_keeps_the_merged_image() {
let case = craft_psd::testgen::all_cases().into_iter().find(|c| c.file.layer_info.as_ref().is_some_and(|l| !l.layers.is_empty())).expect("a layered case");
let mut bytes = case.file.to_bytes().expect("write");
let head = craft_psd::head::HeadView::from_bytes(&bytes).expect("head");
// Overwrite the first layer record's signature.
let at = head.layer_section_offset as usize + if head.header.version.is_psb() { 16 } else { 8 } + 2 + 16;
let channels = usize::from(u16::from_be_bytes([bytes[at], bytes[at + 1]]));
let sig = at + 2 + channels * if head.header.version.is_psb() { 10 } else { 6 };
assert_eq!(&bytes[sig..sig + 4], b"8BIM");
bytes[sig..sig + 4].copy_from_slice(b"XXXX");
let p = Psd::parse(bytes).expect("flattened");
assert!(p.layers.is_empty());
assert!(p.warnings.iter().any(|w| w.contains("Layers could not be read")), "{:?}", p.warnings);
assert!(p.composite().is_ok());
}