From 074033b9aff3090e8e4aca4df7108b189bae3168 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 23 Nov 2026 05:56:14 +0700 Subject: [PATCH] images: read JPEG EXIF orientation through the shared strict reader (craft-libs#778) Replace the local APP1 scan restored in #3105 with craft_image_codecs::jpeg_probe::strict_exif_orientation, which craft-libs#3067 ported from it. Placement policy stays here. --- crates/images/src/resolution.rs | 43 ++++----------------------------- 1 file changed, 5 insertions(+), 38 deletions(-) diff --git a/crates/images/src/resolution.rs b/crates/images/src/resolution.rs index d20d4a940..42b6a448d 100644 --- a/crates/images/src/resolution.rs +++ b/crates/images/src/resolution.rs @@ -3,9 +3,10 @@ //! Delegates container resolution probes to `craft-image-codecs` (`file_resolution`, which reads //! JPEG Photoshop ResolutionInfo -> XMP -> EXIF -> JFIF, PNG eXIf, WebP EXIF, and classic TIFF; //! plus `exif_resolution`, `png_probe::pixels_per_meter`, and `bmp_probe::pixels_per_meter`) and -//! PSD `ResolutionInfo` to [`crate::psd`]. JPEG EXIF orientation keeps the strict local APP1 scan -//! (`"Exif\0\0"` with complete segment bounds). GIF and files without (usable) metadata report -//! nothing and keep the pixel-is-a-point size. +//! PSD `ResolutionInfo` to [`crate::psd`]. JPEG EXIF orientation uses the shared strict APP1 +//! reader (`jpeg_probe::strict_exif_orientation`: `"Exif\0\0"` with complete segment bounds, +//! before the first scan). GIF and files without (usable) metadata report nothing and keep the +//! pixel-is-a-point size. /// Dots per inch in x and y, when the file carries usable resolution metadata. pub fn raster_resolution(bytes: &[u8]) -> Option<(f64, f64)> { @@ -29,7 +30,7 @@ pub fn raster_resolution(bytes: &[u8]) -> Option<(f64, f64)> { /// The EXIF/TIFF orientation (1–8) of `bytes`, or 1 when absent or unreadable. fn orientation(bytes: &[u8]) -> u16 { if bytes.starts_with(b"\xFF\xD8\xFF") { - return jpeg_orientation(bytes); + return craft_image_codecs::jpeg_probe::strict_exif_orientation(bytes); } if bytes.starts_with(b"\x89PNG\r\n\x1a\n") { return craft_image_codecs::png_probe::header(bytes).map_or(1, |(_, _, o)| o); @@ -43,40 +44,6 @@ fn orientation(bytes: &[u8]) -> u16 { 1 } -/// EXIF orientation from a JPEG's first APP1 `"Exif\0\0"` segment before the first scan. -fn jpeg_orientation(bytes: &[u8]) -> u16 { - let mut i = 2usize; - while i + 4 <= bytes.len() { - if bytes[i] != 0xFF { - break; - } - let marker = bytes[i + 1]; - if marker == 0xFF { - i += 1; - continue; - } - if marker == 0xD8 || (0xD0..=0xD7).contains(&marker) || marker == 0x01 { - i += 2; - continue; - } - if marker == 0xDA || marker == 0xD9 { - break; - } - let len = u16::from_be_bytes([bytes[i + 2], bytes[i + 3]]) as usize; - if len < 2 || i + 2 + len > bytes.len() { - break; - } - let seg = &bytes[i + 4..i + 2 + len]; - if marker == 0xE1 - && let Some(exif) = seg.strip_prefix(b"Exif\0\0") - { - return craft_image_codecs::exif_orientation(exif); - } - i += 2 + len; - } - 1 -} - /// PNG `pHYs` pixels-per-metre via the shared probe: meters -> dpi in `f64` (preserving exact /// placement DPI without `dpi_of_ppm` snapping). fn png_resolution(bytes: &[u8]) -> Option<(f64, f64)> {