Coverage Report

Created: 2026-09-01 07:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/exr-1.74.2/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 it is
64
        // 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 borrowing ambiguity
74
                macro_rules! split_off_write_slice {
75
                    () => {{
76
                        let (slice, rest) = write.split_at_mut(sample_count_x);
77
                        write = rest;
78
                        slice
79
                    }};
80
                }
81
82
0
                match channel.sample_type {
83
                    SampleType::F16 => {
84
0
                        let out_byte_tuples =
85
0
                            split_off_write_slice!().iter_mut().zip(split_off_write_slice!());
86
87
0
                        let mut previous_pixel: u32 = 0;
88
0
                        for (out_byte_0, out_byte_1) in out_byte_tuples {
89
0
                            let pixel = u16::read_from_native_endian(&mut remaining_bytes_ne)
90
0
                                .expect("failed to read from in-memory bytes")
91
0
                                as u32;
92
0
93
0
                            let [byte_0, byte_1] =
94
0
                                (pixel.wrapping_sub(previous_pixel) as u16).to_be_bytes();
95
0
96
0
                            *out_byte_0 = byte_0;
97
0
                            *out_byte_1 = byte_1;
98
0
                            previous_pixel = pixel;
99
0
                        }
100
                    }
101
102
                    SampleType::U32 => {
103
0
                        let out_byte_quadruplets = split_off_write_slice!()
104
0
                            .iter_mut()
105
0
                            .zip(split_off_write_slice!())
106
0
                            .zip(split_off_write_slice!())
107
0
                            .zip(split_off_write_slice!());
108
109
0
                        let mut previous_pixel: u32 = 0;
110
0
                        for (((out_byte_0, out_byte_1), out_byte_2), out_byte_3) in
111
0
                            out_byte_quadruplets
112
0
                        {
113
0
                            let pixel = u32::read_from_native_endian(&mut remaining_bytes_ne)
114
0
                                .expect("failed to read from in-memory bytes");
115
0
116
0
                            let [byte_0, byte_1, byte_2, byte_3] =
117
0
                                pixel.wrapping_sub(previous_pixel).to_be_bytes();
118
0
119
0
                            *out_byte_0 = byte_0;
120
0
                            *out_byte_1 = byte_1;
121
0
                            *out_byte_2 = byte_2;
122
0
                            *out_byte_3 = byte_3;
123
0
                            previous_pixel = pixel;
124
0
                        }
125
                    }
126
127
                    SampleType::F32 => {
128
0
                        let out_byte_triplets = split_off_write_slice!()
129
0
                            .iter_mut()
130
0
                            .zip(split_off_write_slice!())
131
0
                            .zip(split_off_write_slice!());
132
133
0
                        let mut previous_pixel: u32 = 0;
134
0
                        for ((out_byte_0, out_byte_1), out_byte_2) in out_byte_triplets {
135
0
                            let pixel = f32_to_f24(
136
0
                                f32::read_from_native_endian(&mut remaining_bytes_ne)
137
0
                                    .expect("failed to read from in-memory bytes"),
138
0
                            );
139
0
140
0
                            let [_, byte_0, byte_1, byte_2] =
141
0
                                pixel.wrapping_sub(previous_pixel).to_be_bytes();
142
0
143
0
                            *out_byte_0 = byte_0;
144
0
                            *out_byte_1 = byte_1;
145
0
                            *out_byte_2 = byte_2;
146
0
                            previous_pixel = pixel;
147
0
                        }
148
                    }
149
                }
150
            }
151
        }
152
153
0
        debug_assert_eq!(write.len(), 0, "bytes left after compression");
154
    }
155
156
0
    Ok(miniz_oxide::deflate::compress_to_vec_zlib(encoded_be.as_slice(), 4))
157
0
}
158
159
1
pub fn decompress(
160
1
    channels: &ChannelList,
161
1
    bytes_le: ByteVec,
162
1
    area: IntegerBounds,
163
1
    expected_byte_size: usize,
164
1
    pedantic: bool,
165
1
) -> Result<ByteVec> {
166
1
    let options = zune_inflate::DeflateOptions::default()
167
1
        .set_limit(expected_byte_size)
168
1
        .set_size_hint(expected_byte_size);
169
1
    let mut decompressor = zune_inflate::DeflateDecoder::new_with_options(&bytes_le, options);
170
171
0
    let encoded_be =
172
1
        decompressor.decode_zlib().map_err(|_| Error::invalid("zlib-compressed data malformed"))?; // TODO share code with zip?
173
174
0
    let mut encoded_be = encoded_be.as_slice();
175
0
    let mut out = Vec::with_capacity(expected_byte_size.min(2048 * 4));
176
177
0
    for y in area.position.1..area.end().1 {
178
0
        for channel in &channels.list {
179
0
            if mod_p(y, usize_to_i32(channel.sampling.1, "sampling")?) != 0 {
180
0
                continue;
181
0
            }
182
183
0
            let sample_count_x = channel.subsampled_resolution(area.size).0;
184
0
            let mut read_sample_line = || {
185
0
                if sample_count_x > encoded_be.len() {
186
0
                    return Err(Error::invalid("not enough data"));
187
0
                }
188
0
                let (samples, rest) = encoded_be.split_at(sample_count_x);
189
0
                encoded_be = rest;
190
0
                Ok(samples)
191
0
            };
192
193
0
            match channel.sample_type {
194
                SampleType::F16 => {
195
0
                    let sample_byte_pairs = read_sample_line()?.iter().zip(read_sample_line()?);
196
197
0
                    let mut pixel_accumulation: u32 = 0;
198
0
                    for (&in_byte_0, &in_byte_1) in sample_byte_pairs {
199
0
                        let difference = u16::from_be_bytes([in_byte_0, in_byte_1]) as u32;
200
0
                        pixel_accumulation = pixel_accumulation.overflowing_add(difference).0;
201
0
                        out.extend_from_slice(&(pixel_accumulation as u16).to_ne_bytes());
202
0
                    }
203
                }
204
205
                SampleType::U32 => {
206
0
                    let sample_byte_quads = read_sample_line()?
207
0
                        .iter()
208
0
                        .zip(read_sample_line()?)
209
0
                        .zip(read_sample_line()?)
210
0
                        .zip(read_sample_line()?);
211
212
0
                    let mut pixel_accumulation: u32 = 0;
213
0
                    for (((&in_byte_0, &in_byte_1), &in_byte_2), &in_byte_3) in sample_byte_quads {
214
0
                        let difference =
215
0
                            u32::from_be_bytes([in_byte_0, in_byte_1, in_byte_2, in_byte_3]);
216
0
                        pixel_accumulation = pixel_accumulation.overflowing_add(difference).0;
217
0
                        out.extend_from_slice(&pixel_accumulation.to_ne_bytes());
218
0
                    }
219
                }
220
221
                SampleType::F32 => {
222
0
                    let sample_byte_triplets = read_sample_line()?
223
0
                        .iter()
224
0
                        .zip(read_sample_line()?)
225
0
                        .zip(read_sample_line()?);
226
227
0
                    let mut pixel_accumulation: u32 = 0;
228
0
                    for ((&in_byte_0, &in_byte_1), &in_byte_2) in sample_byte_triplets {
229
0
                        let difference = u32::from_be_bytes([in_byte_0, in_byte_1, in_byte_2, 0]);
230
0
                        pixel_accumulation = pixel_accumulation.overflowing_add(difference).0;
231
0
                        out.extend_from_slice(&pixel_accumulation.to_ne_bytes());
232
0
                    }
233
                }
234
            }
235
        }
236
    }
237
238
0
    if pedantic && !encoded_be.is_empty() {
239
0
        return Err(Error::invalid("too much data"));
240
0
    }
241
242
0
    Ok(out)
243
1
}
244
245
/// Conversion from 32-bit to 24-bit floating-point numbers.
246
/// Reverse conversion is just a simple 8-bit left shift.
247
0
pub fn f32_to_f24(float: f32) -> u32 {
248
0
    let bits = float.to_bits();
249
250
0
    let sign = bits & 0x80000000;
251
0
    let exponent = bits & 0x7f800000;
252
0
    let mantissa = bits & 0x007fffff;
253
254
0
    let result = if exponent == 0x7f800000 {
255
0
        if mantissa != 0 {
256
            // F is a NAN; we preserve the sign bit and
257
            // the 15 leftmost bits of the significand,
258
            // with one exception: If the 15 leftmost
259
            // bits are all zero, the NAN would turn
260
            // into an infinity, so we have to set at
261
            // least one bit in the significand.
262
263
0
            let mantissa = mantissa >> 8;
264
0
            (exponent >> 8)
265
0
                | mantissa
266
0
                | if mantissa == 0 {
267
0
                    1
268
                } else {
269
0
                    0
270
                }
271
        } else {
272
            // F is an infinity.
273
0
            exponent >> 8
274
        }
275
    } else {
276
        // F is finite, round the significand to 15 bits.
277
0
        let result = ((exponent | mantissa) + (mantissa & 0x00000080)) >> 8;
278
279
0
        if result >= 0x7f8000 {
280
            // F was close to FLT_MAX, and the significand was
281
            // rounded up, resulting in an exponent overflow.
282
            // Avoid the overflow by truncating the significand
283
            // instead of rounding it.
284
285
0
            (exponent | mantissa) >> 8
286
        } else {
287
0
            result
288
        }
289
    };
290
291
0
    return (sign >> 8) | result;
292
0
}