diff --git a/Cargo.lock b/Cargo.lock index 95341b06d..4a9ede9aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2580,6 +2580,7 @@ dependencies = [ "craft-crw", "craft-metadata", "craft-tiff", + "image", "jpeg-encoder 0.7.1", "jxl-oxide", "miniz_oxide 0.9.1", diff --git a/crates/raw/Cargo.toml b/crates/raw/Cargo.toml index 5e5a021c3..b99d4bc35 100644 --- a/crates/raw/Cargo.toml +++ b/crates/raw/Cargo.toml @@ -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. diff --git a/crates/raw/src/dng_lines.rs b/crates/raw/src/dng_lines.rs index 83cd1ed47..46cf36291 100644 --- a/crates/raw/src/dng_lines.rs +++ b/crates/raw/src/dng_lines.rs @@ -1,11 +1,51 @@ //! 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 = "threads")] +#[cfg(feature = "line-dng-threads")] use rayon::prelude::*; -use crate::tiff::{Ifd, Tiff, tags}; -use crate::{Error, Result, ljpeg}; +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 = std::result::Result; +impl From 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 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; @@ -41,7 +81,7 @@ pub struct RawImage { fn uint_or(t: &Tiff, ifd: &Ifd, tag: u16, default: u64) -> Result { match ifd.get(tag) { - Some(e) => t.uint(e), + Some(e) => Ok(t.uint(e)?), None => Ok(default), } } @@ -319,9 +359,9 @@ pub fn read(t: &Tiff, ifd: &Ifd) -> Result { 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 = "threads")] + #[cfg(feature = "line-dng-threads")] let decoded: Vec>> = mine.par_iter().map(|c| decode_chunk(c, &l, t.big_endian)).collect(); - #[cfg(not(feature = "threads"))] + #[cfg(not(feature = "line-dng-threads"))] let decoded: Vec>> = mine.iter().map(|c| decode_chunk(c, &l, t.big_endian)).collect(); for (c, d) in mine.iter().zip(decoded) { let d = d?; @@ -335,9 +375,9 @@ pub fn read(t: &Tiff, ifd: &Ifd) -> Result { } Ok(()) }; - #[cfg(feature = "threads")] + #[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 = "threads"))] + #[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)) } @@ -346,6 +386,37 @@ pub fn read(t: &Tiff, ifd: &Ifd) -> Result { 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 diff --git a/crates/raw/src/lib.rs b/crates/raw/src/lib.rs index a46adbbed..f97f5e11a 100644 --- a/crates/raw/src/lib.rs +++ b/crates/raw/src/lib.rs @@ -90,6 +90,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")]