/rust/registry/src/index.crates.io-1949cf8c6b5b557f/exr-1.74.3/src/compression/rle.rs
Line | Count | Source |
1 | | use super::{optimize_bytes::*, Error, Result, *}; |
2 | | |
3 | | // inspired by https://github.com/openexr/openexr/blob/master/OpenEXR/IlmImf/ImfRle.cpp |
4 | | |
5 | | const MIN_RUN_LENGTH: usize = 3; |
6 | | const MAX_RUN_LENGTH: usize = 127; |
7 | | |
8 | 487k | pub fn decompress_bytes( |
9 | 487k | channels: &ChannelList, |
10 | 487k | compressed_le: ByteVec, |
11 | 487k | rectangle: IntegerBounds, |
12 | 487k | expected_byte_size: usize, |
13 | 487k | pedantic: bool, |
14 | 487k | ) -> Result<ByteVec> { |
15 | 487k | let mut decompressed_le = unpack_rle_tokens(&compressed_le, expected_byte_size, pedantic)?; |
16 | 487k | differences_to_samples(&mut decompressed_le); |
17 | 487k | interleave_byte_blocks(&mut decompressed_le); |
18 | 487k | super::convert_little_endian_to_current(decompressed_le, channels, rectangle) |
19 | | // TODO no alloc |
20 | 487k | } |
21 | | |
22 | | /// Shared by this compression method and DWA's RLE scheme (both port OpenEXR's |
23 | | /// `internal_rle_decompress`, see `compression::dwa`) - kept separate from |
24 | | /// `decompress_bytes` because DWA does not apply the delta prediction / |
25 | | /// byte-block interleaving done there. |
26 | 487k | pub(super) fn unpack_rle_tokens( |
27 | 487k | compressed_le: &[u8], |
28 | 487k | expected_byte_size: usize, |
29 | 487k | pedantic: bool, |
30 | 487k | ) -> Result<ByteVec> { |
31 | 487k | let mut remaining_le = compressed_le; |
32 | 487k | let mut decompressed_le = Vec::with_capacity(expected_byte_size.min(8 * 2048)); |
33 | | |
34 | 9.63M | while !remaining_le.is_empty() && decompressed_le.len() != expected_byte_size { |
35 | 9.15M | let count = take_1(&mut remaining_le)? as i8 as i32; |
36 | | |
37 | 9.15M | if count < 0 { |
38 | | // take the next '-count' bytes as-is |
39 | 827k | let values = take_n(&mut remaining_le, -count as usize)?; |
40 | 827k | decompressed_le.extend_from_slice(values); |
41 | | } else { |
42 | | // repeat the next value 'count + 1' times |
43 | 8.32M | let value = take_1(&mut remaining_le)?; |
44 | 8.32M | decompressed_le.resize(decompressed_le.len() + (count as usize) + 1, value); |
45 | | } |
46 | | } |
47 | | |
48 | 487k | if pedantic && !remaining_le.is_empty() { |
49 | 0 | return Err(Error::invalid("data amount")); |
50 | 487k | } |
51 | | |
52 | 487k | Ok(decompressed_le) |
53 | 487k | } |
54 | | |
55 | 482k | pub fn compress_bytes( |
56 | 482k | channels: &ChannelList, |
57 | 482k | uncompressed_ne: ByteVec, |
58 | 482k | rectangle: IntegerBounds, |
59 | 482k | ) -> Result<ByteVec> { |
60 | | // see https://github.com/AcademySoftwareFoundation/openexr/blob/3bd93f85bcb74c77255f28cdbb913fdbfbb39dfe/OpenEXR/IlmImf/ImfTiledOutputFile.cpp#L750-L842 |
61 | 482k | let mut data_le = |
62 | 482k | super::convert_current_to_little_endian(uncompressed_ne, channels, rectangle)?; // TODO no alloc |
63 | | |
64 | 482k | separate_bytes_fragments(&mut data_le); |
65 | 482k | samples_to_differences(&mut data_le); |
66 | | |
67 | 482k | Ok(pack_rle_tokens(&data_le)) |
68 | 482k | } |
69 | | |
70 | | /// Shared by this compression method and DWA's RLE section. This only emits |
71 | | /// the byte-oriented RLE token stream; callers are responsible for any byte |
72 | | /// prediction, byte interleaving, or zlib wrapping required by their format. |
73 | 482k | pub(super) fn pack_rle_tokens(data_le: &[u8]) -> ByteVec { |
74 | 482k | let mut compressed_le = Vec::with_capacity(data_le.len()); |
75 | 482k | let mut run_start = 0; |
76 | 482k | let mut run_end = 1; |
77 | | |
78 | 6.42M | while run_start < data_le.len() { |
79 | 657M | while run_end < data_le.len() |
80 | 656M | && data_le[run_start] == data_le[run_end] |
81 | 655M | && ((run_end - run_start) as i32) - 1 < (MAX_RUN_LENGTH as i32) |
82 | 651M | { |
83 | 651M | run_end += 1; |
84 | 651M | } |
85 | | |
86 | 5.94M | if run_end - run_start >= MIN_RUN_LENGTH { |
87 | 5.24M | compressed_le.push((((run_end - run_start) as i32) - 1) as u8); |
88 | 5.24M | compressed_le.push(data_le[run_start]); |
89 | 5.24M | run_start = run_end; |
90 | 5.24M | } else { |
91 | 6.19M | while run_end < data_le.len() |
92 | 6.18M | && (run_end + 1 >= data_le.len() |
93 | 6.18M | || data_le[run_end] != data_le[run_end + 1] |
94 | 676k | || run_end + 2 >= data_le.len() |
95 | 676k | || data_le[run_end + 1] != data_le[run_end + 2]) |
96 | 5.52M | && run_end - run_start < MAX_RUN_LENGTH |
97 | 5.49M | { |
98 | 5.49M | run_end += 1; |
99 | 5.49M | } |
100 | | |
101 | 695k | compressed_le.push(((run_start as i32) - (run_end as i32)) as u8); |
102 | 695k | compressed_le.extend_from_slice(&data_le[run_start..run_end]); |
103 | | |
104 | 695k | run_start = run_end; |
105 | 695k | run_end += 1; |
106 | | } |
107 | | } |
108 | | |
109 | 482k | compressed_le |
110 | 482k | } |
111 | | |
112 | 17.4M | fn take_1(slice: &mut &[u8]) -> Result<u8> { |
113 | 17.4M | if !slice.is_empty() { |
114 | 17.4M | let result = slice[0]; |
115 | 17.4M | *slice = &slice[1..]; |
116 | 17.4M | Ok(result) |
117 | | } else { |
118 | 18 | Err(Error::invalid("compressed data")) |
119 | | } |
120 | 17.4M | } |
121 | | |
122 | 827k | fn take_n<'s>(slice: &mut &'s [u8], n: usize) -> Result<&'s [u8]> { |
123 | 827k | if n <= slice.len() { |
124 | 827k | let (front, back) = slice.split_at(n); |
125 | 827k | *slice = back; |
126 | 827k | Ok(front) |
127 | | } else { |
128 | 46 | Err(Error::invalid("compressed data")) |
129 | | } |
130 | 827k | } |