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