istereo

annotate libs/libjpeg/jdmaster.c @ 26:862a3329a8f0

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