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); } }