From 4f31e9c33ae84bd2854b55139cd6fcfe4e82fd85 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Thu, 19 Nov 2026 01:50:13 -0800 Subject: [PATCH] Avoid widened sample buffers in eight-bit DPX export (cherry picked from commit 23393d1defb523aa998521e2330ca51d7d594c23) --- crates/image-codecs/src/codecs/dpx.rs | 19 +++++++++++++++---- crates/image-codecs/tests/it/dpx.rs | 24 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/crates/image-codecs/src/codecs/dpx.rs b/crates/image-codecs/src/codecs/dpx.rs index 39be0e255..018d72796 100644 --- a/crates/image-codecs/src/codecs/dpx.rs +++ b/crates/image-codecs/src/codecs/dpx.rs @@ -636,6 +636,7 @@ fn pack_line(out: &mut Vec, codes: &[u16], bits: u8) { out.resize(start + written.div_ceil(4) * 4, 0); } +/// Write DPX, copying eight-bit rows directly while retaining wider code packing. pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result, CodecError> { let img = src.converted(plan.layout, plan.sample); let (w, h) = img.dimensions(); @@ -647,7 +648,8 @@ pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result = match (img.sample_type(), bits) { - (SampleType::U8, 8) => img.data().iter().map(|&v| u16::from(v)).collect(), + // Eight-bit samples are written directly below, without a widened copy. + (SampleType::U8, 8) => Vec::new(), (SampleType::U16, 16) => img.to_u16_samples().unwrap_or_default(), (SampleType::U16, 10 | 12) => { let max = (1u64 << bits) - 1; @@ -656,7 +658,8 @@ pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result return Err(CodecError::encode(F, format!("cannot write {s:?} samples at {b} bits"))), }; let per_line = w as usize * layout.channels(); - if codes.len() != per_line * h as usize { + let sample_count = if bits == 8 { img.data().len() } else { codes.len() }; + if sample_count != per_line * h as usize { return Err(CodecError::encode(F, "the image has the wrong number of samples")); } let data_len = line_bytes(per_line, bits, if matches!(bits, 10 | 12) { 1 } else { 0 }) @@ -721,8 +724,16 @@ pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result = (0..row_len * 3).map(|i| (i as u8).wrapping_mul(37)).collect(); + let img = Image::from_u8(width, 3, layout, raw.clone()).unwrap(); + let mut expected = Vec::new(); + for row in raw.chunks_exact(row_len) { + expected.extend_from_slice(row); + expected.extend(std::iter::repeat_n(0, (4 - row_len % 4) % 4)); + } + for choice in [None, Some(DpxBits::Bits8)] { + let out = encode(&img, Format::Dpx, &opts(choice)).unwrap(); + let offset = be32_at(&out, 4) as usize; + assert_eq!(be32_at(&out, 16) as usize, out.len()); + assert_eq!(&out[offset..], expected, "{layout:?} width {width}, {choice:?}"); + assert_eq!(decode(&out).unwrap().data(), img.data()); + } + } + } +} + #[test] fn chosen_bit_depth_sets_the_header_and_packing() { let img = Image::from_u16(5, 2, ChannelLayout::Rgb, &(0..30u16).map(|i| i * 2000).collect::>()).unwrap();