| 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 superrust-analyzer cargo exr 1.74.0 compression/::*; |
| 31 | |
| 32 | use craterust-analyzer cargo exr 1.74.0 crate/::errorrust-analyzer cargo exr 1.74.0 error/::Resultrust-analyzer cargo exr 1.74.0 error/Result#; |
| 33 | use leberust-analyzer cargo lebe 0.5.2 crate/::iorust-analyzer cargo lebe 0.5.2 io/::ReadPrimitiverust-analyzer cargo lebe 0.5.2 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 compressrust-analyzer cargo exr 1.74.0 compression/pxr24/compress().(channels: &ChannelListrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelList#, bytes_ne: ByteVecrust-analyzer cargo exr 1.74.0 compression/ByteVec#, area: IntegerBoundsrust-analyzer cargo exr 1.74.0 meta/attribute/IntegerBounds#) -> Resultrust-analyzer cargo exr 1.74.0 error/Result#<ByteVecrust-analyzer cargo exr 1.74.0 compression/ByteVec#> { |
| 43 | if bytes_ne.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() { return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().()); } |
| 44 | |
| 45 | let mut remaining_bytes_ne = bytes_ne.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().(); // TODO less allocation |
| 46 | |
| 47 | let bytes_per_pixel: usize = channels.listrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelList#list..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 48 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|channel| match channel.sample_typerust-analyzer cargo exr 1.74.0 meta/attribute/ChannelDescription#sample_type. { |
| 49 | SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::F16rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#F16# => 2, |
| 50 | SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::F32rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#F32# => 3, |
| 51 | SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::U32rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#U32# => 4, |
| 52 | }) |
| 53 | .sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().(); |
| 54 | |
| 55 | let mut encoded_be = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0_u8; bytes_per_pixel *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). area.sizerust-analyzer cargo exr 1.74.0 meta/attribute/IntegerBounds#size..arearust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]area().()]; |
| 56 | |
| 57 | { |
| 58 | let mut write = encoded_be.as_mut_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]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.positionrust-analyzer cargo exr 1.74.0 meta/attribute/IntegerBounds#position..1rust-analyzer cargo exr 1.74.0 math/Vec2#1...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#area.endrust-analyzer cargo exr 1.74.0 meta/attribute/impl#[IntegerBounds]end().().1rust-analyzer cargo exr 1.74.0 math/Vec2#1. { |
| 62 | for channel in &channels.listrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelList#list. { |
| 63 | if mod_prust-analyzer cargo exr 1.74.0 compression/mod_p().(y, usize_to_i32rust-analyzer cargo exr 1.74.0 error/usize_to_i32().(channel.samplingrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelDescription#sampling..1rust-analyzer cargo exr 1.74.0 math/Vec2#1., "sampling factor")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne().0 { continue; } |
| 64 | |
| 65 | let sample_count_x = channel.subsampled_resolutionrust-analyzer cargo exr 1.74.0 meta/attribute/impl#[ChannelDescription]subsampled_resolution().(area.sizerust-analyzer cargo exr 1.74.0 meta/attribute/IntegerBounds#size.).0rust-analyzer cargo exr 1.74.0 math/Vec2#0.; |
| 66 | |
| 67 | // this apparently can't be a closure in Rust 1.43 due to borrowing ambiguity |
| 68 | macro_rules! split_off_write_slicerust-analyzer cargo exr 1.74.0 compression/pxr24/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_typerust-analyzer cargo exr 1.74.0 meta/attribute/ChannelDescription#sample_type. { |
| 75 | SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::F16rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#F16# => { |
| 76 | let out_byte_tuples = split_off_write_slicerust-analyzer cargo exr 1.74.0 compression/pxr24/split_off_write_slice!!().iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().() |
| 77 | .ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(split_off_write_slicerust-analyzer cargo exr 1.74.0 compression/pxr24/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_endianrust-analyzer cargo lebe 0.5.2 io/ReadPrimitive#read_from_native_endian().(&mut remaining_bytes_ne) |
| 82 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("failed to read from in-memory bytes") as u32; |
| 83 | |
| 84 | let [byte_0, byte_1] = (pixel.wrapping_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]wrapping_sub().(previous_pixel) as u16) |
| 85 | .to_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u16]to_be_bytes().(); |
| 86 | |
| 87 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().out_byte_0 = byte_0; |
| 88 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().out_byte_1 = byte_1; |
| 89 | previous_pixel = pixel; |
| 90 | } |
| 91 | }, |
| 92 | |
| 93 | SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::U32rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#U32# => { |
| 94 | let out_byte_quadruplets = split_off_write_slicerust-analyzer cargo exr 1.74.0 compression/pxr24/split_off_write_slice!!().iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().() |
| 95 | .ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(split_off_write_slicerust-analyzer cargo exr 1.74.0 compression/pxr24/split_off_write_slice!!()) |
| 96 | .ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(split_off_write_slicerust-analyzer cargo exr 1.74.0 compression/pxr24/split_off_write_slice!!()) |
| 97 | .ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(split_off_write_slicerust-analyzer cargo exr 1.74.0 compression/pxr24/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_endianrust-analyzer cargo lebe 0.5.2 io/ReadPrimitive#read_from_native_endian().(&mut remaining_bytes_ne) |
| 102 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("failed to read from in-memory bytes"); |
| 103 | |
| 104 | let [byte_0, byte_1, byte_2, byte_3] = pixel |
| 105 | .wrapping_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]wrapping_sub().(previous_pixel).to_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]to_be_bytes().(); |
| 106 | |
| 107 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().out_byte_0 = byte_0; |
| 108 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().out_byte_1 = byte_1; |
| 109 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().out_byte_2 = byte_2; |
| 110 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().out_byte_3 = byte_3; |
| 111 | previous_pixel = pixel; |
| 112 | } |
| 113 | }, |
| 114 | |
| 115 | SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::F32rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#F32# => { |
| 116 | let out_byte_triplets = split_off_write_slicerust-analyzer cargo exr 1.74.0 compression/pxr24/split_off_write_slice!!().iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().() |
| 117 | .ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(split_off_write_slicerust-analyzer cargo exr 1.74.0 compression/pxr24/split_off_write_slice!!()) |
| 118 | .ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(split_off_write_slicerust-analyzer cargo exr 1.74.0 compression/pxr24/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_f24rust-analyzer cargo exr 1.74.0 compression/pxr24/f32_to_f24().( |
| 123 | f32::read_from_native_endianrust-analyzer cargo lebe 0.5.2 io/ReadPrimitive#read_from_native_endian().(&mut remaining_bytes_ne) |
| 124 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("failed to read from in-memory bytes") |
| 125 | ); |
| 126 | |
| 127 | let [_, byte_0, byte_1, byte_2] = pixel |
| 128 | .wrapping_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]wrapping_sub().(previous_pixel).to_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]to_be_bytes().(); |
| 129 | |
| 130 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().out_byte_0 = byte_0; |
| 131 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().out_byte_1 = byte_1; |
| 132 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().out_byte_2 = byte_2; |
| 133 | previous_pixel = pixel; |
| 134 | } |
| 135 | }, |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | debug_assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert_eq!!(write.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), 0, "bytes left after compression"); |
| 141 | } |
| 142 | |
| 143 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(miniz_oxiderust-analyzer cargo miniz_oxide 0.8.9 crate/::deflaterust-analyzer cargo miniz_oxide 0.8.9 deflate/::compress_to_vec_zlibrust-analyzer cargo miniz_oxide 0.8.9 deflate/compress_to_vec_zlib().(encoded_be.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().(), 4)) |
| 144 | } |
| 145 | |
| 146 | pub fn decompressrust-analyzer cargo exr 1.74.0 compression/pxr24/decompress().(channels: &ChannelListrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelList#, bytes_le: ByteVecrust-analyzer cargo exr 1.74.0 compression/ByteVec#, area: IntegerBoundsrust-analyzer cargo exr 1.74.0 meta/attribute/IntegerBounds#, expected_byte_size: usize, pedantic: bool) -> Resultrust-analyzer cargo exr 1.74.0 error/Result#<ByteVecrust-analyzer cargo exr 1.74.0 compression/ByteVec#> { |
| 147 | let options = zune_inflaterust-analyzer cargo zune-inflate 0.2.54 crate/::DeflateOptionsrust-analyzer cargo zune-inflate 0.2.54 decoder/DeflateOptions#::defaultrust-analyzer cargo zune-inflate 0.2.54 decoder/impl#[DeflateOptions][Default]default().().set_limitrust-analyzer cargo zune-inflate 0.2.54 decoder/impl#[DeflateOptions]set_limit().(expected_byte_size).set_size_hintrust-analyzer cargo zune-inflate 0.2.54 decoder/impl#[DeflateOptions]set_size_hint().(expected_byte_size); |
| 148 | let mut decompressor = zune_inflaterust-analyzer cargo zune-inflate 0.2.54 crate/::DeflateDecoderrust-analyzer cargo zune-inflate 0.2.54 decoder/DeflateDecoder#::new_with_optionsrust-analyzer cargo zune-inflate 0.2.54 decoder/impl#[`DeflateDecoder<'a>`]new_with_options().(&bytes_le, options); |
| 149 | |
| 150 | let encoded_be = decompressor.decode_zlibrust-analyzer cargo zune-inflate 0.2.54 decoder/impl#[`DeflateDecoder<'a>`]decode_zlib().() |
| 151 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| Errorrust-analyzer cargo exr 1.74.0 error/Error#::invalidrust-analyzer cargo exr 1.74.0 error/impl#[Error]invalid().("zlib-compressed data malformed"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; // TODO share code with zip? |
| 152 | |
| 153 | let mut encoded_be = encoded_be.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().(); |
| 154 | let mut out = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(expected_byte_size.minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#min().(2048*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().4)); |
| 155 | |
| 156 | for y in area.positionrust-analyzer cargo exr 1.74.0 meta/attribute/IntegerBounds#position..1rust-analyzer cargo exr 1.74.0 math/Vec2#1. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range# rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#area.endrust-analyzer cargo exr 1.74.0 meta/attribute/impl#[IntegerBounds]end().().1rust-analyzer cargo exr 1.74.0 math/Vec2#1. { |
| 157 | for channel in &channels.listrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelList#list. { |
| 158 | if mod_prust-analyzer cargo exr 1.74.0 compression/mod_p().(y, usize_to_i32rust-analyzer cargo exr 1.74.0 error/usize_to_i32().(channel.samplingrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelDescription#sampling..1rust-analyzer cargo exr 1.74.0 math/Vec2#1., "sampling")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne().0 { continue; } |
| 159 | |
| 160 | let sample_count_x = channel.subsampled_resolutionrust-analyzer cargo exr 1.74.0 meta/attribute/impl#[ChannelDescription]subsampled_resolution().(area.sizerust-analyzer cargo exr 1.74.0 meta/attribute/IntegerBounds#size.).0rust-analyzer cargo exr 1.74.0 math/Vec2#0.; |
| 161 | let mut read_sample_line = ||{ |
| 162 | if sample_count_x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().encoded_be.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() { return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo exr 1.74.0 error/Error#::invalidrust-analyzer cargo exr 1.74.0 error/impl#[Error]invalid().("not enough data")) } |
| 163 | let (samples, rest) = encoded_be.split_atrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]split_at().(sample_count_x); |
| 164 | encoded_be = rest; |
| 165 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(samples) |
| 166 | }; |
| 167 | |
| 168 | |
| 169 | match channel.sample_typerust-analyzer cargo exr 1.74.0 meta/attribute/ChannelDescription#sample_type. { |
| 170 | SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::F16rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#F16# => { |
| 171 | let sample_byte_pairs = read_sample_line()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 172 | .ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(read_sample_line()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().); |
| 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_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u16]from_be_bytes().([in_byte_0, in_byte_1]) as u32; |
| 177 | pixel_accumulation = pixel_accumulation.overflowing_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]overflowing_add().(difference).0; |
| 178 | out.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(&(pixel_accumulation as u16).to_ne_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u16]to_ne_bytes().()); |
| 179 | } |
| 180 | }, |
| 181 | |
| 182 | SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::U32rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#U32# => { |
| 183 | let sample_byte_quads = read_sample_line()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 184 | .ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(read_sample_line()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().) |
| 185 | .ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(read_sample_line()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().) |
| 186 | .ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(read_sample_line()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().); |
| 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_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]from_be_bytes().([in_byte_0, in_byte_1, in_byte_2, in_byte_3]); |
| 191 | pixel_accumulation = pixel_accumulation.overflowing_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]overflowing_add().(difference).0; |
| 192 | out.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(&pixel_accumulation.to_ne_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]to_ne_bytes().()); |
| 193 | } |
| 194 | }, |
| 195 | |
| 196 | SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::F32rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#F32# => { |
| 197 | let sample_byte_triplets = read_sample_line()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 198 | .ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(read_sample_line()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().).ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(read_sample_line()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().); |
| 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_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]from_be_bytes().([in_byte_0, in_byte_1, in_byte_2, 0]); |
| 203 | pixel_accumulation = pixel_accumulation.overflowing_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]overflowing_add().(difference).0; |
| 204 | out.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(&pixel_accumulation.to_ne_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]to_ne_bytes().()); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | if pedantic && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().encoded_be.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() { |
| 212 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo exr 1.74.0 error/Error#::invalidrust-analyzer cargo exr 1.74.0 error/impl#[Error]invalid().("too much data")); |
| 213 | } |
| 214 | |
| 215 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#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_f24rust-analyzer cargo exr 1.74.0 compression/pxr24/f32_to_f24().(float: f32) -> u32 { |
| 224 | let bits = float.to_bitsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]to_bits().(); |
| 225 | |
| 226 | let sign = bits rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().0x80000000; |
| 227 | let exponent = bits rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().0x7f800000; |
| 228 | let mantissa = bits rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().0x007fffff; |
| 229 | |
| 230 | let result = if exponent rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().0x7f800000 { |
| 231 | if mantissa rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().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 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shr<i32>`]shr().>>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shr<i32>`]shr(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shr<i32>`]shr().8; |
| 240 | (exponent rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shr<i32>`]shr().>>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shr<i32>`]shr(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shr<i32>`]shr().8) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor().mantissa rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor().if mantissa rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().0 { 1 } else { 0 } |
| 241 | } |
| 242 | else { // F is an infinity. |
| 243 | exponent rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shr<i32>`]shr().>>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shr<i32>`]shr(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shr<i32>`]shr().8 |
| 244 | } |
| 245 | } |
| 246 | else { // F is finite, round the significand to 15 bits. |
| 247 | let result = ((exponent rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor().mantissa) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().(mantissa rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().0x00000080)) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shr<i32>`]shr().>>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shr<i32>`]shr(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shr<i32>`]shr().8; |
| 248 | |
| 249 | if result rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]ge().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 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor().mantissa) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shr<i32>`]shr().>>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shr<i32>`]shr(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shr<i32>`]shr().8 |
| 256 | } |
| 257 | else { |
| 258 | result |
| 259 | } |
| 260 | }; |
| 261 | |
| 262 | return (sign rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shr<i32>`]shr().>>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shr<i32>`]shr(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shr<i32>`]shr().8) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitOr<Self>`]bitor().result; |
| 263 | } |