Merge pull request #1668 from storytold/codex/shared-raw-tiff-hint

Share legacy TIFF camera RAW classification
This commit is contained in:
FredApps authored and GitHub committed 2026-11-16 00:54:25 +01:00
commit aa9ecc2be4
3 files changed
+163 -1

No files matched your search

+92 -1
View File
@@ -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> = 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);
}
}
+6
View File
@@ -231,6 +231,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
@@ -0,0 +1,65 @@
use craft_image_codecs::legacy_tiff_raw_hint;
fn tiff(tag: u16, ty: u16, value: u32) -> Vec<u8> {
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));
}
/// A little-endian TIFF with one IFD of (tag, type, count, value) entries and `tail` after it.
fn ifd(entries: &[(u16, u16, u32, [u8; 4])], tail: &[u8]) -> Vec<u8> {
let mut b = b"II\x2a\0\x08\0\0\0".to_vec();
b.extend_from_slice(&(entries.len() as u16).to_le_bytes());
for (tag, ty, count, value) in entries {
b.extend_from_slice(&tag.to_le_bytes());
b.extend_from_slice(&ty.to_le_bytes());
b.extend_from_slice(&count.to_le_bytes());
b.extend_from_slice(value);
}
b.extend_from_slice(&0u32.to_le_bytes());
b.extend_from_slice(tail);
b
}
#[test]
fn sony_lossless_ycbcr_needs_a_sony_make_and_every_layout_tag() {
let short = |v: u16| [v.to_le_bytes()[0], v.to_le_bytes()[1], 0, 0];
let layout = [(0x828e, 1, 4, [0, 1, 1, 2]), (0x0103, 3, 1, short(7)), (0x0106, 3, 1, short(6)), (0x0115, 3, 1, short(3))];
// Make stored after the IFD (ASCII, 5 bytes): 8 + 2 + 5 * 12 + 4 = 74.
let with_make = |make: (u16, u16, u32, [u8; 4]), tail: &[u8]| ifd(&[&[make], &layout[..]].concat(), tail);
assert!(legacy_tiff_raw_hint(&with_make((0x010f, 2, 5, 74u32.to_le_bytes()), b"SONY\0")));
assert!(legacy_tiff_raw_hint(&with_make((0x010f, 7, 4, *b"sony"), b"")));
assert!(!legacy_tiff_raw_hint(&with_make((0x010f, 2, 6, 74u32.to_le_bytes()), b"Nikon\0")));
assert!(!legacy_tiff_raw_hint(&with_make((0x010f, 99, 4, *b"SONY"), b"")));
assert!(!legacy_tiff_raw_hint(&with_make((0x010f, 2, 5, 9999u32.to_le_bytes()), b"")));
// A Sony file without the full lossless layout is not a RAW by this rule.
let partial = ifd(&[(0x010f, 7, 4, *b"SONY"), layout[0], layout[1], layout[2]], b"");
assert!(!legacy_tiff_raw_hint(&partial));
// Repeated tags: the first value wins.
let repeated = ifd(&[&[(0x010f, 7, 4, *b"SONY")], &layout[..], &[(0x0103, 3, 1, short(1))]].concat(), b"");
assert!(legacy_tiff_raw_hint(&repeated));
}
#[test]
fn oversized_or_cut_directories_are_skipped() {
let mut huge = b"II\x2a\0\x08\0\0\0".to_vec();
huge.extend_from_slice(&5000u16.to_le_bytes());
assert!(!legacy_tiff_raw_hint(&huge));
let mut cut = ifd(&[(0x010f, 7, 4, *b"SONY"), (0x0103, 3, 1, [7, 0, 0, 0])], b"");
cut.truncate(20);
assert!(!legacy_tiff_raw_hint(&cut));
}