jdmaster.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 20k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. /*
  2.  * jdmaster.c
  3.  *
  4.  * Copyright (C) 1991-1996, 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.  #ifdef NIFTY
  154.   case JCS_YCC:
  155.     cinfo->out_color_components = 3;
  156.     break;
  157.   case JCS_YCCA:
  158.     cinfo->out_color_components = 4;
  159.     break;
  160.   case JCS_YCbCrA:
  161.     cinfo->out_color_components = 4;
  162.     break;
  163.   case JCS_YCbCrALegacy:
  164.     cinfo->out_color_components = 4;
  165.     break;
  166.   case JCS_RGBA:
  167.     cinfo->out_color_components = 4;
  168.     break;
  169. #endif
  170.   case JCS_RGB:
  171. #if RGB_PIXELSIZE != 3
  172.     cinfo->out_color_components = RGB_PIXELSIZE;
  173.     break;
  174. #endif /* else share code with YCbCr */
  175.   case JCS_YCbCr:
  176.     cinfo->out_color_components = 3;
  177.     break;
  178.   case JCS_CMYK:
  179.   case JCS_YCCK:
  180.     cinfo->out_color_components = 4;
  181.     break;
  182.   default: /* else must be same colorspace as in file */
  183.     cinfo->out_color_components = cinfo->num_components;
  184.     break;
  185.   }
  186.   cinfo->output_components = (cinfo->quantize_colors ? 1 :
  187.       cinfo->out_color_components);
  188.   /* See if upsampler will want to emit more than one row at a time */
  189.   if (use_merged_upsample(cinfo))
  190.     cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
  191.   else
  192.     cinfo->rec_outbuf_height = 1;
  193. }
  194. /*
  195.  * Several decompression processes need to range-limit values to the range
  196.  * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
  197.  * due to noise introduced by quantization, roundoff error, etc.  These
  198.  * processes are inner loops and need to be as fast as possible.  On most
  199.  * machines, particularly CPUs with pipelines or instruction prefetch,
  200.  * a (subscript-check-less) C table lookup
  201.  * x = sample_range_limit[x];
  202.  * is faster than explicit tests
  203.  * if (x < 0)  x = 0;
  204.  * else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;
  205.  * These processes all use a common table prepared by the routine below.
  206.  *
  207.  * For most steps we can mathematically guarantee that the initial value
  208.  * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
  209.  * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient.  But for the initial
  210.  * limiting step (just after the IDCT), a wildly out-of-range value is 
  211.  * possible if the input data is corrupt.  To avoid any chance of indexing
  212.  * off the end of memory and getting a bad-pointer trap, we perform the
  213.  * post-IDCT limiting thus:
  214.  * x = range_limit[x & MASK];
  215.  * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
  216.  * samples.  Under normal circumstances this is more than enough range and
  217.  * a correct output will be generated; with bogus input data the mask will
  218.  * cause wraparound, and we will safely generate a bogus-but-in-range output.
  219.  * For the post-IDCT step, we want to convert the data from signed to unsigned
  220.  * representation by adding CENTERJSAMPLE at the same time that we limit it.
  221.  * So the post-IDCT limiting table ends up looking like this:
  222.  *   CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
  223.  *   MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
  224.  *   0          (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
  225.  *   0,1,...,CENTERJSAMPLE-1
  226.  * Negative inputs select values from the upper half of the table after
  227.  * masking.
  228.  *
  229.  * We can save some space by overlapping the start of the post-IDCT table
  230.  * with the simpler range limiting table.  The post-IDCT table begins at
  231.  * sample_range_limit + CENTERJSAMPLE.
  232.  *
  233.  * Note that the table is allocated in near data space on PCs; it's small
  234.  * enough and used often enough to justify this.
  235.  */
  236. LOCAL(void)
  237. prepare_range_limit_table (j_decompress_ptr cinfo)
  238. /* Allocate and fill in the sample_range_limit table */
  239. {
  240.   JSAMPLE * table;
  241.   int i;
  242.   table = (JSAMPLE *)
  243.     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  244. (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
  245.   table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */
  246.   cinfo->sample_range_limit = table;
  247.   /* First segment of "simple" table: limit[x] = 0 for x < 0 */
  248.   MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
  249.   /* Main part of "simple" table: limit[x] = x */
  250.   for (i = 0; i <= MAXJSAMPLE; i++)
  251.     table[i] = (JSAMPLE) i;
  252.   table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */
  253.   /* End of simple table, rest of first half of post-IDCT table */
  254.   for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
  255.     table[i] = MAXJSAMPLE;
  256.   /* Second half of post-IDCT table */
  257.   MEMZERO(table + (2 * (MAXJSAMPLE+1)),
  258.   (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
  259.   MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
  260.   cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
  261. }
  262. /*
  263.  * Master selection of decompression modules.
  264.  * This is done once at jpeg_start_decompress time.  We determine
  265.  * which modules will be used and give them appropriate initialization calls.
  266.  * We also initialize the decompressor input side to begin consuming data.
  267.  *
  268.  * Since jpeg_read_header has finished, we know what is in the SOF
  269.  * and (first) SOS markers.  We also have all the application parameter
  270.  * settings.
  271.  */
  272. LOCAL(void)
  273. master_selection (j_decompress_ptr cinfo)
  274. {
  275.   my_master_ptr master = (my_master_ptr) cinfo->master;
  276.   boolean use_c_buffer;
  277.   long samplesperrow;
  278.   JDIMENSION jd_samplesperrow;
  279.   /* Initialize dimensions and other stuff */
  280.   jpeg_calc_output_dimensions(cinfo);
  281.   prepare_range_limit_table(cinfo);
  282.   /* Width of an output scanline must be representable as JDIMENSION. */
  283.   samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
  284.   jd_samplesperrow = (JDIMENSION) samplesperrow;
  285.   if ((long) jd_samplesperrow != samplesperrow)
  286.     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  287.   /* Initialize my private state */
  288.   master->pass_number = 0;
  289.   master->using_merged_upsample = use_merged_upsample(cinfo);
  290.   /* Color quantizer selection */
  291.   master->quantizer_1pass = NULL;
  292.   master->quantizer_2pass = NULL;
  293.   /* No mode changes if not using buffered-image mode. */
  294.   if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
  295.     cinfo->enable_1pass_quant = FALSE;
  296.     cinfo->enable_external_quant = FALSE;
  297.     cinfo->enable_2pass_quant = FALSE;
  298.   }
  299.   if (cinfo->quantize_colors) {
  300.     if (cinfo->raw_data_out)
  301.       ERREXIT(cinfo, JERR_NOTIMPL);
  302.     /* 2-pass quantizer only works in 3-component color space. */
  303.     if (cinfo->out_color_components != 3) {
  304.       cinfo->enable_1pass_quant = TRUE;
  305.       cinfo->enable_external_quant = FALSE;
  306.       cinfo->enable_2pass_quant = FALSE;
  307.       cinfo->colormap = NULL;
  308.     } else if (cinfo->colormap != NULL) {
  309.       cinfo->enable_external_quant = TRUE;
  310.     } else if (cinfo->two_pass_quantize) {
  311.       cinfo->enable_2pass_quant = TRUE;
  312.     } else {
  313.       cinfo->enable_1pass_quant = TRUE;
  314.     }
  315.     if (cinfo->enable_1pass_quant) {
  316. #ifdef QUANT_1PASS_SUPPORTED
  317.       jinit_1pass_quantizer(cinfo);
  318.       master->quantizer_1pass = cinfo->cquantize;
  319. #else
  320.       ERREXIT(cinfo, JERR_NOT_COMPILED);
  321. #endif
  322.     }
  323.     /* We use the 2-pass code to map to external colormaps. */
  324.     if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
  325. #ifdef QUANT_2PASS_SUPPORTED
  326.       jinit_2pass_quantizer(cinfo);
  327.       master->quantizer_2pass = cinfo->cquantize;
  328. #else
  329.       ERREXIT(cinfo, JERR_NOT_COMPILED);
  330. #endif
  331.     }
  332.     /* If both quantizers are initialized, the 2-pass one is left active;
  333.      * this is necessary for starting with quantization to an external map.
  334.      */
  335.   }
  336.   /* Post-processing: in particular, color conversion first */
  337.   if (! cinfo->raw_data_out) {
  338.     if (master->using_merged_upsample) {
  339. #ifdef UPSAMPLE_MERGING_SUPPORTED
  340.       jinit_merged_upsampler(cinfo); /* does color conversion too */
  341. #else
  342.       ERREXIT(cinfo, JERR_NOT_COMPILED);
  343. #endif
  344.     } else {
  345.       jinit_color_deconverter(cinfo);
  346.       jinit_upsampler(cinfo);
  347.     }
  348.     jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
  349.   }
  350.   /* Inverse DCT */
  351.   jinit_inverse_dct(cinfo);
  352.   /* Entropy decoding: either Huffman or arithmetic coding. */
  353.   if (cinfo->arith_code) {
  354.     ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  355.   } else {
  356.     if (cinfo->progressive_mode) {
  357. #ifdef D_PROGRESSIVE_SUPPORTED
  358.       jinit_phuff_decoder(cinfo);
  359. #else
  360.       ERREXIT(cinfo, JERR_NOT_COMPILED);
  361. #endif
  362.     } else
  363.       jinit_huff_decoder(cinfo);
  364.   }
  365.   /* Initialize principal buffer controllers. */
  366.   use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
  367.   jinit_d_coef_controller(cinfo, use_c_buffer);
  368.   if (! cinfo->raw_data_out)
  369.     jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
  370.   /* We can now tell the memory manager to allocate virtual arrays. */
  371.   (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
  372.   /* Initialize input side of decompressor to consume first scan. */
  373.   (*cinfo->inputctl->start_input_pass) (cinfo);
  374. #ifdef D_MULTISCAN_FILES_SUPPORTED
  375.   /* If jpeg_start_decompress will read the whole file, initialize
  376.    * progress monitoring appropriately.  The input step is counted
  377.    * as one pass.
  378.    */
  379.   if (cinfo->progress != NULL && ! cinfo->buffered_image &&
  380.       cinfo->inputctl->has_multiple_scans) {
  381.     int nscans;
  382.     /* Estimate number of scans to set pass_limit. */
  383.     if (cinfo->progressive_mode) {
  384.       /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
  385.       nscans = 2 + 3 * cinfo->num_components;
  386.     } else {
  387.       /* For a nonprogressive multiscan file, estimate 1 scan per component. */
  388.       nscans = cinfo->num_components;
  389.     }
  390.     cinfo->progress->pass_counter = 0L;
  391.     cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
  392.     cinfo->progress->completed_passes = 0;
  393.     cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
  394.     /* Count the input pass as done */
  395.     master->pass_number++;
  396.   }
  397. #endif /* D_MULTISCAN_FILES_SUPPORTED */
  398. }
  399. /*
  400.  * Per-pass setup.
  401.  * This is called at the beginning of each output pass.  We determine which
  402.  * modules will be active during this pass and give them appropriate
  403.  * start_pass calls.  We also set is_dummy_pass to indicate whether this
  404.  * is a "real" output pass or a dummy pass for color quantization.
  405.  * (In the latter case, jdapi.c will crank the pass to completion.)
  406.  */
  407. METHODDEF(void)
  408. prepare_for_output_pass (j_decompress_ptr cinfo)
  409. {
  410.   my_master_ptr master = (my_master_ptr) cinfo->master;
  411.   if (master->pub.is_dummy_pass) {
  412. #ifdef QUANT_2PASS_SUPPORTED
  413.     /* Final pass of 2-pass quantization */
  414.     master->pub.is_dummy_pass = FALSE;
  415.     (*cinfo->cquantize->start_pass) (cinfo, FALSE);
  416.     (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
  417.     (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
  418. #else
  419.     ERREXIT(cinfo, JERR_NOT_COMPILED);
  420. #endif /* QUANT_2PASS_SUPPORTED */
  421.   } else {
  422.     if (cinfo->quantize_colors && cinfo->colormap == NULL) {
  423.       /* Select new quantization method */
  424.       if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
  425. cinfo->cquantize = master->quantizer_2pass;
  426. master->pub.is_dummy_pass = TRUE;
  427.       } else if (cinfo->enable_1pass_quant) {
  428. cinfo->cquantize = master->quantizer_1pass;
  429.       } else {
  430. ERREXIT(cinfo, JERR_MODE_CHANGE);
  431.       }
  432.     }
  433.     (*cinfo->idct->start_pass) (cinfo);
  434.     (*cinfo->coef->start_output_pass) (cinfo);
  435.     if (! cinfo->raw_data_out) {
  436.       if (! master->using_merged_upsample)
  437. (*cinfo->cconvert->start_pass) (cinfo);
  438.       (*cinfo->upsample->start_pass) (cinfo);
  439.       if (cinfo->quantize_colors)
  440. (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
  441.       (*cinfo->post->start_pass) (cinfo,
  442.     (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
  443.       (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
  444.     }
  445.   }
  446.   /* Set up progress monitor's pass info if present */
  447.   if (cinfo->progress != NULL) {
  448.     cinfo->progress->completed_passes = master->pass_number;
  449.     cinfo->progress->total_passes = master->pass_number +
  450.     (master->pub.is_dummy_pass ? 2 : 1);
  451.     /* In buffered-image mode, we assume one more output pass if EOI not
  452.      * yet reached, but no more passes if EOI has been reached.
  453.      */
  454.     if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
  455.       cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
  456.     }
  457.   }
  458. }
  459. /*
  460.  * Finish up at end of an output pass.
  461.  */
  462. METHODDEF(void)
  463. finish_output_pass (j_decompress_ptr cinfo)
  464. {
  465.   my_master_ptr master = (my_master_ptr) cinfo->master;
  466.   if (cinfo->quantize_colors)
  467.     (*cinfo->cquantize->finish_pass) (cinfo);
  468.   master->pass_number++;
  469. }
  470. #ifdef D_MULTISCAN_FILES_SUPPORTED
  471. /*
  472.  * Switch to a new external colormap between output passes.
  473.  */
  474. GLOBAL(void)
  475. jpeg_new_colormap (j_decompress_ptr cinfo)
  476. {
  477.   my_master_ptr master = (my_master_ptr) cinfo->master;
  478.   /* Prevent application from calling me at wrong times */
  479.   if (cinfo->global_state != DSTATE_BUFIMAGE)
  480.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  481.   if (cinfo->quantize_colors && cinfo->enable_external_quant &&
  482.       cinfo->colormap != NULL) {
  483.     /* Select 2-pass quantizer for external colormap use */
  484.     cinfo->cquantize = master->quantizer_2pass;
  485.     /* Notify quantizer of colormap change */
  486.     (*cinfo->cquantize->new_color_map) (cinfo);
  487.     master->pub.is_dummy_pass = FALSE; /* just in case */
  488.   } else
  489.     ERREXIT(cinfo, JERR_MODE_CHANGE);
  490. }
  491. #endif /* D_MULTISCAN_FILES_SUPPORTED */
  492. /*
  493.  * Initialize master decompression control and select active modules.
  494.  * This is performed at the start of jpeg_start_decompress.
  495.  */
  496. GLOBAL(void)
  497. jinit_master_decompress (j_decompress_ptr cinfo)
  498. {
  499.   my_master_ptr master;
  500.   master = (my_master_ptr)
  501.       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  502.   SIZEOF(my_decomp_master));
  503.   cinfo->master = (struct jpeg_decomp_master *) master;
  504.   master->pub.prepare_for_output_pass = prepare_for_output_pass;
  505.   master->pub.finish_output_pass = finish_output_pass;
  506.   master->pub.is_dummy_pass = FALSE;
  507.   master_selection(cinfo);
  508. }