Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

LR-IMP-FORMATS: merge main; reconcile camera-TIFF detection with the Samsung/Pentax rules (#281)

Brandon Thomasfb6dda44ae70
diff --cc crates/engine/src/files.rs
index c81c7c97b,e1c54b342..eba894c7c
--- a/crates/engine/src/files.rs
+++ b/crates/engine/src/files.rs
@@@ -1090,26 -1464,6 +1464,26 @@@ mod tests
let plain = probe_bytes("small.tif", &tiff_shell(24, 16, false)).unwrap();
assert_eq!((plain.kind, plain.preview_only, plain.width, plain.height), (MediaKind::Image, None, 24, 16));
assert!(lightcraft_raw::probe(&tiff_shell(24, 16, false)).is_none());
+
- // TIFFs preserving camera Make tags (SONY, NIKON, etc.) are ordinary images, not raws
++ // TIFFs preserving camera Make tags (SONY, NIKON, etc.) are ordinary images, not raws (#281)
+ for make in ["SONY", "NIKON CORPORATION", "PENTAX", "RICOH", "SAMSUNG"] {
+ use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value, tags as t};
+ let mut ifd0 = IfdBuilder::new();
+ ifd0.set(t::IMAGE_WIDTH, Value::Long(vec![24]));
+ ifd0.set(t::IMAGE_LENGTH, Value::Long(vec![16]));
+ ifd0.set(t::BITS_PER_SAMPLE, Value::Short(vec![8, 8, 8]));
+ ifd0.set(t::SAMPLES_PER_PIXEL, Value::Short(vec![3]));
+ ifd0.set(t::PHOTOMETRIC, Value::Short(vec![2]));
+ ifd0.set(t::COMPRESSION, Value::Short(vec![1]));
+ ifd0.set(t::MAKE, Value::Ascii(make.into()));
+ let px: Vec<u8> = (0..24 * 16).flat_map(|i| [(i % 251) as u8, 128, 200]).collect();
+ ifd0.set_image(ImageData::Strips { rows_per_strip: 16, strips: vec![px] });
+ let bytes = TiffWriter::default().write(&[ifd0]).unwrap();
+ let p = probe_bytes("photo.tif", &bytes).unwrap();
+ assert_eq!((p.kind, p.preview_only, p.width, p.height), (MediaKind::Image, None, 24, 16), "make: {make}");
+ let (img, src) = load_bytes(&bytes, 24).unwrap();
+ assert_eq!((img.width, img.height, src.raw), (24, 16, false), "make: {make}");
+ }
}
/// Recognised raw containers we don't decode (Minolta MRW here: the preview's first byte is
diff --cc crates/raw/src/lib.rs
index 11ec839fd,ed17107e9..e21ee3473
--- a/crates/raw/src/lib.rs
+++ b/crates/raw/src/lib.rs
@@@ -153,30 -159,42 +159,48 @@@ pub fn probe(bytes: &[u8]) -> Option<Ra
}
let make = t.find(lightcraft_tiff::tags::MAKE).and_then(|e| e.value.as_str()).unwrap_or_default().to_ascii_uppercase();
let has_cfa = has_raw_ifd(&t);
++ // Pentax's reader takes the layout from the Exif `CFAPattern` when no IFD has a CFA photometric (uncompressed
++ // and PackBits PEFs), so for the Pentax family that tag counts as raw evidence too
++ let pentax_raw = has_cfa || t.exif().is_some_and(|e| e.contains(vendor::EXIF_CFA_PATTERN));
++ // Samsung's raw IFD uses private compressions 32769..=32773 (`vendor::srw`), 32773 being PackBits elsewhere
++ let samsung_raw = has_cfa || t.all_ifds().iter().any(|i| i.u16(lightcraft_tiff::tags::COMPRESSION).is_some_and(|c| (32769..=32773).contains(&c)));
if make.starts_with("CANON") && t.ifds.len() >= 4 && t.ifds[3].u16(lightcraft_tiff::tags::COMPRESSION) == Some(6) {
return Some(RawFormat::Cr2);
}
- if make.starts_with("NIKON") {
- return Some(if has_cfa || t.all_ifds().len() > 1 { RawFormat::Nef } else { RawFormat::Nrw });
+ if make.starts_with("NIKON") && has_cfa {
+ return Some(if t.all_ifds().len() > 1 { RawFormat::Nef } else { RawFormat::Nrw });
}
- if make.starts_with("SONY") {
+ if make.starts_with("SONY") && has_cfa {
return Some(RawFormat::Arw);
}
- if (make.starts_with("PENTAX") || make.starts_with("RICOH")) && has_cfa {
- if make.starts_with("PENTAX") || make.starts_with("RICOH") {
++ if (make.starts_with("PENTAX") || make.starts_with("RICOH")) && pentax_raw {
return Some(RawFormat::Pef);
}
- if make.starts_with("SAMSUNG") && has_cfa {
+ // a Samsung-branded body built on a Pentax design writes a Pentax-style maker note (the note's own magic)
- if make.starts_with("SAMSUNG") && has_pentax_maker_note(&t, bytes) {
++ if make.starts_with("SAMSUNG") && pentax_raw && has_pentax_maker_note(&t, bytes) {
+ return Some(RawFormat::Pef);
+ }
- if make.starts_with("SAMSUNG") {
++ if make.starts_with("SAMSUNG") && samsung_raw {
return Some(RawFormat::Srw);
}
+ if dng::is_plain_cfa_tiff(&t, bytes) {
+ return Some(RawFormat::CfaTiff);
+ }
if has_cfa || thumbnail_shell(&t, bytes.len()).is_some() || is_preview_container(ifd0) {
return Some(RawFormat::OtherTiff);
}
None
}
+ /// Whether the Exif maker note starts with the signature of the Pentax layouts (`AOC\0` or `PENTAX \0`).
+ fn has_pentax_maker_note(t: &Tiff, bytes: &[u8]) -> bool {
+ let Some(e) = t.exif().and_then(|e| e.get(lightcraft_tiff::tags::MAKER_NOTE)) else { return false };
+ bytes.get(e.offset as usize..).is_some_and(|n| n.starts_with(b"AOC\0") || n.starts_with(b"PENTAX \0"))
+ }
+
/// Whether some IFD is marked as raw: CFA photometric, or a raw-only compression value (99 is not a
-/// registered TIFF compression; Leaf MOS files use it for their tiled 16-bit lossless-JPEG raw).
+/// registered TIFF compression; Leaf MOS files use it for their tiled 16-bit lossless-JPEG raw),
+/// or vendor-specific raw tags (Sony tone curve, CFA pattern, or Pentax compression 65535).
fn has_raw_ifd(t: &Tiff) -> bool {
t.all_ifds().iter().any(|i| {
i.u16(lightcraft_tiff::tags::PHOTOMETRIC) == Some(lightcraft_tiff::tags::photometric::CFA)
@@@ -876,13 -910,6 +919,25 @@@ mod tests
assert_eq!(probe(&padded(&[rgb_ifd(16, 12), rgb_ifd(400, 300)])), None);
// no dimensions at all
assert_eq!(probe(&padded(&[IfdBuilder::new().with(t::MAKE, Value::Ascii("X".into()))])), None);
++ // raw evidence without a CFA photometric: Pentax's Exif `CFAPattern` (uncompressed / PackBits PEFs) and
++ // Samsung's private compressions (here 32773, PackBits elsewhere: NX1, NX500)
++ let mut pef = rgb_ifd(400, 300);
++ pef.set(t::MAKE, Value::Ascii("PENTAX Corporation".into()));
++ let mut exif = exif_size(400, 300);
++ exif.set(vendor::EXIF_CFA_PATTERN, Value::Undefined(vec![0, 2, 0, 2, 0, 1, 1, 2]));
++ pef.set_child(t::EXIF_IFD, exif);
++ assert_eq!(probe(&padded(&[pef])), Some(RawFormat::Pef));
++ let mut srw = rgb_ifd(400, 300);
++ srw.set(t::MAKE, Value::Ascii("SAMSUNG".into()));
++ srw.set(t::COMPRESSION, Value::Short(vec![32773]));
++ assert_eq!(probe(&padded(&[srw])), Some(RawFormat::Srw));
+ // camera-authored or exported TIFFs preserving camera Make tags remain ordinary images
+ for make in ["SONY", "NIKON CORPORATION", "PENTAX", "RICOH", "SAMSUNG"] {
+ let mut cam = rgb_ifd(400, 300);
+ cam.set(t::MAKE, Value::Ascii(make.into()));
+ cam.set_child(t::EXIF_IFD, exif_size(400, 300));
+ assert_eq!(probe(&padded(&[cam])), None, "{make} TIFF should not be probed as raw");
+ }
}
// --- containers that are recognised but not decoded, with a preview ---
@@@ -930,6 -983,142 +1011,148 @@@
assert_eq!(probe(&write(&[ifd])), Some(RawFormat::OtherTiff));
}
+ // --- TIFF raws that describe their raw IFD the way DNG does but carry no DNG version tag ---
+
+ /// 8 x 6 samples, 16 bits, value `1000 + 100 y + x`.
+ fn cfa_samples() -> Vec<u16> {
+ (0..48u16).map(|i| 1000 + 100 * (i / 8) + i % 8).collect()
+ }
+
+ /// A small raw in the layout of the Hasselblad 3FR/FFF family: IFD0 is a reduced RGB image with the
+ /// colour tags, a SubIFD holds the full-resolution CFA image (no `CFAPattern`, DNG-style levels and
+ /// crop). `strip` is the CFA image's data and `compression` its coding.
+ fn plain_cfa_tiff(compression: u16, strip: Vec<u8>, pattern: Option<[u8; 4]>) -> Vec<u8> {
+ let mut raw = IfdBuilder::new();
+ raw.set(t::NEW_SUBFILE_TYPE, Value::Long(vec![0]));
+ raw.set(t::IMAGE_WIDTH, Value::Long(vec![8]));
+ raw.set(t::IMAGE_LENGTH, Value::Long(vec![6]));
+ raw.set(t::BITS_PER_SAMPLE, Value::Short(vec![16]));
+ raw.set(t::SAMPLES_PER_PIXEL, Value::Short(vec![1]));
+ raw.set(t::PHOTOMETRIC, Value::Short(vec![t::photometric::CFA]));
+ raw.set(t::COMPRESSION, Value::Short(vec![compression]));
+ raw.set(t::BLACK_LEVEL, Value::Rational(vec![(256, 1)]));
+ raw.set(t::WHITE_LEVEL, Value::Long(vec![60000]));
+ raw.set(t::DEFAULT_CROP_ORIGIN, Value::Short(vec![2, 2]));
+ raw.set(t::DEFAULT_CROP_SIZE, Value::Short(vec![4, 2]));
+ if let Some(p) = pattern {
+ raw.set(t::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![2, 2]));
+ raw.set(t::CFA_PATTERN_EP, Value::Byte(p.to_vec()));
+ }
+ raw.set_image(ImageData::Strips { rows_per_strip: 6, strips: vec![strip] });
+ let mut ifd0 = rgb_ifd(4, 3);
+ ifd0.set(t::MAKE, Value::Ascii("Hasselblad".into()));
+ ifd0.set(t::COLOR_MATRIX_1, Value::SRational(vec![(5, 10), (-1, 10), (0, 10), (-5, 10), (12, 10), (3, 10), (-1, 10), (2, 10), (6, 10)]));
+ ifd0.set(t::AS_SHOT_NEUTRAL, Value::Rational(vec![(2, 5), (1, 1), (3, 5)]));
+ ifd0.add_sub_ifd(raw);
+ write(&[ifd0])
+ }
+
+ fn le_words(v: &[u16]) -> Vec<u8> {
+ v.iter().flat_map(|x| x.to_le_bytes()).collect()
+ }
+
+ /// Known answer: samples, black and white level, crop and colour come from the file's own tags; the
+ /// layout it does not state is RGGB.
+ #[test]
+ fn uncompressed_cfa_tiff_without_a_dng_version_decodes() {
+ let bytes = plain_cfa_tiff(1, le_words(&cfa_samples()), None);
+ assert_eq!(probe(&bytes), Some(RawFormat::CfaTiff));
+ let r = decode(&bytes).unwrap();
+ assert_eq!((r.format, r.width, r.height, r.bits), (RawFormat::CfaTiff, 8, 6, 16));
+ assert_eq!(r.data, RawData::U16(cfa_samples()));
+ assert_eq!(r.cfa.as_ref().unwrap().name(), "RGGB");
+ assert_eq!((r.black.mean(), r.white.clone()), (256.0, vec![60000.0]));
+ assert_eq!(r.crop, Rect::new(2, 2, 4, 2));
+ assert!(color::has_matrix(&r.color));
+ assert_eq!(r.color.as_shot_neutral, Some([0.4, 1.0, 0.6]));
+ // headers only: the same description without samples
+ let i = probe_info(&bytes).unwrap();
+ assert_eq!((i.format, i.cfa.as_ref().map(Cfa::name), i.crop), (RawFormat::CfaTiff, Some("RGGB".to_string()), Rect::new(2, 2, 4, 2)));
+ }
+
+ /// Without DNG's white-balance tag the file is left alone (a Kodak or Sinar TIFF with a CFA IFD, say).
+ #[test]
+ fn cfa_tiff_without_a_white_balance_tag_is_not_claimed() {
+ let mut raw = IfdBuilder::new();
+ raw.set(t::IMAGE_WIDTH, Value::Long(vec![8]));
+ raw.set(t::IMAGE_LENGTH, Value::Long(vec![6]));
+ raw.set(t::BITS_PER_SAMPLE, Value::Short(vec![16]));
+ raw.set(t::SAMPLES_PER_PIXEL, Value::Short(vec![1]));
+ raw.set(t::PHOTOMETRIC, Value::Short(vec![t::photometric::CFA]));
+ raw.set(t::COMPRESSION, Value::Short(vec![1]));
+ raw.set_image(ImageData::Strips { rows_per_strip: 6, strips: vec![le_words(&cfa_samples())] });
+ let mut ifd0 = rgb_ifd(4, 3);
+ ifd0.add_sub_ifd(raw);
+ assert_eq!(probe(&write(&[ifd0])), Some(RawFormat::OtherTiff));
+ }
+
+ /// A stated `CFAPattern` wins over the RGGB default.
+ #[test]
+ fn cfa_tiff_keeps_a_stated_pattern() {
+ let bytes = plain_cfa_tiff(1, le_words(&cfa_samples()), Some([2, 1, 1, 0]));
+ assert_eq!(decode(&bytes).unwrap().cfa.unwrap().name(), "BGGR");
+ }
+
+ /// The lossless-JPEG coding of the same layout goes through the lossless decoder (one strip for the
+ /// whole image, as the 3FR/FFF files have it).
+ #[test]
+ fn lossless_jpeg_cfa_tiff_decodes() {
+ let strip = ljpeg::encode(&cfa_samples(), 8, 6, 1, 16, 1, 0);
+ let bytes = plain_cfa_tiff(7, strip, None);
+ assert_eq!(probe(&bytes), Some(RawFormat::CfaTiff));
+ let r = decode(&bytes).unwrap();
+ assert_eq!(r.data, RawData::U16(cfa_samples()));
+ assert_eq!(r.cfa.unwrap().name(), "RGGB");
+ }
+
+ /// A lossless-JPEG scan whose predictor selection value is outside 0 to 7 (the 3FR/FFF files use 8) is
+ /// not a coding the lossless decoder knows: the file stays an undecodable raw, with that as the reason,
+ /// and keeps the stand-in preview an unsupported container gets.
+ #[test]
+ fn cfa_tiff_with_an_unknown_predictor_stays_undecodable() {
+ let mut strip = ljpeg::encode(&cfa_samples(), 8, 6, 1, 16, 1, 0);
+ let sos = strip.windows(2).position(|w| w == [0xff, 0xda]).unwrap();
+ let ss = sos + 2 + 2 + 1 + 2;
+ assert_eq!(strip[ss], 1, "selection value byte");
+ strip[ss] = 8;
+ assert!(matches!(ljpeg::decode(&strip, 1 << 20), Err(RawError::Unsupported(w)) if w.contains("selection value 8")));
+ let bytes = plain_cfa_tiff(7, strip, None);
+ assert_eq!(probe(&bytes), Some(RawFormat::OtherTiff));
+ assert!(!RawFormat::OtherTiff.is_supported());
+ let Err(RawError::Unsupported(why)) = decode(&bytes) else { panic!("expected Unsupported") };
+ assert!(why.contains("lossless JPEG") && why.contains("selection value 8"), "{why}");
+ }
+
+ /// Other codings of a CFA IFD (here Deflate) are not claimed by the plain reader.
+ #[test]
+ fn cfa_tiff_with_another_coding_is_not_claimed() {
+ let bytes = plain_cfa_tiff(8, vec![0; 96], None);
+ assert_eq!(probe(&bytes), Some(RawFormat::OtherTiff));
+ }
+
+ /// A Samsung-branded body whose maker note has a Pentax layout is read by the Pentax reader; another
+ /// Samsung file stays with the Samsung format.
+ #[test]
+ fn samsung_make_with_a_pentax_maker_note_is_pef() {
- let with_note = |note: &[u8]| {
++ let with_note = |note: &[u8], cfa: bool| {
+ let mut ifd = rgb_ifd(16, 12);
++ if cfa {
++ ifd.set(t::PHOTOMETRIC, Value::Short(vec![t::photometric::CFA]));
++ }
+ ifd.set(t::MAKE, Value::Ascii("SAMSUNG TECHWIN".into()));
+ let mut exif = IfdBuilder::new();
+ exif.set(t::MAKER_NOTE, Value::Undefined(note.to_vec()));
+ ifd.set_child(t::EXIF_IFD, exif);
+ write(&[ifd])
+ };
- assert_eq!(probe(&with_note(b"AOC\0MM\0\x01\0\0\0\0\0\0")), Some(RawFormat::Pef));
- assert_eq!(probe(&with_note(b"PENTAX \0MM\0\0\0\0\0\0\0")), Some(RawFormat::Pef));
- assert_eq!(probe(&with_note(b"STMN100\0\0\0\0\0\0\0\0\0")), Some(RawFormat::Srw));
++ assert_eq!(probe(&with_note(b"AOC\0MM\0\x01\0\0\0\0\0\0", true)), Some(RawFormat::Pef));
++ assert_eq!(probe(&with_note(b"PENTAX \0MM\0\0\0\0\0\0\0", true)), Some(RawFormat::Pef));
++ assert_eq!(probe(&with_note(b"STMN100\0\0\0\0\0\0\0\0\0", true)), Some(RawFormat::Srw));
++ // without raw evidence (an RGB image from such a body) neither reader claims it (#281)
++ assert_eq!(probe(&with_note(b"AOC\0MM\0\x01\0\0\0\0\0\0", false)), None);
++ assert_eq!(probe(&with_note(b"STMN100\0\0\0\0\0\0\0\0\0", false)), None);
+ }
+
#[test]
fn probe_rejects_junk() {
assert_eq!(probe(b""), None);