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.
This commit is contained in:
techneissa committed 2026-11-17 16:53:35 +03:00
1 parent 64fcd9531a
commit 60f7c5d74f
2 files changed
+95 -1

No files matched your search

+36 -1
View File
@@ -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
+59
View File
@@ -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<u8> {
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::<Vec<u16>>());
}
/// Any other short strip is still a corrupt file.
#[test]
fn other_short_strips_are_still_refused() {
let refused = |bytes: Vec<u8>| 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));
}