From 1bce1af0eb5e0fd512226ea1247bef958a87e751 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:39:50 +0000 Subject: [PATCH] Expose early PNG dimension preflight --- crates/image-codecs/src/png_probe.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/crates/image-codecs/src/png_probe.rs b/crates/image-codecs/src/png_probe.rs index 6ec610c14..9cb40f402 100644 --- a/crates/image-codecs/src/png_probe.rs +++ b/crates/image-codecs/src/png_probe.rs @@ -4,6 +4,17 @@ fn err(message: &str) -> CodecError { CodecError::malformed(Format::Png, message) } +/// Dimensions declared in a PNG IHDR prefix, without checking the rest of the file. +/// Useful only for an early allocation limit; use [`header`] for structural inspection. +pub fn declared_dimensions(b: &[u8]) -> Option<(u32, u32)> { + if !b.starts_with(b"\x89PNG\r\n\x1a\n") || b.get(12..16) != Some(b"IHDR") { + return None; + } + let w = u32::from_be_bytes(b.get(16..20)?.try_into().ok()?); + let h = u32::from_be_bytes(b.get(20..24)?.try_into().ok()?); + Some((w, h)) +} + /// 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") {