From 60f7c5d74f6e5dfda2fa5779cff0b57d8fd45263 Mon Sep 17 00:00:00 2001 From: techneissa <55485515+techneissa@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:53:35 +0300 Subject: [PATCH 1/3] raw: read a DNG strip whose byte count is only ActiveArea wide in full (#2040) An uncompressed single-sample integer DNG strip whose StripByteCounts is exactly ActiveArea columns (from column 0) by ImageLength rows, while the file holds the whole ImageWidth by ImageLength raster from the strip's offset, is read at its declared size instead of refused. Every other shortfall stays Corrupt. --- crates/raw/src/dng.rs | 37 +++++++++++++- crates/raw/src/dng/compat_review_tests.rs | 59 +++++++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/crates/raw/src/dng.rs b/crates/raw/src/dng.rs index efa1ec4f5..a7be5d67b 100644 --- a/crates/raw/src/dng.rs +++ b/crates/raw/src/dng.rs @@ -8,6 +8,7 @@ use crate::{ BlackLevel, Camera, Cfa, CinemaTags, ColorData, LevelSource, LevelSources, Limits, Matrix3, Mode, Orientation, RawData, RawError, RawFormat, RawImage, RawNotices, Rect, Result, Timecode, opcodes, }; +use craft_tiff::image::{ImageInfo, Layout}; use craft_tiff::tags::{self as t, photometric}; use craft_tiff::{ByteOrder, Ifd, Tiff, Value}; @@ -273,7 +274,10 @@ fn decode_ifd(bytes: &[u8], tiff: &Tiff, pixels: &Ifd, main: &Ifd, mode: Mode, f { return Err(RawError::Unsupported(format!("DNG compression {c} (GoPro VC-5) is not decoded yet"))); } - let info = pixels.image()?; + let mut info = pixels.image()?; + if std::ptr::eq(pixels, raw) { + extend_understated_strip(bytes, &mut info, raw); + } let (w, h, cpp) = (info.width as usize, info.height as usize, info.samples_per_pixel as usize); // one sample (CFA or monochrome), RGB or four colours; two are neither and can't be developed (storytold/lightcraft#1511) if !matches!(cpp, 1 | 3 | 4) { @@ -522,6 +526,37 @@ fn cinema_tags(ifd0: &Ifd, raw: &Ifd) -> CinemaTags { } } +/// Trust the file over `StripByteCounts` for an uncompressed single-strip image whose byte count is exactly +/// `ActiveArea` columns by `ImageLength` rows while the file holds the whole `ImageWidth` by `ImageLength` raster from +/// the strip's offset on. DJI Mavic 3 DNGs write the count of the active columns (5280) for a strip of full 5376-wide +/// rows that ends at the end of the file, so reading only the counted bytes refuses a complete image +/// (storytold/craft-libs#2040). The strip is lengthened only then, so every other short strip is still refused, and +/// it never reaches past the end of the file. The active area has to start at column 0, so it is the stored rows' +/// leading columns. +fn extend_understated_strip(bytes: &[u8], info: &mut ImageInfo, raw: &Ifd) { + let bits = u64::from(info.bits()); + if !matches!(info.layout, Layout::Strips { .. }) + || info.compression != t::compression::NONE + || info.samples_per_pixel != 1 + || info.planar == 2 + || info.sample_format == 3 + || !matches!(bits, 8 | 16) + { + return; + } + let ([offset], [count]) = (&info.offsets[..], &info.byte_counts[..]) else { return }; + let Some([top, left, bottom, right]) = raw.u64s(t::ACTIVE_AREA).and_then(|a| <[u64; 4]>::try_from(a).ok()) else { return }; + if left != 0 || bottom <= top || right == 0 || right >= u64::from(info.width) { + return; + } + let row_bytes = |width: u64| width.checked_mul(u64::from(info.height))?.checked_mul(bits / 8); + let (Some(counted), Some(needed)) = (row_bytes(right), row_bytes(u64::from(info.width))) else { return }; + let fits = offset.checked_add(needed).is_some_and(|end| end <= bytes.len() as u64); + if *count == counted && fits { + info.byte_counts = vec![needed]; + } +} + /// 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 diff --git a/crates/raw/src/dng/compat_review_tests.rs b/crates/raw/src/dng/compat_review_tests.rs index 48300cbf7..309d1e5b7 100644 --- a/crates/raw/src/dng/compat_review_tests.rs +++ b/crates/raw/src/dng/compat_review_tests.rs @@ -118,3 +118,62 @@ fn decoded_notices_record_actual_crop_gain_table_and_cfa_decisions() { let limits = Limits { max_samples: 47, ..Default::default() }; assert!(matches!(decode_as(&linear(&[]), Mode::Full, RawFormat::Dng, &limits), Err(RawError::Limit(_)))); } + +/// A 16-bit CFA DNG of `width` x `height` whose strip holds every row in full but whose `StripByteCounts` says +/// `counted` bytes. +fn understated_strip_dng(width: u32, height: u32, counted: u32, active_area: Option<[u32; 4]>) -> Vec { + let mut raw = IfdBuilder::new(); + for (tag, value) in [ + (t::IMAGE_WIDTH, Value::Long(vec![width])), + (t::IMAGE_LENGTH, Value::Long(vec![height])), + (t::BITS_PER_SAMPLE, Value::Short(vec![16])), + (t::SAMPLES_PER_PIXEL, Value::Short(vec![1])), + (t::PHOTOMETRIC, Value::Short(vec![photometric::CFA])), + (t::COMPRESSION, Value::Short(vec![1])), + (t::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![2, 2])), + (t::CFA_PATTERN_EP, Value::Byte(vec![0, 1, 1, 2])), + ] { + raw.set(tag, value); + } + if let Some(area) = active_area { + raw.set(t::ACTIVE_AREA, Value::Long(area.to_vec())); + } + let samples = (width * height) as u16; + raw.set_image(ImageData::Strips { rows_per_strip: height, strips: vec![(0..samples).flat_map(u16::to_le_bytes).collect()] }); + let mut bytes = TiffWriter::new(ByteOrder::Little, false).write(&[raw]).unwrap(); + // the writer counts the real bytes: understate that one count, a LONG in the IFD + let count = (width * height * 2).to_le_bytes(); + let at = bytes.windows(4).position(|w| w == count).unwrap(); + bytes[at..at + 4].copy_from_slice(&counted.to_le_bytes()); + bytes +} + +/// storytold/craft-libs#2040: the DJI Mavic 3 counts 5280 x 3956 x 2 bytes for a strip that holds 5376-wide rows +/// to the end of the file. +#[test] +fn a_strip_counted_only_active_area_wide_is_read_in_full() { + let bytes = understated_strip_dng(8, 4, 6 * 4 * 2, Some([0, 0, 4, 6])); + for mode in [Mode::Full, Mode::Header] { + let img = decode_as(&bytes, mode, RawFormat::Dng, &Limits::default()).unwrap(); + assert_eq!((img.width, img.height), (8, 4)); + } + let img = decode_as(&bytes, Mode::Full, RawFormat::Dng, &Limits::default()).unwrap(); + let RawData::U16(v) = &img.data else { panic!("integer samples") }; + assert_eq!(v, &(0..32).collect::>()); +} + +/// Any other short strip is still a corrupt file. +#[test] +fn other_short_strips_are_still_refused() { + let refused = |bytes: Vec| matches!(decode_as(&bytes, Mode::Full, RawFormat::Dng, &Limits::default()), Err(RawError::Corrupt(_))); + // no ActiveArea to explain the count + assert!(refused(understated_strip_dng(8, 4, 6 * 4 * 2, None))); + // the count is not exactly the area's bytes + assert!(refused(understated_strip_dng(8, 4, 5 * 4 * 2, Some([0, 0, 4, 6])))); + // the area does not start at column 0 + assert!(refused(understated_strip_dng(8, 4, 6 * 4 * 2, Some([0, 2, 4, 8])))); + // the file does not hold the whole raster + let mut cut = understated_strip_dng(8, 4, 6 * 4 * 2, Some([0, 0, 4, 6])); + cut.truncate(cut.len() - 8); + assert!(refused(cut)); +} From 786c5571f05595276857e7faa36c8be2fe895a7d Mon Sep 17 00:00:00 2001 From: techneissa <55485515+techneissa@users.noreply.github.com> Date: Tue, 17 Nov 2026 18:39:50 +0300 Subject: [PATCH 2/3] raw: refuse an understated DNG strip whose ActiveArea reaches below the image (#2040) decode drops such an ActiveArea for the whole image, so nothing would explain the short byte count. --- crates/raw/src/dng.rs | 2 +- crates/raw/src/dng/compat_review_tests.rs | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/raw/src/dng.rs b/crates/raw/src/dng.rs index a7be5d67b..f62aeef47 100644 --- a/crates/raw/src/dng.rs +++ b/crates/raw/src/dng.rs @@ -546,7 +546,7 @@ fn extend_understated_strip(bytes: &[u8], info: &mut ImageInfo, raw: &Ifd) { } let ([offset], [count]) = (&info.offsets[..], &info.byte_counts[..]) else { return }; let Some([top, left, bottom, right]) = raw.u64s(t::ACTIVE_AREA).and_then(|a| <[u64; 4]>::try_from(a).ok()) else { return }; - if left != 0 || bottom <= top || right == 0 || right >= u64::from(info.width) { + if left != 0 || bottom <= top || bottom > u64::from(info.height) || right == 0 || right >= u64::from(info.width) { return; } let row_bytes = |width: u64| width.checked_mul(u64::from(info.height))?.checked_mul(bits / 8); diff --git a/crates/raw/src/dng/compat_review_tests.rs b/crates/raw/src/dng/compat_review_tests.rs index 309d1e5b7..149ee492d 100644 --- a/crates/raw/src/dng/compat_review_tests.rs +++ b/crates/raw/src/dng/compat_review_tests.rs @@ -170,6 +170,8 @@ fn other_short_strips_are_still_refused() { assert!(refused(understated_strip_dng(8, 4, 6 * 4 * 2, None))); // the count is not exactly the area's bytes assert!(refused(understated_strip_dng(8, 4, 5 * 4 * 2, Some([0, 0, 4, 6])))); + // the area reaches below the image, so decode would discard it + assert!(refused(understated_strip_dng(8, 4, 6 * 4 * 2, Some([4, 0, 5, 6])))); // the area does not start at column 0 assert!(refused(understated_strip_dng(8, 4, 6 * 4 * 2, Some([0, 2, 4, 8])))); // the file does not hold the whole raster From 71e271483380e211f38e7d49c0765c59cff6e785 Mon Sep 17 00:00:00 2001 From: techneissa <55485515+techneissa@users.noreply.github.com> Date: Tue, 17 Nov 2026 18:47:47 +0300 Subject: [PATCH 3/3] raw: lengthen an understated DNG strip only for integer samples (#2040) SampleFormat 4 and other undefined values no longer reach the extension; an absent tag still reads as 1 (unsigned), so a file without it is unchanged. --- crates/raw/src/dng.rs | 2 +- crates/raw/src/dng/compat_review_tests.rs | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/raw/src/dng.rs b/crates/raw/src/dng.rs index f62aeef47..9589f5d4d 100644 --- a/crates/raw/src/dng.rs +++ b/crates/raw/src/dng.rs @@ -539,7 +539,7 @@ fn extend_understated_strip(bytes: &[u8], info: &mut ImageInfo, raw: &Ifd) { || info.compression != t::compression::NONE || info.samples_per_pixel != 1 || info.planar == 2 - || info.sample_format == 3 + || !matches!(info.sample_format, 1 | 2) || !matches!(bits, 8 | 16) { return; diff --git a/crates/raw/src/dng/compat_review_tests.rs b/crates/raw/src/dng/compat_review_tests.rs index 149ee492d..7fcc1b4f4 100644 --- a/crates/raw/src/dng/compat_review_tests.rs +++ b/crates/raw/src/dng/compat_review_tests.rs @@ -122,7 +122,15 @@ fn decoded_notices_record_actual_crop_gain_table_and_cfa_decisions() { /// A 16-bit CFA DNG of `width` x `height` whose strip holds every row in full but whose `StripByteCounts` says /// `counted` bytes. fn understated_strip_dng(width: u32, height: u32, counted: u32, active_area: Option<[u32; 4]>) -> Vec { + understated_strip_dng_with(width, height, counted, active_area, &[]) +} + +/// [`understated_strip_dng`] with `extra` tags set on the raw IFD. +fn understated_strip_dng_with(width: u32, height: u32, counted: u32, active_area: Option<[u32; 4]>, extra: &[(u16, Value)]) -> Vec { let mut raw = IfdBuilder::new(); + for (tag, value) in extra { + raw.set(*tag, value.clone()); + } for (tag, value) in [ (t::IMAGE_WIDTH, Value::Long(vec![width])), (t::IMAGE_LENGTH, Value::Long(vec![height])), @@ -170,6 +178,8 @@ fn other_short_strips_are_still_refused() { assert!(refused(understated_strip_dng(8, 4, 6 * 4 * 2, None))); // the count is not exactly the area's bytes assert!(refused(understated_strip_dng(8, 4, 5 * 4 * 2, Some([0, 0, 4, 6])))); + // samples that are neither integer nor absent-format (SampleFormat 4 is undefined) + assert!(refused(understated_strip_dng_with(8, 4, 6 * 4 * 2, Some([0, 0, 4, 6]), &[(t::SAMPLE_FORMAT, Value::Short(vec![4]))]))); // the area reaches below the image, so decode would discard it assert!(refused(understated_strip_dng(8, 4, 6 * 4 * 2, Some([4, 0, 5, 6])))); // the area does not start at column 0