team-ljyzipelo

공개
674줄27 KB
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//!Routines for progressive decoding
10/*
11This file is needlessly complicated,
12
13It is that way to ensure we don't burn memory anyhow
14
15Memory is a scarce resource in some environments, I would like this to be viable
16in such environments
17
18Half of the complexity comes from the jpeg spec, because progressive decoding,
19is one hell of a ride.
20
21*/
22use alloc::string::ToString;
23use alloc::vec::Vec;
24use alloc::{format, vec};
25use core::cmp::min;
26
27use zune_core::bytestream::{ZByteReaderTrait, ZReader};
28use zune_core::colorspace::ColorSpace;
29use zune_core::log::{debug, error, warn};
30
31use crate::bitstream::BitStream;
32use crate::components::SampleRatios;
33use crate::decoder::{JpegDecoder, MAX_COMPONENTS};
34use crate::errors::DecodeErrors;
35use crate::headers::parse_sos;
36use crate::marker::Marker;
37use crate::mcu::DCT_BLOCK;
38use crate::misc::{calculate_padded_width, setup_component_params};
39
40impl<T: ZByteReaderTrait> JpegDecoder<T> {
41 /// Decode a progressive image
42 ///
43 /// This routine decodes a progressive image, stopping if it finds any error.
44 #[allow(
45 clippy::needless_range_loop,
46 clippy::cast_sign_loss,
47 clippy::redundant_else,
48 clippy::too_many_lines
49 )]
50 #[inline(never)]
51 pub(crate) fn decode_mcu_ycbcr_progressive(
52 &mut self, pixels: &mut [u8]
53 ) -> Result<(), DecodeErrors> {
54 setup_component_params(self)?;
55
56 let mut mcu_height;
57
58 // memory location for decoded pixels for components
59 let mut block: [Vec<i16>; MAX_COMPONENTS] = [vec![], vec![], vec![], vec![]];
60 let mut mcu_width;
61
62 let mut seen_scans = 1;
63
64 if self.input_colorspace == ColorSpace::Luma && self.is_interleaved {
65 warn!("Grayscale image with down-sampled component, resetting component details");
66 self.reset_params();
67 }
68
69 if self.is_interleaved {
70 // this helps us catch component errors.
71 self.set_upsampling()?;
72 }
73 if self.is_interleaved {
74 mcu_width = self.mcu_x;
75 mcu_height = self.mcu_y;
76 } else {
77 mcu_width = (self.info.width as usize + 7) / 8;
78 mcu_height = (self.info.height as usize + 7) / 8;
79 }
80 if self.is_interleaved
81 && self.input_colorspace.num_components() > 1
82 && self.options.jpeg_get_out_colorspace().num_components() == 1
83 && (self.info.sample_ratio == SampleRatios::V
84 || self.info.sample_ratio == SampleRatios::HV)
85 {
86 // For a specific set of images, e.g interleaved,
87 // when converting from YcbCr to grayscale, we need to
88 // take into account mcu height since the MCU decoding needs to take
89 // it into account for padding purposes and the post processor
90 // parses two rows per mcu width.
91 //
92 // set coeff to be 2 to ensure that we increment two rows
93 // for every mcu processed also
94 mcu_height *= self.v_max;
95 mcu_height /= self.h_max;
96 self.coeff = 2;
97 }
98
99 mcu_width *= 64;
100
101 for i in 0..self.input_colorspace.num_components() {
102 let comp = &self.components[i];
103 let len = mcu_width * comp.vertical_sample * comp.horizontal_sample * mcu_height;
104
105 block[i] = vec![0; len];
106 }
107
108 let mut stream = BitStream::new_progressive(self.succ_low, self.spec_start, self.spec_end);
109
110 // there are multiple scans in the stream, this should resolve the first scan
111 let result = self.parse_entropy_coded_data(&mut stream, &mut block);
112
113 if result.is_err() {
114 return if self.options.strict_mode() {
115 Err(result.err().unwrap())
116 } else {
117 error!("{}", result.err().unwrap());
118 // Go process it and return as much as we can, exiting here
119 return self.finish_progressive_decoding(&block, pixels);
120 };
121 }
122
123 // extract marker
124 let mut marker = stream
125 .marker
126 .take()
127 .ok_or(DecodeErrors::FormatStatic("Marker missing where expected"))?;
128
129 // if marker is EOI, we are done, otherwise continue scanning.
130 //
131 // In case we have a premature image, we print a warning or return
132 // an error, depending on the strictness of the decoder, so there
133 // is that logic to handle too
134 'eoi: while marker != Marker::EOI {
135 match marker {
136 Marker::SOS => {
137 parse_sos(self)?;
138
139 stream.update_progressive_params(
140 self.succ_high,
141 self.succ_low,
142 self.spec_start,
143 self.spec_end
144 );
145 // after every SOS, marker, parse data for that scan.
146 let result = self.parse_entropy_coded_data(&mut stream, &mut block);
147
148 // Do not error out too fast, allows the decoder to continue as much as possible
149 // even after errors
150 if result.is_err() {
151 return if self.options.strict_mode() {
152 Err(result.err().unwrap())
153 } else {
154 error!("{}", result.err().unwrap());
155 break 'eoi;
156 };
157 }
158 // extract marker, might either indicate end of image or we continue
159 // scanning(hence the continue statement to determine).
160 match get_marker(&mut self.stream, &mut stream) {
161 Ok(marker_n) => {
162 marker = marker_n;
163 seen_scans += 1;
164 if seen_scans > self.options.jpeg_get_max_scans() {
165 return Err(DecodeErrors::Format(format!(
166 "Too many scans, exceeded limit of {}",
167 self.options.jpeg_get_max_scans()
168 )));
169 }
170
171 stream.reset();
172 continue 'eoi;
173 }
174 Err(msg) => {
175 if self.options.strict_mode() {
176 return Err(msg);
177 }
178 error!("{:?}", msg);
179 break 'eoi;
180 }
181 }
182 }
183 Marker::RST(_n) => {
184 self.handle_rst(&mut stream)?;
185 }
186 _ => {
187 self.parse_marker_inner(marker)?;
188 }
189 }
190
191 match get_marker(&mut self.stream, &mut stream) {
192 Ok(marker_n) => {
193 marker = marker_n;
194 }
195 Err(e) => {
196 if self.options.strict_mode() {
197 return Err(e);
198 }
199 error!("{}", e);
200 // If we can't get the marker, just break away
201 // allows us to decode some corrupt images
202 // e.g https://github.com/etemesi254/zune-image/issues/294
203 break 'eoi;
204 }
205 }
206 }
207
208 self.finish_progressive_decoding(&block, pixels)
209 }
210
211 /// Reset progressive parameters
212 fn reset_prog_params(&mut self, stream: &mut BitStream) {
213 stream.reset();
214 self.components.iter_mut().for_each(|x| x.dc_pred = 0);
215
216 // Also reset JPEG restart intervals
217 self.todo = if self.restart_interval != 0 { self.restart_interval } else { usize::MAX };
218 }
219
220 #[allow(clippy::too_many_lines, clippy::cast_sign_loss)]
221 fn parse_entropy_coded_data(
222 &mut self, stream: &mut BitStream, buffer: &mut [Vec<i16>; MAX_COMPONENTS]
223 ) -> Result<(), DecodeErrors> {
224 self.reset_prog_params(stream);
225
226 if usize::from(self.num_scans) > self.input_colorspace.num_components() {
227 return Err(DecodeErrors::Format(format!(
228 "Number of scans {} cannot be greater than number of components, {}",
229 self.num_scans,
230 self.input_colorspace.num_components()
231 )));
232 }
233 if self.num_scans == 1 {
234 // Safety checks
235 if self.spec_end != 0 && self.spec_start == 0 {
236 return Err(DecodeErrors::FormatStatic(
237 "Can't merge DC and AC corrupt jpeg"
238 ));
239 }
240 // non interleaved data, process one block at a time in trivial scanline order
241
242 let k = self.z_order[0];
243
244 if k >= self.components.len() {
245 return Err(DecodeErrors::Format(format!(
246 "Cannot find component {k}, corrupt image"
247 )));
248 }
249 // For non-interleaved scans, iterate over the component's actual data-unit grid.
250 let component = &self.components[k];
251
252 let mcu_width = (self.info.width as usize * component.horizontal_sample).div_ceil(self.h_max * 8);
253 let mcu_height = (self.info.height as usize * component.vertical_sample).div_ceil(self.v_max * 8);
254
255 for i in 0..mcu_height {
256 for j in 0..mcu_width {
257 if self.spec_start != 0 && self.succ_high == 0 && stream.eob_run > 0 {
258 // handle EOB runs here.
259 stream.eob_run -= 1;
260 } else {
261 let start = 64 * (j + i * (self.components[k].width_stride / 8));
262
263 let data: &mut [i16; 64] = buffer
264 .get_mut(k)
265 .unwrap()
266 .get_mut(start..start + 64)
267 .ok_or(DecodeErrors::FormatStatic("Slice to Small"))?
268 .try_into()
269 .unwrap();
270
271 if self.spec_start == 0 {
272 let pos = self.components[k].dc_huff_table & (MAX_COMPONENTS - 1);
273 let dc_table = self
274 .dc_huffman_tables
275 .get(pos)
276 .ok_or(DecodeErrors::FormatStatic(
277 "No huffman table for DC component"
278 ))?
279 .as_ref()
280 .ok_or(DecodeErrors::FormatStatic(
281 "Huffman table at index {} not initialized"
282 ))?;
283
284 let dc_pred = &mut self.components[k].dc_pred;
285
286 if self.succ_high == 0 {
287 // first scan for this mcu
288 stream.decode_prog_dc_first(
289 &mut self.stream,
290 dc_table,
291 &mut data[0],
292 dc_pred
293 )?;
294 } else {
295 // refining scans for this MCU
296 stream.decode_prog_dc_refine(&mut self.stream, &mut data[0])?;
297 }
298 } else {
299 let pos = self.components[k].ac_huff_table;
300 let ac_table = self
301 .ac_huffman_tables
302 .get(pos)
303 .ok_or_else(|| {
304 DecodeErrors::Format(format!(
305 "No huffman table for component:{pos}"
306 ))
307 })?
308 .as_ref()
309 .ok_or_else(|| {
310 DecodeErrors::Format(format!(
311 "Huffman table at index {pos} not initialized"
312 ))
313 })?;
314
315 if self.succ_high == 0 {
316 debug_assert!(stream.eob_run == 0, "EOB run is not zero");
317
318 stream.decode_mcu_ac_first(&mut self.stream, ac_table, data)?;
319 } else {
320 // refinement scan
321 stream.decode_mcu_ac_refine(&mut self.stream, ac_table, data)?;
322 }
323 // Check for a marker.
324 // It can appear in stream CC https://github.com/etemesi254/zune-image/issues/300
325 // if let Some(marker) = stream.marker.take() {
326 // self.parse_marker_inner(marker)?;
327 // }
328 }
329 }
330
331 // + EOB and investigate effect.
332 self.todo -= 1;
333
334 self.handle_rst_main(stream)?;
335 }
336 }
337 } else {
338 if self.spec_end != 0 {
339 return Err(DecodeErrors::HuffmanDecode(
340 "Can't merge dc and AC corrupt jpeg".to_string()
341 ));
342 }
343 // process scan n elements in order
344
345 // Do the error checking with allocs here.
346 // Make the one in the inner loop free of allocations.
347 for k in 0..self.num_scans {
348 let n = self.z_order[k as usize];
349
350 if n >= self.components.len() {
351 return Err(DecodeErrors::Format(format!(
352 "Cannot find component {n}, corrupt image"
353 )));
354 }
355
356 let component = &mut self.components[n];
357 let _ = self
358 .dc_huffman_tables
359 .get(component.dc_huff_table)
360 .ok_or_else(|| {
361 DecodeErrors::Format(format!(
362 "No huffman table for component:{}",
363 component.dc_huff_table
364 ))
365 })?
366 .as_ref()
367 .ok_or_else(|| {
368 DecodeErrors::Format(format!(
369 "Huffman table at index {} not initialized",
370 component.dc_huff_table
371 ))
372 })?;
373 }
374 // Interleaved scan
375
376 // Components shall not be interleaved in progressive mode, except for
377 // the DC coefficients in the first scan for each component of a progressive frame.
378 for i in 0..self.mcu_y {
379 for j in 0..self.mcu_x {
380 // process scan n elements in order
381 for k in 0..self.num_scans {
382 let n = self.z_order[k as usize];
383 let component = &mut self.components[n];
384 let huff_table = self
385 .dc_huffman_tables
386 .get(component.dc_huff_table)
387 .ok_or(DecodeErrors::FormatStatic("No huffman table for component"))?
388 .as_ref()
389 .ok_or(DecodeErrors::FormatStatic(
390 "Huffman table at index not initialized"
391 ))?;
392
393 for v_samp in 0..component.vertical_sample {
394 for h_samp in 0..component.horizontal_sample {
395 let x2 = j * component.horizontal_sample + h_samp;
396 let y2 = i * component.vertical_sample + v_samp;
397 let position = 64 * (x2 + y2 * component.width_stride / 8);
398 let buf_n = &mut buffer[n];
399
400 let Some(data) = &mut buf_n.get_mut(position) else {
401 // TODO: (CAE), this is another weird sub-sampling bug, so on fix
402 // remove this
403 return Err(DecodeErrors::FormatStatic("Invalid image"));
404 };
405
406 if self.succ_high == 0 {
407 stream.decode_prog_dc_first(
408 &mut self.stream,
409 huff_table,
410 data,
411 &mut component.dc_pred
412 )?;
413 } else {
414 stream.decode_prog_dc_refine(&mut self.stream, data)?;
415 }
416 }
417 }
418 }
419 // We want wrapping subtraction here because it means
420 // we get a higher number in the case this underflows
421 self.todo -= 1;
422 // after every scan that's a mcu, count down restart markers.
423 self.handle_rst_main(stream)?;
424 }
425 }
426 }
427 return Ok(());
428 }
429
430 pub(crate) fn handle_rst_main(&mut self, stream: &mut BitStream) -> Result<(), DecodeErrors> {
431 stream.require_input()?;
432 if self.todo == 0 {
433 stream.refill(&mut self.stream)?;
434 }
435
436 if self.todo == 0
437 && self.restart_interval != 0
438 && stream.marker.is_none()
439 && !stream.seen_eoi
440 {
441 // if no marker and we are to reset RST, look for the marker, this matches
442 // libjpeg-turbo behaviour and allows us to decode images in
443 // https://github.com/etemesi254/zune-image/issues/261
444 let _start = self.stream.position()?;
445 // skip bytes until we find marker
446 let marker = get_marker(&mut self.stream, stream);
447
448 // In some images, the RST marker on the last section may not be available
449 // as it is maybe stopped by an EOI marker, see in the case of https://github.com/etemesi254/zune-image/issues/292
450 // what happened was that we would go looking for the RST marker exhausting all the data
451 // in the image and this would return an error, so for now
452 // translate it to a warning, but return the image decoded up
453 // until that point
454 if let Ok(marker) = marker {
455 let _end = self.stream.position()?;
456 stream.marker = Some(marker);
457 // NB some warnings may be false positives.
458 warn!(
459 "{} Extraneous bytes before marker {:?}",
460 _end - _start,
461 marker
462 );
463 } else {
464 warn!("RST marker was not found, where expected, image may be garbled")
465 }
466 }
467 if self.todo == 0 {
468 self.handle_rst(stream)?
469 }
470 Ok(())
471 }
472 #[allow(clippy::too_many_lines)]
473 #[allow(clippy::needless_range_loop, clippy::cast_sign_loss)]
474 fn finish_progressive_decoding(
475 &mut self, block: &[Vec<i16>; MAX_COMPONENTS], pixels: &mut [u8]
476 ) -> Result<(), DecodeErrors> {
477 // This function is complicated because we need to replicate
478 // the function in mcu.rs
479 //
480 // The advantage is that we do very little allocation and very lot
481 // channel reusing.
482 // The trick is to notice that we repeat the same procedure per MCU
483 // width.
484 //
485 // So we can set it up that we only allocate temporary storage large enough
486 // to store a single mcu width, then reuse it per invocation.
487 //
488 // This is advantageous to us.
489 //
490 // Remember we need to have the whole MCU buffer so we store 3 unprocessed
491 // channels in memory, and then we allocate the whole output buffer in memory, both of
492 // which are huge.
493 //
494 //
495
496 let mcu_height = if self.is_interleaved {
497 self.mcu_y
498 } else {
499 // For non-interleaved images( (1*1) subsampling)
500 // number of MCU's are the widths (+7 to account for paddings) divided by 8.
501 self.info.height.div_ceil(8) as usize
502 };
503
504 // Size of our output image(width*height)
505 let is_hv = usize::from(self.is_interleaved);
506 let upsampler_scratch_size = is_hv * self.components[0].width_stride;
507 let width = usize::from(self.info.width);
508 let padded_width = calculate_padded_width(width, self.info.sample_ratio);
509
510 let mut upsampler_scratch_space = vec![0; upsampler_scratch_size];
511 let mut tmp = [0_i32; DCT_BLOCK];
512
513 for (pos, comp) in self.components.iter_mut().enumerate() {
514 // Allocate only needed components.
515 //
516 // For special colorspaces i.e YCCK and CMYK, just allocate all of the needed
517 // components.
518 if min(
519 self.options.jpeg_get_out_colorspace().num_components() - 1,
520 pos
521 ) == pos
522 || self.input_colorspace == ColorSpace::YCCK
523 || self.input_colorspace == ColorSpace::CMYK
524 {
525 // allocate enough space to hold a whole MCU width
526 // this means we should take into account sampling ratios
527 // `*8` is because each MCU spans 8 widths.
528 let len = comp.width_stride * comp.vertical_sample * 8;
529
530 comp.needed = true;
531 comp.raw_coeff = vec![0; len];
532 } else {
533 comp.needed = false;
534 }
535 }
536
537 let mut pixels_written = 0;
538
539 // dequantize, idct and color convert.
540 for i in 0..mcu_height {
541 'component: for (position, component) in &mut self.components.iter_mut().enumerate() {
542 if !component.needed {
543 continue 'component;
544 }
545 let qt_table = &component.quantization_table;
546
547 // step is the number of pixels this iteration wil be handling
548 // Given by the number of mcu's height and the length of the component block
549 // Since the component block contains the whole channel as raw pixels
550 // we this evenly divides the pixels into MCU blocks
551 //
552 // For interleaved images, this gives us the exact pixels comprising a whole MCU
553 // block
554 let step = block[position].len() / mcu_height;
555 // where we will be reading our pixels from.
556 let start = i * step;
557
558 let slice = &block[position][start..start + step];
559
560 let temp_channel = &mut component.raw_coeff;
561
562 // The next logical step is to iterate width wise.
563 // To figure out how many pixels we iterate by we use effective pixels
564 // Given to us by component.x
565 // iterate per effective pixels.
566 let mcu_x = component.width_stride / 8;
567
568 // iterate per every vertical sample.
569 for k in 0..component.vertical_sample {
570 for j in 0..mcu_x {
571 // after writing a single stride, we need to skip 8 rows.
572 // This does the row calculation
573 let width_stride = k * 8 * component.width_stride;
574 let start = j * 64 + width_stride;
575
576 // See https://github.com/etemesi254/zune-image/issues/262 sample 3.
577 let Some(qt_slice) = slice.get(start..start + 64) else {
578 return Err(DecodeErrors::FormatStatic(
579 "Invalid slice , would panic, invalid image"
580 ));
581 };
582 // dequantize
583 for ((x, out), qt_val) in
584 qt_slice.iter().zip(tmp.iter_mut()).zip(qt_table.iter())
585 {
586 *out = i32::from(*x) * qt_val;
587 }
588 // determine where to write.
589 let sl = &mut temp_channel[component.idct_pos..];
590
591 component.idct_pos += 8;
592 // tmp now contains a dequantized block so idct it
593 (self.idct_func)(&mut tmp, sl, component.width_stride);
594 }
595 // after every write of 8, skip 7 since idct write stride wise 8 times.
596 //
597 // Remember each MCU is 8x8 block, so each idct will write 8 strides into
598 // sl
599 //
600 // and component.idct_pos is one stride long
601 component.idct_pos += 7 * component.width_stride;
602 }
603 component.idct_pos = 0;
604 }
605
606 // process that width up until it's impossible
607 self.post_process(
608 pixels,
609 i,
610 mcu_height,
611 width,
612 padded_width,
613 &mut pixels_written,
614 &mut upsampler_scratch_space
615 )?;
616 }
617
618 debug!("Finished decoding image");
619
620 return Ok(());
621 }
622 pub(crate) fn reset_params(&mut self) {
623 /*
624 Apparently, grayscale images which can be down sampled exists, which is weird in the sense
625 that it has one component Y, which is not usually down sampled.
626
627 This means some calculations will be wrong, so for that we explicitly reset params
628 for such occurrences, warn and reset the image info to appear as if it were
629 a non-sampled image to ensure decoding works
630 */
631 self.h_max = 1;
632 self.v_max = 1;
633 self.info.sample_ratio = SampleRatios::None;
634 self.is_interleaved = false;
635 self.components[0].vertical_sample = 1;
636 self.components[0].width_stride = (((self.info.width as usize) + 7) / 8) * 8;
637 self.components[0].horizontal_sample = 1;
638 }
639}
640
641///Get a marker from the bit-stream.
642///
643/// This reads until it gets a marker or end of file is encountered
644pub fn get_marker<T>(
645 reader: &mut ZReader<T>, stream: &mut BitStream
646) -> Result<Marker, DecodeErrors>
647where
648 T: ZByteReaderTrait
649{
650 if let Some(marker) = stream.marker {
651 stream.marker = None;
652 return Ok(marker);
653 }
654
655 // read until we get a marker
656
657 while !reader.eof()? {
658 let marker = reader.read_u8_err()?;
659
660 if marker == 255 {
661 let mut r = reader.read_u8_err()?;
662 // 0xFF 0XFF(some images may be like that)
663 while r == 0xFF {
664 r = reader.read_u8_err()?;
665 }
666
667 if r != 0 {
668 return Marker::from_u8(r)
669 .ok_or_else(|| DecodeErrors::Format(format!("Unknown marker 0xFF{r:X}")));
670 }
671 }
672 }
673 return Err(DecodeErrors::ExhaustedData);
674}