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