From f30bd05cfed7262d5b0b58ae618e1d12e682ebf2 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Thu, 12 Nov 2026 01:05:29 +0000 Subject: [PATCH] Read the PSD header and image resources with craft-psd lightcraft-meta's EXIF/XMP scan and lightcraft-codecs' PSD probe and decoder take the header, colour mode data and image resources from craft_psd::Head (storytold/craft-libs#885). The merged-image decoder and the flat writer stay. --- Cargo.lock | 32 +++++++++ Cargo.toml | 2 + crates/codecs/Cargo.toml | 1 + crates/codecs/src/psd.rs | 145 ++++++++++++++++++--------------------- crates/meta/Cargo.toml | 1 + crates/meta/src/psd.rs | 63 ++--------------- 6 files changed, 108 insertions(+), 136 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dbaac1f99e..47a44f1def 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1239,6 +1239,16 @@ dependencies = [ "writeable", ] +[[package]] +name = "craft-psd" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=119415f46f601e20b9bc6b1b4d6979ceda86d88e#119415f46f601e20b9bc6b1b4d6979ceda86d88e" +dependencies = [ + "encoding_rs", + "flate2", + "thiserror 2.0.21", +] + [[package]] name = "craft-tiff" version = "0.1.0" @@ -1796,6 +1806,20 @@ dependencies = [ "bytemuck", ] +[[package]] +name = "encoding_rs" +version = "0.8.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89253175e704ab869b04f9db224d38630e0211a53841a79181deae0889e169f5" +dependencies = [ + "cfg-if", + "core_detect", + "multiversion_no_op", + "rustversion", + "scopeguard", + "simdutf8", +] + [[package]] name = "endi" version = "1.1.1" @@ -3687,6 +3711,7 @@ dependencies = [ "bytemuck", "craft-gainmap", "craft-heif", + "craft-psd", "dhat", "image", "image-webp", @@ -3930,6 +3955,7 @@ version = "0.21.0" dependencies = [ "chrono", "craft-heif", + "craft-psd", "craft-tiff", "lightcraft-geom", "proptest", @@ -4309,6 +4335,12 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "multiversion_no_op" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "743fb55ba31b18fb1ecef6bdc9aa2743314978ac084044301a7eee33fb99a20d" + [[package]] name = "naga" version = "30.0.1" diff --git a/Cargo.toml b/Cargo.toml index 41ba05620b..301ca570e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,8 @@ craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0. craft-window = { git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7", features = ["egui"] } # HDR gain maps: ISO 21496-1 and hdrgm metadata, compute and apply (moved from crates/codecs; craft-libs#904). craft-gainmap = { git = "https://github.com/storytold/craft-libs", rev = "119415f46f601e20b9bc6b1b4d6979ceda86d88e" } +# PSD/PSB: the header, colour mode data and image resources (craft-libs#885). +craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "119415f46f601e20b9bc6b1b4d6979ceda86d88e" } # internal lightcraft-denoise-core = { path = "crates/denoise-core" } lightcraft-denoise = { path = "crates/denoise", default-features = false } diff --git a/crates/codecs/Cargo.toml b/crates/codecs/Cargo.toml index 019009d7c1..15b7fb0557 100644 --- a/crates/codecs/Cargo.toml +++ b/crates/codecs/Cargo.toml @@ -13,6 +13,7 @@ lightcraft-geom = { workspace = true } lightcraft-color = { workspace = true } lightcraft-raster = { workspace = true } craft-gainmap = { workspace = true } +craft-psd = { workspace = true } craft-heif = { workspace = true, optional = true } serde = { workspace = true } bytemuck = { workspace = true } diff --git a/crates/codecs/src/psd.rs b/crates/codecs/src/psd.rs index 7cccec0f8f..f0422edc13 100644 --- a/crates/codecs/src/psd.rs +++ b/crates/codecs/src/psd.rs @@ -40,36 +40,46 @@ impl<'a> Rd<'a> { } } +/// The header, colour mode data and image resources (`craft-psd`), with a reader at the layer and mask section. +struct Start<'a> { + psb: bool, + channels: usize, + width: usize, + height: usize, + depth: u16, + mode: u16, + head: craft_psd::Head, + r: Rd<'a>, +} + +fn start<'a>(bytes: &'a [u8], opts: &DecodeOptions) -> Result> { + // the size limit first, from the header's fixed fields, so an oversized file is `TooLarge` as for other formats + if let (Some(&[h0, h1, h2, h3]), Some(&[w0, w1, w2, w3])) = (bytes.get(14..18), bytes.get(18..22)) { + check_size(F, u64::from(u32::from_be_bytes([w0, w1, w2, w3])), u64::from(u32::from_be_bytes([h0, h1, h2, h3])), opts)?; + } + let head = craft_psd::Head::from_bytes(bytes).map_err(|e| match e { + craft_psd::PsdError::UnexpectedEof { .. } => err("truncated"), + e => err(&e.to_string()), + })?; + let h = &head.header; + check_size(F, u64::from(h.width), u64::from(h.height), opts)?; + let p = usize::try_from(head.layer_section_offset).map_err(|_| err("truncated"))?; + Ok(Start { + psb: h.version.is_psb(), + channels: usize::from(h.channels), + width: h.width as usize, + height: h.height as usize, + depth: h.depth, + mode: h.color_mode.as_u16(), + r: Rd { b: bytes, p }, + head, + }) +} + pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result { - let mut r = Rd { b: bytes, p: 0 }; - if r.take(4)? != b"8BPS" { - return Err(err("bad signature")); - } - let version = r.u16()?; - let psb = match version { - 1 => false, - 2 => true, - _ => return Err(err("bad version")), - }; - r.take(6)?; - let channels = r.u16()? as usize; - let height = r.u32()? as usize; - let width = r.u32()? as usize; - let depth = r.u16()?; - let mode = r.u16()?; - check_size(F, width as u64, height as u64, opts)?; - if channels == 0 || channels > 56 { - return Err(err("bad channel count")); - } - - // Colour mode data (palette for indexed). - let cm_len = r.u32()? as usize; - let color_mode_data = r.take(cm_len)?; - - // Image resources. - let res_len = r.u32()? as usize; - let res = r.take(res_len)?; - let (icc, exif, xmp) = resources(res); + let Start { psb, channels, width, height, depth, mode, head, mut r } = start(bytes, opts)?; + let color_mode_data = head.color_mode_data.as_slice(); + let (icc, exif, xmp) = resources(&head); // Layer and mask information: only the layer count sign matters (merged transparency). let lm_len = if psb { r.u64()? } else { u64::from(r.u32()?) }; @@ -342,29 +352,9 @@ fn packbits(src: &[u8], out: &mut Vec) { /// depth, colour mode or compression, or merged image data that runs past the end of the file /// (truncated). The run-length data itself is not unpacked. pub(crate) fn header(bytes: &[u8]) -> Result<(u32, u32, u16)> { - let mut r = Rd { b: bytes, p: 0 }; - if r.take(4)? != b"8BPS" { - return Err(err("bad signature")); - } - let psb = match r.u16()? { - 1 => false, - 2 => true, - _ => return Err(err("bad version")), - }; - r.take(6)?; - let channels = r.u16()? as usize; - let height = r.u32()? as usize; - let width = r.u32()? as usize; - let depth = r.u16()?; - let mode = r.u16()?; - check_size(F, width as u64, height as u64, &DecodeOptions::default())?; - if channels == 0 || channels > 56 { - return Err(err("bad channel count")); - } - let cm_len = r.u32()? as usize; - let color_mode_data = r.take(cm_len)?; - let res_len = r.u32()? as usize; - let (_, exif, _) = resources(r.take(res_len)?); + let Start { psb, channels, width, height, depth, mode, head, mut r } = start(bytes, &DecodeOptions::default())?; + let color_mode_data = head.color_mode_data.as_slice(); + let (_, exif, _) = resources(&head); let lm_len = if psb { r.u64()? as usize } else { r.u32()? as usize }; r.take(lm_len)?; let compression = r.u16()?; @@ -444,28 +434,12 @@ fn unpackbits(mut src: &[u8], row: &mut [u8]) -> Result<()> { Ok(()) } -fn resources(mut b: &[u8]) -> (Option>, Option>, Option) { - let (mut icc, mut exif, mut xmp) = (None, None, None); - while b.len() >= 12 && &b[0..4] == b"8BIM" { - let id = u16::from_be_bytes([b[4], b[5]]); - let name_len = b[6] as usize; - let name_total = (1 + name_len + 1) & !1; - let p = 6 + name_total; - if p + 4 > b.len() { - break; - } - let size = u32::from_be_bytes([b[p], b[p + 1], b[p + 2], b[p + 3]]) as usize; - let start = p + 4; - let Some(data) = start.checked_add(size).and_then(|end| b.get(start..end)) else { break }; - match id { - 1039 => icc = Some(data.to_vec()), - 1058 => exif = Some(if data.starts_with(b"Exif\0\0") { data[6..].to_vec() } else { data.to_vec() }), - 1060 => xmp = Some(String::from_utf8_lossy(data).trim_end_matches('\0').to_string()), - _ => {} - } - let next = start + ((size + 1) & !1); - b = b.get(next..).unwrap_or(&[]); - } +/// ICC (1039), EXIF (1058, without its optional `Exif\0\0` prefix) and XMP (1060) resources; the last of each id. +fn resources(head: &craft_psd::Head) -> (Option>, Option>, Option) { + let last = |id: u16| head.resources.iter().rev().find(|r| r.id == id).map(|r| r.data.as_slice()); + let icc = last(1039).map(<[u8]>::to_vec); + let exif = last(1058).map(|d| d.strip_prefix(b"Exif\0\0").unwrap_or(d).to_vec()); + let xmp = last(1060).map(|d| String::from_utf8_lossy(d).trim_end_matches('\0').to_string()); (icc, exif, xmp) } @@ -514,8 +488,8 @@ mod tests { #[test] fn huge_rle_with_truncated_count_table_is_rejected_before_allocating() { - // 1 × 2^30 px, 56 channels: the row table alone would be 56 Gi entries; the file has 4 bytes. - let mut b = header(56, 1, 1 << 30, 32, 3, 1); + // 1 × 300 000 px, 56 channels: the row table alone would be 16.8 M entries; the file has 4 bytes. + let mut b = header(56, 1, 300_000, 32, 3, 1); b.extend_from_slice(&[0, 0, 0, 0]); let e = decode(&b, &DecodeOptions::default()).unwrap_err(); assert!(matches!(e, Error::Malformed(_, ref m) if m == "truncated"), "{e:?}"); @@ -523,18 +497,29 @@ mod tests { #[test] fn huge_rle_of_empty_rows_is_rejected_before_allocating() { - // 2^30 × 1 px RGB at 32 bits: every row count is 0, which used to zero-pad 3 × 4 GiB. - let mut b = header(3, 1 << 30, 1, 32, 3, 1); + // 300 000 × 1 px RGB at 32 bits: every row count is 0, which used to zero-pad 3 × 1.2 MB. + let mut b = header(3, 300_000, 1, 32, 3, 1); b.extend_from_slice(&[0; 6]); let e = decode(&b, &DecodeOptions::default()).unwrap_err(); assert!(matches!(e, Error::Malformed(_, ref m) if m.contains("too short")), "{e:?}"); - // 1 Gpx RGB 8-bit whose (plausible) row counts claim more data than the file holds. - let mut b = header(3, 1 << 20, 1 << 10, 8, 3, 1); + // 0.27 Gpx RGB 8-bit whose (plausible) row counts claim more data than the file holds. + let mut b = header(3, 1 << 18, 1 << 10, 8, 3, 1); b.extend_from_slice(&[0xFF; 3 * 1024 * 2]); let e = decode(&b, &DecodeOptions::default()).unwrap_err(); assert!(matches!(e, Error::Malformed(_, ref m) if m == "truncated"), "{e:?}"); } + #[test] + fn a_damaged_resource_section_is_malformed() { + // a resource section of 4 bytes that is not an `8BIM` record + let mut b = header(3, 1, 1, 8, 3, 0); + b[30..34].copy_from_slice(&4u32.to_be_bytes()); + b.splice(34..34, *b"JUNK"); + let e = decode(&b, &DecodeOptions::default()).unwrap_err(); + assert!(matches!(e, Error::Malformed(_, ref m) if m.contains("signature")), "{e:?}"); + assert!(super::header(&b).is_err()); + } + #[test] fn rle_repeat_run_past_row_width_is_an_error() { // 4 × 1 gray 8-bit; the single row is a 128-byte repeat run. diff --git a/crates/meta/Cargo.toml b/crates/meta/Cargo.toml index 1b146c99da..e813d3a2a8 100644 --- a/crates/meta/Cargo.toml +++ b/crates/meta/Cargo.toml @@ -10,6 +10,7 @@ workspace = true [dependencies] craft-tiff = { workspace = true } +craft-psd = { workspace = true } lightcraft-geom = { workspace = true } serde = { workspace = true } quick-xml = { workspace = true } diff --git a/crates/meta/src/psd.rs b/crates/meta/src/psd.rs index ebdbdaf723..8fbe448d55 100644 --- a/crates/meta/src/psd.rs +++ b/crates/meta/src/psd.rs @@ -2,73 +2,24 @@ use crate::container::Embedded; -struct Reader<'a> { - bytes: &'a [u8], - offset: usize, -} - -impl<'a> Reader<'a> { - fn take(&mut self, length: usize) -> Option<&'a [u8]> { - let end = self.offset.checked_add(length)?; - let bytes = self.bytes.get(self.offset..end)?; - self.offset = end; - Some(bytes) - } - - fn u16(&mut self) -> Option { - let bytes = self.take(2)?; - Some(u16::from_be_bytes([*bytes.first()?, *bytes.get(1)?])) - } - - fn u32(&mut self) -> Option { - let bytes = self.take(4)?; - Some(u32::from_be_bytes([*bytes.first()?, *bytes.get(1)?, *bytes.get(2)?, *bytes.get(3)?])) - } -} - pub(crate) fn embedded(bytes: &[u8]) -> Embedded { parse(bytes).unwrap_or_default() } +/// The EXIF (1058) and XMP (1060) resources of the head `craft-psd` reads (header, colour mode data, image +/// resources). `None` for a head that does not parse, a second EXIF or XMP resource, or XMP that is not UTF-8. pub(crate) fn parse(bytes: &[u8]) -> Option { - let mut reader = Reader { bytes, offset: 0 }; - if reader.take(4)? != b"8BPS" || !matches!(reader.u16()?, 1 | 2) { - return None; - } - // The fixed header after the version is reserved bytes, channels, dimensions, depth and colour mode. - reader.take(6 + 2 + 4 + 4 + 2 + 2)?; - let colour_mode_length = usize::try_from(reader.u32()?).ok()?; - reader.take(colour_mode_length)?; - let resource_length = usize::try_from(reader.u32()?).ok()?; - let resources = reader.take(resource_length)?; - parse_resources(resources) -} - -fn parse_resources(bytes: &[u8]) -> Option { - let mut reader = Reader { bytes, offset: 0 }; + let head = craft_psd::Head::from_bytes(bytes).ok()?; let (mut exif, mut xmp) = (None, None); - while reader.offset < bytes.len() { - if reader.take(4)? != b"8BIM" { - return None; - } - let id = reader.u16()?; - let name_length = usize::from(*reader.take(1)?.first()?); - let pascal_length = 1usize.checked_add(name_length)?.checked_add(1)? & !1; - reader.take(pascal_length.checked_sub(1)?)?; - let data_length = usize::try_from(reader.u32()?).ok()?; - let data = reader.take(data_length)?; - if !data_length.is_multiple_of(2) { - reader.take(1)?; - } - match id { + for r in &head.resources { + match r.id { 1058 if exif.is_some() => return None, - 1058 => exif = Some(data), + 1058 => exif = Some(r.data.as_slice()), 1060 if xmp.is_some() => return None, - 1060 => xmp = Some(std::str::from_utf8(data).ok()?), + 1060 => xmp = Some(std::str::from_utf8(&r.data).ok()?), _ => {} } } - Some(Embedded { exif: exif.map(crate::strip_exif_header).map(<[u8]>::to_vec), xmp: xmp.map(|text| text.trim_end_matches('\0').to_string()),