4 * This file was part of the Independent JPEG Group's software:
5 * Copyright (C) 1994-1996, Thomas G. Lane.
6 * libjpeg-turbo Modifications:
7 * Copyright (C) 2010, 2015-2020, 2022-2023, D. R. Commander.
8 * Copyright (C) 2015, Google, Inc.
9 * For conditions of distribution and use, see the accompanying README.ijg
12 * This file contains application interface code for the decompression half
13 * of the JPEG library. These are the "standard" API routines that are
14 * used in the normal full-decompression case. They are not used by a
15 * transcoding-only application. Note that if an application links in
16 * jpeg_start_decompress, it will end up linking in the entire decompressor.
17 * We thus must separate this file from jdapimin.c to avoid linking the
18 * whole decompression library into a transcoder.
22 #if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
26 #define JPEG_INTERNALS
34 #if BITS_IN_JSAMPLE == 8
36 /* Forward declarations */
37 LOCAL(boolean) output_pass_setup(j_decompress_ptr cinfo);
41 * Decompression initialization.
42 * jpeg_read_header must be completed before calling this.
44 * If a multipass operating mode was selected, this will do all but the
45 * last pass, and thus may take a great deal of time.
47 * Returns FALSE if suspended. The return value need be inspected only if
48 * a suspending data source is used.
52 jpeg_start_decompress(j_decompress_ptr cinfo)
54 if (cinfo->global_state == DSTATE_READY) {
55 /* First call: initialize master control, select active modules */
56 jinit_master_decompress(cinfo);
57 if (cinfo->buffered_image) {
58 /* No more work here; expecting jpeg_start_output next */
59 cinfo->global_state = DSTATE_BUFIMAGE;
62 cinfo->global_state = DSTATE_PRELOAD;
64 if (cinfo->global_state == DSTATE_PRELOAD) {
65 /* If file has multiple scans, absorb them all into the coef buffer */
66 if (cinfo->inputctl->has_multiple_scans) {
67 #ifdef D_MULTISCAN_FILES_SUPPORTED
70 /* Call progress monitor hook if present */
71 if (cinfo->progress != NULL)
72 (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
73 /* Absorb some more input */
74 retcode = (*cinfo->inputctl->consume_input) (cinfo);
75 if (retcode == JPEG_SUSPENDED)
77 if (retcode == JPEG_REACHED_EOI)
79 /* Advance progress counter if appropriate */
80 if (cinfo->progress != NULL &&
81 (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
82 if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
83 /* jdmaster underestimated number of scans; ratchet up one scan */
84 cinfo->progress->pass_limit += (long)cinfo->total_iMCU_rows;
89 ERREXIT(cinfo, JERR_NOT_COMPILED);
90 #endif /* D_MULTISCAN_FILES_SUPPORTED */
92 cinfo->output_scan_number = cinfo->input_scan_number;
93 } else if (cinfo->global_state != DSTATE_PRESCAN)
94 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
95 /* Perform any dummy output passes, and set up for the final pass */
96 return output_pass_setup(cinfo);
101 * Set up for an output pass, and perform any dummy pass(es) needed.
102 * Common subroutine for jpeg_start_decompress and jpeg_start_output.
103 * Entry: global_state = DSTATE_PRESCAN only if previously suspended.
104 * Exit: If done, returns TRUE and sets global_state for proper output mode.
105 * If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.
109 output_pass_setup(j_decompress_ptr cinfo)
111 if (cinfo->global_state != DSTATE_PRESCAN) {
112 /* First call: do pass setup */
113 (*cinfo->master->prepare_for_output_pass) (cinfo);
114 cinfo->output_scanline = 0;
115 cinfo->global_state = DSTATE_PRESCAN;
117 /* Loop over any required dummy passes */
118 while (cinfo->master->is_dummy_pass) {
119 #ifdef QUANT_2PASS_SUPPORTED
120 /* Crank through the dummy pass */
121 while (cinfo->output_scanline < cinfo->output_height) {
122 JDIMENSION last_scanline;
123 /* Call progress monitor hook if present */
124 if (cinfo->progress != NULL) {
125 cinfo->progress->pass_counter = (long)cinfo->output_scanline;
126 cinfo->progress->pass_limit = (long)cinfo->output_height;
127 (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
129 /* Process some data */
130 last_scanline = cinfo->output_scanline;
131 #ifdef D_LOSSLESS_SUPPORTED
132 if (cinfo->data_precision == 16)
133 (*cinfo->main->process_data_16) (cinfo, (J16SAMPARRAY)NULL,
134 &cinfo->output_scanline,
138 if (cinfo->data_precision == 12)
139 (*cinfo->main->process_data_12) (cinfo, (J12SAMPARRAY)NULL,
140 &cinfo->output_scanline,
143 (*cinfo->main->process_data) (cinfo, (JSAMPARRAY)NULL,
144 &cinfo->output_scanline, (JDIMENSION)0);
145 if (cinfo->output_scanline == last_scanline)
146 return FALSE; /* No progress made, must suspend */
148 /* Finish up dummy pass, and set up for another one */
149 (*cinfo->master->finish_output_pass) (cinfo);
150 (*cinfo->master->prepare_for_output_pass) (cinfo);
151 cinfo->output_scanline = 0;
153 ERREXIT(cinfo, JERR_NOT_COMPILED);
154 #endif /* QUANT_2PASS_SUPPORTED */
156 /* Ready for application to drive output pass through
157 * _jpeg_read_scanlines or _jpeg_read_raw_data.
159 cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
163 #endif /* BITS_IN_JSAMPLE == 8 */
166 #if BITS_IN_JSAMPLE != 16
169 * Enable partial scanline decompression
171 * Must be called after jpeg_start_decompress() and before any calls to
172 * _jpeg_read_scanlines() or _jpeg_skip_scanlines().
174 * Refer to libjpeg.txt for more information.
178 _jpeg_crop_scanline(j_decompress_ptr cinfo, JDIMENSION *xoffset,
181 int ci, align, orig_downsampled_width;
182 JDIMENSION input_xoffset;
183 boolean reinit_upsampler = FALSE;
184 jpeg_component_info *compptr;
185 #ifdef UPSAMPLE_MERGING_SUPPORTED
186 my_master_ptr master = (my_master_ptr)cinfo->master;
189 if (cinfo->data_precision != BITS_IN_JSAMPLE)
190 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
192 if (cinfo->master->lossless)
193 ERREXIT(cinfo, JERR_NOTIMPL);
195 if ((cinfo->global_state != DSTATE_SCANNING &&
196 cinfo->global_state != DSTATE_BUFIMAGE) || cinfo->output_scanline != 0)
197 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
199 if (!xoffset || !width)
200 ERREXIT(cinfo, JERR_BAD_CROP_SPEC);
202 /* xoffset and width must fall within the output image dimensions. */
203 if (*width == 0 || *xoffset + *width > cinfo->output_width)
204 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
206 /* No need to do anything if the caller wants the entire width. */
207 if (*width == cinfo->output_width)
210 /* Ensuring the proper alignment of xoffset is tricky. At minimum, it
211 * must align with an MCU boundary, because:
213 * (1) The IDCT is performed in blocks, and it is not feasible to modify
214 * the algorithm so that it can transform partial blocks.
215 * (2) Because of the SIMD extensions, any input buffer passed to the
216 * upsampling and color conversion routines must be aligned to the
217 * SIMD word size (for instance, 128-bit in the case of SSE2.) The
218 * easiest way to accomplish this without copying data is to ensure
219 * that upsampling and color conversion begin at the start of the
220 * first MCU column that will be inverse transformed.
222 * In practice, we actually impose a stricter alignment requirement. We
223 * require that xoffset be a multiple of the maximum MCU column width of all
224 * of the components (the "iMCU column width.") This is to simplify the
225 * single-pass decompression case, allowing us to use the same MCU column
226 * width for all of the components.
228 if (cinfo->comps_in_scan == 1 && cinfo->num_components == 1)
229 align = cinfo->_min_DCT_scaled_size;
231 align = cinfo->_min_DCT_scaled_size * cinfo->max_h_samp_factor;
233 /* Adjust xoffset to the nearest iMCU boundary <= the requested value */
234 input_xoffset = *xoffset;
235 *xoffset = (input_xoffset / align) * align;
237 /* Adjust the width so that the right edge of the output image is as
238 * requested (only the left edge is altered.) It is important that calling
239 * programs check this value after this function returns, so that they can
240 * allocate an output buffer with the appropriate size.
242 *width = *width + input_xoffset - *xoffset;
243 cinfo->output_width = *width;
244 #ifdef UPSAMPLE_MERGING_SUPPORTED
245 if (master->using_merged_upsample && cinfo->max_v_samp_factor == 2) {
246 my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
247 upsample->out_row_width =
248 cinfo->output_width * cinfo->out_color_components;
252 /* Set the first and last iMCU columns that we must decompress. These values
253 * will be used in single-scan decompressions.
255 cinfo->master->first_iMCU_col = (JDIMENSION)(long)(*xoffset) / (long)align;
256 cinfo->master->last_iMCU_col =
257 (JDIMENSION)jdiv_round_up((long)(*xoffset + cinfo->output_width),
260 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
262 int hsf = (cinfo->comps_in_scan == 1 && cinfo->num_components == 1) ?
263 1 : compptr->h_samp_factor;
265 /* Set downsampled_width to the new output width. */
266 orig_downsampled_width = compptr->downsampled_width;
267 compptr->downsampled_width =
268 (JDIMENSION)jdiv_round_up((long)cinfo->output_width *
269 (long)(compptr->h_samp_factor *
270 compptr->_DCT_scaled_size),
271 (long)(cinfo->max_h_samp_factor *
272 cinfo->_min_DCT_scaled_size));
273 if (compptr->downsampled_width < 2 && orig_downsampled_width >= 2)
274 reinit_upsampler = TRUE;
276 /* Set the first and last iMCU columns that we must decompress. These
277 * values will be used in multi-scan decompressions.
279 cinfo->master->first_MCU_col[ci] =
280 (JDIMENSION)(long)(*xoffset * hsf) / (long)align;
281 cinfo->master->last_MCU_col[ci] =
282 (JDIMENSION)jdiv_round_up((long)((*xoffset + cinfo->output_width) * hsf),
286 if (reinit_upsampler) {
287 cinfo->master->jinit_upsampler_no_alloc = TRUE;
288 _jinit_upsampler(cinfo);
289 cinfo->master->jinit_upsampler_no_alloc = FALSE;
293 #endif /* BITS_IN_JSAMPLE != 16 */
297 * Read some scanlines of data from the JPEG decompressor.
299 * The return value will be the number of lines actually read.
300 * This may be less than the number requested in several cases,
301 * including bottom of image, data source suspension, and operating
302 * modes that emit multiple scanlines at a time.
304 * Note: we warn about excess calls to _jpeg_read_scanlines() since
305 * this likely signals an application programmer error. However,
306 * an oversize buffer (max_lines > scanlines remaining) is not an error.
310 _jpeg_read_scanlines(j_decompress_ptr cinfo, _JSAMPARRAY scanlines,
311 JDIMENSION max_lines)
313 #if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
316 if (cinfo->data_precision != BITS_IN_JSAMPLE)
317 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
319 if (cinfo->global_state != DSTATE_SCANNING)
320 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
321 if (cinfo->output_scanline >= cinfo->output_height) {
322 WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
326 /* Call progress monitor hook if present */
327 if (cinfo->progress != NULL) {
328 cinfo->progress->pass_counter = (long)cinfo->output_scanline;
329 cinfo->progress->pass_limit = (long)cinfo->output_height;
330 (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
333 /* Process some data */
335 (*cinfo->main->_process_data) (cinfo, scanlines, &row_ctr, max_lines);
336 cinfo->output_scanline += row_ctr;
339 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
345 #if BITS_IN_JSAMPLE != 16
347 /* Dummy color convert function used by _jpeg_skip_scanlines() */
349 noop_convert(j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,
350 JDIMENSION input_row, _JSAMPARRAY output_buf, int num_rows)
355 /* Dummy quantize function used by _jpeg_skip_scanlines() */
357 noop_quantize(j_decompress_ptr cinfo, _JSAMPARRAY input_buf,
358 _JSAMPARRAY output_buf, int num_rows)
364 * In some cases, it is best to call _jpeg_read_scanlines() and discard the
365 * output, rather than skipping the scanlines, because this allows us to
366 * maintain the internal state of the context-based upsampler. In these cases,
367 * we set up and tear down a dummy color converter in order to avoid valgrind
368 * errors and to achieve the best possible performance.
372 read_and_discard_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)
375 #ifdef UPSAMPLE_MERGING_SUPPORTED
376 my_master_ptr master = (my_master_ptr)cinfo->master;
378 _JSAMPLE dummy_sample[1] = { 0 };
379 _JSAMPROW dummy_row = dummy_sample;
380 _JSAMPARRAY scanlines = NULL;
381 void (*color_convert) (j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,
382 JDIMENSION input_row, _JSAMPARRAY output_buf,
383 int num_rows) = NULL;
384 void (*color_quantize) (j_decompress_ptr cinfo, _JSAMPARRAY input_buf,
385 _JSAMPARRAY output_buf, int num_rows) = NULL;
387 if (cinfo->cconvert && cinfo->cconvert->_color_convert) {
388 color_convert = cinfo->cconvert->_color_convert;
389 cinfo->cconvert->_color_convert = noop_convert;
390 /* This just prevents UBSan from complaining about adding 0 to a NULL
391 * pointer. The pointer isn't actually used.
393 scanlines = &dummy_row;
396 if (cinfo->cquantize && cinfo->cquantize->_color_quantize) {
397 color_quantize = cinfo->cquantize->_color_quantize;
398 cinfo->cquantize->_color_quantize = noop_quantize;
401 #ifdef UPSAMPLE_MERGING_SUPPORTED
402 if (master->using_merged_upsample && cinfo->max_v_samp_factor == 2) {
403 my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
404 scanlines = &upsample->spare_row;
408 for (n = 0; n < num_lines; n++)
409 _jpeg_read_scanlines(cinfo, scanlines, 1);
412 cinfo->cconvert->_color_convert = color_convert;
415 cinfo->cquantize->_color_quantize = color_quantize;
420 * Called by _jpeg_skip_scanlines(). This partially skips a decompress block
421 * by incrementing the rowgroup counter.
425 increment_simple_rowgroup_ctr(j_decompress_ptr cinfo, JDIMENSION rows)
427 JDIMENSION rows_left;
428 my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
429 my_master_ptr master = (my_master_ptr)cinfo->master;
431 if (master->using_merged_upsample && cinfo->max_v_samp_factor == 2) {
432 read_and_discard_scanlines(cinfo, rows);
436 /* Increment the counter to the next row group after the skipped rows. */
437 main_ptr->rowgroup_ctr += rows / cinfo->max_v_samp_factor;
439 /* Partially skipping a row group would involve modifying the internal state
440 * of the upsampler, so read the remaining rows into a dummy buffer instead.
442 rows_left = rows % cinfo->max_v_samp_factor;
443 cinfo->output_scanline += rows - rows_left;
445 read_and_discard_scanlines(cinfo, rows_left);
449 * Skips some scanlines of data from the JPEG decompressor.
451 * The return value will be the number of lines actually skipped. If skipping
452 * num_lines would move beyond the end of the image, then the actual number of
453 * lines remaining in the image is returned. Otherwise, the return value will
454 * be equal to num_lines.
456 * Refer to libjpeg.txt for more information.
460 _jpeg_skip_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)
462 my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
463 my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
464 my_master_ptr master = (my_master_ptr)cinfo->master;
465 my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
468 JDIMENSION lines_per_iMCU_row, lines_left_in_iMCU_row, lines_after_iMCU_row;
469 JDIMENSION lines_to_skip, lines_to_read;
471 if (cinfo->data_precision != BITS_IN_JSAMPLE)
472 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
474 if (cinfo->master->lossless)
475 ERREXIT(cinfo, JERR_NOTIMPL);
477 /* Two-pass color quantization is not supported. */
478 if (cinfo->quantize_colors && cinfo->two_pass_quantize)
479 ERREXIT(cinfo, JERR_NOTIMPL);
481 if (cinfo->global_state != DSTATE_SCANNING)
482 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
484 /* Do not skip past the bottom of the image. */
485 if (cinfo->output_scanline + num_lines >= cinfo->output_height) {
486 num_lines = cinfo->output_height - cinfo->output_scanline;
487 cinfo->output_scanline = cinfo->output_height;
488 (*cinfo->inputctl->finish_input_pass) (cinfo);
489 cinfo->inputctl->eoi_reached = TRUE;
496 lines_per_iMCU_row = cinfo->_min_DCT_scaled_size * cinfo->max_v_samp_factor;
497 lines_left_in_iMCU_row =
498 (lines_per_iMCU_row - (cinfo->output_scanline % lines_per_iMCU_row)) %
500 lines_after_iMCU_row = num_lines - lines_left_in_iMCU_row;
502 /* Skip the lines remaining in the current iMCU row. When upsampling
503 * requires context rows, we need the previous and next rows in order to read
504 * the current row. This adds some complexity.
506 if (cinfo->upsample->need_context_rows) {
507 /* If the skipped lines would not move us past the current iMCU row, we
508 * read the lines and ignore them. There might be a faster way of doing
509 * this, but we are facing increasing complexity for diminishing returns.
510 * The increasing complexity would be a by-product of meddling with the
511 * state machine used to skip context rows. Near the end of an iMCU row,
512 * the next iMCU row may have already been entropy-decoded. In this unique
513 * case, we will read the next iMCU row if we cannot skip past it as well.
515 if ((num_lines < lines_left_in_iMCU_row + 1) ||
516 (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full &&
517 lines_after_iMCU_row < lines_per_iMCU_row + 1)) {
518 read_and_discard_scanlines(cinfo, num_lines);
522 /* If the next iMCU row has already been entropy-decoded, make sure that
523 * we do not skip too far.
525 if (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full) {
526 cinfo->output_scanline += lines_left_in_iMCU_row + lines_per_iMCU_row;
527 lines_after_iMCU_row -= lines_per_iMCU_row;
529 cinfo->output_scanline += lines_left_in_iMCU_row;
532 /* If we have just completed the first block, adjust the buffer pointers */
533 if (main_ptr->iMCU_row_ctr == 0 ||
534 (main_ptr->iMCU_row_ctr == 1 && lines_left_in_iMCU_row > 2))
535 set_wraparound_pointers(cinfo);
536 main_ptr->buffer_full = FALSE;
537 main_ptr->rowgroup_ctr = 0;
538 main_ptr->context_state = CTX_PREPARE_FOR_IMCU;
539 if (!master->using_merged_upsample) {
540 upsample->next_row_out = cinfo->max_v_samp_factor;
541 upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
545 /* Skipping is much simpler when context rows are not required. */
547 if (num_lines < lines_left_in_iMCU_row) {
548 increment_simple_rowgroup_ctr(cinfo, num_lines);
551 cinfo->output_scanline += lines_left_in_iMCU_row;
552 main_ptr->buffer_full = FALSE;
553 main_ptr->rowgroup_ctr = 0;
554 if (!master->using_merged_upsample) {
555 upsample->next_row_out = cinfo->max_v_samp_factor;
556 upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
561 /* Calculate how many full iMCU rows we can skip. */
562 if (cinfo->upsample->need_context_rows)
563 lines_to_skip = ((lines_after_iMCU_row - 1) / lines_per_iMCU_row) *
566 lines_to_skip = (lines_after_iMCU_row / lines_per_iMCU_row) *
568 /* Calculate the number of lines that remain to be skipped after skipping all
569 * of the full iMCU rows that we can. We will not read these lines unless we
572 lines_to_read = lines_after_iMCU_row - lines_to_skip;
574 /* For images requiring multiple scans (progressive, non-interleaved, etc.),
575 * all of the entropy decoding occurs in jpeg_start_decompress(), assuming
576 * that the input data source is non-suspending. This makes skipping easy.
578 if (cinfo->inputctl->has_multiple_scans || cinfo->buffered_image) {
579 if (cinfo->upsample->need_context_rows) {
580 cinfo->output_scanline += lines_to_skip;
581 cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
582 main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
583 /* It is complex to properly move to the middle of a context block, so
584 * read the remaining lines instead of skipping them.
586 read_and_discard_scanlines(cinfo, lines_to_read);
588 cinfo->output_scanline += lines_to_skip;
589 cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
590 increment_simple_rowgroup_ctr(cinfo, lines_to_read);
592 if (!master->using_merged_upsample)
593 upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
597 /* Skip the iMCU rows that we can safely skip. */
598 for (i = 0; i < lines_to_skip; i += lines_per_iMCU_row) {
599 for (y = 0; y < coef->MCU_rows_per_iMCU_row; y++) {
600 for (x = 0; x < cinfo->MCUs_per_row; x++) {
601 /* Calling decode_mcu() with a NULL pointer causes it to discard the
602 * decoded coefficients. This is ~5% faster for large subsets, but
603 * it's tough to tell a difference for smaller images.
605 if (!cinfo->entropy->insufficient_data)
606 cinfo->master->last_good_iMCU_row = cinfo->input_iMCU_row;
607 (*cinfo->entropy->decode_mcu) (cinfo, NULL);
610 cinfo->input_iMCU_row++;
611 cinfo->output_iMCU_row++;
612 if (cinfo->input_iMCU_row < cinfo->total_iMCU_rows)
613 start_iMCU_row(cinfo);
615 (*cinfo->inputctl->finish_input_pass) (cinfo);
617 cinfo->output_scanline += lines_to_skip;
619 if (cinfo->upsample->need_context_rows) {
620 /* Context-based upsampling keeps track of iMCU rows. */
621 main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
623 /* It is complex to properly move to the middle of a context block, so
624 * read the remaining lines instead of skipping them.
626 read_and_discard_scanlines(cinfo, lines_to_read);
628 increment_simple_rowgroup_ctr(cinfo, lines_to_read);
631 /* Since skipping lines involves skipping the upsampling step, the value of
632 * "rows_to_go" will become invalid unless we set it here. NOTE: This is a
633 * bit odd, since "rows_to_go" seems to be redundantly keeping track of
636 if (!master->using_merged_upsample)
637 upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
639 /* Always skip the requested number of lines. */
644 * Alternate entry point to read raw data.
645 * Processes exactly one iMCU row per call, unless suspended.
649 _jpeg_read_raw_data(j_decompress_ptr cinfo, _JSAMPIMAGE data,
650 JDIMENSION max_lines)
652 JDIMENSION lines_per_iMCU_row;
654 if (cinfo->data_precision != BITS_IN_JSAMPLE)
655 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
657 if (cinfo->master->lossless)
658 ERREXIT(cinfo, JERR_NOTIMPL);
660 if (cinfo->global_state != DSTATE_RAW_OK)
661 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
662 if (cinfo->output_scanline >= cinfo->output_height) {
663 WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
667 /* Call progress monitor hook if present */
668 if (cinfo->progress != NULL) {
669 cinfo->progress->pass_counter = (long)cinfo->output_scanline;
670 cinfo->progress->pass_limit = (long)cinfo->output_height;
671 (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
674 /* Verify that at least one iMCU row can be returned. */
675 lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size;
676 if (max_lines < lines_per_iMCU_row)
677 ERREXIT(cinfo, JERR_BUFFER_SIZE);
679 /* Decompress directly into user's buffer. */
680 if (!(*cinfo->coef->_decompress_data) (cinfo, data))
681 return 0; /* suspension forced, can do nothing more */
683 /* OK, we processed one iMCU row. */
684 cinfo->output_scanline += lines_per_iMCU_row;
685 return lines_per_iMCU_row;
688 #endif /* BITS_IN_JSAMPLE != 16 */
691 #if BITS_IN_JSAMPLE == 8
693 /* Additional entry points for buffered-image mode. */
695 #ifdef D_MULTISCAN_FILES_SUPPORTED
698 * Initialize for an output pass in buffered-image mode.
702 jpeg_start_output(j_decompress_ptr cinfo, int scan_number)
704 if (cinfo->global_state != DSTATE_BUFIMAGE &&
705 cinfo->global_state != DSTATE_PRESCAN)
706 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
707 /* Limit scan number to valid range */
708 if (scan_number <= 0)
710 if (cinfo->inputctl->eoi_reached && scan_number > cinfo->input_scan_number)
711 scan_number = cinfo->input_scan_number;
712 cinfo->output_scan_number = scan_number;
713 /* Perform any dummy output passes, and set up for the real pass */
714 return output_pass_setup(cinfo);
719 * Finish up after an output pass in buffered-image mode.
721 * Returns FALSE if suspended. The return value need be inspected only if
722 * a suspending data source is used.
726 jpeg_finish_output(j_decompress_ptr cinfo)
728 if ((cinfo->global_state == DSTATE_SCANNING ||
729 cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
730 /* Terminate this pass. */
731 /* We do not require the whole pass to have been completed. */
732 (*cinfo->master->finish_output_pass) (cinfo);
733 cinfo->global_state = DSTATE_BUFPOST;
734 } else if (cinfo->global_state != DSTATE_BUFPOST) {
735 /* BUFPOST = repeat call after a suspension, anything else is error */
736 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
738 /* Read markers looking for SOS or EOI */
739 while (cinfo->input_scan_number <= cinfo->output_scan_number &&
740 !cinfo->inputctl->eoi_reached) {
741 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
742 return FALSE; /* Suspend, come back later */
744 cinfo->global_state = DSTATE_BUFIMAGE;
748 #endif /* D_MULTISCAN_FILES_SUPPORTED */
750 #endif /* BITS_IN_JSAMPLE == 8 */