Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * jdapistd.c |
| 3 | * |
DRC | a73e870 | 2012-12-31 02:52:30 +0000 | [diff] [blame] | 4 | * This file was part of the Independent JPEG Group's software: |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 5 | * Copyright (C) 1994-1996, Thomas G. Lane. |
DRC | a6ef282 | 2013-09-28 03:23:49 +0000 | [diff] [blame] | 6 | * libjpeg-turbo Modifications: |
DRC | ac30a1b | 2015-06-25 03:44:36 +0000 | [diff] [blame^] | 7 | * Copyright (C) 2010, 2015, D. R. Commander. |
| 8 | * Copyright (C) 2015, Google, Inc. |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 9 | * For conditions of distribution and use, see the accompanying README file. |
| 10 | * |
| 11 | * This file contains application interface code for the decompression half |
| 12 | * of the JPEG library. These are the "standard" API routines that are |
| 13 | * used in the normal full-decompression case. They are not used by a |
| 14 | * transcoding-only application. Note that if an application links in |
| 15 | * jpeg_start_decompress, it will end up linking in the entire decompressor. |
| 16 | * We thus must separate this file from jdapimin.c to avoid linking the |
| 17 | * whole decompression library into a transcoder. |
| 18 | */ |
| 19 | |
DRC | ac30a1b | 2015-06-25 03:44:36 +0000 | [diff] [blame^] | 20 | #include "jdmainct.h" |
| 21 | #include "jdcoefct.h" |
| 22 | #include "jdsample.h" |
| 23 | #include "jmemsys.h" |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 24 | |
| 25 | /* Forward declarations */ |
DRC | bc56b75 | 2014-05-16 10:43:44 +0000 | [diff] [blame] | 26 | LOCAL(boolean) output_pass_setup (j_decompress_ptr cinfo); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 27 | |
| 28 | |
| 29 | /* |
| 30 | * Decompression initialization. |
| 31 | * jpeg_read_header must be completed before calling this. |
| 32 | * |
| 33 | * If a multipass operating mode was selected, this will do all but the |
| 34 | * last pass, and thus may take a great deal of time. |
| 35 | * |
| 36 | * Returns FALSE if suspended. The return value need be inspected only if |
| 37 | * a suspending data source is used. |
| 38 | */ |
| 39 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 40 | GLOBAL(boolean) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 41 | jpeg_start_decompress (j_decompress_ptr cinfo) |
| 42 | { |
| 43 | if (cinfo->global_state == DSTATE_READY) { |
| 44 | /* First call: initialize master control, select active modules */ |
| 45 | jinit_master_decompress(cinfo); |
| 46 | if (cinfo->buffered_image) { |
| 47 | /* No more work here; expecting jpeg_start_output next */ |
| 48 | cinfo->global_state = DSTATE_BUFIMAGE; |
| 49 | return TRUE; |
| 50 | } |
| 51 | cinfo->global_state = DSTATE_PRELOAD; |
| 52 | } |
| 53 | if (cinfo->global_state == DSTATE_PRELOAD) { |
| 54 | /* If file has multiple scans, absorb them all into the coef buffer */ |
| 55 | if (cinfo->inputctl->has_multiple_scans) { |
| 56 | #ifdef D_MULTISCAN_FILES_SUPPORTED |
| 57 | for (;;) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 58 | int retcode; |
| 59 | /* Call progress monitor hook if present */ |
| 60 | if (cinfo->progress != NULL) |
| 61 | (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); |
| 62 | /* Absorb some more input */ |
| 63 | retcode = (*cinfo->inputctl->consume_input) (cinfo); |
| 64 | if (retcode == JPEG_SUSPENDED) |
| 65 | return FALSE; |
| 66 | if (retcode == JPEG_REACHED_EOI) |
| 67 | break; |
| 68 | /* Advance progress counter if appropriate */ |
| 69 | if (cinfo->progress != NULL && |
| 70 | (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) { |
| 71 | if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) { |
| 72 | /* jdmaster underestimated number of scans; ratchet up one scan */ |
| 73 | cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows; |
| 74 | } |
| 75 | } |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 76 | } |
| 77 | #else |
| 78 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
| 79 | #endif /* D_MULTISCAN_FILES_SUPPORTED */ |
| 80 | } |
| 81 | cinfo->output_scan_number = cinfo->input_scan_number; |
| 82 | } else if (cinfo->global_state != DSTATE_PRESCAN) |
| 83 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
| 84 | /* Perform any dummy output passes, and set up for the final pass */ |
| 85 | return output_pass_setup(cinfo); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /* |
| 90 | * Set up for an output pass, and perform any dummy pass(es) needed. |
| 91 | * Common subroutine for jpeg_start_decompress and jpeg_start_output. |
| 92 | * Entry: global_state = DSTATE_PRESCAN only if previously suspended. |
| 93 | * Exit: If done, returns TRUE and sets global_state for proper output mode. |
| 94 | * If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN. |
| 95 | */ |
| 96 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 97 | LOCAL(boolean) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 98 | output_pass_setup (j_decompress_ptr cinfo) |
| 99 | { |
| 100 | if (cinfo->global_state != DSTATE_PRESCAN) { |
| 101 | /* First call: do pass setup */ |
| 102 | (*cinfo->master->prepare_for_output_pass) (cinfo); |
| 103 | cinfo->output_scanline = 0; |
| 104 | cinfo->global_state = DSTATE_PRESCAN; |
| 105 | } |
| 106 | /* Loop over any required dummy passes */ |
| 107 | while (cinfo->master->is_dummy_pass) { |
| 108 | #ifdef QUANT_2PASS_SUPPORTED |
| 109 | /* Crank through the dummy pass */ |
| 110 | while (cinfo->output_scanline < cinfo->output_height) { |
| 111 | JDIMENSION last_scanline; |
| 112 | /* Call progress monitor hook if present */ |
| 113 | if (cinfo->progress != NULL) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 114 | cinfo->progress->pass_counter = (long) cinfo->output_scanline; |
| 115 | cinfo->progress->pass_limit = (long) cinfo->output_height; |
| 116 | (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 117 | } |
| 118 | /* Process some data */ |
| 119 | last_scanline = cinfo->output_scanline; |
| 120 | (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 121 | &cinfo->output_scanline, (JDIMENSION) 0); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 122 | if (cinfo->output_scanline == last_scanline) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 123 | return FALSE; /* No progress made, must suspend */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 124 | } |
| 125 | /* Finish up dummy pass, and set up for another one */ |
| 126 | (*cinfo->master->finish_output_pass) (cinfo); |
| 127 | (*cinfo->master->prepare_for_output_pass) (cinfo); |
| 128 | cinfo->output_scanline = 0; |
| 129 | #else |
| 130 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
| 131 | #endif /* QUANT_2PASS_SUPPORTED */ |
| 132 | } |
| 133 | /* Ready for application to drive output pass through |
| 134 | * jpeg_read_scanlines or jpeg_read_raw_data. |
| 135 | */ |
| 136 | cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING; |
| 137 | return TRUE; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | /* |
| 142 | * Read some scanlines of data from the JPEG decompressor. |
| 143 | * |
| 144 | * The return value will be the number of lines actually read. |
| 145 | * This may be less than the number requested in several cases, |
| 146 | * including bottom of image, data source suspension, and operating |
| 147 | * modes that emit multiple scanlines at a time. |
| 148 | * |
| 149 | * Note: we warn about excess calls to jpeg_read_scanlines() since |
| 150 | * this likely signals an application programmer error. However, |
| 151 | * an oversize buffer (max_lines > scanlines remaining) is not an error. |
| 152 | */ |
| 153 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 154 | GLOBAL(JDIMENSION) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 155 | jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 156 | JDIMENSION max_lines) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 157 | { |
| 158 | JDIMENSION row_ctr; |
| 159 | |
| 160 | if (cinfo->global_state != DSTATE_SCANNING) |
| 161 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
| 162 | if (cinfo->output_scanline >= cinfo->output_height) { |
| 163 | WARNMS(cinfo, JWRN_TOO_MUCH_DATA); |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | /* Call progress monitor hook if present */ |
| 168 | if (cinfo->progress != NULL) { |
| 169 | cinfo->progress->pass_counter = (long) cinfo->output_scanline; |
| 170 | cinfo->progress->pass_limit = (long) cinfo->output_height; |
| 171 | (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); |
| 172 | } |
| 173 | |
| 174 | /* Process some data */ |
| 175 | row_ctr = 0; |
| 176 | (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines); |
| 177 | cinfo->output_scanline += row_ctr; |
| 178 | return row_ctr; |
| 179 | } |
| 180 | |
| 181 | |
DRC | ac30a1b | 2015-06-25 03:44:36 +0000 | [diff] [blame^] | 182 | /* Prepare temporary row buffer */ |
| 183 | |
| 184 | LOCAL(void) |
| 185 | dummy_buffer_setup (j_decompress_ptr cinfo) |
| 186 | { |
| 187 | int nc; |
| 188 | |
| 189 | if (!cinfo->master || cinfo->master->dummy_row_buffer) |
| 190 | return; |
| 191 | |
| 192 | nc = (cinfo->out_color_space == JCS_RGB565) ? |
| 193 | 2 : cinfo->out_color_components; |
| 194 | cinfo->master->dummy_row_buffer = |
| 195 | jpeg_get_small((j_common_ptr) cinfo, |
| 196 | cinfo->output_width * nc * sizeof(JSAMPLE)); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | /* |
| 201 | * Called by jpeg_skip_scanlines(). This partially skips a decompress block by |
| 202 | * incrementing the rowgroup counter. |
| 203 | */ |
| 204 | |
| 205 | LOCAL(void) |
| 206 | increment_simple_rowgroup_ctr (j_decompress_ptr cinfo, JDIMENSION rows) |
| 207 | { |
| 208 | int i; |
| 209 | JDIMENSION rows_left; |
| 210 | my_main_ptr main_ptr = (my_main_ptr) cinfo->main; |
| 211 | |
| 212 | /* Increment the counter to the next row group after the skipped rows. */ |
| 213 | main_ptr->rowgroup_ctr += rows / cinfo->max_v_samp_factor; |
| 214 | |
| 215 | /* Partially skipping a row group would involve modifying the internal state |
| 216 | * of the upsampler, so read the remaining rows into a dummy buffer instead. |
| 217 | */ |
| 218 | rows_left = rows % cinfo->max_v_samp_factor; |
| 219 | cinfo->output_scanline += rows - rows_left; |
| 220 | |
| 221 | dummy_buffer_setup(cinfo); |
| 222 | for (i = 0; i < rows_left; i++) |
| 223 | jpeg_read_scanlines(cinfo, &(cinfo->master->dummy_row_buffer), 1); |
| 224 | } |
| 225 | |
| 226 | |
| 227 | /* |
| 228 | * Called by jpeg_skip_scanlines(). When we skip iMCU rows, we must update the |
| 229 | * iMCU row counter. |
| 230 | */ |
| 231 | |
| 232 | LOCAL(void) |
| 233 | increment_iMCU_ctr (j_decompress_ptr cinfo, JDIMENSION iMCU_rows) |
| 234 | { |
| 235 | my_main_ptr main_ptr = (my_main_ptr) cinfo->main; |
| 236 | if (main_ptr->iMCU_row_ctr == 0 && iMCU_rows > 0) |
| 237 | set_wraparound_pointers(cinfo); |
| 238 | main_ptr->iMCU_row_ctr += iMCU_rows; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | /* |
| 243 | * Skips some scanlines of data from the JPEG decompressor. |
| 244 | * |
| 245 | * The return value will be the number of lines actually skipped. If skipping |
| 246 | * num_lines would move beyond the end of the image, then the actual number of |
| 247 | * lines remaining in the image is returned. Otherwise, the return value will |
| 248 | * be equal to num_lines. |
| 249 | * |
| 250 | * Refer to libjpeg.txt for more information. |
| 251 | */ |
| 252 | |
| 253 | GLOBAL(JDIMENSION) |
| 254 | jpeg_skip_scanlines (j_decompress_ptr cinfo, JDIMENSION num_lines) |
| 255 | { |
| 256 | my_main_ptr main_ptr = (my_main_ptr) cinfo->main; |
| 257 | my_coef_ptr coef = (my_coef_ptr) cinfo->coef; |
| 258 | my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; |
| 259 | int i, y, x; |
| 260 | JDIMENSION lines_per_iMCU_row, lines_left_in_iMCU_row, lines_after_iMCU_row; |
| 261 | JDIMENSION lines_to_skip, lines_to_read; |
| 262 | |
| 263 | if (cinfo->global_state != DSTATE_SCANNING) |
| 264 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
| 265 | |
| 266 | /* Do not skip past the bottom of the image. */ |
| 267 | if (cinfo->output_scanline + num_lines >= cinfo->output_height) { |
| 268 | cinfo->output_scanline = cinfo->output_height; |
| 269 | return cinfo->output_height - cinfo->output_scanline; |
| 270 | } |
| 271 | |
| 272 | if (num_lines == 0) |
| 273 | return 0; |
| 274 | |
| 275 | lines_per_iMCU_row = cinfo->_min_DCT_scaled_size * cinfo->max_v_samp_factor; |
| 276 | lines_left_in_iMCU_row = |
| 277 | (lines_per_iMCU_row - (cinfo->output_scanline % lines_per_iMCU_row)) % |
| 278 | lines_per_iMCU_row; |
| 279 | lines_after_iMCU_row = num_lines - lines_left_in_iMCU_row; |
| 280 | |
| 281 | /* Skip the lines remaining in the current iMCU row. When upsampling |
| 282 | * requires context rows, we need the previous and next rows in order to read |
| 283 | * the current row. This adds some complexity. |
| 284 | */ |
| 285 | if (cinfo->upsample->need_context_rows) { |
| 286 | /* If the skipped lines would not move us past the current iMCU row, we |
| 287 | * read the lines and ignore them. There might be a faster way of doing |
| 288 | * this, but we are facing increasing complexity for diminishing returns. |
| 289 | * The increasing complexity would be a by-product of meddling with the |
| 290 | * state machine used to skip context rows. Near the end of an iMCU row, |
| 291 | * the next iMCU row may have already been entropy-decoded. In this unique |
| 292 | * case, we will read the next iMCU row if we cannot skip past it as well. |
| 293 | */ |
| 294 | if ((num_lines < lines_left_in_iMCU_row + 1) || |
| 295 | (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full && |
| 296 | lines_after_iMCU_row < lines_per_iMCU_row + 1)) { |
| 297 | dummy_buffer_setup(cinfo); |
| 298 | for (i = 0; i < num_lines; i++) |
| 299 | jpeg_read_scanlines(cinfo, &(cinfo->master->dummy_row_buffer), 1); |
| 300 | return num_lines; |
| 301 | } |
| 302 | |
| 303 | /* If the next iMCU row has already been entropy-decoded, make sure that |
| 304 | * we do not skip too far. |
| 305 | */ |
| 306 | if (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full) { |
| 307 | cinfo->output_scanline += lines_left_in_iMCU_row + lines_per_iMCU_row; |
| 308 | lines_after_iMCU_row -= lines_per_iMCU_row; |
| 309 | } else { |
| 310 | cinfo->output_scanline += lines_left_in_iMCU_row; |
| 311 | } |
| 312 | main_ptr->buffer_full = FALSE; |
| 313 | main_ptr->rowgroup_ctr = 0; |
| 314 | main_ptr->context_state = CTX_PREPARE_FOR_IMCU; |
| 315 | upsample->next_row_out = cinfo->max_v_samp_factor; |
| 316 | upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline; |
| 317 | } |
| 318 | |
| 319 | /* Skipping is much simpler when context rows are not required. */ |
| 320 | else { |
| 321 | if (num_lines < lines_left_in_iMCU_row) { |
| 322 | increment_simple_rowgroup_ctr(cinfo, num_lines); |
| 323 | return num_lines; |
| 324 | } else { |
| 325 | cinfo->output_scanline += lines_left_in_iMCU_row; |
| 326 | main_ptr->buffer_full = FALSE; |
| 327 | main_ptr->rowgroup_ctr = 0; |
| 328 | upsample->next_row_out = cinfo->max_v_samp_factor; |
| 329 | upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /* Calculate how many full iMCU rows we can skip. */ |
| 334 | if (cinfo->upsample->need_context_rows) |
| 335 | lines_to_skip = ((lines_after_iMCU_row - 1) / lines_per_iMCU_row) * |
| 336 | lines_per_iMCU_row; |
| 337 | else |
| 338 | lines_to_skip = (lines_after_iMCU_row / lines_per_iMCU_row) * |
| 339 | lines_per_iMCU_row; |
| 340 | /* Calculate the number of lines that remain to be skipped after skipping all |
| 341 | * of the full iMCU rows that we can. We will not read these lines unless we |
| 342 | * have to. |
| 343 | */ |
| 344 | lines_to_read = lines_after_iMCU_row - lines_to_skip; |
| 345 | |
| 346 | /* For images requiring multiple scans (progressive, non-interleaved, etc.), |
| 347 | * all of the entropy decoding occurs in jpeg_start_decompress(), assuming |
| 348 | * that the input data source is non-suspending. This makes skipping easy. |
| 349 | */ |
| 350 | if (cinfo->inputctl->has_multiple_scans) { |
| 351 | if (cinfo->upsample->need_context_rows) { |
| 352 | cinfo->output_scanline += lines_to_skip; |
| 353 | cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row; |
| 354 | increment_iMCU_ctr(cinfo, lines_after_iMCU_row / lines_per_iMCU_row); |
| 355 | /* It is complex to properly move to the middle of a context block, so |
| 356 | * read the remaining lines instead of skipping them. |
| 357 | */ |
| 358 | dummy_buffer_setup(cinfo); |
| 359 | for (i = 0; i < lines_to_read; i++) |
| 360 | jpeg_read_scanlines(cinfo, &(cinfo->master->dummy_row_buffer), 1); |
| 361 | } else { |
| 362 | cinfo->output_scanline += lines_to_skip; |
| 363 | cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row; |
| 364 | increment_simple_rowgroup_ctr(cinfo, lines_to_read); |
| 365 | } |
| 366 | upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline; |
| 367 | return num_lines; |
| 368 | } |
| 369 | |
| 370 | /* Skip the iMCU rows that we can safely skip. */ |
| 371 | for (i = 0; i < lines_to_skip; i += lines_per_iMCU_row) { |
| 372 | for (y = 0; y < coef->MCU_rows_per_iMCU_row; y++) { |
| 373 | for (x = 0; x < cinfo->MCUs_per_row; x++) { |
| 374 | /* Calling decode_mcu() with a NULL pointer causes it to discard the |
| 375 | * decoded coefficients. This is ~5% faster for large subsets, but |
| 376 | * it's tough to tell a difference for smaller images. Another |
| 377 | * advantage of discarding coefficients is that it allows us to avoid |
| 378 | * accessing the private field cinfo->coef->MCU_buffer (which would |
| 379 | * normally be a parameter to decode_mcu().) |
| 380 | */ |
| 381 | (*cinfo->entropy->decode_mcu) (cinfo, NULL); |
| 382 | } |
| 383 | } |
| 384 | cinfo->input_iMCU_row++; |
| 385 | cinfo->output_iMCU_row++; |
| 386 | if (cinfo->input_iMCU_row < cinfo->total_iMCU_rows) |
| 387 | start_iMCU_row(cinfo); |
| 388 | else |
| 389 | (*cinfo->inputctl->finish_input_pass) (cinfo); |
| 390 | } |
| 391 | cinfo->output_scanline += lines_to_skip; |
| 392 | |
| 393 | if (cinfo->upsample->need_context_rows) { |
| 394 | /* Context-based upsampling keeps track of iMCU rows. */ |
| 395 | increment_iMCU_ctr(cinfo, lines_to_skip / lines_per_iMCU_row); |
| 396 | |
| 397 | /* It is complex to properly move to the middle of a context block, so |
| 398 | * read the remaining lines instead of skipping them. |
| 399 | */ |
| 400 | dummy_buffer_setup(cinfo); |
| 401 | for (i = 0; i < lines_to_read; i++) |
| 402 | jpeg_read_scanlines(cinfo, &(cinfo->master->dummy_row_buffer), 1); |
| 403 | } else { |
| 404 | increment_simple_rowgroup_ctr(cinfo, lines_to_read); |
| 405 | } |
| 406 | |
| 407 | /* Since skipping lines involves skipping the upsampling step, the value of |
| 408 | * "rows_to_go" will become invalid unless we set it here. NOTE: This is a |
| 409 | * bit odd, since "rows_to_go" seems to be redundantly keeping track of |
| 410 | * output_scanline. |
| 411 | */ |
| 412 | upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline; |
| 413 | |
| 414 | /* Always skip the requested number of lines. */ |
| 415 | return num_lines; |
| 416 | } |
| 417 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 418 | /* |
| 419 | * Alternate entry point to read raw data. |
| 420 | * Processes exactly one iMCU row per call, unless suspended. |
| 421 | */ |
| 422 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 423 | GLOBAL(JDIMENSION) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 424 | jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 425 | JDIMENSION max_lines) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 426 | { |
| 427 | JDIMENSION lines_per_iMCU_row; |
| 428 | |
| 429 | if (cinfo->global_state != DSTATE_RAW_OK) |
| 430 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
| 431 | if (cinfo->output_scanline >= cinfo->output_height) { |
| 432 | WARNMS(cinfo, JWRN_TOO_MUCH_DATA); |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | /* Call progress monitor hook if present */ |
| 437 | if (cinfo->progress != NULL) { |
| 438 | cinfo->progress->pass_counter = (long) cinfo->output_scanline; |
| 439 | cinfo->progress->pass_limit = (long) cinfo->output_height; |
| 440 | (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); |
| 441 | } |
| 442 | |
| 443 | /* Verify that at least one iMCU row can be returned. */ |
DRC | 49967cd | 2010-10-09 19:57:51 +0000 | [diff] [blame] | 444 | lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 445 | if (max_lines < lines_per_iMCU_row) |
| 446 | ERREXIT(cinfo, JERR_BUFFER_SIZE); |
| 447 | |
| 448 | /* Decompress directly into user's buffer. */ |
| 449 | if (! (*cinfo->coef->decompress_data) (cinfo, data)) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 450 | return 0; /* suspension forced, can do nothing more */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 451 | |
| 452 | /* OK, we processed one iMCU row. */ |
| 453 | cinfo->output_scanline += lines_per_iMCU_row; |
| 454 | return lines_per_iMCU_row; |
| 455 | } |
| 456 | |
| 457 | |
| 458 | /* Additional entry points for buffered-image mode. */ |
| 459 | |
| 460 | #ifdef D_MULTISCAN_FILES_SUPPORTED |
| 461 | |
| 462 | /* |
| 463 | * Initialize for an output pass in buffered-image mode. |
| 464 | */ |
| 465 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 466 | GLOBAL(boolean) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 467 | jpeg_start_output (j_decompress_ptr cinfo, int scan_number) |
| 468 | { |
| 469 | if (cinfo->global_state != DSTATE_BUFIMAGE && |
| 470 | cinfo->global_state != DSTATE_PRESCAN) |
| 471 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
| 472 | /* Limit scan number to valid range */ |
| 473 | if (scan_number <= 0) |
| 474 | scan_number = 1; |
| 475 | if (cinfo->inputctl->eoi_reached && |
| 476 | scan_number > cinfo->input_scan_number) |
| 477 | scan_number = cinfo->input_scan_number; |
| 478 | cinfo->output_scan_number = scan_number; |
| 479 | /* Perform any dummy output passes, and set up for the real pass */ |
| 480 | return output_pass_setup(cinfo); |
| 481 | } |
| 482 | |
| 483 | |
| 484 | /* |
| 485 | * Finish up after an output pass in buffered-image mode. |
| 486 | * |
| 487 | * Returns FALSE if suspended. The return value need be inspected only if |
| 488 | * a suspending data source is used. |
| 489 | */ |
| 490 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 491 | GLOBAL(boolean) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 492 | jpeg_finish_output (j_decompress_ptr cinfo) |
| 493 | { |
| 494 | if ((cinfo->global_state == DSTATE_SCANNING || |
| 495 | cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) { |
| 496 | /* Terminate this pass. */ |
| 497 | /* We do not require the whole pass to have been completed. */ |
| 498 | (*cinfo->master->finish_output_pass) (cinfo); |
| 499 | cinfo->global_state = DSTATE_BUFPOST; |
| 500 | } else if (cinfo->global_state != DSTATE_BUFPOST) { |
| 501 | /* BUFPOST = repeat call after a suspension, anything else is error */ |
| 502 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
| 503 | } |
| 504 | /* Read markers looking for SOS or EOI */ |
| 505 | while (cinfo->input_scan_number <= cinfo->output_scan_number && |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 506 | ! cinfo->inputctl->eoi_reached) { |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 507 | if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 508 | return FALSE; /* Suspend, come back later */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 509 | } |
DRC | ac30a1b | 2015-06-25 03:44:36 +0000 | [diff] [blame^] | 510 | /* Clean up row buffer */ |
| 511 | if (cinfo->master->dummy_row_buffer) { |
| 512 | int nc = (cinfo->out_color_space == JCS_RGB565) ? |
| 513 | 2 : cinfo->out_color_components; |
| 514 | jpeg_free_small((j_common_ptr) cinfo, cinfo->master->dummy_row_buffer, |
| 515 | cinfo->output_width * nc * sizeof(JSAMPLE)); |
| 516 | } |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 517 | cinfo->global_state = DSTATE_BUFIMAGE; |
| 518 | return TRUE; |
| 519 | } |
| 520 | |
| 521 | #endif /* D_MULTISCAN_FILES_SUPPORTED */ |