istereo2

annotate libs/libjpeg/jdmaster.c @ 28:74b50b538858

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