Coverage Report

Created: 2026-05-16 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/exr-1.74.0/src/compression/pxr24.rs
Line
Count
Source
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
0
pub fn compress(channels: &ChannelList, bytes_ne: ByteVec, area: IntegerBounds) -> Result<ByteVec> {
43
0
    if bytes_ne.is_empty() { return Ok(Vec::new()); }
44
45
0
    let mut remaining_bytes_ne = bytes_ne.as_slice(); // TODO less allocation
46
47
0
    let bytes_per_pixel: usize = channels.list.iter()
48
0
        .map(|channel| match channel.sample_type {
49
0
            SampleType::F16 => 2,
50
0
            SampleType::F32 => 3,
51
0
            SampleType::U32 => 4,
52
0
        })
53
0
        .sum();
54
55
0
    let mut encoded_be = vec![0_u8; bytes_per_pixel * area.size.area()];
56
57
    {
58
0
        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
0
        for y in area.position.1..area.end().1 {
62
0
            for channel in &channels.list {
63
0
                if mod_p(y, usize_to_i32(channel.sampling.1, "sampling factor")?) != 0 { continue; }
64
65
0
                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
0
                match channel.sample_type {
75
                    SampleType::F16 => {
76
0
                        let out_byte_tuples = split_off_write_slice!().iter_mut()
77
0
                            .zip(split_off_write_slice!());
78
79
0
                        let mut previous_pixel: u32 = 0;
80
0
                        for (out_byte_0, out_byte_1) in out_byte_tuples {
81
0
                            let pixel = u16::read_from_native_endian(&mut remaining_bytes_ne)
82
0
                                .expect("failed to read from in-memory bytes") as u32;
83
0
84
0
                            let [byte_0, byte_1] = (pixel.wrapping_sub(previous_pixel) as u16)
85
0
                                .to_be_bytes();
86
0
87
0
                            *out_byte_0 = byte_0;
88
0
                            *out_byte_1 = byte_1;
89
0
                            previous_pixel = pixel;
90
0
                        }
91
                    },
92
93
                    SampleType::U32 => {
94
0
                        let out_byte_quadruplets = split_off_write_slice!().iter_mut()
95
0
                            .zip(split_off_write_slice!())
96
0
                            .zip(split_off_write_slice!())
97
0
                            .zip(split_off_write_slice!());
98
99
0
                        let mut previous_pixel: u32 = 0;
100
0
                        for (((out_byte_0, out_byte_1), out_byte_2), out_byte_3) in out_byte_quadruplets {
101
0
                            let pixel = u32::read_from_native_endian(&mut remaining_bytes_ne)
102
0
                                .expect("failed to read from in-memory bytes");
103
0
104
0
                            let [byte_0, byte_1, byte_2, byte_3] = pixel
105
0
                                .wrapping_sub(previous_pixel).to_be_bytes();
106
0
107
0
                            *out_byte_0 = byte_0;
108
0
                            *out_byte_1 = byte_1;
109
0
                            *out_byte_2 = byte_2;
110
0
                            *out_byte_3 = byte_3;
111
0
                            previous_pixel = pixel;
112
0
                        }
113
                    },
114
115
                    SampleType::F32 => {
116
0
                        let out_byte_triplets = split_off_write_slice!().iter_mut()
117
0
                            .zip(split_off_write_slice!())
118
0
                            .zip(split_off_write_slice!());
119
120
0
                        let mut previous_pixel: u32 = 0;
121
0
                        for ((out_byte_0, out_byte_1), out_byte_2) in out_byte_triplets {
122
0
                            let pixel = f32_to_f24(
123
0
                                f32::read_from_native_endian(&mut remaining_bytes_ne)
124
0
                                    .expect("failed to read from in-memory bytes")
125
0
                            );
126
0
127
0
                            let [_, byte_0, byte_1, byte_2] = pixel
128
0
                                .wrapping_sub(previous_pixel).to_be_bytes();
129
0
130
0
                            *out_byte_0 = byte_0;
131
0
                            *out_byte_1 = byte_1;
132
0
                            *out_byte_2 = byte_2;
133
0
                            previous_pixel = pixel;
134
0
                        }
135
                    },
136
                }
137
            }
138
        }
139
140
0
        debug_assert_eq!(write.len(), 0, "bytes left after compression");
141
    }
142
143
0
    Ok(miniz_oxide::deflate::compress_to_vec_zlib(encoded_be.as_slice(), 4))
144
0
}
145
146
0
pub fn decompress(channels: &ChannelList, bytes_le: ByteVec, area: IntegerBounds, expected_byte_size: usize, pedantic: bool) -> Result<ByteVec> {
147
0
    let options = zune_inflate::DeflateOptions::default().set_limit(expected_byte_size).set_size_hint(expected_byte_size);
148
0
    let mut decompressor = zune_inflate::DeflateDecoder::new_with_options(&bytes_le, options);
149
150
0
    let encoded_be = decompressor.decode_zlib()
151
0
        .map_err(|_| Error::invalid("zlib-compressed data malformed"))?; // TODO share code with zip?
152
153
0
    let mut encoded_be = encoded_be.as_slice();
154
0
    let mut out = Vec::with_capacity(expected_byte_size.min(2048*4));
155
156
0
    for y in area.position.1 .. area.end().1 {
157
0
        for channel in &channels.list {
158
0
            if mod_p(y, usize_to_i32(channel.sampling.1, "sampling")?) != 0 { continue; }
159
160
0
            let sample_count_x = channel.subsampled_resolution(area.size).0;
161
0
            let mut read_sample_line = ||{
162
0
                if sample_count_x > encoded_be.len() { return Err(Error::invalid("not enough data")) }
163
0
                let (samples, rest) = encoded_be.split_at(sample_count_x);
164
0
                encoded_be = rest;
165
0
                Ok(samples)
166
0
            };
167
168
169
0
            match channel.sample_type {
170
                SampleType::F16 => {
171
0
                    let sample_byte_pairs = read_sample_line()?.iter()
172
0
                        .zip(read_sample_line()?);
173
174
0
                    let mut pixel_accumulation: u32 = 0;
175
0
                    for (&in_byte_0, &in_byte_1) in sample_byte_pairs {
176
0
                        let difference = u16::from_be_bytes([in_byte_0, in_byte_1]) as u32;
177
0
                        pixel_accumulation = pixel_accumulation.overflowing_add(difference).0;
178
0
                        out.extend_from_slice(&(pixel_accumulation as u16).to_ne_bytes());
179
0
                    }
180
                },
181
182
                SampleType::U32 => {
183
0
                    let sample_byte_quads = read_sample_line()?.iter()
184
0
                        .zip(read_sample_line()?)
185
0
                        .zip(read_sample_line()?)
186
0
                        .zip(read_sample_line()?);
187
188
0
                    let mut pixel_accumulation: u32 = 0;
189
0
                    for (((&in_byte_0, &in_byte_1), &in_byte_2), &in_byte_3) in sample_byte_quads {
190
0
                        let difference = u32::from_be_bytes([in_byte_0, in_byte_1, in_byte_2, in_byte_3]);
191
0
                        pixel_accumulation = pixel_accumulation.overflowing_add(difference).0;
192
0
                        out.extend_from_slice(&pixel_accumulation.to_ne_bytes());
193
0
                    }
194
                },
195
196
                SampleType::F32 => {
197
0
                    let sample_byte_triplets = read_sample_line()?.iter()
198
0
                        .zip(read_sample_line()?).zip(read_sample_line()?);
199
200
0
                    let mut pixel_accumulation: u32 = 0;
201
0
                    for ((&in_byte_0, &in_byte_1), &in_byte_2) in sample_byte_triplets {
202
0
                        let difference = u32::from_be_bytes([in_byte_0, in_byte_1, in_byte_2, 0]);
203
0
                        pixel_accumulation = pixel_accumulation.overflowing_add(difference).0;
204
0
                        out.extend_from_slice(&pixel_accumulation.to_ne_bytes());
205
0
                    }
206
                }
207
            }
208
        }
209
    }
210
211
0
    if pedantic && !encoded_be.is_empty() {
212
0
        return Err(Error::invalid("too much data"));
213
0
    }
214
215
0
    Ok(out)
216
0
}
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
0
pub fn f32_to_f24(float: f32) -> u32 {
224
0
    let bits = float.to_bits();
225
226
0
    let sign = bits & 0x80000000;
227
0
    let exponent = bits & 0x7f800000;
228
0
    let mantissa = bits & 0x007fffff;
229
230
0
    let result = if exponent == 0x7f800000 {
231
0
        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
0
            let mantissa = mantissa >> 8;
240
0
            (exponent >> 8) | mantissa | if mantissa == 0 { 1 } else { 0 }
241
        }
242
        else { // F is an infinity.
243
0
            exponent >> 8
244
        }
245
    }
246
    else { // F is finite, round the significand to 15 bits.
247
0
        let result = ((exponent | mantissa) + (mantissa & 0x00000080)) >> 8;
248
249
0
        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
0
            (exponent | mantissa) >> 8
256
        }
257
        else {
258
0
            result
259
        }
260
    };
261
262
0
    return (sign >> 8) | result;
263
0
}