From c60e7a20063313b80a994075adb4f797b24ab4f1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 21 Nov 2026 11:39:45 -0500 Subject: [PATCH] image-codecs: expose raw PNG pHYs pixels_per_meter reader (#785) (#2865) --- crates/image-codecs/src/png_probe.rs | 1 + crates/image-codecs/src/resolution_probe.rs | 18 ++++++++++++++---- .../image-codecs/src/resolution_probe_tests.rs | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/crates/image-codecs/src/png_probe.rs b/crates/image-codecs/src/png_probe.rs index e7af74334b..7c27f966bb 100644 --- a/crates/image-codecs/src/png_probe.rs +++ b/crates/image-codecs/src/png_probe.rs @@ -1,4 +1,5 @@ //! Header-only PNG inspection without inflating image data. +pub use crate::resolution_probe::png_pixels_per_meter as pixels_per_meter; use crate::{CodecError, Format}; fn err(message: &str) -> CodecError { CodecError::malformed(Format::Png, message) diff --git a/crates/image-codecs/src/resolution_probe.rs b/crates/image-codecs/src/resolution_probe.rs index 87729f18ec..7363e45a74 100644 --- a/crates/image-codecs/src/resolution_probe.rs +++ b/crates/image-codecs/src/resolution_probe.rs @@ -69,14 +69,19 @@ fn le_i32(b: &[u8], at: usize) -> Option { Some(i32::from_le_bytes(b.get(at..at.checked_add(4)?)?.try_into().ok()?)) } -fn png_phys(bytes: &[u8]) -> Option<(f32, f32)> { +/// Raw PNG `pHYs` pixels per metre `(x, y)` before any DPI snapping, when the chunk precedes +/// `IDAT`/`IEND` within the shared chunk limit, has unit `1` (metre), and both axes are nonzero. +pub fn png_pixels_per_meter(bytes: &[u8]) -> Option<(u32, u32)> { + if !bytes.starts_with(PNG_SIGNATURE) { + return None; + } let mut pos = PNG_SIGNATURE.len(); for _ in 0..MAX_CHUNKS { let len = usize::try_from(be_u32(bytes, pos)?).ok()?; let data = pos.checked_add(8)?; match bytes.get(pos.checked_add(4)?..data)? { // A `pHYs` chunk holds exactly nine bytes; the whole chunk, CRC included, must be there. - b"pHYs" if len == 9 => return bytes.get(data..data.checked_add(13)?).and_then(|chunk| phys(chunk.get(..9)?)), + b"pHYs" if len == 9 => return bytes.get(data..data.checked_add(13)?).and_then(|chunk| phys_ppm(chunk.get(..9)?)), b"pHYs" => return None, b"IDAT" | b"IEND" => return None, _ => pos = data.checked_add(len)?.checked_add(4)?, @@ -85,15 +90,20 @@ fn png_phys(bytes: &[u8]) -> Option<(f32, f32)> { None } +fn png_phys(bytes: &[u8]) -> Option<(f32, f32)> { + let (x, y) = png_pixels_per_meter(bytes)?; + Some((png::dpi_of_ppm(x), png::dpi_of_ppm(y))) +} + /// `pHYs`: pixels per unit in x and y, then the unit (1 = metre; 0 = aspect ratio only). -fn phys(data: &[u8]) -> Option<(f32, f32)> { +fn phys_ppm(data: &[u8]) -> Option<(u32, u32)> { let (x, y) = (be_u32(data, 0)?, be_u32(data, 4)?); // As the decoder does: a zero count, or a unit other than the metre, records no resolution, // so the `eXIf` resolution can still answer. if *data.get(8)? != 1 || x == 0 || y == 0 { return None; } - Some((png::dpi_of_ppm(x), png::dpi_of_ppm(y))) + Some((x, y)) } /// The resolution of a PNG's `eXIf` chunk, read by the decoder's own header probe. diff --git a/crates/image-codecs/src/resolution_probe_tests.rs b/crates/image-codecs/src/resolution_probe_tests.rs index 1a650ec3f3..a7a8d549d3 100644 --- a/crates/image-codecs/src/resolution_probe_tests.rs +++ b/crates/image-codecs/src/resolution_probe_tests.rs @@ -301,3 +301,21 @@ fn png_zero_phys_leaves_the_exif_resolution() { assert_eq!(file_resolution(&png(&[zero(0, 0), exif.clone()])), Some((600.0, 600.0))); assert_eq!(file_resolution(&png(&[zero(11_811, 0), exif])), Some((600.0, 600.0))); } + +#[test] +fn png_pixels_per_meter_preserves_raw_values_and_shared_chunk_limit() { + assert_eq!(crate::png_probe::pixels_per_meter(&png(&[phys(1181, 2362, 1)])), Some((1181, 2362))); + assert_eq!(crate::png_probe::pixels_per_meter(&png(&[phys(1181, 1181, 0)])), None); + assert_eq!(crate::png_probe::pixels_per_meter(&png(&[phys(0, 1181, 1)])), None); + assert_eq!(crate::png_probe::pixels_per_meter(b"not a png"), None); + assert_eq!(crate::png_probe::pixels_per_meter(&png(&[png_chunk(b"IDAT", &[]), phys(1181, 1181, 1)])), None); + + let text = png_chunk(b"tEXt", b"k\x00v"); + let mut at_limit = vec![text.clone(); super::MAX_CHUNKS - 2]; + at_limit.push(phys(1181, 1181, 1)); + assert_eq!(crate::png_probe::pixels_per_meter(&png(&at_limit)), Some((1181, 1181))); + + let mut past_limit = vec![text; super::MAX_CHUNKS - 1]; + past_limit.push(phys(1181, 1181, 1)); + assert_eq!(crate::png_probe::pixels_per_meter(&png(&past_limit)), None); +}