jdmaster.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:19k
源码类别:

浏览器

开发平台:

Unix_Linux

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