raw: charge short lossy JPEG DNG strips their declared rows (craft-libs #778) (#1857)

This commit is contained in:
FredApps authored and GitHub committed 2026-11-16 07:52:02 -05:00
1 parent a62de6c7c3
commit 402dfe46ad
3 files changed
+44 -2

No files matched your search

+11 -2
View File
@@ -250,12 +250,21 @@ pub fn unpack(bytes: &[u8], w: usize, h: usize, spp: usize, bits: u32, big_endia
/// The most samples decoding chunk `c` may allocate, charged against the aggregate budget before
/// anything is decoded: the samples themselves when uncompressed; for lossless JPEG the coded rows
/// with an eighth of slack (the frame may be coded a little larger than its chunk); for lossy JPEG
/// the chunk rounded up to the 32-pixel MCU padding `tiffraw` accepts.
/// the frame its header declares when that fits the chunk rounded up to the 32-pixel MCU padding
/// `tiffraw` accepts (a 16-row strip is charged 16 rows, not 32), else that padded chunk (decoding
/// then refuses the frame).
fn decode_budget(c: &Chunk, l: &RawLayout) -> Option<usize> {
let spp = l.samples_per_pixel;
match l.compression {
1 => c.w.checked_mul(c.h)?.checked_mul(spp),
34892 => c.w.checked_next_multiple_of(32)?.checked_mul(c.coded_h.checked_next_multiple_of(32)?)?.checked_mul(spp),
34892 => {
let (max_w, max_h) = (c.w.checked_next_multiple_of(32)?, c.coded_h.checked_next_multiple_of(32)?);
let padded = max_w.checked_mul(max_h)?.checked_mul(spp)?;
match crate::tiffraw::lossy_jpeg_dimensions(c.bytes) {
Some((w, h)) if w <= max_w && h <= max_h => Some(w.checked_mul(h)?.checked_mul(spp)?.min(padded)),
_ => Some(padded),
}
}
_ => {
let coded = c.w.checked_mul(c.coded_h)?.checked_mul(spp)?;
coded.checked_add(coded / 8)?.checked_add(64)
+13
View File
@@ -458,6 +458,19 @@ pub(crate) fn lossy_jpeg(src: &[u8], cpp: usize, max_samples: usize) -> Result<(
lossy_jpeg_with_tile(src, cpp, max_samples, None)
}
/// The width and height a baseline (lossy) JPEG declares in its frame header, read without
/// decoding any scan; `None` when the header cannot be read.
#[cfg(feature = "line-dng")]
pub(crate) fn lossy_jpeg_dimensions(src: &[u8]) -> Option<(usize, usize)> {
use zune_core::bytestream::ZCursor;
use zune_core::options::DecoderOptions;
let opts = DecoderOptions::default().set_max_width(1 << 16).set_max_height(1 << 16).set_strict_mode(false);
let mut d = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(src), opts);
d.decode_headers().ok()?;
let info = d.info()?;
Some((usize::from(info.width), usize::from(info.height)))
}
/// Validate the actual frame before decode allocates its output. Edge tiles may store
/// a smaller frame covering all visible samples or bounded whole-MCU padding.
#[cfg(feature = "dng")]
+20
View File
@@ -257,3 +257,23 @@ fn a_lossless_jpeg_larger_than_its_tile_is_refused_before_decoding_it_all() {
raw.set_image(ImageData::Tiles { tile_width: 16, tile_height: 16, tiles: vec![fit.clone(), fit] });
assert_eq!(lines(&dng(raw, ByteOrder::Little)).unwrap().data, vec![7; 32 * 16]);
}
#[test]
fn short_lossy_jpeg_strips_are_charged_their_own_rows() {
// 8192 x 4096 RGB (below MAX_SAMPLES) in 16-row lossy JPEG strips: each strip's JPEG is exactly
// 16 rows, so the decode budget charges 16 rows, not the 32 of the MCU padding allowance (which
// would put the image over the aggregate budget and refuse it)
let (w, h, rows) = (8192usize, 4096usize, 16usize);
let strip: Vec<u8> = (0..w * rows).flat_map(|i| [(i % 251) as u8, 90, 160]).collect();
let mut jpeg = Vec::new();
jpeg_encoder::Encoder::new(&mut jpeg, 90).encode(&strip, w as u16, rows as u16, jpeg_encoder::ColorType::Rgb).unwrap();
let mut raw = base_ifd(w, h, 8, 3, false);
raw.set(t::COMPRESSION, Value::Short(vec![34892]));
raw.set_image(ImageData::Strips { rows_per_strip: rows as u32, strips: vec![jpeg; h / rows] });
let img = lines(&dng(raw, ByteOrder::Little)).unwrap();
assert_eq!((img.width, img.height, img.samples_per_pixel), (w, h, 3));
assert_eq!(img.data.len(), w * h * 3);
// every strip holds the same picture
let band = w * rows * 3;
assert_eq!(&img.data[..band], &img.data[img.data.len() - band..]);
}