Debian Codegraph @

.cargo/vendor/zune-jpeg/src/headers.rs

raw
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::ZByteReaderTrait;
18 use zune_core::colorspace::ColorSpace;
19 use zune_core::log::{debug, trace, warn};
20
21 use crate::components::Components;
22 use crate::decoder::{GainMapInfo, 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 pub(crate) fn parse_huffman<T: ZByteReaderTrait>(
30 decoder: &mut JpegDecoder<T>
31 ) -> Result<(), DecodeErrors>
32 where
33 {
34 // Read the length of the Huffman table
35 let mut dht_length = i32::from(decoder.stream.get_u16_be_err()?.checked_sub(2).ok_or(
36 DecodeErrors::FormatStatic("Invalid Huffman length in image")
37 )?);
38
39 while dht_length > 16 {
40 // HT information
41 let ht_info = decoder.stream.read_u8_err()?;
42 // third bit indicates whether the huffman encoding is DC or AC type
43 let dc_or_ac = (ht_info >> 4) & 0xF;
44 // Indicate the position of this table, should be less than 4;
45 let index = (ht_info & 0xF) as usize;
46 // read the number of symbols
47 let mut num_symbols: [u8; 17] = [0; 17];
48
49 if index >= MAX_COMPONENTS {
50 return Err(DecodeErrors::HuffmanDecode(format!(
51 "Invalid DHT index {index}, expected between 0 and 3"
52 )));
53 }
54
55 if dc_or_ac > 1 {
56 return Err(DecodeErrors::HuffmanDecode(format!(
57 "Invalid DHT position {dc_or_ac}, should be 0 or 1"
58 )));
59 }
60
61 decoder.stream.read_exact_bytes(&mut num_symbols[1..17])?;
62
63 dht_length -= 1 + 16;
64
65 let symbols_sum: i32 = num_symbols.iter().map(|f| i32::from(*f)).sum();
66
67 // The sum of the number of symbols cannot be greater than 256;
68 if symbols_sum > 256 {
69 return Err(DecodeErrors::FormatStatic(
70 "Encountered Huffman table with excessive length in DHT"
71 ));
72 }
73 if symbols_sum > dht_length {
74 return Err(DecodeErrors::HuffmanDecode(format!(
75 "Excessive Huffman table of length {symbols_sum} found when header length is {dht_length}"
76 )));
77 }
78 dht_length -= symbols_sum;
79 // A table containing symbols in increasing code length
80 let mut symbols = [0; 256];
81
82 decoder
83 .stream
84 .read_exact_bytes(&mut symbols[0..(symbols_sum as usize)])?;
85 // store
86 match dc_or_ac {
87 0 => {
88 decoder.dc_huffman_tables[index] = Some(HuffmanTable::new(
89 &num_symbols,
90 symbols,
91 true,
92 decoder.is_progressive
93 )?);
94 }
95 _ => {
96 decoder.ac_huffman_tables[index] = Some(HuffmanTable::new(
97 &num_symbols,
98 symbols,
99 false,
100 decoder.is_progressive
101 )?);
102 }
103 }
104 }
105
106 if dht_length > 0 {
107 return Err(DecodeErrors::FormatStatic("Bogus Huffman table definition"));
108 }
109
110 Ok(())
111 }
112
113 ///**B.2.4.1 Quantization table-specification syntax**
114 #[allow(clippy::cast_possible_truncation, clippy::needless_range_loop)]
115 pub(crate) fn parse_dqt<T: ZByteReaderTrait>(img: &mut JpegDecoder<T>) -> Result<(), DecodeErrors> {
116 // read length
117 let mut qt_length =
118 img.stream
119 .get_u16_be_err()?
120 .checked_sub(2)
121 .ok_or(DecodeErrors::FormatStatic(
122 "Invalid DQT length. Length should be greater than 2"
123 ))?;
124 // A single DQT header may have multiple QT's
125 while qt_length > 0 {
126 let qt_info = img.stream.read_u8_err()?;
127 // 0 = 8 bit otherwise 16 bit dqt
128 let precision = (qt_info >> 4) as usize;
129 // last 4 bits give us position
130 let table_position = (qt_info & 0x0f) as usize;
131 let precision_value = 64 * (precision + 1);
132
133 if (precision_value + 1) as u16 > qt_length {
134 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)));
135 }
136
137 let dct_table = match precision {
138 0 => {
139 let mut qt_values = [0; 64];
140
141 img.stream.read_exact_bytes(&mut qt_values)?;
142
143 qt_length -= (precision_value as u16) + 1 /*QT BIT*/;
144 // carry out un zig-zag here
145 un_zig_zag(&qt_values)
146 }
147 1 => {
148 // 16 bit quantization tables
149 let mut qt_values = [0_u16; 64];
150
151 for i in 0..64 {
152 qt_values[i] = img.stream.get_u16_be_err()?;
153 }
154 qt_length -= (precision_value as u16) + 1;
155
156 un_zig_zag(&qt_values)
157 }
158 _ => {
159 return Err(DecodeErrors::DqtError(format!(
160 "Expected QT precision value of either 0 or 1, found {precision:?}"
161 )));
162 }
163 };
164
165 if table_position >= MAX_COMPONENTS {
166 return Err(DecodeErrors::DqtError(format!(
167 "Too large table position for QT :{table_position}, expected between 0 and 3"
168 )));
169 }
170
171 trace!("Assigning qt table {table_position} with precision {precision}");
172 img.qt_tables[table_position] = Some(dct_table);
173 }
174
175 return Ok(());
176 }
177
178 /// Section:`B.2.2 Frame header syntax`
179
180 pub(crate) fn parse_start_of_frame<T: ZByteReaderTrait>(
181 sof: SOFMarkers, img: &mut JpegDecoder<T>
182 ) -> Result<(), DecodeErrors> {
183 if img.seen_sof {
184 return Err(DecodeErrors::SofError(
185 "Two Start of Frame Markers".to_string()
186 ));
187 }
188 // Get length of the frame header
189 let length = img.stream.get_u16_be_err()?;
190 // usually 8, but can be 12 and 16, we currently support only 8
191 // so sorry about that 12 bit images
192 let dt_precision = img.stream.read_u8_err()?;
193
194 if dt_precision != 8 {
195 return Err(DecodeErrors::SofError(format!(
196 "The library can only parse 8-bit images, the image has {dt_precision} bits of precision"
197 )));
198 }
199
200 img.info.set_density(dt_precision);
201
202 // read and set the image height.
203 let img_height = img.stream.get_u16_be_err()?;
204 img.info.set_height(img_height);
205
206 // read and set the image width
207 let img_width = img.stream.get_u16_be_err()?;
208 img.info.set_width(img_width);
209
210 trace!("Image width :{}", img_width);
211 trace!("Image height :{}", img_height);
212
213 if usize::from(img_width) > img.options.max_width() {
214 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.max_width())));
215 }
216
217 if usize::from(img_height) > img.options.max_height() {
218 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.max_height())));
219 }
220
221 // Check image width or height is zero
222 if img_width == 0 || img_height == 0 {
223 return Err(DecodeErrors::ZeroError);
224 }
225
226 // Number of components for the image.
227 let num_components = img.stream.read_u8_err()?;
228
229 if num_components == 0 {
230 return Err(DecodeErrors::SofError(
231 "Number of components cannot be zero.".to_string()
232 ));
233 }
234
235 let expected = 8 + 3 * u16::from(num_components);
236 // length should be equal to num components
237 if length != expected {
238 return Err(DecodeErrors::SofError(format!(
239 "Length of start of frame differs from expected {expected},value is {length}"
240 )));
241 }
242
243 trace!("Image components : {}", num_components);
244
245 if num_components == 1 {
246 // SOF sets the number of image components
247 // and that to us translates to setting input and output
248 // colorspaces to zero
249 img.input_colorspace = ColorSpace::Luma;
250 //img.options = img.options.jpeg_set_out_colorspace(ColorSpace::Luma);
251 debug!("Overriding default colorspace set to Luma");
252 }
253 if num_components == 4 && img.input_colorspace == ColorSpace::YCbCr {
254 trace!("Input image has 4 components, defaulting to CMYK colorspace");
255 // https://entropymine.wordpress.com/2018/10/22/how-is-a-jpeg-images-color-type-determined/
256 img.input_colorspace = ColorSpace::CMYK;
257 }
258
259 // set number of components
260 img.info.components = num_components;
261
262 let mut components = Vec::with_capacity(num_components as usize);
263 let mut temp = [0; 3];
264
265 for pos in 0..num_components {
266 // read 3 bytes for each component
267 img.stream.read_exact_bytes(&mut temp)?;
268
269 // create a component.
270 let component = Components::from(temp, pos)?;
271
272 components.push(component);
273 }
274 img.seen_sof = true;
275
276 img.info.set_sof_marker(sof);
277
278 img.components = components;
279
280 Ok(())
281 }
282
283 /// Parse a start of scan data
284 pub(crate) fn parse_sos<T: ZByteReaderTrait>(
285 image: &mut JpegDecoder<T>
286 ) -> Result<(), DecodeErrors> {
287 // Scan header length
288 let ls = usize::from(image.stream.get_u16_be_err()?);
289 // Number of image components in scan
290 let ns = image.stream.read_u8_err()?;
291
292 let mut seen: [_; 5] = [-1; { MAX_COMPONENTS + 1 }];
293
294 image.num_scans = ns;
295 let smallest_size = 6 + 2 * usize::from(ns);
296
297 if ls != smallest_size {
298 return Err(DecodeErrors::SosError(format!(
299 "Bad SOS length {ls},corrupt jpeg"
300 )));
301 }
302
303 // Check number of components.
304 if !(1..5).contains(&ns) {
305 return Err(DecodeErrors::SosError(format!(
306 "Invalid number of components in start of scan {ns}, expected in range 1..5"
307 )));
308 }
309
310 if image.info.components == 0 {
311 return Err(DecodeErrors::FormatStatic(
312 "Error decoding SOF Marker, Number of components cannot be zero."
313 ));
314 }
315
316 // consume spec parameters
317 image.scan_subsampled = false;
318
319 for i in 0..ns {
320 let id = image.stream.read_u8_err()?;
321
322 if seen.contains(&i32::from(id)) {
323 return Err(DecodeErrors::SofError(format!(
324 "Duplicate ID {id} seen twice in the same component"
325 )));
326 }
327
328 seen[usize::from(i)] = i32::from(id);
329 // DC and AC huffman table position
330 // top 4 bits contain dc huffman destination table
331 // lower four bits contain ac huffman destination table
332 let y = image.stream.read_u8_err()?;
333
334 let mut j = 0;
335
336 while j < image.info.components {
337 if image.components[j as usize].id == id {
338 break;
339 }
340
341 j += 1;
342 }
343
344 if j == image.info.components {
345 return Err(DecodeErrors::SofError(format!(
346 "Invalid component id {}, expected one one of {:?}",
347 id,
348 image.components.iter().map(|c| c.id).collect::<Vec<_>>()
349 )));
350 }
351
352 let component = &mut image.components[usize::from(j)];
353 component.dc_huff_table = usize::from((y >> 4) & 0xF);
354 component.ac_huff_table = usize::from(y & 0xF);
355 image.z_order[i as usize] = j as usize;
356
357 if component.vertical_sample != 1 || component.horizontal_sample != 1 {
358 image.scan_subsampled = true;
359 }
360
361 trace!(
362 "Assigned huffman tables {}/{} to component {j}, id={}",
363 image.components[usize::from(j)].dc_huff_table,
364 image.components[usize::from(j)].ac_huff_table,
365 image.components[usize::from(j)].id,
366 );
367 }
368
369 // Collect the component spec parameters
370 // This is only needed for progressive images but I'll read
371 // them in order to ensure they are correct according to the spec
372
373 // Extract progressive information
374
375 // https://www.w3.org/Graphics/JPEG/itu-t81.pdf
376 // Page 42
377
378 // Start of spectral / predictor selection. (between 0 and 63)
379 image.spec_start = image.stream.read_u8_err()?;
380 // End of spectral selection
381 image.spec_end = image.stream.read_u8_err()?;
382
383 let bit_approx = image.stream.read_u8_err()?;
384 // successive approximation bit position high
385 image.succ_high = bit_approx >> 4;
386
387 if image.spec_end > 63 {
388 return Err(DecodeErrors::SosError(format!(
389 "Invalid Se parameter {}, range should be 0-63",
390 image.spec_end
391 )));
392 }
393 if image.spec_start > 63 {
394 return Err(DecodeErrors::SosError(format!(
395 "Invalid Ss parameter {}, range should be 0-63",
396 image.spec_start
397 )));
398 }
399 if image.succ_high > 13 {
400 return Err(DecodeErrors::SosError(format!(
401 "Invalid Ah parameter {}, range should be 0-13",
402 image.succ_low
403 )));
404 }
405 // successive approximation bit position low
406 image.succ_low = bit_approx & 0xF;
407
408 if image.succ_low > 13 {
409 return Err(DecodeErrors::SosError(format!(
410 "Invalid Al parameter {}, range should be 0-13",
411 image.succ_low
412 )));
413 }
414 // skip any bytes not read
415 image.stream.skip(smallest_size.saturating_sub(ls))?;
416
417 trace!(
418 "Ss={}, Se={} Ah={} Al={}",
419 image.spec_start,
420 image.spec_end,
421 image.succ_high,
422 image.succ_low
423 );
424
425 Ok(())
426 }
427
428 /// Parse the APP13 (IPTC) segment.
429 pub(crate) fn parse_app13<T: ZByteReaderTrait>(
430 decoder: &mut JpegDecoder<T>
431 ) -> Result<(), DecodeErrors> {
432 const IPTC_PREFIX: &[u8] = b"Photoshop 3.0\0";
433 // skip length.
434 let mut length = usize::from(decoder.stream.get_u16_be());
435
436 if length < 2 {
437 return Err(DecodeErrors::FormatStatic("Too small APP13 length"));
438 }
439 // length bytes.
440 length -= 2;
441
442 if length > IPTC_PREFIX.len() && decoder.stream.peek_at(0, IPTC_PREFIX.len())? == IPTC_PREFIX {
443 // skip bytes we read above.
444 decoder.stream.skip(IPTC_PREFIX.len())?;
445 length -= IPTC_PREFIX.len();
446
447 let iptc_bytes = decoder.stream.peek_at(0, length)?.to_vec();
448
449 decoder.info.iptc_data = Some(iptc_bytes);
450 }
451
452 decoder.stream.skip(length)?;
453 Ok(())
454 }
455
456 /// Parse Adobe App14 segment
457 pub(crate) fn parse_app14<T: ZByteReaderTrait>(
458 decoder: &mut JpegDecoder<T>
459 ) -> Result<(), DecodeErrors> {
460 // skip length
461 let mut length = usize::from(decoder.stream.get_u16_be());
462
463 if length < 2 {
464 return Err(DecodeErrors::FormatStatic("Too small APP14 length"));
465 }
466
467 if decoder.stream.peek_at(0, 5)? == b"Adobe" {
468 if length < 14 {
469 return Err(DecodeErrors::FormatStatic(
470 "Too short of a length for App14 segment"
471 ));
472 }
473 // move stream 6 bytes to remove adobe id
474 decoder.stream.skip(6)?;
475 // skip version, flags0 and flags1
476 decoder.stream.skip(5)?;
477 // get color transform
478 let transform = decoder.stream.read_u8();
479 // https://exiftool.org/TagNames/JPEG.html#Adobe
480 match transform {
481 0 => decoder.input_colorspace = ColorSpace::CMYK,
482 1 => decoder.input_colorspace = ColorSpace::YCbCr,
483 2 => decoder.input_colorspace = ColorSpace::YCCK,
484 _ => {
485 return Err(DecodeErrors::Format(format!(
486 "Unknown Adobe colorspace {transform}"
487 )))
488 }
489 }
490 // length = 2
491 // adobe id = 6
492 // version = 5
493 // transform = 1
494 length = length.saturating_sub(14);
495 } else {
496 warn!("Not a valid Adobe APP14 Segment, skipping");
497 }
498 // skip any proceeding lengths.
499 // we do not need them
500 decoder.stream.skip(length)?;
501
502 Ok(())
503 }
504
505 /// Parse the APP1 segment
506 ///
507 /// This contains the exif tag
508 pub(crate) fn parse_app1<T: ZByteReaderTrait>(
509 decoder: &mut JpegDecoder<T>
510 ) -> Result<(), DecodeErrors> {
511 const XMP_NAMESPACE_PREFIX: &[u8] = b"http://ns.adobe.com/xap/1.0/\0";
512
513 // contains exif data
514 let mut length = usize::from(decoder.stream.get_u16_be());
515
516 if length < 2 {
517 return Err(DecodeErrors::FormatStatic("Too small app1 length"));
518 }
519 // length bytes
520 length -= 2;
521
522 if length > 6 && decoder.stream.peek_at(0, 6)? == b"Exif\x00\x00" {
523 trace!("Exif segment present");
524 // skip bytes we read above
525 decoder.stream.skip(6)?;
526 length -= 6;
527
528 let exif_bytes = decoder.stream.peek_at(0, length)?.to_vec();
529
530 decoder.info.exif_data = Some(exif_bytes);
531 } else if length > XMP_NAMESPACE_PREFIX.len()
532 && decoder.stream.peek_at(0, XMP_NAMESPACE_PREFIX.len())? == XMP_NAMESPACE_PREFIX
533 {
534 trace!("XMP Data Present");
535 decoder.stream.skip(XMP_NAMESPACE_PREFIX.len())?;
536 length -= XMP_NAMESPACE_PREFIX.len();
537 let xmp_data = decoder.stream.peek_at(0, length)?.to_vec();
538 decoder.info.xmp_data = Some(xmp_data);
539 } else {
540 warn!("Unknown format for APP1 tag, skipping");
541 }
542
543 decoder.stream.skip(length)?;
544 Ok(())
545 }
546
547 pub(crate) fn parse_app2<T: ZByteReaderTrait>(
548 decoder: &mut JpegDecoder<T>
549 ) -> Result<(), DecodeErrors> {
550 static HDR_META: &[u8] = b"urn:iso:std:iso:ts:21496:-1\0";
551 static MPF_DATA: &[u8] = b"MPF\0";
552
553 let mut length = usize::from(decoder.stream.get_u16_be());
554
555 if length < 2 {
556 return Err(DecodeErrors::FormatStatic("Too small app2 segment"));
557 }
558 // length bytes
559 length -= 2;
560
561 if length > 14 && decoder.stream.peek_at(0, 12)? == *b"ICC_PROFILE\0" {
562 trace!("ICC Profile present");
563 // skip 12 bytes which indicate ICC profile
564 length -= 12;
565 decoder.stream.skip(12)?;
566 let seq_no = decoder.stream.read_u8();
567 let num_markers = decoder.stream.read_u8();
568 // deduct the two bytes we read above
569 length -= 2;
570
571 let data = decoder.stream.peek_at(0, length)?.to_vec();
572
573 let icc_chunk = ICCChunk {
574 seq_no,
575 num_markers,
576 data
577 };
578 decoder.icc_data.push(icc_chunk);
579 } else if length > HDR_META.len() && decoder.stream.peek_at(0, HDR_META.len())? == HDR_META {
580 length = length.saturating_sub(HDR_META.len());
581 decoder.stream.skip(HDR_META.len())?;
582 trace!("Gain Map metadata found");
583 match length {
584 4 => {
585 // If gain map metadata length == 4 then here it variables
586 // https://github.com/google/libultrahdr/blob/bf2aa439eea9ad5da483003fa44182f990f74091/lib/src/jpegr.cpp#L1076C1-L1077C35
587 // 2 bytes minimum_version: (00 00)
588 // 2 bytes writer_version: (00 00)
589 // Perhaps nothing to do with it ?
590 let _ = decoder.stream.get_u16_be();
591 let _ = decoder.stream.get_u16_be();
592 length -= 4;
593 decoder
594 .info
595 .gain_map_info
596 .push(GainMapInfo { data: Vec::new() });
597 }
598 n if n > 4 => {
599 // If there is perhaps useful gain map info
600 // we'll read this until end
601 // https://github.com/google/libultrahdr/blob/bf2aa439eea9ad5da483003fa44182f990f74091/lib/src/jpegr.cpp#L1323
602 let data = decoder.stream.peek_at(0, length)?.to_vec();
603 length -= data.len();
604 decoder.stream.skip(data.len())?;
605
606 decoder.info.gain_map_info.push(GainMapInfo { data });
607 }
608 _ => {}
609 }
610 } else if length > MPF_DATA.len() && decoder.stream.peek_at(0, MPF_DATA.len())? == MPF_DATA {
611 trace!("MPF Signature present");
612 length = length.saturating_sub(MPF_DATA.len());
613 decoder.stream.skip(MPF_DATA.len())?;
614 // MPF signature taken from here
615 // https://github.com/google/libultrahdr/blob/bf2aa439eea9ad5da483003fa44182f990f74091/lib/include/ultrahdr/multipictureformat.h#L50
616 // https://github.com/google/libultrahdr/blob/bf2aa439eea9ad5da483003fa44182f990f74091/lib/src/multipictureformat.cpp#L36
617 // More info https://www.cipa.jp/std/documents/e/DC-X007-KEY_E.pdf
618 let data = decoder.stream.peek_at(0, length)?.to_vec();
619 length -= data.len();
620 decoder.stream.skip(data.len())?;
621 decoder.info.multi_picture_information = Some(data);
622 }
623
624 decoder.stream.skip(length)?;
625
626 Ok(())
627 }
628
629 /// Small utility function to print Un-zig-zagged quantization tables
630
631 fn un_zig_zag<T>(a: &[T]) -> [i32; 64]
632 where
633 T: Default + Copy,
634 i32: core::convert::From<T>
635 {
636 let mut output = [i32::default(); 64];
637
638 for i in 0..64 {
639 output[UN_ZIGZAG[i]] = i32::from(a[i]);
640 }
641
642 output
643 }