raw: share bounded DNG strip and tile decoding

This commit is contained in:
Dylan M. Taylor
2026-11-15 16:44:19 +00:00
parent bdaff3e2ab
commit 863d9dc22b
4 changed files with 436 additions and 0 deletions
Generated
+1
View File
@@ -2553,6 +2553,7 @@ dependencies = [
"craft-crw",
"craft-metadata",
"craft-tiff",
"image",
"jpeg-encoder 0.7.1",
"jxl-oxide",
"miniz_oxide 0.9.1",
+5
View File
@@ -11,6 +11,10 @@ repository.workspace = true
workspace = true
[features]
# Bounded DNG strip/tile reader, with FilmCraft-compatible baseline JPEG decoding.
line-dng = ["dep:image"]
# Decode DNG tile bands in parallel.
line-dng-threads = ["line-dng"]
default = ["dng", "jxl", "crw", "hasselblad", "mrw", "erf", "mef", "kodak", "pef", "srw", "cr2", "nef", "rw2", "raf", "orf", "arw", "cr3"]
## Adobe DNG, reading and writing: uncompressed, lossless JPEG, lossy JPEG, Deflate.
dng = ["dep:miniz_oxide", "dep:zune-jpeg", "dep:zune-core"]
@@ -48,6 +52,7 @@ cr3 = ["dep:craft-metadata"]
crw = ["dep:craft-crw", "dep:chrono"]
[dependencies]
image = { version = "0.25", default-features = false, features = ["jpeg"], optional = true }
craft-tiff = { workspace = true }
craft-crw = { workspace = true, optional = true }
# The CR3 container (ISO base media boxes, CMT1-4, tracks, CMP1 / CDI1): the one CR3 parser.
+427
View File
@@ -0,0 +1,427 @@
//! The raw image data of a DNG: finding the main raw IFD, and reading its strips or tiles
//! (uncompressed or lossless JPEG) into one array of 16-bit samples.
#[cfg(feature = "line-dng-threads")]
use rayon::prelude::*;
use crate::ljpeg_lines as ljpeg;
use craft_tiff::classic::{Ifd, Tiff, tags};
/// Why DNG raw layout or sample data could not be read.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum Error {
/// The file is not classic TIFF.
#[error("not a TIFF")]
NotTiff,
/// Required bytes are missing.
#[error("truncated raw data")]
Truncated,
/// A limit on samples or allocation was exceeded.
#[error("raw image too large")]
TooLarge,
/// Invalid structure.
#[error("{0}")]
Malformed(&'static str),
/// A valid unsupported variation.
#[error("{0}")]
Unsupported(String),
}
/// Result of the bounded raw reader.
pub type Result<T> = std::result::Result<T, Error>;
impl From<craft_tiff::classic::Error> for Error {
fn from(e: craft_tiff::classic::Error) -> Self {
match e {
craft_tiff::classic::Error::NotTiff => Self::NotTiff,
craft_tiff::classic::Error::Malformed(s) => Self::Malformed(s),
}
}
}
impl From<crate::ljpeg_lines::Error> for Error {
fn from(e: crate::ljpeg_lines::Error) -> Self {
match e {
crate::ljpeg_lines::Error::Truncated => Self::Truncated,
crate::ljpeg_lines::Error::TooLarge => Self::TooLarge,
crate::ljpeg_lines::Error::Malformed(s) => Self::Malformed(s),
crate::ljpeg_lines::Error::Unsupported(s) => Self::Unsupported(s),
}
}
}
/// Largest raw image decoded, in samples (width × height × samples per pixel).
pub const MAX_SAMPLES: usize = 1 << 27;
/// Decoded tiles are padded to tile boundaries, so their aggregate can exceed the useful image.
/// Keep that temporary work within the same bound as a complete decoded raw frame.
const MAX_DECODED_TILE_SAMPLES: usize = MAX_SAMPLES;
/// Photometric interpretations of DNG raw data.
pub const PHOTOMETRIC_CFA: u64 = 32803;
pub const PHOTOMETRIC_LINEAR_RAW: u64 = 34892;
/// The layout of a raw IFD's image data.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RawLayout {
pub width: usize,
pub height: usize,
pub samples_per_pixel: usize,
pub bits: u32,
/// 1 uncompressed, 7 lossless JPEG, 34892 lossy JPEG.
pub compression: u64,
pub photometric: u64,
}
/// Raw samples: `width` × `height` pixels of `samples_per_pixel` interleaved 16-bit samples.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RawImage {
pub width: usize,
pub height: usize,
pub samples_per_pixel: usize,
pub bits: u32,
pub data: Vec<u16>,
}
fn uint_or(t: &Tiff, ifd: &Ifd, tag: u16, default: u64) -> Result<u64> {
match ifd.get(tag) {
Some(e) => Ok(t.uint(e)?),
None => Ok(default),
}
}
/// The main raw image IFD: the largest full-resolution (NewSubFileType 0) IFD whose photometric
/// interpretation is CFA or LinearRaw.
pub fn find_raw_ifd<'i>(t: &Tiff, ifds: &'i [Ifd]) -> Result<&'i Ifd> {
let mut best: Option<(&Ifd, u64)> = None;
for ifd in ifds {
let Ok(kind) = uint_or(t, ifd, tags::NEW_SUBFILE_TYPE, 0) else { continue };
let Ok(photometric) = uint_or(t, ifd, tags::PHOTOMETRIC, 0) else { continue };
if kind != 0 || !matches!(photometric, PHOTOMETRIC_CFA | PHOTOMETRIC_LINEAR_RAW) {
continue;
}
let w = uint_or(t, ifd, tags::IMAGE_WIDTH, 0).unwrap_or(0);
let h = uint_or(t, ifd, tags::IMAGE_LENGTH, 0).unwrap_or(0);
let area = w.saturating_mul(h);
if best.is_none_or(|(_, a)| area > a) {
best = Some((ifd, area));
}
}
best.map(|(i, _)| i).ok_or(Error::Unsupported("no raw image in this DNG".into()))
}
/// Read and check the layout tags of a raw IFD.
pub fn layout(t: &Tiff, ifd: &Ifd) -> Result<RawLayout> {
let width = uint_or(t, ifd, tags::IMAGE_WIDTH, 0)?;
let height = uint_or(t, ifd, tags::IMAGE_LENGTH, 0)?;
let spp = uint_or(t, ifd, tags::SAMPLES_PER_PIXEL, 1)?;
let photometric = uint_or(t, ifd, tags::PHOTOMETRIC, 0)?;
let compression = uint_or(t, ifd, tags::COMPRESSION, 1)?;
let bits = match ifd.get(tags::BITS_PER_SAMPLE) {
Some(e) => {
let b = t.uints(e)?;
let first = *b.first().ok_or(Error::Malformed("empty BitsPerSample"))?;
if b.iter().any(|&x| x != first) {
return Err(Error::Unsupported("different bit depths per sample".into()));
}
first
}
None => 1,
};
if width == 0 || height == 0 || width > 65_535 || height > 65_535 {
return Err(Error::Malformed("raw image size"));
}
if !(1..=4).contains(&spp) {
return Err(Error::Unsupported(format!("{spp} samples per pixel")));
}
if photometric == PHOTOMETRIC_CFA && spp != 1 {
return Err(Error::Malformed("a CFA image with more than one sample per pixel"));
}
if !(1..=16).contains(&bits) {
return Err(Error::Unsupported(format!("{bits}-bit raw samples")));
}
if uint_or(t, ifd, tags::SAMPLE_FORMAT, 1)? != 1 {
return Err(Error::Unsupported("floating-point raw data".into()));
}
if spp > 1 && uint_or(t, ifd, tags::PLANAR_CONFIGURATION, 1)? != 1 {
return Err(Error::Unsupported("planar raw data".into()));
}
match compression {
1 => {
if uint_or(t, ifd, tags::PREDICTOR, 1)? != 1 {
return Err(Error::Unsupported("a TIFF predictor on uncompressed raw data".into()));
}
}
7 => {}
8 => return Err(Error::Unsupported("deflate-compressed DNG".into())),
// DNG 1.4: lossy JPEG only for 8-bit LinearRaw (or mask) data
34892 if photometric != PHOTOMETRIC_LINEAR_RAW => {
return Err(Error::Unsupported("lossy JPEG DNG of CFA data (DNG allows it only for 8-bit LinearRaw)".into()));
}
34892 if bits != 8 => return Err(Error::Unsupported(format!("lossy JPEG DNG with {bits}-bit samples (DNG allows only 8)"))),
34892 => {}
52546 => return Err(Error::Unsupported("JPEG XL DNG".into())),
c => return Err(Error::Unsupported(format!("DNG compression {c}"))),
}
Ok(RawLayout { width: width as usize, height: height as usize, samples_per_pixel: spp as usize, bits: bits as u32, compression, photometric })
}
/// One strip or tile: where it sits in the image and its bytes.
struct Chunk<'a> {
x: usize,
y: usize,
/// Pixel size of the chunk as coded (tiles are whole even at the image edge).
w: usize,
h: usize,
bytes: &'a [u8],
}
fn chunks<'a>(t: &Tiff<'a>, ifd: &Ifd, l: &RawLayout) -> Result<Vec<Chunk<'a>>> {
let (offs, counts, tw, th) = if let (Some(o), Some(c)) = (ifd.get(tags::TILE_OFFSETS), ifd.get(tags::TILE_BYTE_COUNTS)) {
let tw = uint_or(t, ifd, tags::TILE_WIDTH, 0)? as usize;
let th = uint_or(t, ifd, tags::TILE_LENGTH, 0)? as usize;
if tw == 0 || th == 0 || tw > 65_535 || th > 65_535 {
return Err(Error::Malformed("tile size"));
}
(t.uints(o)?, t.uints(c)?, tw, th)
} else if let (Some(o), Some(c)) = (ifd.get(tags::STRIP_OFFSETS), ifd.get(tags::STRIP_BYTE_COUNTS)) {
let rows = (uint_or(t, ifd, tags::ROWS_PER_STRIP, l.height as u64)? as usize).clamp(1, l.height);
(t.uints(o)?, t.uints(c)?, l.width, rows)
} else {
return Err(Error::Malformed("no strips or tiles"));
};
let across = l.width.div_ceil(tw);
let down = l.height.div_ceil(th);
let n = across * down;
if offs.len() < n || counts.len() < n {
return Err(Error::Malformed("fewer strips or tiles than the image needs"));
}
let mut out = Vec::with_capacity(n);
let mut ranges = Vec::with_capacity(n);
for i in 0..n {
let (x, y) = ((i % across) * tw, (i / across) * th);
let start = usize::try_from(offs[i]).map_err(|_| Error::Truncated)?;
let len = usize::try_from(counts[i]).map_err(|_| Error::Truncated)?;
let end = start.checked_add(len).ok_or(Error::Truncated)?;
let bytes = t.data.get(start..end).ok_or(Error::Truncated)?;
// strips end at the image; tiles are coded whole
let h = if ifd.get(tags::TILE_OFFSETS).is_some() { th } else { th.min(l.height - y) };
out.push(Chunk { x, y, w: tw, h, bytes });
if len != 0 {
ranges.push((start, end));
}
}
// chunks that share bytes would decode the same data again and again
ranges.sort_unstable();
if ranges.windows(2).any(|w| w[1].0 < w[0].1) {
return Err(Error::Malformed("strips or tiles overlap"));
}
Ok(out)
}
/// Unpack `w` × `h` × `spp` uncompressed samples of `bits` bits: 8 and 16-bit samples as bytes /
/// words in the file's byte order, other depths packed most significant bit first with each row
/// starting on a byte boundary (TIFF 6.0 §7, DNG BitsPerSample).
pub fn unpack(bytes: &[u8], w: usize, h: usize, spp: usize, bits: u32, big_endian: bool) -> Result<Vec<u16>> {
// zero-bit samples need no input, so a header could ask for any number of them
if !(1..=16).contains(&bits) {
return Err(Error::Malformed("unsupported bits per sample"));
}
let per_row = w * spp;
let row_bytes = (per_row * bits as usize).div_ceil(8);
let need = row_bytes.checked_mul(h).ok_or(Error::TooLarge)?;
if bytes.len() < need {
return Err(Error::Truncated);
}
let mut out = Vec::with_capacity(per_row * h);
for row in bytes.chunks_exact(row_bytes.max(1)).take(h) {
match bits {
8 => out.extend(row.iter().map(|&b| u16::from(b))),
16 => out.extend(row.as_chunks::<2>().0.iter().map(|&b| if big_endian { u16::from_be_bytes(b) } else { u16::from_le_bytes(b) })),
_ => {
let mut acc: u32 = 0;
let mut n: u32 = 0;
let mut it = row.iter();
for _ in 0..per_row {
while n < bits {
acc = (acc << 8) | u32::from(*it.next().ok_or(Error::Truncated)?);
n += 8;
}
n -= bits;
out.push(((acc >> n) & ((1 << bits) - 1)) as u16);
acc &= (1 << n) - 1;
}
}
}
}
Ok(out)
}
/// Decode one chunk into `w` × `h` × `spp` samples.
fn decode_chunk(c: &Chunk, l: &RawLayout, big_endian: bool) -> Result<Vec<u16>> {
let need = c.w * c.h * l.samples_per_pixel;
match l.compression {
1 => unpack(c.bytes, c.w, c.h, l.samples_per_pixel, l.bits, big_endian),
34892 => lossy_jpeg(c.bytes, need, l.samples_per_pixel),
_ => {
// the JPEG's samples fill the chunk in raster order (its own width × components may
// differ from the chunk's, e.g. two components of half the width)
// slack for a JPEG coded larger than its chunk (e.g. a last strip at full RowsPerStrip);
// the samples past `need` are dropped below
let d = ljpeg::decode(c.bytes, need.saturating_add(65_536 * 4))?;
if d.samples.len() < need {
return Err(Error::Malformed("lossless JPEG smaller than its tile or strip"));
}
let mut s = d.samples;
s.truncate(need);
Ok(s)
}
}
}
/// Decode a baseline (lossy) JPEG tile or strip (Compression 34892) into `need` 8-bit samples:
/// one component (greyscale) or three (colour, converted from YCbCr by the JPEG decoder as the
/// JPEG says), in raster order.
fn lossy_jpeg(bytes: &[u8], need: usize, spp: usize) -> Result<Vec<u16>> {
use image::ImageDecoder;
let bad = |_| Error::Malformed("lossy JPEG tile");
let decode = || -> Result<Vec<u16>> {
let mut d = image::codecs::jpeg::JpegDecoder::new(std::io::Cursor::new(bytes)).map_err(bad)?;
let channels = match d.color_type() {
image::ColorType::L8 => 1usize,
image::ColorType::Rgb8 => 3,
c => return Err(Error::Unsupported(format!("lossy JPEG DNG tile of colour type {c:?}"))),
};
if channels != spp {
return Err(Error::Malformed("lossy JPEG components differ from SamplesPerPixel"));
}
let (w, h) = d.dimensions();
let total = (w as usize).checked_mul(h as usize).and_then(|v| v.checked_mul(channels)).ok_or(Error::TooLarge)?;
if total < need {
return Err(Error::Malformed("lossy JPEG smaller than its tile or strip"));
}
if total > need.saturating_mul(4).saturating_add(65_536) || total > MAX_SAMPLES {
return Err(Error::TooLarge);
}
let mut limits = image::Limits::default();
limits.max_alloc = Some(total as u64 * 4 + (1 << 20));
d.set_limits(limits).map_err(bad)?;
let mut buf = vec![0u8; total];
d.read_image(&mut buf).map_err(bad)?;
Ok(buf.iter().take(need).map(|&v| u16::from(v)).collect())
};
// a third-party decoder: a panic in it is an error for this file, not a crash
std::panic::catch_unwind(std::panic::AssertUnwindSafe(decode)).unwrap_or(Err(Error::Malformed("lossy JPEG decoder failed")))
}
/// Read the raw image of `ifd`.
pub fn read(t: &Tiff, ifd: &Ifd) -> Result<RawImage> {
let l = layout(t, ifd)?;
let spp = l.samples_per_pixel;
let total = l.width.checked_mul(l.height).and_then(|v| v.checked_mul(spp)).ok_or(Error::TooLarge)?;
if total > MAX_SAMPLES {
return Err(Error::TooLarge);
}
let chunks = chunks(t, ifd, &l)?;
// every sample takes at least one bit of file (a Huffman code is at least one bit long);
// lossy JPEG at least two bits per 8 × 8 block and component (a DC code and an end of
// block), so a tiny file cannot claim a huge picture
let per_byte = if l.compression == 34892 { 256 } else { 8 };
let coded: usize = chunks.iter().map(|c| c.bytes.len()).fold(0usize, usize::saturating_add);
if total.div_ceil(per_byte) > coded {
return Err(Error::Truncated);
}
let mut decoded_samples = 0usize;
for c in &chunks {
let n = c.w.checked_mul(c.h).and_then(|v| v.checked_mul(spp)).ok_or(Error::TooLarge)?;
decoded_samples = decoded_samples.checked_add(n).ok_or(Error::TooLarge)?;
if n > MAX_SAMPLES || decoded_samples > MAX_DECODED_TILE_SAMPLES {
return Err(Error::TooLarge);
}
if n.div_ceil(per_byte) > c.bytes.len() {
return Err(Error::Truncated);
}
}
let raw = |data| RawImage { width: l.width, height: l.height, samples_per_pixel: spp, bits: l.bits, data };
// one chunk covering the image (a single strip): its samples are the image, no copy
if let [c] = chunks.as_slice()
&& (c.x, c.y, c.w) == (0, 0, l.width)
&& c.h >= l.height
{
let mut d = decode_chunk(c, &l, t.big_endian)?;
d.truncate(total);
if d.len() == total {
return Ok(raw(d));
}
return Err(Error::Truncated);
}
// bands of one row of tiles (or one strip): decoded in parallel, each band copying its own
// chunks into place
let row_len = l.width * spp;
let (Some(first), true) = (chunks.first(), row_len > 0) else { return Err(Error::Malformed("no strips or tiles")) };
let (band_rows, across) = (first.h.max(1), l.width.div_ceil(first.w.max(1)));
let mut data = vec![0u16; total];
let band = |k: usize, out: &mut [u16]| -> Result<()> {
let mine = chunks.get(k * across..((k + 1) * across).min(chunks.len())).unwrap_or(&[]);
#[cfg(feature = "line-dng-threads")]
let decoded: Vec<Result<Vec<u16>>> = mine.par_iter().map(|c| decode_chunk(c, &l, t.big_endian)).collect();
#[cfg(not(feature = "line-dng-threads"))]
let decoded: Vec<Result<Vec<u16>>> = mine.iter().map(|c| decode_chunk(c, &l, t.big_endian)).collect();
for (c, d) in mine.iter().zip(decoded) {
let d = d?;
let cw = c.w.min(l.width.saturating_sub(c.x));
let ch = c.h.min(l.height.saturating_sub(c.y));
for r in 0..ch {
let src = d.get(r * c.w * spp..(r * c.w + cw) * spp).ok_or(Error::Truncated)?;
let at = r * row_len + c.x * spp;
out.get_mut(at..at + cw * spp).ok_or(Error::Truncated)?.copy_from_slice(src);
}
}
Ok(())
};
#[cfg(feature = "line-dng-threads")]
data.par_chunks_mut(band_rows * row_len).enumerate().try_for_each(|(k, out)| band(k, out))?;
#[cfg(not(feature = "line-dng-threads"))]
data.chunks_mut(band_rows * row_len).enumerate().try_for_each(|(k, out)| band(k, out))?;
Ok(raw(data))
}
#[cfg(test)]
mod unpack_tests {
use super::*;
#[test]
fn reads_a_complete_uncompressed_cfa_strip() {
let mut bytes = b"II*\0".to_vec();
bytes.extend_from_slice(&8u32.to_le_bytes());
let entries: [(u16, u32); 9] = [
(tags::IMAGE_WIDTH, 2),
(tags::IMAGE_LENGTH, 2),
(tags::BITS_PER_SAMPLE, 8),
(tags::COMPRESSION, 1),
(tags::PHOTOMETRIC, PHOTOMETRIC_CFA as u32),
(tags::STRIP_OFFSETS, 122),
(tags::SAMPLES_PER_PIXEL, 1),
(tags::ROWS_PER_STRIP, 2),
(tags::STRIP_BYTE_COUNTS, 4),
];
bytes.extend_from_slice(&(entries.len() as u16).to_le_bytes());
for (tag, value) in entries {
bytes.extend_from_slice(&tag.to_le_bytes());
bytes.extend_from_slice(&4u16.to_le_bytes());
bytes.extend_from_slice(&1u32.to_le_bytes());
bytes.extend_from_slice(&value.to_le_bytes());
}
bytes.extend_from_slice(&0u32.to_le_bytes());
bytes.extend_from_slice(&[1, 2, 3, 4]);
let (tiff, first) = Tiff::new(&bytes).unwrap();
let ifds = tiff.all_ifds(first).unwrap();
let raw = read(&tiff, find_raw_ifd(&tiff, &ifds).unwrap()).unwrap();
assert_eq!((raw.width, raw.height, raw.bits), (2, 2, 8));
assert_eq!(raw.data, [1, 2, 3, 4]);
}
#[test]
fn zero_or_oversized_bit_depths_are_rejected_before_allocating() {
// zero-bit samples consume no input: one byte used to yield a whole picture of zeros
assert!(matches!(unpack(&[0], 1 << 20, 1 << 10, 1, 0, false), Err(Error::Malformed(_))));
assert!(matches!(unpack(&[0; 64], 4, 1, 1, 17, false), Err(Error::Malformed(_))));
assert_eq!(unpack(&[0x12, 0x34, 0x50], 2, 1, 1, 12, true).unwrap(), vec![0x123, 0x450]);
}
}
+3
View File
@@ -88,6 +88,9 @@ mod arw6_testenc;
mod arw_lens;
#[cfg(feature = "arw")]
mod arw_lens_rm6;
/// Bounded DNG strip and tile sample reader.
#[cfg(feature = "line-dng")]
pub mod dng_lines;
#[allow(dead_code)]
mod huffman;
#[cfg(feature = "jxl")]