Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * jdmaster.c |
| 3 | * |
| 4 | * Copyright (C) 1991-1997, Thomas G. Lane. |
| 5 | * This file is part of the Independent JPEG Group's software. |
| 6 | * For conditions of distribution and use, see the accompanying README file. |
| 7 | * |
| 8 | * This file contains master control logic for the JPEG decompressor. |
| 9 | * These routines are concerned with selecting the modules to be executed |
| 10 | * and with determining the number of passes and the work to be done in each |
| 11 | * pass. |
| 12 | */ |
| 13 | |
| 14 | #define JPEG_INTERNALS |
| 15 | #include "jinclude.h" |
| 16 | #include "jpeglib.h" |
| 17 | |
| 18 | |
| 19 | /* Private state */ |
| 20 | |
| 21 | typedef struct { |
| 22 | struct jpeg_decomp_master pub; /* public fields */ |
| 23 | |
| 24 | int pass_number; /* # of passes completed */ |
| 25 | |
| 26 | boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */ |
| 27 | |
| 28 | /* Saved references to initialized quantizer modules, |
| 29 | * in case we need to switch modes. |
| 30 | */ |
| 31 | struct jpeg_color_quantizer * quantizer_1pass; |
| 32 | struct jpeg_color_quantizer * quantizer_2pass; |
| 33 | } my_decomp_master; |
| 34 | |
| 35 | typedef my_decomp_master * my_master_ptr; |
| 36 | |
| 37 | |
| 38 | /* |
| 39 | * Determine whether merged upsample/color conversion should be used. |
| 40 | * CRUCIAL: this must match the actual capabilities of jdmerge.c! |
| 41 | */ |
| 42 | |
| 43 | LOCAL(boolean) |
| 44 | use_merged_upsample (j_decompress_ptr cinfo) |
| 45 | { |
| 46 | #ifdef UPSAMPLE_MERGING_SUPPORTED |
| 47 | /* Merging is the equivalent of plain box-filter upsampling */ |
| 48 | if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling) |
| 49 | return FALSE; |
| 50 | /* jdmerge.c only supports YCC=>RGB color conversion */ |
| 51 | if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 || |
DRC | 80bc60b | 2009-04-05 21:51:25 +0000 | [diff] [blame] | 52 | (cinfo->out_color_space != JCS_RGB && |
| 53 | cinfo->out_color_space != JCS_EXT_RGB && |
| 54 | cinfo->out_color_space != JCS_EXT_RGBX && |
| 55 | cinfo->out_color_space != JCS_EXT_BGR && |
| 56 | cinfo->out_color_space != JCS_EXT_BGRX && |
| 57 | cinfo->out_color_space != JCS_EXT_XBGR && |
| 58 | cinfo->out_color_space != JCS_EXT_XRGB) || |
| 59 | cinfo->out_color_components != rgb_pixelsize[cinfo->out_color_space]) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 60 | return FALSE; |
| 61 | /* and it only handles 2h1v or 2h2v sampling ratios */ |
| 62 | if (cinfo->comp_info[0].h_samp_factor != 2 || |
| 63 | cinfo->comp_info[1].h_samp_factor != 1 || |
| 64 | cinfo->comp_info[2].h_samp_factor != 1 || |
| 65 | cinfo->comp_info[0].v_samp_factor > 2 || |
| 66 | cinfo->comp_info[1].v_samp_factor != 1 || |
| 67 | cinfo->comp_info[2].v_samp_factor != 1) |
| 68 | return FALSE; |
| 69 | /* furthermore, it doesn't work if we've scaled the IDCTs differently */ |
| 70 | if (cinfo->comp_info[0].DCT_scaled_size != cinfo->min_DCT_scaled_size || |
| 71 | cinfo->comp_info[1].DCT_scaled_size != cinfo->min_DCT_scaled_size || |
| 72 | cinfo->comp_info[2].DCT_scaled_size != cinfo->min_DCT_scaled_size) |
| 73 | return FALSE; |
| 74 | /* ??? also need to test for upsample-time rescaling, when & if supported */ |
| 75 | return TRUE; /* by golly, it'll work... */ |
| 76 | #else |
| 77 | return FALSE; |
| 78 | #endif |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /* |
| 83 | * Compute output image dimensions and related values. |
| 84 | * NOTE: this is exported for possible use by application. |
| 85 | * Hence it mustn't do anything that can't be done twice. |
| 86 | * Also note that it may be called before the master module is initialized! |
| 87 | */ |
| 88 | |
| 89 | GLOBAL(void) |
| 90 | jpeg_calc_output_dimensions (j_decompress_ptr cinfo) |
| 91 | /* Do computations that are needed before master selection phase */ |
| 92 | { |
| 93 | #ifdef IDCT_SCALING_SUPPORTED |
| 94 | int ci; |
| 95 | jpeg_component_info *compptr; |
| 96 | #endif |
| 97 | |
| 98 | /* Prevent application from calling me at wrong times */ |
| 99 | if (cinfo->global_state != DSTATE_READY) |
| 100 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
| 101 | |
| 102 | #ifdef IDCT_SCALING_SUPPORTED |
| 103 | |
| 104 | /* Compute actual output image dimensions and DCT scaling choices. */ |
| 105 | if (cinfo->scale_num * 8 <= cinfo->scale_denom) { |
| 106 | /* Provide 1/8 scaling */ |
| 107 | cinfo->output_width = (JDIMENSION) |
| 108 | jdiv_round_up((long) cinfo->image_width, 8L); |
| 109 | cinfo->output_height = (JDIMENSION) |
| 110 | jdiv_round_up((long) cinfo->image_height, 8L); |
| 111 | cinfo->min_DCT_scaled_size = 1; |
| 112 | } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) { |
| 113 | /* Provide 1/4 scaling */ |
| 114 | cinfo->output_width = (JDIMENSION) |
| 115 | jdiv_round_up((long) cinfo->image_width, 4L); |
| 116 | cinfo->output_height = (JDIMENSION) |
| 117 | jdiv_round_up((long) cinfo->image_height, 4L); |
| 118 | cinfo->min_DCT_scaled_size = 2; |
| 119 | } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) { |
| 120 | /* Provide 1/2 scaling */ |
| 121 | cinfo->output_width = (JDIMENSION) |
| 122 | jdiv_round_up((long) cinfo->image_width, 2L); |
| 123 | cinfo->output_height = (JDIMENSION) |
| 124 | jdiv_round_up((long) cinfo->image_height, 2L); |
| 125 | cinfo->min_DCT_scaled_size = 4; |
| 126 | } else { |
| 127 | /* Provide 1/1 scaling */ |
| 128 | cinfo->output_width = cinfo->image_width; |
| 129 | cinfo->output_height = cinfo->image_height; |
| 130 | cinfo->min_DCT_scaled_size = DCTSIZE; |
| 131 | } |
| 132 | /* In selecting the actual DCT scaling for each component, we try to |
| 133 | * scale up the chroma components via IDCT scaling rather than upsampling. |
| 134 | * This saves time if the upsampler gets to use 1:1 scaling. |
| 135 | * Note this code assumes that the supported DCT scalings are powers of 2. |
| 136 | */ |
| 137 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
| 138 | ci++, compptr++) { |
| 139 | int ssize = cinfo->min_DCT_scaled_size; |
| 140 | while (ssize < DCTSIZE && |
| 141 | (compptr->h_samp_factor * ssize * 2 <= |
| 142 | cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) && |
| 143 | (compptr->v_samp_factor * ssize * 2 <= |
| 144 | cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) { |
| 145 | ssize = ssize * 2; |
| 146 | } |
| 147 | compptr->DCT_scaled_size = ssize; |
| 148 | } |
| 149 | |
| 150 | /* Recompute downsampled dimensions of components; |
| 151 | * application needs to know these if using raw downsampled data. |
| 152 | */ |
| 153 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
| 154 | ci++, compptr++) { |
| 155 | /* Size in samples, after IDCT scaling */ |
| 156 | compptr->downsampled_width = (JDIMENSION) |
| 157 | jdiv_round_up((long) cinfo->image_width * |
| 158 | (long) (compptr->h_samp_factor * compptr->DCT_scaled_size), |
| 159 | (long) (cinfo->max_h_samp_factor * DCTSIZE)); |
| 160 | compptr->downsampled_height = (JDIMENSION) |
| 161 | jdiv_round_up((long) cinfo->image_height * |
| 162 | (long) (compptr->v_samp_factor * compptr->DCT_scaled_size), |
| 163 | (long) (cinfo->max_v_samp_factor * DCTSIZE)); |
| 164 | } |
| 165 | |
| 166 | #else /* !IDCT_SCALING_SUPPORTED */ |
| 167 | |
| 168 | /* Hardwire it to "no scaling" */ |
| 169 | cinfo->output_width = cinfo->image_width; |
| 170 | cinfo->output_height = cinfo->image_height; |
| 171 | /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE, |
| 172 | * and has computed unscaled downsampled_width and downsampled_height. |
| 173 | */ |
| 174 | |
| 175 | #endif /* IDCT_SCALING_SUPPORTED */ |
| 176 | |
| 177 | /* Report number of components in selected colorspace. */ |
| 178 | /* Probably this should be in the color conversion module... */ |
| 179 | switch (cinfo->out_color_space) { |
| 180 | case JCS_GRAYSCALE: |
| 181 | cinfo->out_color_components = 1; |
| 182 | break; |
| 183 | case JCS_RGB: |
DRC | 80bc60b | 2009-04-05 21:51:25 +0000 | [diff] [blame] | 184 | case JCS_EXT_RGB: |
| 185 | case JCS_EXT_RGBX: |
| 186 | case JCS_EXT_BGR: |
| 187 | case JCS_EXT_BGRX: |
| 188 | case JCS_EXT_XBGR: |
| 189 | case JCS_EXT_XRGB: |
| 190 | cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space]; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 191 | break; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 192 | case JCS_YCbCr: |
| 193 | cinfo->out_color_components = 3; |
| 194 | break; |
| 195 | case JCS_CMYK: |
| 196 | case JCS_YCCK: |
| 197 | cinfo->out_color_components = 4; |
| 198 | break; |
| 199 | default: /* else must be same colorspace as in file */ |
| 200 | cinfo->out_color_components = cinfo->num_components; |
| 201 | break; |
| 202 | } |
| 203 | cinfo->output_components = (cinfo->quantize_colors ? 1 : |
| 204 | cinfo->out_color_components); |
| 205 | |
| 206 | /* See if upsampler will want to emit more than one row at a time */ |
| 207 | if (use_merged_upsample(cinfo)) |
| 208 | cinfo->rec_outbuf_height = cinfo->max_v_samp_factor; |
| 209 | else |
| 210 | cinfo->rec_outbuf_height = 1; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | /* |
| 215 | * Several decompression processes need to range-limit values to the range |
| 216 | * 0..MAXJSAMPLE; the input value may fall somewhat outside this range |
| 217 | * due to noise introduced by quantization, roundoff error, etc. These |
| 218 | * processes are inner loops and need to be as fast as possible. On most |
| 219 | * machines, particularly CPUs with pipelines or instruction prefetch, |
| 220 | * a (subscript-check-less) C table lookup |
| 221 | * x = sample_range_limit[x]; |
| 222 | * is faster than explicit tests |
| 223 | * if (x < 0) x = 0; |
| 224 | * else if (x > MAXJSAMPLE) x = MAXJSAMPLE; |
| 225 | * These processes all use a common table prepared by the routine below. |
| 226 | * |
| 227 | * For most steps we can mathematically guarantee that the initial value |
| 228 | * of x is within MAXJSAMPLE+1 of the legal range, so a table running from |
| 229 | * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial |
| 230 | * limiting step (just after the IDCT), a wildly out-of-range value is |
| 231 | * possible if the input data is corrupt. To avoid any chance of indexing |
| 232 | * off the end of memory and getting a bad-pointer trap, we perform the |
| 233 | * post-IDCT limiting thus: |
| 234 | * x = range_limit[x & MASK]; |
| 235 | * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit |
| 236 | * samples. Under normal circumstances this is more than enough range and |
| 237 | * a correct output will be generated; with bogus input data the mask will |
| 238 | * cause wraparound, and we will safely generate a bogus-but-in-range output. |
| 239 | * For the post-IDCT step, we want to convert the data from signed to unsigned |
| 240 | * representation by adding CENTERJSAMPLE at the same time that we limit it. |
| 241 | * So the post-IDCT limiting table ends up looking like this: |
| 242 | * CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE, |
| 243 | * MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times), |
| 244 | * 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times), |
| 245 | * 0,1,...,CENTERJSAMPLE-1 |
| 246 | * Negative inputs select values from the upper half of the table after |
| 247 | * masking. |
| 248 | * |
| 249 | * We can save some space by overlapping the start of the post-IDCT table |
| 250 | * with the simpler range limiting table. The post-IDCT table begins at |
| 251 | * sample_range_limit + CENTERJSAMPLE. |
| 252 | * |
| 253 | * Note that the table is allocated in near data space on PCs; it's small |
| 254 | * enough and used often enough to justify this. |
| 255 | */ |
| 256 | |
| 257 | LOCAL(void) |
| 258 | prepare_range_limit_table (j_decompress_ptr cinfo) |
| 259 | /* Allocate and fill in the sample_range_limit table */ |
| 260 | { |
| 261 | JSAMPLE * table; |
| 262 | int i; |
| 263 | |
| 264 | table = (JSAMPLE *) |
| 265 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 266 | (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE)); |
| 267 | table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */ |
| 268 | cinfo->sample_range_limit = table; |
| 269 | /* First segment of "simple" table: limit[x] = 0 for x < 0 */ |
| 270 | MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE)); |
| 271 | /* Main part of "simple" table: limit[x] = x */ |
| 272 | for (i = 0; i <= MAXJSAMPLE; i++) |
| 273 | table[i] = (JSAMPLE) i; |
| 274 | table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */ |
| 275 | /* End of simple table, rest of first half of post-IDCT table */ |
| 276 | for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++) |
| 277 | table[i] = MAXJSAMPLE; |
| 278 | /* Second half of post-IDCT table */ |
| 279 | MEMZERO(table + (2 * (MAXJSAMPLE+1)), |
| 280 | (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE)); |
| 281 | MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE), |
| 282 | cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE)); |
| 283 | } |
| 284 | |
| 285 | |
| 286 | /* |
| 287 | * Master selection of decompression modules. |
| 288 | * This is done once at jpeg_start_decompress time. We determine |
| 289 | * which modules will be used and give them appropriate initialization calls. |
| 290 | * We also initialize the decompressor input side to begin consuming data. |
| 291 | * |
| 292 | * Since jpeg_read_header has finished, we know what is in the SOF |
| 293 | * and (first) SOS markers. We also have all the application parameter |
| 294 | * settings. |
| 295 | */ |
| 296 | |
| 297 | LOCAL(void) |
| 298 | master_selection (j_decompress_ptr cinfo) |
| 299 | { |
| 300 | my_master_ptr master = (my_master_ptr) cinfo->master; |
| 301 | boolean use_c_buffer; |
| 302 | long samplesperrow; |
| 303 | JDIMENSION jd_samplesperrow; |
| 304 | |
| 305 | /* Initialize dimensions and other stuff */ |
| 306 | jpeg_calc_output_dimensions(cinfo); |
| 307 | prepare_range_limit_table(cinfo); |
| 308 | |
| 309 | /* Width of an output scanline must be representable as JDIMENSION. */ |
| 310 | samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components; |
| 311 | jd_samplesperrow = (JDIMENSION) samplesperrow; |
| 312 | if ((long) jd_samplesperrow != samplesperrow) |
| 313 | ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); |
| 314 | |
| 315 | /* Initialize my private state */ |
| 316 | master->pass_number = 0; |
| 317 | master->using_merged_upsample = use_merged_upsample(cinfo); |
| 318 | |
| 319 | /* Color quantizer selection */ |
| 320 | master->quantizer_1pass = NULL; |
| 321 | master->quantizer_2pass = NULL; |
| 322 | /* No mode changes if not using buffered-image mode. */ |
| 323 | if (! cinfo->quantize_colors || ! cinfo->buffered_image) { |
| 324 | cinfo->enable_1pass_quant = FALSE; |
| 325 | cinfo->enable_external_quant = FALSE; |
| 326 | cinfo->enable_2pass_quant = FALSE; |
| 327 | } |
| 328 | if (cinfo->quantize_colors) { |
| 329 | if (cinfo->raw_data_out) |
| 330 | ERREXIT(cinfo, JERR_NOTIMPL); |
| 331 | /* 2-pass quantizer only works in 3-component color space. */ |
| 332 | if (cinfo->out_color_components != 3) { |
| 333 | cinfo->enable_1pass_quant = TRUE; |
| 334 | cinfo->enable_external_quant = FALSE; |
| 335 | cinfo->enable_2pass_quant = FALSE; |
| 336 | cinfo->colormap = NULL; |
| 337 | } else if (cinfo->colormap != NULL) { |
| 338 | cinfo->enable_external_quant = TRUE; |
| 339 | } else if (cinfo->two_pass_quantize) { |
| 340 | cinfo->enable_2pass_quant = TRUE; |
| 341 | } else { |
| 342 | cinfo->enable_1pass_quant = TRUE; |
| 343 | } |
| 344 | |
| 345 | if (cinfo->enable_1pass_quant) { |
| 346 | #ifdef QUANT_1PASS_SUPPORTED |
| 347 | jinit_1pass_quantizer(cinfo); |
| 348 | master->quantizer_1pass = cinfo->cquantize; |
| 349 | #else |
| 350 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
| 351 | #endif |
| 352 | } |
| 353 | |
| 354 | /* We use the 2-pass code to map to external colormaps. */ |
| 355 | if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) { |
| 356 | #ifdef QUANT_2PASS_SUPPORTED |
| 357 | jinit_2pass_quantizer(cinfo); |
| 358 | master->quantizer_2pass = cinfo->cquantize; |
| 359 | #else |
| 360 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
| 361 | #endif |
| 362 | } |
| 363 | /* If both quantizers are initialized, the 2-pass one is left active; |
| 364 | * this is necessary for starting with quantization to an external map. |
| 365 | */ |
| 366 | } |
| 367 | |
| 368 | /* Post-processing: in particular, color conversion first */ |
| 369 | if (! cinfo->raw_data_out) { |
| 370 | if (master->using_merged_upsample) { |
| 371 | #ifdef UPSAMPLE_MERGING_SUPPORTED |
| 372 | jinit_merged_upsampler(cinfo); /* does color conversion too */ |
| 373 | #else |
| 374 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
| 375 | #endif |
| 376 | } else { |
| 377 | jinit_color_deconverter(cinfo); |
| 378 | jinit_upsampler(cinfo); |
| 379 | } |
| 380 | jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant); |
| 381 | } |
| 382 | /* Inverse DCT */ |
| 383 | jinit_inverse_dct(cinfo); |
| 384 | /* Entropy decoding: either Huffman or arithmetic coding. */ |
| 385 | if (cinfo->arith_code) { |
| 386 | ERREXIT(cinfo, JERR_ARITH_NOTIMPL); |
| 387 | } else { |
| 388 | if (cinfo->progressive_mode) { |
| 389 | #ifdef D_PROGRESSIVE_SUPPORTED |
| 390 | jinit_phuff_decoder(cinfo); |
| 391 | #else |
| 392 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
| 393 | #endif |
| 394 | } else |
| 395 | jinit_huff_decoder(cinfo); |
| 396 | } |
| 397 | |
| 398 | /* Initialize principal buffer controllers. */ |
| 399 | use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image; |
| 400 | jinit_d_coef_controller(cinfo, use_c_buffer); |
| 401 | |
| 402 | if (! cinfo->raw_data_out) |
| 403 | jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */); |
| 404 | |
| 405 | /* We can now tell the memory manager to allocate virtual arrays. */ |
| 406 | (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo); |
| 407 | |
| 408 | /* Initialize input side of decompressor to consume first scan. */ |
| 409 | (*cinfo->inputctl->start_input_pass) (cinfo); |
| 410 | |
| 411 | #ifdef D_MULTISCAN_FILES_SUPPORTED |
| 412 | /* If jpeg_start_decompress will read the whole file, initialize |
| 413 | * progress monitoring appropriately. The input step is counted |
| 414 | * as one pass. |
| 415 | */ |
| 416 | if (cinfo->progress != NULL && ! cinfo->buffered_image && |
| 417 | cinfo->inputctl->has_multiple_scans) { |
| 418 | int nscans; |
| 419 | /* Estimate number of scans to set pass_limit. */ |
| 420 | if (cinfo->progressive_mode) { |
| 421 | /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */ |
| 422 | nscans = 2 + 3 * cinfo->num_components; |
| 423 | } else { |
| 424 | /* For a nonprogressive multiscan file, estimate 1 scan per component. */ |
| 425 | nscans = cinfo->num_components; |
| 426 | } |
| 427 | cinfo->progress->pass_counter = 0L; |
| 428 | cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans; |
| 429 | cinfo->progress->completed_passes = 0; |
| 430 | cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2); |
| 431 | /* Count the input pass as done */ |
| 432 | master->pass_number++; |
| 433 | } |
| 434 | #endif /* D_MULTISCAN_FILES_SUPPORTED */ |
| 435 | } |
| 436 | |
| 437 | |
| 438 | /* |
| 439 | * Per-pass setup. |
| 440 | * This is called at the beginning of each output pass. We determine which |
| 441 | * modules will be active during this pass and give them appropriate |
| 442 | * start_pass calls. We also set is_dummy_pass to indicate whether this |
| 443 | * is a "real" output pass or a dummy pass for color quantization. |
| 444 | * (In the latter case, jdapistd.c will crank the pass to completion.) |
| 445 | */ |
| 446 | |
| 447 | METHODDEF(void) |
| 448 | prepare_for_output_pass (j_decompress_ptr cinfo) |
| 449 | { |
| 450 | my_master_ptr master = (my_master_ptr) cinfo->master; |
| 451 | |
| 452 | if (master->pub.is_dummy_pass) { |
| 453 | #ifdef QUANT_2PASS_SUPPORTED |
| 454 | /* Final pass of 2-pass quantization */ |
| 455 | master->pub.is_dummy_pass = FALSE; |
| 456 | (*cinfo->cquantize->start_pass) (cinfo, FALSE); |
| 457 | (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST); |
| 458 | (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST); |
| 459 | #else |
| 460 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
| 461 | #endif /* QUANT_2PASS_SUPPORTED */ |
| 462 | } else { |
| 463 | if (cinfo->quantize_colors && cinfo->colormap == NULL) { |
| 464 | /* Select new quantization method */ |
| 465 | if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) { |
| 466 | cinfo->cquantize = master->quantizer_2pass; |
| 467 | master->pub.is_dummy_pass = TRUE; |
| 468 | } else if (cinfo->enable_1pass_quant) { |
| 469 | cinfo->cquantize = master->quantizer_1pass; |
| 470 | } else { |
| 471 | ERREXIT(cinfo, JERR_MODE_CHANGE); |
| 472 | } |
| 473 | } |
| 474 | (*cinfo->idct->start_pass) (cinfo); |
| 475 | (*cinfo->coef->start_output_pass) (cinfo); |
| 476 | if (! cinfo->raw_data_out) { |
| 477 | if (! master->using_merged_upsample) |
| 478 | (*cinfo->cconvert->start_pass) (cinfo); |
| 479 | (*cinfo->upsample->start_pass) (cinfo); |
| 480 | if (cinfo->quantize_colors) |
| 481 | (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass); |
| 482 | (*cinfo->post->start_pass) (cinfo, |
| 483 | (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU)); |
| 484 | (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | /* Set up progress monitor's pass info if present */ |
| 489 | if (cinfo->progress != NULL) { |
| 490 | cinfo->progress->completed_passes = master->pass_number; |
| 491 | cinfo->progress->total_passes = master->pass_number + |
| 492 | (master->pub.is_dummy_pass ? 2 : 1); |
| 493 | /* In buffered-image mode, we assume one more output pass if EOI not |
| 494 | * yet reached, but no more passes if EOI has been reached. |
| 495 | */ |
| 496 | if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) { |
| 497 | cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1); |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | |
| 503 | /* |
| 504 | * Finish up at end of an output pass. |
| 505 | */ |
| 506 | |
| 507 | METHODDEF(void) |
| 508 | finish_output_pass (j_decompress_ptr cinfo) |
| 509 | { |
| 510 | my_master_ptr master = (my_master_ptr) cinfo->master; |
| 511 | |
| 512 | if (cinfo->quantize_colors) |
| 513 | (*cinfo->cquantize->finish_pass) (cinfo); |
| 514 | master->pass_number++; |
| 515 | } |
| 516 | |
| 517 | |
| 518 | #ifdef D_MULTISCAN_FILES_SUPPORTED |
| 519 | |
| 520 | /* |
| 521 | * Switch to a new external colormap between output passes. |
| 522 | */ |
| 523 | |
| 524 | GLOBAL(void) |
| 525 | jpeg_new_colormap (j_decompress_ptr cinfo) |
| 526 | { |
| 527 | my_master_ptr master = (my_master_ptr) cinfo->master; |
| 528 | |
| 529 | /* Prevent application from calling me at wrong times */ |
| 530 | if (cinfo->global_state != DSTATE_BUFIMAGE) |
| 531 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
| 532 | |
| 533 | if (cinfo->quantize_colors && cinfo->enable_external_quant && |
| 534 | cinfo->colormap != NULL) { |
| 535 | /* Select 2-pass quantizer for external colormap use */ |
| 536 | cinfo->cquantize = master->quantizer_2pass; |
| 537 | /* Notify quantizer of colormap change */ |
| 538 | (*cinfo->cquantize->new_color_map) (cinfo); |
| 539 | master->pub.is_dummy_pass = FALSE; /* just in case */ |
| 540 | } else |
| 541 | ERREXIT(cinfo, JERR_MODE_CHANGE); |
| 542 | } |
| 543 | |
| 544 | #endif /* D_MULTISCAN_FILES_SUPPORTED */ |
| 545 | |
| 546 | |
| 547 | /* |
| 548 | * Initialize master decompression control and select active modules. |
| 549 | * This is performed at the start of jpeg_start_decompress. |
| 550 | */ |
| 551 | |
| 552 | GLOBAL(void) |
| 553 | jinit_master_decompress (j_decompress_ptr cinfo) |
| 554 | { |
| 555 | my_master_ptr master; |
| 556 | |
| 557 | master = (my_master_ptr) |
| 558 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 559 | SIZEOF(my_decomp_master)); |
| 560 | cinfo->master = (struct jpeg_decomp_master *) master; |
| 561 | master->pub.prepare_for_output_pass = prepare_for_output_pass; |
| 562 | master->pub.finish_output_pass = finish_output_pass; |
| 563 | |
| 564 | master->pub.is_dummy_pass = FALSE; |
| 565 | |
| 566 | master_selection(cinfo); |
| 567 | } |