mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 20:22:39 +00:00
Avoid widened sample buffers in eight-bit DPX export
(cherry picked from commit 23393d1def)
This commit is contained in:
@@ -636,6 +636,7 @@ fn pack_line(out: &mut Vec<u8>, 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<Vec<u8>, 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<Ve
|
||||
};
|
||||
let bits = bits_for(opts.dpx_bits, img.sample_type());
|
||||
let codes: Vec<u16> = 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<Ve
|
||||
(s, b) => 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<Ve
|
||||
|
||||
let mut out = wr.h;
|
||||
out.try_reserve_exact(data_len).map_err(|_| CodecError::encode(F, "out of memory"))?;
|
||||
for row in codes.chunks_exact(per_line.max(1)) {
|
||||
pack_line(&mut out, row, bits);
|
||||
if bits == 8 {
|
||||
for row in img.data().chunks_exact(per_line.max(1)) {
|
||||
let start = out.len();
|
||||
out.extend_from_slice(row);
|
||||
out.resize(start + row.len().div_ceil(4) * 4, 0);
|
||||
}
|
||||
} else {
|
||||
for row in codes.chunks_exact(per_line.max(1)) {
|
||||
pack_line(&mut out, row, bits);
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
@@ -305,6 +305,30 @@ fn listed_header_fields_survive_a_round_trip() {
|
||||
assert_eq!(decode(&out).unwrap().meta.text, img.meta.text);
|
||||
}
|
||||
|
||||
/// Eight-bit rows retain every sample and have zero padding to a four-byte boundary.
|
||||
#[test]
|
||||
fn eight_bit_rows_keep_samples_and_zero_word_padding() {
|
||||
for layout in [ChannelLayout::Gray, ChannelLayout::Rgb, ChannelLayout::Rgba] {
|
||||
for width in 1..=5 {
|
||||
let row_len = width as usize * layout.channels();
|
||||
let raw: Vec<u8> = (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::<Vec<_>>()).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user