diff --git a/crates/image-codecs/README.md b/crates/image-codecs/README.md index bcd42e4e8..eb7c37362 100644 --- a/crates/image-codecs/README.md +++ b/crates/image-codecs/README.md @@ -237,6 +237,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 8c29ed52d..dc2ac2933 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 000000000..f7b9aff83 --- /dev/null +++ b/crates/image-codecs/src/resolution_probe.rs @@ -0,0 +1,126 @@ +//! 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)? { + // 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" => return None, + 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)> { + 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))) +} + +/// 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 000000000..65a797853 --- /dev/null +++ b/crates/image-codecs/src/resolution_probe_tests.rs @@ -0,0 +1,266 @@ +#![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); + } + } + } +} + +#[test] +fn png_phys_must_be_a_whole_nine_byte_chunk() { + // A pHYs that declares no data doesn't borrow the bytes after it. + let mut empty = png_chunk(b"pHYs", &[]); + empty.extend_from_slice(&phys(11_811, 11_811, 1)[8..]); + assert_eq!(file_resolution(&png(&[empty])), None); + // Cut off before its CRC. + let whole = png(&[phys(11_811, 11_811, 1)]); + let crc_at = whole.len() - 12 - 4; + assert_eq!(file_resolution(&whole[..crc_at]), None); + assert_eq!(file_resolution(&whole[..crc_at + 4]), Some((300.0, 300.0))); +} + +/// A PNG chunk with its real CRC, for the decoder's own probe, which checks it. +fn crc_chunk(kind: &[u8; 4], data: &[u8]) -> Vec { + let mut c = png_chunk(kind, data); + let body = c.get(4..c.len() - 4).unwrap().to_vec(); + let mut crc = !0u32; + for b in body { + crc ^= u32::from(b); + for _ in 0..8 { + crc = (crc >> 1) ^ (0xEDB8_8320 & (crc & 1).wrapping_neg()); + } + } + let n = c.len(); + c[n - 4..].copy_from_slice(&(!crc).to_be_bytes()); + c +} + +#[test] +fn png_zero_phys_leaves_the_exif_resolution() { + let exif = crc_chunk(b"eXIf", &tiff(false, 600, 600, Some(2))); + let zero = |x: u32, y: u32| { + let mut d = x.to_be_bytes().to_vec(); + d.extend_from_slice(&y.to_be_bytes()); + d.push(1); + crc_chunk(b"pHYs", &d) + }; + let png = |chunks: &[Vec]| { + let mut p = b"\x89PNG\r\n\x1a\n".to_vec(); + p.extend_from_slice(&crc_chunk(b"IHDR", &[0, 0, 0, 1, 0, 0, 0, 1, 8, 6, 0, 0, 0])); + chunks.iter().for_each(|c| p.extend_from_slice(c)); + // The decoder's probe reads up to the image data, so there has to be some. + p.extend_from_slice(&crc_chunk(b"IDAT", &[0x78, 0x9c, 0x63, 0, 0, 0, 1, 0, 1])); + p.extend_from_slice(&crc_chunk(b"IEND", &[])); + p + }; + assert_eq!(file_resolution(&png(std::slice::from_ref(&exif))), Some((600.0, 600.0))); + 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))); +}