mirror of
https://github.com/storytold/designcraft.git
synced 2026-11-21 20:39:22 +00:00
images: restore strict JPEG APP1 EXIF scan (craft-libs#785) (#3105)
This commit is contained in:
Generated
-1
@@ -2003,7 +2003,6 @@ dependencies = [
|
||||
"craft-file-formats",
|
||||
"craft-image-codecs",
|
||||
"craft-jpeg",
|
||||
"craft-metadata",
|
||||
"craft-ps",
|
||||
"craft-psd",
|
||||
"craft-svg",
|
||||
|
||||
@@ -32,7 +32,6 @@ craft-wordmodel = { git = "https://github.com/storytold/craft-libs", rev = "c60e
|
||||
craft-csv = { git = "https://github.com/storytold/craft-libs", rev = "c60e7a20063313b80a994075adb4f797b24ab4f1", version = "=0.1.0" }
|
||||
craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "c60e7a20063313b80a994075adb4f797b24ab4f1", version = "=0.1.0" }
|
||||
craft-jpeg = { git = "https://github.com/storytold/craft-libs", rev = "c60e7a20063313b80a994075adb4f797b24ab4f1", version = "=0.1.0" }
|
||||
craft-metadata = { git = "https://github.com/storytold/craft-libs", rev = "c60e7a20063313b80a994075adb4f797b24ab4f1", version = "=0.1.0" }
|
||||
craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "c60e7a20063313b80a994075adb4f797b24ab4f1", version = "=0.1.0" }
|
||||
craft-file-formats = { git = "https://github.com/storytold/craft-libs", rev = "c60e7a20063313b80a994075adb4f797b24ab4f1", version = "=0.1.0" }
|
||||
craft-opc = { git = "https://github.com/storytold/craft-libs", rev = "c60e7a20063313b80a994075adb4f797b24ab4f1", version = "=0.1.0" }
|
||||
|
||||
@@ -23,7 +23,6 @@ craft-image-codecs = { workspace = true }
|
||||
craft-psd = { workspace = true }
|
||||
craft-file-formats = { workspace = true }
|
||||
craft-jpeg = { workspace = true }
|
||||
craft-metadata = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
image = { workspace = true, features = ["png", "jpeg", "webp", "gif", "tiff", "bmp"] }
|
||||
|
||||
@@ -2,9 +2,10 @@
|
||||
//! it instead of assuming 72 ppi (#1991: a 600 px photo saved at 600 dpi places one inch wide).
|
||||
//! 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`),
|
||||
//! JPEG EXIF orientation to `craft-metadata`, and PSD `ResolutionInfo` to [`crate::psd`]. GIF and
|
||||
//! files without (usable) metadata report nothing and keep the pixel-is-a-point size.
|
||||
//! 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.
|
||||
|
||||
/// Dots per inch in x and y, when the file carries usable resolution metadata.
|
||||
pub fn raster_resolution(bytes: &[u8]) -> Option<(f64, f64)> {
|
||||
@@ -44,7 +45,36 @@ fn orientation(bytes: &[u8]) -> u16 {
|
||||
|
||||
/// EXIF orientation from a JPEG's first APP1 `"Exif\0\0"` segment before the first scan.
|
||||
fn jpeg_orientation(bytes: &[u8]) -> u16 {
|
||||
craft_metadata::jpeg_segments(bytes).exif.as_deref().map_or(1, craft_image_codecs::exif_orientation)
|
||||
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
|
||||
|
||||
@@ -1401,6 +1401,28 @@ fn jpeg_orientation_keeps_first_exif_app1_segment() {
|
||||
assert_eq!(raster_resolution(&jpeg_with(&upright_first)), Some((300.0, 150.0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn jpeg_orientation_requires_exact_exif_prefix_and_complete_app1() {
|
||||
let tiff_rot6 = tiff_bytes_with_orientation((300, 1), (150, 1), 2, 6);
|
||||
|
||||
// Looser prefix ("Exif\0\x01" instead of "Exif\0\0") must not be treated as EXIF.
|
||||
let mut looser_payload = b"Exif\0\x01".to_vec();
|
||||
looser_payload.extend_from_slice(&tiff_rot6);
|
||||
let mut looser_seg = vec![0xFF, 0xE1];
|
||||
looser_seg.extend_from_slice(&((looser_payload.len() + 2) as u16).to_be_bytes());
|
||||
looser_seg.extend_from_slice(&looser_payload);
|
||||
let mut with_looser = jfif_app(1, 300, 150);
|
||||
with_looser.extend_from_slice(&looser_seg);
|
||||
assert_eq!(raster_resolution(&jpeg_with(&with_looser)), Some((300.0, 150.0)));
|
||||
|
||||
// Truncated APP1 segment (declared length past end of file) must be ignored.
|
||||
let mut truncated = vec![0xFF, 0xD8];
|
||||
truncated.extend_from_slice(&jfif_app(1, 300, 150));
|
||||
truncated.extend_from_slice(&exif_app(&tiff_rot6));
|
||||
truncated.pop();
|
||||
assert_eq!(raster_resolution(&truncated), Some((300.0, 150.0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn png_phys_exif_fallback_orientation_swap_and_shared_chunk_limit() {
|
||||
fn png_chunk(kind: &[u8; 4], data: &[u8]) -> Vec<u8> {
|
||||
|
||||
Reference in New Issue
Block a user