/rust/registry/src/index.crates.io-1949cf8c6b5b557f/zune-jpeg-0.4.21/src/headers.rs
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2023. |
3 | | * |
4 | | * This software is free software; |
5 | | * |
6 | | * You can redistribute it or modify it under terms of the MIT, Apache License or Zlib license |
7 | | */ |
8 | | |
9 | | //! Decode Decoder markers/segments |
10 | | //! |
11 | | //! This file deals with decoding header information in a jpeg file |
12 | | //! |
13 | | use alloc::format; |
14 | | use alloc::string::ToString; |
15 | | use alloc::vec::Vec; |
16 | | |
17 | | use zune_core::bytestream::ZReaderTrait; |
18 | | use zune_core::colorspace::ColorSpace; |
19 | | use zune_core::log::{debug, error, trace, warn}; |
20 | | |
21 | | use crate::components::Components; |
22 | | use crate::decoder::{ICCChunk, JpegDecoder, MAX_COMPONENTS}; |
23 | | use crate::errors::DecodeErrors; |
24 | | use crate::huffman::HuffmanTable; |
25 | | use crate::misc::{SOFMarkers, UN_ZIGZAG}; |
26 | | |
27 | | ///**B.2.4.2 Huffman table-specification syntax** |
28 | | #[allow(clippy::similar_names, clippy::cast_sign_loss)] |
29 | 17.2k | pub(crate) fn parse_huffman<T: ZReaderTrait>( |
30 | 17.2k | decoder: &mut JpegDecoder<T> |
31 | 17.2k | ) -> Result<(), DecodeErrors> |
32 | 17.2k | where |
33 | | { |
34 | | // Read the length of the Huffman table |
35 | 17.2k | let mut dht_length = i32::from(decoder.stream.get_u16_be_err()?.checked_sub(2).ok_or( |
36 | 17.2k | DecodeErrors::FormatStatic("Invalid Huffman length in image") |
37 | 3 | )?); |
38 | | |
39 | 46.4k | while dht_length > 16 { |
40 | | // HT information |
41 | 29.6k | let ht_info = decoder.stream.get_u8_err()?; |
42 | | // third bit indicates whether the huffman encoding is DC or AC type |
43 | 29.6k | let dc_or_ac = (ht_info >> 4) & 0xF; |
44 | | // Indicate the position of this table, should be less than 4; |
45 | 29.6k | let index = (ht_info & 0xF) as usize; |
46 | | // read the number of symbols |
47 | 29.6k | let mut num_symbols: [u8; 17] = [0; 17]; |
48 | | |
49 | 29.6k | if index >= MAX_COMPONENTS { |
50 | 18 | return Err(DecodeErrors::HuffmanDecode(format!( |
51 | 18 | "Invalid DHT index {index}, expected between 0 and 3" |
52 | 18 | ))); |
53 | 29.6k | } |
54 | | |
55 | 29.6k | if dc_or_ac > 1 { |
56 | 4 | return Err(DecodeErrors::HuffmanDecode(format!( |
57 | 4 | "Invalid DHT position {dc_or_ac}, should be 0 or 1" |
58 | 4 | ))); |
59 | 29.6k | } |
60 | | |
61 | 29.6k | decoder |
62 | 29.6k | .stream |
63 | 29.6k | .read_exact(&mut num_symbols[1..17]) |
64 | 29.6k | .map_err(|_| DecodeErrors::ExhaustedData)?; |
65 | | |
66 | 29.5k | dht_length -= 1 + 16; |
67 | | |
68 | 502k | let symbols_sum: i32 = num_symbols.iter().map(|f| i32::from(*f)).sum(); zune_jpeg::headers::parse_huffman::<alloc::vec::Vec<u8>>::{closure#1}Line | Count | Source | 68 | 502k | let symbols_sum: i32 = num_symbols.iter().map(|f| i32::from(*f)).sum(); |
Unexecuted instantiation: zune_jpeg::headers::parse_huffman::<_>::{closure#1} |
69 | | |
70 | | // The sum of the number of symbols cannot be greater than 256; |
71 | 29.5k | if symbols_sum > 256 { |
72 | 53 | return Err(DecodeErrors::FormatStatic( |
73 | 53 | "Encountered Huffman table with excessive length in DHT" |
74 | 53 | )); |
75 | 29.5k | } |
76 | 29.5k | if symbols_sum > dht_length { |
77 | 24 | return Err(DecodeErrors::HuffmanDecode(format!( |
78 | 24 | "Excessive Huffman table of length {symbols_sum} found when header length is {dht_length}" |
79 | 24 | ))); |
80 | 29.4k | } |
81 | 29.4k | dht_length -= symbols_sum; |
82 | | // A table containing symbols in increasing code length |
83 | 29.4k | let mut symbols = [0; 256]; |
84 | | |
85 | 29.4k | decoder |
86 | 29.4k | .stream |
87 | 29.4k | .read_exact(&mut symbols[0..(symbols_sum as usize)]) |
88 | 29.4k | .map_err(|x| { |
89 | 32 | DecodeErrors::Format(format!("Could not read symbols into the buffer\n{x}")) |
90 | 32 | })?; zune_jpeg::headers::parse_huffman::<alloc::vec::Vec<u8>>::{closure#2}Line | Count | Source | 88 | 32 | .map_err(|x| { | 89 | 32 | DecodeErrors::Format(format!("Could not read symbols into the buffer\n{x}")) | 90 | 32 | })?; |
Unexecuted instantiation: zune_jpeg::headers::parse_huffman::<_>::{closure#2} |
91 | | // store |
92 | 29.4k | match dc_or_ac { |
93 | | 0 => { |
94 | 17.7k | decoder.dc_huffman_tables[index] = Some(HuffmanTable::new( |
95 | 17.7k | &num_symbols, |
96 | 17.7k | symbols, |
97 | | true, |
98 | 17.7k | decoder.is_progressive |
99 | 171 | )?); |
100 | | } |
101 | | _ => { |
102 | 11.6k | decoder.ac_huffman_tables[index] = Some(HuffmanTable::new( |
103 | 11.6k | &num_symbols, |
104 | 11.6k | symbols, |
105 | | false, |
106 | 11.6k | decoder.is_progressive |
107 | 10 | )?); |
108 | | } |
109 | | } |
110 | | } |
111 | | |
112 | 16.8k | if dht_length > 0 { |
113 | 25 | return Err(DecodeErrors::FormatStatic("Bogus Huffman table definition")); |
114 | 16.7k | } |
115 | | |
116 | 16.7k | Ok(()) |
117 | 17.2k | } zune_jpeg::headers::parse_huffman::<alloc::vec::Vec<u8>> Line | Count | Source | 29 | 17.2k | pub(crate) fn parse_huffman<T: ZReaderTrait>( | 30 | 17.2k | decoder: &mut JpegDecoder<T> | 31 | 17.2k | ) -> Result<(), DecodeErrors> | 32 | 17.2k | where | 33 | | { | 34 | | // Read the length of the Huffman table | 35 | 17.2k | let mut dht_length = i32::from(decoder.stream.get_u16_be_err()?.checked_sub(2).ok_or( | 36 | 17.2k | DecodeErrors::FormatStatic("Invalid Huffman length in image") | 37 | 3 | )?); | 38 | | | 39 | 46.4k | while dht_length > 16 { | 40 | | // HT information | 41 | 29.6k | let ht_info = decoder.stream.get_u8_err()?; | 42 | | // third bit indicates whether the huffman encoding is DC or AC type | 43 | 29.6k | let dc_or_ac = (ht_info >> 4) & 0xF; | 44 | | // Indicate the position of this table, should be less than 4; | 45 | 29.6k | let index = (ht_info & 0xF) as usize; | 46 | | // read the number of symbols | 47 | 29.6k | let mut num_symbols: [u8; 17] = [0; 17]; | 48 | | | 49 | 29.6k | if index >= MAX_COMPONENTS { | 50 | 18 | return Err(DecodeErrors::HuffmanDecode(format!( | 51 | 18 | "Invalid DHT index {index}, expected between 0 and 3" | 52 | 18 | ))); | 53 | 29.6k | } | 54 | | | 55 | 29.6k | if dc_or_ac > 1 { | 56 | 4 | return Err(DecodeErrors::HuffmanDecode(format!( | 57 | 4 | "Invalid DHT position {dc_or_ac}, should be 0 or 1" | 58 | 4 | ))); | 59 | 29.6k | } | 60 | | | 61 | 29.6k | decoder | 62 | 29.6k | .stream | 63 | 29.6k | .read_exact(&mut num_symbols[1..17]) | 64 | 29.6k | .map_err(|_| DecodeErrors::ExhaustedData)?; | 65 | | | 66 | 29.5k | dht_length -= 1 + 16; | 67 | | | 68 | 29.5k | let symbols_sum: i32 = num_symbols.iter().map(|f| i32::from(*f)).sum(); | 69 | | | 70 | | // The sum of the number of symbols cannot be greater than 256; | 71 | 29.5k | if symbols_sum > 256 { | 72 | 53 | return Err(DecodeErrors::FormatStatic( | 73 | 53 | "Encountered Huffman table with excessive length in DHT" | 74 | 53 | )); | 75 | 29.5k | } | 76 | 29.5k | if symbols_sum > dht_length { | 77 | 24 | return Err(DecodeErrors::HuffmanDecode(format!( | 78 | 24 | "Excessive Huffman table of length {symbols_sum} found when header length is {dht_length}" | 79 | 24 | ))); | 80 | 29.4k | } | 81 | 29.4k | dht_length -= symbols_sum; | 82 | | // A table containing symbols in increasing code length | 83 | 29.4k | let mut symbols = [0; 256]; | 84 | | | 85 | 29.4k | decoder | 86 | 29.4k | .stream | 87 | 29.4k | .read_exact(&mut symbols[0..(symbols_sum as usize)]) | 88 | 29.4k | .map_err(|x| { | 89 | | DecodeErrors::Format(format!("Could not read symbols into the buffer\n{x}")) | 90 | 32 | })?; | 91 | | // store | 92 | 29.4k | match dc_or_ac { | 93 | | 0 => { | 94 | 17.7k | decoder.dc_huffman_tables[index] = Some(HuffmanTable::new( | 95 | 17.7k | &num_symbols, | 96 | 17.7k | symbols, | 97 | | true, | 98 | 17.7k | decoder.is_progressive | 99 | 171 | )?); | 100 | | } | 101 | | _ => { | 102 | 11.6k | decoder.ac_huffman_tables[index] = Some(HuffmanTable::new( | 103 | 11.6k | &num_symbols, | 104 | 11.6k | symbols, | 105 | | false, | 106 | 11.6k | decoder.is_progressive | 107 | 10 | )?); | 108 | | } | 109 | | } | 110 | | } | 111 | | | 112 | 16.8k | if dht_length > 0 { | 113 | 25 | return Err(DecodeErrors::FormatStatic("Bogus Huffman table definition")); | 114 | 16.7k | } | 115 | | | 116 | 16.7k | Ok(()) | 117 | 17.2k | } |
Unexecuted instantiation: zune_jpeg::headers::parse_huffman::<_> |
118 | | |
119 | | ///**B.2.4.1 Quantization table-specification syntax** |
120 | | #[allow(clippy::cast_possible_truncation, clippy::needless_range_loop)] |
121 | 7.31k | pub(crate) fn parse_dqt<T: ZReaderTrait>(img: &mut JpegDecoder<T>) -> Result<(), DecodeErrors> { |
122 | | // read length |
123 | 7.30k | let mut qt_length = |
124 | 7.31k | img.stream |
125 | 7.31k | .get_u16_be_err()? |
126 | 7.31k | .checked_sub(2) |
127 | 7.31k | .ok_or(DecodeErrors::FormatStatic( |
128 | 7.31k | "Invalid DQT length. Length should be greater than 2" |
129 | 7.31k | ))?; |
130 | | // A single DQT header may have multiple QT's |
131 | 14.8k | while qt_length > 0 { |
132 | 7.64k | let qt_info = img.stream.get_u8_err()?; |
133 | | // 0 = 8 bit otherwise 16 bit dqt |
134 | 7.64k | let precision = (qt_info >> 4) as usize; |
135 | | // last 4 bits give us position |
136 | 7.64k | let table_position = (qt_info & 0x0f) as usize; |
137 | 7.64k | let precision_value = 64 * (precision + 1); |
138 | | |
139 | 7.64k | if (precision_value + 1) as u16 > qt_length { |
140 | 15 | return Err(DecodeErrors::DqtError(format!("Invalid QT table bytes left :{}. Too small to construct a valid qt table which should be {} long", qt_length, precision_value + 1))); |
141 | 7.62k | } |
142 | | |
143 | 7.62k | let dct_table = match precision { |
144 | | 0 => { |
145 | 7.48k | let mut qt_values = [0; 64]; |
146 | | |
147 | 7.48k | img.stream.read_exact(&mut qt_values).map_err(|x| { |
148 | 21 | DecodeErrors::Format(format!("Could not read symbols into the buffer\n{x}")) |
149 | 21 | })?; zune_jpeg::headers::parse_dqt::<alloc::vec::Vec<u8>>::{closure#0}Line | Count | Source | 147 | 21 | img.stream.read_exact(&mut qt_values).map_err(|x| { | 148 | 21 | DecodeErrors::Format(format!("Could not read symbols into the buffer\n{x}")) | 149 | 21 | })?; |
Unexecuted instantiation: zune_jpeg::headers::parse_dqt::<_>::{closure#0} |
150 | 7.46k | qt_length -= (precision_value as u16) + 1 /*QT BIT*/; |
151 | | // carry out un zig-zag here |
152 | 7.46k | un_zig_zag(&qt_values) |
153 | | } |
154 | | 1 => { |
155 | | // 16 bit quantization tables |
156 | 119 | let mut qt_values = [0_u16; 64]; |
157 | | |
158 | 6.86k | for i in 0..64 { |
159 | 6.77k | qt_values[i] = img.stream.get_u16_be_err()?; |
160 | | } |
161 | 89 | qt_length -= (precision_value as u16) + 1; |
162 | | |
163 | 89 | un_zig_zag(&qt_values) |
164 | | } |
165 | | _ => { |
166 | 22 | return Err(DecodeErrors::DqtError(format!( |
167 | 22 | "Expected QT precision value of either 0 or 1, found {precision:?}" |
168 | 22 | ))); |
169 | | } |
170 | | }; |
171 | | |
172 | 7.55k | if table_position >= MAX_COMPONENTS { |
173 | 8 | return Err(DecodeErrors::DqtError(format!( |
174 | 8 | "Too large table position for QT :{table_position}, expected between 0 and 3" |
175 | 8 | ))); |
176 | 7.54k | } |
177 | | |
178 | 7.54k | img.qt_tables[table_position] = Some(dct_table); |
179 | | } |
180 | | |
181 | 7.20k | return Ok(()); |
182 | 7.31k | } zune_jpeg::headers::parse_dqt::<alloc::vec::Vec<u8>> Line | Count | Source | 121 | 7.31k | pub(crate) fn parse_dqt<T: ZReaderTrait>(img: &mut JpegDecoder<T>) -> Result<(), DecodeErrors> { | 122 | | // read length | 123 | 7.30k | let mut qt_length = | 124 | 7.31k | img.stream | 125 | 7.31k | .get_u16_be_err()? | 126 | 7.31k | .checked_sub(2) | 127 | 7.31k | .ok_or(DecodeErrors::FormatStatic( | 128 | 7.31k | "Invalid DQT length. Length should be greater than 2" | 129 | 7.31k | ))?; | 130 | | // A single DQT header may have multiple QT's | 131 | 14.8k | while qt_length > 0 { | 132 | 7.64k | let qt_info = img.stream.get_u8_err()?; | 133 | | // 0 = 8 bit otherwise 16 bit dqt | 134 | 7.64k | let precision = (qt_info >> 4) as usize; | 135 | | // last 4 bits give us position | 136 | 7.64k | let table_position = (qt_info & 0x0f) as usize; | 137 | 7.64k | let precision_value = 64 * (precision + 1); | 138 | | | 139 | 7.64k | if (precision_value + 1) as u16 > qt_length { | 140 | 15 | return Err(DecodeErrors::DqtError(format!("Invalid QT table bytes left :{}. Too small to construct a valid qt table which should be {} long", qt_length, precision_value + 1))); | 141 | 7.62k | } | 142 | | | 143 | 7.62k | let dct_table = match precision { | 144 | | 0 => { | 145 | 7.48k | let mut qt_values = [0; 64]; | 146 | | | 147 | 7.48k | img.stream.read_exact(&mut qt_values).map_err(|x| { | 148 | | DecodeErrors::Format(format!("Could not read symbols into the buffer\n{x}")) | 149 | 21 | })?; | 150 | 7.46k | qt_length -= (precision_value as u16) + 1 /*QT BIT*/; | 151 | | // carry out un zig-zag here | 152 | 7.46k | un_zig_zag(&qt_values) | 153 | | } | 154 | | 1 => { | 155 | | // 16 bit quantization tables | 156 | 119 | let mut qt_values = [0_u16; 64]; | 157 | | | 158 | 6.86k | for i in 0..64 { | 159 | 6.77k | qt_values[i] = img.stream.get_u16_be_err()?; | 160 | | } | 161 | 89 | qt_length -= (precision_value as u16) + 1; | 162 | | | 163 | 89 | un_zig_zag(&qt_values) | 164 | | } | 165 | | _ => { | 166 | 22 | return Err(DecodeErrors::DqtError(format!( | 167 | 22 | "Expected QT precision value of either 0 or 1, found {precision:?}" | 168 | 22 | ))); | 169 | | } | 170 | | }; | 171 | | | 172 | 7.55k | if table_position >= MAX_COMPONENTS { | 173 | 8 | return Err(DecodeErrors::DqtError(format!( | 174 | 8 | "Too large table position for QT :{table_position}, expected between 0 and 3" | 175 | 8 | ))); | 176 | 7.54k | } | 177 | | | 178 | 7.54k | img.qt_tables[table_position] = Some(dct_table); | 179 | | } | 180 | | | 181 | 7.20k | return Ok(()); | 182 | 7.31k | } |
Unexecuted instantiation: zune_jpeg::headers::parse_dqt::<_> |
183 | | |
184 | | /// Section:`B.2.2 Frame header syntax` |
185 | | |
186 | 4.94k | pub(crate) fn parse_start_of_frame<T: ZReaderTrait>( |
187 | 4.94k | sof: SOFMarkers, img: &mut JpegDecoder<T> |
188 | 4.94k | ) -> Result<(), DecodeErrors> { |
189 | 4.94k | if img.seen_sof { |
190 | 57 | return Err(DecodeErrors::SofError( |
191 | 57 | "Two Start of Frame Markers".to_string() |
192 | 57 | )); |
193 | 4.88k | } |
194 | | // Get length of the frame header |
195 | 4.88k | let length = img.stream.get_u16_be_err()?; |
196 | | // usually 8, but can be 12 and 16, we currently support only 8 |
197 | | // so sorry about that 12 bit images |
198 | 4.88k | let dt_precision = img.stream.get_u8_err()?; |
199 | | |
200 | 4.88k | if dt_precision != 8 { |
201 | 4 | return Err(DecodeErrors::SofError(format!( |
202 | 4 | "The library can only parse 8-bit images, the image has {dt_precision} bits of precision" |
203 | 4 | ))); |
204 | 4.87k | } |
205 | | |
206 | 4.87k | img.info.set_density(dt_precision); |
207 | | |
208 | | // read and set the image height. |
209 | 4.87k | let img_height = img.stream.get_u16_be_err()?; |
210 | 4.87k | img.info.set_height(img_height); |
211 | | |
212 | | // read and set the image width |
213 | 4.87k | let img_width = img.stream.get_u16_be_err()?; |
214 | 4.87k | img.info.set_width(img_width); |
215 | | |
216 | | trace!("Image width :{}", img_width); |
217 | | trace!("Image height :{}", img_height); |
218 | | |
219 | 4.87k | if usize::from(img_width) > img.options.get_max_width() { |
220 | 1 | return Err(DecodeErrors::Format(format!("Image width {} greater than width limit {}. If use `set_limits` if you want to support huge images", img_width, img.options.get_max_width()))); |
221 | 4.87k | } |
222 | | |
223 | 4.87k | if usize::from(img_height) > img.options.get_max_height() { |
224 | 3 | return Err(DecodeErrors::Format(format!("Image height {} greater than height limit {}. If use `set_limits` if you want to support huge images", img_height, img.options.get_max_height()))); |
225 | 4.87k | } |
226 | | |
227 | | // Check image width or height is zero |
228 | 4.87k | if img_width == 0 || img_height == 0 { |
229 | 4 | return Err(DecodeErrors::ZeroError); |
230 | 4.86k | } |
231 | | |
232 | | // Number of components for the image. |
233 | 4.86k | let num_components = img.stream.get_u8_err()?; |
234 | | |
235 | 4.86k | if num_components == 0 { |
236 | 1 | return Err(DecodeErrors::SofError( |
237 | 1 | "Number of components cannot be zero.".to_string() |
238 | 1 | )); |
239 | 4.86k | } |
240 | | |
241 | 4.86k | let expected = 8 + 3 * u16::from(num_components); |
242 | | // length should be equal to num components |
243 | 4.86k | if length != expected { |
244 | 7 | return Err(DecodeErrors::SofError(format!( |
245 | 7 | "Length of start of frame differs from expected {expected},value is {length}" |
246 | 7 | ))); |
247 | 4.85k | } |
248 | | |
249 | | trace!("Image components : {}", num_components); |
250 | | |
251 | 4.85k | if num_components == 1 { |
252 | 4.13k | // SOF sets the number of image components |
253 | 4.13k | // and that to us translates to setting input and output |
254 | 4.13k | // colorspaces to zero |
255 | 4.13k | img.input_colorspace = ColorSpace::Luma; |
256 | 4.13k | img.options = img.options.jpeg_set_out_colorspace(ColorSpace::Luma); |
257 | 4.13k | debug!("Overriding default colorspace set to Luma"); |
258 | 4.13k | } |
259 | 4.85k | if num_components == 4 && img.input_colorspace == ColorSpace::YCbCr { |
260 | 530 | trace!("Input image has 4 components, defaulting to CMYK colorspace"); |
261 | 530 | // https://entropymine.wordpress.com/2018/10/22/how-is-a-jpeg-images-color-type-determined/ |
262 | 530 | img.input_colorspace = ColorSpace::CMYK; |
263 | 4.32k | } |
264 | | |
265 | | // set number of components |
266 | 4.85k | img.info.components = num_components; |
267 | | |
268 | 4.85k | let mut components = Vec::with_capacity(num_components as usize); |
269 | 4.85k | let mut temp = [0; 3]; |
270 | | |
271 | 6.83k | for pos in 0..num_components { |
272 | | // read 3 bytes for each component |
273 | 6.83k | img.stream |
274 | 6.83k | .read_exact(&mut temp) |
275 | 6.83k | .map_err(|x| DecodeErrors::Format(format!("Could not read component data\n{x}")))?;zune_jpeg::headers::parse_start_of_frame::<alloc::vec::Vec<u8>>::{closure#0}Line | Count | Source | 275 | 1 | .map_err(|x| DecodeErrors::Format(format!("Could not read component data\n{x}")))?; |
Unexecuted instantiation: zune_jpeg::headers::parse_start_of_frame::<_>::{closure#0} |
276 | | // create a component. |
277 | 6.83k | let component = Components::from(temp, pos)?; |
278 | | |
279 | 6.82k | components.push(component); |
280 | | } |
281 | 4.84k | img.seen_sof = true; |
282 | | |
283 | 4.84k | img.info.set_sof_marker(sof); |
284 | | |
285 | 4.84k | img.components = components; |
286 | | |
287 | 4.84k | Ok(()) |
288 | 4.94k | } zune_jpeg::headers::parse_start_of_frame::<alloc::vec::Vec<u8>> Line | Count | Source | 186 | 4.94k | pub(crate) fn parse_start_of_frame<T: ZReaderTrait>( | 187 | 4.94k | sof: SOFMarkers, img: &mut JpegDecoder<T> | 188 | 4.94k | ) -> Result<(), DecodeErrors> { | 189 | 4.94k | if img.seen_sof { | 190 | 57 | return Err(DecodeErrors::SofError( | 191 | 57 | "Two Start of Frame Markers".to_string() | 192 | 57 | )); | 193 | 4.88k | } | 194 | | // Get length of the frame header | 195 | 4.88k | let length = img.stream.get_u16_be_err()?; | 196 | | // usually 8, but can be 12 and 16, we currently support only 8 | 197 | | // so sorry about that 12 bit images | 198 | 4.88k | let dt_precision = img.stream.get_u8_err()?; | 199 | | | 200 | 4.88k | if dt_precision != 8 { | 201 | 4 | return Err(DecodeErrors::SofError(format!( | 202 | 4 | "The library can only parse 8-bit images, the image has {dt_precision} bits of precision" | 203 | 4 | ))); | 204 | 4.87k | } | 205 | | | 206 | 4.87k | img.info.set_density(dt_precision); | 207 | | | 208 | | // read and set the image height. | 209 | 4.87k | let img_height = img.stream.get_u16_be_err()?; | 210 | 4.87k | img.info.set_height(img_height); | 211 | | | 212 | | // read and set the image width | 213 | 4.87k | let img_width = img.stream.get_u16_be_err()?; | 214 | 4.87k | img.info.set_width(img_width); | 215 | | | 216 | | trace!("Image width :{}", img_width); | 217 | | trace!("Image height :{}", img_height); | 218 | | | 219 | 4.87k | if usize::from(img_width) > img.options.get_max_width() { | 220 | 1 | return Err(DecodeErrors::Format(format!("Image width {} greater than width limit {}. If use `set_limits` if you want to support huge images", img_width, img.options.get_max_width()))); | 221 | 4.87k | } | 222 | | | 223 | 4.87k | if usize::from(img_height) > img.options.get_max_height() { | 224 | 3 | return Err(DecodeErrors::Format(format!("Image height {} greater than height limit {}. If use `set_limits` if you want to support huge images", img_height, img.options.get_max_height()))); | 225 | 4.87k | } | 226 | | | 227 | | // Check image width or height is zero | 228 | 4.87k | if img_width == 0 || img_height == 0 { | 229 | 4 | return Err(DecodeErrors::ZeroError); | 230 | 4.86k | } | 231 | | | 232 | | // Number of components for the image. | 233 | 4.86k | let num_components = img.stream.get_u8_err()?; | 234 | | | 235 | 4.86k | if num_components == 0 { | 236 | 1 | return Err(DecodeErrors::SofError( | 237 | 1 | "Number of components cannot be zero.".to_string() | 238 | 1 | )); | 239 | 4.86k | } | 240 | | | 241 | 4.86k | let expected = 8 + 3 * u16::from(num_components); | 242 | | // length should be equal to num components | 243 | 4.86k | if length != expected { | 244 | 7 | return Err(DecodeErrors::SofError(format!( | 245 | 7 | "Length of start of frame differs from expected {expected},value is {length}" | 246 | 7 | ))); | 247 | 4.85k | } | 248 | | | 249 | | trace!("Image components : {}", num_components); | 250 | | | 251 | 4.85k | if num_components == 1 { | 252 | 4.13k | // SOF sets the number of image components | 253 | 4.13k | // and that to us translates to setting input and output | 254 | 4.13k | // colorspaces to zero | 255 | 4.13k | img.input_colorspace = ColorSpace::Luma; | 256 | 4.13k | img.options = img.options.jpeg_set_out_colorspace(ColorSpace::Luma); | 257 | 4.13k | debug!("Overriding default colorspace set to Luma"); | 258 | 4.13k | } | 259 | 4.85k | if num_components == 4 && img.input_colorspace == ColorSpace::YCbCr { | 260 | 530 | trace!("Input image has 4 components, defaulting to CMYK colorspace"); | 261 | 530 | // https://entropymine.wordpress.com/2018/10/22/how-is-a-jpeg-images-color-type-determined/ | 262 | 530 | img.input_colorspace = ColorSpace::CMYK; | 263 | 4.32k | } | 264 | | | 265 | | // set number of components | 266 | 4.85k | img.info.components = num_components; | 267 | | | 268 | 4.85k | let mut components = Vec::with_capacity(num_components as usize); | 269 | 4.85k | let mut temp = [0; 3]; | 270 | | | 271 | 6.83k | for pos in 0..num_components { | 272 | | // read 3 bytes for each component | 273 | 6.83k | img.stream | 274 | 6.83k | .read_exact(&mut temp) | 275 | 6.83k | .map_err(|x| DecodeErrors::Format(format!("Could not read component data\n{x}")))?; | 276 | | // create a component. | 277 | 6.83k | let component = Components::from(temp, pos)?; | 278 | | | 279 | 6.82k | components.push(component); | 280 | | } | 281 | 4.84k | img.seen_sof = true; | 282 | | | 283 | 4.84k | img.info.set_sof_marker(sof); | 284 | | | 285 | 4.84k | img.components = components; | 286 | | | 287 | 4.84k | Ok(()) | 288 | 4.94k | } |
Unexecuted instantiation: zune_jpeg::headers::parse_start_of_frame::<_> |
289 | | |
290 | | /// Parse a start of scan data |
291 | 26.2k | pub(crate) fn parse_sos<T: ZReaderTrait>(image: &mut JpegDecoder<T>) -> Result<(), DecodeErrors> { |
292 | | // Scan header length |
293 | 26.2k | let ls = image.stream.get_u16_be_err()?; |
294 | | // Number of image components in scan |
295 | 26.2k | let ns = image.stream.get_u8_err()?; |
296 | | |
297 | 26.2k | let mut seen = [-1; { MAX_COMPONENTS + 1 }]; |
298 | | |
299 | 26.2k | image.num_scans = ns; |
300 | | |
301 | 26.2k | if ls != 6 + 2 * u16::from(ns) { |
302 | 68 | return Err(DecodeErrors::SosError(format!( |
303 | 68 | "Bad SOS length {ls},corrupt jpeg" |
304 | 68 | ))); |
305 | 26.1k | } |
306 | | |
307 | | // Check number of components. |
308 | 26.1k | if !(1..5).contains(&ns) { |
309 | 3 | return Err(DecodeErrors::SosError(format!( |
310 | 3 | "Number of components in start of scan should be less than 3 but more than 0. Found {ns}" |
311 | 3 | ))); |
312 | 26.1k | } |
313 | | |
314 | 26.1k | if image.info.components == 0 { |
315 | 4 | return Err(DecodeErrors::FormatStatic( |
316 | 4 | "Error decoding SOF Marker, Number of components cannot be zero." |
317 | 4 | )); |
318 | 26.1k | } |
319 | | |
320 | | // consume spec parameters |
321 | 28.1k | for i in 0..ns { |
322 | | // CS_i parameter, I don't need it so I might as well delete it |
323 | 28.1k | let id = image.stream.get_u8_err()?; |
324 | | |
325 | 28.1k | if seen.contains(&i32::from(id)) { |
326 | 5 | return Err(DecodeErrors::SofError(format!( |
327 | 5 | "Duplicate ID {id} seen twice in the same component" |
328 | 5 | ))); |
329 | 28.1k | } |
330 | | |
331 | 28.1k | seen[usize::from(i)] = i32::from(id); |
332 | | // DC and AC huffman table position |
333 | | // top 4 bits contain dc huffman destination table |
334 | | // lower four bits contain ac huffman destination table |
335 | 28.1k | let y = image.stream.get_u8_err()?; |
336 | | |
337 | 28.1k | let mut j = 0; |
338 | | |
339 | 31.9k | while j < image.info.components { |
340 | 31.8k | if image.components[j as usize].id == id { |
341 | 28.0k | break; |
342 | 3.79k | } |
343 | | |
344 | 3.79k | j += 1; |
345 | | } |
346 | | |
347 | 28.1k | if j == image.info.components { |
348 | 30 | return Err(DecodeErrors::SofError(format!( |
349 | 30 | "Invalid component id {}, expected a value between 0 and {}", |
350 | 30 | id, |
351 | 30 | image.components.len() |
352 | 30 | ))); |
353 | 28.0k | } |
354 | | |
355 | 28.0k | image.components[usize::from(j)].dc_huff_table = usize::from((y >> 4) & 0xF); |
356 | 28.0k | image.components[usize::from(j)].ac_huff_table = usize::from(y & 0xF); |
357 | 28.0k | image.z_order[i as usize] = j as usize; |
358 | | } |
359 | | |
360 | | // Collect the component spec parameters |
361 | | // This is only needed for progressive images but I'll read |
362 | | // them in order to ensure they are correct according to the spec |
363 | | |
364 | | // Extract progressive information |
365 | | |
366 | | // https://www.w3.org/Graphics/JPEG/itu-t81.pdf |
367 | | // Page 42 |
368 | | |
369 | | // Start of spectral / predictor selection. (between 0 and 63) |
370 | 26.1k | image.spec_start = image.stream.get_u8_err()?; |
371 | | // End of spectral selection |
372 | 26.0k | image.spec_end = image.stream.get_u8_err()?; |
373 | | |
374 | 26.0k | let bit_approx = image.stream.get_u8_err()?; |
375 | | // successive approximation bit position high |
376 | 26.0k | image.succ_high = bit_approx >> 4; |
377 | | |
378 | 26.0k | if image.spec_end > 63 { |
379 | 10 | return Err(DecodeErrors::SosError(format!( |
380 | 10 | "Invalid Se parameter {}, range should be 0-63", |
381 | 10 | image.spec_end |
382 | 10 | ))); |
383 | 26.0k | } |
384 | 26.0k | if image.spec_start > 63 { |
385 | 6 | return Err(DecodeErrors::SosError(format!( |
386 | 6 | "Invalid Ss parameter {}, range should be 0-63", |
387 | 6 | image.spec_start |
388 | 6 | ))); |
389 | 26.0k | } |
390 | 26.0k | if image.succ_high > 13 { |
391 | 5 | return Err(DecodeErrors::SosError(format!( |
392 | 5 | "Invalid Ah parameter {}, range should be 0-13", |
393 | 5 | image.succ_low |
394 | 5 | ))); |
395 | 26.0k | } |
396 | | // successive approximation bit position low |
397 | 26.0k | image.succ_low = bit_approx & 0xF; |
398 | | |
399 | 26.0k | if image.succ_low > 13 { |
400 | 3 | return Err(DecodeErrors::SosError(format!( |
401 | 3 | "Invalid Al parameter {}, range should be 0-13", |
402 | 3 | image.succ_low |
403 | 3 | ))); |
404 | 26.0k | } |
405 | | |
406 | | trace!( |
407 | | "Ss={}, Se={} Ah={} Al={}", |
408 | | image.spec_start, |
409 | | image.spec_end, |
410 | | image.succ_high, |
411 | | image.succ_low |
412 | | ); |
413 | | |
414 | 26.0k | Ok(()) |
415 | 26.2k | } zune_jpeg::headers::parse_sos::<alloc::vec::Vec<u8>> Line | Count | Source | 291 | 26.2k | pub(crate) fn parse_sos<T: ZReaderTrait>(image: &mut JpegDecoder<T>) -> Result<(), DecodeErrors> { | 292 | | // Scan header length | 293 | 26.2k | let ls = image.stream.get_u16_be_err()?; | 294 | | // Number of image components in scan | 295 | 26.2k | let ns = image.stream.get_u8_err()?; | 296 | | | 297 | 26.2k | let mut seen = [-1; { MAX_COMPONENTS + 1 }]; | 298 | | | 299 | 26.2k | image.num_scans = ns; | 300 | | | 301 | 26.2k | if ls != 6 + 2 * u16::from(ns) { | 302 | 68 | return Err(DecodeErrors::SosError(format!( | 303 | 68 | "Bad SOS length {ls},corrupt jpeg" | 304 | 68 | ))); | 305 | 26.1k | } | 306 | | | 307 | | // Check number of components. | 308 | 26.1k | if !(1..5).contains(&ns) { | 309 | 3 | return Err(DecodeErrors::SosError(format!( | 310 | 3 | "Number of components in start of scan should be less than 3 but more than 0. Found {ns}" | 311 | 3 | ))); | 312 | 26.1k | } | 313 | | | 314 | 26.1k | if image.info.components == 0 { | 315 | 4 | return Err(DecodeErrors::FormatStatic( | 316 | 4 | "Error decoding SOF Marker, Number of components cannot be zero." | 317 | 4 | )); | 318 | 26.1k | } | 319 | | | 320 | | // consume spec parameters | 321 | 28.1k | for i in 0..ns { | 322 | | // CS_i parameter, I don't need it so I might as well delete it | 323 | 28.1k | let id = image.stream.get_u8_err()?; | 324 | | | 325 | 28.1k | if seen.contains(&i32::from(id)) { | 326 | 5 | return Err(DecodeErrors::SofError(format!( | 327 | 5 | "Duplicate ID {id} seen twice in the same component" | 328 | 5 | ))); | 329 | 28.1k | } | 330 | | | 331 | 28.1k | seen[usize::from(i)] = i32::from(id); | 332 | | // DC and AC huffman table position | 333 | | // top 4 bits contain dc huffman destination table | 334 | | // lower four bits contain ac huffman destination table | 335 | 28.1k | let y = image.stream.get_u8_err()?; | 336 | | | 337 | 28.1k | let mut j = 0; | 338 | | | 339 | 31.9k | while j < image.info.components { | 340 | 31.8k | if image.components[j as usize].id == id { | 341 | 28.0k | break; | 342 | 3.79k | } | 343 | | | 344 | 3.79k | j += 1; | 345 | | } | 346 | | | 347 | 28.1k | if j == image.info.components { | 348 | 30 | return Err(DecodeErrors::SofError(format!( | 349 | 30 | "Invalid component id {}, expected a value between 0 and {}", | 350 | 30 | id, | 351 | 30 | image.components.len() | 352 | 30 | ))); | 353 | 28.0k | } | 354 | | | 355 | 28.0k | image.components[usize::from(j)].dc_huff_table = usize::from((y >> 4) & 0xF); | 356 | 28.0k | image.components[usize::from(j)].ac_huff_table = usize::from(y & 0xF); | 357 | 28.0k | image.z_order[i as usize] = j as usize; | 358 | | } | 359 | | | 360 | | // Collect the component spec parameters | 361 | | // This is only needed for progressive images but I'll read | 362 | | // them in order to ensure they are correct according to the spec | 363 | | | 364 | | // Extract progressive information | 365 | | | 366 | | // https://www.w3.org/Graphics/JPEG/itu-t81.pdf | 367 | | // Page 42 | 368 | | | 369 | | // Start of spectral / predictor selection. (between 0 and 63) | 370 | 26.1k | image.spec_start = image.stream.get_u8_err()?; | 371 | | // End of spectral selection | 372 | 26.0k | image.spec_end = image.stream.get_u8_err()?; | 373 | | | 374 | 26.0k | let bit_approx = image.stream.get_u8_err()?; | 375 | | // successive approximation bit position high | 376 | 26.0k | image.succ_high = bit_approx >> 4; | 377 | | | 378 | 26.0k | if image.spec_end > 63 { | 379 | 10 | return Err(DecodeErrors::SosError(format!( | 380 | 10 | "Invalid Se parameter {}, range should be 0-63", | 381 | 10 | image.spec_end | 382 | 10 | ))); | 383 | 26.0k | } | 384 | 26.0k | if image.spec_start > 63 { | 385 | 6 | return Err(DecodeErrors::SosError(format!( | 386 | 6 | "Invalid Ss parameter {}, range should be 0-63", | 387 | 6 | image.spec_start | 388 | 6 | ))); | 389 | 26.0k | } | 390 | 26.0k | if image.succ_high > 13 { | 391 | 5 | return Err(DecodeErrors::SosError(format!( | 392 | 5 | "Invalid Ah parameter {}, range should be 0-13", | 393 | 5 | image.succ_low | 394 | 5 | ))); | 395 | 26.0k | } | 396 | | // successive approximation bit position low | 397 | 26.0k | image.succ_low = bit_approx & 0xF; | 398 | | | 399 | 26.0k | if image.succ_low > 13 { | 400 | 3 | return Err(DecodeErrors::SosError(format!( | 401 | 3 | "Invalid Al parameter {}, range should be 0-13", | 402 | 3 | image.succ_low | 403 | 3 | ))); | 404 | 26.0k | } | 405 | | | 406 | | trace!( | 407 | | "Ss={}, Se={} Ah={} Al={}", | 408 | | image.spec_start, | 409 | | image.spec_end, | 410 | | image.succ_high, | 411 | | image.succ_low | 412 | | ); | 413 | | | 414 | 26.0k | Ok(()) | 415 | 26.2k | } |
Unexecuted instantiation: zune_jpeg::headers::parse_sos::<_> |
416 | | |
417 | | /// Parse the APP13 (IPTC) segment. |
418 | 249 | pub(crate) fn parse_app13<T: ZReaderTrait>( |
419 | 249 | decoder: &mut JpegDecoder<T>, |
420 | 249 | ) -> Result<(), DecodeErrors> { |
421 | | const IPTC_PREFIX: &[u8] = b"Photoshop 3.0"; |
422 | | // skip length. |
423 | 249 | let mut length = usize::from(decoder.stream.get_u16_be()); |
424 | | |
425 | 249 | if length < 2 { |
426 | 5 | return Err(DecodeErrors::FormatStatic("Too small APP13 length")); |
427 | 244 | } |
428 | | // length bytes. |
429 | 244 | length -= 2; |
430 | | |
431 | 244 | if length > IPTC_PREFIX.len() && decoder.stream.peek_at(0, IPTC_PREFIX.len())? == IPTC_PREFIX { |
432 | | // skip bytes we read above. |
433 | 37 | decoder.stream.skip(IPTC_PREFIX.len()); |
434 | 37 | length -= IPTC_PREFIX.len(); |
435 | | |
436 | 37 | let iptc_bytes = decoder.stream.peek_at(0, length)?.to_vec(); |
437 | | |
438 | 26 | decoder.info.iptc_data = Some(iptc_bytes); |
439 | 203 | } |
440 | | |
441 | 229 | decoder.stream.skip(length); |
442 | 229 | Ok(()) |
443 | 249 | } zune_jpeg::headers::parse_app13::<alloc::vec::Vec<u8>> Line | Count | Source | 418 | 249 | pub(crate) fn parse_app13<T: ZReaderTrait>( | 419 | 249 | decoder: &mut JpegDecoder<T>, | 420 | 249 | ) -> Result<(), DecodeErrors> { | 421 | | const IPTC_PREFIX: &[u8] = b"Photoshop 3.0"; | 422 | | // skip length. | 423 | 249 | let mut length = usize::from(decoder.stream.get_u16_be()); | 424 | | | 425 | 249 | if length < 2 { | 426 | 5 | return Err(DecodeErrors::FormatStatic("Too small APP13 length")); | 427 | 244 | } | 428 | | // length bytes. | 429 | 244 | length -= 2; | 430 | | | 431 | 244 | if length > IPTC_PREFIX.len() && decoder.stream.peek_at(0, IPTC_PREFIX.len())? == IPTC_PREFIX { | 432 | | // skip bytes we read above. | 433 | 37 | decoder.stream.skip(IPTC_PREFIX.len()); | 434 | 37 | length -= IPTC_PREFIX.len(); | 435 | | | 436 | 37 | let iptc_bytes = decoder.stream.peek_at(0, length)?.to_vec(); | 437 | | | 438 | 26 | decoder.info.iptc_data = Some(iptc_bytes); | 439 | 203 | } | 440 | | | 441 | 229 | decoder.stream.skip(length); | 442 | 229 | Ok(()) | 443 | 249 | } |
Unexecuted instantiation: zune_jpeg::headers::parse_app13::<_> |
444 | | |
445 | | /// Parse Adobe App14 segment |
446 | 103 | pub(crate) fn parse_app14<T: ZReaderTrait>( |
447 | 103 | decoder: &mut JpegDecoder<T> |
448 | 103 | ) -> Result<(), DecodeErrors> { |
449 | | // skip length |
450 | 103 | let mut length = usize::from(decoder.stream.get_u16_be()); |
451 | | |
452 | 103 | if length < 2 || !decoder.stream.has(length - 2) { |
453 | 42 | return Err(DecodeErrors::ExhaustedData); |
454 | 61 | } |
455 | 61 | if length < 14 { |
456 | 2 | return Err(DecodeErrors::FormatStatic( |
457 | 2 | "Too short of a length for App14 segment" |
458 | 2 | )); |
459 | 59 | } |
460 | 59 | if decoder.stream.peek_at(0, 5) == Ok(b"Adobe") { |
461 | | // move stream 6 bytes to remove adobe id |
462 | 27 | decoder.stream.skip(6); |
463 | | // skip version, flags0 and flags1 |
464 | 27 | decoder.stream.skip(5); |
465 | | // get color transform |
466 | 27 | let transform = decoder.stream.get_u8(); |
467 | | // https://exiftool.org/TagNames/JPEG.html#Adobe |
468 | 27 | match transform { |
469 | 23 | 0 => decoder.input_colorspace = ColorSpace::CMYK, |
470 | 2 | 1 => decoder.input_colorspace = ColorSpace::YCbCr, |
471 | 1 | 2 => decoder.input_colorspace = ColorSpace::YCCK, |
472 | | _ => { |
473 | 1 | return Err(DecodeErrors::Format(format!( |
474 | 1 | "Unknown Adobe colorspace {transform}" |
475 | 1 | ))) |
476 | | } |
477 | | } |
478 | | // length = 2 |
479 | | // adobe id = 6 |
480 | | // version = 5 |
481 | | // transform = 1 |
482 | 26 | length = length.saturating_sub(14); |
483 | 32 | } else if decoder.options.get_strict_mode() { |
484 | 32 | return Err(DecodeErrors::FormatStatic("Corrupt Adobe App14 segment")); |
485 | 0 | } else { |
486 | 0 | length = length.saturating_sub(2); |
487 | 0 | error!("Not a valid Adobe APP14 Segment"); |
488 | 0 | } |
489 | | // skip any proceeding lengths. |
490 | | // we do not need them |
491 | 26 | decoder.stream.skip(length); |
492 | | |
493 | 26 | Ok(()) |
494 | 103 | } zune_jpeg::headers::parse_app14::<alloc::vec::Vec<u8>> Line | Count | Source | 446 | 103 | pub(crate) fn parse_app14<T: ZReaderTrait>( | 447 | 103 | decoder: &mut JpegDecoder<T> | 448 | 103 | ) -> Result<(), DecodeErrors> { | 449 | | // skip length | 450 | 103 | let mut length = usize::from(decoder.stream.get_u16_be()); | 451 | | | 452 | 103 | if length < 2 || !decoder.stream.has(length - 2) { | 453 | 42 | return Err(DecodeErrors::ExhaustedData); | 454 | 61 | } | 455 | 61 | if length < 14 { | 456 | 2 | return Err(DecodeErrors::FormatStatic( | 457 | 2 | "Too short of a length for App14 segment" | 458 | 2 | )); | 459 | 59 | } | 460 | 59 | if decoder.stream.peek_at(0, 5) == Ok(b"Adobe") { | 461 | | // move stream 6 bytes to remove adobe id | 462 | 27 | decoder.stream.skip(6); | 463 | | // skip version, flags0 and flags1 | 464 | 27 | decoder.stream.skip(5); | 465 | | // get color transform | 466 | 27 | let transform = decoder.stream.get_u8(); | 467 | | // https://exiftool.org/TagNames/JPEG.html#Adobe | 468 | 27 | match transform { | 469 | 23 | 0 => decoder.input_colorspace = ColorSpace::CMYK, | 470 | 2 | 1 => decoder.input_colorspace = ColorSpace::YCbCr, | 471 | 1 | 2 => decoder.input_colorspace = ColorSpace::YCCK, | 472 | | _ => { | 473 | 1 | return Err(DecodeErrors::Format(format!( | 474 | 1 | "Unknown Adobe colorspace {transform}" | 475 | 1 | ))) | 476 | | } | 477 | | } | 478 | | // length = 2 | 479 | | // adobe id = 6 | 480 | | // version = 5 | 481 | | // transform = 1 | 482 | 26 | length = length.saturating_sub(14); | 483 | 32 | } else if decoder.options.get_strict_mode() { | 484 | 32 | return Err(DecodeErrors::FormatStatic("Corrupt Adobe App14 segment")); | 485 | 0 | } else { | 486 | 0 | length = length.saturating_sub(2); | 487 | 0 | error!("Not a valid Adobe APP14 Segment"); | 488 | 0 | } | 489 | | // skip any proceeding lengths. | 490 | | // we do not need them | 491 | 26 | decoder.stream.skip(length); | 492 | | | 493 | 26 | Ok(()) | 494 | 103 | } |
Unexecuted instantiation: zune_jpeg::headers::parse_app14::<_> |
495 | | |
496 | | /// Parse the APP1 segment |
497 | | /// |
498 | | /// This contains the exif tag |
499 | 7.13k | pub(crate) fn parse_app1<T: ZReaderTrait>( |
500 | 7.13k | decoder: &mut JpegDecoder<T> |
501 | 7.13k | ) -> Result<(), DecodeErrors> { |
502 | | // contains exif data |
503 | 7.13k | let mut length = usize::from(decoder.stream.get_u16_be()); |
504 | | |
505 | 7.13k | if length < 2 || !decoder.stream.has(length - 2) { |
506 | 34 | return Err(DecodeErrors::ExhaustedData); |
507 | 7.10k | } |
508 | | // length bytes |
509 | 7.10k | length -= 2; |
510 | | |
511 | 7.10k | if length > 6 && decoder.stream.peek_at(0, 6).unwrap() == b"Exif\x00\x00" { |
512 | 267 | trace!("Exif segment present"); |
513 | 267 | // skip bytes we read above |
514 | 267 | decoder.stream.skip(6); |
515 | 267 | length -= 6; |
516 | 267 | |
517 | 267 | let exif_bytes = decoder.stream.peek_at(0, length).unwrap().to_vec(); |
518 | 267 | |
519 | 267 | decoder.exif_data = Some(exif_bytes); |
520 | 6.83k | } else { |
521 | 6.83k | warn!("Wrongly formatted exif tag"); |
522 | 6.83k | } |
523 | | |
524 | 7.10k | decoder.stream.skip(length); |
525 | 7.10k | Ok(()) |
526 | 7.13k | } zune_jpeg::headers::parse_app1::<alloc::vec::Vec<u8>> Line | Count | Source | 499 | 7.13k | pub(crate) fn parse_app1<T: ZReaderTrait>( | 500 | 7.13k | decoder: &mut JpegDecoder<T> | 501 | 7.13k | ) -> Result<(), DecodeErrors> { | 502 | | // contains exif data | 503 | 7.13k | let mut length = usize::from(decoder.stream.get_u16_be()); | 504 | | | 505 | 7.13k | if length < 2 || !decoder.stream.has(length - 2) { | 506 | 34 | return Err(DecodeErrors::ExhaustedData); | 507 | 7.10k | } | 508 | | // length bytes | 509 | 7.10k | length -= 2; | 510 | | | 511 | 7.10k | if length > 6 && decoder.stream.peek_at(0, 6).unwrap() == b"Exif\x00\x00" { | 512 | 267 | trace!("Exif segment present"); | 513 | 267 | // skip bytes we read above | 514 | 267 | decoder.stream.skip(6); | 515 | 267 | length -= 6; | 516 | 267 | | 517 | 267 | let exif_bytes = decoder.stream.peek_at(0, length).unwrap().to_vec(); | 518 | 267 | | 519 | 267 | decoder.exif_data = Some(exif_bytes); | 520 | 6.83k | } else { | 521 | 6.83k | warn!("Wrongly formatted exif tag"); | 522 | 6.83k | } | 523 | | | 524 | 7.10k | decoder.stream.skip(length); | 525 | 7.10k | Ok(()) | 526 | 7.13k | } |
Unexecuted instantiation: zune_jpeg::headers::parse_app1::<_> |
527 | | |
528 | 39.2k | pub(crate) fn parse_app2<T: ZReaderTrait>( |
529 | 39.2k | decoder: &mut JpegDecoder<T> |
530 | 39.2k | ) -> Result<(), DecodeErrors> { |
531 | 39.2k | let mut length = usize::from(decoder.stream.get_u16_be()); |
532 | | |
533 | 39.2k | if length < 2 || !decoder.stream.has(length - 2) { |
534 | 79 | return Err(DecodeErrors::ExhaustedData); |
535 | 39.1k | } |
536 | | // length bytes |
537 | 39.1k | length -= 2; |
538 | | |
539 | 39.1k | if length > 14 && decoder.stream.peek_at(0, 12).unwrap() == *b"ICC_PROFILE\0" { |
540 | 27.7k | trace!("ICC Profile present"); |
541 | 27.7k | // skip 12 bytes which indicate ICC profile |
542 | 27.7k | length -= 12; |
543 | 27.7k | decoder.stream.skip(12); |
544 | 27.7k | let seq_no = decoder.stream.get_u8(); |
545 | 27.7k | let num_markers = decoder.stream.get_u8(); |
546 | 27.7k | // deduct the two bytes we read above |
547 | 27.7k | length -= 2; |
548 | 27.7k | |
549 | 27.7k | let data = decoder.stream.peek_at(0, length).unwrap().to_vec(); |
550 | 27.7k | |
551 | 27.7k | let icc_chunk = ICCChunk { |
552 | 27.7k | seq_no, |
553 | 27.7k | num_markers, |
554 | 27.7k | data |
555 | 27.7k | }; |
556 | 27.7k | decoder.icc_data.push(icc_chunk); |
557 | 27.7k | } |
558 | | |
559 | 39.1k | decoder.stream.skip(length); |
560 | | |
561 | 39.1k | Ok(()) |
562 | 39.2k | } zune_jpeg::headers::parse_app2::<alloc::vec::Vec<u8>> Line | Count | Source | 528 | 39.2k | pub(crate) fn parse_app2<T: ZReaderTrait>( | 529 | 39.2k | decoder: &mut JpegDecoder<T> | 530 | 39.2k | ) -> Result<(), DecodeErrors> { | 531 | 39.2k | let mut length = usize::from(decoder.stream.get_u16_be()); | 532 | | | 533 | 39.2k | if length < 2 || !decoder.stream.has(length - 2) { | 534 | 79 | return Err(DecodeErrors::ExhaustedData); | 535 | 39.1k | } | 536 | | // length bytes | 537 | 39.1k | length -= 2; | 538 | | | 539 | 39.1k | if length > 14 && decoder.stream.peek_at(0, 12).unwrap() == *b"ICC_PROFILE\0" { | 540 | 27.7k | trace!("ICC Profile present"); | 541 | 27.7k | // skip 12 bytes which indicate ICC profile | 542 | 27.7k | length -= 12; | 543 | 27.7k | decoder.stream.skip(12); | 544 | 27.7k | let seq_no = decoder.stream.get_u8(); | 545 | 27.7k | let num_markers = decoder.stream.get_u8(); | 546 | 27.7k | // deduct the two bytes we read above | 547 | 27.7k | length -= 2; | 548 | 27.7k | | 549 | 27.7k | let data = decoder.stream.peek_at(0, length).unwrap().to_vec(); | 550 | 27.7k | | 551 | 27.7k | let icc_chunk = ICCChunk { | 552 | 27.7k | seq_no, | 553 | 27.7k | num_markers, | 554 | 27.7k | data | 555 | 27.7k | }; | 556 | 27.7k | decoder.icc_data.push(icc_chunk); | 557 | 27.7k | } | 558 | | | 559 | 39.1k | decoder.stream.skip(length); | 560 | | | 561 | 39.1k | Ok(()) | 562 | 39.2k | } |
Unexecuted instantiation: zune_jpeg::headers::parse_app2::<_> |
563 | | |
564 | | /// Small utility function to print Un-zig-zagged quantization tables |
565 | | |
566 | 7.55k | fn un_zig_zag<T>(a: &[T]) -> [i32; 64] |
567 | 7.55k | where |
568 | 7.55k | T: Default + Copy, |
569 | 7.55k | i32: core::convert::From<T> |
570 | | { |
571 | 7.55k | let mut output = [i32::default(); 64]; |
572 | | |
573 | 491k | for i in 0..64 { |
574 | 483k | output[UN_ZIGZAG[i]] = i32::from(a[i]); |
575 | 483k | } |
576 | | |
577 | 7.55k | output |
578 | 7.55k | } zune_jpeg::headers::un_zig_zag::<u8> Line | Count | Source | 566 | 7.46k | fn un_zig_zag<T>(a: &[T]) -> [i32; 64] | 567 | 7.46k | where | 568 | 7.46k | T: Default + Copy, | 569 | 7.46k | i32: core::convert::From<T> | 570 | | { | 571 | 7.46k | let mut output = [i32::default(); 64]; | 572 | | | 573 | 485k | for i in 0..64 { | 574 | 477k | output[UN_ZIGZAG[i]] = i32::from(a[i]); | 575 | 477k | } | 576 | | | 577 | 7.46k | output | 578 | 7.46k | } |
zune_jpeg::headers::un_zig_zag::<u16> Line | Count | Source | 566 | 89 | fn un_zig_zag<T>(a: &[T]) -> [i32; 64] | 567 | 89 | where | 568 | 89 | T: Default + Copy, | 569 | 89 | i32: core::convert::From<T> | 570 | | { | 571 | 89 | let mut output = [i32::default(); 64]; | 572 | | | 573 | 5.78k | for i in 0..64 { | 574 | 5.69k | output[UN_ZIGZAG[i]] = i32::from(a[i]); | 575 | 5.69k | } | 576 | | | 577 | 89 | output | 578 | 89 | } |
Unexecuted instantiation: zune_jpeg::headers::un_zig_zag::<_> |