| 1 | |
| 2 | //! Lossy compression for F32 data, but lossless compression for U32 and F16 data. |
| 3 | // see https://github.com/AcademySoftwareFoundation/openexr/blob/master/OpenEXR/IlmImf/ImfPxr24Compressor.cpp |
| 4 | |
| 5 | // This compressor is based on source code that was contributed to |
| 6 | // OpenEXR by Pixar Animation Studios. The compression method was |
| 7 | // developed by Loren Carpenter. |
| 8 | |
| 9 | |
| 10 | // The compressor preprocesses the pixel data to reduce entropy, and then calls zlib. |
| 11 | // Compression of HALF and UINT channels is lossless, but compressing |
| 12 | // FLOAT channels is lossy: 32-bit floating-point numbers are converted |
| 13 | // to 24 bits by rounding the significand to 15 bits. |
| 14 | // |
| 15 | // When the compressor is invoked, the caller has already arranged |
| 16 | // the pixel data so that the values for each channel appear in a |
| 17 | // contiguous block of memory. The compressor converts the pixel |
| 18 | // values to unsigned integers: For UINT, this is a no-op. HALF |
| 19 | // values are simply re-interpreted as 16-bit integers. FLOAT |
| 20 | // values are converted to 24 bits, and the resulting bit patterns |
| 21 | // are interpreted as integers. The compressor then replaces each |
| 22 | // value with the difference between the value and its left neighbor. |
| 23 | // This turns flat fields in the image into zeroes, and ramps into |
| 24 | // strings of similar values. Next, each difference is split into |
| 25 | // 2, 3 or 4 bytes, and the bytes are transposed so that all the |
| 26 | // most significant bytes end up in a contiguous block, followed |
| 27 | // by the second most significant bytes, and so on. The resulting |
| 28 | // string of bytes is compressed with zlib. |
| 29 | |
| 30 | use super::*; |
| 31 | |
| 32 | use crate::error::Result; |
| 33 | use lebe::io::ReadPrimitive; |
| 34 | |
| 35 | |
| 36 | // scanline decompreroussion tine, see https://github.com/openexr/openexr/blob/master/OpenEXR/IlmImf/ImfScanLineInputFile.cpp |
| 37 | // 1. Uncompress the data, if necessary (If the line is uncompressed, it's in XDR format, regardless of the compressor's output format.) |
| 38 | // 3. Convert one scan line's worth of pixel data back from the machine-independent representation |
| 39 | // 4. Fill the frame buffer with pixel data, respective to sampling and whatnot |
| 40 | |
| 41 | |
| 42 | pub fn compress(channels: &ChannelList, bytes_ne: ByteVec, area: IntegerBounds) -> Result<ByteVec> { |
| 43 | if bytes_ne.is_empty() { return Ok(Vec::new()); } |
| 44 | |
| 45 | let mut remaining_bytes_ne = bytes_ne.as_slice(); // TODO less allocation |
| 46 | |
| 47 | let bytes_per_pixel: usize = channels.list.iter() |
| 48 | .map(|channel| match channel.sample_type { |
| 49 | SampleType::F16 => 2, |
| 50 | SampleType::F32 => 3, |
| 51 | SampleType::U32 => 4, |
| 52 | }) |
| 53 | .sum(); |
| 54 | |
| 55 | let mut encoded_be = vec![0_u8; bytes_per_pixel * area.size.area()]; |
| 56 | |
| 57 | { |
| 58 | let mut write = encoded_be.as_mut_slice(); |
| 59 | |
| 60 | // TODO this loop should be an iterator in the `IntegerBounds` class, as it is used in all compression methods |
| 61 | for y in area.position.1..area.end().1 { |
| 62 | for channel in &channels.list { |
| 63 | if mod_p(y, usize_to_i32(channel.sampling.1, "sampling factor")?) != 0 { continue; } |
| 64 | |
| 65 | let sample_count_x = channel.subsampled_resolution(area.size).0; |
| 66 | |
| 67 | // this apparently can't be a closure in Rust 1.43 due to borrowing ambiguity |
| 68 | macro_rules! split_off_write_slice { () => {{ |
| 69 | let (slice, rest) = write.split_at_mut(sample_count_x); |
| 70 | write = rest; |
| 71 | slice |
| 72 | }}; } |
| 73 | |
| 74 | match channel.sample_type { |
| 75 | SampleType::F16 => { |
| 76 | let out_byte_tuples = split_off_write_slice!().iter_mut() |
| 77 | .zip(split_off_write_slice!()); |
| 78 | |
| 79 | let mut previous_pixel: u32 = 0; |
| 80 | for (out_byte_0, out_byte_1) in out_byte_tuples { |
| 81 | let pixel = u16::read_from_native_endian(&mut remaining_bytes_ne) |
| 82 | .expect("failed to read from in-memory bytes") as u32; |
| 83 | |
| 84 | let [byte_0, byte_1] = (pixel.wrapping_sub(previous_pixel) as u16) |
| 85 | .to_be_bytes(); |
| 86 | |
| 87 | *out_byte_0 = byte_0; |
| 88 | *out_byte_1 = byte_1; |
| 89 | previous_pixel = pixel; |
| 90 | } |
| 91 | }, |
| 92 | |
| 93 | SampleType::U32 => { |
| 94 | let out_byte_quadruplets = split_off_write_slice!().iter_mut() |
| 95 | .zip(split_off_write_slice!()) |
| 96 | .zip(split_off_write_slice!()) |
| 97 | .zip(split_off_write_slice!()); |
| 98 | |
| 99 | let mut previous_pixel: u32 = 0; |
| 100 | for (((out_byte_0, out_byte_1), out_byte_2), out_byte_3) in out_byte_quadruplets { |
| 101 | let pixel = u32::read_from_native_endian(&mut remaining_bytes_ne) |
| 102 | .expect("failed to read from in-memory bytes"); |
| 103 | |
| 104 | let [byte_0, byte_1, byte_2, byte_3] = pixel |
| 105 | .wrapping_sub(previous_pixel).to_be_bytes(); |
| 106 | |
| 107 | *out_byte_0 = byte_0; |
| 108 | *out_byte_1 = byte_1; |
| 109 | *out_byte_2 = byte_2; |
| 110 | *out_byte_3 = byte_3; |
| 111 | previous_pixel = pixel; |
| 112 | } |
| 113 | }, |
| 114 | |
| 115 | SampleType::F32 => { |
| 116 | let out_byte_triplets = split_off_write_slice!().iter_mut() |
| 117 | .zip(split_off_write_slice!()) |
| 118 | .zip(split_off_write_slice!()); |
| 119 | |
| 120 | let mut previous_pixel: u32 = 0; |
| 121 | for ((out_byte_0, out_byte_1), out_byte_2) in out_byte_triplets { |
| 122 | let pixel = f32_to_f24( |
| 123 | f32::read_from_native_endian(&mut remaining_bytes_ne) |
| 124 | .expect("failed to read from in-memory bytes") |
| 125 | ); |
| 126 | |
| 127 | let [_, byte_0, byte_1, byte_2] = pixel |
| 128 | .wrapping_sub(previous_pixel).to_be_bytes(); |
| 129 | |
| 130 | *out_byte_0 = byte_0; |
| 131 | *out_byte_1 = byte_1; |
| 132 | *out_byte_2 = byte_2; |
| 133 | previous_pixel = pixel; |
| 134 | } |
| 135 | }, |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | debug_assert_eq!(write.len(), 0, "bytes left after compression"); |
| 141 | } |
| 142 | |
| 143 | Ok(miniz_oxide::deflate::compress_to_vec_zlib(encoded_be.as_slice(), 4)) |
| 144 | } |
| 145 | |
| 146 | pub fn decompress(channels: &ChannelList, bytes_le: ByteVec, area: IntegerBounds, expected_byte_size: usize, pedantic: bool) -> Result<ByteVec> { |
| 147 | let options = zune_inflate::DeflateOptions::default().set_limit(expected_byte_size).set_size_hint(expected_byte_size); |
| 148 | let mut decompressor = zune_inflate::DeflateDecoder::new_with_options(&bytes_le, options); |
| 149 | |
| 150 | let encoded_be = decompressor.decode_zlib() |
| 151 | .map_err(|_| Error::invalid("zlib-compressed data malformed"))?; // TODO share code with zip? |
| 152 | |
| 153 | let mut encoded_be = encoded_be.as_slice(); |
| 154 | let mut out = Vec::with_capacity(expected_byte_size.min(2048*4)); |
| 155 | |
| 156 | for y in area.position.1 .. area.end().1 { |
| 157 | for channel in &channels.list { |
| 158 | if mod_p(y, usize_to_i32(channel.sampling.1, "sampling")?) != 0 { continue; } |
| 159 | |
| 160 | let sample_count_x = channel.subsampled_resolution(area.size).0; |
| 161 | let mut read_sample_line = ||{ |
| 162 | if sample_count_x > encoded_be.len() { return Err(Error::invalid("not enough data")) } |
| 163 | let (samples, rest) = encoded_be.split_at(sample_count_x); |
| 164 | encoded_be = rest; |
| 165 | Ok(samples) |
| 166 | }; |
| 167 | |
| 168 | |
| 169 | match channel.sample_type { |
| 170 | SampleType::F16 => { |
| 171 | let sample_byte_pairs = read_sample_line()?.iter() |
| 172 | .zip(read_sample_line()?); |
| 173 | |
| 174 | let mut pixel_accumulation: u32 = 0; |
| 175 | for (&in_byte_0, &in_byte_1) in sample_byte_pairs { |
| 176 | let difference = u16::from_be_bytes([in_byte_0, in_byte_1]) as u32; |
| 177 | pixel_accumulation = pixel_accumulation.overflowing_add(difference).0; |
| 178 | out.extend_from_slice(&(pixel_accumulation as u16).to_ne_bytes()); |
| 179 | } |
| 180 | }, |
| 181 | |
| 182 | SampleType::U32 => { |
| 183 | let sample_byte_quads = read_sample_line()?.iter() |
| 184 | .zip(read_sample_line()?) |
| 185 | .zip(read_sample_line()?) |
| 186 | .zip(read_sample_line()?); |
| 187 | |
| 188 | let mut pixel_accumulation: u32 = 0; |
| 189 | for (((&in_byte_0, &in_byte_1), &in_byte_2), &in_byte_3) in sample_byte_quads { |
| 190 | let difference = u32::from_be_bytes([in_byte_0, in_byte_1, in_byte_2, in_byte_3]); |
| 191 | pixel_accumulation = pixel_accumulation.overflowing_add(difference).0; |
| 192 | out.extend_from_slice(&pixel_accumulation.to_ne_bytes()); |
| 193 | } |
| 194 | }, |
| 195 | |
| 196 | SampleType::F32 => { |
| 197 | let sample_byte_triplets = read_sample_line()?.iter() |
| 198 | .zip(read_sample_line()?).zip(read_sample_line()?); |
| 199 | |
| 200 | let mut pixel_accumulation: u32 = 0; |
| 201 | for ((&in_byte_0, &in_byte_1), &in_byte_2) in sample_byte_triplets { |
| 202 | let difference = u32::from_be_bytes([in_byte_0, in_byte_1, in_byte_2, 0]); |
| 203 | pixel_accumulation = pixel_accumulation.overflowing_add(difference).0; |
| 204 | out.extend_from_slice(&pixel_accumulation.to_ne_bytes()); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | if pedantic && !encoded_be.is_empty() { |
| 212 | return Err(Error::invalid("too much data")); |
| 213 | } |
| 214 | |
| 215 | Ok(out) |
| 216 | } |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | /// Conversion from 32-bit to 24-bit floating-point numbers. |
| 222 | /// Reverse conversion is just a simple 8-bit left shift. |
| 223 | pub fn f32_to_f24(float: f32) -> u32 { |
| 224 | let bits = float.to_bits(); |
| 225 | |
| 226 | let sign = bits & 0x80000000; |
| 227 | let exponent = bits & 0x7f800000; |
| 228 | let mantissa = bits & 0x007fffff; |
| 229 | |
| 230 | let result = if exponent == 0x7f800000 { |
| 231 | if mantissa != 0 { |
| 232 | // F is a NAN; we preserve the sign bit and |
| 233 | // the 15 leftmost bits of the significand, |
| 234 | // with one exception: If the 15 leftmost |
| 235 | // bits are all zero, the NAN would turn |
| 236 | // into an infinity, so we have to set at |
| 237 | // least one bit in the significand. |
| 238 | |
| 239 | let mantissa = mantissa >> 8; |
| 240 | (exponent >> 8) | mantissa | if mantissa == 0 { 1 } else { 0 } |
| 241 | } |
| 242 | else { // F is an infinity. |
| 243 | exponent >> 8 |
| 244 | } |
| 245 | } |
| 246 | else { // F is finite, round the significand to 15 bits. |
| 247 | let result = ((exponent | mantissa) + (mantissa & 0x00000080)) >> 8; |
| 248 | |
| 249 | if result >= 0x7f8000 { |
| 250 | // F was close to FLT_MAX, and the significand was |
| 251 | // rounded up, resulting in an exponent overflow. |
| 252 | // Avoid the overflow by truncating the significand |
| 253 | // instead of rounding it. |
| 254 | |
| 255 | (exponent | mantissa) >> 8 |
| 256 | } |
| 257 | else { |
| 258 | result |
| 259 | } |
| 260 | }; |
| 261 | |
| 262 | return (sign >> 8) | result; |
| 263 | } |