DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

jdmaster.cpp (22052B)


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