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