Debian Codegraph @

.cargo/vendor/zune-jpeg/src/mcu.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 use alloc::vec::Vec;
10 use alloc::{format, vec};
11 use core::cmp::min;
12
13 use zune_core::bytestream::ZByteReaderTrait;
14 use zune_core::colorspace::ColorSpace;
15 use zune_core::colorspace::ColorSpace::Luma;
16 use zune_core::log::{error, trace, warn};
17
18 use crate::bitstream::BitStream;
19 use crate::components::SampleRatios;
20 use crate::decoder::MAX_COMPONENTS;
21 use crate::errors::DecodeErrors;
22 use crate::marker::Marker;
23 use crate::mcu_prog::get_marker;
24 use crate::misc::{calculate_padded_width, setup_component_params};
25 use crate::worker::{color_convert, upsample};
26 use crate::JpegDecoder;
27
28 /// The size of a DC block for a MCU.
29
30 pub const DCT_BLOCK: usize = 64;
31
32 impl<T: ZByteReaderTrait> JpegDecoder<T> {
33 /// Check for existence of DC and AC Huffman Tables
34 pub(crate) fn check_tables(&self) -> Result<(), DecodeErrors> {
35 // check that dc and AC tables exist outside the hot path
36 for component in &self.components {
37 let _ = &self
38 .dc_huffman_tables
39 .get(component.dc_huff_table)
40 .as_ref()
41 .ok_or_else(|| {
42 DecodeErrors::HuffmanDecode(format!(
43 "No Huffman DC table for component {:?} ",
44 component.component_id
45 ))
46 })?
47 .as_ref()
48 .ok_or_else(|| {
49 DecodeErrors::HuffmanDecode(format!(
50 "No DC table for component {:?}",
51 component.component_id
52 ))
53 })?;
54
55 let _ = &self
56 .ac_huffman_tables
57 .get(component.ac_huff_table)
58 .as_ref()
59 .ok_or_else(|| {
60 DecodeErrors::HuffmanDecode(format!(
61 "No Huffman AC table for component {:?} ",
62 component.component_id
63 ))
64 })?
65 .as_ref()
66 .ok_or_else(|| {
67 DecodeErrors::HuffmanDecode(format!(
68 "No AC table for component {:?}",
69 component.component_id
70 ))
71 })?;
72 }
73 Ok(())
74 }
75
76 /// Decode MCUs and carry out post processing.
77 ///
78 /// This is the main decoder loop for the library, the hot path.
79 ///
80 /// Because of this, we pull in some very crazy optimization tricks hence readability is a pinch
81 /// here.
82 #[allow(
83 clippy::similar_names,
84 clippy::too_many_lines,
85 clippy::cast_possible_truncation
86 )]
87 #[inline(never)]
88 pub(crate) fn decode_mcu_ycbcr_baseline(
89 &mut self, pixels: &mut [u8]
90 ) -> Result<(), DecodeErrors> {
91 setup_component_params(self)?;
92
93 // check dc and AC tables
94 self.check_tables()?;
95
96 let (mut mcu_width, mut mcu_height);
97
98 if self.is_interleaved {
99 // set upsampling functions
100 self.set_upsampling()?;
101
102 mcu_width = self.mcu_x;
103 mcu_height = self.mcu_y;
104 } else {
105 // For non-interleaved images( (1*1) subsampling)
106 // number of MCU's are the widths (+7 to account for paddings) divided bu 8.
107 mcu_width = ((self.info.width + 7) / 8) as usize;
108 mcu_height = ((self.info.height + 7) / 8) as usize;
109 }
110 if self.is_interleaved
111 && self.input_colorspace.num_components() > 1
112 && self.options.jpeg_get_out_colorspace().num_components() == 1
113 && (self.sub_sample_ratio == SampleRatios::V
114 || self.sub_sample_ratio == SampleRatios::HV)
115 {
116 // For a specific set of images, e.g interleaved,
117 // when converting from YcbCr to grayscale, we need to
118 // take into account mcu height since the MCU decoding needs to take
119 // it into account for padding purposes and the post processor
120 // parses two rows per mcu width.
121 //
122 // set coeff to be 2 to ensure that we increment two rows
123 // for every mcu processed also
124 mcu_height *= self.v_max;
125 mcu_height /= self.h_max;
126 self.coeff = 2;
127 }
128
129 if self.input_colorspace == ColorSpace::Luma && self.is_interleaved {
130 warn!("Grayscale image with down-sampled component, resetting component details");
131
132 self.reset_params();
133
134 mcu_width = ((self.info.width + 7) / 8) as usize;
135 mcu_height = ((self.info.height + 7) / 8) as usize;
136 }
137 let width = usize::from(self.info.width);
138
139 let padded_width = calculate_padded_width(width, self.sub_sample_ratio);
140
141 let mut stream = BitStream::new();
142 let mut tmp = [0_i32; DCT_BLOCK];
143
144 let comp_len = self.components.len();
145
146 for (pos, comp) in self.components.iter_mut().enumerate() {
147 // Allocate only needed components.
148 //
149 // For special colorspaces i.e YCCK and CMYK, just allocate all of the needed
150 // components.
151 if min(
152 self.options.jpeg_get_out_colorspace().num_components() - 1,
153 pos
154 ) == pos
155 || comp_len == 4
156 // Special colorspace
157 {
158 // allocate enough space to hold a whole MCU width
159 // this means we should take into account sampling ratios
160 // `*8` is because each MCU spans 8 widths.
161 let len = comp.width_stride * comp.vertical_sample * 8;
162
163 comp.needed = true;
164 comp.raw_coeff = vec![0; len];
165 } else {
166 comp.needed = false;
167 }
168 }
169
170 // If all components are contained in the first scan of MCUs, then we can process into
171 // (upsampled) pixels immediately after each MCU, for convenience we use each row of MCUS.
172 // Otherwise, we must first wait until following SOS provide the remaining components.
173 let all_components_in_first_scan = usize::from(self.num_scans) == self.components.len();
174 let mut progressive_mcus: [Vec<i16>; 4] = core::array::from_fn(|_| vec![]);
175
176 if !all_components_in_first_scan {
177 for (component, mcu) in self.components.iter().zip(&mut progressive_mcus) {
178 let len = mcu_width
179 * component.vertical_sample
180 * component.horizontal_sample
181 * mcu_height
182 * 64;
183 *mcu = vec![0; len];
184 }
185 }
186
187 let mut pixels_written = 0;
188
189 let is_hv = usize::from(self.is_interleaved);
190 let upsampler_scratch_size = is_hv * self.components[0].width_stride;
191 let mut upsampler_scratch_space = vec![0; upsampler_scratch_size];
192
193 'sos: loop {
194 trace!(
195 "Baseline decoding of components: {:?}",
196 &self.z_order[..usize::from(self.num_scans)]
197 );
198
199 trace!("Decoding MCU width: {mcu_width}, height: {mcu_height}");
200
201 for i in 0..mcu_height {
202 if stream.overread_by > 0 {
203 pixels.get_mut(pixels_written..).map(|v| v.fill(128));
204 if self.options.strict_mode() {
205 return Err(DecodeErrors::FormatStatic("Premature end of buffer"));
206 };
207
208 error!("Premature end of buffer");
209 break;
210 }
211
212 // decode a whole MCU width,
213 // this takes into account interleaved components.
214 let terminate = if all_components_in_first_scan {
215 self.decode_mcu_width::<false>(
216 mcu_width,
217 i,
218 &mut tmp,
219 &mut stream,
220 &mut progressive_mcus
221 )?
222 } else {
223 /* NB: (cae). This code was added due to the issue at https://github.com/etemesi254/zune-image/issues/277
224 *
225 * There is a particular set of images that interleave the start of scan (SOS) with the MCU,
226 * E.g if it's a three component image, we have SOS->MCU ->SOS->MCU ->SOS->MCU
227 * which presents a problem on decoding, we need to buffer the whole image before continuing since
228 * we won't have a row containing all the component data which will be needed e.g for color conversion.
229 *
230 * The mechanisms is that we decode the whole image upfront, which goes against the normal
231 * routine of decoding MCU width , so this requires more memory upfront than initial routines
232 * but it is a single image out of the many corpuses that exist, so its fine.
233 * (image in test-images/jpeg/sos_news.jpeg)
234
235 * Code contributed by Aurelia Molzer (https://github.com/197g)
236
237 *
238 */
239
240 self.decode_mcu_width::<true>(
241 mcu_width,
242 i,
243 &mut tmp,
244 &mut stream,
245 &mut progressive_mcus
246 )?
247 };
248
249 // process that width up until it's impossible. This is faster than allocation the
250 // full components, which we skipped earlier.
251 if all_components_in_first_scan {
252 self.post_process(
253 pixels,
254 i,
255 mcu_height,
256 width,
257 padded_width,
258 &mut pixels_written,
259 &mut upsampler_scratch_space
260 )?;
261 }
262
263 match terminate {
264 McuContinuation::Ok => {}
265 McuContinuation::AnotherSos if all_components_in_first_scan => {
266 warn!("More than one SOS despite already having all components");
267 return Ok(());
268 }
269 McuContinuation::AnotherSos => continue 'sos,
270 McuContinuation::Terminate => {
271 warn!("Got terminate signal, will not process further");
272 pixels.get_mut(pixels_written..).map(|v| v.fill(128));
273 return Ok(());
274 }
275 }
276 }
277
278 // Breaks if we get here, looping only if we have restarted, i.e. found another SOS and
279 // continued at `'sos'.
280 break;
281 }
282
283 if !all_components_in_first_scan {
284 self.finish_baseline_decoding(&progressive_mcus, mcu_width, pixels)?;
285 }
286
287 // it may happen that some images don't have the whole buffer
288 // so we can't panic in case of that
289 // assert_eq!(pixels_written, pixels.len());
290
291 // For UHD usecases that tie two images separating them with EOI and
292 // SOI markers, it may happen that we do not reach this image end of image
293 // So this ensures we reach it
294 // Ensure we read EOI
295 if !stream.seen_eoi {
296 let marker = get_marker(&mut self.stream, &mut stream);
297 match marker {
298 Ok(_m) => {
299 trace!("Found marker {:?}", _m);
300 }
301 Err(_) => {
302 // ignore error
303 }
304 }
305 }
306
307 trace!("Finished decoding image");
308
309 Ok(())
310 }
311
312 /// Process all MCUs when baseline decoding has been processing them component-after-component.
313 /// For simplicity this assembles the dequantized blocks in the order that the post processing
314 /// of an interleaved baseline decoding would use.
315 #[allow(clippy::too_many_lines)]
316 #[allow(clippy::cast_sign_loss)]
317 pub(crate) fn finish_baseline_decoding(
318 &mut self, block: &[Vec<i16>; MAX_COMPONENTS], _mcu_width: usize, pixels: &mut [u8]
319 ) -> Result<(), DecodeErrors> {
320 let mcu_height = self.mcu_y;
321
322 // Size of our output image(width*height)
323 let is_hv = usize::from(self.is_interleaved);
324 let upsampler_scratch_size = is_hv * self.components[0].width_stride;
325 let width = usize::from(self.info.width);
326 let padded_width = calculate_padded_width(width, self.sub_sample_ratio);
327
328 let mut upsampler_scratch_space = vec![0; upsampler_scratch_size];
329
330 for (pos, comp) in self.components.iter_mut().enumerate() {
331 // Mark only needed components for computing output colors.
332 if min(
333 self.options.jpeg_get_out_colorspace().num_components() - 1,
334 pos
335 ) == pos
336 || self.input_colorspace == ColorSpace::YCCK
337 || self.input_colorspace == ColorSpace::CMYK
338 {
339 comp.needed = true;
340 } else {
341 comp.needed = false;
342 }
343 }
344
345 let mut pixels_written = 0;
346
347 // dequantize and idct have been performed, only color convert.
348 for i in 0..mcu_height {
349 // All the data is already in the right order, we just need to be able to pass it to
350 // the post_process & upsample method. That expects all the data to be stored as one
351 // row of MCUs in each component's `raw_coeff`.
352 'component: for (position, component) in &mut self.components.iter_mut().enumerate() {
353 if !component.needed {
354 continue 'component;
355 }
356
357 // step is the number of pixels this iteration wil be handling
358 // Given by the number of mcu's height and the length of the component block
359 // Since the component block contains the whole channel as raw pixels
360 // we this evenly divides the pixels into MCU blocks
361 //
362 // For interleaved images, this gives us the exact pixels comprising a whole MCU
363 // block
364 let step = block[position].len() / mcu_height;
365
366 // where we will be reading our pixels from.
367 let slice = &block[position][i * step..][..step];
368 let temp_channel = &mut component.raw_coeff;
369 temp_channel[..step].copy_from_slice(slice);
370 }
371
372 // process that whole stripe of MCUs
373 self.post_process(
374 pixels,
375 i,
376 mcu_height,
377 width,
378 padded_width,
379 &mut pixels_written,
380 &mut upsampler_scratch_space
381 )?;
382 }
383
384 return Ok(());
385 }
386
387 fn decode_mcu_width<const PROGRESSIVE: bool>(
388 &mut self, mcu_width: usize, mcu_height: usize, tmp: &mut [i32; 64],
389 stream: &mut BitStream, progressive: &mut [Vec<i16>; 4]
390 ) -> Result<McuContinuation, DecodeErrors> {
391 let is_one_by_one = !self.scan_subsampled;
392
393 // The definition of MCU depends on the sampling factor of involved scans. When components
394 // have different factors then each Minimal-Coding-Unit is the least common multiple such
395 // that we have an integer number of blocks from each component. But the decoding of these
396 // components differs from it otherwise, we need an inner loop with a dynamic amount of
397 // coefficients per component, whereas otherwise we have exactly one block of coefficients
398 // encoded for each component in the bitstream order.
399 //
400 // We statically specialize on this to improve code generation of the common case a little
401 // bit. We could also special case common sub-sampling cases but be mindful of code bloat.
402 if is_one_by_one {
403 self.inner_decode_mcu_width::<PROGRESSIVE, false>(
404 mcu_width,
405 mcu_height,
406 tmp,
407 stream,
408 progressive
409 )
410 } else {
411 self.inner_decode_mcu_width::<PROGRESSIVE, true>(
412 mcu_width,
413 mcu_height,
414 tmp,
415 stream,
416 progressive
417 )
418 }
419 }
420
421 // Inline-never ensures we do get this function optimize on its own, into two different
422 // versions, without the optimizer tripping up over the complexity that comes with the
423 // constant folding. And constant folding is quite important for performance here as
424 // when `not SAMPLED` then the inner loop has exactly one iteration per component in
425 // the scan. The difference was ~1% or a bit more.
426 fn inner_decode_mcu_width<const PROGRESSIVE: bool, const SAMPLED: bool>(
427 &mut self, mcu_width: usize, mcu_height: usize, tmp: &mut [i32; 64],
428 stream: &mut BitStream, progressive: &mut [Vec<i16>; 4]
429 ) -> Result<McuContinuation, DecodeErrors> {
430 let z_order = self.z_order;
431 let z_scans = &z_order[..usize::from(self.num_scans)];
432
433 // How much of the head of `tmp` was written by the last MCU decoding? We only check for
434 // two different cases and not all possible outcomes as this is only used to optimize the
435 // bytes written in `fill`. Since the clobber happens in UNZIGZAG order we'd be straddling
436 // most cache lines anyways even if we did a partial write with the exact length of the
437 // coefficient data which was written into `tmp`.
438 let mut clobber_more_than_4x4 = true;
439
440 for j in 0..mcu_width {
441 // iterate over components
442 for &k in z_scans {
443 // we made this loop body massive due to several different paths that depend on
444 // static conditions. Note we (potentially) call into other functions so the
445 // compiler will not unroll anything here anyways. The gains from separating
446 // differently optimized loop bodies are much greater than a single additional jump
447 // here.
448 let component = &mut self.components[k];
449
450 let dc_table = self.dc_huffman_tables[component.dc_huff_table % MAX_COMPONENTS]
451 .as_ref()
452 .ok_or(DecodeErrors::FormatStatic("DC table not found"))?;
453
454 let ac_table = self.ac_huffman_tables[component.ac_huff_table % MAX_COMPONENTS]
455 .as_ref()
456 .ok_or(DecodeErrors::FormatStatic("AC table not found"))?;
457
458 let qt_table = &component.quantization_table;
459 let channel = if PROGRESSIVE {
460 let offset =
461 mcu_height * component.width_stride * 8 * component.vertical_sample;
462 &mut progressive[k][offset..]
463 } else {
464 &mut component.raw_coeff
465 };
466
467 let component_samples_needed = component.needed;
468
469 // If image is interleaved iterate over scan components,
470 // otherwise if it-s non-interleaved, these routines iterate in
471 // trivial scanline order(Y,Cb,Cr)
472 //
473 // Turn the bounds into a compile time constant for a common special case. This
474 // allows the compiler to unroll the loop and then do a bunch of interleaving.
475 let v_step = if SAMPLED { 0..component.vertical_sample } else { 0..1 };
476
477 for v_samp in v_step {
478 let h_step = if SAMPLED { 0..component.horizontal_sample } else { 0..1 };
479
480 for h_samp in h_step {
481 let result = if component_samples_needed {
482 // Fill the array with zeroes, decode_mcu_block expects
483 // a zero based array. Clobber is in zig-zag order though.
484 // Writing consecutive entries is basically free in terms
485 // of memory throughput so we opt for a larger power of
486 // two which lets the compiler turn this into a repeated
487 // write of a zeroed vector register, which does not have
488 // any branches, instead of a more difficult pattern where
489 // we attempt to overwrite exactly one coefficient.
490 let clobber_len = if !clobber_more_than_4x4 { 32 } else { 64 };
491
492 tmp[..clobber_len].fill(0);
493
494 stream.decode_mcu_block(
495 &mut self.stream,
496 dc_table,
497 ac_table,
498 qt_table,
499 tmp,
500 &mut component.dc_pred
501 )
502 } else {
503 // We do not touch tmp so there is no need to reset it.
504 stream.discard_mcu_block(&mut self.stream, dc_table, ac_table)
505 };
506
507 // If an error occurs we can either propagate it
508 // as an error or print it and call terminate.
509 //
510 // This allows even corrupt images to render something,
511 // even if its bad, matching browsers.
512 //
513 // See example in https://github.com/etemesi254/zune-image/issues/293
514 let len = if let Ok(len) = result {
515 len
516 } else {
517 // result.is_err()
518 return if self.options.strict_mode() {
519 Err(result.err().unwrap())
520 } else {
521 error!("{}", result.err().unwrap());
522 Ok(McuContinuation::Terminate)
523 };
524 };
525
526 if component_samples_needed {
527 // tmp was only written partially, note that len is in ZigZag order.
528 clobber_more_than_4x4 = len > 10;
529
530 let idct_position = {
531 // derived from stb and rewritten for my tastes
532 let c2 = v_samp * 8;
533 let c3 = ((j * component.horizontal_sample) + h_samp) * 8;
534
535 component.width_stride * c2 + c3
536 };
537
538 let idct_pos = channel.get_mut(idct_position..).unwrap();
539
540 if len <= 1 {
541 (self.idct_1x1_func)(tmp, idct_pos, component.width_stride);
542 } else if len <= 10 {
543 (self.idct_4x4_func)(tmp, idct_pos, component.width_stride);
544 } else {
545 // call idct.
546 (self.idct_func)(tmp, idct_pos, component.width_stride);
547 }
548 }
549 }
550 }
551 }
552
553 self.todo = self.todo.wrapping_sub(1);
554
555 if self.todo == 0 {
556 self.handle_rst_main(stream)?;
557 continue;
558 }
559
560 if stream.marker.is_some() && stream.bits_left == 0 {
561 break;
562 }
563 }
564
565 self.check_stream_marker_after_mcu_width(stream)
566 }
567
568 fn check_stream_marker_after_mcu_width(
569 &mut self, stream: &mut BitStream
570 ) -> Result<McuContinuation, DecodeErrors> {
571 // After all interleaved components, that's an MCU
572 // handle stream markers
573 //
574 // In some corrupt images, it may occur that header markers occur in the stream.
575 // The spec EXPLICITLY FORBIDS this, specifically, in
576 // routine F.2.2.5 it says
577 // `The only valid marker which may occur within the Huffman coded data is the RSTm marker.`
578 //
579 // But libjpeg-turbo allows it because of some weird reason. so I'll also
580 // allow it because of some weird reason.
581 if let Some(m) = stream.marker {
582 if m == Marker::EOI {
583 // acknowledge and ignore EOI marker.
584 stream.marker.take();
585 trace!("Found EOI marker");
586 // Google Introduced the Ultra-HD image format which is basically
587 // stitching two images into one container.
588 // They basically separate two images via a EOI and SOI marker
589 // so let's just ensure if we ever see EOI, we never read past that
590 // ever.
591 // https://github.com/google/libultrahdr
592 stream.seen_eoi = true;
593 } else if let Marker::RST(_) = m {
594 //debug_assert_eq!(self.todo, 0);
595 if self.todo == 0 {
596 self.handle_rst(stream)?;
597 }
598 } else if let Marker::SOS = m {
599 self.parse_marker_inner(m)?;
600 stream.marker.take();
601 stream.reset();
602 trace!("Found SOS marker");
603 return Ok(McuContinuation::AnotherSos);
604 } else {
605 if self.options.strict_mode() {
606 return Err(DecodeErrors::Format(format!(
607 "Marker {m:?} found where not expected"
608 )));
609 }
610 error!(
611 "Marker `{:?}` Found within Huffman Stream, possibly corrupt jpeg",
612 m
613 );
614
615 self.parse_marker_inner(m)?;
616 stream.marker.take();
617 stream.reset();
618 return Ok(McuContinuation::Terminate);
619 }
620 }
621
622 Ok(McuContinuation::Ok)
623 }
624
625 // handle RST markers.
626 // No-op if not using restarts
627 // this routine is shared with mcu_prog
628 #[cold]
629 pub(crate) fn handle_rst(&mut self, stream: &mut BitStream) -> Result<(), DecodeErrors> {
630 self.todo = self.restart_interval;
631
632 if let Some(marker) = stream.marker {
633 // Found a marker
634 // Read stream and see what marker is stored there
635 match marker {
636 Marker::RST(_) => {
637 // reset stream
638 stream.reset();
639 // Initialize dc predictions to zero for all components
640 self.components.iter_mut().for_each(|x| x.dc_pred = 0);
641 // Start iterating again. from position.
642 }
643 Marker::EOI => {
644 // silent pass
645 }
646 _ => {
647 return Err(DecodeErrors::MCUError(format!(
648 "Marker {marker:?} found in bitstream, possibly corrupt jpeg"
649 )));
650 }
651 }
652 }
653 Ok(())
654 }
655 #[allow(clippy::too_many_lines, clippy::too_many_arguments)]
656 pub(crate) fn post_process(
657 &mut self, pixels: &mut [u8], i: usize, mcu_height: usize, width: usize,
658 padded_width: usize, pixels_written: &mut usize, upsampler_scratch_space: &mut [i16]
659 ) -> Result<(), DecodeErrors> {
660 let out_colorspace_components = self.options.jpeg_get_out_colorspace().num_components();
661
662 let mut px = *pixels_written;
663 // indicates whether image is vertically up-sampled
664 let is_vertically_sampled = self
665 .components
666 .iter()
667 .any(|c| c.sample_ratio == SampleRatios::HV || c.sample_ratio == SampleRatios::V);
668
669 let mut comp_len = self.components.len();
670
671 // If we are moving from YCbCr -> Luma, we do not allocate storage for other components, so we
672 // will panic when we are trying to read samples, so for that case,
673 // hardcode it so that we don't panic when doing
674 // *samp = &samples[j][pos * padded_width..(pos + 1) * padded_width]
675 if out_colorspace_components < comp_len && self.options.jpeg_get_out_colorspace() == Luma {
676 comp_len = out_colorspace_components;
677 }
678 let mut color_conv_function =
679 |num_iters: usize, samples: [&[i16]; 4]| -> Result<(), DecodeErrors> {
680 for (pos, output) in pixels[px..]
681 .chunks_exact_mut(width * out_colorspace_components)
682 .take(num_iters)
683 .enumerate()
684 {
685 let mut raw_samples: [&[i16]; 4] = [&[], &[], &[], &[]];
686
687 // iterate over each line, since color-convert needs only
688 // one line
689 for (j, samp) in raw_samples.iter_mut().enumerate().take(comp_len) {
690 let temp = &samples[j].get(pos * padded_width..(pos + 1) * padded_width);
691 if temp.is_none() {
692 return Err(DecodeErrors::FormatStatic("Missing samples"));
693 }
694 *samp = temp.unwrap();
695 }
696 color_convert(
697 &raw_samples,
698 self.color_convert_16,
699 self.input_colorspace,
700 self.options.jpeg_get_out_colorspace(),
701 output,
702 width,
703 padded_width
704 )?;
705 px += width * out_colorspace_components;
706 }
707 Ok(())
708 };
709
710 let comps = &mut self.components[..];
711
712 if self.is_interleaved && self.options.jpeg_get_out_colorspace() != ColorSpace::Luma {
713 for comp in comps.iter_mut() {
714 upsample(
715 comp,
716 mcu_height,
717 i,
718 upsampler_scratch_space,
719 is_vertically_sampled
720 )?;
721 }
722
723 if is_vertically_sampled {
724 if i > 0 {
725 // write the last line, it wasn't up-sampled as we didn't have row_down
726 // yet
727 let mut samples: [&[i16]; 4] = [&[], &[], &[], &[]];
728
729 for (samp, component) in samples.iter_mut().zip(comps.iter()) {
730 *samp = &component.first_row_upsample_dest;
731 }
732
733 // ensure length matches for all samples
734 let _first_len = samples[0].len();
735
736 // This was a good check, but can be caused to panic, esp on invalid/corrupt images.
737 // See one in issue https://github.com/etemesi254/zune-image/issues/262, so for now
738 // we just ignore and generate invalid images at the end.
739
740 //
741 //
742 // for samp in samples.iter().take(comp_len) {
743 // assert_eq!(first_len, samp.len());
744 // }
745 let num_iters = self.coeff * self.v_max;
746
747 color_conv_function(num_iters, samples)?;
748 }
749
750 // After up-sampling the last row, save any row that can be used for
751 // a later up-sampling,
752 //
753 // E.g the Y sample is not sampled but we haven't finished upsampling the last row of
754 // the previous mcu, since we don't have the down row, so save it
755 for component in comps.iter_mut() {
756 if component.sample_ratio != SampleRatios::H {
757 // We don't care about H sampling factors, since it's copied in the workers function
758
759 // copy last row to be used for the next color conversion
760 let size = component.vertical_sample
761 * component.width_stride
762 * component.sample_ratio.sample();
763
764 let last_bytes =
765 component.raw_coeff.rchunks_exact_mut(size).next().unwrap();
766
767 component
768 .first_row_upsample_dest
769 .copy_from_slice(last_bytes);
770 }
771 }
772 }
773
774 let mut samples: [&[i16]; 4] = [&[], &[], &[], &[]];
775
776 for (samp, component) in samples.iter_mut().zip(comps.iter()) {
777 *samp = if component.sample_ratio == SampleRatios::None {
778 &component.raw_coeff
779 } else {
780 &component.upsample_dest
781 };
782 }
783
784 // we either do 7 or 8 MCU's depending on the state, this only applies to
785 // vertically sampled images
786 //
787 // for rows up until the last MCU, we do not upsample the last stride of the MCU
788 // which means that the number of iterations should take that into account is one less the
789 // up-sampled size
790 //
791 // For the last MCU, we upsample the last stride, meaning that if we hit the last MCU, we
792 // should sample full raw coeffs
793 let is_last_considered = is_vertically_sampled && (i != mcu_height.saturating_sub(1));
794
795 let num_iters = (8 - usize::from(is_last_considered)) * self.coeff * self.v_max;
796
797 color_conv_function(num_iters, samples)?;
798 } else {
799 let mut channels_ref: [&[i16]; MAX_COMPONENTS] = [&[]; MAX_COMPONENTS];
800
801 self.components
802 .iter()
803 .enumerate()
804 .for_each(|(pos, x)| channels_ref[pos] = &x.raw_coeff);
805
806 if let SampleRatios::Generic(_, v) = self.sub_sample_ratio {
807 color_conv_function(8 * v * self.coeff, channels_ref)?;
808 } else {
809 color_conv_function(8 * self.coeff, channels_ref)?;
810 }
811 }
812
813 *pixels_written = px;
814 Ok(())
815 }
816 }
817
818 enum McuContinuation {
819 Ok,
820 AnotherSos,
821 Terminate
822 }