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 <noreply@anthropic.com>
This commit is contained in:
vangel76andClaude Sonnet 5.5 authored and Dylan M. Taylor committed 2026-11-15 19:19:49 -05:00
1 parent b8a3f828a1
commit 41801059b3
1 file changed
+5 -2
+5 -2
View File
@@ -214,7 +214,7 @@ 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),
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<Vec<u16>>
/// 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<Vec<u16>> {
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>> {
@@ -244,6 +244,9 @@ fn lossy_jpeg(bytes: &[u8], need: usize) -> Result<Vec<u16>> {
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 {