#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 <noreply@anthropic.com>
This commit is contained in:
vangel76
2026-11-15 19:19:49 -05:00
committed by Dylan M. Taylor
co-authored by Claude Opus 5.5
parent eb5844d4da
commit 589d84d0f4
+46 -5
View File
@@ -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<RawLayout> {
}
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<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),
_ => {
// 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<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>> {
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:?}"))),
};
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)?;
@@ -220,9 +258,12 @@ pub fn read(t: &Tiff, ifd: &Ifd) -> Result<RawImage> {
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<RawImage> {
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);
}
}