mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 03:52:45 +01:00
Reduce temporary memory when decoding DPX and Cineon
(cherry picked from commit f08789c7a6)
This commit is contained in:
1 parent
4f31e9c33a
commit
0596a975ae
2 files changed
+22
-4
No files matched your search
@@ -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<Image, CodecError> {
|
||||
let h = parse(bytes)?;
|
||||
let e = h.element;
|
||||
@@ -330,8 +331,11 @@ pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError>
|
||||
16 => Image::from_u16(h.width, h.height, layout, &codes)?,
|
||||
b => {
|
||||
let max = (1u64 << b) - 1;
|
||||
let wide: Vec<u16> = 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<Image, Code
|
||||
let line = data.get(start..start + line_len).ok_or_else(|| err("truncated image data"))?;
|
||||
unpack_line(line, o, 10, 1, row)?;
|
||||
}
|
||||
let wide: Vec<u16> = 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.
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user