jdmaster.c 27.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * jdmaster.c
 *
 * This file was part of the Independent JPEG Group's software:
 * Copyright (C) 1991-1997, Thomas G. Lane.
 * Modified 2002-2009 by Guido Vollbeding.
 * libjpeg-turbo Modifications:
 * Copyright (C) 2009-2011, 2016, D. R. Commander.
 * Copyright (C) 2013, Linaro Limited.
 * Copyright (C) 2015, Google, Inc.
 * For conditions of distribution and use, see the accompanying README.ijg
 * file.
 *
 * This file contains master control logic for the JPEG decompressor.
 * These routines are concerned with selecting the modules to be executed
 * and with determining the number of passes and the work to be done in each
 * pass.
 */

#define JPEG_INTERNALS
#include "jinclude.h"
#include "jpeglib.h"
#include "jpegcomp.h"
#include "jdmaster.h"
#include "jsimd.h"


/*
 * Determine whether merged upsample/color conversion should be used.
 * CRUCIAL: this must match the actual capabilities of jdmerge.c!
 */

LOCAL(boolean)
34
use_merged_upsample(j_decompress_ptr cinfo)
35 36 37 38 39 40 41 42
{
#ifdef UPSAMPLE_MERGING_SUPPORTED
  /* Merging is the equivalent of plain box-filter upsampling */
  if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
    return FALSE;
  /* jdmerge.c only supports YCC=>RGB and YCC=>RGB565 color conversion */
  if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
      (cinfo->out_color_space != JCS_RGB &&
43 44 45 46 47 48 49 50 51 52 53
       cinfo->out_color_space != JCS_RGB565 &&
       cinfo->out_color_space != JCS_EXT_RGB &&
       cinfo->out_color_space != JCS_EXT_RGBX &&
       cinfo->out_color_space != JCS_EXT_BGR &&
       cinfo->out_color_space != JCS_EXT_BGRX &&
       cinfo->out_color_space != JCS_EXT_XBGR &&
       cinfo->out_color_space != JCS_EXT_XRGB &&
       cinfo->out_color_space != JCS_EXT_RGBA &&
       cinfo->out_color_space != JCS_EXT_BGRA &&
       cinfo->out_color_space != JCS_EXT_ABGR &&
       cinfo->out_color_space != JCS_EXT_ARGB))
54 55
    return FALSE;
  if ((cinfo->out_color_space == JCS_RGB565 &&
56
       cinfo->out_color_components != 3) ||
57
      (cinfo->out_color_space != JCS_RGB565 &&
58
       cinfo->out_color_components != rgb_pixelsize[cinfo->out_color_space]))
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    return FALSE;
  /* and it only handles 2h1v or 2h2v sampling ratios */
  if (cinfo->comp_info[0].h_samp_factor != 2 ||
      cinfo->comp_info[1].h_samp_factor != 1 ||
      cinfo->comp_info[2].h_samp_factor != 1 ||
      cinfo->comp_info[0].v_samp_factor >  2 ||
      cinfo->comp_info[1].v_samp_factor != 1 ||
      cinfo->comp_info[2].v_samp_factor != 1)
    return FALSE;
  /* furthermore, it doesn't work if we've scaled the IDCTs differently */
  if (cinfo->comp_info[0]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
      cinfo->comp_info[1]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
      cinfo->comp_info[2]._DCT_scaled_size != cinfo->_min_DCT_scaled_size)
    return FALSE;
#ifdef WITH_SIMD
  /* If YCbCr-to-RGB color conversion is SIMD-accelerated but merged upsampling
     isn't, then disabling merged upsampling is likely to be faster when
     decompressing YCbCr JPEG images. */
  if (!jsimd_can_h2v2_merged_upsample() && !jsimd_can_h2v1_merged_upsample() &&
      jsimd_can_ycc_rgb() && cinfo->jpeg_color_space == JCS_YCbCr &&
      (cinfo->out_color_space == JCS_RGB ||
       (cinfo->out_color_space >= JCS_EXT_RGB &&
        cinfo->out_color_space <= JCS_EXT_ARGB)))
    return FALSE;
#endif
  /* ??? also need to test for upsample-time rescaling, when & if supported */
  return TRUE;                  /* by golly, it'll work... */
#else
  return FALSE;
#endif
}


/*
 * Compute output image dimensions and related values.
 * NOTE: this is exported for possible use by application.
 * Hence it mustn't do anything that can't be done twice.
 */

#if JPEG_LIB_VERSION >= 80
GLOBAL(void)
#else
LOCAL(void)
#endif
103
jpeg_core_output_dimensions(j_decompress_ptr cinfo)
104 105 106 107 108 109 110 111 112 113 114 115
/* Do computations that are needed before master selection phase.
 * This function is used for transcoding and full decompression.
 */
{
#ifdef IDCT_SCALING_SUPPORTED
  int ci;
  jpeg_component_info *compptr;

  /* Compute actual output image dimensions and DCT scaling choices. */
  if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom) {
    /* Provide 1/block_size scaling */
    cinfo->output_width = (JDIMENSION)
116
      jdiv_round_up((long)cinfo->image_width, (long)DCTSIZE);
117
    cinfo->output_height = (JDIMENSION)
118
      jdiv_round_up((long)cinfo->image_height, (long)DCTSIZE);
119 120 121 122 123
    cinfo->_min_DCT_h_scaled_size = 1;
    cinfo->_min_DCT_v_scaled_size = 1;
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 2) {
    /* Provide 2/block_size scaling */
    cinfo->output_width = (JDIMENSION)
124
      jdiv_round_up((long)cinfo->image_width * 2L, (long)DCTSIZE);
125
    cinfo->output_height = (JDIMENSION)
126
      jdiv_round_up((long)cinfo->image_height * 2L, (long)DCTSIZE);
127 128 129 130 131
    cinfo->_min_DCT_h_scaled_size = 2;
    cinfo->_min_DCT_v_scaled_size = 2;
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 3) {
    /* Provide 3/block_size scaling */
    cinfo->output_width = (JDIMENSION)
132
      jdiv_round_up((long)cinfo->image_width * 3L, (long)DCTSIZE);
133
    cinfo->output_height = (JDIMENSION)
134
      jdiv_round_up((long)cinfo->image_height * 3L, (long)DCTSIZE);
135 136 137 138 139
    cinfo->_min_DCT_h_scaled_size = 3;
    cinfo->_min_DCT_v_scaled_size = 3;
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 4) {
    /* Provide 4/block_size scaling */
    cinfo->output_width = (JDIMENSION)
140
      jdiv_round_up((long)cinfo->image_width * 4L, (long)DCTSIZE);
141
    cinfo->output_height = (JDIMENSION)
142
      jdiv_round_up((long)cinfo->image_height * 4L, (long)DCTSIZE);
143 144 145 146 147
    cinfo->_min_DCT_h_scaled_size = 4;
    cinfo->_min_DCT_v_scaled_size = 4;
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 5) {
    /* Provide 5/block_size scaling */
    cinfo->output_width = (JDIMENSION)
148
      jdiv_round_up((long)cinfo->image_width * 5L, (long)DCTSIZE);
149
    cinfo->output_height = (JDIMENSION)
150
      jdiv_round_up((long)cinfo->image_height * 5L, (long)DCTSIZE);
151 152 153 154 155
    cinfo->_min_DCT_h_scaled_size = 5;
    cinfo->_min_DCT_v_scaled_size = 5;
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 6) {
    /* Provide 6/block_size scaling */
    cinfo->output_width = (JDIMENSION)
156
      jdiv_round_up((long)cinfo->image_width * 6L, (long)DCTSIZE);
157
    cinfo->output_height = (JDIMENSION)
158
      jdiv_round_up((long)cinfo->image_height * 6L, (long)DCTSIZE);
159 160 161 162 163
    cinfo->_min_DCT_h_scaled_size = 6;
    cinfo->_min_DCT_v_scaled_size = 6;
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 7) {
    /* Provide 7/block_size scaling */
    cinfo->output_width = (JDIMENSION)
164
      jdiv_round_up((long)cinfo->image_width * 7L, (long)DCTSIZE);
165
    cinfo->output_height = (JDIMENSION)
166
      jdiv_round_up((long)cinfo->image_height * 7L, (long)DCTSIZE);
167 168 169 170 171
    cinfo->_min_DCT_h_scaled_size = 7;
    cinfo->_min_DCT_v_scaled_size = 7;
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 8) {
    /* Provide 8/block_size scaling */
    cinfo->output_width = (JDIMENSION)
172
      jdiv_round_up((long)cinfo->image_width * 8L, (long)DCTSIZE);
173
    cinfo->output_height = (JDIMENSION)
174
      jdiv_round_up((long)cinfo->image_height * 8L, (long)DCTSIZE);
175 176 177 178 179
    cinfo->_min_DCT_h_scaled_size = 8;
    cinfo->_min_DCT_v_scaled_size = 8;
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 9) {
    /* Provide 9/block_size scaling */
    cinfo->output_width = (JDIMENSION)
180
      jdiv_round_up((long)cinfo->image_width * 9L, (long)DCTSIZE);
181
    cinfo->output_height = (JDIMENSION)
182
      jdiv_round_up((long)cinfo->image_height * 9L, (long)DCTSIZE);
183 184 185 186 187
    cinfo->_min_DCT_h_scaled_size = 9;
    cinfo->_min_DCT_v_scaled_size = 9;
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 10) {
    /* Provide 10/block_size scaling */
    cinfo->output_width = (JDIMENSION)
188
      jdiv_round_up((long)cinfo->image_width * 10L, (long)DCTSIZE);
189
    cinfo->output_height = (JDIMENSION)
190
      jdiv_round_up((long)cinfo->image_height * 10L, (long)DCTSIZE);
191 192 193 194 195
    cinfo->_min_DCT_h_scaled_size = 10;
    cinfo->_min_DCT_v_scaled_size = 10;
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 11) {
    /* Provide 11/block_size scaling */
    cinfo->output_width = (JDIMENSION)
196
      jdiv_round_up((long)cinfo->image_width * 11L, (long)DCTSIZE);
197
    cinfo->output_height = (JDIMENSION)
198
      jdiv_round_up((long)cinfo->image_height * 11L, (long)DCTSIZE);
199 200 201 202 203
    cinfo->_min_DCT_h_scaled_size = 11;
    cinfo->_min_DCT_v_scaled_size = 11;
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 12) {
    /* Provide 12/block_size scaling */
    cinfo->output_width = (JDIMENSION)
204
      jdiv_round_up((long)cinfo->image_width * 12L, (long)DCTSIZE);
205
    cinfo->output_height = (JDIMENSION)
206
      jdiv_round_up((long)cinfo->image_height * 12L, (long)DCTSIZE);
207 208 209 210 211
    cinfo->_min_DCT_h_scaled_size = 12;
    cinfo->_min_DCT_v_scaled_size = 12;
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 13) {
    /* Provide 13/block_size scaling */
    cinfo->output_width = (JDIMENSION)
212
      jdiv_round_up((long)cinfo->image_width * 13L, (long)DCTSIZE);
213
    cinfo->output_height = (JDIMENSION)
214
      jdiv_round_up((long)cinfo->image_height * 13L, (long)DCTSIZE);
215 216 217 218 219
    cinfo->_min_DCT_h_scaled_size = 13;
    cinfo->_min_DCT_v_scaled_size = 13;
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 14) {
    /* Provide 14/block_size scaling */
    cinfo->output_width = (JDIMENSION)
220
      jdiv_round_up((long)cinfo->image_width * 14L, (long)DCTSIZE);
221
    cinfo->output_height = (JDIMENSION)
222
      jdiv_round_up((long)cinfo->image_height * 14L, (long)DCTSIZE);
223 224 225 226 227
    cinfo->_min_DCT_h_scaled_size = 14;
    cinfo->_min_DCT_v_scaled_size = 14;
  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 15) {
    /* Provide 15/block_size scaling */
    cinfo->output_width = (JDIMENSION)
228
      jdiv_round_up((long)cinfo->image_width * 15L, (long)DCTSIZE);
229
    cinfo->output_height = (JDIMENSION)
230
      jdiv_round_up((long)cinfo->image_height * 15L, (long)DCTSIZE);
231 232 233 234 235
    cinfo->_min_DCT_h_scaled_size = 15;
    cinfo->_min_DCT_v_scaled_size = 15;
  } else {
    /* Provide 16/block_size scaling */
    cinfo->output_width = (JDIMENSION)
236
      jdiv_round_up((long)cinfo->image_width * 16L, (long)DCTSIZE);
237
    cinfo->output_height = (JDIMENSION)
238
      jdiv_round_up((long)cinfo->image_height * 16L, (long)DCTSIZE);
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    cinfo->_min_DCT_h_scaled_size = 16;
    cinfo->_min_DCT_v_scaled_size = 16;
  }

  /* Recompute dimensions of components */
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
       ci++, compptr++) {
    compptr->_DCT_h_scaled_size = cinfo->_min_DCT_h_scaled_size;
    compptr->_DCT_v_scaled_size = cinfo->_min_DCT_v_scaled_size;
  }

#else /* !IDCT_SCALING_SUPPORTED */

  /* Hardwire it to "no scaling" */
  cinfo->output_width = cinfo->image_width;
  cinfo->output_height = cinfo->image_height;
  /* jdinput.c has already initialized DCT_scaled_size,
   * and has computed unscaled downsampled_width and downsampled_height.
   */

#endif /* IDCT_SCALING_SUPPORTED */
}


/*
 * Compute output image dimensions and related values.
 * NOTE: this is exported for possible use by application.
 * Hence it mustn't do anything that can't be done twice.
 * Also note that it may be called before the master module is initialized!
 */

GLOBAL(void)
271
jpeg_calc_output_dimensions(j_decompress_ptr cinfo)
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
/* Do computations that are needed before master selection phase */
{
#ifdef IDCT_SCALING_SUPPORTED
  int ci;
  jpeg_component_info *compptr;
#endif

  /* Prevent application from calling me at wrong times */
  if (cinfo->global_state != DSTATE_READY)
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);

  /* Compute core output image dimensions and DCT scaling choices. */
  jpeg_core_output_dimensions(cinfo);

#ifdef IDCT_SCALING_SUPPORTED

  /* In selecting the actual DCT scaling for each component, we try to
   * scale up the chroma components via IDCT scaling rather than upsampling.
   * This saves time if the upsampler gets to use 1:1 scaling.
   * Note this code adapts subsampling ratios which are powers of 2.
   */
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
       ci++, compptr++) {
    int ssize = cinfo->_min_DCT_scaled_size;
    while (ssize < DCTSIZE &&
           ((cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) %
            (compptr->h_samp_factor * ssize * 2) == 0) &&
           ((cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size) %
            (compptr->v_samp_factor * ssize * 2) == 0)) {
      ssize = ssize * 2;
    }
#if JPEG_LIB_VERSION >= 70
    compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = ssize;
#else
    compptr->DCT_scaled_size = ssize;
#endif
  }

  /* Recompute downsampled dimensions of components;
   * application needs to know these if using raw downsampled data.
   */
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
       ci++, compptr++) {
    /* Size in samples, after IDCT scaling */
    compptr->downsampled_width = (JDIMENSION)
317 318 319
      jdiv_round_up((long)cinfo->image_width *
                    (long)(compptr->h_samp_factor * compptr->_DCT_scaled_size),
                    (long)(cinfo->max_h_samp_factor * DCTSIZE));
320
    compptr->downsampled_height = (JDIMENSION)
321 322 323
      jdiv_round_up((long)cinfo->image_height *
                    (long)(compptr->v_samp_factor * compptr->_DCT_scaled_size),
                    (long)(cinfo->max_v_samp_factor * DCTSIZE));
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
  }

#else /* !IDCT_SCALING_SUPPORTED */

  /* Hardwire it to "no scaling" */
  cinfo->output_width = cinfo->image_width;
  cinfo->output_height = cinfo->image_height;
  /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
   * and has computed unscaled downsampled_width and downsampled_height.
   */

#endif /* IDCT_SCALING_SUPPORTED */

  /* Report number of components in selected colorspace. */
  /* Probably this should be in the color conversion module... */
  switch (cinfo->out_color_space) {
  case JCS_GRAYSCALE:
    cinfo->out_color_components = 1;
    break;
  case JCS_RGB:
  case JCS_EXT_RGB:
  case JCS_EXT_RGBX:
  case JCS_EXT_BGR:
  case JCS_EXT_BGRX:
  case JCS_EXT_XBGR:
  case JCS_EXT_XRGB:
  case JCS_EXT_RGBA:
  case JCS_EXT_BGRA:
  case JCS_EXT_ABGR:
  case JCS_EXT_ARGB:
    cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
    break;
  case JCS_YCbCr:
  case JCS_RGB565:
    cinfo->out_color_components = 3;
    break;
  case JCS_CMYK:
  case JCS_YCCK:
    cinfo->out_color_components = 4;
    break;
  default:                      /* else must be same colorspace as in file */
    cinfo->out_color_components = cinfo->num_components;
    break;
  }
  cinfo->output_components = (cinfo->quantize_colors ? 1 :
                              cinfo->out_color_components);

  /* See if upsampler will want to emit more than one row at a time */
  if (use_merged_upsample(cinfo))
    cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
  else
    cinfo->rec_outbuf_height = 1;
}


/*
 * Several decompression processes need to range-limit values to the range
 * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
 * due to noise introduced by quantization, roundoff error, etc.  These
 * processes are inner loops and need to be as fast as possible.  On most
 * machines, particularly CPUs with pipelines or instruction prefetch,
 * a (subscript-check-less) C table lookup
 *              x = sample_range_limit[x];
 * is faster than explicit tests
 *              if (x < 0)  x = 0;
 *              else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;
 * These processes all use a common table prepared by the routine below.
 *
 * For most steps we can mathematically guarantee that the initial value
 * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
 * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient.  But for the initial
 * limiting step (just after the IDCT), a wildly out-of-range value is
 * possible if the input data is corrupt.  To avoid any chance of indexing
 * off the end of memory and getting a bad-pointer trap, we perform the
 * post-IDCT limiting thus:
 *              x = range_limit[x & MASK];
 * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
 * samples.  Under normal circumstances this is more than enough range and
 * a correct output will be generated; with bogus input data the mask will
 * cause wraparound, and we will safely generate a bogus-but-in-range output.
 * For the post-IDCT step, we want to convert the data from signed to unsigned
 * representation by adding CENTERJSAMPLE at the same time that we limit it.
 * So the post-IDCT limiting table ends up looking like this:
 *   CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
 *   MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
 *   0          (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
 *   0,1,...,CENTERJSAMPLE-1
 * Negative inputs select values from the upper half of the table after
 * masking.
 *
 * We can save some space by overlapping the start of the post-IDCT table
 * with the simpler range limiting table.  The post-IDCT table begins at
 * sample_range_limit + CENTERJSAMPLE.
 */

LOCAL(void)
420
prepare_range_limit_table(j_decompress_ptr cinfo)
421 422 423 424 425 426
/* Allocate and fill in the sample_range_limit table */
{
  JSAMPLE *table;
  int i;

  table = (JSAMPLE *)
427 428 429
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
                (5 * (MAXJSAMPLE + 1) + CENTERJSAMPLE) * sizeof(JSAMPLE));
  table += (MAXJSAMPLE + 1);    /* allow negative subscripts of simple table */
430 431
  cinfo->sample_range_limit = table;
  /* First segment of "simple" table: limit[x] = 0 for x < 0 */
432
  MEMZERO(table - (MAXJSAMPLE + 1), (MAXJSAMPLE + 1) * sizeof(JSAMPLE));
433 434
  /* Main part of "simple" table: limit[x] = x */
  for (i = 0; i <= MAXJSAMPLE; i++)
435
    table[i] = (JSAMPLE)i;
436 437
  table += CENTERJSAMPLE;       /* Point to where post-IDCT table starts */
  /* End of simple table, rest of first half of post-IDCT table */
438
  for (i = CENTERJSAMPLE; i < 2 * (MAXJSAMPLE + 1); i++)
439 440
    table[i] = MAXJSAMPLE;
  /* Second half of post-IDCT table */
441 442 443
  MEMZERO(table + (2 * (MAXJSAMPLE + 1)),
          (2 * (MAXJSAMPLE + 1) - CENTERJSAMPLE) * sizeof(JSAMPLE));
  MEMCOPY(table + (4 * (MAXJSAMPLE + 1) - CENTERJSAMPLE),
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
          cinfo->sample_range_limit, CENTERJSAMPLE * sizeof(JSAMPLE));
}


/*
 * Master selection of decompression modules.
 * This is done once at jpeg_start_decompress time.  We determine
 * which modules will be used and give them appropriate initialization calls.
 * We also initialize the decompressor input side to begin consuming data.
 *
 * Since jpeg_read_header has finished, we know what is in the SOF
 * and (first) SOS markers.  We also have all the application parameter
 * settings.
 */

LOCAL(void)
460
master_selection(j_decompress_ptr cinfo)
461
{
462
  my_master_ptr master = (my_master_ptr)cinfo->master;
463 464 465 466 467 468 469 470 471
  boolean use_c_buffer;
  long samplesperrow;
  JDIMENSION jd_samplesperrow;

  /* Initialize dimensions and other stuff */
  jpeg_calc_output_dimensions(cinfo);
  prepare_range_limit_table(cinfo);

  /* Width of an output scanline must be representable as JDIMENSION. */
472 473 474 475
  samplesperrow = (long)cinfo->output_width *
                  (long)cinfo->out_color_components;
  jd_samplesperrow = (JDIMENSION)samplesperrow;
  if ((long)jd_samplesperrow != samplesperrow)
476 477 478 479 480 481 482 483 484 485
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);

  /* Initialize my private state */
  master->pass_number = 0;
  master->using_merged_upsample = use_merged_upsample(cinfo);

  /* Color quantizer selection */
  master->quantizer_1pass = NULL;
  master->quantizer_2pass = NULL;
  /* No mode changes if not using buffered-image mode. */
486
  if (!cinfo->quantize_colors || !cinfo->buffered_image) {
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
    cinfo->enable_1pass_quant = FALSE;
    cinfo->enable_external_quant = FALSE;
    cinfo->enable_2pass_quant = FALSE;
  }
  if (cinfo->quantize_colors) {
    if (cinfo->raw_data_out)
      ERREXIT(cinfo, JERR_NOTIMPL);
    /* 2-pass quantizer only works in 3-component color space. */
    if (cinfo->out_color_components != 3) {
      cinfo->enable_1pass_quant = TRUE;
      cinfo->enable_external_quant = FALSE;
      cinfo->enable_2pass_quant = FALSE;
      cinfo->colormap = NULL;
    } else if (cinfo->colormap != NULL) {
      cinfo->enable_external_quant = TRUE;
    } else if (cinfo->two_pass_quantize) {
      cinfo->enable_2pass_quant = TRUE;
    } else {
      cinfo->enable_1pass_quant = TRUE;
    }

    if (cinfo->enable_1pass_quant) {
#ifdef QUANT_1PASS_SUPPORTED
      jinit_1pass_quantizer(cinfo);
      master->quantizer_1pass = cinfo->cquantize;
#else
      ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
    }

    /* We use the 2-pass code to map to external colormaps. */
    if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
#ifdef QUANT_2PASS_SUPPORTED
      jinit_2pass_quantizer(cinfo);
      master->quantizer_2pass = cinfo->cquantize;
#else
      ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
    }
    /* If both quantizers are initialized, the 2-pass one is left active;
     * this is necessary for starting with quantization to an external map.
     */
  }

  /* Post-processing: in particular, color conversion first */
532
  if (!cinfo->raw_data_out) {
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
    if (master->using_merged_upsample) {
#ifdef UPSAMPLE_MERGING_SUPPORTED
      jinit_merged_upsampler(cinfo); /* does color conversion too */
#else
      ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
    } else {
      jinit_color_deconverter(cinfo);
      jinit_upsampler(cinfo);
    }
    jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
  }
  /* Inverse DCT */
  jinit_inverse_dct(cinfo);
  /* Entropy decoding: either Huffman or arithmetic coding. */
  if (cinfo->arith_code) {
#ifdef D_ARITH_CODING_SUPPORTED
    jinit_arith_decoder(cinfo);
#else
    ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
#endif
  } else {
    if (cinfo->progressive_mode) {
#ifdef D_PROGRESSIVE_SUPPORTED
      jinit_phuff_decoder(cinfo);
#else
      ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
    } else
      jinit_huff_decoder(cinfo);
  }

  /* Initialize principal buffer controllers. */
  use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
  jinit_d_coef_controller(cinfo, use_c_buffer);

569
  if (!cinfo->raw_data_out)
570 571 572
    jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);

  /* We can now tell the memory manager to allocate virtual arrays. */
573
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588

  /* Initialize input side of decompressor to consume first scan. */
  (*cinfo->inputctl->start_input_pass) (cinfo);

  /* Set the first and last iMCU columns to decompress from single-scan images.
   * By default, decompress all of the iMCU columns.
   */
  cinfo->master->first_iMCU_col = 0;
  cinfo->master->last_iMCU_col = cinfo->MCUs_per_row - 1;

#ifdef D_MULTISCAN_FILES_SUPPORTED
  /* If jpeg_start_decompress will read the whole file, initialize
   * progress monitoring appropriately.  The input step is counted
   * as one pass.
   */
589
  if (cinfo->progress != NULL && !cinfo->buffered_image &&
590 591 592 593 594 595 596 597 598 599 600
      cinfo->inputctl->has_multiple_scans) {
    int nscans;
    /* Estimate number of scans to set pass_limit. */
    if (cinfo->progressive_mode) {
      /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
      nscans = 2 + 3 * cinfo->num_components;
    } else {
      /* For a nonprogressive multiscan file, estimate 1 scan per component. */
      nscans = cinfo->num_components;
    }
    cinfo->progress->pass_counter = 0L;
601
    cinfo->progress->pass_limit = (long)cinfo->total_iMCU_rows * nscans;
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
    cinfo->progress->completed_passes = 0;
    cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
    /* Count the input pass as done */
    master->pass_number++;
  }
#endif /* D_MULTISCAN_FILES_SUPPORTED */
}


/*
 * Per-pass setup.
 * This is called at the beginning of each output pass.  We determine which
 * modules will be active during this pass and give them appropriate
 * start_pass calls.  We also set is_dummy_pass to indicate whether this
 * is a "real" output pass or a dummy pass for color quantization.
 * (In the latter case, jdapistd.c will crank the pass to completion.)
 */

METHODDEF(void)
621
prepare_for_output_pass(j_decompress_ptr cinfo)
622
{
623
  my_master_ptr master = (my_master_ptr)cinfo->master;
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648

  if (master->pub.is_dummy_pass) {
#ifdef QUANT_2PASS_SUPPORTED
    /* Final pass of 2-pass quantization */
    master->pub.is_dummy_pass = FALSE;
    (*cinfo->cquantize->start_pass) (cinfo, FALSE);
    (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
    (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
#else
    ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif /* QUANT_2PASS_SUPPORTED */
  } else {
    if (cinfo->quantize_colors && cinfo->colormap == NULL) {
      /* Select new quantization method */
      if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
        cinfo->cquantize = master->quantizer_2pass;
        master->pub.is_dummy_pass = TRUE;
      } else if (cinfo->enable_1pass_quant) {
        cinfo->cquantize = master->quantizer_1pass;
      } else {
        ERREXIT(cinfo, JERR_MODE_CHANGE);
      }
    }
    (*cinfo->idct->start_pass) (cinfo);
    (*cinfo->coef->start_output_pass) (cinfo);
649 650
    if (!cinfo->raw_data_out) {
      if (!master->using_merged_upsample)
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
        (*cinfo->cconvert->start_pass) (cinfo);
      (*cinfo->upsample->start_pass) (cinfo);
      if (cinfo->quantize_colors)
        (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
      (*cinfo->post->start_pass) (cinfo,
            (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
      (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
    }
  }

  /* Set up progress monitor's pass info if present */
  if (cinfo->progress != NULL) {
    cinfo->progress->completed_passes = master->pass_number;
    cinfo->progress->total_passes = master->pass_number +
                                    (master->pub.is_dummy_pass ? 2 : 1);
    /* In buffered-image mode, we assume one more output pass if EOI not
     * yet reached, but no more passes if EOI has been reached.
     */
669
    if (cinfo->buffered_image && !cinfo->inputctl->eoi_reached) {
670 671 672 673 674 675 676 677 678 679 680
      cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
    }
  }
}


/*
 * Finish up at end of an output pass.
 */

METHODDEF(void)
681
finish_output_pass(j_decompress_ptr cinfo)
682
{
683
  my_master_ptr master = (my_master_ptr)cinfo->master;
684 685 686 687 688 689 690 691 692 693 694 695 696 697

  if (cinfo->quantize_colors)
    (*cinfo->cquantize->finish_pass) (cinfo);
  master->pass_number++;
}


#ifdef D_MULTISCAN_FILES_SUPPORTED

/*
 * Switch to a new external colormap between output passes.
 */

GLOBAL(void)
698
jpeg_new_colormap(j_decompress_ptr cinfo)
699
{
700
  my_master_ptr master = (my_master_ptr)cinfo->master;
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725

  /* Prevent application from calling me at wrong times */
  if (cinfo->global_state != DSTATE_BUFIMAGE)
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);

  if (cinfo->quantize_colors && cinfo->enable_external_quant &&
      cinfo->colormap != NULL) {
    /* Select 2-pass quantizer for external colormap use */
    cinfo->cquantize = master->quantizer_2pass;
    /* Notify quantizer of colormap change */
    (*cinfo->cquantize->new_color_map) (cinfo);
    master->pub.is_dummy_pass = FALSE; /* just in case */
  } else
    ERREXIT(cinfo, JERR_MODE_CHANGE);
}

#endif /* D_MULTISCAN_FILES_SUPPORTED */


/*
 * Initialize master decompression control and select active modules.
 * This is performed at the start of jpeg_start_decompress.
 */

GLOBAL(void)
726
jinit_master_decompress(j_decompress_ptr cinfo)
727
{
728
  my_master_ptr master = (my_master_ptr)cinfo->master;
729 730 731 732 733 734 735 736 737

  master->pub.prepare_for_output_pass = prepare_for_output_pass;
  master->pub.finish_output_pass = finish_output_pass;

  master->pub.is_dummy_pass = FALSE;
  master->pub.jinit_upsampler_no_alloc = FALSE;

  master_selection(cinfo);
}