Debian Codegraph @

src/compression/mod.rs

raw symbols SCIP
1
2 //! Contains the compression attribute definition
3 //! and methods to compress and decompress data.
4
5
6 // private modules make non-breaking changes easier
7 mod ziprust-analyzer cargo exr 1.74.0 compression/zip/;
8 mod rlerust-analyzer cargo exr 1.74.0 compression/rle/;
9 mod pizrust-analyzer cargo exr 1.74.0 compression/piz/;
10 mod pxr24rust-analyzer cargo exr 1.74.0 compression/pxr24/;
11 mod b44rust-analyzer cargo exr 1.74.0 compression/b44/;
12
13
14 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::convertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/::TryIntorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryInto#;
15 use craterust-analyzer cargo exr 1.74.0 crate/::metarust-analyzer cargo exr 1.74.0 meta/::attributerust-analyzer cargo exr 1.74.0 meta/attribute/::{IntegerBoundsrust-analyzer cargo exr 1.74.0 meta/attribute/IntegerBounds#, SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#, ChannelListrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelList#};
16 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#, Errorrust-analyzer cargo exr 1.74.0 error/Error#, usize_to_i32rust-analyzer cargo exr 1.74.0 error/usize_to_i32()., UnitResultrust-analyzer cargo exr 1.74.0 error/UnitResult#};
17 use craterust-analyzer cargo exr 1.74.0 crate/::metarust-analyzer cargo exr 1.74.0 meta/::headerrust-analyzer cargo exr 1.74.0 meta/header/::Headerrust-analyzer cargo exr 1.74.0 meta/header/Header#;
18
19
20 /// A byte vector.
21 pub type ByteVecrust-analyzer cargo exr 1.74.0 compression/ByteVec# = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>;
22
23 /// A byte slice.
24 pub type Bytesrust-analyzer cargo exr 1.74.0 compression/Bytes#<'s> = &'s [u8];
25
26 /// Specifies which compression method to use.
27 /// Use uncompressed data for fastest loading and writing speeds.
28 /// Use RLE compression for fast loading and writing with slight memory savings.
29 /// Use ZIP compression for slow processing with large memory savings.
30 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
31 pub enum Compressionrust-analyzer cargo exr 1.74.0 compression/Compression# {
32
33 /// Store uncompressed values.
34 /// Produces large files that can be read and written very quickly.
35 /// Consider using RLE instead, as it provides some compression with almost equivalent speed.
36 Uncompressedrust-analyzer cargo exr 1.74.0 compression/Compression#Uncompressed#,
37
38 /// Produces slightly smaller files
39 /// that can still be read and written rather quickly.
40 /// The compressed file size is usually between 60 and 75 percent of the uncompressed size.
41 /// Works best for images with large flat areas, such as masks and abstract graphics.
42 /// This compression method is lossless.
43 RLErust-analyzer cargo exr 1.74.0 compression/Compression#RLE#,
44
45 /// Uses ZIP compression to compress each line. Slowly produces small images
46 /// which can be read with moderate speed. This compression method is lossless.
47 /// Might be slightly faster but larger than `ZIP16´.
48 ZIP1rust-analyzer cargo exr 1.74.0 compression/Compression#ZIP1#, // TODO ZIP { individual_lines: bool, compression_level: Option<u8> } // TODO specify zip compression level?
49
50 /// Uses ZIP compression to compress blocks of 16 lines. Slowly produces small images
51 /// which can be read with moderate speed. This compression method is lossless.
52 /// Might be slightly slower but smaller than `ZIP1´.
53 ZIP16rust-analyzer cargo exr 1.74.0 compression/Compression#ZIP16#, // TODO collapse with ZIP1
54
55 /// PIZ compression works well for noisy and natural images. Works better with larger tiles.
56 /// Only supported for flat images, but not for deep data.
57 /// This compression method is lossless.
58 // A wavelet transform is applied to the pixel data, and the result is Huffman-
59 // encoded. This scheme tends to provide the best compression ratio for the types of
60 // images that are typically processed at Industrial Light & Magic. Files are
61 // compressed and decompressed at roughly the same speed. For photographic
62 // images with film grain, the files are reduced to between 35 and 55 percent of their
63 // uncompressed size.
64 // PIZ compression works well for scan-line based files, and also for tiled files with
65 // large tiles, but small tiles do not shrink much. (PIZ-compressed data start with a
66 // relatively long header; if the input to the compressor is short, adding the header
67 // tends to offset any size reduction of the input.)
68 PIZrust-analyzer cargo exr 1.74.0 compression/Compression#PIZ#,
69
70 /// Like `ZIP1`, but reduces precision of `f32` images to `f24`.
71 /// Therefore, this is lossless compression for `f16` and `u32` data, lossy compression for `f32` data.
72 /// This compression method works well for depth
73 /// buffers and similar images, where the possible range of values is very large, but
74 /// where full 32-bit floating-point accuracy is not necessary. Rounding improves
75 /// compression significantly by eliminating the pixels' 8 least significant bits, which
76 /// tend to be very noisy, and therefore difficult to compress.
77 /// This produces really small image files. Only supported for flat images, not for deep data.
78 // After reducing 32-bit floating-point data to 24 bits by rounding (while leaving 16-bit
79 // floating-point data unchanged), differences between horizontally adjacent pixels
80 // are compressed with zlib, similar to ZIP. PXR24 compression preserves image
81 // channels of type HALF and UINT exactly, but the relative error of FLOAT data
82 // increases to about ???.
83 PXR24rust-analyzer cargo exr 1.74.0 compression/Compression#PXR24#, // TODO specify zip compression level?
84
85 /// This is a lossy compression method for f16 images.
86 /// It's the predecessor of the `B44A` compression,
87 /// which has improved compression rates for uniformly colored areas.
88 /// You should probably use `B44A` instead of the plain `B44`.
89 ///
90 /// Only supported for flat images, not for deep data.
91 // lossy 4-by-4 pixel block compression,
92 // flat fields are compressed more
93 // Channels of type HALF are split into blocks of four by four pixels or 32 bytes. Each
94 // block is then packed into 14 bytes, reducing the data to 44 percent of their
95 // uncompressed size. When B44 compression is applied to RGB images in
96 // combination with luminance/chroma encoding (see below), the size of the
97 // compressed pixels is about 22 percent of the size of the original RGB data.
98 // Channels of type UINT or FLOAT are not compressed.
99 // Decoding is fast enough to allow real-time playback of B44-compressed OpenEXR
100 // image sequences on commodity hardware.
101 // The size of a B44-compressed file depends on the number of pixels in the image,
102 // but not on the data in the pixels. All images with the same resolution and the same
103 // set of channels have the same size. This can be advantageous for systems that
104 // support real-time playback of image sequences; the predictable file size makes it
105 // easier to allocate space on storage media efficiently.
106 // B44 compression is only supported for flat images.
107 B44rust-analyzer cargo exr 1.74.0 compression/Compression#B44#, // TODO B44 { optimize_uniform_areas: bool }
108
109 /// This is a lossy compression method for f16 images.
110 /// All f32 and u32 channels will be stored without compression.
111 /// All the f16 pixels are divided into 4x4 blocks.
112 /// Each block is then compressed as a whole.
113 ///
114 /// The 32 bytes of a block will require only ~14 bytes after compression,
115 /// independent of the actual pixel contents. With chroma subsampling,
116 /// a block will be compressed to ~7 bytes.
117 /// Uniformly colored blocks will be compressed to ~3 bytes.
118 ///
119 /// The 512 bytes of an f32 block will not be compressed at all.
120 ///
121 /// Should be fast enough for realtime playback.
122 /// Only supported for flat images, not for deep data.
123 B44Arust-analyzer cargo exr 1.74.0 compression/Compression#B44A#, // TODO collapse with B44
124
125 /// __This lossy compression is not yet supported by this implementation.__
126 // lossy DCT based compression, in blocks
127 // of 32 scanlines. More efficient for partial buffer access.
128 DWAArust-analyzer cargo exr 1.74.0 compression/Compression#DWAA#(Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<f32>), // TODO does this have a default value? make this non optional? default Compression Level setting is 45.0
129
130 /// __This lossy compression is not yet supported by this implementation.__
131 // lossy DCT based compression, in blocks
132 // of 256 scanlines. More efficient space
133 // wise and faster to decode full frames
134 // than DWAA_COMPRESSION.
135 DWABrust-analyzer cargo exr 1.74.0 compression/Compression#DWAB#(Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<f32>), // TODO collapse with DWAA. default Compression Level setting is 45.0
136
137 /// __This lossy compression is not yet supported by this implementation.__
138 // High-Throughput JPEG 2000 (32 lines)
139 HTJ2K32rust-analyzer cargo exr 1.74.0 compression/Compression#HTJ2K32#,
140
141 /// __This lossy compression is not yet supported by this implementation.__
142 // High-Throughput JPEG 2000 (256 lines)
143 HTJ2K256rust-analyzer cargo exr 1.74.0 compression/Compression#HTJ2K256#,
144 }
145
146 impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Compressionrust-analyzer cargo exr 1.74.0 compression/Compression# {
147 fn fmtrust-analyzer cargo exr 1.74.0 compression/impl#[Compression][Display]fmt().(&self, formatter: &mut stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
148 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(formatter, "{} compression", match self {
149 Compressionrust-analyzer cargo exr 1.74.0 compression/Compression#::Uncompressedrust-analyzer cargo exr 1.74.0 compression/Compression#Uncompressed# => "no",
150 Compressionrust-analyzer cargo exr 1.74.0 compression/Compression#::RLErust-analyzer cargo exr 1.74.0 compression/Compression#RLE# => "rle",
151 Compressionrust-analyzer cargo exr 1.74.0 compression/Compression#::ZIP1rust-analyzer cargo exr 1.74.0 compression/Compression#ZIP1# => "zip line",
152 Compressionrust-analyzer cargo exr 1.74.0 compression/Compression#::ZIP16rust-analyzer cargo exr 1.74.0 compression/Compression#ZIP16# => "zip block",
153 Compressionrust-analyzer cargo exr 1.74.0 compression/Compression#::B44rust-analyzer cargo exr 1.74.0 compression/Compression#B44# => "b44",
154 Compressionrust-analyzer cargo exr 1.74.0 compression/Compression#::B44Arust-analyzer cargo exr 1.74.0 compression/Compression#B44A# => "b44a",
155 Compressionrust-analyzer cargo exr 1.74.0 compression/Compression#::DWAArust-analyzer cargo exr 1.74.0 compression/Compression#DWAA#(_) => "dwaa",
156 Compressionrust-analyzer cargo exr 1.74.0 compression/Compression#::DWABrust-analyzer cargo exr 1.74.0 compression/Compression#DWAB#(_) => "dwab",
157 Compressionrust-analyzer cargo exr 1.74.0 compression/Compression#::PIZrust-analyzer cargo exr 1.74.0 compression/Compression#PIZ# => "piz",
158 Compressionrust-analyzer cargo exr 1.74.0 compression/Compression#::PXR24rust-analyzer cargo exr 1.74.0 compression/Compression#PXR24# => "pxr24",
159 Compressionrust-analyzer cargo exr 1.74.0 compression/Compression#::HTJ2K32rust-analyzer cargo exr 1.74.0 compression/Compression#HTJ2K32# => "ht j2k 32",
160 Compressionrust-analyzer cargo exr 1.74.0 compression/Compression#::HTJ2K256rust-analyzer cargo exr 1.74.0 compression/Compression#HTJ2K256# => "ht j2k 256",
161 })
162 }
163 }
164
165
166
167 impl Compressionrust-analyzer cargo exr 1.74.0 compression/Compression# {
168
169 /// Compress the image section, converting from native endian into with little-endian format.
170 pub fn compress_image_section_to_lerust-analyzer cargo exr 1.74.0 compression/impl#[Compression]compress_image_section_to_le().(self, header: &Headerrust-analyzer cargo exr 1.74.0 meta/header/Header#, uncompressed_native_endian: ByteVecrust-analyzer cargo exr 1.74.0 compression/ByteVec#, pixel_section: 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#> {
171 let max_tile_size = header.max_block_pixel_sizerust-analyzer cargo exr 1.74.0 meta/header/impl#[Header]max_block_pixel_size().();
172
173 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(pixel_section.validaterust-analyzer cargo exr 1.74.0 meta/attribute/impl#[IntegerBounds]validate().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(max_tile_size)).is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().(), "decompress tile coordinate bug");
174 if header.deeprust-analyzer cargo exr 1.74.0 meta/header/Header#deep. { assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(self.supports_deep_datarust-analyzer cargo exr 1.74.0 compression/impl#[Compression]supports_deep_data().()) }
175
176 use selfrust-analyzer cargo exr 1.74.0 compression/::Compressionrust-analyzer cargo exr 1.74.0 compression/Compression#::*;
177 let compressed_little_endian = match self {
178 Uncompressedrust-analyzer cargo exr 1.74.0 compression/Compression#Uncompressed# => {
179 return convert_current_to_little_endianrust-analyzer cargo exr 1.74.0 compression/convert_current_to_little_endian().(
180 uncompressed_native_endian, &header.channelsrust-analyzer cargo exr 1.74.0 meta/header/Header#channels., pixel_section
181 )
182 },
183
184 // we need to clone here, because we might have to fallback to the uncompressed data later (when compressed data is larger than raw data)
185 ZIP16rust-analyzer cargo exr 1.74.0 compression/Compression#ZIP16# => ziprust-analyzer cargo exr 1.74.0 compression/zip/::compress_bytesrust-analyzer cargo exr 1.74.0 compression/zip/compress_bytes().(&header.channelsrust-analyzer cargo exr 1.74.0 meta/header/Header#channels., uncompressed_native_endian.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().(), pixel_section),
186 ZIP1rust-analyzer cargo exr 1.74.0 compression/Compression#ZIP1# => ziprust-analyzer cargo exr 1.74.0 compression/zip/::compress_bytesrust-analyzer cargo exr 1.74.0 compression/zip/compress_bytes().(&header.channelsrust-analyzer cargo exr 1.74.0 meta/header/Header#channels., uncompressed_native_endian.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().(), pixel_section),
187 RLErust-analyzer cargo exr 1.74.0 compression/Compression#RLE# => rlerust-analyzer cargo exr 1.74.0 compression/rle/::compress_bytesrust-analyzer cargo exr 1.74.0 compression/rle/compress_bytes().(&header.channelsrust-analyzer cargo exr 1.74.0 meta/header/Header#channels., uncompressed_native_endian.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().(), pixel_section),
188 PIZrust-analyzer cargo exr 1.74.0 compression/Compression#PIZ# => pizrust-analyzer cargo exr 1.74.0 compression/piz/::compressrust-analyzer cargo exr 1.74.0 compression/piz/compress().(&header.channelsrust-analyzer cargo exr 1.74.0 meta/header/Header#channels., uncompressed_native_endian.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().(), pixel_section),
189 PXR24rust-analyzer cargo exr 1.74.0 compression/Compression#PXR24# => pxr24rust-analyzer cargo exr 1.74.0 compression/pxr24/::compressrust-analyzer cargo exr 1.74.0 compression/pxr24/compress().(&header.channelsrust-analyzer cargo exr 1.74.0 meta/header/Header#channels., uncompressed_native_endian.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().(), pixel_section),
190 B44rust-analyzer cargo exr 1.74.0 compression/Compression#B44# => b44rust-analyzer cargo exr 1.74.0 compression/b44/::compressrust-analyzer cargo exr 1.74.0 compression/b44/compress().(&header.channelsrust-analyzer cargo exr 1.74.0 meta/header/Header#channels., uncompressed_native_endian.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().(), pixel_section, false),
191 B44Arust-analyzer cargo exr 1.74.0 compression/Compression#B44A# => b44rust-analyzer cargo exr 1.74.0 compression/b44/::compressrust-analyzer cargo exr 1.74.0 compression/b44/compress().(&header.channelsrust-analyzer cargo exr 1.74.0 meta/header/Header#channels., uncompressed_native_endian.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().(), pixel_section, true),
192 _ => 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#::unsupportedrust-analyzer cargo exr 1.74.0 error/impl#[Error]unsupported().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("yet unimplemented compression method: {}", self)))
193 };
194
195 let compressed_little_endian = compressed_little_endian.map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_|
196 Errorrust-analyzer cargo exr 1.74.0 error/Error#::invalidrust-analyzer cargo exr 1.74.0 error/impl#[Error]invalid().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("pixels cannot be compressed ({})", self))
197 )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
198
199 if self rust-analyzer cargo exr 1.74.0 compression/impl#[Compression][`PartialEq<Self>`]eq().==rust-analyzer cargo exr 1.74.0 compression/impl#[Compression][`PartialEq<Self>`]eq(). rust-analyzer cargo exr 1.74.0 compression/impl#[Compression][`PartialEq<Self>`]eq().Uncompressedrust-analyzer cargo exr 1.74.0 compression/Compression#Uncompressed# || compressed_little_endian.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().uncompressed_native_endian.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() {
200 // only write compressed if it actually is smaller than raw
201 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(compressed_little_endian)
202 }
203 else {
204 // if we do not use compression, manually convert uncompressed data
205 convert_current_to_little_endianrust-analyzer cargo exr 1.74.0 compression/convert_current_to_little_endian().(uncompressed_native_endian, &header.channelsrust-analyzer cargo exr 1.74.0 meta/header/Header#channels., pixel_section)
206 }
207 }
208
209 /// Decompress the image section from bytes of little-endian format, returning native-endian format.
210 pub fn decompress_image_section_from_lerust-analyzer cargo exr 1.74.0 compression/impl#[Compression]decompress_image_section_from_le().(self, header: &Headerrust-analyzer cargo exr 1.74.0 meta/header/Header#, compressed_le: ByteVecrust-analyzer cargo exr 1.74.0 compression/ByteVec#, pixel_section: IntegerBoundsrust-analyzer cargo exr 1.74.0 meta/attribute/IntegerBounds#, pedantic: bool) -> Resultrust-analyzer cargo exr 1.74.0 error/Result#<ByteVecrust-analyzer cargo exr 1.74.0 compression/ByteVec#> {
211 let max_tile_size = header.max_block_pixel_sizerust-analyzer cargo exr 1.74.0 meta/header/impl#[Header]max_block_pixel_size().();
212
213 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(pixel_section.validaterust-analyzer cargo exr 1.74.0 meta/attribute/impl#[IntegerBounds]validate().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(max_tile_size)).is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().(), "decompress tile coordinate bug");
214 if header.deeprust-analyzer cargo exr 1.74.0 meta/header/Header#deep. { assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(self.supports_deep_datarust-analyzer cargo exr 1.74.0 compression/impl#[Compression]supports_deep_data().()) }
215
216 let expected_byte_size = pixel_section.sizerust-analyzer cargo exr 1.74.0 meta/attribute/IntegerBounds#size..arearust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]area().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().header.channelsrust-analyzer cargo exr 1.74.0 meta/header/Header#channels..bytes_per_pixelrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelList#bytes_per_pixel.; // FIXME this needs to account for subsampling anywhere
217
218 // note: always true where self == Uncompressed
219 if compressed_le.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().expected_byte_size {
220 // the compressed data was larger than the raw data, so the small raw data has been written
221 convert_little_endian_to_currentrust-analyzer cargo exr 1.74.0 compression/convert_little_endian_to_current().(compressed_le, &header.channelsrust-analyzer cargo exr 1.74.0 meta/header/Header#channels., pixel_section)
222 }
223 else {
224 use selfrust-analyzer cargo exr 1.74.0 compression/::Compressionrust-analyzer cargo exr 1.74.0 compression/Compression#::*;
225 let bytes_ne = match self {
226 Uncompressedrust-analyzer cargo exr 1.74.0 compression/Compression#Uncompressed# => convert_little_endian_to_currentrust-analyzer cargo exr 1.74.0 compression/convert_little_endian_to_current().(compressed_le, &header.channelsrust-analyzer cargo exr 1.74.0 meta/header/Header#channels., pixel_section),
227 ZIP16rust-analyzer cargo exr 1.74.0 compression/Compression#ZIP16# => ziprust-analyzer cargo exr 1.74.0 compression/zip/::decompress_bytesrust-analyzer cargo exr 1.74.0 compression/zip/decompress_bytes().(&header.channelsrust-analyzer cargo exr 1.74.0 meta/header/Header#channels., compressed_le, pixel_section, expected_byte_size, pedantic),
228 ZIP1rust-analyzer cargo exr 1.74.0 compression/Compression#ZIP1# => ziprust-analyzer cargo exr 1.74.0 compression/zip/::decompress_bytesrust-analyzer cargo exr 1.74.0 compression/zip/decompress_bytes().(&header.channelsrust-analyzer cargo exr 1.74.0 meta/header/Header#channels., compressed_le, pixel_section, expected_byte_size, pedantic),
229 RLErust-analyzer cargo exr 1.74.0 compression/Compression#RLE# => rlerust-analyzer cargo exr 1.74.0 compression/rle/::decompress_bytesrust-analyzer cargo exr 1.74.0 compression/rle/decompress_bytes().(&header.channelsrust-analyzer cargo exr 1.74.0 meta/header/Header#channels., compressed_le, pixel_section, expected_byte_size, pedantic),
230 PIZrust-analyzer cargo exr 1.74.0 compression/Compression#PIZ# => pizrust-analyzer cargo exr 1.74.0 compression/piz/::decompressrust-analyzer cargo exr 1.74.0 compression/piz/decompress().(&header.channelsrust-analyzer cargo exr 1.74.0 meta/header/Header#channels., compressed_le, pixel_section, expected_byte_size, pedantic),
231 PXR24rust-analyzer cargo exr 1.74.0 compression/Compression#PXR24# => pxr24rust-analyzer cargo exr 1.74.0 compression/pxr24/::decompressrust-analyzer cargo exr 1.74.0 compression/pxr24/decompress().(&header.channelsrust-analyzer cargo exr 1.74.0 meta/header/Header#channels., compressed_le, pixel_section, expected_byte_size, pedantic),
232 B44rust-analyzer cargo exr 1.74.0 compression/Compression#B44# | B44Arust-analyzer cargo exr 1.74.0 compression/Compression#B44A# => b44rust-analyzer cargo exr 1.74.0 compression/b44/::decompressrust-analyzer cargo exr 1.74.0 compression/b44/decompress().(&header.channelsrust-analyzer cargo exr 1.74.0 meta/header/Header#channels., compressed_le, pixel_section, expected_byte_size, pedantic),
233 _ => 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#::unsupportedrust-analyzer cargo exr 1.74.0 error/impl#[Error]unsupported().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("yet unimplemented compression method: {}", self)))
234 };
235
236 // map all errors to compression errors
237 let bytes_ne = bytes_ne
238 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|decompression_error| match decompression_error {
239 Errorrust-analyzer cargo exr 1.74.0 error/Error#::NotSupportedrust-analyzer cargo exr 1.74.0 error/Error#NotSupported#(message) =>
240 Errorrust-analyzer cargo exr 1.74.0 error/Error#::unsupportedrust-analyzer cargo exr 1.74.0 error/impl#[Error]unsupported().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("yet unimplemented compression special case ({})", message)),
241
242 error => Errorrust-analyzer cargo exr 1.74.0 error/Error#::invalidrust-analyzer cargo exr 1.74.0 error/impl#[Error]invalid().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
243 "compressed {:?} data ({})",
244 self, error.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()
245 )),
246 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
247
248 if bytes_ne.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().expected_byte_size {
249 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().("decompressed data"))
250 }
251
252 else { Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(bytes_ne) }
253 }
254 }
255
256 /// For scan line images and deep scan line images, one or more scan lines may be
257 /// stored together as a scan line block. The number of scan lines per block
258 /// depends on how the pixel data are compressed.
259 pub fn scan_lines_per_blockrust-analyzer cargo exr 1.74.0 compression/impl#[Compression]scan_lines_per_block().(self) -> usize {
260 use selfrust-analyzer cargo exr 1.74.0 compression/::Compressionrust-analyzer cargo exr 1.74.0 compression/Compression#::*;
261 match self {
262 Uncompressedrust-analyzer cargo exr 1.74.0 compression/Compression#Uncompressed# | RLErust-analyzer cargo exr 1.74.0 compression/Compression#RLE# | ZIP1rust-analyzer cargo exr 1.74.0 compression/Compression#ZIP1# => 1,
263 ZIP16rust-analyzer cargo exr 1.74.0 compression/Compression#ZIP16# | PXR24rust-analyzer cargo exr 1.74.0 compression/Compression#PXR24# => 16,
264 PIZrust-analyzer cargo exr 1.74.0 compression/Compression#PIZ# | B44rust-analyzer cargo exr 1.74.0 compression/Compression#B44# | B44Arust-analyzer cargo exr 1.74.0 compression/Compression#B44A# | DWAArust-analyzer cargo exr 1.74.0 compression/Compression#DWAA#(_) | HTJ2K32rust-analyzer cargo exr 1.74.0 compression/Compression#HTJ2K32# => 32,
265 DWABrust-analyzer cargo exr 1.74.0 compression/Compression#DWAB#(_) | HTJ2K256rust-analyzer cargo exr 1.74.0 compression/Compression#HTJ2K256# => 256,
266 }
267 }
268
269 /// Deep data can only be compressed using RLE or ZIP compression.
270 pub fn supports_deep_datarust-analyzer cargo exr 1.74.0 compression/impl#[Compression]supports_deep_data().(self) -> bool {
271 use selfrust-analyzer cargo exr 1.74.0 compression/::Compressionrust-analyzer cargo exr 1.74.0 compression/Compression#::*;
272 match self {
273 Uncompressedrust-analyzer cargo exr 1.74.0 compression/Compression#Uncompressed# | RLErust-analyzer cargo exr 1.74.0 compression/Compression#RLE# | ZIP1rust-analyzer cargo exr 1.74.0 compression/Compression#ZIP1# =>
274 true,
275
276 ZIP16rust-analyzer cargo exr 1.74.0 compression/Compression#ZIP16# | PXR24rust-analyzer cargo exr 1.74.0 compression/Compression#PXR24# | PIZrust-analyzer cargo exr 1.74.0 compression/Compression#PIZ# | B44rust-analyzer cargo exr 1.74.0 compression/Compression#B44# | B44Arust-analyzer cargo exr 1.74.0 compression/Compression#B44A# |
277 DWAArust-analyzer cargo exr 1.74.0 compression/Compression#DWAA#(_) | DWABrust-analyzer cargo exr 1.74.0 compression/Compression#DWAB#(_) | HTJ2K256rust-analyzer cargo exr 1.74.0 compression/Compression#HTJ2K256# | HTJ2K32rust-analyzer cargo exr 1.74.0 compression/Compression#HTJ2K32# =>
278 false,
279 }
280 }
281
282 /// Most compression methods will reconstruct the exact pixel bytes,
283 /// but some might throw away unimportant data for specific types of samples.
284 pub fn is_lossless_forrust-analyzer cargo exr 1.74.0 compression/impl#[Compression]is_lossless_for().(self, sample_type: SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#) -> bool {
285 use selfrust-analyzer cargo exr 1.74.0 compression/::Compressionrust-analyzer cargo exr 1.74.0 compression/Compression#::*;
286 match self {
287 PXR24rust-analyzer cargo exr 1.74.0 compression/Compression#PXR24# => sample_type rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::F32rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#F32#, // pxr reduces f32 to f24
288 B44rust-analyzer cargo exr 1.74.0 compression/Compression#B44# | B44Arust-analyzer cargo exr 1.74.0 compression/Compression#B44A# => sample_type rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::F16rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#F16#, // b44 only compresses f16 values, others are left uncompressed
289 Uncompressedrust-analyzer cargo exr 1.74.0 compression/Compression#Uncompressed# | RLErust-analyzer cargo exr 1.74.0 compression/Compression#RLE# | ZIP1rust-analyzer cargo exr 1.74.0 compression/Compression#ZIP1# | ZIP16rust-analyzer cargo exr 1.74.0 compression/Compression#ZIP16# | PIZrust-analyzer cargo exr 1.74.0 compression/Compression#PIZ# | HTJ2K32rust-analyzer cargo exr 1.74.0 compression/Compression#HTJ2K32# | HTJ2K256rust-analyzer cargo exr 1.74.0 compression/Compression#HTJ2K256# => true,
290 DWABrust-analyzer cargo exr 1.74.0 compression/Compression#DWAB#(_) | DWAArust-analyzer cargo exr 1.74.0 compression/Compression#DWAA#(_) => false,
291 }
292 }
293
294 /// Most compression methods will reconstruct the exact pixel bytes,
295 /// but some might throw away unimportant data in some cases.
296 pub fn may_loose_datarust-analyzer cargo exr 1.74.0 compression/impl#[Compression]may_loose_data().(self) -> bool {
297 use selfrust-analyzer cargo exr 1.74.0 compression/::Compressionrust-analyzer cargo exr 1.74.0 compression/Compression#::*;
298 match self {
299 Uncompressedrust-analyzer cargo exr 1.74.0 compression/Compression#Uncompressed# | RLErust-analyzer cargo exr 1.74.0 compression/Compression#RLE# | ZIP1rust-analyzer cargo exr 1.74.0 compression/Compression#ZIP1# | ZIP16rust-analyzer cargo exr 1.74.0 compression/Compression#ZIP16# | PIZrust-analyzer cargo exr 1.74.0 compression/Compression#PIZ# | HTJ2K32rust-analyzer cargo exr 1.74.0 compression/Compression#HTJ2K32# | HTJ2K256rust-analyzer cargo exr 1.74.0 compression/Compression#HTJ2K256# => false,
300 PXR24rust-analyzer cargo exr 1.74.0 compression/Compression#PXR24# | B44rust-analyzer cargo exr 1.74.0 compression/Compression#B44# | B44Arust-analyzer cargo exr 1.74.0 compression/Compression#B44A# | DWABrust-analyzer cargo exr 1.74.0 compression/Compression#DWAB#(_) | DWAArust-analyzer cargo exr 1.74.0 compression/Compression#DWAA#(_) => true,
301 }
302 }
303
304 /// Most compression methods will reconstruct the exact pixel bytes,
305 /// but some might replace NaN with zeroes.
306 /// This might also depend on the sample type of the pixels.
307 /// Even a compression method that supports NaN might change the bit patterns of those NaNs.
308 pub fn supports_nanrust-analyzer cargo exr 1.74.0 compression/impl#[Compression]supports_nan().(self) -> bool {
309 use selfrust-analyzer cargo exr 1.74.0 compression/::Compressionrust-analyzer cargo exr 1.74.0 compression/Compression#::*;
310 match self {
311 B44Arust-analyzer cargo exr 1.74.0 compression/Compression#B44A# | DWABrust-analyzer cargo exr 1.74.0 compression/Compression#DWAB#(_) | DWAArust-analyzer cargo exr 1.74.0 compression/Compression#DWAA#(_) => false,
312 Uncompressedrust-analyzer cargo exr 1.74.0 compression/Compression#Uncompressed# | PXR24rust-analyzer cargo exr 1.74.0 compression/Compression#PXR24# | RLErust-analyzer cargo exr 1.74.0 compression/Compression#RLE# | ZIP1rust-analyzer cargo exr 1.74.0 compression/Compression#ZIP1# | ZIP16rust-analyzer cargo exr 1.74.0 compression/Compression#ZIP16# | PIZrust-analyzer cargo exr 1.74.0 compression/Compression#PIZ# | B44rust-analyzer cargo exr 1.74.0 compression/Compression#B44# | HTJ2K32rust-analyzer cargo exr 1.74.0 compression/Compression#HTJ2K32# | HTJ2K256rust-analyzer cargo exr 1.74.0 compression/Compression#HTJ2K256# => true,
313 }
314 }
315
316 /// Most compression methods will reconstruct the exact pixel and NaN bits,
317 /// but some might replace NaN bits with other NaN bits.
318 /// This might also depend on the sample type of the pixels.
319 pub fn preserves_nan_bitsrust-analyzer cargo exr 1.74.0 compression/impl#[Compression]preserves_nan_bits().(self) -> bool {
320 use selfrust-analyzer cargo exr 1.74.0 compression/::Compressionrust-analyzer cargo exr 1.74.0 compression/Compression#::*;
321 match self {
322 B44Arust-analyzer cargo exr 1.74.0 compression/Compression#B44A# | PXR24rust-analyzer cargo exr 1.74.0 compression/Compression#PXR24# | DWABrust-analyzer cargo exr 1.74.0 compression/Compression#DWAB#(_) | DWAArust-analyzer cargo exr 1.74.0 compression/Compression#DWAA#(_) => false,
323 B44rust-analyzer cargo exr 1.74.0 compression/Compression#B44# | Uncompressedrust-analyzer cargo exr 1.74.0 compression/Compression#Uncompressed# | RLErust-analyzer cargo exr 1.74.0 compression/Compression#RLE# | ZIP1rust-analyzer cargo exr 1.74.0 compression/Compression#ZIP1# | ZIP16rust-analyzer cargo exr 1.74.0 compression/Compression#ZIP16# | PIZrust-analyzer cargo exr 1.74.0 compression/Compression#PIZ# | HTJ2K32rust-analyzer cargo exr 1.74.0 compression/Compression#HTJ2K32# | HTJ2K256rust-analyzer cargo exr 1.74.0 compression/Compression#HTJ2K256# => true
324 }
325 }
326
327 }
328
329 // see https://github.com/AcademySoftwareFoundation/openexr/blob/6a9f8af6e89547bcd370ae3cec2b12849eee0b54/OpenEXR/IlmImf/ImfMisc.cpp#L1456-L1541
330
331 #[allow(unused)] // allows the extra parameters to be unused
332 fn convert_current_to_little_endianrust-analyzer cargo exr 1.74.0 compression/convert_current_to_little_endian().(mut bytes: ByteVecrust-analyzer cargo exr 1.74.0 compression/ByteVec#, channels: &ChannelListrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelList#, rectangle: 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#> {
333 #[cfg(target_endian = "big")]
334 reverse_block_endiannessrust-analyzer cargo exr 1.74.0 compression/reverse_block_endianness().(&mut bytes, channels, rectangle)?;
335
336 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(bytes)
337 }
338
339 #[allow(unused)] // allows the extra parameters to be unused
340 fn convert_little_endian_to_currentrust-analyzer cargo exr 1.74.0 compression/convert_little_endian_to_current().(mut bytes: ByteVecrust-analyzer cargo exr 1.74.0 compression/ByteVec#, channels: &ChannelListrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelList#, rectangle: 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#> {
341 #[cfg(target_endian = "big")]
342 reverse_block_endiannessrust-analyzer cargo exr 1.74.0 compression/reverse_block_endianness().(&mut bytes, channels, rectangle)?;
343
344 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(bytes)
345 }
346
347 #[allow(unused)] // unused when on little endian system
348 fn reverse_block_endiannessrust-analyzer cargo exr 1.74.0 compression/reverse_block_endianness().(bytes: &mut [u8], channels: &ChannelListrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelList#, rectangle: IntegerBoundsrust-analyzer cargo exr 1.74.0 meta/attribute/IntegerBounds#) -> UnitResultrust-analyzer cargo exr 1.74.0 error/UnitResult# {
349 let mut remaining_bytes: &mut [u8] = bytes;
350
351 for y in rectangle.positionrust-analyzer cargo exr 1.74.0 meta/attribute/IntegerBounds#position..yrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]y().() 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#rectangle.endrust-analyzer cargo exr 1.74.0 meta/attribute/impl#[IntegerBounds]end().().yrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]y().() {
352 for channel in &channels.listrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelList#list. {
353 let line_is_subsampled = 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..yrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]y().(), "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;
354 if line_is_subsampled { continue; }
355
356 let sample_count = rectangle.sizerust-analyzer cargo exr 1.74.0 meta/attribute/IntegerBounds#size..widthrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]width().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().channel.samplingrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelDescription#sampling..xrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]x().();
357
358 match channel.sample_typerust-analyzer cargo exr 1.74.0 meta/attribute/ChannelDescription#sample_type. {
359 SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::F16rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#F16# =>
360 remaining_bytes = convert_byte_chunksrust-analyzer cargo exr 1.74.0 compression/convert_byte_chunks().(reverse_2_bytesrust-analyzer cargo exr 1.74.0 compression/reverse_2_bytes()., 2, remaining_bytes, sample_count),
361
362 SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::F32rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#F32# =>
363 remaining_bytes = convert_byte_chunksrust-analyzer cargo exr 1.74.0 compression/convert_byte_chunks().(reverse_4_bytesrust-analyzer cargo exr 1.74.0 compression/reverse_4_bytes()., 4, remaining_bytes, sample_count),
364
365 SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::U32rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#U32# =>
366 remaining_bytes = convert_byte_chunksrust-analyzer cargo exr 1.74.0 compression/convert_byte_chunks().(reverse_4_bytesrust-analyzer cargo exr 1.74.0 compression/reverse_4_bytes()., 4, remaining_bytes, sample_count),
367 }
368 }
369 }
370
371 // Converts groups of bytes (e.g. 2 bytes), as many groups as specified. Returns a slice of the remaining bytes.
372 #[inline]
373 fn convert_byte_chunksrust-analyzer cargo exr 1.74.0 compression/convert_byte_chunks().(convert_single_value: fn(&mut[u8]), batch_size: usize, bytes: &mut [u8], batch_count: usize) -> &mut [u8] {
374 let (line_bytes, rest) = bytes.split_at_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]split_at_mut().(batch_count rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().batch_size);
375 let value_byte_chunks = line_bytes.chunks_exact_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact_mut().(batch_size);
376
377 for value_bytes in value_byte_chunks {
378 convert_single_value(value_bytes);
379 }
380
381 rest
382 }
383
384 debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(remaining_bytes.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().(), "not all bytes were converted to little endian");
385 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
386 }
387
388 #[inline]
389 fn reverse_2_bytesrust-analyzer cargo exr 1.74.0 compression/reverse_2_bytes().(bytes: &mut [u8]){
390 // this code seems like it could be optimized easily by the compiler
391 let two_bytes: [u8; 2] = bytes.try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("invalid byte count");
392 bytes.copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(&[two_bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., two_bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().]);
393 }
394
395 #[inline]
396 fn reverse_4_bytesrust-analyzer cargo exr 1.74.0 compression/reverse_4_bytes().(bytes: &mut [u8]){
397 let four_bytes: [u8; 4] = bytes.try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("invalid byte count");
398 bytes.copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(&[four_bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().3]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., four_bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., four_bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., four_bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().]);
399 }
400
401 #[inline]
402 fn div_prust-analyzer cargo exr 1.74.0 compression/div_p(). (x: i32, y: i32) -> i32 {
403 if x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]ge().0 {
404 if y rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]ge().0 { x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Div<Self>`]div().y }
405 else { -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().(x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Div<Self>`]div().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().y) }
406 }
407 else {
408 if y rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]ge().0 { -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().((y-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Sub<Self>`]sub().1-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Sub<Self>`]sub().x) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Div<Self>`]div().y) }
409 else { (-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().y-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Sub<Self>`]sub().1-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Sub<Self>`]sub().x) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Div<Self>`]div().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().y }
410 }
411 }
412
413 #[inline]
414 fn mod_prust-analyzer cargo exr 1.74.0 compression/mod_p().(x: i32, y: i32) -> i32 {
415 x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Sub<Self>`]sub().y rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Mul<Self>`]mul().div_prust-analyzer cargo exr 1.74.0 compression/div_p().(x, y)
416 }
417
418 /// A collection of functions used to prepare data for compression.
419 mod optimize_bytesrust-analyzer cargo exr 1.74.0 compression/optimize_bytes/ {
420
421 /// Integrate over all differences to the previous value in order to reconstruct sample values.
422 pub fn differences_to_samplesrust-analyzer cargo exr 1.74.0 compression/optimize_bytes/differences_to_samples().(buffer: &mut [u8]) {
423 // The naive implementation is very simple:
424 //
425 // for index in 1..buffer.len() {
426 // buffer[index] = (buffer[index - 1] as i32 + buffer[index] as i32 - 128) as u8;
427 // }
428 //
429 // But we process elements in pairs to take advantage of instruction-level parallelism.
430 // When computations within a pair do not depend on each other, they can be processed in parallel.
431 // Since this function is responsible for a very large chunk of execution time,
432 // this tweak alone improves decoding performance of RLE images by 20%.
433 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(first) = buffer.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(0) {
434 let mut previous = *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().first as i16;
435 for chunk in &mut buffer[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()..chunks_exact_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact_mut().(2) {
436 // no bounds checks here due to indices and chunk size being constant
437 let diff0 = chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). as i16;
438 let diff1 = chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). as i16;
439 // these two computations do not depend on each other, unlike in the naive version,
440 // so they can be executed by the CPU in parallel via instruction-level parallelism
441 let sample0 = (previous rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().diff0 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().128) as u8;
442 let sample1 = (previous rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().diff0 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().diff1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().128 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Mul<Self>`]mul().2) as u8;
443 chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = sample0;
444 chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = sample1;
445 previous = sample1 as i16;
446 }
447 // handle the remaining element at the end not processed by the loop over pairs, if present
448 for elem in &mut buffer[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()..chunks_exact_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact_mut().(2).into_remainderrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`ChunksExactMut<'a, T>`]into_remainder().().iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().() {
449 let sample = (previous rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().elem as i16 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().128) as u8;
450 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().elem = sample;
451 previous = sample as i16;
452 }
453 }
454 }
455
456 /// Derive over all values in order to produce differences to the previous value.
457 pub fn samples_to_differencesrust-analyzer cargo exr 1.74.0 compression/optimize_bytes/samples_to_differences().(buffer: &mut [u8]){
458 // naive version:
459 // for index in (1..buffer.len()).rev() {
460 // buffer[index] = (buffer[index] as i32 - buffer[index - 1] as i32 + 128) as u8;
461 // }
462 //
463 // But we process elements in batches to take advantage of autovectorization.
464 // If the target platform has no vector instructions (e.g. 32-bit ARM without `-C target-cpu=native`)
465 // this will instead take advantage of instruction-level parallelism.
466 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(first) = buffer.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(0) {
467 let mut previous = *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().first as i16;
468 // Chunk size is 16 because we process bytes (8 bits),
469 // and 8*16 = 128 bits is the size of a typical SIMD register.
470 // Even WASM has 128-bit SIMD registers.
471 for chunk in &mut buffer[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()..chunks_exact_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact_mut().(16) {
472 // no bounds checks here due to indices and chunk size being constant
473 let sample0 = chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). as i16;
474 let sample1 = chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). as i16;
475 let sample2 = chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). as i16;
476 let sample3 = chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().3]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). as i16;
477 let sample4 = chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().4]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). as i16;
478 let sample5 = chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().5]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). as i16;
479 let sample6 = chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().6]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). as i16;
480 let sample7 = chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().7]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). as i16;
481 let sample8 = chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().8]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). as i16;
482 let sample9 = chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().9]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). as i16;
483 let sample10 = chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().10]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). as i16;
484 let sample11 = chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().11]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). as i16;
485 let sample12 = chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().12]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). as i16;
486 let sample13 = chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().13]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). as i16;
487 let sample14 = chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().14]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). as i16;
488 let sample15 = chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().15]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). as i16;
489 // Unlike in decoding, computations in here are truly independent from each other,
490 // which enables the compiler to vectorize this loop.
491 // Even if the target platform has no vector instructions,
492 // so using more parallelism doesn't imply doing more work,
493 // and we're not really limited in how wide we can go.
494 chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = (sample0 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().previous rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().128) as u8;
495 chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = (sample1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().sample0 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().128) as u8;
496 chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = (sample2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().sample1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().128) as u8;
497 chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().3]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = (sample3 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().sample2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().128) as u8;
498 chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().4]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = (sample4 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().sample3 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().128) as u8;
499 chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().5]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = (sample5 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().sample4 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().128) as u8;
500 chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().6]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = (sample6 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().sample5 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().128) as u8;
501 chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().7]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = (sample7 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().sample6 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().128) as u8;
502 chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().8]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = (sample8 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().sample7 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().128) as u8;
503 chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().9]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = (sample9 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().sample8 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().128) as u8;
504 chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().10]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = (sample10 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().sample9 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().128) as u8;
505 chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().11]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = (sample11 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().sample10 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().128) as u8;
506 chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().12]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = (sample12 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().sample11 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().128) as u8;
507 chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().13]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = (sample13 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().sample12 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().128) as u8;
508 chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().14]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = (sample14 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().sample13 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().128) as u8;
509 chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().15]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = (sample15 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().sample14 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().128) as u8;
510 previous = sample15;
511 }
512 // Handle the remaining element at the end not processed by the loop over batches, if present
513 // This is what the iterator-based version of this function would look like without vectorization
514 for elem in &mut buffer[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()..chunks_exact_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact_mut().(16).into_remainderrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`ChunksExactMut<'a, T>`]into_remainder().().iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().() {
515 let diff = (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().elem as i16 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Sub<Self>`]sub().previous rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i16][`Add<Self>`]add().128) as u8;
516 previous = *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().elem as i16;
517 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().elem = diff;
518 }
519 }
520 }
521
522 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/::Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#;
523 thread_localrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/local/thread_local!! {
524 // A buffer for reusing between invocations of interleaving and deinterleaving.
525 // Allocating memory is cheap, but zeroing or otherwise initializing it is not.
526 // Doing it hundreds of times (once per block) would be expensive.
527 // This optimization brings down the time spent in interleaving from 15% to 5%.
528 static SCRATCH_SPACErust-analyzer cargo exr 1.74.0 compression/optimize_bytes/SCRATCH_SPACE.: Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>> = Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]new().(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().());
529 }
530
531 fn with_reused_bufferrust-analyzer cargo exr 1.74.0 compression/optimize_bytes/with_reused_buffer().<F>(length: usize, mut func: F) where F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&mut [u8]) {
532 SCRATCH_SPACErust-analyzer cargo exr 1.74.0 compression/optimize_bytes/SCRATCH_SPACE..withrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/local/impl#[`LocalKey<T>`]with().(|scratch_space| {
533 // reuse a buffer if we've already initialized one
534 let mut buffer = scratch_space.takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]take().();
535 if buffer.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().length {
536 // Efficiently create a zeroed Vec by requesting zeroed memory from the OS.
537 // This is slightly faster than a `memcpy()` plus `memset()` that would happen otherwise,
538 // but is not a big deal either way since it's not a hot codepath.
539 buffer = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0u8; length];
540 }
541
542 // call the function
543 func(&mut buffer[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#length]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().);
544
545 // save the internal buffer for reuse
546 scratch_space.setrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]set().(buffer);
547 });
548 }
549
550 /// Interleave the bytes such that the second half of the array is every other byte.
551 pub fn interleave_byte_blocksrust-analyzer cargo exr 1.74.0 compression/optimize_bytes/interleave_byte_blocks().(separated: &mut [u8]) {
552 with_reused_bufferrust-analyzer cargo exr 1.74.0 compression/optimize_bytes/with_reused_buffer().(separated.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), |interleaved| {
553
554 // Split the two halves that we are going to interleave.
555 let (first_half, second_half) = separated.split_atrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]split_at().((separated.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().2);
556 // The first half can be 1 byte longer than the second if the length of the input is odd,
557 // but the loop below only processes numbers in pairs.
558 // To handle it, preserve the last element of the first slice, to be handled after the loop.
559 let first_half_last = first_half.lastrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]last().();
560 // Truncate the first half to match the lenght of the second one; more optimizer-friendly
561 let first_half_iter = &first_half[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#second_half.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().;
562
563 // Main loop that performs the interleaving
564 for ((first, second), interleaved) in first_half_iter.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(second_half.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().())
565 .ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(interleaved.chunks_exact_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact_mut().(2)) {
566 // The length of each chunk is known to be 2 at compile time,
567 // and each index is also a constant.
568 // This allows the compiler to remove the bounds checks.
569 interleaved[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().first;
570 interleaved[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().second;
571 }
572
573 // If the length of the slice was odd, restore the last element of the first half that we saved
574 if interleaved.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().1 {
575 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(value) = first_half_last {
576 // we can unwrap() here because we just checked that the lenght is non-zero:
577 // `% 2 == 1` will fail for zero
578 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().interleaved.last_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]last_mut().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().() = *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().value;
579 }
580 }
581
582 // write out the results
583 separated.copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(&interleaved);
584 });
585 }
586
587 /// Separate the bytes such that the second half contains every other byte.
588 /// This performs deinterleaving - the inverse of interleaving.
589 pub fn separate_bytes_fragmentsrust-analyzer cargo exr 1.74.0 compression/optimize_bytes/separate_bytes_fragments().(source: &mut [u8]) {
590 with_reused_bufferrust-analyzer cargo exr 1.74.0 compression/optimize_bytes/with_reused_buffer().(source.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), |separated| {
591
592 // Split the two halves that we are going to interleave.
593 let (first_half, second_half) = separated.split_at_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]split_at_mut().((source.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().2);
594 // The first half can be 1 byte longer than the second if the length of the input is odd,
595 // but the loop below only processes numbers in pairs.
596 // To handle it, preserve the last element of the input, to be handled after the loop.
597 let last = source.lastrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]last().();
598 let first_half_iter = &mut first_half[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#second_half.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().;
599
600 // Main loop that performs the deinterleaving
601 for ((first, second), interleaved) in first_half_iter.iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().().ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(second_half.iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().())
602 .ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(source.chunks_exactrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact().(2)) {
603 // The length of each chunk is known to be 2 at compile time,
604 // and each index is also a constant.
605 // This allows the compiler to remove the bounds checks.
606 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().first = interleaved[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().;
607 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().second = interleaved[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().;
608 }
609
610 // If the length of the slice was odd, restore the last element of the input that we saved
611 if source.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().1 {
612 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(value) = last {
613 // we can unwrap() here because we just checked that the lenght is non-zero:
614 // `% 2 == 1` will fail for zero
615 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().first_half.last_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]last_mut().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().() = *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().value;
616 }
617 }
618
619 // write out the results
620 source.copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(&separated);
621 });
622 }
623
624
625 #[cfg(test)]
626 pub mod testrust-analyzer cargo exr 1.74.0 compression/optimize_bytes/test/ {
627
628 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
629 fn roundtrip_interleaverust-analyzer cargo exr 1.74.0 compression/optimize_bytes/test/roundtrip_interleave().(){
630 let source = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
631 let mut modified = source.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().();
632
633 superrust-analyzer cargo exr 1.74.0 compression/optimize_bytes/::separate_bytes_fragmentsrust-analyzer cargo exr 1.74.0 compression/optimize_bytes/separate_bytes_fragments().(&mut modified);
634 superrust-analyzer cargo exr 1.74.0 compression/optimize_bytes/::interleave_byte_blocksrust-analyzer cargo exr 1.74.0 compression/optimize_bytes/interleave_byte_blocks().(&mut modified);
635
636 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(source, modified);
637 }
638
639 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
640 fn roundtrip_deriverust-analyzer cargo exr 1.74.0 compression/optimize_bytes/test/roundtrip_derive().(){
641 let source = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![ 0, 1, 2, 7, 4, 5, 6, 7, 13, 9, 10 ];
642 let mut modified = source.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().();
643
644 superrust-analyzer cargo exr 1.74.0 compression/optimize_bytes/::samples_to_differencesrust-analyzer cargo exr 1.74.0 compression/optimize_bytes/samples_to_differences().(&mut modified);
645 superrust-analyzer cargo exr 1.74.0 compression/optimize_bytes/::differences_to_samplesrust-analyzer cargo exr 1.74.0 compression/optimize_bytes/differences_to_samples().(&mut modified);
646
647 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(source, modified);
648 }
649
650 }
651 }
652
653
654 #[cfg(test)]
655 mod testrust-analyzer cargo exr 1.74.0 compression/test/ {
656 use superrust-analyzer cargo exr 1.74.0 compression/::*;
657 use craterust-analyzer cargo exr 1.74.0 crate/::metarust-analyzer cargo exr 1.74.0 meta/::attributerust-analyzer cargo exr 1.74.0 meta/attribute/::ChannelDescriptionrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelDescription#;
658 use craterust-analyzer cargo exr 1.74.0 crate/::blockrust-analyzer cargo exr 1.74.0 block/::samplesrust-analyzer cargo exr 1.74.0 block/samples/::IntoNativeSamplerust-analyzer cargo exr 1.74.0 block/samples/IntoNativeSample#;
659
660 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
661 fn roundtrip_endianness_mixed_channelsrust-analyzer cargo exr 1.74.0 compression/test/roundtrip_endianness_mixed_channels().(){
662 let a32 = ChannelDescriptionrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelDescription#::newrust-analyzer cargo exr 1.74.0 meta/attribute/impl#[ChannelDescription]new().("A", SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::F32rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#F32#, true);
663 let y16 = ChannelDescriptionrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelDescription#::newrust-analyzer cargo exr 1.74.0 meta/attribute/impl#[ChannelDescription]new().("Y", SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::F16rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#F16#, true);
664 let channels = ChannelListrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelList#::newrust-analyzer cargo exr 1.74.0 meta/attribute/impl#[ChannelList]new().(smallvecrust-analyzer cargo smallvec 1.15.1 smallvec!![ a32, y16 ]);
665
666 let data = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![
667 23582740683_f32.to_ne_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]to_ne_bytes().().as_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]as_slice().(),
668 35827420683_f32.to_ne_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]to_ne_bytes().().as_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]as_slice().(),
669 27406832358_f32.to_f16rust-analyzer cargo exr 1.74.0 block/samples/impl#[f32][IntoNativeSample]to_f16().().to_ne_bytesrust-analyzer cargo half 2.6.0 binary16/impl#[f16]to_ne_bytes().().as_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]as_slice().(),
670 74062358283_f32.to_f16rust-analyzer cargo exr 1.74.0 block/samples/impl#[f32][IntoNativeSample]to_f16().().to_ne_bytesrust-analyzer cargo half 2.6.0 binary16/impl#[f16]to_ne_bytes().().as_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]as_slice().(),
671
672 52582740683_f32.to_ne_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]to_ne_bytes().().as_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]as_slice().(),
673 45827420683_f32.to_ne_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]to_ne_bytes().().as_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]as_slice().(),
674 15406832358_f32.to_f16rust-analyzer cargo exr 1.74.0 block/samples/impl#[f32][IntoNativeSample]to_f16().().to_ne_bytesrust-analyzer cargo half 2.6.0 binary16/impl#[f16]to_ne_bytes().().as_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]as_slice().(),
675 65062358283_f32.to_f16rust-analyzer cargo exr 1.74.0 block/samples/impl#[f32][IntoNativeSample]to_f16().().to_ne_bytesrust-analyzer cargo half 2.6.0 binary16/impl#[f16]to_ne_bytes().().as_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]as_slice().(),
676 ].into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().flattenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#flatten().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|x| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().x).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
677
678 roundtrip_convert_endiannessrust-analyzer cargo exr 1.74.0 compression/test/roundtrip_convert_endianness().(
679 data, &channels,
680 IntegerBoundsrust-analyzer cargo exr 1.74.0 meta/attribute/IntegerBounds#::from_dimensionsrust-analyzer cargo exr 1.74.0 meta/attribute/impl#[IntegerBounds]from_dimensions().((2, 2))
681 );
682 }
683
684 fn roundtrip_convert_endiannessrust-analyzer cargo exr 1.74.0 compression/test/roundtrip_convert_endianness().(
685 current_endian: ByteVecrust-analyzer cargo exr 1.74.0 compression/ByteVec#, channels: &ChannelListrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelList#, rectangle: IntegerBoundsrust-analyzer cargo exr 1.74.0 meta/attribute/IntegerBounds#
686 ){
687 let little_endian = convert_current_to_little_endianrust-analyzer cargo exr 1.74.0 compression/convert_current_to_little_endian().(
688 current_endian.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().(), channels, rectangle
689 ).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
690
691 let current_endian_decoded = convert_little_endian_to_currentrust-analyzer cargo exr 1.74.0 compression/convert_little_endian_to_current().(
692 little_endian.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().(), channels, rectangle
693 ).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
694
695 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(current_endian, current_endian_decoded, "endianness conversion failed");
696 }
697 }