1 /*
2 * jdapistd.c
3 *
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-2018, 2020, D. R. Commander.
8 * Copyright (C) 2015, Google, Inc.
9 * For conditions of distribution and use, see the accompanying README.ijg
10 * file.
11 *
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.
19 */
20
21 #include "jinclude.h"
22 #include "jdmainct.h"
23 #include "jdcoefct.h"
24 #include "jdmaster.h"
25 #include "jdmerge.h"
26 #include "jdsample.h"
27 #include "jmemsys.h"
28
29 /* Forward declarations */
30 LOCAL(boolean) output_pass_setup(j_decompress_ptr cinfo);
31
32
33 /*
34 * Decompression initialization.
35 * jpeg_read_header must be completed before calling this.
36 *
37 * If a multipass operating mode was selected, this will do all but the
38 * last pass, and thus may take a great deal of time.
39 *
40 * Returns FALSE if suspended. The return value need be inspected only if
41 * a suspending data source is used.
42 */
43
44 GLOBAL(boolean)
jpeg_start_decompress(j_decompress_ptr cinfo)45 jpeg_start_decompress(j_decompress_ptr cinfo)
46 {
47 if (cinfo->global_state == DSTATE_READY) {
48 /* First call: initialize master control, select active modules */
49 jinit_master_decompress(cinfo);
50 if (cinfo->buffered_image) {
51 /* No more work here; expecting jpeg_start_output next */
52 cinfo->global_state = DSTATE_BUFIMAGE;
53 return TRUE;
54 }
55 cinfo->global_state = DSTATE_PRELOAD;
56 }
57 if (cinfo->global_state == DSTATE_PRELOAD) {
58 /* If file has multiple scans, absorb them all into the coef buffer */
59 if (cinfo->inputctl->has_multiple_scans) {
60 #ifdef D_MULTISCAN_FILES_SUPPORTED
61 for (;;) {
62 int retcode;
63 /* Call progress monitor hook if present */
64 if (cinfo->progress != NULL)
65 (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
66 /* Absorb some more input */
67 retcode = (*cinfo->inputctl->consume_input) (cinfo);
68 if (retcode == JPEG_SUSPENDED)
69 return FALSE;
70 if (retcode == JPEG_REACHED_EOI)
71 break;
72 /* Advance progress counter if appropriate */
73 if (cinfo->progress != NULL &&
74 (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
75 if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
76 /* jdmaster underestimated number of scans; ratchet up one scan */
77 cinfo->progress->pass_limit += (long)cinfo->total_iMCU_rows;
78 }
79 }
80 }
81 #else
82 ERREXIT(cinfo, JERR_NOT_COMPILED);
83 #endif /* D_MULTISCAN_FILES_SUPPORTED */
84 }
85 cinfo->output_scan_number = cinfo->input_scan_number;
86 } else if (cinfo->global_state != DSTATE_PRESCAN)
87 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
88 /* Perform any dummy output passes, and set up for the final pass */
89 return output_pass_setup(cinfo);
90 }
91
92
93 /*
94 * Set up for an output pass, and perform any dummy pass(es) needed.
95 * Common subroutine for jpeg_start_decompress and jpeg_start_output.
96 * Entry: global_state = DSTATE_PRESCAN only if previously suspended.
97 * Exit: If done, returns TRUE and sets global_state for proper output mode.
98 * If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.
99 */
100
101 LOCAL(boolean)
output_pass_setup(j_decompress_ptr cinfo)102 output_pass_setup(j_decompress_ptr cinfo)
103 {
104 if (cinfo->global_state != DSTATE_PRESCAN) {
105 /* First call: do pass setup */
106 (*cinfo->master->prepare_for_output_pass) (cinfo);
107 cinfo->output_scanline = 0;
108 cinfo->global_state = DSTATE_PRESCAN;
109 }
110 /* Loop over any required dummy passes */
111 while (cinfo->master->is_dummy_pass) {
112 #ifdef QUANT_2PASS_SUPPORTED
113 /* Crank through the dummy pass */
114 while (cinfo->output_scanline < cinfo->output_height) {
115 JDIMENSION last_scanline;
116 /* Call progress monitor hook if present */
117 if (cinfo->progress != NULL) {
118 cinfo->progress->pass_counter = (long)cinfo->output_scanline;
119 cinfo->progress->pass_limit = (long)cinfo->output_height;
120 (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
121 }
122 /* Process some data */
123 last_scanline = cinfo->output_scanline;
124 (*cinfo->main->process_data) (cinfo, (JSAMPARRAY)NULL,
125 &cinfo->output_scanline, (JDIMENSION)0);
126 if (cinfo->output_scanline == last_scanline)
127 return FALSE; /* No progress made, must suspend */
128 }
129 /* Finish up dummy pass, and set up for another one */
130 (*cinfo->master->finish_output_pass) (cinfo);
131 (*cinfo->master->prepare_for_output_pass) (cinfo);
132 cinfo->output_scanline = 0;
133 #else
134 ERREXIT(cinfo, JERR_NOT_COMPILED);
135 #endif /* QUANT_2PASS_SUPPORTED */
136 }
137 /* Ready for application to drive output pass through
138 * jpeg_read_scanlines or jpeg_read_raw_data.
139 */
140 cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
141 return TRUE;
142 }
143
144
145 /*
146 * Enable partial scanline decompression
147 *
148 * Must be called after jpeg_start_decompress() and before any calls to
149 * jpeg_read_scanlines() or jpeg_skip_scanlines().
150 *
151 * Refer to libjpeg.txt for more information.
152 */
153
154 GLOBAL(void)
jpeg_crop_scanline(j_decompress_ptr cinfo,JDIMENSION * xoffset,JDIMENSION * width)155 jpeg_crop_scanline(j_decompress_ptr cinfo, JDIMENSION *xoffset,
156 JDIMENSION *width)
157 {
158 int ci, align, orig_downsampled_width;
159 JDIMENSION input_xoffset;
160 boolean reinit_upsampler = FALSE;
161 jpeg_component_info *compptr;
162
163 if (cinfo->global_state != DSTATE_SCANNING || cinfo->output_scanline != 0)
164 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
165
166 if (!xoffset || !width)
167 ERREXIT(cinfo, JERR_BAD_CROP_SPEC);
168
169 /* xoffset and width must fall within the output image dimensions. */
170 if (*width == 0 || *xoffset + *width > cinfo->output_width)
171 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
172
173 /* No need to do anything if the caller wants the entire width. */
174 if (*width == cinfo->output_width)
175 return;
176
177 /* Ensuring the proper alignment of xoffset is tricky. At minimum, it
178 * must align with an MCU boundary, because:
179 *
180 * (1) The IDCT is performed in blocks, and it is not feasible to modify
181 * the algorithm so that it can transform partial blocks.
182 * (2) Because of the SIMD extensions, any input buffer passed to the
183 * upsampling and color conversion routines must be aligned to the
184 * SIMD word size (for instance, 128-bit in the case of SSE2.) The
185 * easiest way to accomplish this without copying data is to ensure
186 * that upsampling and color conversion begin at the start of the
187 * first MCU column that will be inverse transformed.
188 *
189 * In practice, we actually impose a stricter alignment requirement. We
190 * require that xoffset be a multiple of the maximum MCU column width of all
191 * of the components (the "iMCU column width.") This is to simplify the
192 * single-pass decompression case, allowing us to use the same MCU column
193 * width for all of the components.
194 */
195 if (cinfo->comps_in_scan == 1 && cinfo->num_components == 1)
196 align = cinfo->_min_DCT_scaled_size;
197 else
198 align = cinfo->_min_DCT_scaled_size * cinfo->max_h_samp_factor;
199
200 /* Adjust xoffset to the nearest iMCU boundary <= the requested value */
201 input_xoffset = *xoffset;
202 *xoffset = (input_xoffset / align) * align;
203
204 /* Adjust the width so that the right edge of the output image is as
205 * requested (only the left edge is altered.) It is important that calling
206 * programs check this value after this function returns, so that they can
207 * allocate an output buffer with the appropriate size.
208 */
209 *width = *width + input_xoffset - *xoffset;
210 cinfo->output_width = *width;
211
212 /* Set the first and last iMCU columns that we must decompress. These values
213 * will be used in single-scan decompressions.
214 */
215 cinfo->master->first_iMCU_col = (JDIMENSION)(long)(*xoffset) / (long)align;
216 cinfo->master->last_iMCU_col =
217 (JDIMENSION)jdiv_round_up((long)(*xoffset + cinfo->output_width),
218 (long)align) - 1;
219
220 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
221 ci++, compptr++) {
222 int hsf = (cinfo->comps_in_scan == 1 && cinfo->num_components == 1) ?
223 1 : compptr->h_samp_factor;
224
225 /* Set downsampled_width to the new output width. */
226 orig_downsampled_width = compptr->downsampled_width;
227 compptr->downsampled_width =
228 (JDIMENSION)jdiv_round_up((long)(cinfo->output_width *
229 compptr->h_samp_factor),
230 (long)cinfo->max_h_samp_factor);
231 if (compptr->downsampled_width < 2 && orig_downsampled_width >= 2)
232 reinit_upsampler = TRUE;
233
234 /* Set the first and last iMCU columns that we must decompress. These
235 * values will be used in multi-scan decompressions.
236 */
237 cinfo->master->first_MCU_col[ci] =
238 (JDIMENSION)(long)(*xoffset * hsf) / (long)align;
239 cinfo->master->last_MCU_col[ci] =
240 (JDIMENSION)jdiv_round_up((long)((*xoffset + cinfo->output_width) * hsf),
241 (long)align) - 1;
242 }
243
244 if (reinit_upsampler) {
245 cinfo->master->jinit_upsampler_no_alloc = TRUE;
246 jinit_upsampler(cinfo);
247 cinfo->master->jinit_upsampler_no_alloc = FALSE;
248 }
249 }
250
251
252 /*
253 * Read some scanlines of data from the JPEG decompressor.
254 *
255 * The return value will be the number of lines actually read.
256 * This may be less than the number requested in several cases,
257 * including bottom of image, data source suspension, and operating
258 * modes that emit multiple scanlines at a time.
259 *
260 * Note: we warn about excess calls to jpeg_read_scanlines() since
261 * this likely signals an application programmer error. However,
262 * an oversize buffer (max_lines > scanlines remaining) is not an error.
263 */
264
265 GLOBAL(JDIMENSION)
jpeg_read_scanlines(j_decompress_ptr cinfo,JSAMPARRAY scanlines,JDIMENSION max_lines)266 jpeg_read_scanlines(j_decompress_ptr cinfo, JSAMPARRAY scanlines,
267 JDIMENSION max_lines)
268 {
269 JDIMENSION row_ctr;
270
271 if (cinfo->global_state != DSTATE_SCANNING)
272 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
273 if (cinfo->output_scanline >= cinfo->output_height) {
274 WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
275 return 0;
276 }
277
278 /* Call progress monitor hook if present */
279 if (cinfo->progress != NULL) {
280 cinfo->progress->pass_counter = (long)cinfo->output_scanline;
281 cinfo->progress->pass_limit = (long)cinfo->output_height;
282 (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
283 }
284
285 /* Process some data */
286 row_ctr = 0;
287 (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
288 cinfo->output_scanline += row_ctr;
289 return row_ctr;
290 }
291
292
293 /* Dummy color convert function used by jpeg_skip_scanlines() */
294 LOCAL(void)
noop_convert(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION input_row,JSAMPARRAY output_buf,int num_rows)295 noop_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
296 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
297 {
298 }
299
300
301 /* Dummy quantize function used by jpeg_skip_scanlines() */
302 LOCAL(void)
noop_quantize(j_decompress_ptr cinfo,JSAMPARRAY input_buf,JSAMPARRAY output_buf,int num_rows)303 noop_quantize(j_decompress_ptr cinfo, JSAMPARRAY input_buf,
304 JSAMPARRAY output_buf, int num_rows)
305 {
306 }
307
308
309 /* Dummy postprocessing function used by jpeg_skip_scanlines() */
310 LOCAL(void)
noop_post_process(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION * in_row_group_ctr,JDIMENSION in_row_groups_avail,JSAMPARRAY output_buf,JDIMENSION * out_row_ctr,JDIMENSION out_rows_avail)311 noop_post_process (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
312 JDIMENSION *in_row_group_ctr,
313 JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
314 JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
315 {
316 }
317
318
319 /*
320 * In some cases, it is best to call jpeg_read_scanlines() and discard the
321 * output, rather than skipping the scanlines, because this allows us to
322 * maintain the internal state of the context-based upsampler. In these cases,
323 * we set up and tear down a dummy color converter in order to avoid valgrind
324 * errors and to achieve the best possible performance.
325 */
326
327 LOCAL(void)
read_and_discard_scanlines(j_decompress_ptr cinfo,JDIMENSION num_lines)328 read_and_discard_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)
329 {
330 JDIMENSION n;
331 my_master_ptr master = (my_master_ptr)cinfo->master;
332 void (*color_convert) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
333 JDIMENSION input_row, JSAMPARRAY output_buf,
334 int num_rows) = NULL;
335 void (*color_quantize) (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
336 JSAMPARRAY output_buf, int num_rows) = NULL;
337 void (*post_process_data) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
338 JDIMENSION *in_row_group_ctr,
339 JDIMENSION in_row_groups_avail,
340 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
341 JDIMENSION out_rows_avail) = NULL;
342
343 if (cinfo->cconvert && cinfo->cconvert->color_convert) {
344 color_convert = cinfo->cconvert->color_convert;
345 cinfo->cconvert->color_convert = noop_convert;
346 }
347
348 if (cinfo->cquantize && cinfo->cquantize->color_quantize) {
349 color_quantize = cinfo->cquantize->color_quantize;
350 cinfo->cquantize->color_quantize = noop_quantize;
351 }
352
353 if (master->using_merged_upsample && cinfo->post &&
354 cinfo->post->post_process_data) {
355 post_process_data = cinfo->post->post_process_data;
356 cinfo->post->post_process_data = noop_post_process;
357 }
358
359 for (n = 0; n < num_lines; n++)
360 jpeg_read_scanlines(cinfo, NULL, 1);
361
362 if (color_convert)
363 cinfo->cconvert->color_convert = color_convert;
364
365 if (color_quantize)
366 cinfo->cquantize->color_quantize = color_quantize;
367
368 if (post_process_data)
369 cinfo->post->post_process_data = post_process_data;
370 }
371
372
373 /*
374 * Called by jpeg_skip_scanlines(). This partially skips a decompress block by
375 * incrementing the rowgroup counter.
376 */
377
378 LOCAL(void)
increment_simple_rowgroup_ctr(j_decompress_ptr cinfo,JDIMENSION rows)379 increment_simple_rowgroup_ctr(j_decompress_ptr cinfo, JDIMENSION rows)
380 {
381 JDIMENSION rows_left;
382 my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
383
384 /* Increment the counter to the next row group after the skipped rows. */
385 main_ptr->rowgroup_ctr += rows / cinfo->max_v_samp_factor;
386
387 /* Partially skipping a row group would involve modifying the internal state
388 * of the upsampler, so read the remaining rows into a dummy buffer instead.
389 */
390 rows_left = rows % cinfo->max_v_samp_factor;
391 cinfo->output_scanline += rows - rows_left;
392
393 read_and_discard_scanlines(cinfo, rows_left);
394 }
395
396 /*
397 * Skips some scanlines of data from the JPEG decompressor.
398 *
399 * The return value will be the number of lines actually skipped. If skipping
400 * num_lines would move beyond the end of the image, then the actual number of
401 * lines remaining in the image is returned. Otherwise, the return value will
402 * be equal to num_lines.
403 *
404 * Refer to libjpeg.txt for more information.
405 */
406
407 GLOBAL(JDIMENSION)
jpeg_skip_scanlines(j_decompress_ptr cinfo,JDIMENSION num_lines)408 jpeg_skip_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)
409 {
410 my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
411 my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
412 my_master_ptr master = (my_master_ptr)cinfo->master;
413 JDIMENSION i, x;
414 int y;
415 JDIMENSION lines_per_iMCU_row, lines_left_in_iMCU_row, lines_after_iMCU_row;
416 JDIMENSION lines_to_skip, lines_to_read;
417
418 if (cinfo->global_state != DSTATE_SCANNING)
419 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
420
421 /* Do not skip past the bottom of the image. */
422 if (cinfo->output_scanline + num_lines >= cinfo->output_height) {
423 cinfo->output_scanline = cinfo->output_height;
424 (*cinfo->inputctl->finish_input_pass) (cinfo);
425 cinfo->inputctl->eoi_reached = TRUE;
426 return cinfo->output_height - cinfo->output_scanline;
427 }
428
429 if (num_lines == 0)
430 return 0;
431
432 lines_per_iMCU_row = cinfo->_min_DCT_scaled_size * cinfo->max_v_samp_factor;
433 lines_left_in_iMCU_row =
434 (lines_per_iMCU_row - (cinfo->output_scanline % lines_per_iMCU_row)) %
435 lines_per_iMCU_row;
436 lines_after_iMCU_row = num_lines - lines_left_in_iMCU_row;
437
438 /* Skip the lines remaining in the current iMCU row. When upsampling
439 * requires context rows, we need the previous and next rows in order to read
440 * the current row. This adds some complexity.
441 */
442 if (cinfo->upsample->need_context_rows) {
443 /* If the skipped lines would not move us past the current iMCU row, we
444 * read the lines and ignore them. There might be a faster way of doing
445 * this, but we are facing increasing complexity for diminishing returns.
446 * The increasing complexity would be a by-product of meddling with the
447 * state machine used to skip context rows. Near the end of an iMCU row,
448 * the next iMCU row may have already been entropy-decoded. In this unique
449 * case, we will read the next iMCU row if we cannot skip past it as well.
450 */
451 if ((num_lines < lines_left_in_iMCU_row + 1) ||
452 (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full &&
453 lines_after_iMCU_row < lines_per_iMCU_row + 1)) {
454 read_and_discard_scanlines(cinfo, num_lines);
455 return num_lines;
456 }
457
458 /* If the next iMCU row has already been entropy-decoded, make sure that
459 * we do not skip too far.
460 */
461 if (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full) {
462 cinfo->output_scanline += lines_left_in_iMCU_row + lines_per_iMCU_row;
463 lines_after_iMCU_row -= lines_per_iMCU_row;
464 } else {
465 cinfo->output_scanline += lines_left_in_iMCU_row;
466 }
467
468 /* If we have just completed the first block, adjust the buffer pointers */
469 if (main_ptr->iMCU_row_ctr == 0 ||
470 (main_ptr->iMCU_row_ctr == 1 && lines_left_in_iMCU_row > 2))
471 set_wraparound_pointers(cinfo);
472 main_ptr->buffer_full = FALSE;
473 main_ptr->rowgroup_ctr = 0;
474 main_ptr->context_state = CTX_PREPARE_FOR_IMCU;
475 if (master->using_merged_upsample) {
476 my_merged_upsample_ptr upsample =
477 (my_merged_upsample_ptr)cinfo->upsample;
478 upsample->spare_full = FALSE;
479 upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
480 } else {
481 my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
482 upsample->next_row_out = cinfo->max_v_samp_factor;
483 upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
484 }
485 }
486
487 /* Skipping is much simpler when context rows are not required. */
488 else {
489 if (num_lines < lines_left_in_iMCU_row) {
490 increment_simple_rowgroup_ctr(cinfo, num_lines);
491 return num_lines;
492 } else {
493 cinfo->output_scanline += lines_left_in_iMCU_row;
494 main_ptr->buffer_full = FALSE;
495 main_ptr->rowgroup_ctr = 0;
496 if (master->using_merged_upsample) {
497 my_merged_upsample_ptr upsample =
498 (my_merged_upsample_ptr)cinfo->upsample;
499 upsample->spare_full = FALSE;
500 upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
501 } else {
502 my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
503 upsample->next_row_out = cinfo->max_v_samp_factor;
504 upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
505 }
506 }
507 }
508
509 /* Calculate how many full iMCU rows we can skip. */
510 if (cinfo->upsample->need_context_rows)
511 lines_to_skip = ((lines_after_iMCU_row - 1) / lines_per_iMCU_row) *
512 lines_per_iMCU_row;
513 else
514 lines_to_skip = (lines_after_iMCU_row / lines_per_iMCU_row) *
515 lines_per_iMCU_row;
516 /* Calculate the number of lines that remain to be skipped after skipping all
517 * of the full iMCU rows that we can. We will not read these lines unless we
518 * have to.
519 */
520 lines_to_read = lines_after_iMCU_row - lines_to_skip;
521
522 /* For images requiring multiple scans (progressive, non-interleaved, etc.),
523 * all of the entropy decoding occurs in jpeg_start_decompress(), assuming
524 * that the input data source is non-suspending. This makes skipping easy.
525 */
526 if (cinfo->inputctl->has_multiple_scans) {
527 if (cinfo->upsample->need_context_rows) {
528 cinfo->output_scanline += lines_to_skip;
529 cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
530 main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
531 /* It is complex to properly move to the middle of a context block, so
532 * read the remaining lines instead of skipping them.
533 */
534 read_and_discard_scanlines(cinfo, lines_to_read);
535 } else {
536 cinfo->output_scanline += lines_to_skip;
537 cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
538 increment_simple_rowgroup_ctr(cinfo, lines_to_read);
539 }
540 if (master->using_merged_upsample) {
541 my_merged_upsample_ptr upsample =
542 (my_merged_upsample_ptr)cinfo->upsample;
543 upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
544 } else {
545 my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
546 upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
547 }
548 return num_lines;
549 }
550
551 /* Skip the iMCU rows that we can safely skip. */
552 for (i = 0; i < lines_to_skip; i += lines_per_iMCU_row) {
553 for (y = 0; y < coef->MCU_rows_per_iMCU_row; y++) {
554 for (x = 0; x < cinfo->MCUs_per_row; x++) {
555 /* Calling decode_mcu() with a NULL pointer causes it to discard the
556 * decoded coefficients. This is ~5% faster for large subsets, but
557 * it's tough to tell a difference for smaller images.
558 */
559 (*cinfo->entropy->decode_mcu) (cinfo, NULL);
560 }
561 }
562 cinfo->input_iMCU_row++;
563 cinfo->output_iMCU_row++;
564 if (cinfo->input_iMCU_row < cinfo->total_iMCU_rows)
565 start_iMCU_row(cinfo);
566 else
567 (*cinfo->inputctl->finish_input_pass) (cinfo);
568 }
569 cinfo->output_scanline += lines_to_skip;
570
571 if (cinfo->upsample->need_context_rows) {
572 /* Context-based upsampling keeps track of iMCU rows. */
573 main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
574
575 /* It is complex to properly move to the middle of a context block, so
576 * read the remaining lines instead of skipping them.
577 */
578 read_and_discard_scanlines(cinfo, lines_to_read);
579 } else {
580 increment_simple_rowgroup_ctr(cinfo, lines_to_read);
581 }
582
583 /* Since skipping lines involves skipping the upsampling step, the value of
584 * "rows_to_go" will become invalid unless we set it here. NOTE: This is a
585 * bit odd, since "rows_to_go" seems to be redundantly keeping track of
586 * output_scanline.
587 */
588 if (master->using_merged_upsample) {
589 my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
590 upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
591 } else {
592 my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
593 upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
594 }
595
596 /* Always skip the requested number of lines. */
597 return num_lines;
598 }
599
600 /*
601 * Alternate entry point to read raw data.
602 * Processes exactly one iMCU row per call, unless suspended.
603 */
604
605 GLOBAL(JDIMENSION)
jpeg_read_raw_data(j_decompress_ptr cinfo,JSAMPIMAGE data,JDIMENSION max_lines)606 jpeg_read_raw_data(j_decompress_ptr cinfo, JSAMPIMAGE data,
607 JDIMENSION max_lines)
608 {
609 JDIMENSION lines_per_iMCU_row;
610
611 if (cinfo->global_state != DSTATE_RAW_OK)
612 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
613 if (cinfo->output_scanline >= cinfo->output_height) {
614 WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
615 return 0;
616 }
617
618 /* Call progress monitor hook if present */
619 if (cinfo->progress != NULL) {
620 cinfo->progress->pass_counter = (long)cinfo->output_scanline;
621 cinfo->progress->pass_limit = (long)cinfo->output_height;
622 (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
623 }
624
625 /* Verify that at least one iMCU row can be returned. */
626 lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size;
627 if (max_lines < lines_per_iMCU_row)
628 ERREXIT(cinfo, JERR_BUFFER_SIZE);
629
630 /* Decompress directly into user's buffer. */
631 if (!(*cinfo->coef->decompress_data) (cinfo, data))
632 return 0; /* suspension forced, can do nothing more */
633
634 /* OK, we processed one iMCU row. */
635 cinfo->output_scanline += lines_per_iMCU_row;
636 return lines_per_iMCU_row;
637 }
638
639
640 /* Additional entry points for buffered-image mode. */
641
642 #ifdef D_MULTISCAN_FILES_SUPPORTED
643
644 /*
645 * Initialize for an output pass in buffered-image mode.
646 */
647
648 GLOBAL(boolean)
jpeg_start_output(j_decompress_ptr cinfo,int scan_number)649 jpeg_start_output(j_decompress_ptr cinfo, int scan_number)
650 {
651 if (cinfo->global_state != DSTATE_BUFIMAGE &&
652 cinfo->global_state != DSTATE_PRESCAN)
653 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
654 /* Limit scan number to valid range */
655 if (scan_number <= 0)
656 scan_number = 1;
657 if (cinfo->inputctl->eoi_reached && scan_number > cinfo->input_scan_number)
658 scan_number = cinfo->input_scan_number;
659 cinfo->output_scan_number = scan_number;
660 /* Perform any dummy output passes, and set up for the real pass */
661 return output_pass_setup(cinfo);
662 }
663
664
665 /*
666 * Finish up after an output pass in buffered-image mode.
667 *
668 * Returns FALSE if suspended. The return value need be inspected only if
669 * a suspending data source is used.
670 */
671
672 GLOBAL(boolean)
jpeg_finish_output(j_decompress_ptr cinfo)673 jpeg_finish_output(j_decompress_ptr cinfo)
674 {
675 if ((cinfo->global_state == DSTATE_SCANNING ||
676 cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
677 /* Terminate this pass. */
678 /* We do not require the whole pass to have been completed. */
679 (*cinfo->master->finish_output_pass) (cinfo);
680 cinfo->global_state = DSTATE_BUFPOST;
681 } else if (cinfo->global_state != DSTATE_BUFPOST) {
682 /* BUFPOST = repeat call after a suspension, anything else is error */
683 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
684 }
685 /* Read markers looking for SOS or EOI */
686 while (cinfo->input_scan_number <= cinfo->output_scan_number &&
687 !cinfo->inputctl->eoi_reached) {
688 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
689 return FALSE; /* Suspend, come back later */
690 }
691 cinfo->global_state = DSTATE_BUFIMAGE;
692 return TRUE;
693 }
694
695 #endif /* D_MULTISCAN_FILES_SUPPORTED */
696