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