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