From 0596a975aeccbd3552e384dfa1798d5084ca2d7b Mon Sep 17 00:00:00 2001 From: yoriswind Date: Thu, 19 Nov 2026 01:50:21 -0800 Subject: [PATCH] Reduce temporary memory when decoding DPX and Cineon (cherry picked from commit f08789c7a6a62db6a2010ee698f9f0ae94d3a1e0) --- crates/image-codecs/src/codecs/dpx.rs | 14 ++++++++++---- crates/image-codecs/tests/it/dpx.rs | 12 ++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/crates/image-codecs/src/codecs/dpx.rs b/crates/image-codecs/src/codecs/dpx.rs index 018d72796..f691e3cc0 100644 --- a/crates/image-codecs/src/codecs/dpx.rs +++ b/crates/image-codecs/src/codecs/dpx.rs @@ -272,6 +272,7 @@ fn narrow(v: u16, max: u64) -> u16 { ((u64::from(v) * 2 * max + 65535) / (2 * 65535)) as u16 } +/// Decode the first unsigned DPX element, retaining its code mapping and header metadata. pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { let h = parse(bytes)?; let e = h.element; @@ -330,8 +331,11 @@ pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result 16 => Image::from_u16(h.width, h.height, layout, &codes)?, b => { let max = (1u64 << b) - 1; - let wide: Vec = codes.iter().map(|&c| widen(c, max)).collect(); - Image::from_u16(h.width, h.height, layout, &wide)? + // Reuse the owned codes instead of holding another full-frame sample buffer. + for code in &mut codes { + *code = widen(*code, max); + } + Image::from_u16(h.width, h.height, layout, &codes)? } }; if h.elements > 1 { @@ -413,8 +417,10 @@ pub(crate) fn decode_cineon(bytes: &[u8], limits: &Limits) -> Result = codes.iter().map(|&c| widen(c, 1023)).collect(); - Image::from_u16(width, height, layout, &wide) + for code in &mut codes { + *code = widen(*code, 1023); + } + Image::from_u16(width, height, layout, &codes) } /// Cineon's generic plus film header: where our image data starts. diff --git a/crates/image-codecs/tests/it/dpx.rs b/crates/image-codecs/tests/it/dpx.rs index b975ae72e..953565836 100644 --- a/crates/image-codecs/tests/it/dpx.rs +++ b/crates/image-codecs/tests/it/dpx.rs @@ -549,3 +549,15 @@ fn any_image_saves_as_ten_bit_cineon() { let gray8 = Image::from_u8(2, 1, ChannelLayout::Gray, vec![0, 255]).unwrap(); assert_eq!(cineon_written(&encode(&gray8, Format::Cineon, &EncodeOptions::default()).unwrap()), (1, 2, 1, vec![0, 1023])); } + +/// Allocation limits describe decoded U16 pixels, even when the stored codes use fewer bits. +#[test] +fn widened_samples_obey_the_exact_decoded_pixel_budget() { + let frames = [Fixture::new(50, 10, 1, 5, 3).bytes(), Fixture::new(50, 12, 1, 5, 3).bytes(), cineon_bytes(3, 5, 3, &cineon_codes(45))]; + for bytes in frames { + let options = |max_alloc| DecodeOptions { limits: Limits { max_alloc, ..Limits::default() }, ..Default::default() }; + let budget = 5 * 3 * 3 * 2; + assert!(decode_with(&bytes, &options(budget)).is_ok(), "the exact U16 pixel budget accepts the frame"); + assert!(matches!(decode_with(&bytes, &options(budget - 1)), Err(CodecError::LimitExceeded(_))), "one byte below the decoded size rejects the frame"); + } +}