From b643432db5ec91836690fdcfafb4029f742d2f18 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:59:09 +0100 Subject: [PATCH] image-codecs: read a raster's resolution from its headers (resolution_probe) file_resolution(bytes) gives the pixels per inch a JPEG, PNG, WebP, BMP or classic TIFF records without decoding pixels. JPEG uses the decoder's marker scan (Photoshop, XMP, EXIF, then JFIF), PNG reads pHYs as the decoder does and falls back to eXIf, WebP its EXIF chunk, TIFF its IFD0 tags; BMP reads the info header's pixels per metre. Values outside 1-100000 ppi count as none. DesignCraft (crates/images/src/resolution.rs) and FigCraft (crates/format/src/density.rs) each carry their own marker and chunk walkers for this; they can switch to the shared probe. Part of #778 and #785. --- crates/image-codecs/README.md | 5 + crates/image-codecs/src/lib.rs | 2 + crates/image-codecs/src/resolution_probe.rs | 121 ++++++++++ .../src/resolution_probe_tests.rs | 214 ++++++++++++++++++ 4 files changed, 342 insertions(+) create mode 100644 crates/image-codecs/src/resolution_probe.rs create mode 100644 crates/image-codecs/src/resolution_probe_tests.rs diff --git a/crates/image-codecs/README.md b/crates/image-codecs/README.md index dbd7dca4c3..2b8b2d1277 100644 --- a/crates/image-codecs/README.md +++ b/crates/image-codecs/README.md @@ -236,6 +236,11 @@ embedded table (and a PNG's transparent entry) so `photocraft-io` can open them the EXIF and XMP resolution are rewritten to the image's (`export_exif`, `export_xmp`): the RATIONAL values in place, ResolutionUnit set to 2, an entry that can't be rewritten removed from IFD0 without moving any other data. IFD1 (the thumbnail's) is left as it is. +* **Resolution without decoding** (module `resolution_probe`): `file_resolution(bytes)` reads + the pixels per inch a JPEG, PNG, WebP, BMP or classic TIFF records from its headers alone, for + placing a picture at its physical size before (or without) decoding it. JPEG, PNG, WebP and + TIFF answer as `decode` would; BMP reads the info header's pixels per metre. Values outside + 1–100 000 ppi count as none. * **JPEG** * 8-bit only. Neither decoder backend supports 12-bit. * A file cut off inside its image data decodes leniently (a baseline JPEG's missing rows come diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 24fd32deb7..c50ff62f7c 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -34,6 +34,7 @@ pub mod orientation; pub mod png_probe; pub mod raw_metadata; pub mod resolution; +pub mod resolution_probe; pub mod source_settings; pub mod tiff_probe; pub mod web; @@ -67,6 +68,7 @@ pub use crate::options::{ }; pub use crate::orientation::{OrientSteps, exif_orientation, upright_exif, upright_xmp}; pub use crate::resolution::{exif_resolution, export_exif, export_xmp, photoshop_resolution, xmp_resolution}; +pub use crate::resolution_probe::file_resolution; pub use crate::source_settings::{WebpEncoding, jpeg_quality, webp_encoding}; pub use half::f16; diff --git a/crates/image-codecs/src/resolution_probe.rs b/crates/image-codecs/src/resolution_probe.rs new file mode 100644 index 0000000000..2c5166f09f --- /dev/null +++ b/crates/image-codecs/src/resolution_probe.rs @@ -0,0 +1,121 @@ +//! The resolution a raster file records, read from its headers without decoding any pixels. +//! +//! A layout app placing a picture wants its physical size before (or without) decoding it: a +//! 600-pixel photo saved at 600 ppi places one inch wide. [`file_resolution`] answers from the +//! container alone: +//! +//! - JPEG: the resolution [`crate::decode`] reports, from the same marker scan (stopping at the +//! start of scan): Photoshop ResolutionInfo, then XMP, then EXIF, then the JFIF density. +//! - PNG: the `pHYs` chunk when its unit is the metre (read as the decoder reads it, so a stored +//! 11811 is 300 ppi), else the `eXIf` resolution; the chunk scan stops at `IDAT` or `IEND`. +//! - WebP: the IFD0 resolution of the `EXIF` chunk. +//! - BMP: the pixels-per-metre fields of a `BITMAPINFOHEADER` or later header. +//! - Classic TIFF: the IFD0 resolution tags. +//! +//! EXIF and TIFF resolution tags are read by [`crate::resolution::exif_resolution`]. Every scan +//! is bounded by the input length and a chunk count, and anything malformed, absent, +//! non-positive or past [`MAX_PPI`] gives `None`, so the caller keeps its own default. + +use crate::Limits; +use crate::codecs::{jpeg, png, webp}; +use crate::resolution::exif_resolution; + +/// Resolutions above this many pixels per inch are treated as corrupt. +pub const MAX_PPI: f32 = 100_000.0; + +/// PNG chunks looked at before giving up. +const MAX_CHUNKS: usize = 4096; + +const PNG_SIGNATURE: &[u8] = b"\x89PNG\r\n\x1a\n"; + +/// Pixels per inch (horizontal, vertical) that `bytes` records in its headers, or `None` when +/// the format is not one listed in the [module documentation](self) or carries no usable value. +pub fn file_resolution(bytes: &[u8]) -> Option<(f32, f32)> { + let ppi = if bytes.starts_with(b"\xFF\xD8\xFF") { + jpeg::scan_metadata(bytes).dpi + } else if bytes.starts_with(PNG_SIGNATURE) { + png_phys(bytes).or_else(|| png_exif(bytes)) + } else if bytes.starts_with(b"RIFF") && bytes.get(8..12) == Some(b"WEBP".as_slice()) { + webp_exif(bytes) + } else if bytes.starts_with(b"BM") { + bmp(bytes) + } else if is_classic_tiff(bytes) { + exif_resolution(bytes) + } else { + None + }; + ppi.filter(|&p| usable(p)) +} + +/// Whether both axes are finite and between 1 and [`MAX_PPI`]. +pub fn usable((x, y): (f32, f32)) -> bool { + let ok = |v: f32| v.is_finite() && (1.0..=MAX_PPI).contains(&v); + ok(x) && ok(y) +} + +fn is_classic_tiff(bytes: &[u8]) -> bool { + bytes.starts_with(b"II*\0") || bytes.starts_with(b"MM\0*") +} + +fn be_u32(b: &[u8], at: usize) -> Option { + Some(u32::from_be_bytes(b.get(at..at.checked_add(4)?)?.try_into().ok()?)) +} + +fn le_u32(b: &[u8], at: usize) -> Option { + Some(u32::from_le_bytes(b.get(at..at.checked_add(4)?)?.try_into().ok()?)) +} + +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)> { + 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)? { + b"pHYs" => return phys(bytes.get(data..data.checked_add(9)?)?), + b"IDAT" | b"IEND" => return None, + _ => pos = data.checked_add(len)?.checked_add(4)?, + } + } + None +} + +/// `pHYs`: pixels per unit in x and y, then the unit (1 = metre; 0 = aspect ratio only). +fn phys(data: &[u8]) -> Option<(f32, f32)> { + if *data.get(8)? != 1 { + return None; + } + Some((png::dpi_of_ppm(be_u32(data, 0)?), png::dpi_of_ppm(be_u32(data, 4)?))) +} + +/// The resolution of a PNG's `eXIf` chunk, read by the decoder's own header probe. +fn png_exif(bytes: &[u8]) -> Option<(f32, f32)> { + let (_, _, exif) = png::probe(bytes, &Limits::default()).ok()?; + exif_resolution(&exif?) +} + +/// The resolution of a WebP's `EXIF` chunk, read by the decoder's own container probe. +fn webp_exif(bytes: &[u8]) -> Option<(f32, f32)> { + let (_, _, exif) = webp::probe(bytes, &Limits::default()).ok()?; + exif_resolution(&exif?) +} + +/// BMP: the info header size at 14 (40 or more for a header with resolution fields), then the +/// signed pixels-per-metre values at 38 and 42. +fn bmp(bytes: &[u8]) -> Option<(f32, f32)> { + if le_u32(bytes, 14)? < 40 { + return None; + } + let (x, y) = (le_i32(bytes, 38)?, le_i32(bytes, 42)?); + if x <= 0 || y <= 0 { + return None; + } + Some((png::dpi_of_ppm(x.unsigned_abs()), png::dpi_of_ppm(y.unsigned_abs()))) +} + +#[cfg(test)] +#[path = "resolution_probe_tests.rs"] +mod tests; diff --git a/crates/image-codecs/src/resolution_probe_tests.rs b/crates/image-codecs/src/resolution_probe_tests.rs new file mode 100644 index 0000000000..10e2df4953 --- /dev/null +++ b/crates/image-codecs/src/resolution_probe_tests.rs @@ -0,0 +1,214 @@ +#![allow(clippy::unwrap_used)] + +use super::{MAX_PPI, file_resolution, usable}; +use crate::{ChannelLayout, EncodeOptions, Format, Image, SampleType}; + +/// A little- or big-endian classic TIFF whose IFD0 holds XResolution and YResolution +/// (`x`/1, `y`/1) and, when given, ResolutionUnit. +fn tiff(le: bool, x: u32, y: u32, unit: Option) -> Vec { + let u16b = |v: u16| if le { v.to_le_bytes() } else { v.to_be_bytes() }; + let u32b = |v: u32| if le { v.to_le_bytes() } else { v.to_be_bytes() }; + let entries: u16 = if unit.is_some() { 3 } else { 2 }; + let rationals = 8 + 2 + 12 * u32::from(entries) + 4; + let mut t = Vec::new(); + t.extend_from_slice(if le { b"II*\0" } else { b"MM\0*" }); + t.extend_from_slice(&u32b(8)); + t.extend_from_slice(&u16b(entries)); + for (tag, offset) in [(0x011Au16, rationals), (0x011B, rationals + 8)] { + t.extend_from_slice(&u16b(tag)); + t.extend_from_slice(&u16b(5)); + t.extend_from_slice(&u32b(1)); + t.extend_from_slice(&u32b(offset)); + } + if let Some(unit) = unit { + t.extend_from_slice(&u16b(0x0128)); + t.extend_from_slice(&u16b(3)); + t.extend_from_slice(&u32b(1)); + t.extend_from_slice(&u16b(unit)); + t.extend_from_slice(&[0, 0]); + } + t.extend_from_slice(&u32b(0)); + for v in [x, 1, y, 1] { + t.extend_from_slice(&u32b(v)); + } + t +} + +fn segment(marker: u8, payload: &[u8]) -> Vec { + let mut s = vec![0xFF, marker]; + s.extend_from_slice(&u16::try_from(payload.len() + 2).unwrap().to_be_bytes()); + s.extend_from_slice(payload); + s +} + +fn jfif(units: u8, x: u16, y: u16) -> Vec { + let mut p = b"JFIF\0\x01\x02".to_vec(); + p.push(units); + p.extend_from_slice(&x.to_be_bytes()); + p.extend_from_slice(&y.to_be_bytes()); + p.extend_from_slice(&[0, 0]); + segment(0xE0, &p) +} + +fn exif_app1(tiff: &[u8]) -> Vec { + let mut p = b"Exif\0\0".to_vec(); + p.extend_from_slice(tiff); + segment(0xE1, &p) +} + +fn jpeg(segments: &[Vec]) -> Vec { + let mut j = vec![0xFF, 0xD8]; + for s in segments { + j.extend_from_slice(s); + } + // Start of scan, then entropy data holding bytes that look like an APP0 marker. + j.extend_from_slice(&segment(0xDA, &[1, 1, 0, 0, 63, 0])); + j.extend_from_slice(&jfif(1, 9, 9)); + j.extend_from_slice(&[0xFF, 0xD9]); + j +} + +fn png_chunk(kind: &[u8; 4], data: &[u8]) -> Vec { + let mut c = u32::try_from(data.len()).unwrap().to_be_bytes().to_vec(); + c.extend_from_slice(kind); + c.extend_from_slice(data); + c.extend_from_slice(&[0, 0, 0, 0]); // the CRC is not checked by a header probe + c +} + +fn png(chunks: &[Vec]) -> Vec { + let mut p = b"\x89PNG\r\n\x1a\n".to_vec(); + p.extend_from_slice(&png_chunk(b"IHDR", &[0, 0, 0, 1, 0, 0, 0, 1, 8, 6, 0, 0, 0])); + for c in chunks { + p.extend_from_slice(c); + } + p.extend_from_slice(&png_chunk(b"IEND", &[])); + p +} + +fn phys(x: u32, y: u32, unit: u8) -> Vec { + let mut d = x.to_be_bytes().to_vec(); + d.extend_from_slice(&y.to_be_bytes()); + d.push(unit); + png_chunk(b"pHYs", &d) +} + +fn bmp(header_size: u32, x: i32, y: i32) -> Vec { + let mut b = vec![0u8; 54]; + b[..2].copy_from_slice(b"BM"); + b[14..18].copy_from_slice(&header_size.to_le_bytes()); + b[38..42].copy_from_slice(&x.to_le_bytes()); + b[42..46].copy_from_slice(&y.to_le_bytes()); + b +} + +fn close(got: Option<(f32, f32)>, want: (f32, f32)) { + let (x, y) = got.unwrap(); + assert!((x - want.0).abs() < 0.05 && (y - want.1).abs() < 0.05, "{x} {y} vs {want:?}"); +} + +#[test] +fn jpeg_jfif_density_in_inches_and_centimetres() { + assert_eq!(file_resolution(&jpeg(&[jfif(1, 300, 150)])), Some((300.0, 150.0))); + close(file_resolution(&jpeg(&[jfif(2, 100, 100)])), (254.0, 254.0)); + // Units 0 is an aspect ratio, not a density. + assert_eq!(file_resolution(&jpeg(&[jfif(0, 1, 1)])), None); + // Nothing in the header: the APP0 look-alike after the start of scan is not read. + assert_eq!(file_resolution(&jpeg(&[])), None); +} + +#[test] +fn jpeg_exif_wins_over_jfif_and_non_exif_app1_is_skipped() { + let xmp = segment(0xE1, b"http://ns.adobe.com/xap/1.0/\0"); + let exif = exif_app1(&tiff(false, 600, 600, Some(2))); + assert_eq!(file_resolution(&jpeg(&[jfif(1, 72, 72), xmp.clone(), exif])), Some((600.0, 600.0))); + // An unusable EXIF value falls back to JFIF. + let zero = exif_app1(&tiff(true, 0, 0, None)); + assert_eq!(file_resolution(&jpeg(&[jfif(1, 96, 96), xmp, zero])), Some((96.0, 96.0))); +} + +#[test] +fn jpeg_fill_bytes_and_standalone_markers_are_stepped_over() { + let mut j = vec![0xFF, 0xD8, 0xFF, 0xFF, 0xFF, 0x01, 0xFF, 0xD0]; + j.extend_from_slice(&jfif(1, 200, 200)); + assert_eq!(file_resolution(&j), Some((200.0, 200.0))); +} + +#[test] +fn png_phys_in_metres_only_before_image_data() { + assert_eq!(file_resolution(&png(&[phys(11_811, 11_811, 1)])), Some((300.0, 300.0))); + assert_eq!(file_resolution(&png(&[phys(2, 1, 0)])), None); + assert_eq!(file_resolution(&png(&[png_chunk(b"IDAT", &[0; 4]), phys(11_811, 11_811, 1)])), None); + assert_eq!(file_resolution(&png(&[png_chunk(b"tEXt", b"a\0b"), phys(3_780, 3_780, 1)])).map(|p| p.0.round()), Some(96.0)); + assert_eq!(file_resolution(&png(&[])), None); +} + +/// What the probe reads is what a full decode reports, for each format the encoder writes with +/// a resolution. +#[test] +fn agrees_with_decode_for_encoded_files() { + let mut img = Image::from_raw(2, 1, ChannelLayout::Rgb, SampleType::U8, vec![10, 20, 30, 40, 50, 60]).unwrap(); + img.meta.dpi = Some((300.0, 150.0)); + for format in [Format::Jpeg, Format::Png, Format::WebP, Format::Tiff] { + let bytes = crate::encode(&img, format, &EncodeOptions::default()).unwrap(); + let decoded = crate::decode_as(format, &bytes).unwrap().meta.dpi; + assert_eq!(file_resolution(&bytes), decoded, "{format:?}"); + assert_eq!(decoded, Some((300.0, 150.0)), "{format:?}"); + } +} + +#[test] +fn webp_without_exif_has_none() { + let img = Image::from_raw(1, 1, ChannelLayout::Rgb, SampleType::U8, vec![1, 2, 3]).unwrap(); + let bytes = crate::encode(&img, Format::WebP, &EncodeOptions::default()).unwrap(); + assert_eq!(file_resolution(&bytes), None); + // A RIFF/WEBP container the decoder's probe refuses gives nothing either. + assert_eq!(file_resolution(b"RIFF\x04\0\0\0WEBP"), None); +} + +#[test] +fn bmp_pixels_per_metre() { + close(file_resolution(&bmp(40, 3_780, 7_559)), (96.0, 192.0)); + assert_eq!(file_resolution(&bmp(12, 3_780, 3_780)), None); + assert_eq!(file_resolution(&bmp(40, -3_780, 3_780)), None); + assert_eq!(file_resolution(&bmp(40, 0, 0)), None); +} + +#[test] +fn tiff_tags_in_both_byte_orders_and_units() { + assert_eq!(file_resolution(&tiff(true, 300, 300, None)), Some((300.0, 300.0))); + close(file_resolution(&tiff(false, 100, 50, Some(3))), (254.0, 127.0)); + assert_eq!(file_resolution(&tiff(true, 300, 300, Some(1))), None); +} + +#[test] +fn other_formats_and_absurd_values_give_none() { + assert_eq!(file_resolution(b"GIF89a\x01\0\x01\0"), None); + assert_eq!(file_resolution(b""), None); + assert_eq!(file_resolution(&tiff(true, 1_000_000, 1_000_000, None)), None); + assert!(usable((1.0, MAX_PPI))); + assert!(!usable((0.5, 72.0))); + assert!(!usable((f32::NAN, 72.0))); +} + +#[test] +fn truncated_and_corrupted_headers_never_panic() { + let samples = [ + jpeg(&[jfif(1, 300, 300), exif_app1(&tiff(false, 600, 600, Some(2)))]), + png(&[phys(11_811, 11_811, 1)]), + bmp(40, 3_780, 3_780), + tiff(false, 300, 300, Some(2)), + ]; + for s in &samples { + for cut in 0..=s.len() { + let _ = file_resolution(&s[..cut]); + } + for at in 0..s.len() { + for v in [0u8, 0x7F, 0xFF] { + let mut c = s.clone(); + c[at] = v; + let _ = file_resolution(&c); + } + } + } +}