From 1fb21f048785bba73b5ba604ec61cf5fb69367b0 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:25:28 +0000 Subject: [PATCH] Share legacy TIFF RAW classification policy --- crates/image-codecs/src/codecs/tiff.rs | 93 +++++++++++++++++++- crates/image-codecs/src/lib.rs | 6 ++ crates/image-codecs/tests/legacy_tiff_raw.rs | 21 +++++ 3 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 crates/image-codecs/tests/legacy_tiff_raw.rs diff --git a/crates/image-codecs/src/codecs/tiff.rs b/crates/image-codecs/src/codecs/tiff.rs index c74ab4436..376d3dcf6 100644 --- a/crates/image-codecs/src/codecs/tiff.rs +++ b/crates/image-codecs/src/codecs/tiff.rs @@ -71,6 +71,18 @@ const TAG_DNG_VERSION: u16 = 50706; /// first IFD is often a reduced preview, so this runs before decoding. /// Reads only bounds-checked header bytes; anything malformed is "not raw". fn camera_raw(b: &[u8]) -> bool { + camera_raw_mode(b, false) +} + +/// The older LightCraft RAW/TIFF classification policy, sharing this bounded directory walk. +pub(crate) fn legacy_camera_raw(b: &[u8]) -> bool { + camera_raw_mode(b, true) +} + +fn camera_raw_mode(b: &[u8], legacy: bool) -> bool { + if legacy && b.get(8..10) == Some(b"CR") { + return true; + } let le = match b.get(0..4) { Some(b"II*\0") => true, Some(b"MM\0*") => false, @@ -92,8 +104,48 @@ fn camera_raw(b: &[u8]) -> bool { let value = |e: usize| match u16_at(e.saturating_add(2))? { 3 => u16_at(e.saturating_add(8)).map(u32::from), 4 | 13 => u32_at(e.saturating_add(8)), + 1 | 7 if legacy => b.get(e.saturating_add(8)).copied().map(u32::from), _ => None, }; + // The compatibility policy recognizes Sony lossless YCbCr only when IFD0 names Sony. + let is_sony = if legacy { + u32_at(4) + .and_then(|v| usize::try_from(v).ok()) + .and_then(|ifd| { + let n = usize::from(u16_at(ifd)?); + if n > 4096 { + return None; + } + for i in 0..n { + let e = ifd.checked_add(2)?.checked_add(i.checked_mul(12)?)?; + let tag = u16_at(e)?; + let ty = u16_at(e + 2)?; + let count = u32_at(e + 4)? as usize; + if tag == 0x010f { + let matches = (|| { + let size = match ty { + 1 | 2 | 6 | 7 => 1usize, + 3 | 8 => 2, + 4 | 9 | 11 | 13 => 4, + 5 | 10 | 12 => 8, + _ => return None, + }; + let len = size.checked_mul(count)?; + let start = if len <= 4 { e + 8 } else { u32_at(e + 8)? as usize }; + Some(b.get(start..start.checked_add(len)?)?.get(..4)?.eq_ignore_ascii_case(b"SONY")) + })() + .unwrap_or(false); + if matches { + return Some(true); + } + } + } + Some(false) + }) + .unwrap_or(false) + } else { + false + }; let mut queue: Vec = u32_at(4).into_iter().collect(); let mut visited = 0; while let Some(ifd) = queue.pop() { @@ -104,6 +156,44 @@ fn camera_raw(b: &[u8]) -> bool { } let ifd = ifd as usize; let Some(n) = u16_at(ifd) else { continue }; + if legacy + && (n > 4096 + || (n > 0 + && ifd + .checked_add(2) + .and_then(|p| usize::from(n - 1).checked_mul(12).and_then(|v| p.checked_add(v))) + .and_then(|p| p.checked_add(8)) + .is_none_or(|end| end > b.len()))) + { + continue; + } + if is_sony { + let mut cfa = false; + let (mut compression, mut photo, mut samples) = (None, None, None); + let (mut saw_compression, mut saw_photo, mut saw_samples) = (false, false, false); + for i in 0..usize::from(n) { + let e = ifd.saturating_add(2 + 12 * i); + match u16_at(e) { + Some(0x828e) => cfa = true, + Some(0x0103) if !saw_compression => { + saw_compression = true; + compression = value(e); + } + Some(0x0106) if !saw_photo => { + saw_photo = true; + photo = value(e); + } + Some(0x0115) if !saw_samples => { + saw_samples = true; + samples = value(e); + } + _ => {} + } + } + if cfa && compression == Some(7) && photo == Some(6) && samples == Some(3) { + return true; + } + } for i in 0..usize::from(n) { let e = ifd.saturating_add(2 + 12 * i); let (Some(tag), Some(count)) = (u16_at(e), u32_at(e.saturating_add(4))) else { @@ -111,6 +201,7 @@ fn camera_raw(b: &[u8]) -> bool { }; match tag { TAG_DNG_VERSION => return true, + 0x0103 if legacy && matches!(value(e), Some(32767 | 32769 | 32770 | 34316 | 34713 | 65000 | 65535)) => return true, TAG_PHOTOMETRIC if matches!(value(e), Some(PHOTOMETRIC_CFA | PHOTOMETRIC_LINEAR_RAW)) => { return true; } @@ -125,7 +216,7 @@ fn camera_raw(b: &[u8]) -> bool { _ => {} } } - if let Some(next) = u32_at(ifd.saturating_add(2 + 12 * usize::from(n))).filter(|&o| o != 0) { + if !legacy && let Some(next) = u32_at(ifd.saturating_add(2 + 12 * usize::from(n))).filter(|&o| o != 0) { queue.push(next); } } diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index d8659bf54..96a6d78a2 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -216,6 +216,12 @@ pub fn probe_size(bytes: &[u8]) -> Result<(u32, u32), CodecError> { Ok(if (5..=8).contains(&o) { (h, w) } else { (w, h) }) } +/// Classify classic TIFF camera RAWs using LightCraft's historical bounded heuristic. +/// This compatibility policy differs from the decoder's own RAW rejection policy. +pub fn legacy_tiff_raw_hint(bytes: &[u8]) -> bool { + tiff::legacy_camera_raw(bytes) +} + /// A JPEG's frame as its header declares it, read without decoding pixels: the stored size, the /// layout [`decode`] returns, whether [`decode`] hands the file to its whole-coefficient /// decoder (arithmetic-coded, lossless or 12-bit frames), whose working set is several times the diff --git a/crates/image-codecs/tests/legacy_tiff_raw.rs b/crates/image-codecs/tests/legacy_tiff_raw.rs new file mode 100644 index 000000000..87d99bad7 --- /dev/null +++ b/crates/image-codecs/tests/legacy_tiff_raw.rs @@ -0,0 +1,21 @@ +use craft_image_codecs::legacy_tiff_raw_hint; + +fn tiff(tag: u16, ty: u16, value: u32) -> Vec { + let mut b = b"II\x2a\0\x08\0\0\0\x01\0".to_vec(); + b.extend_from_slice(&tag.to_le_bytes()); + b.extend_from_slice(&ty.to_le_bytes()); + b.extend_from_slice(&1u32.to_le_bytes()); + b.extend_from_slice(&value.to_le_bytes()); + b.extend_from_slice(&0u32.to_le_bytes()); + b +} + +#[test] +fn recognizes_legacy_raw_compression_and_broad_cr_marker() { + assert!(legacy_tiff_raw_hint(&tiff(0x0103, 3, 34713))); + assert!(!legacy_tiff_raw_hint(&tiff(0x0103, 3, 1))); + assert!(legacy_tiff_raw_hint(b"II*\0\x08\0\0\0CR")); + let mut cut = tiff(0xc612, 1, 1); + cut.truncate(17); + assert!(!legacy_tiff_raw_hint(&cut)); +}