From 1d52e89405ff9237c53f34584ff97fef8decc611 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 07:11:11 -0500 Subject: [PATCH] Share raster metadata parsing with craft-metadata --- Cargo.lock | 1 + crates/image-codecs/Cargo.toml | 1 + crates/image-codecs/src/codecs/tiff/bands.rs | 35 +- crates/image-codecs/src/exif_color.rs | 68 +-- crates/image-codecs/src/orientation.rs | 231 +------- crates/image-codecs/src/resolution.rs | 578 +------------------ 6 files changed, 38 insertions(+), 876 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0bc90ee3d..9702836da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1758,6 +1758,7 @@ version = "0.1.0" dependencies = [ "craft-heif", "craft-jpeg", + "craft-metadata", "exr", "flate2", "half", diff --git a/crates/image-codecs/Cargo.toml b/crates/image-codecs/Cargo.toml index 30f7f7046..b893c347a 100644 --- a/crates/image-codecs/Cargo.toml +++ b/crates/image-codecs/Cargo.toml @@ -30,6 +30,7 @@ zune-jpeg = "0.5" zune-core = "0.5" jpeg-encoder = "0.6" craft-jpeg = { workspace = true } +craft-metadata = { workspace = true } zune-jpegxl = { version = "=0.5.2", default-features = false, features = ["std"] } craft-heif = { workspace = true, optional = true } diff --git a/crates/image-codecs/src/codecs/tiff/bands.rs b/crates/image-codecs/src/codecs/tiff/bands.rs index 093f90efd..117c95ded 100644 --- a/crates/image-codecs/src/codecs/tiff/bands.rs +++ b/crates/image-codecs/src/codecs/tiff/bands.rs @@ -230,16 +230,7 @@ fn decode_band( } continue; } - if pref.len > lay.scratch_cap { - return Err(CodecError::LimitExceeded(format!("a {} byte strip or tile", pref.len))); - } - if scratch.len() < pref.len { - scratch - .try_reserve_exact(pref.len - scratch.len()) - .map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {} bytes for a strip or tile", pref.len)))?; - scratch.resize(pref.len, 0); - } - let buf = scratch.get_mut(..pref.len).ok_or_else(|| err("scratch buffer"))?; + let buf = chunk_scratch(lay, scratch, pref.len)?; match dec.read_chunk_bytes(chunk, buf) { Ok(()) => tally.decoded += 1, Err(e) if is_eof(&e) => { @@ -284,6 +275,19 @@ fn deposit_extras(lay: &Layout, src: &[u8], dst: &mut [u8], n: usize, x0: usize) } } +fn chunk_scratch<'a>(lay: &Layout, scratch: &'a mut Vec, len: usize) -> Result<&'a mut [u8], CodecError> { + if len > lay.scratch_cap { + return Err(CodecError::LimitExceeded(format!("a {} byte strip or tile", len))); + } + if scratch.len() < len { + scratch + .try_reserve_exact(len - scratch.len()) + .map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {} bytes for a strip or tile", len)))?; + scratch.resize(len, 0); + } + scratch.get_mut(..len).ok_or_else(|| err("scratch buffer")) +} + /// Planar files store each extra sample as its own plane. Read those planes into `extra`. fn read_planar_extras( lay: &Layout, @@ -310,16 +314,7 @@ fn read_planar_extras( .ok_or_else(|| err("chunk index out of range"))?; let pref = dec.image_chunk_buffer_layout(chunk).map_err(map_err)?; let stride = pref.row_stride.map(|s| s.get()).ok_or_else(|| err("empty chunk row"))?; - if pref.len > lay.scratch_cap { - return Err(CodecError::LimitExceeded(format!("a {} byte strip or tile", pref.len))); - } - if scratch.len() < pref.len { - scratch - .try_reserve_exact(pref.len - scratch.len()) - .map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {} bytes for a strip or tile", pref.len)))?; - scratch.resize(pref.len, 0); - } - let buf = scratch.get_mut(..pref.len).ok_or_else(|| err("scratch buffer"))?; + let buf = chunk_scratch(lay, scratch, pref.len)?; match dec.read_chunk_bytes(chunk, buf) { Ok(()) => tally.decoded += 1, Err(e) if is_eof(&e) => { diff --git a/crates/image-codecs/src/exif_color.rs b/crates/image-codecs/src/exif_color.rs index 4259bb15c..8a35907ff 100644 --- a/crates/image-codecs/src/exif_color.rs +++ b/crates/image-codecs/src/exif_color.rs @@ -1,70 +1,16 @@ -//! The EXIF ColorSpace tag (0xA001, in the Exif sub-IFD that IFD0's tag 0x8769 points to): -//! 1 = sRGB, 0xFFFF = uncalibrated (EXIF 2.3, §4.6.5). -//! -//! Cameras write it instead of an ICC profile. Photoshop opens a file with no embedded profile but -//! ColorSpace = sRGB as sRGB, unless Preferences › File Handling › Ignore EXIF Profile Tag is on. -//! Anything malformed, truncated or missing reads as no tag. +//! Shared EXIF ColorSpace tag parsing. -use crate::orientation::{Order, ifd0_entries, tiff_body}; - -/// IFD0's pointer to the Exif sub-IFD. -const TAG_EXIF_IFD: u16 = 0x8769; -/// The ColorSpace tag in the Exif sub-IFD. -const TAG_COLOR_SPACE: u16 = 0xA001; -const TYPE_SHORT: u16 = 3; -const TYPE_LONG: u16 = 4; -/// TIFF type 13: an IFD offset (some writers use it for sub-IFD pointers). -const TYPE_IFD: u16 = 13; -/// Bytes per classic TIFF IFD entry; EXIF is never BigTIFF. -const ENTRY: usize = 12; -/// No IFD can start inside the 8-byte TIFF header. -const HEADER: usize = 8; - -/// The ColorSpace value meaning sRGB. -pub const EXIF_COLOR_SPACE_SRGB: u16 = 1; - -/// The EXIF ColorSpace value of a TIFF-structured block (an EXIF payload with or without the -/// `Exif\0\0` prefix), or `None` when it has none or it is malformed. -pub fn exif_color_space(exif: &[u8]) -> Option { - let b = tiff_body(exif); - let (order, first, count, entry_bytes, big_tiff) = ifd0_entries(b)?; - if big_tiff { - return None; - } - let ptr = find(order, b, first, count, entry_bytes, TAG_EXIF_IFD)?; - if !matches!(order.u16(b, ptr.checked_add(2)?)?, TYPE_LONG | TYPE_IFD) || order.u32(b, ptr.checked_add(4)?)? != 1 { - return None; - } - let ifd = usize::try_from(order.u32(b, ptr.checked_add(8)?)?).ok()?; - if ifd < HEADER { - return None; - } - let n = usize::from(order.u16(b, ifd)?); - let first = ifd.checked_add(2)?; - // The whole table must be present. - if first.checked_add(n.checked_mul(ENTRY)?)? > b.len() { - return None; - } - let e = find(order, b, first, n, ENTRY, TAG_COLOR_SPACE)?; - if order.u32(b, e.checked_add(4)?)? != 1 { - return None; - } - match order.u16(b, e.checked_add(2)?)? { - TYPE_SHORT => order.u16(b, e.checked_add(8)?), - TYPE_LONG => u16::try_from(order.u32(b, e.checked_add(8)?)?).ok(), - _ => None, - } -} - -/// Offset of the first entry tagged `tag` in the IFD whose `count` entries start at `first`. -fn find(order: Order, b: &[u8], first: usize, count: usize, entry_bytes: usize, tag: u16) -> Option { - (0..count).filter_map(|i| first.checked_add(i.checked_mul(entry_bytes)?)).find(|&e| order.u16(b, e) == Some(tag)) -} +pub use craft_metadata::edit::{EXIF_COLOR_SPACE_SRGB, exif_color_space}; #[cfg(test)] mod tests { use super::*; + const TAG_EXIF_IFD: u16 = 0x8769; + const TAG_COLOR_SPACE: u16 = 0xA001; + const TYPE_SHORT: u16 = 3; + const TYPE_LONG: u16 = 4; + /// A little- or big-endian EXIF block: IFD0 holding only the Exif pointer, then an Exif IFD /// holding `entries` (tag, type, count, value). fn exif(big_endian: bool, entries: &[(u16, u16, u32, u32)]) -> Vec { diff --git a/crates/image-codecs/src/orientation.rs b/crates/image-codecs/src/orientation.rs index f77e6691f..2db0de75f 100644 --- a/crates/image-codecs/src/orientation.rs +++ b/crates/image-codecs/src/orientation.rs @@ -1,232 +1,15 @@ -//! EXIF / TIFF orientation (tag 274): reading it, applying it to the pixels, -//! and rewriting it to 1 ("top-left", upright) once the pixels are upright. -//! -//! Decoders apply the orientation by default (like Photoshop and every photo -//! viewer), so a document is always edited upright. Encoders then write -//! Orientation = 1, so a viewer never rotates the already-upright pixels a -//! second time. -//! -//! Orientation values (TIFF 6.0, EXIF 2.3): 1 = upright, 2 = mirrored, 3 = -//! rotated 180°, 4 = flipped vertically, 5 = transposed, 6 = needs a 90° -//! clockwise turn, 7 = transverse, 8 = needs a 90° counter-clockwise turn. -//! Anything malformed, truncated or out of range reads as 1: an IFD0 that -//! starts inside the 8-byte header or is cut off before its next-IFD pointer, -//! or an Orientation entry that is not exactly one SHORT or LONG (the spec says -//! SHORT, but some cameras and tools write LONG). When IFD0 holds -//! several Orientation entries the first one wins (like libexif). - -use std::borrow::Cow; +//! EXIF orientation transforms for decoded images. Byte-level EXIF/XMP inspection and edits are +//! shared with `craft-metadata`. use crate::error::CodecError; use crate::image::Image; +use std::borrow::Cow; -/// The TIFF/EXIF Orientation tag. -const TAG_ORIENTATION: u16 = 274; -/// Field types accepted for the Orientation tag: SHORT (the spec) and LONG -/// (written by some cameras and tools). -const TYPE_SHORT: u16 = 3; -const TYPE_LONG: u16 = 4; -/// Bytes per IFD entry. -const ENTRY: usize = 12; -/// The TIFF header is 8 bytes, so no IFD can start before it. -const HEADER: usize = 8; -/// BigTIFF uses a 16-byte header, 20-byte entries, and 8-byte offsets. -const BIG_ENTRY: usize = 20; -const BIG_HEADER: usize = 16; +pub use craft_metadata::edit::{exif_orientation, upright_exif, upright_xmp}; -/// Byte order of a TIFF structure. -#[derive(Clone, Copy)] -pub(crate) enum Order { - Little, - Big, -} - -impl Order { - pub(crate) fn u16(self, b: &[u8], at: usize) -> Option { - let s: [u8; 2] = b.get(at..at.checked_add(2)?)?.try_into().ok()?; - Some(match self { - Order::Little => u16::from_le_bytes(s), - Order::Big => u16::from_be_bytes(s), - }) - } - - pub(crate) fn u32(self, b: &[u8], at: usize) -> Option { - let s: [u8; 4] = b.get(at..at.checked_add(4)?)?.try_into().ok()?; - Some(match self { - Order::Little => u32::from_le_bytes(s), - Order::Big => u32::from_be_bytes(s), - }) - } - - pub(crate) fn u64(self, b: &[u8], at: usize) -> Option { - let s: [u8; 8] = b.get(at..at.checked_add(8)?)?.try_into().ok()?; - Some(match self { - Order::Little => u64::from_le_bytes(s), - Order::Big => u64::from_be_bytes(s), - }) - } -} - -/// The TIFF payload of an EXIF block (a JPEG-style `Exif\0\0` prefix is skipped). -pub(crate) fn tiff_body(b: &[u8]) -> &[u8] { - b.strip_prefix(b"Exif\0\0").unwrap_or(b) -} - -/// IFD0 of a TIFF structure: byte order, entry layout, and entry offsets. -/// -/// The full table and next-IFD pointer must be present, and IFD0 must start -/// after the corresponding TIFF or BigTIFF header. -pub(crate) fn ifd0_entries(b: &[u8]) -> Option<(Order, usize, usize, usize, bool)> { - let (order, big_tiff) = match b.get(0..4)? { - [b'I', b'I', 42, 0] => (Order::Little, false), - [b'M', b'M', 0, 42] => (Order::Big, false), - [b'I', b'I', 43, 0] => (Order::Little, true), - [b'M', b'M', 0, 43] => (Order::Big, true), - _ => return None, - }; - let (ifd, count_bytes, entry_bytes, next_ifd_bytes, header) = if big_tiff { - if order.u16(b, 4)? != 8 || order.u16(b, 6)? != 0 { - return None; - } - (usize::try_from(order.u64(b, 8)?).ok()?, 8usize, BIG_ENTRY, 8usize, BIG_HEADER) - } else { - (usize::try_from(order.u32(b, 4)?).ok()?, 2usize, ENTRY, 4usize, HEADER) - }; - if ifd < header { - return None; - } - let count = if big_tiff { usize::try_from(order.u64(b, ifd)?).ok()? } else { usize::from(order.u16(b, ifd)?) }; - let first = ifd.checked_add(count_bytes)?; - let next_ifd = first.checked_add(count.checked_mul(entry_bytes)?)?; - if next_ifd.checked_add(next_ifd_bytes)? > b.len() { - return None; - } - // In bounds: every entry ends at or before `next_ifd`. - Some((order, first, count, entry_bytes, big_tiff)) -} - -/// A well-formed Orientation entry's value: where it sits and its field type. -#[derive(Clone, Copy)] -struct Value { - at: usize, - ty: u16, -} - -impl Value { - /// The stored value (a SHORT or LONG, left-justified in the 4-byte field). - fn read(self, order: Order, b: &[u8]) -> Option { - match self.ty { - TYPE_SHORT => order.u16(b, self.at).map(u32::from), - _ => order.u32(b, self.at), - } - } - - /// 1 in this entry's own type, width and byte order. - fn one(self, order: Order) -> Vec { - match (self.ty, order) { - (TYPE_SHORT, Order::Little) => 1u16.to_le_bytes().to_vec(), - (TYPE_SHORT, Order::Big) => 1u16.to_be_bytes().to_vec(), - (_, Order::Little) => 1u32.to_le_bytes().to_vec(), - (_, Order::Big) => 1u32.to_be_bytes().to_vec(), - } - } -} - -/// The value of the entry at `e` if it is a well-formed Orientation entry: -/// exactly one SHORT or LONG. -fn orientation_value(order: Order, b: &[u8], e: usize, big_tiff: bool) -> Option { - let count = if big_tiff { order.u64(b, e.checked_add(4)?)? } else { u64::from(order.u32(b, e.checked_add(4)?)?) }; - if order.u16(b, e)? != TAG_ORIENTATION || count != 1 { - return None; - } - let ty = order.u16(b, e.checked_add(2)?)?; - let value_offset = if big_tiff { 12 } else { 8 }; - matches!(ty, TYPE_SHORT | TYPE_LONG).then_some(Value { at: e.checked_add(value_offset)?, ty }) -} - -/// Locates the Orientation value in IFD0. -/// -/// The **first** Orientation entry wins (like libexif and Photoshop); if that -/// one is malformed (count ≠ 1, or neither SHORT nor LONG) the tag is ignored, -/// even when a later duplicate is well formed. -fn find_entry(b: &[u8]) -> Option<(Order, Value)> { - let (order, first, count, entry_bytes, big_tiff) = ifd0_entries(b)?; - let e = (0..count).filter_map(|i| first.checked_add(i.checked_mul(entry_bytes)?)).find(|&e| order.u16(b, e) == Some(TAG_ORIENTATION))?; - Some((order, orientation_value(order, b, e, big_tiff)?)) -} - -/// The orientation (1–8) recorded in a TIFF-structured block: an EXIF payload -/// (with or without the `Exif\0\0` prefix) or a whole TIFF file. Missing, -/// malformed or out-of-range values give 1. -pub fn exif_orientation(exif: &[u8]) -> u16 { - let b = tiff_body(exif); - find_entry(b).and_then(|(order, v)| v.read(order, b)).and_then(|v| u16::try_from(v).ok()).filter(|v| (1..=8).contains(v)).unwrap_or(1) -} - -/// `exif` with its Orientation rewritten to 1, borrowed when there is nothing -/// to change (no well-formed tag, already 1, or unparseable). -/// -/// Only the value bytes of each well-formed Orientation entry change, written -/// as 1 in the entry's existing type and width (SHORT or LONG); every other -/// byte (other tags, their offset-based values, sub-IFDs) is kept as is. -/// Well-formed duplicates are all set to 1, so a reader that lets a later -/// duplicate win cannot rotate the upright pixels either. -pub fn upright_exif(exif: &[u8]) -> Cow<'_, [u8]> { - let body = tiff_body(exif); - let prefix = exif.len() - body.len(); - let Some((order, first, count, entry_bytes, big_tiff)) = ifd0_entries(body) else { - return Cow::Borrowed(exif); - }; - let values: Vec = (0..count) - .filter_map(|i| first.checked_add(i.checked_mul(entry_bytes)?)) - .filter_map(|e| orientation_value(order, body, e, big_tiff)) - .collect(); - if values.iter().all(|v| v.read(order, body) == Some(1)) { - return Cow::Borrowed(exif); - } - let mut out = exif.to_vec(); - for v in values { - let one = v.one(order); - let slot = prefix.checked_add(v.at).and_then(|at| out.get_mut(at..at.checked_add(one.len())?)); - if let Some(slot) = slot { - slot.copy_from_slice(&one); - } - } - Cow::Owned(out) -} - -/// `xmp` with any `tiff:Orientation` (attribute or element form) set to 1, -/// borrowed when there is nothing to change. -pub fn upright_xmp(xmp: &str) -> Cow<'_, str> { - const KEY: &str = "tiff:Orientation"; - if !xmp.contains(KEY) { - return Cow::Borrowed(xmp); - } - let mut out = String::with_capacity(xmp.len()); - let mut rest = xmp; - let mut changed = false; - while let Some(i) = rest.find(KEY) { - let (head, tail) = rest.split_at(i + KEY.len()); - out.push_str(head); - rest = tail; - // `tiff:Orientation="6"`, `tiff:Orientation='6'` or `6<`. - let open = rest.char_indices().take_while(|&(_, c)| c.is_ascii_whitespace() || matches!(c, '=' | '"' | '\'' | '>')).last(); - let Some((at, c)) = open else { continue }; - if !matches!(c, '"' | '\'' | '>') { - continue; - } - let start = at + 1; - let digits = rest.get(start..).map_or(0, |s| s.bytes().take_while(u8::is_ascii_digit).count()); - if digits == 0 || rest.get(start..start + digits) == Some("1") { - continue; - } - out.push_str(rest.get(..start).unwrap_or_default()); - out.push('1'); - rest = rest.get(start + digits..).unwrap_or_default(); - changed = true; - } - out.push_str(rest); - if changed { Cow::Owned(out) } else { Cow::Borrowed(xmp) } +/// TIFF payload of an EXIF block (a JPEG-style `Exif\0\0` prefix is skipped). +pub(crate) fn tiff_body(bytes: &[u8]) -> &[u8] { + bytes.strip_prefix(b"Exif\0\0").unwrap_or(bytes) } /// Source pixel index for output pixel `(0, y2)` and the step per output column, for diff --git a/crates/image-codecs/src/resolution.rs b/crates/image-codecs/src/resolution.rs index a787cacbc..54e6759a0 100644 --- a/crates/image-codecs/src/resolution.rs +++ b/crates/image-codecs/src/resolution.rs @@ -1,575 +1,11 @@ -//! Image resolution (pixels per inch) recorded in metadata blocks: EXIF / TIFF IFD0 -//! (XResolution 282, YResolution 283, ResolutionUnit 296), XMP (`tiff:XResolution`, -//! `tiff:YResolution`, `tiff:ResolutionUnit`) and Photoshop's image resource -//! ResolutionInfo (`8BIM` resource 0x03ED); reading them, and rewriting the EXIF and XMP -//! copies on export so they agree with the resolution the file is written at. -//! -//! Implemented from TIFF 6.0 (section 8, "Baseline fields": XResolution / YResolution are -//! RATIONAL, ResolutionUnit is SHORT, 1 = no absolute unit, 2 = inch, 3 = centimetre, -//! default 2), EXIF 2.3 (same tags in the 0th IFD, same default) and the Adobe Photoshop File -//! Formats Specification (ResolutionInfo: Fixed 16.16 hRes in pixels per inch, then the -//! display units; the vertical resolution likewise). -//! -//! **Precedence** when a file carries several, as Photoshop resolves them: Photoshop's own -//! ResolutionInfo, then the XMP `tiff:` properties, then EXIF, and only then the format's own -//! density field when it is the generic JFIF one (see the JPEG decoder). JFIF density is last -//! because cameras record the real resolution in EXIF while many tools and libraries write a -//! default JFIF density of 72 (or a bare aspect ratio) without knowing it; Photoshop shows a -//! camera JPEG at its EXIF resolution whatever its JFIF header says. Formats whose own field is -//! written deliberately (PNG `pHYs`, TIFF tags) keep it first; the metadata blocks only fill in -//! when it is missing. -//! -//! Everything read here is untrusted: offsets and counts are bounds-checked, both byte orders -//! and BigTIFF-style EXIF are handled, and anything malformed reads as "no resolution" rather -//! than as a wrong one. +//! Shared EXIF/XMP/Photoshop resolution parsing and edits. -use std::borrow::Cow; +pub use craft_metadata::edit::{ + exif_resolution, exif_with_pixel_size, exif_with_resolution, exif_with_resolution_adding_missing, export_exif, + export_exif_adding_missing_resolution, export_xmp, photoshop_resolution, resolution, xmp_resolution, xmp_with_resolution, +}; -use crate::orientation::{Order, ifd0_entries, tiff_body, upright_exif, upright_xmp}; - -const TAG_X_RESOLUTION: u16 = 282; -const TAG_Y_RESOLUTION: u16 = 283; -const TAG_RESOLUTION_UNIT: u16 = 296; -const TYPE_SHORT: u16 = 3; -const TYPE_RATIONAL: u16 = 5; -const CM_PER_INCH: f64 = 2.54; -/// Photoshop's ResolutionInfo image resource. -const RESOLUTION_INFO: u16 = 0x03ED; -/// Resolutions above this (pixels per inch) are treated as damage, not data. -const MAX_PPI: f64 = 1_000_000.0; -/// Two resolutions this close (pixels per inch) are the same. -const SAME_PPI: f64 = 1e-3; - -/// A usable resolution pair, or `None`. -fn ppi_pair(x: f64, y: f64) -> Option<(f32, f32)> { - let ok = |v: f64| v.is_finite() && v > 0.0 && v <= MAX_PPI; - (ok(x) && ok(y)).then_some((x as f32, y as f32)) -} - -fn same(a: (f32, f32), b: (f32, f32)) -> bool { - (f64::from(a.0) - f64::from(b.0)).abs() < SAME_PPI && (f64::from(a.1) - f64::from(b.1)).abs() < SAME_PPI -} - -/// `ppi` as a TIFF RATIONAL: a whole number over 1 when it is one, else over 1000. -fn rational(ppi: f32) -> (u32, u32) { - let p = f64::from(ppi); - if (p - p.round()).abs() < SAME_PPI { (p.round() as u32, 1) } else { ((p * 1000.0).round() as u32, 1000) } -} - -/// One of the three resolution entries of IFD0. -#[derive(Clone, Copy)] -struct Field { - /// Offset of the 12- (BigTIFF: 20-) byte entry. - entry: usize, - tag: u16, - ty: u16, - count: u64, - /// Where the value bytes are, when the type is one we read and they lie inside the block. - value: Option, -} - -impl Field { - fn short(&self, order: Order, b: &[u8]) -> Option { - (self.ty == TYPE_SHORT && self.count == 1).then_some(())?; - order.u16(b, self.value?) - } - - fn rational(&self, order: Order, b: &[u8]) -> Option { - (self.ty == TYPE_RATIONAL && self.count == 1).then_some(())?; - let at = self.value?; - let (n, d) = (order.u32(b, at)?, order.u32(b, at.checked_add(4)?)?); - (d != 0).then(|| f64::from(n) / f64::from(d)) - } -} - -/// IFD0's resolution entries. -struct Ifd0 { - order: Order, - big: bool, - first: usize, - count: usize, - entry_bytes: usize, - fields: Vec, -} - -impl Ifd0 { - fn parse(b: &[u8]) -> Option { - let (order, first, count, entry_bytes, big) = ifd0_entries(b)?; - let mut fields = Vec::new(); - for i in 0..count { - let e = first.checked_add(i.checked_mul(entry_bytes)?)?; - let tag = order.u16(b, e)?; - if !matches!(tag, TAG_X_RESOLUTION | TAG_Y_RESOLUTION | TAG_RESOLUTION_UNIT) { - continue; - } - let ty = order.u16(b, e.checked_add(2)?)?; - let count = if big { order.u64(b, e.checked_add(4)?)? } else { u64::from(order.u32(b, e.checked_add(4)?)?) }; - let value = value_at(order, b, e, big, ty, count); - fields.push(Field { entry: e, tag, ty, count, value }); - } - Some(Ifd0 { order, big, first, count, entry_bytes, fields }) - } - - fn first_of(&self, tag: u16) -> Option<&Field> { - self.fields.iter().find(|f| f.tag == tag) - } -} - -/// Where an entry's SHORT or RATIONAL value bytes are: inline in the entry when they fit, -/// else at the offset it holds. `None` for other types or when they leave the block. -fn value_at(order: Order, b: &[u8], e: usize, big: bool, ty: u16, count: u64) -> Option { - let size = match ty { - TYPE_SHORT => 2u64, - TYPE_RATIONAL => 8, - _ => return None, - } - .checked_mul(count)?; - let (field, inline) = if big { (e.checked_add(12)?, 8) } else { (e.checked_add(8)?, 4) }; - let at = if size <= inline { - field - } else if big { - usize::try_from(order.u64(b, field)?).ok()? - } else { - usize::try_from(order.u32(b, field)?).ok()? - }; - (at.checked_add(usize::try_from(size).ok()?)? <= b.len()).then_some(at) -} - -/// The resolution (pixels per inch, horizontal and vertical) recorded in IFD0 of an EXIF block -/// (with or without the JPEG `Exif\0\0` prefix) or a TIFF structure. -/// -/// Both XResolution and YResolution must be one RATIONAL each with a non-zero denominator. -/// ResolutionUnit 2 (inch, also when the tag is absent) is used as is, 3 (centimetre) is -/// converted; 1 (no absolute unit), any other value or a malformed unit entry gives `None`. -/// The first of duplicated entries wins. -pub fn exif_resolution(exif: &[u8]) -> Option<(f32, f32)> { - let b = tiff_body(exif); - let ifd = Ifd0::parse(b)?; - let unit = match ifd.first_of(TAG_RESOLUTION_UNIT) { - None => 2, - Some(f) => f.short(ifd.order, b)?, - }; - let scale = match unit { - 2 => 1.0, - 3 => CM_PER_INCH, - _ => return None, - }; - let x = ifd.first_of(TAG_X_RESOLUTION)?.rational(ifd.order, b)?; - let y = ifd.first_of(TAG_Y_RESOLUTION)?.rational(ifd.order, b)?; - ppi_pair(x * scale, y * scale) -} - -/// `exif` with its IFD0 resolution set to `ppi` (pixels per inch), borrowed when it has no -/// resolution entries, already records that resolution, or cannot be parsed. -/// -/// Existing well-formed XResolution / YResolution (one RATIONAL) entries are rewritten in place, -/// and every well-formed ResolutionUnit (one SHORT) becomes 2 (inch). Malformed entries are -/// removed from IFD0 instead, so no stale value survives; all other bytes are kept. -pub fn exif_with_resolution(exif: &[u8], ppi: (f32, f32)) -> Cow<'_, [u8]> { - exif_with_resolution_inner(exif, ppi, false) -} - -/// Like [`exif_with_resolution`], but adds X/Y resolution tags when a valid IFD0 has none. -/// Existing TIFF data stays in place, so its offsets remain valid. Used by CMYK JPEG, which -/// cannot carry a JFIF density marker. -pub(crate) fn exif_with_resolution_adding_missing(exif: &[u8], ppi: (f32, f32)) -> Cow<'_, [u8]> { - exif_with_resolution_inner(exif, ppi, true) -} - -/// Rewrite valid IFD0 resolution tags and optionally append tags that are missing or malformed. -fn exif_with_resolution_inner(exif: &[u8], ppi: (f32, f32), add_missing: bool) -> Cow<'_, [u8]> { - if ppi_pair(f64::from(ppi.0), f64::from(ppi.1)).is_none() { - return Cow::Borrowed(exif); - } - if exif.is_empty() { - if !add_missing { - return Cow::Borrowed(exif); - } - // An image with DPI but no source EXIF still needs a valid EXIF block when the output - // format cannot carry resolution in its own header (CMYK JPEG has no valid JFIF marker). - return Cow::Owned(exif_with_resolution_inner(b"II*\0\x08\0\0\0\0\0\0\0\0\0\0", ppi, true).into_owned()); - } - let body = tiff_body(exif); - let prefix = exif.len() - body.len(); - let Some(ifd) = Ifd0::parse(body) else { return Cow::Borrowed(exif) }; - if exif_resolution(body).is_some_and(|r| same(r, ppi)) { - return Cow::Borrowed(exif); - } - let missing_xy = ifd.first_of(TAG_X_RESOLUTION).and_then(|f| f.rational(ifd.order, body)).is_none() - || ifd.first_of(TAG_Y_RESOLUTION).and_then(|f| f.rational(ifd.order, body)).is_none(); - if add_missing && missing_xy { - let Some(body) = append_resolution_ifd(body, &ifd, ppi) else { return Cow::Borrowed(exif) }; - let mut full = exif.get(..prefix).unwrap_or_default().to_vec(); - full.extend_from_slice(&body); - return Cow::Owned(full); - } - if ifd.fields.is_empty() { - return Cow::Borrowed(exif); - } - let order = ifd.order; - let mut out = body.to_vec(); - let mut removed = Vec::new(); - for f in &ifd.fields { - let bytes = match f.tag { - TAG_RESOLUTION_UNIT if f.short(order, body).is_some() => put_u16(order, 2).to_vec(), - TAG_X_RESOLUTION | TAG_Y_RESOLUTION if f.rational(order, body).is_some() => { - let (n, d) = rational(if f.tag == TAG_X_RESOLUTION { ppi.0 } else { ppi.1 }); - [put_u32(order, n), put_u32(order, d)].concat() - } - _ => { - removed.push(f.entry); - continue; - } - }; - let slot = f.value.and_then(|at| out.get_mut(at..at.checked_add(bytes.len())?)); - match slot { - Some(slot) => slot.copy_from_slice(&bytes), - None => removed.push(f.entry), - } - } - if !removed.is_empty() { - remove_entries(&mut out, &ifd, &removed); - } - let mut full = exif.get(..prefix).unwrap_or_default().to_vec(); - full.extend_from_slice(&out); - if full == exif { Cow::Borrowed(exif) } else { Cow::Owned(full) } -} - -/// Append a replacement IFD0 with fresh resolution tags. Every other entry keeps its original -/// bytes and offsets, since the TIFF block's existing data remains where it was. -fn append_resolution_ifd(body: &[u8], ifd: &Ifd0, ppi: (f32, f32)) -> Option> { - let (count_bytes, next_bytes, alignment) = if ifd.big { (8usize, 8usize, 8usize) } else { (2usize, 4usize, 2usize) }; - let mut entries = Vec::with_capacity(ifd.count.saturating_add(3)); - for index in 0..ifd.count { - let entry = ifd.first.checked_add(index.checked_mul(ifd.entry_bytes)?)?; - let tag = ifd.order.u16(body, entry)?; - if matches!(tag, TAG_X_RESOLUTION | TAG_Y_RESOLUTION | TAG_RESOLUTION_UNIT) { - continue; - } - entries.push((tag, body.get(entry..entry.checked_add(ifd.entry_bytes)?)?.to_vec())); - } - let count = entries.len().checked_add(3)?; - if !ifd.big && count > usize::from(u16::MAX) { - return None; - } - - let new_ifd = align_up(body.len(), alignment)?; - let table_end = new_ifd.checked_add(count_bytes)?.checked_add(count.checked_mul(ifd.entry_bytes)?)?; - let next_at = ifd.first.checked_add(ifd.count.checked_mul(ifd.entry_bytes)?)?; - let next_ifd = body.get(next_at..next_at.checked_add(next_bytes)?)?; - let rational_at = align_up(table_end.checked_add(next_bytes)?, if ifd.big { 8 } else { 4 })?; - let y_rational_at = rational_at.checked_add(8)?; - if !ifd.big && (new_ifd > u32::MAX as usize || y_rational_at.checked_add(8)? > u32::MAX as usize) { - return None; - } - - entries.push((TAG_X_RESOLUTION, resolution_entry(ifd.order, ifd.big, TAG_X_RESOLUTION, u64::try_from(rational_at).ok()?)?)); - entries.push((TAG_Y_RESOLUTION, resolution_entry(ifd.order, ifd.big, TAG_Y_RESOLUTION, u64::try_from(y_rational_at).ok()?)?)); - entries.push((TAG_RESOLUTION_UNIT, resolution_entry(ifd.order, ifd.big, TAG_RESOLUTION_UNIT, 2)?)); - entries.sort_by_key(|(tag, _)| *tag); - - let mut out = body.to_vec(); - out.resize(new_ifd, 0); - if ifd.big { - out.extend_from_slice(&put_u64(ifd.order, count as u64)); - } else { - out.extend_from_slice(&put_u16(ifd.order, count as u16)); - } - for (_, entry) in entries { - out.extend_from_slice(&entry); - } - out.extend_from_slice(next_ifd); - out.resize(rational_at, 0); - let (xn, xd) = rational(ppi.0); - let (yn, yd) = rational(ppi.1); - out.extend_from_slice(&put_u32(ifd.order, xn)); - out.extend_from_slice(&put_u32(ifd.order, xd)); - out.extend_from_slice(&put_u32(ifd.order, yn)); - out.extend_from_slice(&put_u32(ifd.order, yd)); - - let offset_at: usize = if ifd.big { 8 } else { 4 }; - if ifd.big { - out.get_mut(offset_at..offset_at.checked_add(8)?)?.copy_from_slice(&put_u64(ifd.order, new_ifd as u64)); - } else { - out.get_mut(offset_at..offset_at.checked_add(4)?)?.copy_from_slice(&put_u32(ifd.order, u32::try_from(new_ifd).ok()?)); - } - Some(out) -} - -/// One IFD entry. Rational fields store an offset; the SHORT unit is inline. -fn resolution_entry(order: Order, big: bool, tag: u16, value: u64) -> Option> { - let entry_bytes = if big { 20 } else { 12 }; - let count_at = 4; - let value_at = if big { 12 } else { 8 }; - let mut entry = vec![0; entry_bytes]; - entry.get_mut(..2)?.copy_from_slice(&put_u16(order, tag)); - entry.get_mut(2..4)?.copy_from_slice(&put_u16(order, if tag == TAG_RESOLUTION_UNIT { TYPE_SHORT } else { TYPE_RATIONAL })); - if big { - entry.get_mut(count_at..count_at + 8)?.copy_from_slice(&put_u64(order, 1)); - if tag == TAG_RESOLUTION_UNIT { - entry.get_mut(value_at..value_at + 2)?.copy_from_slice(&put_u16(order, value as u16)); - } else { - entry.get_mut(value_at..value_at + 8)?.copy_from_slice(&put_u64(order, value)); - } - } else { - entry.get_mut(count_at..count_at + 4)?.copy_from_slice(&put_u32(order, 1)); - if tag == TAG_RESOLUTION_UNIT { - entry.get_mut(value_at..value_at + 2)?.copy_from_slice(&put_u16(order, value as u16)); - } else { - entry.get_mut(value_at..value_at + 4)?.copy_from_slice(&put_u32(order, u32::try_from(value).ok()?)); - } - } - Some(entry) -} - -/// Round `value` up to the next `alignment` boundary, returning `None` on overflow. -fn align_up(value: usize, alignment: usize) -> Option { - value.checked_add((alignment - value % alignment) % alignment) -} - -fn put_u16(order: Order, v: u16) -> [u8; 2] { - match order { - Order::Little => v.to_le_bytes(), - Order::Big => v.to_be_bytes(), - } -} - -fn put_u32(order: Order, v: u32) -> [u8; 4] { - match order { - Order::Little => v.to_le_bytes(), - Order::Big => v.to_be_bytes(), - } -} - -fn put_u64(order: Order, v: u64) -> [u8; 8] { - match order { - Order::Little => v.to_le_bytes(), - Order::Big => v.to_be_bytes(), - } -} - -/// Removes the IFD0 entries at offsets `removed` (see [`exif_with_resolution`]). -fn remove_entries(b: &mut [u8], ifd: &Ifd0, removed: &[usize]) { - let eb = ifd.entry_bytes; - let next_bytes = if ifd.big { 8 } else { 4 }; - let count_bytes = if ifd.big { 8 } else { 2 }; - let Some(table_end) = ifd.count.checked_mul(eb).and_then(|n| ifd.first.checked_add(n)) else { return }; - let Some(old) = b.get(ifd.first..table_end.saturating_add(next_bytes)).map(<[u8]>::to_vec) else { return }; - let mut table = Vec::with_capacity(old.len()); - let mut kept = 0usize; - for (i, entry) in old.chunks_exact(eb).take(ifd.count).enumerate() { - if !removed.contains(&(ifd.first + i * eb)) { - table.extend_from_slice(entry); - kept += 1; - } - } - table.extend_from_slice(old.get(ifd.count * eb..).unwrap_or_default()); - table.resize(old.len(), 0); - let count: Vec = if ifd.big { put_u64(ifd.order, kept as u64).to_vec() } else { put_u16(ifd.order, kept as u16).to_vec() }; - let Some(count_at) = ifd.first.checked_sub(count_bytes) else { return }; - if let Some(slot) = b.get_mut(count_at..ifd.first) { - slot.copy_from_slice(&count); - } - if let Some(slot) = b.get_mut(ifd.first..ifd.first + table.len()) { - slot.copy_from_slice(&table); - } -} - -/// The resolution in a Photoshop image resource block (a sequence of `8BIM` resources, as in a -/// JPEG's APP13 "Photoshop 3.0" segment or TIFF tag 34377): its ResolutionInfo (0x03ED), whose -/// Fixed 16.16 hRes / vRes are pixels per inch whatever display unit follows them. -pub fn photoshop_resolution(resources: &[u8]) -> Option<(f32, f32)> { - let be16 = |at: usize| resources.get(at..at.checked_add(2)?).map(|s| u16::from_be_bytes([s[0], s[1]])); - let be32 = |at: usize| resources.get(at..at.checked_add(4)?).map(|s| u32::from_be_bytes([s[0], s[1], s[2], s[3]])); - let mut i = 0usize; - while i < resources.len() { - let signature = resources.get(i..i.checked_add(4)?)?; - let id = be16(i.checked_add(4)?)?; - // Pascal name: length byte plus text, padded to an even size. - let name_len = usize::from(*resources.get(i.checked_add(6)?)?); - let name = (name_len + 1).next_multiple_of(2); - let size_at = i.checked_add(6)?.checked_add(name)?; - let size = usize::try_from(be32(size_at)?).ok()?; - let data = size_at.checked_add(4)?; - if signature == b"8BIM" && id == RESOLUTION_INFO { - let info = resources.get(data..data.checked_add(size)?)?; - if info.len() < 16 { - return None; - } - let fixed = |at: usize| be32(data + at).map(|v| f64::from(v) / 65536.0); - return ppi_pair(fixed(0)?, fixed(8)?); - } - i = data.checked_add(size)?.checked_add(size % 2)?; - } - None -} - -/// The text ranges of every value of XMP property `key` (`key="v"`, `key='v'` or -/// `v`), in order. -fn xmp_values(xmp: &str, key: &str) -> Vec<(usize, usize)> { - let mut out = Vec::new(); - let mut from = 0; - while let Some(k) = xmp.get(from..).and_then(|s| s.find(key)).map(|k| k + from) { - from = k + key.len(); - // An attribute or an opening tag of exactly this name (not ``, not `xkey`). - if !xmp.get(..k).and_then(|h| h.chars().next_back()).is_some_and(|c| c == '<' || c.is_ascii_whitespace()) { - continue; - } - let rest = xmp.get(from..).unwrap_or_default(); - let after_key = rest.trim_start(); - let (value, close) = if let Some(r) = after_key.strip_prefix('=') { - let r = r.trim_start(); - match r.chars().next() { - Some(q @ ('"' | '\'')) => (r.get(1..).unwrap_or_default(), q), - _ => continue, - } - } else if let Some(r) = rest.strip_prefix('>') { - (r, '<') - } else { - continue; - }; - let start = xmp.len() - value.len(); - // Advance past this value before searching for the next key. Without this, `from` - // only moved past the key text, so property-like text repeated inside an unrelated - // attribute re-ran this full-suffix `find(close)` for every occurrence, which is - // quadratic in the packet size. A missing close means the value is unterminated, so - // no later value can parse either; stop scanning in that case. - let Some(len) = value.find(close) else { break }; - out.push((start, start + len)); - from = start + len + close.len_utf8(); - } - out -} - -/// An XMP rational (`300/1`) or plain number. -fn xmp_number(s: &str) -> Option { - let s = s.trim(); - match s.split_once('/') { - Some((n, d)) => { - let (n, d) = (n.trim().parse::().ok()?, d.trim().parse::().ok()?); - (d != 0.0).then(|| n / d) - } - None => s.parse().ok(), - } -} - -const XMP_X: &str = "tiff:XResolution"; -const XMP_Y: &str = "tiff:YResolution"; -const XMP_UNIT: &str = "tiff:ResolutionUnit"; - -/// The resolution (pixels per inch) in an XMP packet's `tiff:XResolution` / `tiff:YResolution` -/// (with `tiff:ResolutionUnit`, 2 when absent, 3 converted from centimetres), or `None`. -pub fn xmp_resolution(xmp: &str) -> Option<(f32, f32)> { - let first = |key: &str| xmp_values(xmp, key).first().and_then(|&(a, b)| xmp.get(a..b)); - let unit = match first(XMP_UNIT) { - None => 2.0, - Some(u) => xmp_number(u)?, - }; - let scale = if unit == 2.0 { - 1.0 - } else if unit == 3.0 { - CM_PER_INCH - } else { - return None; - }; - ppi_pair(xmp_number(first(XMP_X)?)? * scale, xmp_number(first(XMP_Y)?)? * scale) -} - -/// `xmp` with its `tiff:` resolution properties set to `ppi` (and the unit to 2, inch), -/// borrowed when it has none or already says so. -pub fn xmp_with_resolution(xmp: &str, ppi: (f32, f32)) -> Cow<'_, str> { - if ppi_pair(f64::from(ppi.0), f64::from(ppi.1)).is_none() || xmp_resolution(xmp).is_some_and(|r| same(r, ppi)) { - return Cow::Borrowed(xmp); - } - let text = |v: f32| { - let (n, d) = rational(v); - format!("{n}/{d}") - }; - let mut edits: Vec<(usize, usize, String)> = Vec::new(); - for (key, value) in [(XMP_X, text(ppi.0)), (XMP_Y, text(ppi.1)), (XMP_UNIT, "2".to_owned())] { - edits.extend(xmp_values(xmp, key).into_iter().map(|(a, b)| (a, b, value.clone()))); - } - if edits.is_empty() { - return Cow::Borrowed(xmp); - } - edits.sort_by_key(|e| e.0); - let mut out = String::with_capacity(xmp.len() + 16); - let mut at = 0; - for (a, b, v) in edits { - // Overlapping ranges can only come from malformed markup; keep the first edit. - if a < at { - continue; - } - out.push_str(xmp.get(at..a).unwrap_or_default()); - out.push_str(&v); - at = b; - } - out.push_str(xmp.get(at..).unwrap_or_default()); - if out == xmp { Cow::Borrowed(xmp) } else { Cow::Owned(out) } -} - -/// `exif` without its embedded thumbnail, or `None` when it has none (or doesn't parse). -/// -/// EXIF keeps the thumbnail in IFD1, the IFD IFD0's next-IFD pointer names. Finder, Android's -/// gallery and upload pickers show that thumbnail instead of decoding the image, so a copied one -/// keeps showing the photo as it was before it was edited (#2147, #2373). The pointer is cleared; -/// nothing else moves, so every other offset in the block stays valid. -fn exif_without_thumbnail(exif: &[u8]) -> Option> { - let body = tiff_body(exif); - let prefix = exif.len() - body.len(); - let (_, first, count, entry_bytes, big_tiff) = ifd0_entries(body)?; - let next = prefix.checked_add(first)?.checked_add(count.checked_mul(entry_bytes)?)?; - let width = if big_tiff { 8 } else { 4 }; - // In bounds: `ifd0_entries` checked that the whole pointer is present. - if exif.get(next..next.checked_add(width)?)?.iter().all(|b| *b == 0) { - return None; - } - let mut out = exif.to_vec(); - out.get_mut(next..next + width)?.fill(0); - Some(out) -} - -/// The EXIF to write next to upright pixels at resolution `ppi`: Orientation 1 (see -/// [`upright_exif`]), no thumbnail of the source image (see [`exif_without_thumbnail`]) and, -/// when `ppi` is known, the resolution (see [`exif_with_resolution`]). -pub fn export_exif(exif: &[u8], ppi: Option<(f32, f32)>) -> Cow<'_, [u8]> { - export_exif_inner(exif, ppi, false) -} - -/// Export EXIF for a format without its own resolution field, adding missing X/Y resolution -/// entries to valid metadata when necessary. -pub(crate) fn export_exif_adding_missing_resolution(exif: &[u8], ppi: Option<(f32, f32)>) -> Cow<'_, [u8]> { - export_exif_inner(exif, ppi, true) -} - -/// Normalize EXIF orientation and thumbnails, optionally adding missing output resolution tags. -fn export_exif_inner(exif: &[u8], ppi: Option<(f32, f32)>, add_missing: bool) -> Cow<'_, [u8]> { - let up = upright_exif(exif); - let up = match exif_without_thumbnail(&up) { - Some(v) => Cow::Owned(v), - None => up, - }; - let Some(ppi) = ppi else { return up }; - let result = if add_missing { exif_with_resolution_adding_missing(&up, ppi) } else { exif_with_resolution(&up, ppi) }; - if let Cow::Owned(v) = result { - return Cow::Owned(v); - } - up -} - -/// The XMP to write next to upright pixels at resolution `ppi` (see [`upright_xmp`] and -/// [`xmp_with_resolution`]). -pub fn export_xmp(xmp: &str, ppi: Option<(f32, f32)>) -> Cow<'_, str> { - let up = upright_xmp(xmp); - let Some(ppi) = ppi else { return up }; - if let Cow::Owned(v) = xmp_with_resolution(&up, ppi) { - return Cow::Owned(v); - } - up -} - -/// The resolution a decoded file's metadata blocks record, in precedence order: XMP, then -/// EXIF (Photoshop's ResolutionInfo is format-specific and read by the decoder). +/// Resolution used by raster decode, preferring XMP over EXIF as before. pub(crate) fn from_metadata(exif: Option<&[u8]>, xmp: Option<&str>) -> Option<(f32, f32)> { - xmp.and_then(xmp_resolution).or_else(|| exif.and_then(exif_resolution)) + craft_metadata::edit::resolution(exif, xmp) }