Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * jdapistd.c |
michael@0 | 3 | * |
michael@0 | 4 | * This file was part of the Independent JPEG Group's software: |
michael@0 | 5 | * Copyright (C) 1994-1996, Thomas G. Lane. |
michael@0 | 6 | * libjpeg-turbo Modifications: |
michael@0 | 7 | * Copyright (C) 2010, D. R. Commander. |
michael@0 | 8 | * For conditions of distribution and use, see the accompanying README file. |
michael@0 | 9 | * |
michael@0 | 10 | * This file contains application interface code for the decompression half |
michael@0 | 11 | * of the JPEG library. These are the "standard" API routines that are |
michael@0 | 12 | * used in the normal full-decompression case. They are not used by a |
michael@0 | 13 | * transcoding-only application. Note that if an application links in |
michael@0 | 14 | * jpeg_start_decompress, it will end up linking in the entire decompressor. |
michael@0 | 15 | * We thus must separate this file from jdapimin.c to avoid linking the |
michael@0 | 16 | * whole decompression library into a transcoder. |
michael@0 | 17 | */ |
michael@0 | 18 | |
michael@0 | 19 | #define JPEG_INTERNALS |
michael@0 | 20 | #include "jinclude.h" |
michael@0 | 21 | #include "jpeglib.h" |
michael@0 | 22 | #include "jpegcomp.h" |
michael@0 | 23 | |
michael@0 | 24 | |
michael@0 | 25 | /* Forward declarations */ |
michael@0 | 26 | LOCAL(boolean) output_pass_setup JPP((j_decompress_ptr cinfo)); |
michael@0 | 27 | |
michael@0 | 28 | |
michael@0 | 29 | /* |
michael@0 | 30 | * Decompression initialization. |
michael@0 | 31 | * jpeg_read_header must be completed before calling this. |
michael@0 | 32 | * |
michael@0 | 33 | * If a multipass operating mode was selected, this will do all but the |
michael@0 | 34 | * last pass, and thus may take a great deal of time. |
michael@0 | 35 | * |
michael@0 | 36 | * Returns FALSE if suspended. The return value need be inspected only if |
michael@0 | 37 | * a suspending data source is used. |
michael@0 | 38 | */ |
michael@0 | 39 | |
michael@0 | 40 | GLOBAL(boolean) |
michael@0 | 41 | jpeg_start_decompress (j_decompress_ptr cinfo) |
michael@0 | 42 | { |
michael@0 | 43 | if (cinfo->global_state == DSTATE_READY) { |
michael@0 | 44 | /* First call: initialize master control, select active modules */ |
michael@0 | 45 | jinit_master_decompress(cinfo); |
michael@0 | 46 | if (cinfo->buffered_image) { |
michael@0 | 47 | /* No more work here; expecting jpeg_start_output next */ |
michael@0 | 48 | cinfo->global_state = DSTATE_BUFIMAGE; |
michael@0 | 49 | return TRUE; |
michael@0 | 50 | } |
michael@0 | 51 | cinfo->global_state = DSTATE_PRELOAD; |
michael@0 | 52 | } |
michael@0 | 53 | if (cinfo->global_state == DSTATE_PRELOAD) { |
michael@0 | 54 | /* If file has multiple scans, absorb them all into the coef buffer */ |
michael@0 | 55 | if (cinfo->inputctl->has_multiple_scans) { |
michael@0 | 56 | #ifdef D_MULTISCAN_FILES_SUPPORTED |
michael@0 | 57 | for (;;) { |
michael@0 | 58 | int retcode; |
michael@0 | 59 | /* Call progress monitor hook if present */ |
michael@0 | 60 | if (cinfo->progress != NULL) |
michael@0 | 61 | (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); |
michael@0 | 62 | /* Absorb some more input */ |
michael@0 | 63 | retcode = (*cinfo->inputctl->consume_input) (cinfo); |
michael@0 | 64 | if (retcode == JPEG_SUSPENDED) |
michael@0 | 65 | return FALSE; |
michael@0 | 66 | if (retcode == JPEG_REACHED_EOI) |
michael@0 | 67 | break; |
michael@0 | 68 | /* Advance progress counter if appropriate */ |
michael@0 | 69 | if (cinfo->progress != NULL && |
michael@0 | 70 | (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) { |
michael@0 | 71 | if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) { |
michael@0 | 72 | /* jdmaster underestimated number of scans; ratchet up one scan */ |
michael@0 | 73 | cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows; |
michael@0 | 74 | } |
michael@0 | 75 | } |
michael@0 | 76 | } |
michael@0 | 77 | #else |
michael@0 | 78 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
michael@0 | 79 | #endif /* D_MULTISCAN_FILES_SUPPORTED */ |
michael@0 | 80 | } |
michael@0 | 81 | cinfo->output_scan_number = cinfo->input_scan_number; |
michael@0 | 82 | } else if (cinfo->global_state != DSTATE_PRESCAN) |
michael@0 | 83 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
michael@0 | 84 | /* Perform any dummy output passes, and set up for the final pass */ |
michael@0 | 85 | return output_pass_setup(cinfo); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | |
michael@0 | 89 | /* |
michael@0 | 90 | * Set up for an output pass, and perform any dummy pass(es) needed. |
michael@0 | 91 | * Common subroutine for jpeg_start_decompress and jpeg_start_output. |
michael@0 | 92 | * Entry: global_state = DSTATE_PRESCAN only if previously suspended. |
michael@0 | 93 | * Exit: If done, returns TRUE and sets global_state for proper output mode. |
michael@0 | 94 | * If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN. |
michael@0 | 95 | */ |
michael@0 | 96 | |
michael@0 | 97 | LOCAL(boolean) |
michael@0 | 98 | output_pass_setup (j_decompress_ptr cinfo) |
michael@0 | 99 | { |
michael@0 | 100 | if (cinfo->global_state != DSTATE_PRESCAN) { |
michael@0 | 101 | /* First call: do pass setup */ |
michael@0 | 102 | (*cinfo->master->prepare_for_output_pass) (cinfo); |
michael@0 | 103 | cinfo->output_scanline = 0; |
michael@0 | 104 | cinfo->global_state = DSTATE_PRESCAN; |
michael@0 | 105 | } |
michael@0 | 106 | /* Loop over any required dummy passes */ |
michael@0 | 107 | while (cinfo->master->is_dummy_pass) { |
michael@0 | 108 | #ifdef QUANT_2PASS_SUPPORTED |
michael@0 | 109 | /* Crank through the dummy pass */ |
michael@0 | 110 | while (cinfo->output_scanline < cinfo->output_height) { |
michael@0 | 111 | JDIMENSION last_scanline; |
michael@0 | 112 | /* Call progress monitor hook if present */ |
michael@0 | 113 | if (cinfo->progress != NULL) { |
michael@0 | 114 | cinfo->progress->pass_counter = (long) cinfo->output_scanline; |
michael@0 | 115 | cinfo->progress->pass_limit = (long) cinfo->output_height; |
michael@0 | 116 | (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); |
michael@0 | 117 | } |
michael@0 | 118 | /* Process some data */ |
michael@0 | 119 | last_scanline = cinfo->output_scanline; |
michael@0 | 120 | (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL, |
michael@0 | 121 | &cinfo->output_scanline, (JDIMENSION) 0); |
michael@0 | 122 | if (cinfo->output_scanline == last_scanline) |
michael@0 | 123 | return FALSE; /* No progress made, must suspend */ |
michael@0 | 124 | } |
michael@0 | 125 | /* Finish up dummy pass, and set up for another one */ |
michael@0 | 126 | (*cinfo->master->finish_output_pass) (cinfo); |
michael@0 | 127 | (*cinfo->master->prepare_for_output_pass) (cinfo); |
michael@0 | 128 | cinfo->output_scanline = 0; |
michael@0 | 129 | #else |
michael@0 | 130 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
michael@0 | 131 | #endif /* QUANT_2PASS_SUPPORTED */ |
michael@0 | 132 | } |
michael@0 | 133 | /* Ready for application to drive output pass through |
michael@0 | 134 | * jpeg_read_scanlines or jpeg_read_raw_data. |
michael@0 | 135 | */ |
michael@0 | 136 | cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING; |
michael@0 | 137 | return TRUE; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | |
michael@0 | 141 | /* |
michael@0 | 142 | * Read some scanlines of data from the JPEG decompressor. |
michael@0 | 143 | * |
michael@0 | 144 | * The return value will be the number of lines actually read. |
michael@0 | 145 | * This may be less than the number requested in several cases, |
michael@0 | 146 | * including bottom of image, data source suspension, and operating |
michael@0 | 147 | * modes that emit multiple scanlines at a time. |
michael@0 | 148 | * |
michael@0 | 149 | * Note: we warn about excess calls to jpeg_read_scanlines() since |
michael@0 | 150 | * this likely signals an application programmer error. However, |
michael@0 | 151 | * an oversize buffer (max_lines > scanlines remaining) is not an error. |
michael@0 | 152 | */ |
michael@0 | 153 | |
michael@0 | 154 | GLOBAL(JDIMENSION) |
michael@0 | 155 | jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines, |
michael@0 | 156 | JDIMENSION max_lines) |
michael@0 | 157 | { |
michael@0 | 158 | JDIMENSION row_ctr; |
michael@0 | 159 | |
michael@0 | 160 | if (cinfo->global_state != DSTATE_SCANNING) |
michael@0 | 161 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
michael@0 | 162 | if (cinfo->output_scanline >= cinfo->output_height) { |
michael@0 | 163 | WARNMS(cinfo, JWRN_TOO_MUCH_DATA); |
michael@0 | 164 | return 0; |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | /* Call progress monitor hook if present */ |
michael@0 | 168 | if (cinfo->progress != NULL) { |
michael@0 | 169 | cinfo->progress->pass_counter = (long) cinfo->output_scanline; |
michael@0 | 170 | cinfo->progress->pass_limit = (long) cinfo->output_height; |
michael@0 | 171 | (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | /* Process some data */ |
michael@0 | 175 | row_ctr = 0; |
michael@0 | 176 | (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines); |
michael@0 | 177 | cinfo->output_scanline += row_ctr; |
michael@0 | 178 | return row_ctr; |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | |
michael@0 | 182 | /* |
michael@0 | 183 | * Alternate entry point to read raw data. |
michael@0 | 184 | * Processes exactly one iMCU row per call, unless suspended. |
michael@0 | 185 | */ |
michael@0 | 186 | |
michael@0 | 187 | GLOBAL(JDIMENSION) |
michael@0 | 188 | jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data, |
michael@0 | 189 | JDIMENSION max_lines) |
michael@0 | 190 | { |
michael@0 | 191 | JDIMENSION lines_per_iMCU_row; |
michael@0 | 192 | |
michael@0 | 193 | if (cinfo->global_state != DSTATE_RAW_OK) |
michael@0 | 194 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
michael@0 | 195 | if (cinfo->output_scanline >= cinfo->output_height) { |
michael@0 | 196 | WARNMS(cinfo, JWRN_TOO_MUCH_DATA); |
michael@0 | 197 | return 0; |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | /* Call progress monitor hook if present */ |
michael@0 | 201 | if (cinfo->progress != NULL) { |
michael@0 | 202 | cinfo->progress->pass_counter = (long) cinfo->output_scanline; |
michael@0 | 203 | cinfo->progress->pass_limit = (long) cinfo->output_height; |
michael@0 | 204 | (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | /* Verify that at least one iMCU row can be returned. */ |
michael@0 | 208 | lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size; |
michael@0 | 209 | if (max_lines < lines_per_iMCU_row) |
michael@0 | 210 | ERREXIT(cinfo, JERR_BUFFER_SIZE); |
michael@0 | 211 | |
michael@0 | 212 | /* Decompress directly into user's buffer. */ |
michael@0 | 213 | if (! (*cinfo->coef->decompress_data) (cinfo, data)) |
michael@0 | 214 | return 0; /* suspension forced, can do nothing more */ |
michael@0 | 215 | |
michael@0 | 216 | /* OK, we processed one iMCU row. */ |
michael@0 | 217 | cinfo->output_scanline += lines_per_iMCU_row; |
michael@0 | 218 | return lines_per_iMCU_row; |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | |
michael@0 | 222 | /* Additional entry points for buffered-image mode. */ |
michael@0 | 223 | |
michael@0 | 224 | #ifdef D_MULTISCAN_FILES_SUPPORTED |
michael@0 | 225 | |
michael@0 | 226 | /* |
michael@0 | 227 | * Initialize for an output pass in buffered-image mode. |
michael@0 | 228 | */ |
michael@0 | 229 | |
michael@0 | 230 | GLOBAL(boolean) |
michael@0 | 231 | jpeg_start_output (j_decompress_ptr cinfo, int scan_number) |
michael@0 | 232 | { |
michael@0 | 233 | if (cinfo->global_state != DSTATE_BUFIMAGE && |
michael@0 | 234 | cinfo->global_state != DSTATE_PRESCAN) |
michael@0 | 235 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
michael@0 | 236 | /* Limit scan number to valid range */ |
michael@0 | 237 | if (scan_number <= 0) |
michael@0 | 238 | scan_number = 1; |
michael@0 | 239 | if (cinfo->inputctl->eoi_reached && |
michael@0 | 240 | scan_number > cinfo->input_scan_number) |
michael@0 | 241 | scan_number = cinfo->input_scan_number; |
michael@0 | 242 | cinfo->output_scan_number = scan_number; |
michael@0 | 243 | /* Perform any dummy output passes, and set up for the real pass */ |
michael@0 | 244 | return output_pass_setup(cinfo); |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | |
michael@0 | 248 | /* |
michael@0 | 249 | * Finish up after an output pass in buffered-image mode. |
michael@0 | 250 | * |
michael@0 | 251 | * Returns FALSE if suspended. The return value need be inspected only if |
michael@0 | 252 | * a suspending data source is used. |
michael@0 | 253 | */ |
michael@0 | 254 | |
michael@0 | 255 | GLOBAL(boolean) |
michael@0 | 256 | jpeg_finish_output (j_decompress_ptr cinfo) |
michael@0 | 257 | { |
michael@0 | 258 | if ((cinfo->global_state == DSTATE_SCANNING || |
michael@0 | 259 | cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) { |
michael@0 | 260 | /* Terminate this pass. */ |
michael@0 | 261 | /* We do not require the whole pass to have been completed. */ |
michael@0 | 262 | (*cinfo->master->finish_output_pass) (cinfo); |
michael@0 | 263 | cinfo->global_state = DSTATE_BUFPOST; |
michael@0 | 264 | } else if (cinfo->global_state != DSTATE_BUFPOST) { |
michael@0 | 265 | /* BUFPOST = repeat call after a suspension, anything else is error */ |
michael@0 | 266 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
michael@0 | 267 | } |
michael@0 | 268 | /* Read markers looking for SOS or EOI */ |
michael@0 | 269 | while (cinfo->input_scan_number <= cinfo->output_scan_number && |
michael@0 | 270 | ! cinfo->inputctl->eoi_reached) { |
michael@0 | 271 | if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED) |
michael@0 | 272 | return FALSE; /* Suspend, come back later */ |
michael@0 | 273 | } |
michael@0 | 274 | cinfo->global_state = DSTATE_BUFIMAGE; |
michael@0 | 275 | return TRUE; |
michael@0 | 276 | } |
michael@0 | 277 | |
michael@0 | 278 | #endif /* D_MULTISCAN_FILES_SUPPORTED */ |