From 32eff39b74661ad121a0aa08a36b69c01defaf58 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:39:23 +0000 Subject: [PATCH] Share PNG header and color chunk inspection --- crates/image-codecs/src/lib.rs | 1 + crates/image-codecs/src/png_probe.rs | 169 +++++++++++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 crates/image-codecs/src/png_probe.rs diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 6516b9ff2..d8659bf54 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -28,6 +28,7 @@ mod inspection; mod lab; mod options; pub mod orientation; +pub mod png_probe; pub mod resolution; pub mod source_settings; pub mod web; diff --git a/crates/image-codecs/src/png_probe.rs b/crates/image-codecs/src/png_probe.rs new file mode 100644 index 000000000..6ec610c14 --- /dev/null +++ b/crates/image-codecs/src/png_probe.rs @@ -0,0 +1,169 @@ +//! Header-only PNG inspection without inflating image data. +use crate::{CodecError, Format}; +fn err(message: &str) -> CodecError { + CodecError::malformed(Format::Png, message) +} + +/// Stored dimensions and EXIF orientation. This does not validate CRCs or compressed pixels. +pub fn header(b: &[u8]) -> Result<(u32, u32, u16), CodecError> { + if !b.starts_with(b"\x89PNG\r\n\x1a\n") { + return Err(err("bad signature")); + } + let be32 = |p: usize| b.get(p..p.checked_add(4)?).map(|v| u32::from_be_bytes([v[0], v[1], v[2], v[3]])); + if be32(8) != Some(13) || b.get(12..16) != Some(b"IHDR") { + return Err(err("missing IHDR")); + } + let ihdr = b.get(16..29).ok_or_else(|| err("truncated IHDR"))?; + let (w, h) = (u32::from_be_bytes([ihdr[0], ihdr[1], ihdr[2], ihdr[3]]), u32::from_be_bytes([ihdr[4], ihdr[5], ihdr[6], ihdr[7]])); + let (depth, color, compression, filter, interlace) = (ihdr[8], ihdr[9], ihdr[10], ihdr[11], ihdr[12]); + if w > i32::MAX as u32 || h > i32::MAX as u32 { + return Err(err("dimensions out of range")); + } + if w == 0 || h == 0 { + return Err(err("zero dimension")); + } + let depth_ok = match color { + 0 => matches!(depth, 1 | 2 | 4 | 8 | 16), + 3 => matches!(depth, 1 | 2 | 4 | 8), + 2 | 4 | 6 => matches!(depth, 8 | 16), + _ => false, + }; + if !depth_ok || compression != 0 || filter != 0 || interlace > 1 { + return Err(err("invalid IHDR")); + } + let (mut p, mut palette, mut idat, mut after_idat, mut exif) = (8usize, false, false, false, None); + while let Some(len) = be32(p) { + let Some(kind) = p.checked_add(4).and_then(|k| b.get(k..k.checked_add(4)?)) else { break }; + if idat && kind != b"IDAT" { + // a chunk follows the image data: the IDAT run is complete + after_idat = true; + } + if kind == b"IEND" { + break; + } + let start = p.saturating_add(8); + let data = (len <= i32::MAX as u32).then(|| start.checked_add(len as usize).and_then(|end| b.get(start..end))).flatten(); + let Some(data) = data else { + if after_idat { + break; // a damaged chunk after the image data: a decode ignores it too + } + return Err(err("truncated")); + }; + match kind { + b"PLTE" => palette = true, + b"IDAT" if after_idat => {} // a stray IDAT after other chunks: the first run is the image + b"IDAT" => { + if color == 3 && !palette { + return Err(err("indexed image without a palette")); + } + idat = true; + } + b"eXIf" if exif.is_none() => exif = Some(data.strip_prefix(b"Exif\0\0").unwrap_or(data)), + _ => {} + } + p = start.saturating_add(data.len()).saturating_add(4); + } + if !idat { + return Err(err("no image data")); + } + if !after_idat { + return Err(err("truncated image data")); + } + Ok((w, h, exif.map_or(1, crate::exif_orientation))) +} + +/// Container color declarations preceding image data. Raw values leave color policy to callers. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct ColorChunks { + pub srgb: bool, + pub gamma: Option, + pub chromaticities: Option<[f64; 8]>, +} + +/// Read bounded color chunks before image data; malformed tails are ignored. +pub fn color_chunks(b: &[u8]) -> Option { + if !b.starts_with(b"\x89PNG\r\n\x1a\n") { + return None; + } + let mut result = ColorChunks::default(); + let mut p = 8usize; + while let Some(length) = b.get(p..p.checked_add(4)?) { + let len = u32::from_be_bytes(length.try_into().ok()?) as usize; + let start = p.checked_add(8)?; + let Some(kind) = b.get(p.checked_add(4)?..start) else { break }; + let Some(payload) = start.checked_add(len).and_then(|end| b.get(start..end)) else { break }; + match kind { + b"sRGB" => result.srgb = true, + b"gAMA" if payload.len() == 4 => { + let v = u32::from_be_bytes(payload.try_into().ok()?) as f32 / 100_000.0; + if v > 0.0 && v.is_finite() { + result.gamma = Some(v); + } + } + b"cHRM" if payload.len() == 32 => { + let mut vals = [0.0; 8]; + for (i, dst) in vals.iter_mut().enumerate() { + let raw = i32::from_be_bytes(payload[i * 4..i * 4 + 4].try_into().ok()?); + *dst = f64::from(raw) / 100_000.0; + } + result.chromaticities = Some(vals); + } + b"IDAT" | b"IEND" => break, + _ => {} + } + let Some(next) = start.checked_add(len).and_then(|end| end.checked_add(4)) else { break }; + p = next; + } + Some(result) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn chunk(out: &mut Vec, kind: &[u8; 4], data: &[u8]) { + out.extend_from_slice(&(data.len() as u32).to_be_bytes()); + out.extend_from_slice(kind); + out.extend_from_slice(data); + out.extend_from_slice(&[0; 4]); // Header inspection deliberately does not validate CRCs. + } + + fn png(color: u8) -> Vec { + let mut bytes = b"\x89PNG\r\n\x1a\n".to_vec(); + let mut ihdr = Vec::new(); + ihdr.extend_from_slice(&2u32.to_be_bytes()); + ihdr.extend_from_slice(&3u32.to_be_bytes()); + ihdr.extend_from_slice(&[8, color, 0, 0, 0]); + chunk(&mut bytes, b"IHDR", &ihdr); + bytes + } + + #[test] + fn header_checks_structure_and_reads_late_orientation_without_crc_validation() { + let mut bytes = png(2); + chunk(&mut bytes, b"IDAT", &[1]); + let exif = b"II\x2a\x00\x08\x00\x00\x00\x01\x00\x12\x01\x03\x00\x01\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00"; + chunk(&mut bytes, b"eXIf", exif); + chunk(&mut bytes, b"IEND", &[]); + assert_eq!(header(&bytes).ok(), Some((2, 3, 6))); + assert!(header(&bytes[..34]).is_err()); + let mut indexed = png(3); + chunk(&mut indexed, b"IDAT", &[1]); + chunk(&mut indexed, b"IEND", &[]); + assert!(header(&indexed).is_err()); + indexed[25] = 7; + assert!(header(&indexed).is_err()); + } + + #[test] + fn reads_color_chunks_only_before_image_data() { + let mut bytes = png(2); + chunk(&mut bytes, b"gAMA", &45455u32.to_be_bytes()); + chunk(&mut bytes, b"IDAT", &[1]); + chunk(&mut bytes, b"sRGB", &[0]); + let color = color_chunks(&bytes).unwrap(); + assert_eq!(color.gamma, Some(0.45455)); + assert!(!color.srgb); + assert!(color_chunks(b"not png").is_none()); + } +}