PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Correct IFF chunky decoding and preserve bitmap transparency.
Vikram Bhaskarand5535f35174d
diff --git a/crates/codecs/README.md b/crates/codecs/README.mdindex 958d71d88..4cb0c0b4f 100644--- a/crates/codecs/README.md+++ b/crates/codecs/README.md@@ -48,7 +48,7 @@ let out = encode(&img, Format::Tiff, &EncodeOptions::default())?;| FITS | yes | yes | U8, U16, F32 | Gray, RGB | no | no | no | no | no | yes (header cards) | no | built in || DPX | yes | yes | U8, U16 (10/12-bit code values read and written exactly through U16) | Gray, RGB, RGBA | yes | no | no | no | no | DPX header fields only (creator, project, copyright, creation time, transfer, colorimetric, frame position, frame rate, time code) | no | built in || Cineon | yes | **no** | U16 (10-bit code values) | Gray, RGB | no | no | no | no | no | no | n/a | built in |-| Amiga IFF (ILBM/PBM) | yes | **no** | U8 | RGB | no | no | no | no | no | no | n/a | built in |+| Amiga IFF (ILBM/PBM) | yes | **no** | U8 | RGB, RGBA | yes | no | no | no | no | no | n/a | built in || AVIF | **no** | only with feature `avif` | U8 | RGB, RGBA | yes | no | no | no | no | no | yes | `image` + `ravif` || HEIF/HEIC | with feature `heif` | **no** | U8, U16 (10/12-bit decodes to U16) | RGB, RGBA | yes (auxiliary alpha) | yes (`colr` prof) | yes | yes | no | no | n/a | `heic-rs` |@@ -105,11 +105,12 @@ embedded table (and a PNG's transparent entry) so `photocraft-io` can open thema channel count other than 1/3 returns `CodecError::Unsupported`; truncation errors rather thanpanicking. There is no Cineon writer (`ASYMMETRIC_EXCEPTIONS`); save as DPX, TIFF or EXR.-* **Amiga IFF (read-only).** `FORM ... ILBM` (interleaved) and `FORM ... PBM ` (planar) bitmaps- from the published IFF/ILBM layout: 1–8 bitplanes decoded through the `CMAP` palette (a grey- ramp when there is none), masking 0/1/2, and both stored (0) and ByteRun1/PackBits (1)- compression. Other bit-plane counts or compressions return `CodecError::Unsupported`; a- malformed chunk length or run errors rather than panicking. There is no IFF writer; save as PNG.+* **Amiga IFF (read-only).** `FORM ... ILBM` stores interleaved 1–8 bitplanes;+ `FORM ... PBM ` stores 8-bit chunky palette indices with even-padded rows. Both support+ stored and ByteRun1 compression, a `CMAP` palette (grey ramp when absent, black for missing+ entries), and transparent-colour masking. ILBM also supports a separate mask plane. HAM/EHB+ viewport modes, PBM mask planes, other bit depths and compressions return+ `CodecError::Unsupported`. There is no IFF writer; save as PNG.* **Camera raw files** (DNG, CR2, NEF, ARW… which are TIFF-structured) are recognised and refusedwith `CodecError::Unsupported`: they are sensor data, not flat images. `photocraft-raw` decodesdiff --git a/crates/codecs/src/codecs/iff.rs b/crates/codecs/src/codecs/iff.rsindex 421d55c89..cae046f3b 100644--- a/crates/codecs/src/codecs/iff.rs+++ b/crates/codecs/src/codecs/iff.rs@@ -1,4 +1,4 @@-//! Amiga IFF/ILBM reader (EA "Interchange File Format", "InterLeaved BitMap" and the planar+//! Amiga IFF/ILBM reader (EA "Interchange File Format", "InterLeaved BitMap" and the chunky//! "PBM " variant). Written from the published IFF/ILBM layout: a `FORM` chunk of four-byte//! chunks (`BMHD` bitmap header, `CMAP` palette, `CAMG` Amiga viewport mode, `BODY` pixel data).//! Read-only: modern saves should use PNG.@@ -32,7 +32,7 @@ fn be16(b: &[u8], at: usize) -> Option<u16> {Some(u16::from_be_bytes(s))}-/// `FORM` + a bitmap type (`ILBM` interleaved, `PBM ` planar) at offset 8.+/// `FORM` + a bitmap type (`ILBM` interleaved, `PBM ` chunky) at offset 8.pub(crate) fn looks_like_iff(b: &[u8]) -> bool {b.get(0..4) == Some(b"FORM") && matches!(b.get(8..12), Some(b"ILBM" | b"PBM "))}@@ -44,8 +44,9 @@ struct Header {planes: u8,masking: u8,compression: u8,- /// `ILBM` interleaves a row's planes; `PBM ` stores a whole plane before the next.- interleaved: bool,+ transparent: u16,+ /// `PBM ` stores one palette index per byte, padded to even row lengths.+ chunky: bool,}/// One four-byte chunk's id and byte range.@@ -73,7 +74,7 @@ fn chunks(b: &[u8]) -> impl Iterator<Item = Chunk<'_>> {})}-fn parse_header(b: &[u8], interleaved: bool) -> Result<Header, CodecError> {+fn parse_header(b: &[u8], chunky: bool) -> Result<Header, CodecError> {let bmhd = chunks(b).find(|c| &c.id == b"BMHD").ok_or_else(|| bad("no BMHD chunk"))?;let d = bmhd.data;if d.len() < 20 {@@ -96,7 +97,11 @@ fn parse_header(b: &[u8], interleaved: bool) -> Result<Header, CodecError> {if compression > 1 {return Err(unsupported(format!("compression {compression} (only stored and ByteRun1)")));}- Ok(Header { width, height, planes, masking, compression, interleaved })+ if chunky && (planes != 8 || masking == 1) {+ return Err(unsupported("PBM requires 8-bit indices without a mask plane"));+ }+ let transparent = be16(d, 12).ok_or_else(|| bad("BMHD transparent colour"))?;+ Ok(Header { width, height, planes, masking, compression, transparent, chunky })}/// ByteRun1 / PackBits decode of one plane row into `out`; returns the bytes consumed. A `0x80`@@ -133,32 +138,43 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result<Image, CodecError> {if !looks_like_iff(b) {return Err(bad("not an IFF/ILBM file (no FORM ... ILBM)"));}- let interleaved = b.get(8..12) == Some(b"ILBM");- let h = parse_header(b, interleaved)?;- limits.check(h.width, h.height, ChannelLayout::Rgb, SampleType::U8)?;+ let h = parse_header(b, b.get(8..12) == Some(b"PBM "))?;+ if let Some(camg) = chunks(b).find(|c| &c.id == b"CAMG") {+ let mode = be32(camg.data, 0).ok_or_else(|| bad("short CAMG chunk"))?;+ if mode & (0x80 | 0x800) != 0 {+ return Err(unsupported("EHB and HAM viewport modes"));+ }+ }+ let layout = if h.masking == 0 { ChannelLayout::Rgb } else { ChannelLayout::Rgba };+ limits.check(h.width, h.height, layout, SampleType::U8)?;// Palette: three bytes per colour; missing entries read as black.- let cmap = chunks(b).find(|c| &c.id == b"CMAP").map(|c| c.data);+ let cmap = chunks(b).find(|c| &c.id == b"CMAP").map(|c| c.data).filter(|p| !p.is_empty());let colors = 1usize << h.planes;let palette = |idx: usize| -> [u8; 3] {- cmap.and_then(|p| p.get(idx * 3..idx * 3 + 3)).map(|c| [c[0], c[1], c[2]]).unwrap_or({- // No palette: a grey ramp across the index range.+ if let Some(p) = cmap {+ p.get(idx * 3..idx * 3 + 3).map(|c| [c[0], c[1], c[2]]).unwrap_or([0; 3])+ } else {let v = ((idx as u32 * 255) / (colors as u32 - 1).max(1)) as u8;[v, v, v]- })+ }};let body = chunks(b).find(|c| &c.id == b"BODY").map(|c| c.data).ok_or_else(|| bad("no BODY chunk"))?;let (w, hh) = (h.width as usize, h.height as usize);// Each plane row is stored as a whole number of 16-bit words.- let plane_row = w.div_ceil(16) * 2;+ let plane_row = if h.chunky { w.div_ceil(2) * 2 } else { w.div_ceil(16) * 2 };// A masking type 1 bitmap carries one extra (mask) plane per row.- let rows_planes: usize = if h.masking == 1 { usize::from(h.planes) + 1 } else { usize::from(h.planes) };+ let rows_planes: usize = if h.chunky { 1 } else { usize::from(h.planes) + usize::from(h.masking == 1) };// Decode every stored plane row up front, then recombine.let total_rows = hh.checked_mul(rows_planes).ok_or_else(|| CodecError::LimitExceeded("bitmap too large".into()))?;let total_bytes = total_rows.checked_mul(plane_row).ok_or_else(|| CodecError::LimitExceeded("bitmap too large".into()))?;- let mut stored = vec![0u8; total_bytes];+ if total_bytes as u64 > limits.max_alloc {+ return Err(CodecError::LimitExceeded("bitmap storage exceeds max_alloc".into()));+ }+ Limits::check_plausible(total_bytes as u64, body.len(), if h.compression == 0 { 1 } else { 128 })?;+ let mut stored = crate::options::alloc_zeroed::<u8>(total_bytes)?;let mut cursor = 0usize;for row in stored.chunks_mut(plane_row.max(1)).take(total_rows) {match h.compression {@@ -176,22 +192,24 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result<Image, CodecError> {}let mut out = Vec::new();- out.try_reserve_exact(w.saturating_mul(hh).saturating_mul(3)).map_err(|_| CodecError::LimitExceeded("cannot allocate the image".into()))?;+ out.try_reserve_exact(w.saturating_mul(hh).saturating_mul(layout.channels())).map_err(|_| CodecError::LimitExceeded("cannot allocate the image".into()))?;let planes = usize::from(h.planes);for y in 0..hh {for x in 0..w {- let index = if h.interleaved {- // Row-major, interleaved: row y, plane p sits at (y * rows_planes + p).- (0..planes).filter(|&p| bit_at(&stored, (y * rows_planes + p) * plane_row, x)).fold(0usize, |acc, p| acc | (1 << p))+ let index = if h.chunky {+ usize::from(*stored.get(y * plane_row + x).ok_or_else(|| bad("PBM pixel outside BODY"))?)} else {- // Planar: plane p's rows come first, then the next plane.- (0..planes).filter(|&p| bit_at(&stored, (p * hh + y) * plane_row, x)).fold(0usize, |acc, p| acc | (1 << p))+ (0..planes).filter(|&p| bit_at(&stored, (y * rows_planes + p) * plane_row, x)).fold(0usize, |acc, p| acc | (1 << p))};let c = palette(index);out.extend_from_slice(&c);+ if h.masking != 0 {+ let opaque = if h.masking == 1 { bit_at(&stored, (y * rows_planes + planes) * plane_row, x) } else { index != usize::from(h.transparent) };+ out.push(if opaque { 255 } else { 0 });+ }}}- Image::from_u8(h.width, h.height, ChannelLayout::Rgb, out)+ Image::from_u8(h.width, h.height, layout, out)}/// The bit for pixel `x` of a stored plane row at `base` (bits run MSB-first).@@ -228,6 +246,9 @@ mod tests {for c in palette {b.extend_from_slice(c);}+ if palette.len() % 2 == 1 {+ b.push(0);+ }// BODYb.extend_from_slice(b"BODY");b.extend_from_slice(&(body.len() as u32).to_be_bytes());@@ -272,6 +293,52 @@ mod tests {assert_eq!(img.data(), &[0, 0, 0, 255, 255, 255]);}+ #[test]+ fn pbm_reads_chunky_indices_with_odd_width_padding() {+ let pal = [[10, 20, 30], [40, 50, 60], [70, 80, 90]];+ for (compression, body) in [(0, vec![2, 0, 1, 255, 1, 2, 0, 255]), (1, vec![3, 2, 0, 1, 255, 3, 1, 2, 0, 255])] {+ let mut b = ilbm(3, 2, 8, &pal, compression, &body);+ b[8..12].copy_from_slice(b"PBM ");+ let img = decode(&b, &Limits::default()).unwrap();+ assert_eq!(img.data(), &[70, 80, 90, 10, 20, 30, 40, 50, 60, 40, 50, 60, 70, 80, 90, 10, 20, 30]);+ }+ }++ #[test]+ fn transparency_uses_the_mask_plane_or_transparent_index() {+ for masking in [1, 2] {+ let mut b = ilbm(2, 1, 1, &[[10, 20, 30], [40, 50, 60]], 0, if masking == 1 { &[0x40, 0, 0x40, 0] } else { &[0x40, 0] });+ b[29] = masking;+ let img = decode(&b, &Limits::default()).unwrap();+ assert_eq!(img.layout(), ChannelLayout::Rgba);+ assert_eq!(img.data(), &[10, 20, 30, 0, 40, 50, 60, 255]);+ }+ let mut b = ilbm(2, 1, 8, &[[10, 20, 30], [40, 50, 60]], 0, &[0, 1]);+ b[8..12].copy_from_slice(b"PBM ");+ b[29] = 2;+ b[32..34].copy_from_slice(&1u16.to_be_bytes());+ assert_eq!(decode(&b, &Limits::default()).unwrap().data(), &[10, 20, 30, 255, 40, 50, 60, 0]);+ }++ #[test]+ fn rejects_ham_and_ehb_instead_of_returning_wrong_colours() {+ for mode in [0x80u32, 0x800] {+ let mut b = ilbm(1, 1, 1, &[], 0, &[0, 0]);+ b.extend_from_slice(b"CAMG");+ b.extend_from_slice(&4u32.to_be_bytes());+ b.extend_from_slice(&mode.to_be_bytes());+ let len = (b.len() - 8) as u32;+ b[4..8].copy_from_slice(&len.to_be_bytes());+ assert!(matches!(decode(&b, &Limits::default()), Err(CodecError::Unsupported { .. })));+ }+ }++ #[test]+ fn missing_palette_entries_are_black() {+ let b = ilbm(1, 1, 2, &[[10, 20, 30]], 0, &[0x80, 0, 0x80, 0]);+ assert_eq!(decode(&b, &Limits::default()).unwrap().data(), &[0, 0, 0]);+ }+#[test]fn hostile_input_errors_not_panics() {// Truncations of a valid file never panic.diff --git a/crates/codecs/src/format.rs b/crates/codecs/src/format.rsindex 6d89bf241..1aa5b9e84 100644--- a/crates/codecs/src/format.rs+++ b/crates/codecs/src/format.rs@@ -274,7 +274,7 @@ pub fn caps(format: Format) -> FormatCaps {// `text`: the DPX header fields only (see `fidelity_warnings_with`).Format::Dpx => FormatCaps { depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb, L::Rgba], text: true, ..base },Format::Cineon => FormatCaps { read: true, write: false, depths: &[S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base },- Format::Iff => FormatCaps { read: true, write: false, depths: &[S::U8], layouts: &[L::Rgb], alpha: false, ..base },+ Format::Iff => FormatCaps { read: true, write: false, depths: &[S::U8], layouts: &[L::Rgb, L::Rgba], alpha: true, ..base },}}diff --git a/crates/codecs/tests/detect_caps.rs b/crates/codecs/tests/detect_caps.rsindex c0cd569e3..87b2755cc 100644--- a/crates/codecs/tests/detect_caps.rs+++ b/crates/codecs/tests/detect_caps.rs@@ -206,6 +206,8 @@ fn detect_magic_iff() {assert_eq!(from_extension("art.LBM"), Some(Format::Iff));assert_eq!(from_extension("art.ilbm"), Some(Format::Iff));assert!(caps(Format::Iff).read && !caps(Format::Iff).write, "IFF is read-only");+ assert!(caps(Format::Iff).alpha);+ assert!(caps(Format::Iff).layouts.contains(&photocraft_codecs::ChannelLayout::Rgba));assert!(ASYMMETRIC_EXCEPTIONS.iter().any(|(f, _)| *f == Format::Iff));// A bare FORM without a bitmap type is not IFF.assert_eq!(detect(b"FORM\0\0\0\x14AIFF"), None);