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