From eb5844d4dad270a341fd66dee7ea1485779504bc Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 11 Oct 2026 14:42:27 +0000 Subject: [PATCH 1/4] #1065: faster DNG decode and development, reduced-resolution path Problem: a 4096 x 2160 12-bit lossless-JPEG CinemaDNG frame took about 50 ms (tiles) to 115 ms (one strip) on 16 threads and about 200 ms on one, too slow for smooth 4K playback, and every frame was developed at full size even for small monitors and proxies. Fix, without changing the output (f32 bit patterns compared): - Lossless JPEG: one 13-bit lookup gives a code and its difference bits together; the bit reader takes eight bytes at once when they hold no 0xFF; per-predictor line loops. - Tiles decode in parallel by bands straight into place; a single strip covering the image is used without a copy. - Normalisation: without black-level deltas, a lookup table per CFA / black-level site replaces the per-sample float arithmetic (each entry computed by the same expression). - Demosaic, crop and colour run row by row from the normalised plane (no padded copy, no RGB intermediate), with 5 x 5 windows sliding along the five mirrored rows; the bilinear (Fast) Bayer demosaic sums in the reference order. - ProcessOptions::target_size now develops Bayer, LinearRaw and mono pictures at 1/2, 1/4 or 1/8 size (the smallest covering the target) by averaging each colour over the blocks, without demosaic. The first version is kept as `reference` (feature `writer`, tests and benchmarks only): tests/equivalence.rs checks identical raw samples and developed bits over all Bayer phases, both qualities, odd crops, active areas, linearisation, black deltas, codes above the bit depth, LinearRaw, mono, other CFAs and tiny pictures; examples/bench.rs times both interleaved at 1/4/16 threads. Best of 9 under load ~12: tiles 2.5x (1 thread) to 2.9x (16), one strip 2.1-2.6x; half size 3-4x. Co-Authored-By: Claude Opus 5.5 --- crates/raw/src/dng_lines.rs | 55 +++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/crates/raw/src/dng_lines.rs b/crates/raw/src/dng_lines.rs index b1c448ffc..201af3feb 100644 --- a/crates/raw/src/dng_lines.rs +++ b/crates/raw/src/dng_lines.rs @@ -234,21 +234,46 @@ pub fn read(t: &Tiff, ifd: &Ifd) -> Result { return Err(Error::Truncated); } } - #[cfg(feature = "threads")] - let decoded: Vec>> = chunks.par_iter().map(|c| decode_chunk(c, &l, t.big_endian)).collect(); - #[cfg(not(feature = "threads"))] - let decoded: Vec>> = chunks.iter().map(|c| decode_chunk(c, &l, t.big_endian)).collect(); - let mut data = vec![0u16; total]; - let row_len = l.width * spp; - for (c, d) in chunks.iter().zip(decoded) { - let d = d?; - let cw = c.w.min(l.width - c.x); - let ch = c.h.min(l.height - 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 = (c.y + r) * row_len + c.x * spp; - data.get_mut(at..at + cw * spp).ok_or(Error::Truncated)?.copy_from_slice(src); + 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); } - Ok(RawImage { width: l.width, height: l.height, samples_per_pixel: spp, bits: l.bits, data }) + // 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 = "threads")] + let decoded: Vec>> = mine.par_iter().map(|c| decode_chunk(c, &l, t.big_endian)).collect(); + #[cfg(not(feature = "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?; + 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 = "threads")] + data.par_chunks_mut(band_rows * row_len).enumerate().try_for_each(|(k, out)| band(k, out))?; + #[cfg(not(feature = "threads"))] + data.chunks_mut(band_rows * row_len).enumerate().try_for_each(|(k, out)| band(k, out))?; + Ok(raw(data)) } From 589d84d0f415fb350e37d8fdc15abe9258bb331f Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 11 Oct 2026 14:52:03 +0000 Subject: [PATCH 2/4] #1065: lossy JPEG DNG (Compression 34892) Problem: lossy DNGs (DNG 1.4 Compression 34892, used for proxy and "lossy compressed" DNGs, 8-bit LinearRaw tiles usually with a LinearizationTable) were refused as unsupported. Fix: decode each tile or strip with the baseline JPEG decoder the workspace already depends on (`image` with only its jpeg feature, as in crates/codecs and crates/media; no new crate in Cargo.lock, builds for wasm32), one component as greyscale or three as colour, the samples filling the chunk in raster order as with lossless JPEG. As the spec allows it only for 8-bit LinearRaw, CFA or other depths are refused with a message naming lossy JPEG. The decoder runs under catch_unwind with an allocation limit, its picture must match the chunk, and a tiny file cannot claim a huge picture (at least two bits per 8 x 8 block). Info reports "lossy JPEG". The test writer gains a lossy JPEG path through `image`'s encoder. Co-Authored-By: Claude Opus 5.5 --- crates/raw/src/dng_lines.rs | 51 +++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/crates/raw/src/dng_lines.rs b/crates/raw/src/dng_lines.rs index 201af3feb..884483fd5 100644 --- a/crates/raw/src/dng_lines.rs +++ b/crates/raw/src/dng_lines.rs @@ -21,7 +21,7 @@ pub struct RawLayout { pub height: usize, pub samples_per_pixel: usize, pub bits: u32, - /// 1 uncompressed, 7 lossless JPEG. + /// 1 uncompressed, 7 lossless JPEG, 34892 lossy JPEG. pub compression: u64, pub photometric: u64, } @@ -107,7 +107,12 @@ pub fn layout(t: &Tiff, ifd: &Ifd) -> Result { } 7 => {} 8 => return Err(Error::Unsupported("deflate-compressed DNG".into())), - 34892 => return Err(Error::Unsupported("lossy JPEG 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}"))), } @@ -197,6 +202,7 @@ fn decode_chunk(c: &Chunk, l: &RawLayout, big_endian: bool) -> Result> 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), _ => { // 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) @@ -211,6 +217,38 @@ fn decode_chunk(c: &Chunk, l: &RawLayout, big_endian: bool) -> Result> } } +/// 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) -> Result> { + use image::ImageDecoder; + let bad = |_| Error::Malformed("lossy JPEG tile"); + let decode = || -> Result> { + 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:?}"))), + }; + 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 { let l = layout(t, ifd)?; @@ -220,9 +258,12 @@ pub fn read(t: &Tiff, ifd: &Ifd) -> Result { 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) + // 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 / 8 > coded { + if total / per_byte > coded { return Err(Error::Truncated); } for c in &chunks { @@ -230,7 +271,7 @@ pub fn read(t: &Tiff, ifd: &Ifd) -> Result { if n > MAX_SAMPLES { return Err(Error::TooLarge); } - if n / 8 > c.bytes.len() { + if n / per_byte > c.bytes.len() { return Err(Error::Truncated); } } From 2f093b0e2bf87dff39e2da3c997031d4170af44a Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 24 Oct 2026 11:29:25 +0000 Subject: [PATCH 3/4] dng: restore rayon import in raw.rs after merge Co-Authored-By: Claude Sonnet 5.5 --- crates/raw/src/dng_lines.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/raw/src/dng_lines.rs b/crates/raw/src/dng_lines.rs index fb9f383f5..95aa3455a 100644 --- a/crates/raw/src/dng_lines.rs +++ b/crates/raw/src/dng_lines.rs @@ -1,6 +1,9 @@ //! 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")] +use rayon::prelude::*; + use crate::tiff::{Ifd, Tiff, tags}; use crate::{Error, Result, ljpeg}; From 41801059b36edc1031e4468e2e6ebf8b0553c7d8 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 15:26:08 +0000 Subject: [PATCH 4/4] Address review: lossy JPEG channel check, opcode opt-in in develop_with, writer errors - raw: a lossy JPEG tile must have as many components as SamplesPerPixel. - process::develop_with: use the opcode lists only when apply_opcodes is set. - writer: lossy_jpeg returns an error instead of an empty chunk or an encoder panic; new try_write rejects unsupported sample counts and bits/CFA options for lossy JPEG. Co-Authored-By: Claude Sonnet 5.5 --- crates/raw/src/dng_lines.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/raw/src/dng_lines.rs b/crates/raw/src/dng_lines.rs index 1ad135e19..2e83ccdca 100644 --- a/crates/raw/src/dng_lines.rs +++ b/crates/raw/src/dng_lines.rs @@ -214,7 +214,7 @@ fn decode_chunk(c: &Chunk, l: &RawLayout, big_endian: bool) -> Result> 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), + 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) @@ -234,7 +234,7 @@ fn decode_chunk(c: &Chunk, l: &RawLayout, big_endian: bool) -> Result> /// 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) -> Result> { +fn lossy_jpeg(bytes: &[u8], need: usize, spp: usize) -> Result> { use image::ImageDecoder; let bad = |_| Error::Malformed("lossy JPEG tile"); let decode = || -> Result> { @@ -244,6 +244,9 @@ fn lossy_jpeg(bytes: &[u8], need: usize) -> Result> { 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 {