Delegate TIFF RAW classification to shared codecs (#4311)

This commit is contained in:
Dylan M. Taylor authored and GitHub committed 2026-11-16 21:58:44 -05:00
1 parent 9de93337c5
commit 7bb9165c6b
2 files changed
+26 -1

No files matched your search

+1
View File
@@ -57,6 +57,7 @@ impl<'a> Tiff<'a> {
Some((v, next))
}
/// First value of a SHORT/LONG entry.
#[cfg(test)]
pub fn uint(&self, e: &Entry) -> Option<u32> {
match e.typ {
3 => self.u16_at(e.value_pos).map(u32::from),
+25 -1
View File
@@ -1,5 +1,6 @@
//! Format detection from leading bytes.
#[cfg(test)]
use crate::exif::Tiff;
use serde::{Deserialize, Serialize};
@@ -125,7 +126,7 @@ pub fn sniff(b: &[u8]) -> Option<Format> {
return Some(Format::RawTiffLike);
}
if b.starts_with(b"II*\0") || b.starts_with(b"MM\0*") {
return Some(if tiff_is_raw(b) { Format::RawTiffLike } else { Format::Tiff });
return Some(if craft_image_codecs::legacy_tiff_raw_hint(b) { Format::RawTiffLike } else { Format::Tiff });
}
if b.starts_with(b"II+\0") || b.starts_with(b"MM\0+") {
return Some(Format::Tiff);
@@ -167,6 +168,7 @@ fn sniff_bmff(b: &[u8]) -> Option<Format> {
/// Heuristic camera-raw detection for classic TIFF: DNGVersion, the CR2 signature, CFA/LinearRaw
/// photometric or raw-only compressions in IFD0 or its SubIFDs, or a camera Make with 10–16-bit data
/// and no ordinary photometric.
#[cfg(test)]
fn tiff_is_raw(b: &[u8]) -> bool {
if b.len() >= 10 && &b[8..10] == b"CR" {
return true;
@@ -221,6 +223,28 @@ fn tiff_is_raw(b: &[u8]) -> bool {
mod tests {
use super::*;
#[test]
fn raw_tiff_classifier_matches_legacy_on_mutations() {
let fixtures: &[&[u8]] = &[
b"II*\0\x08\0\0\0\x01\0\x12\xc6\x01\0\x04\0\0\0\x01\x04\0\0\0\0\0\0",
b"II*\0\x08\0\0\0\x01\0\x03\x01\x03\0\x01\0\0\0\x09\x80\0\0\0\0\0\0",
b"II*\0\x10\0\0\0CR\x02\0",
];
for fixture in fixtures {
for end in 0..=fixture.len() {
let bytes = &fixture[..end];
assert_eq!(craft_image_codecs::legacy_tiff_raw_hint(bytes), tiff_is_raw(bytes), "truncation {end}");
for i in 0..bytes.len() {
for value in [0, 255] {
let mut changed = bytes.to_vec();
changed[i] = value;
assert_eq!(craft_image_codecs::legacy_tiff_raw_hint(&changed), tiff_is_raw(&changed), "byte {i} value {value} length {end}");
}
}
}
}
}
#[test]
fn magic_numbers() {
assert_eq!(sniff(b"\xFF\xD8\xFF\xE0\0\x10JFIF"), Some(Format::Jpeg));