exr: share bounded RLE and byte prediction (#2921)

This commit is contained in:
Terra854
2026-11-23 03:36:07 +07:00
committed by GitHub
parent 0da914f4a2
commit d5493fe6e6
3 changed files with 100 additions and 82 deletions
+5 -82
View File
@@ -24,6 +24,7 @@ use half::f16;
use crate::channels::ColorLayout;
use crate::decode::{Error, Limits, Result, SampleType, check_previews};
use crate::scanline;
fn err(e: impl std::fmt::Display) -> Error {
Error::Malformed(e.to_string())
@@ -152,49 +153,6 @@ fn seek_to(cur: &mut Cursor<&[u8]>, pos: u64) -> Result<()> {
// --- decompression ----------------------------------------------------------------------------
/// Undo the ZIP predictor: each byte was stored as its difference to the previous byte plus
/// 128 (so a zero difference is 0x80), modulo 256. Applies to the sample-count table and the
/// sample data alike.
fn differences_to_samples(buf: &mut [u8]) {
if let Some(&first) = buf.first() {
let mut prev = i16::from(first);
for b in buf.iter_mut().skip(1) {
let s = (prev + i16::from(*b) - 128) as u8;
prev = i16::from(s);
*b = s;
}
}
}
/// Undo the ZIP half-split: the stream is `[first half][second half]` with the second half
/// holding every second byte of the original; weave them back together. The scratch copy is
/// allocated fallibly, so a buffer that passed `max_bytes` but not the allocator is an error.
fn interleave_byte_blocks(buf: &mut [u8]) -> Result<()> {
let half = buf.len().div_ceil(2);
let mut out = alloc_zeroed::<u8>(buf.len())?;
let mut k = 0usize;
for i in 0..buf.len() / 2 {
let (a, b) = (buf.get(i).copied(), buf.get(half + i).copied());
if let (Some(a), Some(b), Some(pair)) = (a, b, out.get_mut(k..k + 2)) {
if let Some(byte) = pair.first_mut() {
*byte = a;
}
if let Some(byte) = pair.get_mut(1) {
*byte = b;
}
}
k += 2;
}
if buf.len() % 2 == 1 && half > 0 {
let last = buf.get(half - 1).copied();
if let (Some(last), Some(slot)) = (last, out.get_mut(k)) {
*slot = last;
}
}
buf.copy_from_slice(&out);
Ok(())
}
/// Make room in `out` for `extra` more bytes, `limit` in all. Grows by doubling like `Vec`,
/// but never reserves past `limit`: callers admit exactly that size against `max_bytes`, and
/// plain doubling reserved up to twice as much. Fallible, so a failed allocation is an error.
@@ -211,38 +169,8 @@ fn reserve_up_to(out: &mut Vec<u8>, extra: usize, limit: usize, what: &str) -> R
Ok(())
}
/// OpenEXR RLE tokens: a negative byte `n` means `-n` literal bytes follow, a non-negative
/// byte `n` means the next byte repeats `n + 1` times (as `ImfRle.cpp` and the `exr` crate).
fn rle_decode(src: &[u8], expected: usize, what: &str) -> Result<Vec<u8>> {
let mut out = Vec::new();
reserve_up_to(&mut out, expected.min(1 << 20), expected, what)?;
let mut i = 0;
while i < src.len() {
let count = src[i] as i8;
i += 1;
let n = if count < 0 { (-(count as i32)) as usize } else { count as usize + 1 };
if out.len().saturating_add(n) > expected {
return Err(err(format!("{what}: RLE data expands past {expected} bytes")));
}
reserve_up_to(&mut out, n, expected, what)?;
if count < 0 {
let end = i.saturating_add(n);
let run = src.get(i..end.min(src.len())).ok_or_else(|| err(format!("{what}: RLE run points past the compressed data")))?;
if run.len() != n {
return Err(err(format!("{what}: RLE run points past the compressed data")));
}
out.extend_from_slice(run);
i = end;
} else {
let b = src.get(i).copied().ok_or_else(|| err(format!("{what}: RLE run points past the compressed data")))?;
i += 1;
out.resize(out.len() + n, b);
}
}
if out.len() != expected {
return Err(err(format!("{what}: RLE data expands to {} bytes, expected {expected}", out.len())));
}
Ok(out)
fn scanline_error(what: &str, error: std::io::Error) -> Error {
if error.kind() == std::io::ErrorKind::OutOfMemory { Error::LimitExceeded(format!("{what}: {error}")) } else { err(format!("{what}: {error}")) }
}
/// Decompress `what` to exactly `expected` bytes. Like flat blocks, a payload that already
@@ -258,10 +186,7 @@ fn decompress(compression: Compression, packed: &[u8], expected: usize, what: &s
Compression::Uncompressed => Err(err(format!("{what}: expected {expected} uncompressed bytes, found {}", packed.len()))),
Compression::RLE => {
// Like ZIPS, RLE runs over the predicted, byte-split stream.
let mut out = rle_decode(packed, expected, what)?;
differences_to_samples(&mut out);
interleave_byte_blocks(&mut out)?;
Ok(out)
scanline::decode_rle(packed, expected).map_err(|error| scanline_error(what, error))
}
Compression::ZIP1 => {
// Inflate straight into `out`, grown with `reserve_up_to` rather than by
@@ -290,9 +215,7 @@ fn decompress(compression: Compression, packed: &[u8], expected: usize, what: &s
if out.len() != expected {
return Err(err(format!("{what}: zlib data expands to {} bytes, expected {expected}", out.len())));
}
differences_to_samples(&mut out);
interleave_byte_blocks(&mut out)?;
Ok(out)
scanline::undo_byte_prediction(out).map_err(|error| scanline_error(what, error))
}
other => Err(unsup(format!("deep data with {other:?} compression (NONE, RLE and ZIPS are supported)"))),
}
+3
View File
@@ -20,6 +20,9 @@
//! - [`stream`]: one flat scan-line part read block by block, for pictures larger than memory.
//! - [`cryptomatte_samples`]: a file's Cryptomatte layers and their per-pixel (ID, coverage) ranks.
/// Bounded RLE expansion and inverse byte prediction for EXR blocks.
pub mod scanline;
/// Scanline OpenEXR writer using a caller-supplied linearization table.
pub mod rgba8;
/// RFC 1950/1951 compression and bounded inflation for EXR chunks.
+92
View File
@@ -0,0 +1,92 @@
//! Byte transforms shared by flat and deep OpenEXR blocks.
use std::io::{Error, ErrorKind, Result};
/// Expand a compressed RLE block to exactly `expected` bytes and undo its
/// byte prediction. The caller checks its chunk limit and handles blocks
/// stored uncompressed before calling this function.
pub fn decode_rle(mut packed: &[u8], expected: usize) -> Result<Vec<u8>> {
let mut out = Vec::new();
while let Some((&count, rest)) = packed.split_first() {
let count = count as i8;
let literal = count < 0;
let run = if literal { usize::from(count.unsigned_abs()) } else { count as usize + 1 };
let Some(end) = out.len().checked_add(run).filter(|&end| end <= expected) else {
return Err(Error::new(ErrorKind::InvalidData, format!("RLE data expands past {expected} bytes")));
};
let (data, next) = rest
.split_at_checked(if literal { run } else { 1 })
.ok_or_else(|| Error::new(ErrorKind::UnexpectedEof, "RLE run points past the compressed data"))?;
if end > out.capacity() {
let target = out.capacity().saturating_mul(2).max(end).min(expected);
out.try_reserve_exact(target - out.len())
.map_err(|_| Error::new(ErrorKind::OutOfMemory, format!("cannot allocate {target} RLE bytes")))?;
}
if literal {
out.extend_from_slice(data);
} else if let Some(&value) = data.first() {
out.resize(end, value);
}
packed = next;
}
if out.len() != expected {
return Err(Error::new(ErrorKind::InvalidData, format!("RLE data expands to {} bytes, expected {expected}", out.len())));
}
undo_byte_prediction(out)
}
/// Undo RLE/ZIP's byte differences and even/odd split, returning the bytes
/// in their original order. The scratch allocation is fallible and no larger
/// than the already bounded decompressed block.
pub fn undo_byte_prediction(mut bytes: Vec<u8>) -> Result<Vec<u8>> {
if let Some((&mut first, differences)) = bytes.split_first_mut() {
let mut previous = first;
for byte in differences {
*byte = previous.wrapping_add(*byte).wrapping_sub(128);
previous = *byte;
}
}
let (even, odd) = bytes.split_at(bytes.len().div_ceil(2));
let mut out = Vec::new();
out.try_reserve_exact(bytes.len()).map_err(|_| Error::new(ErrorKind::OutOfMemory, format!("cannot allocate {} elements", bytes.len())))?;
for (&even, &odd) in even.iter().zip(odd) {
out.extend([even, odd]);
}
out.extend_from_slice(&even[odd.len()..]);
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rle_reverses_wrapping_prediction_and_an_odd_byte_split() {
// Literal 50, repeated delta 128 three times, then three literals.
let rle = [255, 50, 2, 128, 253, 100, 250, 129];
let expected = [50, 22, 50, 144, 50, 145, 50];
assert_eq!(decode_rle(&rle, 7).unwrap(), expected);
assert_eq!(undo_byte_prediction(vec![50, 128, 128, 128, 100, 250, 129]).unwrap(), expected);
}
#[test]
fn rle_handles_maximum_literal_and_repeat_counts() {
let mut rle = vec![128];
rle.extend([128; 128]);
rle.extend([127, 128]);
assert_eq!(decode_rle(&rle, 256).unwrap(), [128; 256]);
}
#[test]
fn rle_refuses_short_input_and_wrong_output_sizes() {
for block in [[255], [1]] {
assert_eq!(decode_rle(&block, 2).unwrap_err().kind(), ErrorKind::UnexpectedEof);
}
assert_eq!(decode_rle(&[1, 128], 1).unwrap_err().kind(), ErrorKind::InvalidData);
assert_eq!(decode_rle(&[0, 128], 2).unwrap_err().kind(), ErrorKind::InvalidData);
// A truncated stream does not reserve the declared output size up front.
assert_eq!(decode_rle(&[0, 128], usize::MAX).unwrap_err().kind(), ErrorKind::InvalidData);
}
}