dbf-halloween2015

diff libs/libjpeg/jdmaster.c @ 1:c3f5c32cb210

barfed all the libraries in the source tree to make porting easier
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Nov 2015 00:36:56 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/libjpeg/jdmaster.c	Sun Nov 01 00:36:56 2015 +0200
     1.3 @@ -0,0 +1,557 @@
     1.4 +/*
     1.5 + * jdmaster.c
     1.6 + *
     1.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
     1.8 + * This file is part of the Independent JPEG Group's software.
     1.9 + * For conditions of distribution and use, see the accompanying README file.
    1.10 + *
    1.11 + * This file contains master control logic for the JPEG decompressor.
    1.12 + * These routines are concerned with selecting the modules to be executed
    1.13 + * and with determining the number of passes and the work to be done in each
    1.14 + * pass.
    1.15 + */
    1.16 +
    1.17 +#define JPEG_INTERNALS
    1.18 +#include "jinclude.h"
    1.19 +#include "jpeglib.h"
    1.20 +
    1.21 +
    1.22 +/* Private state */
    1.23 +
    1.24 +typedef struct {
    1.25 +  struct jpeg_decomp_master pub; /* public fields */
    1.26 +
    1.27 +  int pass_number;		/* # of passes completed */
    1.28 +
    1.29 +  boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
    1.30 +
    1.31 +  /* Saved references to initialized quantizer modules,
    1.32 +   * in case we need to switch modes.
    1.33 +   */
    1.34 +  struct jpeg_color_quantizer * quantizer_1pass;
    1.35 +  struct jpeg_color_quantizer * quantizer_2pass;
    1.36 +} my_decomp_master;
    1.37 +
    1.38 +typedef my_decomp_master * my_master_ptr;
    1.39 +
    1.40 +
    1.41 +/*
    1.42 + * Determine whether merged upsample/color conversion should be used.
    1.43 + * CRUCIAL: this must match the actual capabilities of jdmerge.c!
    1.44 + */
    1.45 +
    1.46 +LOCAL(boolean)
    1.47 +use_merged_upsample (j_decompress_ptr cinfo)
    1.48 +{
    1.49 +#ifdef UPSAMPLE_MERGING_SUPPORTED
    1.50 +  /* Merging is the equivalent of plain box-filter upsampling */
    1.51 +  if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
    1.52 +    return FALSE;
    1.53 +  /* jdmerge.c only supports YCC=>RGB color conversion */
    1.54 +  if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
    1.55 +      cinfo->out_color_space != JCS_RGB ||
    1.56 +      cinfo->out_color_components != RGB_PIXELSIZE)
    1.57 +    return FALSE;
    1.58 +  /* and it only handles 2h1v or 2h2v sampling ratios */
    1.59 +  if (cinfo->comp_info[0].h_samp_factor != 2 ||
    1.60 +      cinfo->comp_info[1].h_samp_factor != 1 ||
    1.61 +      cinfo->comp_info[2].h_samp_factor != 1 ||
    1.62 +      cinfo->comp_info[0].v_samp_factor >  2 ||
    1.63 +      cinfo->comp_info[1].v_samp_factor != 1 ||
    1.64 +      cinfo->comp_info[2].v_samp_factor != 1)
    1.65 +    return FALSE;
    1.66 +  /* furthermore, it doesn't work if we've scaled the IDCTs differently */
    1.67 +  if (cinfo->comp_info[0].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
    1.68 +      cinfo->comp_info[1].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
    1.69 +      cinfo->comp_info[2].DCT_scaled_size != cinfo->min_DCT_scaled_size)
    1.70 +    return FALSE;
    1.71 +  /* ??? also need to test for upsample-time rescaling, when & if supported */
    1.72 +  return TRUE;			/* by golly, it'll work... */
    1.73 +#else
    1.74 +  return FALSE;
    1.75 +#endif
    1.76 +}
    1.77 +
    1.78 +
    1.79 +/*
    1.80 + * Compute output image dimensions and related values.
    1.81 + * NOTE: this is exported for possible use by application.
    1.82 + * Hence it mustn't do anything that can't be done twice.
    1.83 + * Also note that it may be called before the master module is initialized!
    1.84 + */
    1.85 +
    1.86 +GLOBAL(void)
    1.87 +jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
    1.88 +/* Do computations that are needed before master selection phase */
    1.89 +{
    1.90 +#ifdef IDCT_SCALING_SUPPORTED
    1.91 +  int ci;
    1.92 +  jpeg_component_info *compptr;
    1.93 +#endif
    1.94 +
    1.95 +  /* Prevent application from calling me at wrong times */
    1.96 +  if (cinfo->global_state != DSTATE_READY)
    1.97 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
    1.98 +
    1.99 +#ifdef IDCT_SCALING_SUPPORTED
   1.100 +
   1.101 +  /* Compute actual output image dimensions and DCT scaling choices. */
   1.102 +  if (cinfo->scale_num * 8 <= cinfo->scale_denom) {
   1.103 +    /* Provide 1/8 scaling */
   1.104 +    cinfo->output_width = (JDIMENSION)
   1.105 +      jdiv_round_up((long) cinfo->image_width, 8L);
   1.106 +    cinfo->output_height = (JDIMENSION)
   1.107 +      jdiv_round_up((long) cinfo->image_height, 8L);
   1.108 +    cinfo->min_DCT_scaled_size = 1;
   1.109 +  } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {
   1.110 +    /* Provide 1/4 scaling */
   1.111 +    cinfo->output_width = (JDIMENSION)
   1.112 +      jdiv_round_up((long) cinfo->image_width, 4L);
   1.113 +    cinfo->output_height = (JDIMENSION)
   1.114 +      jdiv_round_up((long) cinfo->image_height, 4L);
   1.115 +    cinfo->min_DCT_scaled_size = 2;
   1.116 +  } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) {
   1.117 +    /* Provide 1/2 scaling */
   1.118 +    cinfo->output_width = (JDIMENSION)
   1.119 +      jdiv_round_up((long) cinfo->image_width, 2L);
   1.120 +    cinfo->output_height = (JDIMENSION)
   1.121 +      jdiv_round_up((long) cinfo->image_height, 2L);
   1.122 +    cinfo->min_DCT_scaled_size = 4;
   1.123 +  } else {
   1.124 +    /* Provide 1/1 scaling */
   1.125 +    cinfo->output_width = cinfo->image_width;
   1.126 +    cinfo->output_height = cinfo->image_height;
   1.127 +    cinfo->min_DCT_scaled_size = DCTSIZE;
   1.128 +  }
   1.129 +  /* In selecting the actual DCT scaling for each component, we try to
   1.130 +   * scale up the chroma components via IDCT scaling rather than upsampling.
   1.131 +   * This saves time if the upsampler gets to use 1:1 scaling.
   1.132 +   * Note this code assumes that the supported DCT scalings are powers of 2.
   1.133 +   */
   1.134 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.135 +       ci++, compptr++) {
   1.136 +    int ssize = cinfo->min_DCT_scaled_size;
   1.137 +    while (ssize < DCTSIZE &&
   1.138 +	   (compptr->h_samp_factor * ssize * 2 <=
   1.139 +	    cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) &&
   1.140 +	   (compptr->v_samp_factor * ssize * 2 <=
   1.141 +	    cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) {
   1.142 +      ssize = ssize * 2;
   1.143 +    }
   1.144 +    compptr->DCT_scaled_size = ssize;
   1.145 +  }
   1.146 +
   1.147 +  /* Recompute downsampled dimensions of components;
   1.148 +   * application needs to know these if using raw downsampled data.
   1.149 +   */
   1.150 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.151 +       ci++, compptr++) {
   1.152 +    /* Size in samples, after IDCT scaling */
   1.153 +    compptr->downsampled_width = (JDIMENSION)
   1.154 +      jdiv_round_up((long) cinfo->image_width *
   1.155 +		    (long) (compptr->h_samp_factor * compptr->DCT_scaled_size),
   1.156 +		    (long) (cinfo->max_h_samp_factor * DCTSIZE));
   1.157 +    compptr->downsampled_height = (JDIMENSION)
   1.158 +      jdiv_round_up((long) cinfo->image_height *
   1.159 +		    (long) (compptr->v_samp_factor * compptr->DCT_scaled_size),
   1.160 +		    (long) (cinfo->max_v_samp_factor * DCTSIZE));
   1.161 +  }
   1.162 +
   1.163 +#else /* !IDCT_SCALING_SUPPORTED */
   1.164 +
   1.165 +  /* Hardwire it to "no scaling" */
   1.166 +  cinfo->output_width = cinfo->image_width;
   1.167 +  cinfo->output_height = cinfo->image_height;
   1.168 +  /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
   1.169 +   * and has computed unscaled downsampled_width and downsampled_height.
   1.170 +   */
   1.171 +
   1.172 +#endif /* IDCT_SCALING_SUPPORTED */
   1.173 +
   1.174 +  /* Report number of components in selected colorspace. */
   1.175 +  /* Probably this should be in the color conversion module... */
   1.176 +  switch (cinfo->out_color_space) {
   1.177 +  case JCS_GRAYSCALE:
   1.178 +    cinfo->out_color_components = 1;
   1.179 +    break;
   1.180 +  case JCS_RGB:
   1.181 +#if RGB_PIXELSIZE != 3
   1.182 +    cinfo->out_color_components = RGB_PIXELSIZE;
   1.183 +    break;
   1.184 +#endif /* else share code with YCbCr */
   1.185 +  case JCS_YCbCr:
   1.186 +    cinfo->out_color_components = 3;
   1.187 +    break;
   1.188 +  case JCS_CMYK:
   1.189 +  case JCS_YCCK:
   1.190 +    cinfo->out_color_components = 4;
   1.191 +    break;
   1.192 +  default:			/* else must be same colorspace as in file */
   1.193 +    cinfo->out_color_components = cinfo->num_components;
   1.194 +    break;
   1.195 +  }
   1.196 +  cinfo->output_components = (cinfo->quantize_colors ? 1 :
   1.197 +			      cinfo->out_color_components);
   1.198 +
   1.199 +  /* See if upsampler will want to emit more than one row at a time */
   1.200 +  if (use_merged_upsample(cinfo))
   1.201 +    cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
   1.202 +  else
   1.203 +    cinfo->rec_outbuf_height = 1;
   1.204 +}
   1.205 +
   1.206 +
   1.207 +/*
   1.208 + * Several decompression processes need to range-limit values to the range
   1.209 + * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
   1.210 + * due to noise introduced by quantization, roundoff error, etc.  These
   1.211 + * processes are inner loops and need to be as fast as possible.  On most
   1.212 + * machines, particularly CPUs with pipelines or instruction prefetch,
   1.213 + * a (subscript-check-less) C table lookup
   1.214 + *		x = sample_range_limit[x];
   1.215 + * is faster than explicit tests
   1.216 + *		if (x < 0)  x = 0;
   1.217 + *		else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;
   1.218 + * These processes all use a common table prepared by the routine below.
   1.219 + *
   1.220 + * For most steps we can mathematically guarantee that the initial value
   1.221 + * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
   1.222 + * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient.  But for the initial
   1.223 + * limiting step (just after the IDCT), a wildly out-of-range value is 
   1.224 + * possible if the input data is corrupt.  To avoid any chance of indexing
   1.225 + * off the end of memory and getting a bad-pointer trap, we perform the
   1.226 + * post-IDCT limiting thus:
   1.227 + *		x = range_limit[x & MASK];
   1.228 + * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
   1.229 + * samples.  Under normal circumstances this is more than enough range and
   1.230 + * a correct output will be generated; with bogus input data the mask will
   1.231 + * cause wraparound, and we will safely generate a bogus-but-in-range output.
   1.232 + * For the post-IDCT step, we want to convert the data from signed to unsigned
   1.233 + * representation by adding CENTERJSAMPLE at the same time that we limit it.
   1.234 + * So the post-IDCT limiting table ends up looking like this:
   1.235 + *   CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
   1.236 + *   MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
   1.237 + *   0          (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
   1.238 + *   0,1,...,CENTERJSAMPLE-1
   1.239 + * Negative inputs select values from the upper half of the table after
   1.240 + * masking.
   1.241 + *
   1.242 + * We can save some space by overlapping the start of the post-IDCT table
   1.243 + * with the simpler range limiting table.  The post-IDCT table begins at
   1.244 + * sample_range_limit + CENTERJSAMPLE.
   1.245 + *
   1.246 + * Note that the table is allocated in near data space on PCs; it's small
   1.247 + * enough and used often enough to justify this.
   1.248 + */
   1.249 +
   1.250 +LOCAL(void)
   1.251 +prepare_range_limit_table (j_decompress_ptr cinfo)
   1.252 +/* Allocate and fill in the sample_range_limit table */
   1.253 +{
   1.254 +  JSAMPLE * table;
   1.255 +  int i;
   1.256 +
   1.257 +  table = (JSAMPLE *)
   1.258 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.259 +		(5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
   1.260 +  table += (MAXJSAMPLE+1);	/* allow negative subscripts of simple table */
   1.261 +  cinfo->sample_range_limit = table;
   1.262 +  /* First segment of "simple" table: limit[x] = 0 for x < 0 */
   1.263 +  MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
   1.264 +  /* Main part of "simple" table: limit[x] = x */
   1.265 +  for (i = 0; i <= MAXJSAMPLE; i++)
   1.266 +    table[i] = (JSAMPLE) i;
   1.267 +  table += CENTERJSAMPLE;	/* Point to where post-IDCT table starts */
   1.268 +  /* End of simple table, rest of first half of post-IDCT table */
   1.269 +  for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
   1.270 +    table[i] = MAXJSAMPLE;
   1.271 +  /* Second half of post-IDCT table */
   1.272 +  MEMZERO(table + (2 * (MAXJSAMPLE+1)),
   1.273 +	  (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
   1.274 +  MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
   1.275 +	  cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
   1.276 +}
   1.277 +
   1.278 +
   1.279 +/*
   1.280 + * Master selection of decompression modules.
   1.281 + * This is done once at jpeg_start_decompress time.  We determine
   1.282 + * which modules will be used and give them appropriate initialization calls.
   1.283 + * We also initialize the decompressor input side to begin consuming data.
   1.284 + *
   1.285 + * Since jpeg_read_header has finished, we know what is in the SOF
   1.286 + * and (first) SOS markers.  We also have all the application parameter
   1.287 + * settings.
   1.288 + */
   1.289 +
   1.290 +LOCAL(void)
   1.291 +master_selection (j_decompress_ptr cinfo)
   1.292 +{
   1.293 +  my_master_ptr master = (my_master_ptr) cinfo->master;
   1.294 +  boolean use_c_buffer;
   1.295 +  long samplesperrow;
   1.296 +  JDIMENSION jd_samplesperrow;
   1.297 +
   1.298 +  /* Initialize dimensions and other stuff */
   1.299 +  jpeg_calc_output_dimensions(cinfo);
   1.300 +  prepare_range_limit_table(cinfo);
   1.301 +
   1.302 +  /* Width of an output scanline must be representable as JDIMENSION. */
   1.303 +  samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
   1.304 +  jd_samplesperrow = (JDIMENSION) samplesperrow;
   1.305 +  if ((long) jd_samplesperrow != samplesperrow)
   1.306 +    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
   1.307 +
   1.308 +  /* Initialize my private state */
   1.309 +  master->pass_number = 0;
   1.310 +  master->using_merged_upsample = use_merged_upsample(cinfo);
   1.311 +
   1.312 +  /* Color quantizer selection */
   1.313 +  master->quantizer_1pass = NULL;
   1.314 +  master->quantizer_2pass = NULL;
   1.315 +  /* No mode changes if not using buffered-image mode. */
   1.316 +  if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
   1.317 +    cinfo->enable_1pass_quant = FALSE;
   1.318 +    cinfo->enable_external_quant = FALSE;
   1.319 +    cinfo->enable_2pass_quant = FALSE;
   1.320 +  }
   1.321 +  if (cinfo->quantize_colors) {
   1.322 +    if (cinfo->raw_data_out)
   1.323 +      ERREXIT(cinfo, JERR_NOTIMPL);
   1.324 +    /* 2-pass quantizer only works in 3-component color space. */
   1.325 +    if (cinfo->out_color_components != 3) {
   1.326 +      cinfo->enable_1pass_quant = TRUE;
   1.327 +      cinfo->enable_external_quant = FALSE;
   1.328 +      cinfo->enable_2pass_quant = FALSE;
   1.329 +      cinfo->colormap = NULL;
   1.330 +    } else if (cinfo->colormap != NULL) {
   1.331 +      cinfo->enable_external_quant = TRUE;
   1.332 +    } else if (cinfo->two_pass_quantize) {
   1.333 +      cinfo->enable_2pass_quant = TRUE;
   1.334 +    } else {
   1.335 +      cinfo->enable_1pass_quant = TRUE;
   1.336 +    }
   1.337 +
   1.338 +    if (cinfo->enable_1pass_quant) {
   1.339 +#ifdef QUANT_1PASS_SUPPORTED
   1.340 +      jinit_1pass_quantizer(cinfo);
   1.341 +      master->quantizer_1pass = cinfo->cquantize;
   1.342 +#else
   1.343 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
   1.344 +#endif
   1.345 +    }
   1.346 +
   1.347 +    /* We use the 2-pass code to map to external colormaps. */
   1.348 +    if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
   1.349 +#ifdef QUANT_2PASS_SUPPORTED
   1.350 +      jinit_2pass_quantizer(cinfo);
   1.351 +      master->quantizer_2pass = cinfo->cquantize;
   1.352 +#else
   1.353 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
   1.354 +#endif
   1.355 +    }
   1.356 +    /* If both quantizers are initialized, the 2-pass one is left active;
   1.357 +     * this is necessary for starting with quantization to an external map.
   1.358 +     */
   1.359 +  }
   1.360 +
   1.361 +  /* Post-processing: in particular, color conversion first */
   1.362 +  if (! cinfo->raw_data_out) {
   1.363 +    if (master->using_merged_upsample) {
   1.364 +#ifdef UPSAMPLE_MERGING_SUPPORTED
   1.365 +      jinit_merged_upsampler(cinfo); /* does color conversion too */
   1.366 +#else
   1.367 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
   1.368 +#endif
   1.369 +    } else {
   1.370 +      jinit_color_deconverter(cinfo);
   1.371 +      jinit_upsampler(cinfo);
   1.372 +    }
   1.373 +    jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
   1.374 +  }
   1.375 +  /* Inverse DCT */
   1.376 +  jinit_inverse_dct(cinfo);
   1.377 +  /* Entropy decoding: either Huffman or arithmetic coding. */
   1.378 +  if (cinfo->arith_code) {
   1.379 +    ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
   1.380 +  } else {
   1.381 +    if (cinfo->progressive_mode) {
   1.382 +#ifdef D_PROGRESSIVE_SUPPORTED
   1.383 +      jinit_phuff_decoder(cinfo);
   1.384 +#else
   1.385 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
   1.386 +#endif
   1.387 +    } else
   1.388 +      jinit_huff_decoder(cinfo);
   1.389 +  }
   1.390 +
   1.391 +  /* Initialize principal buffer controllers. */
   1.392 +  use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
   1.393 +  jinit_d_coef_controller(cinfo, use_c_buffer);
   1.394 +
   1.395 +  if (! cinfo->raw_data_out)
   1.396 +    jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
   1.397 +
   1.398 +  /* We can now tell the memory manager to allocate virtual arrays. */
   1.399 +  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
   1.400 +
   1.401 +  /* Initialize input side of decompressor to consume first scan. */
   1.402 +  (*cinfo->inputctl->start_input_pass) (cinfo);
   1.403 +
   1.404 +#ifdef D_MULTISCAN_FILES_SUPPORTED
   1.405 +  /* If jpeg_start_decompress will read the whole file, initialize
   1.406 +   * progress monitoring appropriately.  The input step is counted
   1.407 +   * as one pass.
   1.408 +   */
   1.409 +  if (cinfo->progress != NULL && ! cinfo->buffered_image &&
   1.410 +      cinfo->inputctl->has_multiple_scans) {
   1.411 +    int nscans;
   1.412 +    /* Estimate number of scans to set pass_limit. */
   1.413 +    if (cinfo->progressive_mode) {
   1.414 +      /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
   1.415 +      nscans = 2 + 3 * cinfo->num_components;
   1.416 +    } else {
   1.417 +      /* For a nonprogressive multiscan file, estimate 1 scan per component. */
   1.418 +      nscans = cinfo->num_components;
   1.419 +    }
   1.420 +    cinfo->progress->pass_counter = 0L;
   1.421 +    cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
   1.422 +    cinfo->progress->completed_passes = 0;
   1.423 +    cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
   1.424 +    /* Count the input pass as done */
   1.425 +    master->pass_number++;
   1.426 +  }
   1.427 +#endif /* D_MULTISCAN_FILES_SUPPORTED */
   1.428 +}
   1.429 +
   1.430 +
   1.431 +/*
   1.432 + * Per-pass setup.
   1.433 + * This is called at the beginning of each output pass.  We determine which
   1.434 + * modules will be active during this pass and give them appropriate
   1.435 + * start_pass calls.  We also set is_dummy_pass to indicate whether this
   1.436 + * is a "real" output pass or a dummy pass for color quantization.
   1.437 + * (In the latter case, jdapistd.c will crank the pass to completion.)
   1.438 + */
   1.439 +
   1.440 +METHODDEF(void)
   1.441 +prepare_for_output_pass (j_decompress_ptr cinfo)
   1.442 +{
   1.443 +  my_master_ptr master = (my_master_ptr) cinfo->master;
   1.444 +
   1.445 +  if (master->pub.is_dummy_pass) {
   1.446 +#ifdef QUANT_2PASS_SUPPORTED
   1.447 +    /* Final pass of 2-pass quantization */
   1.448 +    master->pub.is_dummy_pass = FALSE;
   1.449 +    (*cinfo->cquantize->start_pass) (cinfo, FALSE);
   1.450 +    (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
   1.451 +    (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
   1.452 +#else
   1.453 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
   1.454 +#endif /* QUANT_2PASS_SUPPORTED */
   1.455 +  } else {
   1.456 +    if (cinfo->quantize_colors && cinfo->colormap == NULL) {
   1.457 +      /* Select new quantization method */
   1.458 +      if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
   1.459 +	cinfo->cquantize = master->quantizer_2pass;
   1.460 +	master->pub.is_dummy_pass = TRUE;
   1.461 +      } else if (cinfo->enable_1pass_quant) {
   1.462 +	cinfo->cquantize = master->quantizer_1pass;
   1.463 +      } else {
   1.464 +	ERREXIT(cinfo, JERR_MODE_CHANGE);
   1.465 +      }
   1.466 +    }
   1.467 +    (*cinfo->idct->start_pass) (cinfo);
   1.468 +    (*cinfo->coef->start_output_pass) (cinfo);
   1.469 +    if (! cinfo->raw_data_out) {
   1.470 +      if (! master->using_merged_upsample)
   1.471 +	(*cinfo->cconvert->start_pass) (cinfo);
   1.472 +      (*cinfo->upsample->start_pass) (cinfo);
   1.473 +      if (cinfo->quantize_colors)
   1.474 +	(*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
   1.475 +      (*cinfo->post->start_pass) (cinfo,
   1.476 +	    (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
   1.477 +      (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
   1.478 +    }
   1.479 +  }
   1.480 +
   1.481 +  /* Set up progress monitor's pass info if present */
   1.482 +  if (cinfo->progress != NULL) {
   1.483 +    cinfo->progress->completed_passes = master->pass_number;
   1.484 +    cinfo->progress->total_passes = master->pass_number +
   1.485 +				    (master->pub.is_dummy_pass ? 2 : 1);
   1.486 +    /* In buffered-image mode, we assume one more output pass if EOI not
   1.487 +     * yet reached, but no more passes if EOI has been reached.
   1.488 +     */
   1.489 +    if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
   1.490 +      cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
   1.491 +    }
   1.492 +  }
   1.493 +}
   1.494 +
   1.495 +
   1.496 +/*
   1.497 + * Finish up at end of an output pass.
   1.498 + */
   1.499 +
   1.500 +METHODDEF(void)
   1.501 +finish_output_pass (j_decompress_ptr cinfo)
   1.502 +{
   1.503 +  my_master_ptr master = (my_master_ptr) cinfo->master;
   1.504 +
   1.505 +  if (cinfo->quantize_colors)
   1.506 +    (*cinfo->cquantize->finish_pass) (cinfo);
   1.507 +  master->pass_number++;
   1.508 +}
   1.509 +
   1.510 +
   1.511 +#ifdef D_MULTISCAN_FILES_SUPPORTED
   1.512 +
   1.513 +/*
   1.514 + * Switch to a new external colormap between output passes.
   1.515 + */
   1.516 +
   1.517 +GLOBAL(void)
   1.518 +jpeg_new_colormap (j_decompress_ptr cinfo)
   1.519 +{
   1.520 +  my_master_ptr master = (my_master_ptr) cinfo->master;
   1.521 +
   1.522 +  /* Prevent application from calling me at wrong times */
   1.523 +  if (cinfo->global_state != DSTATE_BUFIMAGE)
   1.524 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   1.525 +
   1.526 +  if (cinfo->quantize_colors && cinfo->enable_external_quant &&
   1.527 +      cinfo->colormap != NULL) {
   1.528 +    /* Select 2-pass quantizer for external colormap use */
   1.529 +    cinfo->cquantize = master->quantizer_2pass;
   1.530 +    /* Notify quantizer of colormap change */
   1.531 +    (*cinfo->cquantize->new_color_map) (cinfo);
   1.532 +    master->pub.is_dummy_pass = FALSE; /* just in case */
   1.533 +  } else
   1.534 +    ERREXIT(cinfo, JERR_MODE_CHANGE);
   1.535 +}
   1.536 +
   1.537 +#endif /* D_MULTISCAN_FILES_SUPPORTED */
   1.538 +
   1.539 +
   1.540 +/*
   1.541 + * Initialize master decompression control and select active modules.
   1.542 + * This is performed at the start of jpeg_start_decompress.
   1.543 + */
   1.544 +
   1.545 +GLOBAL(void)
   1.546 +jinit_master_decompress (j_decompress_ptr cinfo)
   1.547 +{
   1.548 +  my_master_ptr master;
   1.549 +
   1.550 +  master = (my_master_ptr)
   1.551 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.552 +				  SIZEOF(my_decomp_master));
   1.553 +  cinfo->master = (struct jpeg_decomp_master *) master;
   1.554 +  master->pub.prepare_for_output_pass = prepare_for_output_pass;
   1.555 +  master->pub.finish_output_pass = finish_output_pass;
   1.556 +
   1.557 +  master->pub.is_dummy_pass = FALSE;
   1.558 +
   1.559 +  master_selection(cinfo);
   1.560 +}