use super::*;
use crate::error::Result;
use lebe::io::ReadPrimitive;
#[cfg_attr(target_endian = "big", allow(unused, unreachable_code))]
pub fn compress(channels: &ChannelList, remaining_bytes: ByteVec, area: IntegerBounds) -> Result<ByteVec> {
#[cfg(target_endian = "big")] {
return Err(Error::unsupported(
"PXR24 compression method not supported yet on big endian processor architecture"
))
}
if remaining_bytes.is_empty() { return Ok(Vec::new()); }
let remaining_bytes = super::convert_current_to_little_endian(remaining_bytes, channels, area);
let mut remaining_bytes = remaining_bytes.as_slice();
let bytes_per_pixel: usize = channels.list.iter()
.map(|channel| match channel.sample_type {
SampleType::F16 => 2, SampleType::F32 => 3, SampleType::U32 => 4,
})
.sum();
let mut raw = vec![0_u8; bytes_per_pixel * area.size.area()];
{
let mut write = raw.as_mut_slice();
for y in area.position.1..area.end().1 {
for channel in &channels.list {
if mod_p(y, usize_to_i32(channel.sampling.1)) != 0 { continue; }
let sample_count_x = channel.subsampled_resolution(area.size).0;
macro_rules! split_off_write_slice { () => {{
let (slice, rest) = write.split_at_mut(sample_count_x);
write = rest;
slice
}}; }
let mut previous_pixel: u32 = 0;
match channel.sample_type {
SampleType::F16 => {
let out_byte_tuples = split_off_write_slice!().iter_mut()
.zip(split_off_write_slice!());
for (out_byte_0, out_byte_1) in out_byte_tuples {
let pixel = u16::read_from_native_endian(&mut remaining_bytes).unwrap() as u32;
let [byte_1, byte_0] = (pixel.wrapping_sub(previous_pixel) as u16).to_ne_bytes();
*out_byte_0 = byte_0;
*out_byte_1 = byte_1;
previous_pixel = pixel;
}
},
SampleType::U32 => {
let out_byte_quadruplets = split_off_write_slice!().iter_mut()
.zip(split_off_write_slice!())
.zip(split_off_write_slice!())
.zip(split_off_write_slice!());
for (((out_byte_0, out_byte_1), out_byte_2), out_byte_3) in out_byte_quadruplets {
let pixel = u32::read_from_native_endian(&mut remaining_bytes).unwrap();
let [byte_3, byte_2, byte_1, byte_0] = pixel.wrapping_sub(previous_pixel).to_ne_bytes();
*out_byte_0 = byte_0;
*out_byte_1 = byte_1;
*out_byte_2 = byte_2;
*out_byte_3 = byte_3;
previous_pixel = pixel;
}
},
SampleType::F32 => {
let out_byte_triplets = split_off_write_slice!().iter_mut()
.zip(split_off_write_slice!())
.zip(split_off_write_slice!());
for ((out_byte_0, out_byte_1), out_byte_2) in out_byte_triplets {
let pixel = f32_to_f24(f32::read_from_native_endian(&mut remaining_bytes).unwrap());
let [byte_2, byte_1, byte_0, _] = pixel.wrapping_sub(previous_pixel).to_ne_bytes();
previous_pixel = pixel;
*out_byte_0 = byte_0;
*out_byte_1 = byte_1;
*out_byte_2 = byte_2;
}
},
}
}
}
debug_assert_eq!(write.len(), 0, "bytes left after compression");
}
Ok(miniz_oxide::deflate::compress_to_vec_zlib(raw.as_slice(), 4))
}
#[cfg_attr(target_endian = "big", allow(unused, unreachable_code))]
pub fn decompress(channels: &ChannelList, bytes: ByteVec, area: IntegerBounds, expected_byte_size: usize, pedantic: bool) -> Result<ByteVec> {
#[cfg(target_endian = "big")] {
return Err(Error::unsupported(
"PXR24 decompression method not supported yet on big endian processor architecture"
))
}
let options = zune_inflate::DeflateOptions::default().set_limit(expected_byte_size).set_size_hint(expected_byte_size);
let mut decoder = zune_inflate::DeflateDecoder::new_with_options(&bytes, options);
let raw = decoder.decode_zlib()
.map_err(|_| Error::invalid("zlib-compressed data malformed"))?;
let mut read = raw.as_slice();
let mut out = Vec::with_capacity(expected_byte_size.min(2048*4));
for y in area.position.1 .. area.end().1 {
for channel in &channels.list {
if mod_p(y, usize_to_i32(channel.sampling.1)) != 0 { continue; }
let sample_count_x = channel.subsampled_resolution(area.size).0;
let mut read_sample_line = ||{
if sample_count_x > read.len() { return Err(Error::invalid("not enough data")) }
let (samples, rest) = read.split_at(sample_count_x);
read = rest;
Ok(samples)
};
let mut pixel_accumulation: u32 = 0;
match channel.sample_type {
SampleType::F16 => {
let sample_byte_pairs = read_sample_line()?.iter()
.zip(read_sample_line()?);
for (&in_byte_0, &in_byte_1) in sample_byte_pairs {
let difference = u16::from_ne_bytes([in_byte_1, in_byte_0]) as u32;
pixel_accumulation = pixel_accumulation.overflowing_add(difference).0;
out.extend_from_slice(&(pixel_accumulation as u16).to_ne_bytes());
}
},
SampleType::U32 => {
let sample_byte_quads = read_sample_line()?.iter()
.zip(read_sample_line()?)
.zip(read_sample_line()?)
.zip(read_sample_line()?);
for (((&in_byte_0, &in_byte_1), &in_byte_2), &in_byte_3) in sample_byte_quads {
let difference = u32::from_ne_bytes([in_byte_3, in_byte_2, in_byte_1, in_byte_0]);
pixel_accumulation = pixel_accumulation.overflowing_add(difference).0;
out.extend_from_slice(&pixel_accumulation.to_ne_bytes());
}
},
SampleType::F32 => {
let sample_byte_triplets = read_sample_line()?.iter()
.zip(read_sample_line()?).zip(read_sample_line()?);
for ((&in_byte_0, &in_byte_1), &in_byte_2) in sample_byte_triplets {
let difference = u32::from_ne_bytes([0, in_byte_2, in_byte_1, in_byte_0]);
pixel_accumulation = pixel_accumulation.overflowing_add(difference).0;
out.extend_from_slice(&pixel_accumulation.to_ne_bytes());
}
}
}
}
}
if pedantic && !read.is_empty() {
return Err(Error::invalid("too much data"));
}
Ok(super::convert_little_endian_to_current(out, channels, area))
}
pub fn f32_to_f24(float: f32) -> u32 {
let bits = float.to_bits();
let sign = bits & 0x80000000;
let exponent = bits & 0x7f800000;
let mantissa = bits & 0x007fffff;
let result = if exponent == 0x7f800000 {
if mantissa != 0 {
let mantissa = mantissa >> 8;
(exponent >> 8) | mantissa | if mantissa == 0 { 1 } else { 0 }
}
else { exponent >> 8
}
}
else { let result = ((exponent | mantissa) + (mantissa & 0x00000080)) >> 8;
if result >= 0x7f8000 {
(exponent | mantissa) >> 8
}
else {
result
}
};
return (sign >> 8) | result;
}