From 176810ad2ee5e76ca5e595018b73fc87bbfe2a55 Mon Sep 17 00:00:00 2001 From: Terra854 Date: Sat, 14 Nov 2026 21:49:09 +0800 Subject: [PATCH] fix: validate RAW framing and preserve decoder metadata --- crates/pcraft-bundle/src/lib.rs | 5 +- .../pcraft-bundle/tests/directory_cleanup.rs | 75 +++++++++ crates/raw/src/arw.rs | 1 + crates/raw/src/cr2.rs | 124 +++++++++++++- crates/raw/src/cr2/sraw.rs | 1 + crates/raw/src/cr3.rs | 2 + crates/raw/src/crw.rs | 1 + crates/raw/src/dng.rs | 103 ++++++++---- crates/raw/src/dng/compat_review_tests.rs | 120 +++++++++++++ crates/raw/src/dngwrite.rs | 1 + crates/raw/src/erf.rs | 1 + crates/raw/src/kodak.rs | 1 + crates/raw/src/lib.rs | 35 ++++ crates/raw/src/mef.rs | 1 + crates/raw/src/mrw.rs | 1 + crates/raw/src/nef.rs | 111 ++++++++++++- crates/raw/src/orf.rs | 42 +++++ crates/raw/src/pef.rs | 1 + crates/raw/src/preview.rs | 12 +- crates/raw/src/preview/tests.rs | 30 ++++ crates/raw/src/raf.rs | 157 ++++++++++++++++-- crates/raw/src/rw2.rs | 1 + crates/raw/src/srw.rs | 1 + crates/raw/src/tests.rs | 1 + crates/raw/src/tiffraw.rs | 85 ++++++++-- crates/raw/src/tiffraw/compat_review_tests.rs | 115 +++++++++++++ crates/raw/tests/it/dng.rs | 20 ++- crates/raw/tests/it/robust.rs | 1 + 28 files changed, 971 insertions(+), 78 deletions(-) create mode 100644 crates/raw/src/dng/compat_review_tests.rs create mode 100644 crates/raw/src/tiffraw/compat_review_tests.rs diff --git a/crates/pcraft-bundle/src/lib.rs b/crates/pcraft-bundle/src/lib.rs index 9156df70c..1368fa747 100644 --- a/crates/pcraft-bundle/src/lib.rs +++ b/crates/pcraft-bundle/src/lib.rs @@ -402,6 +402,10 @@ impl DirectoryBundleWriter { Err(std::fs::TryLockError::Error(_)) => break None, } }; + // A known unsafe manifest cannot be published. Refuse it before writing objects or + // previews, and still recheck immediately before publication below. + let manifest_path = root.join(craft_pcraft_format::MANIFEST); + refuse_symlink(&manifest_path)?; self.generation = self.generation.saturating_add(1); let generation = self.generation; let recheck = self.rolling_recheck(&bundle.entries); @@ -450,7 +454,6 @@ impl DirectoryBundleWriter { } let manifest = bundle.entries.get(craft_pcraft_format::MANIFEST).ok_or_else(|| Error::InvalidBundle("manifest not set".into()))?; - let manifest_path = root.join(craft_pcraft_format::MANIFEST); refuse_symlink(&manifest_path)?; craft_atomic::atomic_write(&manifest_path, manifest).map_err(io_error)?; stats.written = stats.written.saturating_add(1); diff --git a/crates/pcraft-bundle/tests/directory_cleanup.rs b/crates/pcraft-bundle/tests/directory_cleanup.rs index 29c09f72b..29015bd23 100644 --- a/crates/pcraft-bundle/tests/directory_cleanup.rs +++ b/crates/pcraft-bundle/tests/directory_cleanup.rs @@ -267,3 +267,78 @@ fn cleanup_does_not_traverse_windows_directory_junctions() { assert!(root.join("blobs").exists()); assert!(root.join("composite").exists()); } + +#[cfg(any(unix, windows))] +#[test] +fn manifest_symlink_is_refused_before_writing_any_objects_or_previews() { + let temp = Temporary::new(); + let root = temp.bundle(); + let mut writer = DirectoryBundleWriter::default(); + writer.save_directory(&root, entries(OLD)).unwrap(); + assert_manifest(&root, OLD); // Ordinary publication remains supported. + let external = temp.0.join("external-manifest.json"); + fs::write(&external, b"unchanged external manifest").unwrap(); + fs::remove_file(root.join(MANIFEST)).unwrap(); + #[cfg(unix)] + std::os::unix::fs::symlink(&external, root.join(MANIFEST)).unwrap(); + #[cfg(windows)] + if let Err(error) = std::os::windows::fs::symlink_file(&external, root.join(MANIFEST)) { + if error.raw_os_error() == Some(1314) || error.kind() == std::io::ErrorKind::Unsupported { + eprintln!("SKIP manifest symlink fixture: {error}"); + return; + } + panic!("create manifest symlink fixture: {error}"); + } + let mut changed = entries(NEW); + changed.add_object(ObjectKind::Tile, b"must not write tile").unwrap(); + changed.add_object(ObjectKind::Blob, b"must not write blob").unwrap(); + changed.set_preview(THUMB, b"must not write thumbnail".to_vec()).unwrap(); + changed.set_preview(FULL_COMPOSITE, b"must not write composite".to_vec()).unwrap(); + assert!(writer.save_directory(&root, changed).is_err()); + assert_eq!(fs::read(&external).unwrap(), b"unchanged external manifest"); + assert!(fs::symlink_metadata(root.join(MANIFEST)).unwrap().file_type().is_symlink()); + for unwritten in ["tiles", "blobs", THUMB, "composite"] { + assert!(!root.join(unwritten).exists(), "manifest refusal must precede {unwritten} publication"); + } +} + +#[cfg(unix)] +#[test] +fn chosen_root_symlink_cleanup_works_with_search_only_root_and_composite_permissions() { + use std::os::unix::fs::{PermissionsExt, symlink}; + struct Restore(PathBuf, fs::Permissions); + impl Restore { + fn restrict(path: &Path) -> Self { + let old = fs::metadata(path).unwrap().permissions(); + fs::set_permissions(path, fs::Permissions::from_mode(0o300)).unwrap(); + Self(path.to_path_buf(), old) + } + } + impl Drop for Restore { + fn drop(&mut self) { + let _ = fs::set_permissions(&self.0, self.1.clone()); + } + } + let temp = Temporary::new(); + let root = temp.bundle(); + let mut writer = DirectoryBundleWriter::default(); + let mut first = entries(OLD); + let stale = first.add_object(ObjectKind::Blob, b"stale blob").unwrap(); + first.set_preview(THUMB, b"stale thumbnail".to_vec()).unwrap(); + first.set_preview(COMPOSITE, b"stale preview".to_vec()).unwrap(); + writer.save_directory(&root, first).unwrap(); + let chosen = temp.0.join("chosen.pcraft"); + symlink(&root, &chosen).unwrap(); + let composite_mode = Restore::restrict(&root.join("composite")); + let root_mode = Restore::restrict(&root); + let result = writer.save_directory(&chosen, entries(NEW)); + drop(root_mode); + drop(composite_mode); + let stats = result.expect("chosen root resolves before cleanup; traversal does not require directory read permission"); + assert_manifest(&root, NEW); + assert_eq!(stats.removed, 1); + assert!(!root.join(stale).exists()); + assert!(!root.join(THUMB).exists()); + assert!(!root.join(COMPOSITE).exists()); + assert!(fs::symlink_metadata(chosen).unwrap().file_type().is_symlink()); +} diff --git a/crates/raw/src/arw.rs b/crates/raw/src/arw.rs index 639fc0032..18273866c 100644 --- a/crates/raw/src/arw.rs +++ b/crates/raw/src/arw.rs @@ -842,6 +842,7 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result) -> Option { Some(Cfa::bayer_static(name)) } +/// Fallback for files without CR2CFAPattern: Canon red sites are in even sensor columns, +/// but the row phase varies. The green diagonal must agree on both local differences and +/// relative block means; black, clipped, tiny and ambiguous frames remain inconclusive. +fn measured_red_row(data: &[u16], width: usize, area: Rect, black: f32, white: f32) -> Option { + let mut diff = [0f64; 2]; + let mut upper = [0f64; 2]; + let mut lower = [0f64; 2]; + let mut n = [0u64; 2]; + let (lo, hi) = (black + 1.0, black + (white - black) * 0.98); + let x_end = area.x.saturating_add(area.width).min(width).saturating_sub(1); + let y_end = area.y.saturating_add(area.height).saturating_sub(1); + let row = |y: usize| data.get(y.checked_mul(width)?..y.checked_add(1)?.checked_mul(width)?); + let mut y = area.y.saturating_add(area.y & 1); + while y < y_end { + let (Some(r0), Some(r1)) = (row(y), y.checked_add(1).and_then(row)) else { break }; + for x in area.x..x_end { + let (Some(&a), Some(&b)) = (r0.get(x), r1.get(x + 1)) else { break }; + let (a, b) = (f32::from(a), f32::from(b)); + if a < lo || b < lo || a > hi || b > hi { + continue; + } + let p = x & 1; + diff[p] += f64::from((a - b).abs()); + upper[p] += f64::from(a - black); + lower[p] += f64::from(b - black); + n[p] += 1; + } + y = y.saturating_add(4); + } + if n.iter().any(|&k| k < 256) { + return None; + } + let d = [diff[0] / n[0] as f64, diff[1] / n[1] as f64]; + let rel = [0, 1].map(|p| (upper[p] - lower[p]).abs() / (upper[p] + lower[p]).max(1.0)); + if d[0] < 0.8 * d[1] && rel[0] < rel[1] { + Some(1) + } else if d[1] < 0.8 * d[0] && rel[1] < rel[0] { + Some(0) + } else { + None + } +} + +fn fallback_cfa(data: &[u16], width: usize, area: Rect, black: f32, white: f32, model: Option) -> (Cfa, bool) { + // Observed CC0 raw.pixls.us captures: 5D II, 7D, 550D, 600D and 1200D. + let green_first = [0x8000_0218, 0x8000_0250, 0x8000_0270, 0x8000_0286, 0x8000_0327].into_iter().any(|id| model == Some(id)); + let measured = measured_red_row(data, width, area, black, white); + let row = measured.unwrap_or(usize::from(green_first)); + (Cfa::bayer_static(if row == 1 { "GBRG" } else { "RGGB" }), measured.is_none() && !green_first) +} + /// The 16-bit words of a maker-note array. Canon writes `ColorData` as SHORT in most models, but as UNDEFINED /// bytes (count = byte length, e.g. 5120) in the PowerShot / EOS M models with ColorData versions -3 and -4; /// those bytes are the same 16-bit words in the maker note's byte order, so they are paired up, not read one by @@ -198,6 +253,7 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result { let total = (fw as u64) @@ -270,12 +326,19 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result= 2 => black_from_columns_sourced(&data, width, cols, active.y..active.y + active.height, active), _ => (BlackLevel::uniform(0.0), crate::LevelSource::Fallback), }; + let model = mn.as_ref().and_then(|m| m.ifd.u64(CANON_MODEL_ID)); + let (cfa, assumed_cfa) = match tagged_cfa { + Some(cfa) => (cfa, false), + None => { + let (cfa, guessed) = fallback_cfa(&data, width, active, black.mean(), white, model); + (cfa, guessed || mode == Mode::Header) + } + }; let img = RawImage { format: RawFormat::Cr2, width, @@ -298,6 +361,7 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result = (0..w * h) + .map(|i| { + let (x, y) = (i % w, i / w); + let texture = ((x / 2 + y / 2) % 17) as u16; + if i % 97 == 0 { + return 13000; + } + 100 + if (y & 1) == red_row { + if x & 1 == 0 { 500 + texture } else { 1000 + texture } + } else if x & 1 == 0 { + 1000 + texture + } else { + 667 + texture + } + }) + .collect(); + let expected = if red_row == 0 { "RGGB" } else { "GBRG" }; + for (left, top) in [(16, 4), (17, 4), (16, 5), (17, 5)] { + let area = Rect::new(left, top, w - left, h - top); + assert_eq!(fallback_cfa(&samples, w, area, 100.0, 4000.0, None).0.name(), expected); + } + let mut bytes = synthetic_cr2_of(&samples, w, h, None, None); + let marker = [0xe0, 0xc5, 4, 0, 1, 0, 0, 0]; + let at = bytes.windows(marker.len()).position(|b| b == marker).unwrap(); + // The explicit tag remains authoritative even when the observed sensor phase disagrees. + bytes[at + 8..at + 12].copy_from_slice(&4u32.to_le_bytes()); + let explicit = crate::decode(&bytes).unwrap(); + assert_eq!(explicit.cfa.as_ref().unwrap().name(), "GRBG"); + assert_eq!(explicit.info(), crate::probe_info(&bytes).unwrap()); + assert!(!explicit.notices.assumed_cfa); + bytes[at..at + 2].copy_from_slice(&0xc5dfu16.to_le_bytes()); + let full = crate::decode(&bytes).unwrap(); + assert_eq!(full.cfa.as_ref().unwrap().name(), expected); + assert!(!full.notices.assumed_cfa); + let header = decode(&bytes, Mode::Header, &Limits::default()).unwrap(); + assert!(header.notices.assumed_cfa); + assert!(matches!(header.data, RawData::U16(ref v) if v.is_empty())); + assert_eq!(full.data, RawData::U16(samples)); + } + } + + #[test] + fn inconclusive_cfa_measurement_keeps_observed_model_fallback_and_safe_geometry() { + let data = vec![512; 64 * 32]; + let area = Rect::new(0, 0, 64, 32); + assert_eq!(fallback_cfa(&data, 64, area, 512.0, 16000.0, Some(0x8000_0250)).0.name(), "GBRG"); + assert_eq!(fallback_cfa(&data, 64, area, 512.0, 16000.0, None).0.name(), "RGGB"); + for (width, area) in [(0, area), (64, Rect::new(usize::MAX, usize::MAX, 3, 3)), (64, Rect::new(63, 31, 1, 1))] { + assert_eq!(measured_red_row(&data, width, area, 512.0, 16000.0), None); + } + } + #[test] fn wb_probe() { let mut v = vec![0u64; 120]; diff --git a/crates/raw/src/cr2/sraw.rs b/crates/raw/src/cr2/sraw.rs index 7ad98906b..4b02881d2 100644 --- a/crates/raw/src/cr2/sraw.rs +++ b/crates/raw/src/cr2/sraw.rs @@ -393,6 +393,7 @@ pub(super) fn decode(bytes: &[u8], tiff: &Tiff, raw: &Ifd, mode: Mode, limits: & camera: tiff.ifds.first().map(Camera::from_ifd0).unwrap_or_default(), cinema: Default::default(), lens_metadata: Default::default(), + notices: Default::default(), level_sources: Default::default(), }; img.validate_for(mode)?; diff --git a/crates/raw/src/cr3.rs b/crates/raw/src/cr3.rs index 956446c6e..8f2bbd27e 100644 --- a/crates/raw/src/cr3.rs +++ b/crates/raw/src/cr3.rs @@ -274,6 +274,7 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result Result = levels(t::WHITE_LEVEL).f64s(t::WHITE_LEVEL).unwrap_or_default().into_iter().map(|v| v as f32).filter(|v| v.is_finite() && *v > 0.0).collect(); + let white_source = if !white.is_empty() || table_max.is_some() { LevelSource::Metadata } else { LevelSource::Fallback }; if white.is_empty() { white = vec![default_white]; } @@ -333,7 +339,13 @@ fn decode_ifd(bytes: &[u8], tiff: &Tiff, pixels: &Ifd, main: &Ifd, mode: Mode, f }; // (a FLOAT / DOUBLE tag can hold NaN or infinity: such a level reads as 0, in its place) let level = |v: f64| Some(v as f32).filter(|v| v.is_finite()).unwrap_or(0.0); - let mut values: Vec = levels(t::BLACK_LEVEL).f64s(t::BLACK_LEVEL).unwrap_or_default().into_iter().map(level).collect(); + let declared_black = levels(t::BLACK_LEVEL).f64s(t::BLACK_LEVEL).unwrap_or_default(); + let black_source = if !declared_black.is_empty() && declared_black.iter().all(|value| (*value as f32).is_finite()) { + LevelSource::Metadata + } else { + LevelSource::Fallback + }; + let mut values: Vec = declared_black.into_iter().map(level).collect(); // A TIFF that is not a DNG may state four black levels without `BlackLevelRepeatDim`: one per site of its 2 × 2 // CFA, row-major (PhotoCraft's reading of Hasselblad files). In a DNG the count has to match the repeat. let (br, bc) = if format != RawFormat::Dng && cpp == 1 && values.len() == 4 && !levels(t::BLACK_LEVEL).contains(t::BLACK_LEVEL_REPEAT_DIM) { @@ -364,7 +376,7 @@ fn decode_ifd(bytes: &[u8], tiff: &Tiff, pixels: &Ifd, main: &Ifd, mode: Mode, f } _ => Rect::new(0, 0, full_w, full_h), }; - let crop = default_crop(raw, active_area); + let (crop, default_crop_adjusted) = default_crop(raw, active_area, full_w, full_h); let (active_area, crop) = if (full_w, full_h) == (w, h) { (active_area, crop) } else { @@ -378,6 +390,7 @@ fn decode_ifd(bytes: &[u8], tiff: &Tiff, pixels: &Ifd, main: &Ifd, mode: Mode, f (area, scale(crop).clipped(area.width, area.height)) }; + let mut assumed_cfa = false; let cfa = if info.photometric == photometric::CFA { if raw.u16(t::CFA_LAYOUT).unwrap_or(1) != 1 { return Err(RawError::Unsupported("non-rectangular CFA layout".into())); @@ -391,7 +404,10 @@ fn decode_ifd(bytes: &[u8], tiff: &Tiff, pixels: &Ifd, main: &Ifd, mode: Mode, f let assumed = [0u8, 1, 1, 2]; let pat = match raw.bytes(t::CFA_PATTERN_EP) { Some(p) => p, - None if format != RawFormat::Dng && (rows, cols) == (2, 2) => &assumed, + None if format != RawFormat::Dng && (rows, cols) == (2, 2) => { + assumed_cfa = true; + &assumed + } None => return Err(RawError::Corrupt("missing CFAPattern".into())), }; if pat.len() != rows * cols { @@ -422,6 +438,9 @@ fn decode_ifd(bytes: &[u8], tiff: &Tiff, pixels: &Ifd, main: &Ifd, mode: Mode, f let opcodes = crate::OpcodeLists { list1: opcode_list(t::OPCODE_LIST_1), list2: opcode_list(t::OPCODE_LIST_2), list3: opcode_list(t::OPCODE_LIST_3) }; + let color = color_data(ifd0, raw, tiff.order); + let gain_table_omitted = [raw, ifd0].iter().any(|ifd| ifd.contains(t::PROFILE_GAIN_TABLE_MAP) || ifd.contains(t::PROFILE_GAIN_TABLE_MAP_2)) + && color.profile.gain_table_map.is_none(); let img = RawImage { format, width: w, @@ -435,7 +454,7 @@ fn decode_ifd(bytes: &[u8], tiff: &Tiff, pixels: &Ifd, main: &Ifd, mode: Mode, f active_area, crop, orientation: Orientation::from_exif(ifd0.u16(t::ORIENTATION).unwrap_or(1)), - color: color_data(ifd0, raw, tiff.order), + color, wb_multipliers: None, linearized, pixel_aspect: default_scale_aspect(raw.f64s(t::DEFAULT_SCALE).as_deref()), @@ -443,7 +462,8 @@ fn decode_ifd(bytes: &[u8], tiff: &Tiff, pixels: &Ifd, main: &Ifd, mode: Mode, f camera: Camera::from_ifd0(ifd0), cinema: cinema_tags(ifd0, raw), lens_metadata: Default::default(), - level_sources: Default::default(), + level_sources: LevelSources { black: black_source, white: white_source }, + notices: RawNotices { default_crop_adjusted, gain_table_omitted, assumed_cfa }, }; img.validate_for(mode)?; Ok(img) @@ -472,33 +492,45 @@ fn cinema_tags(ifd0: &Ifd, raw: &Ifd) -> CinemaTags { } } -/// The default crop, relative to the active area. Each tag has its own default (DNG 1.7.1, `DefaultCropOrigin` -/// and `DefaultCropSize`): the origin (0, 0), the size the whole area, so a file with only a size crops from the -/// area's corner. A tag that is present must hold two finite, non-negative values, and the crop must start inside -/// the area and be at least one pixel each way; otherwise the crop is the whole active area. -fn default_crop(raw: &Ifd, active_area: Rect) -> Rect { +/// The default crop is relative to ActiveArea. DNG 1.7.1 defines independent +/// defaults: origin (0, 0), size ImageWidth/ImageLength. An unusable optional crop +/// preserves the entire active image. Valid partly overlapping rectangles retain the +/// existing clipped-crop policy, with an explicit notice rather than silent adjustment. +fn default_crop(raw: &Ifd, active_area: Rect, full_width: usize, full_height: usize) -> (Rect, bool) { let whole = Rect::new(0, 0, active_area.width, active_area.height); - let pair = |tag: u16, default: [f64; 2]| -> Option<[f64; 2]> { - if !raw.contains(tag) { - return Some(default); - } - match raw.f64s(tag).as_deref() { - Some([a, b]) if a.is_finite() && b.is_finite() && *a >= 0.0 && *b >= 0.0 => Some([*a, *b]), - _ => None, - } - }; - let (Some([x, y]), Some([cw, ch])) = - (pair(t::DEFAULT_CROP_ORIGIN, [0.0, 0.0]), pair(t::DEFAULT_CROP_SIZE, [active_area.width as f64, active_area.height as f64])) - else { - return whole; - }; - if cw < 1.0 || ch < 1.0 { - return whole; + if !raw.contains(t::DEFAULT_CROP_ORIGIN) && !raw.contains(t::DEFAULT_CROP_SIZE) { + return (whole, false); } - // (`as usize` saturates, and `clipped` holds the origin to the area) - let crop = - Rect::new(x.round() as usize, y.round() as usize, cw.round() as usize, ch.round() as usize).clipped(active_area.width, active_area.height); - if crop.width == 0 || crop.height == 0 { whole } else { crop } + let pair = |tag: u16, default: [f64; 2]| -> Option<[usize; 2]> { + let values = if raw.contains(tag) { + match raw.f64s(tag).as_deref() { + Some([a, b]) => [*a, *b], + _ => return None, + } + } else { + default + }; + // Float-to-integer casts saturate. Refuse nonfinite/unrepresentable numbers before + // casting so a hostile origin cannot become a seemingly valid clipped rectangle. + if values.iter().any(|value| !value.is_finite() || value.round() >= usize::MAX as f64) { + return None; + } + if values.iter().any(|value| *value < 0.0) { + return None; + } + Some(values.map(|value| value.round() as usize)) + }; + let (Some([x, y]), Some([width, height])) = + (pair(t::DEFAULT_CROP_ORIGIN, [0.0, 0.0]), pair(t::DEFAULT_CROP_SIZE, [full_width as f64, full_height as f64])) + else { + return (whole, true); + }; + let requested = Rect::new(x, y, width, height); + let crop = requested.clipped(active_area.width, active_area.height); + if crop.width == 0 || crop.height == 0 { + return (whole, true); + } + (crop, crop != requested) } /// Pixel height over width from `DefaultScale` (horizontal, vertical): only a clear 1.5 to 4 ratio counts (the rows stretch; a wider-than-tall pixel is left alone). @@ -509,6 +541,9 @@ fn default_scale_aspect(scale: Option<&[f64]>) -> f32 { } } +#[cfg(test)] +mod compat_review_tests; + #[cfg(test)] mod tests { #[test] diff --git a/crates/raw/src/dng/compat_review_tests.rs b/crates/raw/src/dng/compat_review_tests.rs new file mode 100644 index 000000000..48300cbf7 --- /dev/null +++ b/crates/raw/src/dng/compat_review_tests.rs @@ -0,0 +1,120 @@ +use super::*; +use craft_tiff::{Entry, IfdBuilder, ImageData, TiffWriter}; + +fn ifd(tags: Vec<(u16, Value)>) -> Ifd { + let mut entries: Vec<_> = tags.into_iter().map(|(tag, value)| Entry { tag, value, offset: 0 }).collect(); + entries.sort_by_key(|entry| entry.tag); + Ifd { entries, ..Default::default() } +} + +#[test] +fn optional_crop_adjustments_are_explicit_and_valid_clipping_is_preserved() { + let active = Rect::new(3, 2, 11, 8); + let whole = Rect::new(0, 0, 11, 8); + let clipped = ifd(vec![(t::DEFAULT_CROP_ORIGIN, Value::Long(vec![10, 0])), (t::DEFAULT_CROP_SIZE, Value::Long(vec![5, 4]))]); + assert_eq!(default_crop(&clipped, active, 16, 12), (Rect::new(10, 0, 1, 4), true)); + assert_eq!(default_crop(&ifd(vec![(t::DEFAULT_CROP_ORIGIN, Value::Long(vec![1, 1]))]), active, 16, 12), (Rect::new(1, 1, 10, 7), true)); + for fields in [ + vec![(t::DEFAULT_CROP_SIZE, Value::Long(vec![4]))], + vec![(t::DEFAULT_CROP_ORIGIN, Value::Long(vec![0, 0])), (t::DEFAULT_CROP_SIZE, Value::Long(vec![0, 4]))], + ] { + assert_eq!(default_crop(&ifd(fields), active, 16, 12), (whole, true)); + } + assert_eq!(default_crop(&Ifd::default(), active, 16, 12), (whole, false)); + assert_eq!(default_crop(&ifd(vec![(t::DEFAULT_CROP_SIZE, Value::Long(vec![5, 4]))]), active, 16, 12), (Rect::new(0, 0, 5, 4), false)); + assert_eq!( + default_crop(&ifd(vec![(t::DEFAULT_CROP_ORIGIN, Value::Long(vec![2, 1])), (t::DEFAULT_CROP_SIZE, Value::Long(vec![5, 4]))]), active, 16, 12), + (Rect::new(2, 1, 5, 4), false) + ); +} + +#[test] +fn hostile_crop_coordinates_fall_back_explicitly_before_saturating_casts() { + for bad in [1e300, f64::NAN, f64::INFINITY, f64::NEG_INFINITY] { + for tag in [t::DEFAULT_CROP_ORIGIN, t::DEFAULT_CROP_SIZE] { + let raw = ifd(vec![(tag, Value::Double(vec![bad, 1.]))]); + assert_eq!(default_crop(&raw, Rect::new(0, 0, 16, 12), 16, 12), (Rect::new(0, 0, 16, 12), true)); + } + } +} + +fn linear(extra: &[(u16, Value)]) -> Vec { + let mut raw = IfdBuilder::new(); + for (tag, value) in [ + (t::IMAGE_WIDTH, Value::Long(vec![8])), + (t::IMAGE_LENGTH, Value::Long(vec![6])), + (t::BITS_PER_SAMPLE, Value::Short(vec![16])), + (t::SAMPLES_PER_PIXEL, Value::Short(vec![1])), + (t::PHOTOMETRIC, Value::Short(vec![photometric::LINEAR_RAW])), + (t::COMPRESSION, Value::Short(vec![1])), + ] { + raw.set(tag, value); + } + for (tag, value) in extra { + raw.set(*tag, value.clone()); + } + raw.set_image(ImageData::Strips { rows_per_strip: 6, strips: vec![(0u16..48).flat_map(u16::to_le_bytes).collect()] }); + TiffWriter::new(ByteOrder::Little, false).write(&[raw]).unwrap() +} + +#[test] +fn dng_and_generic_cfa_tiff_report_actual_level_sources_instead_of_unknown() { + for format in [RawFormat::Dng, RawFormat::CfaTiff] { + let missing = decode_as(&linear(&[]), Mode::Full, format, &Limits::default()).unwrap(); + assert_eq!(missing.level_sources, LevelSources { black: LevelSource::Fallback, white: LevelSource::Fallback }); + let declared = decode_as( + &linear(&[(t::BLACK_LEVEL, Value::Short(vec![17])), (t::WHITE_LEVEL, Value::Short(vec![1000]))]), + Mode::Full, + format, + &Limits::default(), + ) + .unwrap(); + assert_eq!(declared.level_sources, LevelSources { black: LevelSource::Metadata, white: LevelSource::Metadata }); + assert_eq!(declared.black.values, vec![17.]); + assert_eq!(declared.white, vec![1000.]); + let invalid = decode_as( + &linear(&[(t::BLACK_LEVEL, Value::Double(vec![f64::NAN])), (t::WHITE_LEVEL, Value::Double(vec![f64::INFINITY]))]), + Mode::Full, + format, + &Limits::default(), + ) + .unwrap(); + assert_eq!(invalid.level_sources, LevelSources { black: LevelSource::Fallback, white: LevelSource::Fallback }); + } + let table = decode_as(&linear(&[(t::LINEARIZATION_TABLE, Value::Short(vec![0, 1000]))]), Mode::Full, RawFormat::Dng, &Limits::default()).unwrap(); + assert_eq!(table.white, vec![1000.]); + assert_eq!(table.level_sources.white, LevelSource::Metadata); +} + +#[test] +fn decoded_notices_record_actual_crop_gain_table_and_cfa_decisions() { + let plain = decode_as(&linear(&[]), Mode::Full, RawFormat::Dng, &Limits::default()).unwrap(); + assert_eq!(plain.notices, RawNotices::default()); + let adjusted = decode_as( + &linear(&[(t::DEFAULT_CROP_ORIGIN, Value::Long(vec![7, 0])), (t::DEFAULT_CROP_SIZE, Value::Long(vec![5, 4]))]), + Mode::Full, + RawFormat::Dng, + &Limits::default(), + ) + .unwrap(); + assert_eq!(adjusted.crop, Rect::new(7, 0, 1, 4)); + assert!(adjusted.notices.default_crop_adjusted); + let invalid_gain = + decode_as(&linear(&[(t::PROFILE_GAIN_TABLE_MAP, Value::Undefined(vec![0; 16]))]), Mode::Full, RawFormat::Dng, &Limits::default()).unwrap(); + assert!(invalid_gain.notices.gain_table_omitted); + assert!(invalid_gain.color.profile.gain_table_map.is_none()); + let assumed = + decode_as(&linear(&[(t::PHOTOMETRIC, Value::Short(vec![photometric::CFA]))]), Mode::Full, RawFormat::CfaTiff, &Limits::default()).unwrap(); + assert!(assumed.notices.assumed_cfa); + assert_eq!(assumed.cfa.as_ref().unwrap().pattern, vec![0, 1, 1, 2]); + let tagged = decode_as( + &linear(&[(t::PHOTOMETRIC, Value::Short(vec![photometric::CFA])), (t::CFA_PATTERN_EP, Value::Byte(vec![0, 1, 1, 2]))]), + Mode::Full, + RawFormat::Dng, + &Limits::default(), + ) + .unwrap(); + assert!(!tagged.notices.assumed_cfa); + let limits = Limits { max_samples: 47, ..Default::default() }; + assert!(matches!(decode_as(&linear(&[]), Mode::Full, RawFormat::Dng, &limits), Err(RawError::Limit(_)))); +} diff --git a/crates/raw/src/dngwrite.rs b/crates/raw/src/dngwrite.rs index d7f6cdedd..8953f94ea 100644 --- a/crates/raw/src/dngwrite.rs +++ b/crates/raw/src/dngwrite.rs @@ -462,6 +462,7 @@ mod tests { cinema: Default::default(), lens_metadata: Default::default(), level_sources: Default::default(), + notices: crate::RawNotices::default(), }; let opts = |tile: u32| DngWriteOptions { compression: DngCompression::Deflate { tile, half: false }, ..DngWriteOptions::default() }; let hostile = write_dng(&raw, &opts(u32::MAX)).expect("the clamped tile writes"); diff --git a/crates/raw/src/erf.rs b/crates/raw/src/erf.rs index 40190ca20..fcdfa43cd 100644 --- a/crates/raw/src/erf.rs +++ b/crates/raw/src/erf.rs @@ -128,6 +128,7 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result Result, } +/// Bounded diagnostics for decisions actually made by a raw decoder. An absent optional tag +/// does not itself set a notice; hosts can translate these flags into their own warning text +/// without inferring a fallback from missing metadata or retaining source-controlled strings. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(default)] +pub struct RawNotices { + /// Explicit default-crop metadata needed correction or a fallback to the active area. + pub default_crop_adjusted: bool, + /// A source profile gain table could not be used and was omitted from the decoded image. + pub gain_table_omitted: bool, + /// The CFA layout was selected by a decoder fallback instead of usable source metadata. + pub assumed_cfa: bool, +} + /// A decoded raw image. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct RawImage { @@ -1041,6 +1055,9 @@ pub struct RawImage { /// measured levels are placeholders. #[serde(default)] pub level_sources: LevelSources, + /// Fixed-size decoder decisions, separate from ordinary missing optional metadata. + #[serde(default)] + pub notices: RawNotices, } impl RawImage { @@ -1173,3 +1190,21 @@ mod checked_arithmetic_tests { assert_eq!(Orientation::from_exif(9), Orientation::Normal); } } + +#[cfg(test)] +mod raw_notices_tests { + use super::*; + + #[test] + fn missing_notices_default_to_no_claims_and_explicit_flags_are_independent() { + type Error = serde::de::value::Error; + let empty = serde::de::value::MapDeserializer::<_, Error>::new(std::iter::empty::<(&str, bool)>()); + assert_eq!(RawNotices::deserialize(empty).unwrap(), RawNotices::default()); + let one = serde::de::value::MapDeserializer::<_, Error>::new([("assumed_cfa", true)].into_iter()); + let notices = RawNotices::deserialize(one).unwrap(); + let copied = notices; + assert_eq!(notices, copied); + assert_eq!(notices, RawNotices { assumed_cfa: true, default_crop_adjusted: false, gain_table_omitted: false }); + assert!(!std::mem::needs_drop::(), "decoder notices own no allocated strings"); + } +} diff --git a/crates/raw/src/mef.rs b/crates/raw/src/mef.rs index 5cd1fb705..36ba18e0f 100644 --- a/crates/raw/src/mef.rs +++ b/crates/raw/src/mef.rs @@ -128,6 +128,7 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result Result Result= 2 && v[0] > 0.1 && v[1] > 0.1 && v[0] < 10.0 && v[1] < 10.0) .map(|v| [v[0] as f32, 1.0, v[1] as f32]); - let cfa = match (raw.u64s(t::CFA_REPEAT_PATTERN_DIM).as_deref(), raw.bytes(t::CFA_PATTERN_EP)) { + let (cfa, assumed_cfa) = match (raw.u64s(t::CFA_REPEAT_PATTERN_DIM).as_deref(), raw.bytes(t::CFA_PATTERN_EP)) { (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => { - Cfa { width: 2, height: 2, pattern: p.to_vec(), origin_x: 0, origin_y: 0 } + (Cfa { width: 2, height: 2, pattern: p.to_vec(), origin_x: 0, origin_y: 0 }, false) } - _ => Cfa::bayer_static("RGGB"), + _ => (Cfa::bayer_static("RGGB"), true), }; let model = ifd0.string(t::MODEL).unwrap_or_default(); let body = measured_body(&model, w, h, bits); @@ -330,6 +330,7 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result Opt } let stride = strip / h; let tight = w.checked_mul(bits as usize)?.div_ceil(8); - let tight = if bits == 12 && w.is_multiple_of(2) || bits == 14 && w.is_multiple_of(4) { tight } else { return None }; + // Unmarked data must still match a measured complete-group layout: do not guess a packing for compressed + // data. An explicit packed maker mode permits a final partial group, byte-aligned independently on each row. + if !stated && !(bits == 12 && w.is_multiple_of(2) || bits == 14 && w.is_multiple_of(4)) { + return None; + } let padded = tight.checked_next_multiple_of(16)?; let lsb_ok = match bits { 12 => stride == tight || stride == padded, @@ -524,7 +529,7 @@ fn compressed(bytes: &[u8], info: &ImageInfo, mn: Option<&makernote::MakerNote>, } /// A "compressed" strip of packed samples: the strip size must match a measured layout exactly. `stated` is set -/// when the maker note names the packed mode, which also allows 14-bit rows without padding. +/// when the maker note names the packed mode, which also allows partial groups and 14-bit rows without padding. fn packed(bytes: &[u8], info: &ImageInfo, rows: usize, stated: bool, limits: &Limits) -> Result { let (w, h) = (info.width as usize, info.height as usize); let bits = info.bits() as u32; @@ -663,6 +668,7 @@ fn small_raw(bytes: &[u8], tiff: &Tiff, info: &ImageInfo, strip: craft_tiff::ima cinema: Default::default(), lens_metadata: Default::default(), level_sources: Default::default(), + notices: Default::default(), }; img.validate_for(mode)?; Ok(img) @@ -1513,4 +1519,95 @@ mod tests { } } } + + mod partial_row_tests { + use super::*; + + // Independent wire oracle: place each sample bit directly, leaving nonzero tail bits and padding. + fn image(data: &[u16], w: usize, bits: u32, padded: bool) -> Vec { + let tight = (w * bits as usize).div_ceil(8); + let stride = if padded { tight.next_multiple_of(16) } else { tight }; + let mut bytes = vec![0xA5; stride * (data.len() / w)]; + for (i, &sample) in data.iter().enumerate() { + for bit in 0..bits as usize { + let at = (i % w) * bits as usize + bit; + let byte = &mut bytes[(i / w) * stride + at / 8]; + *byte = (*byte & !(1 << (at % 8))) | (((sample >> bit) as u8 & 1) << (at % 8)); + } + } + bytes + } + + fn note(order: ByteOrder, modern: bool, mode: u16) -> IfdBuilder { + let mut n = IfdBuilder::new(); + if modern { + let mut record = b"01000400\0\0".to_vec(); + record.extend(if order == ByteOrder::Big { mode.to_be_bytes() } else { mode.to_le_bytes() }); + n.set(NEF_COMPRESSION_RECORD, Value::Undefined(record)); + } else { + n.set(NEF_COMPRESSION, Value::Short(vec![mode])); + } + n + } + + #[test] + fn declared_packed_partial_rows_preserve_every_sample() { + let h = 5; + for (bits, mode) in [(12u32, 6), (14, 10)] { + for w in [1, 3, 5, 22, 23] { + let data = samples(w * h, bits); + for padded in [false, true] { + let wire = image(&data, w, bits, padded); + if w == 22 && bits == 14 { + assert_eq!(wire.len() / h, if padded { 48 } else { 39 }); + } + for order in [ByteOrder::Big, ByteOrder::Little] { + for modern in [false, true] { + let file = nef_in( + order, + 34713, + bits as u16, + vec![wire.clone()], + w as u32, + h as u32, + h as u32, + Some(note(order, modern, mode)), + ); + let raw = crate::decode(&file).unwrap(); + assert_eq!((raw.width, raw.height, raw.cpp), (w, h, 1)); + assert!(!raw.notices.assumed_cfa, "valid CFA tag is retained"); + let RawData::U16(got) = raw.data else { panic!("integer samples") }; + assert_eq!(got, data, "{bits}-bit width {w}, padded {padded}, {order:?}, modern {modern}"); + } + } + } + } + } + } + + #[test] + fn partial_rows_keep_mode_depth_and_strip_guards() { + let (w, h) = (22, 5); + let data = samples(w * h, 14); + for padded in [false, true] { + let wire = image(&data, w, 14, padded); + let file = |bytes, mode: Option| { + nef_with_note(34713, 14, vec![bytes], w as u32, h as u32, h as u32, mode.map(|m| note(ByteOrder::Big, false, m))) + }; + for mode in [None, Some(0), Some(6), Some(13), Some(14)] { + assert!(matches!(crate::decode(&file(wire.clone(), mode)), Err(RawError::Unsupported(_))), "mode {mode:?}"); + } + let good = file(wire.clone(), Some(10)); + assert!(crate::decode(&good).is_ok()); + let short = truncated_tail_strip(good, wire.len() - 1); + assert!(matches!(crate::decode(&short), Err(RawError::Corrupt(_)))); + for len in [wire.len() - 1, wire.len() + 1] { + assert!(matches!(crate::decode(&file(vec![0; len], Some(10))), Err(RawError::Unsupported(_)))); + } + } + for (w, h, bits) in [(0, 5, 14), (22, 0, 14), (22, 5, 13), (usize::MAX, 5, 14)] { + assert!(packed_layout(w, h, bits, u64::MAX, true).is_none()); + } + } + } } diff --git a/crates/raw/src/orf.rs b/crates/raw/src/orf.rs index ebfa28eb2..68e0b66bb 100644 --- a/crates/raw/src/orf.rs +++ b/crates/raw/src/orf.rs @@ -207,6 +207,19 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result 0 && *n <= limits.samples()).ok_or(RawError::Limit("image too large"))?; + // A partial TIFF directory is damage, not evidence of an unsupported compressed layout. + // Do not infer packing from an absent strip table or from clipped sensor bytes. + if info.compression == 1 + && (info.offsets.is_empty() + || info.offsets.len() != info.byte_counts.len() + || info + .offsets + .iter() + .zip(&info.byte_counts) + .any(|(&at, &len)| len == 0 || at.checked_add(len).is_none_or(|end| end > bytes.len() as u64))) + { + return Err(RawError::Corrupt("ORF strip table is missing or outside file".into())); + } let chunks = info.chunks(bytes.len() as u64); let total = chunks.iter().fold(0u64, |a, c| a.saturating_add(c.len)); let stated = cfa_from_exif(&tiff); @@ -348,6 +361,7 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result Result Option<&[u8]> { /// A CR3's full-size JPEG track, when it holds a DCT JPEG. #[cfg(feature = "cr3")] fn cr3_full_jpeg(bytes: &[u8]) -> Option<&[u8]> { - let full = craft_metadata::cr3::parse_cr3(bytes) - .and_then(|c| c.tracks.iter().find(|t| t.kind == craft_metadata::cr3::Cr3TrackKind::Jpeg).and_then(|t| t.data)); - full.and_then(|(at, len)| bytes.get(at..at.checked_add(len)?)).filter(|j| is_dct_jpeg(j)) + let cr3 = craft_metadata::cr3::parse_cr3(bytes)?; + // An unusable preview track must not hide a later usable one. Share the marker budget + // across candidates so overlapping hostile track ranges cannot cause quadratic walks. + let mut work = CR3_MAX_WALK; + cr3.tracks.iter().filter(|t| t.kind == craft_metadata::cr3::Cr3TrackKind::Jpeg).find_map(|t| { + let (at, len) = t.data?; + let jpeg = bytes.get(at..at.checked_add(len)?)?; + first_frame_within(jpeg, &mut work).filter(|(m, _)| matches!(m, 0xc0..=0xc2)).map(|_| jpeg) + }) } /// Marker-walk budget of the CR3 fallback: candidates can overlap (each claiming the rest of the file) and diff --git a/crates/raw/src/preview/tests.rs b/crates/raw/src/preview/tests.rs index 19018d9b1..7f0833da9 100644 --- a/crates/raw/src/preview/tests.rs +++ b/crates/raw/src/preview/tests.rs @@ -592,6 +592,36 @@ fn cr3_prefers_the_full_size_jpeg_track() { assert_eq!(embedded_preview(&file).unwrap(), small); } +#[cfg(feature = "cr3")] +#[test] +fn cr3_skips_bad_preview_tracks_but_never_uses_sensor_tracks_as_jpeg() { + let bx = |kind: &[u8; 4], body: &[u8]| -> Vec { [&((body.len() + 8) as u32).to_be_bytes()[..], kind, body].concat() }; + let full = |kind: &[u8; 4], body: &[u8]| bx(kind, &[&[0u8; 4][..], body].concat()); + let track = |jpeg: bool, at: u64, len: u32| { + let mut entry = vec![0u8; 82]; + if jpeg { + entry.extend(bx(b"JPEG", &[0; 4])); + } + let stsd = full(b"stsd", &[&1u32.to_be_bytes()[..], &bx(b"CRAW", &entry)].concat()); + let stsz = full(b"stsz", &[len.to_be_bytes(), 1u32.to_be_bytes()].concat()); + let co64 = full(b"co64", &[&1u32.to_be_bytes()[..], &at.to_be_bytes()].concat()); + bx(b"trak", &bx(b"mdia", &bx(b"minf", &bx(b"stbl", &[stsd, stsz, co64].concat())))) + }; + let good = fake_jpeg(100); + for first in [track(true, u64::MAX, 32), track(true, 4096, 32)] { + let mut bytes = bx(b"ftyp", b"crx \0\0\0\x01crx isom"); + bytes.extend(bx(b"moov", &[first, track(true, 4128, good.len() as u32)].concat())); + bytes.resize(4128, 0); + bytes.extend(&good); + assert_eq!(embedded_preview(&bytes).as_deref(), Some(good.as_slice())); + } + let mut bytes = bx(b"ftyp", b"crx \0\0\0\x01crx isom"); + bytes.extend(bx(b"moov", &track(false, 4096, good.len() as u32))); + bytes.resize(4096, 0); + bytes.extend(&good); + assert_eq!(embedded_preview(&bytes), None, "a sensor sample is not a rendered preview even if its bytes look like JPEG"); +} + #[cfg(feature = "cr3")] /// A CR3 whose `PRVW` box holds `prvw` and whose JPEG track holds `big`. fn cr3_with(prvw_jpeg: &[u8], big: &[u8]) -> Vec { diff --git a/crates/raw/src/raf.rs b/crates/raw/src/raf.rs index f75fa160b..464c086f1 100644 --- a/crates/raw/src/raf.rs +++ b/crates/raw/src/raf.rs @@ -18,7 +18,10 @@ //! - CFA: the X-Trans layout record (36 bytes, 0 = R, 1 = G, 2 = B) is stored in reverse order and, reversed, //! is anchored at raw pixel (0, 0) — verified on X-Trans I and III samples by minimising the difference //! between horizontally / vertically adjacent "green" samples over all 72 orientations/phases, and by colour -//! renders. Bayer bodies (X-A series) use RGGB at (0, 0), verified by colour renders. +//! renders. Bayer FujiLayout (`0x0130`) gives the reversed 2×2 pattern in its low two bits +//! (0 = R, 1/3 = G, 2 = B); absent layout records retain the assumed RGGB fallback. +//! - RawExposureBias (`0x9650`) is a signed big-endian 16-bit fraction; its negative is the +//! baseline exposure used to restore the camera's dynamic-range exposure reduction. //! - Striped predictive compression is decoded by `rafc`; see `docs/raf-compression.md`. use crate::shared::white_from_data_sourced; @@ -87,7 +90,7 @@ fn pair(h: &Header, tag: u16) -> Option<(usize, usize)> { pub(crate) fn raw_ifd(raw: &[u8]) -> Result { let tiff = Tiff::parse(raw).map_err(|_| RawError::Unsupported("RAF without a raw IFD (older FinePix layout)".into()))?; let off = tiff.ifds.first().and_then(|i| i.u64(0xf000)).ok_or_else(|| RawError::Unsupported("RAF without a raw IFD".into()))?; - let (ifd, _) = craft_tiff::parse_ifd_at(raw, off, ByteOrder::Little, 0, false, &Default::default())?; + let (ifd, _) = craft_tiff::parse_ifd_at(raw, off, tiff.order, 0, false, &Default::default())?; Ok(ifd) } @@ -122,14 +125,34 @@ fn camera_name(b: &[u8]) -> String { String::from_utf8_lossy(name.get(..end).unwrap_or_default()).trim().to_string() } -/// The CFA: X-Trans from the (reversed) layout record, else RGGB. -fn cfa(h: &Header) -> Cfa { - match record(h, XTRANS_LAYOUT) { - Some(l) if l.len() == 36 && l.iter().all(|&c| c <= 2) => { - Cfa { width: 6, height: 6, pattern: l.iter().rev().copied().collect(), origin_x: 0, origin_y: 0 } +/// Layout records are reversed and anchored at sensor (0, 0). FujiLayout's low +/// two bits encode Bayer colour; both 1 and 3 denote green. +fn cfa(h: &Header) -> Result { + if let Some(l) = record(h, XTRANS_LAYOUT) { + if l.len() != 36 || l.iter().any(|&c| c > 2) { + return Err(RawError::Unsupported("Fujifilm RAF with an unknown X-Trans layout".into())); } - _ => Cfa::bayer_static("RGGB"), + return Ok(Cfa { width: 6, height: 6, pattern: l.iter().rev().copied().collect(), origin_x: 0, origin_y: 0 }); } + let Some(l) = record(h, 0x0130) else { return Ok(Cfa::bayer_static("RGGB")) }; + let cfa = Cfa { width: 2, height: 2, pattern: l.iter().rev().map(|&c| if c & 3 == 3 { 1 } else { c & 3 }).collect(), origin_x: 0, origin_y: 0 }; + if l.len() != 4 || !cfa.is_bayer() { + return Err(RawError::Unsupported("Fujifilm RAF with an unknown Bayer layout".into())); + } + Ok(cfa) +} + +/// Fuji RawExposureBias is a signed big-endian 16-bit fraction, undone like +/// DNG BaselineExposure. Invalid or implausible metadata does not alter exposure. +fn baseline_exposure(h: &Header) -> f64 { + record(h, 0x9650) + .and_then(|d| { + let num = i16::from_be_bytes(d.get(..2)?.try_into().ok()?); + let den = i16::from_be_bytes(d.get(2..4)?.try_into().ok()?); + (den != 0).then(|| -f64::from(num) / f64::from(den)) + }) + .filter(|v| v.is_finite() && v.abs() <= 8.0) + .unwrap_or(0.0) } /// Unpack one row of `bits`-bit samples in Fujifilm's packing. `words32`: the raw IFD's `0xf006` flag is set, the @@ -137,7 +160,7 @@ fn cfa(h: &Header) -> Cfa { /// LSB-first bit stream, or (other depths) the same 32-bit words. pub(crate) fn unpack_row(src: &[u8], bits: u32, words32: bool, out: &mut [u16]) { match bits { - 16 => out.iter_mut().zip(src.as_chunks::<2>().0).for_each(|(o, c)| *o = u16::from_le_bytes([c[0], c[1]])), + 16 if !words32 => out.iter_mut().zip(src.as_chunks::<2>().0).for_each(|(o, c)| *o = u16::from_le_bytes([c[0], c[1]])), 12 if !words32 => unpack_lsb(src, 12, out), _ => unpack_words32_msb(src, ByteOrder::Little, bits, out), } @@ -171,7 +194,7 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result false, @@ -190,9 +213,18 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result= (n as u64) * 2 { 16 } else { bits }; - let packed_row = w.checked_mul(store as usize).map(|n| n.div_ceil(8)).ok_or(RawError::Limit("RAF row too large"))?; + // An explicit packed-word flag wins over the size heuristic: padding in short + // packed rows can occupy as many bytes as unpacked 16-bit samples. + // Unflagged samples may use 16-bit words (e.g. 14-bit X-T20). + let store = if !words32 && len as u64 >= (n as u64) * 2 { 16 } else { bits }; + let row_bits = w.checked_mul(store as usize).ok_or(RawError::Limit("RAF row too large"))?; + // Word-packed rows store the high bits in the last little-endian word's final bytes. + // Reading only ceil(bits / 8) bytes would lose those samples, not just padding. + let packed_row = if words32 || (store != 16 && store != 12) { + row_bits.div_ceil(32).checked_mul(4).ok_or(RawError::Limit("RAF row too large"))? + } else { + row_bits.div_ceil(8) + }; let stride = match get(0xf005)?.unwrap_or(0) { 0 => packed_row, words => words.checked_mul(4).ok_or(RawError::Limit("RAF stride too large"))?.max(packed_row), @@ -206,7 +238,11 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result Result Result = (0..w * h).map(|i| ((i * 977 + 101) % (1 << bits)) as u16).collect(); + // Independently place each sample bit into whole little-endian 32-bit row words. + let strip: Vec = px + .chunks(w as usize) + .flat_map(|row| { + let mut words = vec![0u32; (row.len() * bits as usize).div_ceil(32)]; + for (i, &sample) in row.iter().enumerate() { + for bit in 0..bits as usize { + let at = i * bits as usize + bit; + words[at / 32] |= u32::from((sample >> (bits as usize - 1 - bit)) & 1) << (31 - at % 32); + } + } + words.into_iter().flat_map(u32::to_le_bytes).collect::>() + }) + .collect(); + let bytes = raf_flagged(w, h, bits, strip, None, &[], flag); + assert_eq!(crate::decode(&bytes).unwrap().data, RawData::U16(px)); + assert!(crate::decode(&bytes[..bytes.len() - 1]).is_err()); + } + } + + #[test] + fn big_endian_words_and_bayer_layout_preserve_exact_sensor_values() { + use craft_tiff::{IfdBuilder, TiffWriter, Value}; + let (w, h) = (6u32, 4u32); + let px: Vec = (0..w * h).map(|i| (i * 977 + 123) as u16).collect(); + for order in [ByteOrder::Little, ByteOrder::Big] { + let build = |at| { + let mut sensor = IfdBuilder::new(); + for (tag, value) in [(0xf001, w), (0xf002, h), (0xf003, 16), (0xf007, at), (0xf008, w * h * 2)] { + sensor.set(tag, Value::Long(vec![value])); + } + let mut root = IfdBuilder::new(); + root.set_child(0xf000, sensor); + TiffWriter::new(order, false).write(&[root]).unwrap() + }; + let mut raw = build(build(0).len() as u32); + raw.extend(px.iter().flat_map(|v| if order == ByteOrder::Big { v.to_be_bytes() } else { v.to_le_bytes() })); + let mut bytes = raf(w, h, 16, vec![0; (w * h * 2) as usize], None, &[]); + let raw_at = be32(&bytes, 100).unwrap(); + bytes.truncate(raw_at); + bytes[104..108].copy_from_slice(&(raw.len() as u32).to_be_bytes()); + bytes.extend(raw); + // A four-site Bayer record is reversed in storage, just like the X-Trans record. + let dir_at = be32(&bytes, 92).unwrap(); + let mut records = bytes[dir_at..raw_at].to_vec(); + let n = u32::from_be_bytes(records[..4].try_into().unwrap()) + 1; + records[..4].copy_from_slice(&n.to_be_bytes()); + records.extend_from_slice(&[0x01, 0x30, 0, 4, 0x08, 0x09, 0x0b, 0x0a]); + bytes.splice(dir_at..raw_at, records.clone()); + bytes[96..100].copy_from_slice(&(records.len() as u32).to_be_bytes()); + bytes[100..104].copy_from_slice(&((raw_at + 8) as u32).to_be_bytes()); + let image = crate::decode(&bytes).unwrap(); + assert_eq!(image.data, RawData::U16(px.clone())); + assert_eq!(image.cfa.unwrap().name(), "BGGR"); + } + } + + #[test] + fn raw_exposure_bias_is_preserved_as_baseline_exposure_and_invalid_values_are_ignored() { + for (num, den, expected) in [(-72i16, 100i16, 0.72f64), (1, 0, 0.0), (32767, 1, 0.0)] { + let mut bytes = raf(6, 4, 16, pack(&[1000; 24], 16), None, &[]); + let (dir_at, raw_at) = (be32(&bytes, 92).unwrap(), be32(&bytes, 100).unwrap()); + let mut records = bytes[dir_at..raw_at].to_vec(); + let n = u32::from_be_bytes(records[..4].try_into().unwrap()) + 1; + records[..4].copy_from_slice(&n.to_be_bytes()); + records.extend_from_slice(&[0x96, 0x50, 0, 4]); + records.extend(num.to_be_bytes()); + records.extend(den.to_be_bytes()); + bytes.splice(dir_at..raw_at, records.clone()); + bytes[96..100].copy_from_slice(&(records.len() as u32).to_be_bytes()); + bytes[100..104].copy_from_slice(&((raw_at + 8) as u32).to_be_bytes()); + for mode in [Mode::Header, Mode::Full] { + assert!((decode(&bytes, mode, &Limits::default()).unwrap().color.baseline_exposure - expected).abs() < 1e-9); + } + } + } + + #[test] + fn invalid_declared_cfa_layouts_are_not_silently_replaced() { + for (tag, data) in [(0x0130, vec![8, 8, 10, 10]), (0x0131, vec![3; 36])] { + let header = Header { jpeg: None, records: vec![(tag, data.as_slice())], raw: None }; + assert!(matches!(cfa(&header), Err(RawError::Unsupported(_)))); + } + } + /// Raw IFD flag `0xf006` selects the 32-bit-word packing at 12 bits; without it 12 bits are an LSB-first stream. #[test] fn flag_selects_word_packed_12_bit() { diff --git a/crates/raw/src/rw2.rs b/crates/raw/src/rw2.rs index 9984ed987..12a643de1 100644 --- a/crates/raw/src/rw2.rs +++ b/crates/raw/src/rw2.rs @@ -671,6 +671,7 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result Result read_image(data, info, order, packing, limits), crate::Mode::Header => { check_image(data, info, limits)?; + check_uncompressed_chunks(data, info, &info.chunks(data.len() as u64), packing)?; // a lossless-JPEG layout the decoder rejects (subsampled components, e.g. Sony's // lossless M/S sizes) must fail here too, as the full decode will; likewise a JPEG XL // chunk whose headers don't match the IFD @@ -151,6 +152,7 @@ pub(crate) fn read_image(data: &[u8], info: &ImageInfo, order: ByteOrder, packin let bits = info.bits() as u32; let float = info.sample_format == 3; let chunks = info.chunks(data.len() as u64); + check_uncompressed_chunks(data, info, &chunks, packing)?; let planar = info.planar == 2 && cpp > 1; let ccpp = if planar { 1 } else { cpp }; // The chunks are decoded and copied into the output in groups whose combined nominal size stays within @@ -222,6 +224,26 @@ pub(crate) fn read_image(data: &[u8], info: &ImageInfo, order: ByteOrder, packin Ok(if float { RawData::F32(out_f32) } else { RawData::U16(out_u16) }) } +/// Framing precedes allocation even in header-only probes: missing source rows may not +/// be reported as a decodable image and later silently filled with zero samples. +fn check_uncompressed_chunks(data: &[u8], info: &ImageInfo, chunks: &[Chunk], packing: Packing) -> Result<()> { + if info.compression != comp::NONE { + return Ok(()); + } + let cpp = if info.planar == 2 { 1 } else { info.samples_per_pixel as usize }; + for chunk in chunks { + let src = chunk_bytes(data, chunk).ok_or_else(|| RawError::Corrupt("uncompressed chunk is outside the file".into()))?; + let (cw, _, vw, vh) = chunk_dims(info, chunk); + let row = packed_row_bytes(cw as u64 * cpp as u64, info.bits() as u32, packing, info.sample_format == 3); + let last = packed_row_bytes(vw as u64 * cpp as u64, info.bits() as u32, packing, info.sample_format == 3); + let need = row.saturating_mul(vh.saturating_sub(1) as u64).saturating_add(last); + if (src.len() as u64) < need { + return Err(RawError::Corrupt(format!("an uncompressed chunk has {} bytes, expected at least {need}", src.len()))); + } + } + Ok(()) +} + /// Bytes that `n` samples take in a row of an uncompressed or inflated chunk, laid out as /// [`unpack_chunk`] reads them: whole bytes or 16-bit words, or 32-bit words for `Words32Msb`. fn packed_row_bytes(n: u64, bits: u32, packing: Packing, float: bool) -> u64 { @@ -285,10 +307,12 @@ fn decode_chunk( } match info.compression { comp::NONE => { - // uncompressed: missing rows are filled with zeros, but a chunk holding a small fraction of what its - // samples need is no picture: refused before the nominal chunk is allocated (storytold/lightcraft#1512) - let need = (n as u64).saturating_mul(u64::from(bits)) / 8; - if (src.len() as u64).saturating_mul(64) < need { + // Require every visible sample. Nominal edge-tile padding may be absent, but + // missing image rows must not turn a truncated file into a zero-filled success. + let row_bytes = packed_row_bytes(cw as u64 * cpp as u64, bits, packing, float); + let last_row = packed_row_bytes(vw as u64 * cpp as u64, bits, packing, float); + let need = row_bytes.saturating_mul(vh.saturating_sub(1) as u64).saturating_add(last_row); + if (src.len() as u64) < need { return Err(RawError::Corrupt(format!("an uncompressed chunk of {n} samples has only {} bytes", src.len()))); } unpack_chunk(src, order, packing, cw, ch, cpp, bits, float, 1) @@ -314,13 +338,7 @@ fn decode_chunk( let mcu = |v: usize| v.div_ceil(32).saturating_mul(32); let tile_budget = mcu(cw).saturating_mul(mcu(ch)).saturating_mul(if cpp == 1 { 1 } else { 3 }).clamp(1, limits.max_lossy_tile_samples.max(1)); - let (px, jw, jh, jc) = lossy_jpeg(src, cpp, tile_budget)?; - // An edge tile's JPEG may be smaller than the nominal tile, but it has to cover the part of the tile - // inside the image: one that doesn't would leave a black region in a picture reported as decoded - // (PhotoCraft's and FilmCraft's readers refuse a JPEG smaller than its tile; this keeps the edge case). - if jw < vw || jh < vh { - return Err(RawError::Corrupt(format!("a lossy JPEG of {jw} × {jh} does not cover the {vw} × {vh} of its tile inside the image"))); - } + let (px, jw, jh, jc) = lossy_jpeg_with_tile(src, cpp, tile_budget, Some((cw, ch, vw, vh)))?; // the buffer holds the rows the decoded JPEG covers (the caller zero-fills the tile's // remainder and reads only rows it fills): a tile-many-larger decoded JPEG does not // get the nominal tile's full buffer (storytold/lightcraft#1517) @@ -408,9 +426,21 @@ const MAX_PADDING_FACTOR: usize = 32; #[cfg(feature = "dng")] const MAX_WRITER_TILE_SAMPLES: usize = 2048 * 2048; -#[cfg(feature = "dng")] +#[cfg(all(test, feature = "dng"))] /// Decode one baseline JPEG chunk to 8-bit samples: (samples, width, height, channels). pub(crate) fn lossy_jpeg(src: &[u8], cpp: usize, max_samples: usize) -> Result<(Vec, usize, usize, usize)> { + lossy_jpeg_with_tile(src, cpp, max_samples, None) +} + +/// Validate the actual frame before decode allocates its output. Edge tiles may store +/// a smaller frame covering all visible samples or bounded whole-MCU padding. +#[cfg(feature = "dng")] +fn lossy_jpeg_with_tile( + src: &[u8], + cpp: usize, + max_samples: usize, + tile: Option<(usize, usize, usize, usize)>, +) -> Result<(Vec, usize, usize, usize)> { use zune_core::bytestream::ZCursor; use zune_core::colorspace::ColorSpace; use zune_core::options::DecoderOptions; @@ -422,7 +452,27 @@ pub(crate) fn lossy_jpeg(src: &[u8], cpp: usize, max_samples: usize) -> Result<( d.decode_headers().map_err(|e| RawError::Corrupt(format!("lossy JPEG tile: {e}")))?; let info = d.info().ok_or_else(|| RawError::Corrupt("lossy JPEG tile without a header".into()))?; let (w, h) = (info.width as usize, info.height as usize); - let c = if cpp == 1 { 1 } else { 3 }; + if !matches!(cpp, 1 | 3) { + return Err(RawError::Unsupported(format!("lossy JPEG with {cpp} samples per pixel"))); + } + if usize::from(info.components) != cpp { + return Err(RawError::Corrupt(format!("lossy JPEG has {} components, expected {cpp}", info.components))); + } + if let Some((cw, ch, vw, vh)) = tile { + if w < vw || h < vh { + return Err(RawError::Corrupt(format!("a lossy JPEG of {w} x {h} does not cover the {vw} x {vh} of its tile inside the image"))); + } + // Writers can store a whole MCU around an edge tile. Keep the documented + // 32-pixel padding allowance without letting one tiny tile decode an arbitrary frame. + let mcu_extent = |value: usize| value.checked_add(31).map(|rounded| rounded / 32).and_then(|blocks| blocks.checked_mul(32)); + let (Some(max_w), Some(max_h)) = (mcu_extent(cw), mcu_extent(ch)) else { + return Err(RawError::Corrupt("lossy JPEG tile dimensions overflow MCU padding".into())); + }; + if w > max_w || h > max_h { + return Err(RawError::Corrupt(format!("a lossy JPEG of {w} x {h} exceeds its {max_w} x {max_h} MCU-padded tile dimensions"))); + } + } + let c = cpp; let out = w as u64 * h as u64 * c as u64; if w == 0 || h == 0 || out > max_samples as u64 { return Err(RawError::Corrupt(format!("a lossy JPEG of {w} × {h} × {c} would exceed the {max_samples} output samples allowed"))); @@ -519,6 +569,12 @@ fn unpack_chunk( } } else { undo_diff_u16(row, cpp * factor); + // Horizontal differences wrap in the declared sample precision, not in + // the u16 container used by the unpacker (TIFF 6.0 Predictor 2). + if bits < 16 { + let mask = ((1u32 << bits) - 1) as u16; + row.iter_mut().for_each(|sample| *sample &= mask); + } } } } @@ -982,3 +1038,6 @@ mod budget_tests { assert_eq!(decode_groups(&big.chunks(64), 1, 1 << 20), vec![0..1]); } } + +#[cfg(all(test, feature = "dng"))] +mod compat_review_tests; diff --git a/crates/raw/src/tiffraw/compat_review_tests.rs b/crates/raw/src/tiffraw/compat_review_tests.rs new file mode 100644 index 000000000..ae5da8f2f --- /dev/null +++ b/crates/raw/src/tiffraw/compat_review_tests.rs @@ -0,0 +1,115 @@ +use super::*; +use craft_tiff::image::Layout; + +fn image(width: u32, height: u32, bits: u16, cpp: u16, compression: u16, len: usize) -> ImageInfo { + ImageInfo { + width, + height, + bits_per_sample: vec![bits; cpp as usize], + samples_per_pixel: cpp, + compression, + photometric: 34892, + planar: 1, + predictor: 1, + sample_format: 1, + new_subfile_type: 0, + layout: Layout::Strips { rows_per_strip: height }, + offsets: vec![0], + byte_counts: vec![len as u64], + } +} + +#[test] +fn truncated_uncompressed_rows_are_rejected_in_full_and_header_modes() { + for (bits, packing, float, bytes) in [ + (8, Packing::Msb, false, 8), + (12, Packing::Msb, false, 12), + (12, Packing::Word16, false, 16), + (16, Packing::Msb, false, 16), + (32, Packing::Msb, true, 32), + ] { + let data = vec![0u8; bytes]; + let mut info = image(4, 2, bits, 1, comp::NONE, bytes); + info.sample_format = if float { 3 } else { 1 }; + assert!(read_image_in(crate::Mode::Full, &data, &info, ByteOrder::Little, packing, &Limits::default()).is_ok()); + info.byte_counts[0] -= 1; + for mode in [crate::Mode::Full, crate::Mode::Header] { + assert!( + matches!(read_image_in(mode, &data[..bytes - 1], &info, ByteOrder::Little, packing, &Limits::default()), Err(RawError::Corrupt(message)) if message.contains("uncompressed chunk")), + "{bits}/{float}/{mode:?}" + ); + } + } +} + +#[test] +fn edge_padding_may_be_absent_but_visible_uncompressed_samples_must_all_exist() { + let data = vec![31u8; 16 * 7 + 8]; + let mut info = image(8, 8, 8, 1, comp::NONE, data.len()); + info.layout = Layout::Tiles { tile_width: 16, tile_height: 16 }; + let RawData::U16(values) = read_image(&data, &info, ByteOrder::Little, Packing::Msb, &Limits::default()).unwrap() else { panic!("integer") }; + assert_eq!(values, vec![31; 64]); + info.byte_counts[0] -= 1; + assert!(read_image(&data[..data.len() - 1], &info, ByteOrder::Little, Packing::Msb, &Limits::default()).is_err()); +} + +#[test] +fn packed_predictor_two_wraps_at_declared_twelve_bits() { + // Samples [250, 10, 4090, 20]; differences modulo4096 are 0FA,F10,FF0,01A. + let packed = [0x0f, 0xaf, 0x10, 0xff, 0x00, 0x1a]; + let ChunkPx::U16(values) = unpack_chunk(&packed, ByteOrder::Little, Packing::Msb, 4, 1, 1, 12, false, 2).unwrap() else { panic!("integer") }; + assert_eq!(values, vec![250, 10, 4090, 20]); +} + +#[allow(clippy::unwrap_used)] // Test-only fixture encoder; failed construction must fail the test. +fn jpeg(cpp: usize, width: u16, height: u16) -> Vec { + let mut data = Vec::new(); + let color = if cpp == 1 { jpeg_encoder::ColorType::Luma } else { jpeg_encoder::ColorType::Rgb }; + jpeg_encoder::Encoder::new(&mut data, 100).encode(&vec![90; width as usize * height as usize * cpp], width, height, color).unwrap(); + data +} + +#[test] +fn baseline_jpeg_components_are_checked_before_forced_colorspace_conversion() { + for (actual, expected) in [(3, 1), (1, 3)] { + let data = jpeg(actual, 8, 8); + let error = lossy_jpeg(&data, expected, 1024).unwrap_err(); + assert!(matches!(error, RawError::Corrupt(message) if message.contains(&format!("{actual} components")))); + } + assert!(matches!(lossy_jpeg(&jpeg(3, 8, 8), 4, 1024), Err(RawError::Unsupported(message)) if message.contains("4 samples"))); + for cpp in [1, 3] { + assert_eq!(lossy_jpeg(&jpeg(cpp, 8, 8), cpp, 1024).unwrap().3, cpp); + } +} + +#[test] +fn jpeg_tile_geometry_and_limits_are_checked_at_the_header_with_valid_edge_controls() { + let data = jpeg(3, 8, 8); + assert!(lossy_jpeg_with_tile(&data, 3, 1024, Some((16, 16, 8, 8))).is_ok()); + assert!( + matches!(lossy_jpeg_with_tile(&data, 3, 1024, Some((16, 16, 9, 8))), Err(RawError::Corrupt(message)) if message.contains("does not cover")) + ); + assert!(matches!(lossy_jpeg_with_tile(&data, 3, 32, Some((16, 16, 8, 8))), Err(RawError::Corrupt(message)) if message.contains("would exceed"))); + // Mutate only SOF dimensions, retaining a valid header but no entropy for the claimed frame. + // The geometry error proves refusal precedes decode's short/corrupt entropy failure. + for (width, height) in [(33u16, 8u16), (8, 33), (256, 256)] { + let mut oversized = data.clone(); + let sof = oversized.windows(2).position(|bytes| bytes == [0xff, 0xc0]).unwrap(); + oversized[sof + 5..sof + 7].copy_from_slice(&height.to_be_bytes()); + oversized[sof + 7..sof + 9].copy_from_slice(&width.to_be_bytes()); + assert!( + matches!(lossy_jpeg_with_tile(&oversized, 3, 1 << 20, Some((16, 16, 8, 8))), Err(RawError::Corrupt(message)) if message.contains("exceeds its")) + ); + } + // A genuine full MCU remains valid even when only a 4x4 part of the nominal16 tile is visible. + let padded = jpeg(3, 32, 32); + assert!(lossy_jpeg_with_tile(&padded, 3, 32 * 32 * 3, Some((16, 16, 4, 4))).is_ok()); + assert!( + matches!(lossy_jpeg_with_tile(&data, 3, 1 << 20, Some((usize::MAX, 16, 8, 8))), Err(RawError::Corrupt(message)) if message.contains("overflow MCU padding")) + ); + let mut info = image(8, 8, 8, 3, comp::LOSSY_JPEG, data.len()); + info.layout = Layout::Tiles { tile_width: 16, tile_height: 16 }; + let RawData::U16(values) = read_image(&data, &info, ByteOrder::Little, Packing::Msb, &Limits::default()).unwrap() else { panic!("integer") }; + assert_eq!(values.len(), 8 * 8 * 3); + assert!(values.iter().all(|sample| (i32::from(*sample) - 90).abs() <= 3)); +} diff --git a/crates/raw/tests/it/dng.rs b/crates/raw/tests/it/dng.rs index 3b7f4bc0a..216937862 100644 --- a/crates/raw/tests/it/dng.rs +++ b/crates/raw/tests/it/dng.rs @@ -64,6 +64,7 @@ fn synthetic(w: usize, h: usize, cfa: Option, cpp: usize) -> RawImage { cinema: Default::default(), lens_metadata: Default::default(), level_sources: Default::default(), + notices: craft_raw::RawNotices::default(), } } @@ -948,18 +949,31 @@ fn malformed_semantic_masks_are_skipped() { let mut zero = mask_ifd(2, 2, "zero size", vec![0; 4]); zero.set(t::IMAGE_WIDTH, Value::Long(vec![0])); ifd0.add_sub_ifd(zero); + // A 4x4 8-bit mask needs 16 visible samples; three bytes must not be + // accepted as a synthetic mask with thirteen zero-filled samples. let mut short = mask_ifd(4, 4, "short strip", vec![200; 3]); short.set(t::MASK_SUB_AREA, Value::Long(vec![u32::MAX, u32::MAX, u32::MAX, u32::MAX])); ifd0.add_sub_ifd(short); + let mut valid = mask_ifd(4, 2, "valid crop", vec![10, 20, 30, 40, 50, 60, 70, 255]); + valid.set(t::MASK_SUB_AREA, Value::Long(vec![1, 2, 8, 4])); + ifd0.add_sub_ifd(valid); let mut outside = mask_ifd(2, 2, "sub area too small", vec![255; 4]); outside.set(t::MASK_SUB_AREA, Value::Long(vec![3, 0, 2, 4])); ifd0.add_sub_ifd(outside); }); - assert_eq!(decode(&bytes).unwrap().cpp, 3); + let raw = decode(&bytes).unwrap(); + let baseline = decode(&profile_dng(ByteOrder::Little, |_| {})).unwrap(); + assert_eq!((raw.width, raw.height, raw.cpp), (8, 6, 3)); + assert_eq!(raw.data, baseline.data, "optional damaged masks must not change base image samples"); let masks = craft_raw::semantic_masks(&bytes); let names: Vec<&str> = masks.iter().map(|m| m.name.as_str()).collect(); - assert_eq!(names, ["short strip", "sub area too small"]); - assert!(masks.iter().all(|m| m.sub_area.is_none()), "sub areas the mask doesn't fit are ignored"); + assert_eq!(names, ["valid crop", "sub area too small"], "the truncated mask is omitted, and later usable masks survive"); + assert_eq!(masks[0].data, [10u16, 20, 30, 40, 50, 60, 70, 255].map(|sample| sample * 257)); + assert_eq!(masks[0].sub_area, Some(craft_raw::semantic::MaskSubArea { top: 1, left: 2, full_width: 8, full_height: 4 })); + assert!((masks[0].value_at(2.5, 1.5) - 10. / 255.).abs() < 1e-4); + assert_eq!(masks[0].value_at(1.5, 1.5), 0.); + assert!(masks[1].sub_area.is_none(), "the complete mask survives while a sub area it doesn't fit is ignored"); + assert_eq!(masks[1].data, vec![65535; 4]); for m in &masks { assert_eq!(m.data.len(), m.width * m.height); } diff --git a/crates/raw/tests/it/robust.rs b/crates/raw/tests/it/robust.rs index b2adb26e0..5ad1d1b32 100644 --- a/crates/raw/tests/it/robust.rs +++ b/crates/raw/tests/it/robust.rs @@ -28,6 +28,7 @@ fn sample(comp: DngCompression) -> Vec { cinema: Default::default(), lens_metadata: Default::default(), level_sources: Default::default(), + notices: craft_raw::RawNotices::default(), }; craft_raw::write_dng(&raw, &DngWriteOptions { compression: comp, ..Default::default() }).unwrap() }