From 41801059b36edc1031e4468e2e6ebf8b0553c7d8 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 15:26:08 +0000 Subject: [PATCH] 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 {