blob: 17ce7b62f0a94223747b83400e2199df5b696495 [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:
Leon Scroggins III3993b372018-07-16 10:43:45 -04007 * Copyright (C) 2010, 2015-2018, D. R. Commander.
DRCac30a1b2015-06-25 03:44:36 +00008 * Copyright (C) 2015, Google, Inc.
Alex Naidis6eb7d372016-10-16 23:10:08 +02009 * For conditions of distribution and use, see the accompanying README.ijg
10 * file.
Thomas G. Lanebc79e061995-08-02 00:00:00 +000011 *
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
Alex Naidis6eb7d372016-10-16 23:10:08 +020021#include "jinclude.h"
DRCac30a1b2015-06-25 03:44:36 +000022#include "jdmainct.h"
23#include "jdcoefct.h"
24#include "jdsample.h"
25#include "jmemsys.h"
Thomas G. Lanebc79e061995-08-02 00:00:00 +000026
27/* Forward declarations */
Leon Scroggins III3993b372018-07-16 10:43:45 -040028LOCAL(boolean) output_pass_setup(j_decompress_ptr cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +000029
30
31/*
32 * Decompression initialization.
33 * jpeg_read_header must be completed before calling this.
34 *
35 * If a multipass operating mode was selected, this will do all but the
36 * last pass, and thus may take a great deal of time.
37 *
38 * Returns FALSE if suspended. The return value need be inspected only if
39 * a suspending data source is used.
40 */
41
Thomas G. Lane489583f1996-02-07 00:00:00 +000042GLOBAL(boolean)
Leon Scroggins III3993b372018-07-16 10:43:45 -040043jpeg_start_decompress(j_decompress_ptr cinfo)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000044{
45 if (cinfo->global_state == DSTATE_READY) {
46 /* First call: initialize master control, select active modules */
47 jinit_master_decompress(cinfo);
48 if (cinfo->buffered_image) {
49 /* No more work here; expecting jpeg_start_output next */
50 cinfo->global_state = DSTATE_BUFIMAGE;
51 return TRUE;
52 }
53 cinfo->global_state = DSTATE_PRELOAD;
54 }
55 if (cinfo->global_state == DSTATE_PRELOAD) {
56 /* If file has multiple scans, absorb them all into the coef buffer */
57 if (cinfo->inputctl->has_multiple_scans) {
58#ifdef D_MULTISCAN_FILES_SUPPORTED
59 for (;;) {
DRCe5eaf372014-05-09 18:00:32 +000060 int retcode;
61 /* Call progress monitor hook if present */
62 if (cinfo->progress != NULL)
Leon Scroggins III3993b372018-07-16 10:43:45 -040063 (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
DRCe5eaf372014-05-09 18:00:32 +000064 /* Absorb some more input */
65 retcode = (*cinfo->inputctl->consume_input) (cinfo);
66 if (retcode == JPEG_SUSPENDED)
67 return FALSE;
68 if (retcode == JPEG_REACHED_EOI)
69 break;
70 /* Advance progress counter if appropriate */
71 if (cinfo->progress != NULL &&
72 (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
73 if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
74 /* jdmaster underestimated number of scans; ratchet up one scan */
Leon Scroggins III3993b372018-07-16 10:43:45 -040075 cinfo->progress->pass_limit += (long)cinfo->total_iMCU_rows;
DRCe5eaf372014-05-09 18:00:32 +000076 }
77 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +000078 }
79#else
80 ERREXIT(cinfo, JERR_NOT_COMPILED);
81#endif /* D_MULTISCAN_FILES_SUPPORTED */
82 }
83 cinfo->output_scan_number = cinfo->input_scan_number;
84 } else if (cinfo->global_state != DSTATE_PRESCAN)
85 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
86 /* Perform any dummy output passes, and set up for the final pass */
87 return output_pass_setup(cinfo);
88}
89
90
91/*
92 * Set up for an output pass, and perform any dummy pass(es) needed.
93 * Common subroutine for jpeg_start_decompress and jpeg_start_output.
94 * Entry: global_state = DSTATE_PRESCAN only if previously suspended.
95 * Exit: If done, returns TRUE and sets global_state for proper output mode.
96 * If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.
97 */
98
Thomas G. Lane489583f1996-02-07 00:00:00 +000099LOCAL(boolean)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400100output_pass_setup(j_decompress_ptr cinfo)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000101{
102 if (cinfo->global_state != DSTATE_PRESCAN) {
103 /* First call: do pass setup */
104 (*cinfo->master->prepare_for_output_pass) (cinfo);
105 cinfo->output_scanline = 0;
106 cinfo->global_state = DSTATE_PRESCAN;
107 }
108 /* Loop over any required dummy passes */
109 while (cinfo->master->is_dummy_pass) {
110#ifdef QUANT_2PASS_SUPPORTED
111 /* Crank through the dummy pass */
112 while (cinfo->output_scanline < cinfo->output_height) {
113 JDIMENSION last_scanline;
114 /* Call progress monitor hook if present */
115 if (cinfo->progress != NULL) {
Leon Scroggins III3993b372018-07-16 10:43:45 -0400116 cinfo->progress->pass_counter = (long)cinfo->output_scanline;
117 cinfo->progress->pass_limit = (long)cinfo->output_height;
118 (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000119 }
120 /* Process some data */
121 last_scanline = cinfo->output_scanline;
Leon Scroggins III3993b372018-07-16 10:43:45 -0400122 (*cinfo->main->process_data) (cinfo, (JSAMPARRAY)NULL,
123 &cinfo->output_scanline, (JDIMENSION)0);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000124 if (cinfo->output_scanline == last_scanline)
DRCe5eaf372014-05-09 18:00:32 +0000125 return FALSE; /* No progress made, must suspend */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000126 }
127 /* Finish up dummy pass, and set up for another one */
128 (*cinfo->master->finish_output_pass) (cinfo);
129 (*cinfo->master->prepare_for_output_pass) (cinfo);
130 cinfo->output_scanline = 0;
131#else
132 ERREXIT(cinfo, JERR_NOT_COMPILED);
133#endif /* QUANT_2PASS_SUPPORTED */
134 }
135 /* Ready for application to drive output pass through
136 * jpeg_read_scanlines or jpeg_read_raw_data.
137 */
138 cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
139 return TRUE;
140}
141
142
143/*
DRC0ef076f2016-02-19 18:32:10 -0600144 * Enable partial scanline decompression
145 *
146 * Must be called after jpeg_start_decompress() and before any calls to
147 * jpeg_read_scanlines() or jpeg_skip_scanlines().
148 *
149 * Refer to libjpeg.txt for more information.
150 */
151
152GLOBAL(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400153jpeg_crop_scanline(j_decompress_ptr cinfo, JDIMENSION *xoffset,
154 JDIMENSION *width)
DRC0ef076f2016-02-19 18:32:10 -0600155{
156 int ci, align, orig_downsampled_width;
157 JDIMENSION input_xoffset;
158 boolean reinit_upsampler = FALSE;
159 jpeg_component_info *compptr;
160
161 if (cinfo->global_state != DSTATE_SCANNING || cinfo->output_scanline != 0)
162 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
163
164 if (!xoffset || !width)
165 ERREXIT(cinfo, JERR_BAD_CROP_SPEC);
166
167 /* xoffset and width must fall within the output image dimensions. */
168 if (*width == 0 || *xoffset + *width > cinfo->output_width)
169 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
170
171 /* No need to do anything if the caller wants the entire width. */
172 if (*width == cinfo->output_width)
173 return;
174
175 /* Ensuring the proper alignment of xoffset is tricky. At minimum, it
176 * must align with an MCU boundary, because:
177 *
178 * (1) The IDCT is performed in blocks, and it is not feasible to modify
179 * the algorithm so that it can transform partial blocks.
180 * (2) Because of the SIMD extensions, any input buffer passed to the
181 * upsampling and color conversion routines must be aligned to the
182 * SIMD word size (for instance, 128-bit in the case of SSE2.) The
183 * easiest way to accomplish this without copying data is to ensure
184 * that upsampling and color conversion begin at the start of the
185 * first MCU column that will be inverse transformed.
186 *
187 * In practice, we actually impose a stricter alignment requirement. We
188 * require that xoffset be a multiple of the maximum MCU column width of all
189 * of the components (the "iMCU column width.") This is to simplify the
190 * single-pass decompression case, allowing us to use the same MCU column
191 * width for all of the components.
192 */
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500193 if (cinfo->comps_in_scan == 1 && cinfo->num_components == 1)
194 align = cinfo->_min_DCT_scaled_size;
195 else
196 align = cinfo->_min_DCT_scaled_size * cinfo->max_h_samp_factor;
DRC0ef076f2016-02-19 18:32:10 -0600197
198 /* Adjust xoffset to the nearest iMCU boundary <= the requested value */
199 input_xoffset = *xoffset;
200 *xoffset = (input_xoffset / align) * align;
201
202 /* Adjust the width so that the right edge of the output image is as
203 * requested (only the left edge is altered.) It is important that calling
204 * programs check this value after this function returns, so that they can
205 * allocate an output buffer with the appropriate size.
206 */
207 *width = *width + input_xoffset - *xoffset;
208 cinfo->output_width = *width;
209
210 /* Set the first and last iMCU columns that we must decompress. These values
211 * will be used in single-scan decompressions.
212 */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400213 cinfo->master->first_iMCU_col = (JDIMENSION)(long)(*xoffset) / (long)align;
DRC0ef076f2016-02-19 18:32:10 -0600214 cinfo->master->last_iMCU_col =
Leon Scroggins III3993b372018-07-16 10:43:45 -0400215 (JDIMENSION)jdiv_round_up((long)(*xoffset + cinfo->output_width),
216 (long)align) - 1;
DRC0ef076f2016-02-19 18:32:10 -0600217
218 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
219 ci++, compptr++) {
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500220 int hsf = (cinfo->comps_in_scan == 1 && cinfo->num_components == 1) ?
221 1 : compptr->h_samp_factor;
222
DRC0ef076f2016-02-19 18:32:10 -0600223 /* Set downsampled_width to the new output width. */
224 orig_downsampled_width = compptr->downsampled_width;
225 compptr->downsampled_width =
Leon Scroggins III3993b372018-07-16 10:43:45 -0400226 (JDIMENSION)jdiv_round_up((long)(cinfo->output_width *
227 compptr->h_samp_factor),
228 (long)cinfo->max_h_samp_factor);
DRC0ef076f2016-02-19 18:32:10 -0600229 if (compptr->downsampled_width < 2 && orig_downsampled_width >= 2)
230 reinit_upsampler = TRUE;
231
232 /* Set the first and last iMCU columns that we must decompress. These
233 * values will be used in multi-scan decompressions.
234 */
235 cinfo->master->first_MCU_col[ci] =
Leon Scroggins III3993b372018-07-16 10:43:45 -0400236 (JDIMENSION)(long)(*xoffset * hsf) / (long)align;
DRC0ef076f2016-02-19 18:32:10 -0600237 cinfo->master->last_MCU_col[ci] =
Leon Scroggins III3993b372018-07-16 10:43:45 -0400238 (JDIMENSION)jdiv_round_up((long)((*xoffset + cinfo->output_width) * hsf),
239 (long)align) - 1;
DRC0ef076f2016-02-19 18:32:10 -0600240 }
241
242 if (reinit_upsampler) {
243 cinfo->master->jinit_upsampler_no_alloc = TRUE;
244 jinit_upsampler(cinfo);
245 cinfo->master->jinit_upsampler_no_alloc = FALSE;
246 }
247}
248
249
250/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000251 * Read some scanlines of data from the JPEG decompressor.
252 *
253 * The return value will be the number of lines actually read.
254 * This may be less than the number requested in several cases,
255 * including bottom of image, data source suspension, and operating
256 * modes that emit multiple scanlines at a time.
257 *
258 * Note: we warn about excess calls to jpeg_read_scanlines() since
259 * this likely signals an application programmer error. However,
260 * an oversize buffer (max_lines > scanlines remaining) is not an error.
261 */
262
Thomas G. Lane489583f1996-02-07 00:00:00 +0000263GLOBAL(JDIMENSION)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400264jpeg_read_scanlines(j_decompress_ptr cinfo, JSAMPARRAY scanlines,
265 JDIMENSION max_lines)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000266{
267 JDIMENSION row_ctr;
268
269 if (cinfo->global_state != DSTATE_SCANNING)
270 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
271 if (cinfo->output_scanline >= cinfo->output_height) {
272 WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
273 return 0;
274 }
275
276 /* Call progress monitor hook if present */
277 if (cinfo->progress != NULL) {
Leon Scroggins III3993b372018-07-16 10:43:45 -0400278 cinfo->progress->pass_counter = (long)cinfo->output_scanline;
279 cinfo->progress->pass_limit = (long)cinfo->output_height;
280 (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000281 }
282
283 /* Process some data */
284 row_ctr = 0;
285 (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
286 cinfo->output_scanline += row_ctr;
287 return row_ctr;
288}
289
290
DRC90c92ed2015-07-21 17:36:18 -0500291/* Dummy color convert function used by jpeg_skip_scanlines() */
292LOCAL(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400293noop_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
294 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
DRC90c92ed2015-07-21 17:36:18 -0500295{
296}
297
298
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500299/* Dummy quantize function used by jpeg_skip_scanlines() */
300LOCAL(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400301noop_quantize(j_decompress_ptr cinfo, JSAMPARRAY input_buf,
302 JSAMPARRAY output_buf, int num_rows)
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500303{
304}
305
306
DRC90c92ed2015-07-21 17:36:18 -0500307/*
308 * In some cases, it is best to call jpeg_read_scanlines() and discard the
309 * output, rather than skipping the scanlines, because this allows us to
310 * maintain the internal state of the context-based upsampler. In these cases,
311 * we set up and tear down a dummy color converter in order to avoid valgrind
312 * errors and to achieve the best possible performance.
313 */
DRCac30a1b2015-06-25 03:44:36 +0000314
315LOCAL(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400316read_and_discard_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)
DRCac30a1b2015-06-25 03:44:36 +0000317{
DRC90c92ed2015-07-21 17:36:18 -0500318 JDIMENSION n;
DRC080c4cc2020-11-18 13:25:06 -0600319 JSAMPLE dummy_sample[1] = { 0 };
320 JSAMPROW dummy_row = dummy_sample;
321 JSAMPARRAY scanlines = NULL;
DRC90c92ed2015-07-21 17:36:18 -0500322 void (*color_convert) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
323 JDIMENSION input_row, JSAMPARRAY output_buf,
Leon Scroggins III3993b372018-07-16 10:43:45 -0400324 int num_rows) = NULL;
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500325 void (*color_quantize) (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
326 JSAMPARRAY output_buf, int num_rows) = NULL;
DRCac30a1b2015-06-25 03:44:36 +0000327
Leon Scroggins III3993b372018-07-16 10:43:45 -0400328 if (cinfo->cconvert && cinfo->cconvert->color_convert) {
329 color_convert = cinfo->cconvert->color_convert;
330 cinfo->cconvert->color_convert = noop_convert;
DRC080c4cc2020-11-18 13:25:06 -0600331 /* This just prevents UBSan from complaining about adding 0 to a NULL
332 * pointer. The pointer isn't actually used.
333 */
334 scanlines = &dummy_row;
Leon Scroggins III3993b372018-07-16 10:43:45 -0400335 }
336
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500337 if (cinfo->cquantize && cinfo->cquantize->color_quantize) {
338 color_quantize = cinfo->cquantize->color_quantize;
339 cinfo->cquantize->color_quantize = noop_quantize;
340 }
DRCac30a1b2015-06-25 03:44:36 +0000341
DRC90c92ed2015-07-21 17:36:18 -0500342 for (n = 0; n < num_lines; n++)
DRC080c4cc2020-11-18 13:25:06 -0600343 jpeg_read_scanlines(cinfo, scanlines, 1);
DRC90c92ed2015-07-21 17:36:18 -0500344
Leon Scroggins III3993b372018-07-16 10:43:45 -0400345 if (color_convert)
346 cinfo->cconvert->color_convert = color_convert;
347
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500348 if (color_quantize)
349 cinfo->cquantize->color_quantize = color_quantize;
DRCac30a1b2015-06-25 03:44:36 +0000350}
351
352
353/*
354 * Called by jpeg_skip_scanlines(). This partially skips a decompress block by
355 * incrementing the rowgroup counter.
356 */
357
358LOCAL(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400359increment_simple_rowgroup_ctr(j_decompress_ptr cinfo, JDIMENSION rows)
DRCac30a1b2015-06-25 03:44:36 +0000360{
DRC90c92ed2015-07-21 17:36:18 -0500361 JDIMENSION rows_left;
Leon Scroggins III3993b372018-07-16 10:43:45 -0400362 my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
DRCac30a1b2015-06-25 03:44:36 +0000363
364 /* Increment the counter to the next row group after the skipped rows. */
365 main_ptr->rowgroup_ctr += rows / cinfo->max_v_samp_factor;
366
367 /* Partially skipping a row group would involve modifying the internal state
368 * of the upsampler, so read the remaining rows into a dummy buffer instead.
369 */
370 rows_left = rows % cinfo->max_v_samp_factor;
371 cinfo->output_scanline += rows - rows_left;
372
DRC90c92ed2015-07-21 17:36:18 -0500373 read_and_discard_scanlines(cinfo, rows_left);
DRCac30a1b2015-06-25 03:44:36 +0000374}
375
DRCac30a1b2015-06-25 03:44:36 +0000376/*
377 * Skips some scanlines of data from the JPEG decompressor.
378 *
379 * The return value will be the number of lines actually skipped. If skipping
380 * num_lines would move beyond the end of the image, then the actual number of
381 * lines remaining in the image is returned. Otherwise, the return value will
382 * be equal to num_lines.
383 *
384 * Refer to libjpeg.txt for more information.
385 */
386
387GLOBAL(JDIMENSION)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400388jpeg_skip_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)
DRCac30a1b2015-06-25 03:44:36 +0000389{
Leon Scroggins III3993b372018-07-16 10:43:45 -0400390 my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
391 my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
392 my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
DRC3fb56e92015-06-27 08:10:32 +0000393 JDIMENSION i, x;
394 int y;
DRCac30a1b2015-06-25 03:44:36 +0000395 JDIMENSION lines_per_iMCU_row, lines_left_in_iMCU_row, lines_after_iMCU_row;
396 JDIMENSION lines_to_skip, lines_to_read;
397
398 if (cinfo->global_state != DSTATE_SCANNING)
399 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
400
401 /* Do not skip past the bottom of the image. */
402 if (cinfo->output_scanline + num_lines >= cinfo->output_height) {
403 cinfo->output_scanline = cinfo->output_height;
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -0500404 (*cinfo->inputctl->finish_input_pass) (cinfo);
405 cinfo->inputctl->eoi_reached = TRUE;
DRCac30a1b2015-06-25 03:44:36 +0000406 return cinfo->output_height - cinfo->output_scanline;
407 }
408
409 if (num_lines == 0)
410 return 0;
411
412 lines_per_iMCU_row = cinfo->_min_DCT_scaled_size * cinfo->max_v_samp_factor;
413 lines_left_in_iMCU_row =
414 (lines_per_iMCU_row - (cinfo->output_scanline % lines_per_iMCU_row)) %
415 lines_per_iMCU_row;
416 lines_after_iMCU_row = num_lines - lines_left_in_iMCU_row;
417
418 /* Skip the lines remaining in the current iMCU row. When upsampling
419 * requires context rows, we need the previous and next rows in order to read
420 * the current row. This adds some complexity.
421 */
422 if (cinfo->upsample->need_context_rows) {
423 /* If the skipped lines would not move us past the current iMCU row, we
424 * read the lines and ignore them. There might be a faster way of doing
425 * this, but we are facing increasing complexity for diminishing returns.
426 * The increasing complexity would be a by-product of meddling with the
427 * state machine used to skip context rows. Near the end of an iMCU row,
428 * the next iMCU row may have already been entropy-decoded. In this unique
429 * case, we will read the next iMCU row if we cannot skip past it as well.
430 */
431 if ((num_lines < lines_left_in_iMCU_row + 1) ||
432 (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full &&
433 lines_after_iMCU_row < lines_per_iMCU_row + 1)) {
DRC90c92ed2015-07-21 17:36:18 -0500434 read_and_discard_scanlines(cinfo, num_lines);
DRCac30a1b2015-06-25 03:44:36 +0000435 return num_lines;
436 }
437
438 /* If the next iMCU row has already been entropy-decoded, make sure that
439 * we do not skip too far.
440 */
441 if (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full) {
442 cinfo->output_scanline += lines_left_in_iMCU_row + lines_per_iMCU_row;
443 lines_after_iMCU_row -= lines_per_iMCU_row;
444 } else {
445 cinfo->output_scanline += lines_left_in_iMCU_row;
446 }
DRCf8a17752015-06-27 08:10:30 +0000447
448 /* If we have just completed the first block, adjust the buffer pointers */
449 if (main_ptr->iMCU_row_ctr == 0 ||
450 (main_ptr->iMCU_row_ctr == 1 && lines_left_in_iMCU_row > 2))
451 set_wraparound_pointers(cinfo);
DRCac30a1b2015-06-25 03:44:36 +0000452 main_ptr->buffer_full = FALSE;
453 main_ptr->rowgroup_ctr = 0;
454 main_ptr->context_state = CTX_PREPARE_FOR_IMCU;
455 upsample->next_row_out = cinfo->max_v_samp_factor;
456 upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
457 }
458
459 /* Skipping is much simpler when context rows are not required. */
460 else {
461 if (num_lines < lines_left_in_iMCU_row) {
462 increment_simple_rowgroup_ctr(cinfo, num_lines);
463 return num_lines;
464 } else {
465 cinfo->output_scanline += lines_left_in_iMCU_row;
466 main_ptr->buffer_full = FALSE;
467 main_ptr->rowgroup_ctr = 0;
468 upsample->next_row_out = cinfo->max_v_samp_factor;
469 upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
470 }
471 }
472
473 /* Calculate how many full iMCU rows we can skip. */
474 if (cinfo->upsample->need_context_rows)
475 lines_to_skip = ((lines_after_iMCU_row - 1) / lines_per_iMCU_row) *
476 lines_per_iMCU_row;
477 else
478 lines_to_skip = (lines_after_iMCU_row / lines_per_iMCU_row) *
479 lines_per_iMCU_row;
480 /* Calculate the number of lines that remain to be skipped after skipping all
481 * of the full iMCU rows that we can. We will not read these lines unless we
482 * have to.
483 */
484 lines_to_read = lines_after_iMCU_row - lines_to_skip;
485
486 /* For images requiring multiple scans (progressive, non-interleaved, etc.),
487 * all of the entropy decoding occurs in jpeg_start_decompress(), assuming
488 * that the input data source is non-suspending. This makes skipping easy.
489 */
490 if (cinfo->inputctl->has_multiple_scans) {
491 if (cinfo->upsample->need_context_rows) {
492 cinfo->output_scanline += lines_to_skip;
493 cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
Leon Scroggins III3993b372018-07-16 10:43:45 -0400494 main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
DRCac30a1b2015-06-25 03:44:36 +0000495 /* It is complex to properly move to the middle of a context block, so
496 * read the remaining lines instead of skipping them.
497 */
DRC90c92ed2015-07-21 17:36:18 -0500498 read_and_discard_scanlines(cinfo, lines_to_read);
DRCac30a1b2015-06-25 03:44:36 +0000499 } else {
500 cinfo->output_scanline += lines_to_skip;
501 cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
502 increment_simple_rowgroup_ctr(cinfo, lines_to_read);
503 }
504 upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
505 return num_lines;
506 }
507
508 /* Skip the iMCU rows that we can safely skip. */
509 for (i = 0; i < lines_to_skip; i += lines_per_iMCU_row) {
510 for (y = 0; y < coef->MCU_rows_per_iMCU_row; y++) {
511 for (x = 0; x < cinfo->MCUs_per_row; x++) {
512 /* Calling decode_mcu() with a NULL pointer causes it to discard the
513 * decoded coefficients. This is ~5% faster for large subsets, but
DRCf8a17752015-06-27 08:10:30 +0000514 * it's tough to tell a difference for smaller images.
DRCac30a1b2015-06-25 03:44:36 +0000515 */
516 (*cinfo->entropy->decode_mcu) (cinfo, NULL);
517 }
518 }
519 cinfo->input_iMCU_row++;
520 cinfo->output_iMCU_row++;
521 if (cinfo->input_iMCU_row < cinfo->total_iMCU_rows)
522 start_iMCU_row(cinfo);
523 else
524 (*cinfo->inputctl->finish_input_pass) (cinfo);
525 }
526 cinfo->output_scanline += lines_to_skip;
527
528 if (cinfo->upsample->need_context_rows) {
529 /* Context-based upsampling keeps track of iMCU rows. */
DRCf8a17752015-06-27 08:10:30 +0000530 main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
DRCac30a1b2015-06-25 03:44:36 +0000531
532 /* It is complex to properly move to the middle of a context block, so
533 * read the remaining lines instead of skipping them.
534 */
DRC90c92ed2015-07-21 17:36:18 -0500535 read_and_discard_scanlines(cinfo, lines_to_read);
DRCac30a1b2015-06-25 03:44:36 +0000536 } else {
537 increment_simple_rowgroup_ctr(cinfo, lines_to_read);
538 }
539
540 /* Since skipping lines involves skipping the upsampling step, the value of
541 * "rows_to_go" will become invalid unless we set it here. NOTE: This is a
542 * bit odd, since "rows_to_go" seems to be redundantly keeping track of
543 * output_scanline.
544 */
545 upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
546
547 /* Always skip the requested number of lines. */
548 return num_lines;
549}
550
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000551/*
552 * Alternate entry point to read raw data.
553 * Processes exactly one iMCU row per call, unless suspended.
554 */
555
Thomas G. Lane489583f1996-02-07 00:00:00 +0000556GLOBAL(JDIMENSION)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400557jpeg_read_raw_data(j_decompress_ptr cinfo, JSAMPIMAGE data,
558 JDIMENSION max_lines)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000559{
560 JDIMENSION lines_per_iMCU_row;
561
562 if (cinfo->global_state != DSTATE_RAW_OK)
563 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
564 if (cinfo->output_scanline >= cinfo->output_height) {
565 WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
566 return 0;
567 }
568
569 /* Call progress monitor hook if present */
570 if (cinfo->progress != NULL) {
Leon Scroggins III3993b372018-07-16 10:43:45 -0400571 cinfo->progress->pass_counter = (long)cinfo->output_scanline;
572 cinfo->progress->pass_limit = (long)cinfo->output_height;
573 (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000574 }
575
576 /* Verify that at least one iMCU row can be returned. */
DRC49967cd2010-10-09 19:57:51 +0000577 lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000578 if (max_lines < lines_per_iMCU_row)
579 ERREXIT(cinfo, JERR_BUFFER_SIZE);
580
581 /* Decompress directly into user's buffer. */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400582 if (!(*cinfo->coef->decompress_data) (cinfo, data))
DRCe5eaf372014-05-09 18:00:32 +0000583 return 0; /* suspension forced, can do nothing more */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000584
585 /* OK, we processed one iMCU row. */
586 cinfo->output_scanline += lines_per_iMCU_row;
587 return lines_per_iMCU_row;
588}
589
590
591/* Additional entry points for buffered-image mode. */
592
593#ifdef D_MULTISCAN_FILES_SUPPORTED
594
595/*
596 * Initialize for an output pass in buffered-image mode.
597 */
598
Thomas G. Lane489583f1996-02-07 00:00:00 +0000599GLOBAL(boolean)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400600jpeg_start_output(j_decompress_ptr cinfo, int scan_number)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000601{
602 if (cinfo->global_state != DSTATE_BUFIMAGE &&
603 cinfo->global_state != DSTATE_PRESCAN)
604 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
605 /* Limit scan number to valid range */
606 if (scan_number <= 0)
607 scan_number = 1;
Leon Scroggins III3993b372018-07-16 10:43:45 -0400608 if (cinfo->inputctl->eoi_reached && scan_number > cinfo->input_scan_number)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000609 scan_number = cinfo->input_scan_number;
610 cinfo->output_scan_number = scan_number;
611 /* Perform any dummy output passes, and set up for the real pass */
612 return output_pass_setup(cinfo);
613}
614
615
616/*
617 * Finish up after an output pass in buffered-image mode.
618 *
619 * Returns FALSE if suspended. The return value need be inspected only if
620 * a suspending data source is used.
621 */
622
Thomas G. Lane489583f1996-02-07 00:00:00 +0000623GLOBAL(boolean)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400624jpeg_finish_output(j_decompress_ptr cinfo)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000625{
626 if ((cinfo->global_state == DSTATE_SCANNING ||
627 cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
628 /* Terminate this pass. */
629 /* We do not require the whole pass to have been completed. */
630 (*cinfo->master->finish_output_pass) (cinfo);
631 cinfo->global_state = DSTATE_BUFPOST;
632 } else if (cinfo->global_state != DSTATE_BUFPOST) {
633 /* BUFPOST = repeat call after a suspension, anything else is error */
634 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
635 }
636 /* Read markers looking for SOS or EOI */
637 while (cinfo->input_scan_number <= cinfo->output_scan_number &&
Leon Scroggins III3993b372018-07-16 10:43:45 -0400638 !cinfo->inputctl->eoi_reached) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000639 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
DRCe5eaf372014-05-09 18:00:32 +0000640 return FALSE; /* Suspend, come back later */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000641 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000642 cinfo->global_state = DSTATE_BUFIMAGE;
643 return TRUE;
644}
645
646#endif /* D_MULTISCAN_FILES_SUPPORTED */