blob: 76cb2def913eb9f919b7bd02422dc4fb39f9e1e7 [file] [log] [blame]
Thomas G. Lanebc79e061995-08-02 00:00:00 +00001/*
2 * jdapistd.c
3 *
DRCa73e8702012-12-31 02:52:30 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane489583f1996-02-07 00:00:00 +00005 * Copyright (C) 1994-1996, Thomas G. Lane.
DRCa6ef2822013-09-28 03:23:49 +00006 * libjpeg-turbo Modifications:
DRCac30a1b2015-06-25 03:44:36 +00007 * Copyright (C) 2010, 2015, D. R. Commander.
8 * Copyright (C) 2015, Google, Inc.
Thomas G. Lanebc79e061995-08-02 00:00:00 +00009 * For conditions of distribution and use, see the accompanying README file.
10 *
11 * This file contains application interface code for the decompression half
12 * of the JPEG library. These are the "standard" API routines that are
13 * used in the normal full-decompression case. They are not used by a
14 * transcoding-only application. Note that if an application links in
15 * jpeg_start_decompress, it will end up linking in the entire decompressor.
16 * We thus must separate this file from jdapimin.c to avoid linking the
17 * whole decompression library into a transcoder.
18 */
19
DRCac30a1b2015-06-25 03:44:36 +000020#include "jdmainct.h"
21#include "jdcoefct.h"
22#include "jdsample.h"
23#include "jmemsys.h"
Thomas G. Lanebc79e061995-08-02 00:00:00 +000024
25/* Forward declarations */
DRCbc56b752014-05-16 10:43:44 +000026LOCAL(boolean) output_pass_setup (j_decompress_ptr cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +000027
28
29/*
30 * Decompression initialization.
31 * jpeg_read_header must be completed before calling this.
32 *
33 * If a multipass operating mode was selected, this will do all but the
34 * last pass, and thus may take a great deal of time.
35 *
36 * Returns FALSE if suspended. The return value need be inspected only if
37 * a suspending data source is used.
38 */
39
Thomas G. Lane489583f1996-02-07 00:00:00 +000040GLOBAL(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000041jpeg_start_decompress (j_decompress_ptr cinfo)
42{
43 if (cinfo->global_state == DSTATE_READY) {
44 /* First call: initialize master control, select active modules */
45 jinit_master_decompress(cinfo);
46 if (cinfo->buffered_image) {
47 /* No more work here; expecting jpeg_start_output next */
48 cinfo->global_state = DSTATE_BUFIMAGE;
49 return TRUE;
50 }
51 cinfo->global_state = DSTATE_PRELOAD;
52 }
53 if (cinfo->global_state == DSTATE_PRELOAD) {
54 /* If file has multiple scans, absorb them all into the coef buffer */
55 if (cinfo->inputctl->has_multiple_scans) {
56#ifdef D_MULTISCAN_FILES_SUPPORTED
57 for (;;) {
DRCe5eaf372014-05-09 18:00:32 +000058 int retcode;
59 /* Call progress monitor hook if present */
60 if (cinfo->progress != NULL)
61 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
62 /* Absorb some more input */
63 retcode = (*cinfo->inputctl->consume_input) (cinfo);
64 if (retcode == JPEG_SUSPENDED)
65 return FALSE;
66 if (retcode == JPEG_REACHED_EOI)
67 break;
68 /* Advance progress counter if appropriate */
69 if (cinfo->progress != NULL &&
70 (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
71 if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
72 /* jdmaster underestimated number of scans; ratchet up one scan */
73 cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
74 }
75 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +000076 }
77#else
78 ERREXIT(cinfo, JERR_NOT_COMPILED);
79#endif /* D_MULTISCAN_FILES_SUPPORTED */
80 }
81 cinfo->output_scan_number = cinfo->input_scan_number;
82 } else if (cinfo->global_state != DSTATE_PRESCAN)
83 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
84 /* Perform any dummy output passes, and set up for the final pass */
85 return output_pass_setup(cinfo);
86}
87
88
89/*
90 * Set up for an output pass, and perform any dummy pass(es) needed.
91 * Common subroutine for jpeg_start_decompress and jpeg_start_output.
92 * Entry: global_state = DSTATE_PRESCAN only if previously suspended.
93 * Exit: If done, returns TRUE and sets global_state for proper output mode.
94 * If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.
95 */
96
Thomas G. Lane489583f1996-02-07 00:00:00 +000097LOCAL(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000098output_pass_setup (j_decompress_ptr cinfo)
99{
100 if (cinfo->global_state != DSTATE_PRESCAN) {
101 /* First call: do pass setup */
102 (*cinfo->master->prepare_for_output_pass) (cinfo);
103 cinfo->output_scanline = 0;
104 cinfo->global_state = DSTATE_PRESCAN;
105 }
106 /* Loop over any required dummy passes */
107 while (cinfo->master->is_dummy_pass) {
108#ifdef QUANT_2PASS_SUPPORTED
109 /* Crank through the dummy pass */
110 while (cinfo->output_scanline < cinfo->output_height) {
111 JDIMENSION last_scanline;
112 /* Call progress monitor hook if present */
113 if (cinfo->progress != NULL) {
DRCe5eaf372014-05-09 18:00:32 +0000114 cinfo->progress->pass_counter = (long) cinfo->output_scanline;
115 cinfo->progress->pass_limit = (long) cinfo->output_height;
116 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000117 }
118 /* Process some data */
119 last_scanline = cinfo->output_scanline;
120 (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,
DRCe5eaf372014-05-09 18:00:32 +0000121 &cinfo->output_scanline, (JDIMENSION) 0);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000122 if (cinfo->output_scanline == last_scanline)
DRCe5eaf372014-05-09 18:00:32 +0000123 return FALSE; /* No progress made, must suspend */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000124 }
125 /* Finish up dummy pass, and set up for another one */
126 (*cinfo->master->finish_output_pass) (cinfo);
127 (*cinfo->master->prepare_for_output_pass) (cinfo);
128 cinfo->output_scanline = 0;
129#else
130 ERREXIT(cinfo, JERR_NOT_COMPILED);
131#endif /* QUANT_2PASS_SUPPORTED */
132 }
133 /* Ready for application to drive output pass through
134 * jpeg_read_scanlines or jpeg_read_raw_data.
135 */
136 cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
137 return TRUE;
138}
139
140
141/*
DRC0ef076f2016-02-19 18:32:10 -0600142 * Enable partial scanline decompression
143 *
144 * Must be called after jpeg_start_decompress() and before any calls to
145 * jpeg_read_scanlines() or jpeg_skip_scanlines().
146 *
147 * Refer to libjpeg.txt for more information.
148 */
149
150GLOBAL(void)
151jpeg_crop_scanline (j_decompress_ptr cinfo, JDIMENSION *xoffset,
152 JDIMENSION *width)
153{
154 int ci, align, orig_downsampled_width;
155 JDIMENSION input_xoffset;
156 boolean reinit_upsampler = FALSE;
157 jpeg_component_info *compptr;
158
159 if (cinfo->global_state != DSTATE_SCANNING || cinfo->output_scanline != 0)
160 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
161
162 if (!xoffset || !width)
163 ERREXIT(cinfo, JERR_BAD_CROP_SPEC);
164
165 /* xoffset and width must fall within the output image dimensions. */
166 if (*width == 0 || *xoffset + *width > cinfo->output_width)
167 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
168
169 /* No need to do anything if the caller wants the entire width. */
170 if (*width == cinfo->output_width)
171 return;
172
173 /* Ensuring the proper alignment of xoffset is tricky. At minimum, it
174 * must align with an MCU boundary, because:
175 *
176 * (1) The IDCT is performed in blocks, and it is not feasible to modify
177 * the algorithm so that it can transform partial blocks.
178 * (2) Because of the SIMD extensions, any input buffer passed to the
179 * upsampling and color conversion routines must be aligned to the
180 * SIMD word size (for instance, 128-bit in the case of SSE2.) The
181 * easiest way to accomplish this without copying data is to ensure
182 * that upsampling and color conversion begin at the start of the
183 * first MCU column that will be inverse transformed.
184 *
185 * In practice, we actually impose a stricter alignment requirement. We
186 * require that xoffset be a multiple of the maximum MCU column width of all
187 * of the components (the "iMCU column width.") This is to simplify the
188 * single-pass decompression case, allowing us to use the same MCU column
189 * width for all of the components.
190 */
191 align = cinfo->min_DCT_scaled_size * cinfo->max_h_samp_factor;
192
193 /* Adjust xoffset to the nearest iMCU boundary <= the requested value */
194 input_xoffset = *xoffset;
195 *xoffset = (input_xoffset / align) * align;
196
197 /* Adjust the width so that the right edge of the output image is as
198 * requested (only the left edge is altered.) It is important that calling
199 * programs check this value after this function returns, so that they can
200 * allocate an output buffer with the appropriate size.
201 */
202 *width = *width + input_xoffset - *xoffset;
203 cinfo->output_width = *width;
204
205 /* Set the first and last iMCU columns that we must decompress. These values
206 * will be used in single-scan decompressions.
207 */
208 cinfo->master->first_iMCU_col =
209 (JDIMENSION) (long) (*xoffset) / (long) align;
210 cinfo->master->last_iMCU_col =
211 (JDIMENSION) jdiv_round_up((long) (*xoffset + cinfo->output_width),
212 (long) align) - 1;
213
214 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
215 ci++, compptr++) {
216 /* Set downsampled_width to the new output width. */
217 orig_downsampled_width = compptr->downsampled_width;
218 compptr->downsampled_width =
219 (JDIMENSION) jdiv_round_up((long) (cinfo->output_width *
220 compptr->h_samp_factor),
221 (long) cinfo->max_h_samp_factor);
222 if (compptr->downsampled_width < 2 && orig_downsampled_width >= 2)
223 reinit_upsampler = TRUE;
224
225 /* Set the first and last iMCU columns that we must decompress. These
226 * values will be used in multi-scan decompressions.
227 */
228 cinfo->master->first_MCU_col[ci] =
229 (JDIMENSION) (long) (*xoffset * compptr->h_samp_factor) /
230 (long) align;
231 cinfo->master->last_MCU_col[ci] =
232 (JDIMENSION) jdiv_round_up((long) ((*xoffset + cinfo->output_width) *
233 compptr->h_samp_factor),
234 (long) align) - 1;
235 }
236
237 if (reinit_upsampler) {
238 cinfo->master->jinit_upsampler_no_alloc = TRUE;
239 jinit_upsampler(cinfo);
240 cinfo->master->jinit_upsampler_no_alloc = FALSE;
241 }
242}
243
244
245/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000246 * Read some scanlines of data from the JPEG decompressor.
247 *
248 * The return value will be the number of lines actually read.
249 * This may be less than the number requested in several cases,
250 * including bottom of image, data source suspension, and operating
251 * modes that emit multiple scanlines at a time.
252 *
253 * Note: we warn about excess calls to jpeg_read_scanlines() since
254 * this likely signals an application programmer error. However,
255 * an oversize buffer (max_lines > scanlines remaining) is not an error.
256 */
257
Thomas G. Lane489583f1996-02-07 00:00:00 +0000258GLOBAL(JDIMENSION)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000259jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines,
DRCe5eaf372014-05-09 18:00:32 +0000260 JDIMENSION max_lines)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000261{
262 JDIMENSION row_ctr;
263
264 if (cinfo->global_state != DSTATE_SCANNING)
265 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
266 if (cinfo->output_scanline >= cinfo->output_height) {
267 WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
268 return 0;
269 }
270
271 /* Call progress monitor hook if present */
272 if (cinfo->progress != NULL) {
273 cinfo->progress->pass_counter = (long) cinfo->output_scanline;
274 cinfo->progress->pass_limit = (long) cinfo->output_height;
275 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
276 }
277
278 /* Process some data */
279 row_ctr = 0;
280 (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
281 cinfo->output_scanline += row_ctr;
282 return row_ctr;
283}
284
285
DRC90c92ed2015-07-21 17:36:18 -0500286/* Dummy color convert function used by jpeg_skip_scanlines() */
287LOCAL(void)
288noop_convert (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
289 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
290{
291}
292
293
294/*
295 * In some cases, it is best to call jpeg_read_scanlines() and discard the
296 * output, rather than skipping the scanlines, because this allows us to
297 * maintain the internal state of the context-based upsampler. In these cases,
298 * we set up and tear down a dummy color converter in order to avoid valgrind
299 * errors and to achieve the best possible performance.
300 */
DRCac30a1b2015-06-25 03:44:36 +0000301
302LOCAL(void)
DRC90c92ed2015-07-21 17:36:18 -0500303read_and_discard_scanlines (j_decompress_ptr cinfo, JDIMENSION num_lines)
DRCac30a1b2015-06-25 03:44:36 +0000304{
DRC90c92ed2015-07-21 17:36:18 -0500305 JDIMENSION n;
306 void (*color_convert) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
307 JDIMENSION input_row, JSAMPARRAY output_buf,
308 int num_rows);
DRCac30a1b2015-06-25 03:44:36 +0000309
DRC90c92ed2015-07-21 17:36:18 -0500310 color_convert = cinfo->cconvert->color_convert;
311 cinfo->cconvert->color_convert = noop_convert;
DRCac30a1b2015-06-25 03:44:36 +0000312
DRC90c92ed2015-07-21 17:36:18 -0500313 for (n = 0; n < num_lines; n++)
314 jpeg_read_scanlines(cinfo, NULL, 1);
315
316 cinfo->cconvert->color_convert = color_convert;
DRCac30a1b2015-06-25 03:44:36 +0000317}
318
319
320/*
321 * Called by jpeg_skip_scanlines(). This partially skips a decompress block by
322 * incrementing the rowgroup counter.
323 */
324
325LOCAL(void)
326increment_simple_rowgroup_ctr (j_decompress_ptr cinfo, JDIMENSION rows)
327{
DRC90c92ed2015-07-21 17:36:18 -0500328 JDIMENSION rows_left;
DRCac30a1b2015-06-25 03:44:36 +0000329 my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
330
331 /* Increment the counter to the next row group after the skipped rows. */
332 main_ptr->rowgroup_ctr += rows / cinfo->max_v_samp_factor;
333
334 /* Partially skipping a row group would involve modifying the internal state
335 * of the upsampler, so read the remaining rows into a dummy buffer instead.
336 */
337 rows_left = rows % cinfo->max_v_samp_factor;
338 cinfo->output_scanline += rows - rows_left;
339
DRC90c92ed2015-07-21 17:36:18 -0500340 read_and_discard_scanlines(cinfo, rows_left);
DRCac30a1b2015-06-25 03:44:36 +0000341}
342
DRCac30a1b2015-06-25 03:44:36 +0000343/*
344 * Skips some scanlines of data from the JPEG decompressor.
345 *
346 * The return value will be the number of lines actually skipped. If skipping
347 * num_lines would move beyond the end of the image, then the actual number of
348 * lines remaining in the image is returned. Otherwise, the return value will
349 * be equal to num_lines.
350 *
351 * Refer to libjpeg.txt for more information.
352 */
353
354GLOBAL(JDIMENSION)
355jpeg_skip_scanlines (j_decompress_ptr cinfo, JDIMENSION num_lines)
356{
357 my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
358 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
359 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
DRC3fb56e92015-06-27 08:10:32 +0000360 JDIMENSION i, x;
361 int y;
DRCac30a1b2015-06-25 03:44:36 +0000362 JDIMENSION lines_per_iMCU_row, lines_left_in_iMCU_row, lines_after_iMCU_row;
363 JDIMENSION lines_to_skip, lines_to_read;
364
365 if (cinfo->global_state != DSTATE_SCANNING)
366 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
367
368 /* Do not skip past the bottom of the image. */
369 if (cinfo->output_scanline + num_lines >= cinfo->output_height) {
370 cinfo->output_scanline = cinfo->output_height;
371 return cinfo->output_height - cinfo->output_scanline;
372 }
373
374 if (num_lines == 0)
375 return 0;
376
377 lines_per_iMCU_row = cinfo->_min_DCT_scaled_size * cinfo->max_v_samp_factor;
378 lines_left_in_iMCU_row =
379 (lines_per_iMCU_row - (cinfo->output_scanline % lines_per_iMCU_row)) %
380 lines_per_iMCU_row;
381 lines_after_iMCU_row = num_lines - lines_left_in_iMCU_row;
382
383 /* Skip the lines remaining in the current iMCU row. When upsampling
384 * requires context rows, we need the previous and next rows in order to read
385 * the current row. This adds some complexity.
386 */
387 if (cinfo->upsample->need_context_rows) {
388 /* If the skipped lines would not move us past the current iMCU row, we
389 * read the lines and ignore them. There might be a faster way of doing
390 * this, but we are facing increasing complexity for diminishing returns.
391 * The increasing complexity would be a by-product of meddling with the
392 * state machine used to skip context rows. Near the end of an iMCU row,
393 * the next iMCU row may have already been entropy-decoded. In this unique
394 * case, we will read the next iMCU row if we cannot skip past it as well.
395 */
396 if ((num_lines < lines_left_in_iMCU_row + 1) ||
397 (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full &&
398 lines_after_iMCU_row < lines_per_iMCU_row + 1)) {
DRC90c92ed2015-07-21 17:36:18 -0500399 read_and_discard_scanlines(cinfo, num_lines);
DRCac30a1b2015-06-25 03:44:36 +0000400 return num_lines;
401 }
402
403 /* If the next iMCU row has already been entropy-decoded, make sure that
404 * we do not skip too far.
405 */
406 if (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full) {
407 cinfo->output_scanline += lines_left_in_iMCU_row + lines_per_iMCU_row;
408 lines_after_iMCU_row -= lines_per_iMCU_row;
409 } else {
410 cinfo->output_scanline += lines_left_in_iMCU_row;
411 }
DRCf8a17752015-06-27 08:10:30 +0000412
413 /* If we have just completed the first block, adjust the buffer pointers */
414 if (main_ptr->iMCU_row_ctr == 0 ||
415 (main_ptr->iMCU_row_ctr == 1 && lines_left_in_iMCU_row > 2))
416 set_wraparound_pointers(cinfo);
DRCac30a1b2015-06-25 03:44:36 +0000417 main_ptr->buffer_full = FALSE;
418 main_ptr->rowgroup_ctr = 0;
419 main_ptr->context_state = CTX_PREPARE_FOR_IMCU;
420 upsample->next_row_out = cinfo->max_v_samp_factor;
421 upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
422 }
423
424 /* Skipping is much simpler when context rows are not required. */
425 else {
426 if (num_lines < lines_left_in_iMCU_row) {
427 increment_simple_rowgroup_ctr(cinfo, num_lines);
428 return num_lines;
429 } else {
430 cinfo->output_scanline += lines_left_in_iMCU_row;
431 main_ptr->buffer_full = FALSE;
432 main_ptr->rowgroup_ctr = 0;
433 upsample->next_row_out = cinfo->max_v_samp_factor;
434 upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
435 }
436 }
437
438 /* Calculate how many full iMCU rows we can skip. */
439 if (cinfo->upsample->need_context_rows)
440 lines_to_skip = ((lines_after_iMCU_row - 1) / lines_per_iMCU_row) *
441 lines_per_iMCU_row;
442 else
443 lines_to_skip = (lines_after_iMCU_row / lines_per_iMCU_row) *
444 lines_per_iMCU_row;
445 /* Calculate the number of lines that remain to be skipped after skipping all
446 * of the full iMCU rows that we can. We will not read these lines unless we
447 * have to.
448 */
449 lines_to_read = lines_after_iMCU_row - lines_to_skip;
450
451 /* For images requiring multiple scans (progressive, non-interleaved, etc.),
452 * all of the entropy decoding occurs in jpeg_start_decompress(), assuming
453 * that the input data source is non-suspending. This makes skipping easy.
454 */
455 if (cinfo->inputctl->has_multiple_scans) {
456 if (cinfo->upsample->need_context_rows) {
457 cinfo->output_scanline += lines_to_skip;
458 cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
DRCf8a17752015-06-27 08:10:30 +0000459 main_ptr->iMCU_row_ctr += lines_after_iMCU_row / lines_per_iMCU_row;
DRCac30a1b2015-06-25 03:44:36 +0000460 /* It is complex to properly move to the middle of a context block, so
461 * read the remaining lines instead of skipping them.
462 */
DRC90c92ed2015-07-21 17:36:18 -0500463 read_and_discard_scanlines(cinfo, lines_to_read);
DRCac30a1b2015-06-25 03:44:36 +0000464 } else {
465 cinfo->output_scanline += lines_to_skip;
466 cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
467 increment_simple_rowgroup_ctr(cinfo, lines_to_read);
468 }
469 upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
470 return num_lines;
471 }
472
473 /* Skip the iMCU rows that we can safely skip. */
474 for (i = 0; i < lines_to_skip; i += lines_per_iMCU_row) {
475 for (y = 0; y < coef->MCU_rows_per_iMCU_row; y++) {
476 for (x = 0; x < cinfo->MCUs_per_row; x++) {
477 /* Calling decode_mcu() with a NULL pointer causes it to discard the
478 * decoded coefficients. This is ~5% faster for large subsets, but
DRCf8a17752015-06-27 08:10:30 +0000479 * it's tough to tell a difference for smaller images.
DRCac30a1b2015-06-25 03:44:36 +0000480 */
481 (*cinfo->entropy->decode_mcu) (cinfo, NULL);
482 }
483 }
484 cinfo->input_iMCU_row++;
485 cinfo->output_iMCU_row++;
486 if (cinfo->input_iMCU_row < cinfo->total_iMCU_rows)
487 start_iMCU_row(cinfo);
488 else
489 (*cinfo->inputctl->finish_input_pass) (cinfo);
490 }
491 cinfo->output_scanline += lines_to_skip;
492
493 if (cinfo->upsample->need_context_rows) {
494 /* Context-based upsampling keeps track of iMCU rows. */
DRCf8a17752015-06-27 08:10:30 +0000495 main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
DRCac30a1b2015-06-25 03:44:36 +0000496
497 /* It is complex to properly move to the middle of a context block, so
498 * read the remaining lines instead of skipping them.
499 */
DRC90c92ed2015-07-21 17:36:18 -0500500 read_and_discard_scanlines(cinfo, lines_to_read);
DRCac30a1b2015-06-25 03:44:36 +0000501 } else {
502 increment_simple_rowgroup_ctr(cinfo, lines_to_read);
503 }
504
505 /* Since skipping lines involves skipping the upsampling step, the value of
506 * "rows_to_go" will become invalid unless we set it here. NOTE: This is a
507 * bit odd, since "rows_to_go" seems to be redundantly keeping track of
508 * output_scanline.
509 */
510 upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
511
512 /* Always skip the requested number of lines. */
513 return num_lines;
514}
515
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000516/*
517 * Alternate entry point to read raw data.
518 * Processes exactly one iMCU row per call, unless suspended.
519 */
520
Thomas G. Lane489583f1996-02-07 00:00:00 +0000521GLOBAL(JDIMENSION)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000522jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data,
DRCe5eaf372014-05-09 18:00:32 +0000523 JDIMENSION max_lines)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000524{
525 JDIMENSION lines_per_iMCU_row;
526
527 if (cinfo->global_state != DSTATE_RAW_OK)
528 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
529 if (cinfo->output_scanline >= cinfo->output_height) {
530 WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
531 return 0;
532 }
533
534 /* Call progress monitor hook if present */
535 if (cinfo->progress != NULL) {
536 cinfo->progress->pass_counter = (long) cinfo->output_scanline;
537 cinfo->progress->pass_limit = (long) cinfo->output_height;
538 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
539 }
540
541 /* Verify that at least one iMCU row can be returned. */
DRC49967cd2010-10-09 19:57:51 +0000542 lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000543 if (max_lines < lines_per_iMCU_row)
544 ERREXIT(cinfo, JERR_BUFFER_SIZE);
545
546 /* Decompress directly into user's buffer. */
547 if (! (*cinfo->coef->decompress_data) (cinfo, data))
DRCe5eaf372014-05-09 18:00:32 +0000548 return 0; /* suspension forced, can do nothing more */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000549
550 /* OK, we processed one iMCU row. */
551 cinfo->output_scanline += lines_per_iMCU_row;
552 return lines_per_iMCU_row;
553}
554
555
556/* Additional entry points for buffered-image mode. */
557
558#ifdef D_MULTISCAN_FILES_SUPPORTED
559
560/*
561 * Initialize for an output pass in buffered-image mode.
562 */
563
Thomas G. Lane489583f1996-02-07 00:00:00 +0000564GLOBAL(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000565jpeg_start_output (j_decompress_ptr cinfo, int scan_number)
566{
567 if (cinfo->global_state != DSTATE_BUFIMAGE &&
568 cinfo->global_state != DSTATE_PRESCAN)
569 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
570 /* Limit scan number to valid range */
571 if (scan_number <= 0)
572 scan_number = 1;
573 if (cinfo->inputctl->eoi_reached &&
574 scan_number > cinfo->input_scan_number)
575 scan_number = cinfo->input_scan_number;
576 cinfo->output_scan_number = scan_number;
577 /* Perform any dummy output passes, and set up for the real pass */
578 return output_pass_setup(cinfo);
579}
580
581
582/*
583 * Finish up after an output pass in buffered-image mode.
584 *
585 * Returns FALSE if suspended. The return value need be inspected only if
586 * a suspending data source is used.
587 */
588
Thomas G. Lane489583f1996-02-07 00:00:00 +0000589GLOBAL(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000590jpeg_finish_output (j_decompress_ptr cinfo)
591{
592 if ((cinfo->global_state == DSTATE_SCANNING ||
593 cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
594 /* Terminate this pass. */
595 /* We do not require the whole pass to have been completed. */
596 (*cinfo->master->finish_output_pass) (cinfo);
597 cinfo->global_state = DSTATE_BUFPOST;
598 } else if (cinfo->global_state != DSTATE_BUFPOST) {
599 /* BUFPOST = repeat call after a suspension, anything else is error */
600 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
601 }
602 /* Read markers looking for SOS or EOI */
603 while (cinfo->input_scan_number <= cinfo->output_scan_number &&
DRCe5eaf372014-05-09 18:00:32 +0000604 ! cinfo->inputctl->eoi_reached) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000605 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
DRCe5eaf372014-05-09 18:00:32 +0000606 return FALSE; /* Suspend, come back later */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000607 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000608 cinfo->global_state = DSTATE_BUFIMAGE;
609 return TRUE;
610}
611
612#endif /* D_MULTISCAN_FILES_SUPPORTED */