JCMASTER.c
上传用户:qiutianh
上传日期:2022-08-08
资源大小:939k
文件大小:20k
源码类别:

图形图象

开发平台:

Visual C++

  1. ////////////////////////////////////////////////////////////////////////
  2. //
  3. // Note : this file is included as part of the Smaller Animals Software
  4. // JpegFile package. Though this file has not been modified from it's 
  5. // original IJG 6a form, it is not the responsibility on the Independent
  6. // JPEG Group to answer questions regarding this code.
  7. //
  8. // Any questions you have about this code should be addressed to :
  9. //
  10. // CHRISDL@PAGESZ.NET - the distributor of this package.
  11. //
  12. // Remember, by including this code in the JpegFile package, Smaller 
  13. // Animals Software assumes all responsibilities for answering questions
  14. // about it. If we (SA Software) can't answer your questions ourselves, we 
  15. // will direct you to people who can.
  16. //
  17. // Thanks, CDL.
  18. //
  19. ////////////////////////////////////////////////////////////////////////
  20. /*
  21.  * jcmaster.c
  22.  *
  23.  * Copyright (C) 1991-1996, Thomas G. Lane.
  24.  * This file is part of the Independent JPEG Group's software.
  25.  * For conditions of distribution and use, see the accompanying README file.
  26.  *
  27.  * This file contains master control logic for the JPEG compressor.
  28.  * These routines are concerned with parameter validation, initial setup,
  29.  * and inter-pass control (determining the number of passes and the work 
  30.  * to be done in each pass).
  31.  */
  32. #define JPEG_INTERNALS
  33. #include "jinclude.h"
  34. #include "jpeglib.h"
  35. /* Private state */
  36. typedef enum {
  37. main_pass, /* input data, also do first output step */
  38. huff_opt_pass, /* Huffman code optimization pass */
  39. output_pass /* data output pass */
  40. } c_pass_type;
  41. typedef struct {
  42.   struct jpeg_comp_master pub; /* public fields */
  43.   c_pass_type pass_type; /* the type of the current pass */
  44.   int pass_number; /* # of passes completed */
  45.   int total_passes; /* total # of passes needed */
  46.   int scan_number; /* current index in scan_info[] */
  47. } my_comp_master;
  48. typedef my_comp_master * my_master_ptr;
  49. /*
  50.  * Support routines that do various essential calculations.
  51.  */
  52. LOCAL(void)
  53. initial_setup (j_compress_ptr cinfo)
  54. /* Do computations that are needed before master selection phase */
  55. {
  56.   int ci;
  57.   jpeg_component_info *compptr;
  58.   long samplesperrow;
  59.   JDIMENSION jd_samplesperrow;
  60.   /* Sanity check on image dimensions */
  61.   if (cinfo->image_height <= 0 || cinfo->image_width <= 0
  62.       || cinfo->num_components <= 0 || cinfo->input_components <= 0)
  63.     ERREXIT(cinfo, JERR_EMPTY_IMAGE);
  64.   /* Make sure image isn't bigger than I can handle */
  65.   if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
  66.       (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
  67.     ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
  68.   /* Width of an input scanline must be representable as JDIMENSION. */
  69.   samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
  70.   jd_samplesperrow = (JDIMENSION) samplesperrow;
  71.   if ((long) jd_samplesperrow != samplesperrow)
  72.     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  73.   /* For now, precision must match compiled-in value... */
  74.   if (cinfo->data_precision != BITS_IN_JSAMPLE)
  75.     ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
  76.   /* Check that number of components won't exceed internal array sizes */
  77.   if (cinfo->num_components > MAX_COMPONENTS)
  78.     ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  79.      MAX_COMPONENTS);
  80.   /* Compute maximum sampling factors; check factor validity */
  81.   cinfo->max_h_samp_factor = 1;
  82.   cinfo->max_v_samp_factor = 1;
  83.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  84.        ci++, compptr++) {
  85.     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
  86. compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
  87.       ERREXIT(cinfo, JERR_BAD_SAMPLING);
  88.     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
  89.    compptr->h_samp_factor);
  90.     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
  91.    compptr->v_samp_factor);
  92.   }
  93.   /* Compute dimensions of components */
  94.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  95.        ci++, compptr++) {
  96.     /* Fill in the correct component_index value; don't rely on application */
  97.     compptr->component_index = ci;
  98.     /* For compression, we never do DCT scaling. */
  99.     compptr->DCT_scaled_size = DCTSIZE;
  100.     /* Size in DCT blocks */
  101.     compptr->width_in_blocks = (JDIMENSION)
  102.       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  103.     (long) (cinfo->max_h_samp_factor * DCTSIZE));
  104.     compptr->height_in_blocks = (JDIMENSION)
  105.       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  106.     (long) (cinfo->max_v_samp_factor * DCTSIZE));
  107.     /* Size in samples */
  108.     compptr->downsampled_width = (JDIMENSION)
  109.       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  110.     (long) cinfo->max_h_samp_factor);
  111.     compptr->downsampled_height = (JDIMENSION)
  112.       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  113.     (long) cinfo->max_v_samp_factor);
  114.     /* Mark component needed (this flag isn't actually used for compression) */
  115.     compptr->component_needed = TRUE;
  116.   }
  117.   /* Compute number of fully interleaved MCU rows (number of times that
  118.    * main controller will call coefficient controller).
  119.    */
  120.   cinfo->total_iMCU_rows = (JDIMENSION)
  121.     jdiv_round_up((long) cinfo->image_height,
  122.   (long) (cinfo->max_v_samp_factor*DCTSIZE));
  123. }
  124. #ifdef C_MULTISCAN_FILES_SUPPORTED
  125. LOCAL(void)
  126. validate_script (j_compress_ptr cinfo)
  127. /* Verify that the scan script in cinfo->scan_info[] is valid; also
  128.  * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
  129.  */
  130. {
  131.   const jpeg_scan_info * scanptr;
  132.   int scanno, ncomps, ci, coefi, thisi;
  133.   int Ss, Se, Ah, Al;
  134.   boolean component_sent[MAX_COMPONENTS];
  135. #ifdef C_PROGRESSIVE_SUPPORTED
  136.   int * last_bitpos_ptr;
  137.   int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
  138.   /* -1 until that coefficient has been seen; then last Al for it */
  139. #endif
  140.   if (cinfo->num_scans <= 0)
  141.     ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
  142.   /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
  143.    * for progressive JPEG, no scan can have this.
  144.    */
  145.   scanptr = cinfo->scan_info;
  146.   if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
  147. #ifdef C_PROGRESSIVE_SUPPORTED
  148.     cinfo->progressive_mode = TRUE;
  149.     last_bitpos_ptr = & last_bitpos[0][0];
  150.     for (ci = 0; ci < cinfo->num_components; ci++) 
  151.       for (coefi = 0; coefi < DCTSIZE2; coefi++)
  152. *last_bitpos_ptr++ = -1;
  153. #else
  154.     ERREXIT(cinfo, JERR_NOT_COMPILED);
  155. #endif
  156.   } else {
  157.     cinfo->progressive_mode = FALSE;
  158.     for (ci = 0; ci < cinfo->num_components; ci++) 
  159.       component_sent[ci] = FALSE;
  160.   }
  161.   for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
  162.     /* Validate component indexes */
  163.     ncomps = scanptr->comps_in_scan;
  164.     if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
  165.       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
  166.     for (ci = 0; ci < ncomps; ci++) {
  167.       thisi = scanptr->component_index[ci];
  168.       if (thisi < 0 || thisi >= cinfo->num_components)
  169. ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  170.       /* Components must appear in SOF order within each scan */
  171.       if (ci > 0 && thisi <= scanptr->component_index[ci-1])
  172. ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  173.     }
  174.     /* Validate progression parameters */
  175.     Ss = scanptr->Ss;
  176.     Se = scanptr->Se;
  177.     Ah = scanptr->Ah;
  178.     Al = scanptr->Al;
  179.     if (cinfo->progressive_mode) {
  180. #ifdef C_PROGRESSIVE_SUPPORTED
  181.       if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
  182.   Ah < 0 || Ah > 13 || Al < 0 || Al > 13)
  183. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  184.       if (Ss == 0) {
  185. if (Se != 0) /* DC and AC together not OK */
  186.   ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  187.       } else {
  188. if (ncomps != 1) /* AC scans must be for only one component */
  189.   ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  190.       }
  191.       for (ci = 0; ci < ncomps; ci++) {
  192. last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
  193. if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
  194.   ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  195. for (coefi = Ss; coefi <= Se; coefi++) {
  196.   if (last_bitpos_ptr[coefi] < 0) {
  197.     /* first scan of this coefficient */
  198.     if (Ah != 0)
  199.       ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  200.   } else {
  201.     /* not first scan */
  202.     if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
  203.       ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  204.   }
  205.   last_bitpos_ptr[coefi] = Al;
  206. }
  207.       }
  208. #endif
  209.     } else {
  210.       /* For sequential JPEG, all progression parameters must be these: */
  211.       if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
  212. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  213.       /* Make sure components are not sent twice */
  214.       for (ci = 0; ci < ncomps; ci++) {
  215. thisi = scanptr->component_index[ci];
  216. if (component_sent[thisi])
  217.   ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  218. component_sent[thisi] = TRUE;
  219.       }
  220.     }
  221.   }
  222.   /* Now verify that everything got sent. */
  223.   if (cinfo->progressive_mode) {
  224. #ifdef C_PROGRESSIVE_SUPPORTED
  225.     /* For progressive mode, we only check that at least some DC data
  226.      * got sent for each component; the spec does not require that all bits
  227.      * of all coefficients be transmitted.  Would it be wiser to enforce
  228.      * transmission of all coefficient bits??
  229.      */
  230.     for (ci = 0; ci < cinfo->num_components; ci++) {
  231.       if (last_bitpos[ci][0] < 0)
  232. ERREXIT(cinfo, JERR_MISSING_DATA);
  233.     }
  234. #endif
  235.   } else {
  236.     for (ci = 0; ci < cinfo->num_components; ci++) {
  237.       if (! component_sent[ci])
  238. ERREXIT(cinfo, JERR_MISSING_DATA);
  239.     }
  240.   }
  241. }
  242. #endif /* C_MULTISCAN_FILES_SUPPORTED */
  243. LOCAL(void)
  244. select_scan_parameters (j_compress_ptr cinfo)
  245. /* Set up the scan parameters for the current scan */
  246. {
  247.   int ci;
  248. #ifdef C_MULTISCAN_FILES_SUPPORTED
  249.   if (cinfo->scan_info != NULL) {
  250.     /* Prepare for current scan --- the script is already validated */
  251.     my_master_ptr master = (my_master_ptr) cinfo->master;
  252.     const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
  253.     cinfo->comps_in_scan = scanptr->comps_in_scan;
  254.     for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
  255.       cinfo->cur_comp_info[ci] =
  256. &cinfo->comp_info[scanptr->component_index[ci]];
  257.     }
  258.     cinfo->Ss = scanptr->Ss;
  259.     cinfo->Se = scanptr->Se;
  260.     cinfo->Ah = scanptr->Ah;
  261.     cinfo->Al = scanptr->Al;
  262.   }
  263.   else
  264. #endif
  265.   {
  266.     /* Prepare for single sequential-JPEG scan containing all components */
  267.     if (cinfo->num_components > MAX_COMPS_IN_SCAN)
  268.       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  269.        MAX_COMPS_IN_SCAN);
  270.     cinfo->comps_in_scan = cinfo->num_components;
  271.     for (ci = 0; ci < cinfo->num_components; ci++) {
  272.       cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
  273.     }
  274.     cinfo->Ss = 0;
  275.     cinfo->Se = DCTSIZE2-1;
  276.     cinfo->Ah = 0;
  277.     cinfo->Al = 0;
  278.   }
  279. }
  280. LOCAL(void)
  281. per_scan_setup (j_compress_ptr cinfo)
  282. /* Do computations that are needed before processing a JPEG scan */
  283. /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
  284. {
  285.   int ci, mcublks, tmp;
  286.   jpeg_component_info *compptr;
  287.   
  288.   if (cinfo->comps_in_scan == 1) {
  289.     
  290.     /* Noninterleaved (single-component) scan */
  291.     compptr = cinfo->cur_comp_info[0];
  292.     
  293.     /* Overall image size in MCUs */
  294.     cinfo->MCUs_per_row = compptr->width_in_blocks;
  295.     cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
  296.     
  297.     /* For noninterleaved scan, always one block per MCU */
  298.     compptr->MCU_width = 1;
  299.     compptr->MCU_height = 1;
  300.     compptr->MCU_blocks = 1;
  301.     compptr->MCU_sample_width = DCTSIZE;
  302.     compptr->last_col_width = 1;
  303.     /* For noninterleaved scans, it is convenient to define last_row_height
  304.      * as the number of block rows present in the last iMCU row.
  305.      */
  306.     tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  307.     if (tmp == 0) tmp = compptr->v_samp_factor;
  308.     compptr->last_row_height = tmp;
  309.     
  310.     /* Prepare array describing MCU composition */
  311.     cinfo->blocks_in_MCU = 1;
  312.     cinfo->MCU_membership[0] = 0;
  313.     
  314.   } else {
  315.     
  316.     /* Interleaved (multi-component) scan */
  317.     if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  318.       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
  319.        MAX_COMPS_IN_SCAN);
  320.     
  321.     /* Overall image size in MCUs */
  322.     cinfo->MCUs_per_row = (JDIMENSION)
  323.       jdiv_round_up((long) cinfo->image_width,
  324.     (long) (cinfo->max_h_samp_factor*DCTSIZE));
  325.     cinfo->MCU_rows_in_scan = (JDIMENSION)
  326.       jdiv_round_up((long) cinfo->image_height,
  327.     (long) (cinfo->max_v_samp_factor*DCTSIZE));
  328.     
  329.     cinfo->blocks_in_MCU = 0;
  330.     
  331.     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  332.       compptr = cinfo->cur_comp_info[ci];
  333.       /* Sampling factors give # of blocks of component in each MCU */
  334.       compptr->MCU_width = compptr->h_samp_factor;
  335.       compptr->MCU_height = compptr->v_samp_factor;
  336.       compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  337.       compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
  338.       /* Figure number of non-dummy blocks in last MCU column & row */
  339.       tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
  340.       if (tmp == 0) tmp = compptr->MCU_width;
  341.       compptr->last_col_width = tmp;
  342.       tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
  343.       if (tmp == 0) tmp = compptr->MCU_height;
  344.       compptr->last_row_height = tmp;
  345.       /* Prepare array describing MCU composition */
  346.       mcublks = compptr->MCU_blocks;
  347.       if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
  348. ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
  349.       while (mcublks-- > 0) {
  350. cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  351.       }
  352.     }
  353.     
  354.   }
  355.   /* Convert restart specified in rows to actual MCU count. */
  356.   /* Note that count must fit in 16 bits, so we provide limiting. */
  357.   if (cinfo->restart_in_rows > 0) {
  358.     long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
  359.     cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
  360.   }
  361. }
  362. /*
  363.  * Per-pass setup.
  364.  * This is called at the beginning of each pass.  We determine which modules
  365.  * will be active during this pass and give them appropriate start_pass calls.
  366.  * We also set is_last_pass to indicate whether any more passes will be
  367.  * required.
  368.  */
  369. METHODDEF(void)
  370. prepare_for_pass (j_compress_ptr cinfo)
  371. {
  372.   my_master_ptr master = (my_master_ptr) cinfo->master;
  373.   switch (master->pass_type) {
  374.   case main_pass:
  375.     /* Initial pass: will collect input data, and do either Huffman
  376.      * optimization or data output for the first scan.
  377.      */
  378.     select_scan_parameters(cinfo);
  379.     per_scan_setup(cinfo);
  380.     if (! cinfo->raw_data_in) {
  381.       (*cinfo->cconvert->start_pass) (cinfo);
  382.       (*cinfo->downsample->start_pass) (cinfo);
  383.       (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
  384.     }
  385.     (*cinfo->fdct->start_pass) (cinfo);
  386.     (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
  387.     (*cinfo->coef->start_pass) (cinfo,
  388. (master->total_passes > 1 ?
  389.  JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
  390.     (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
  391.     if (cinfo->optimize_coding) {
  392.       /* No immediate data output; postpone writing frame/scan headers */
  393.       master->pub.call_pass_startup = FALSE;
  394.     } else {
  395.       /* Will write frame/scan headers at first jpeg_write_scanlines call */
  396.       master->pub.call_pass_startup = TRUE;
  397.     }
  398.     break;
  399. #ifdef ENTROPY_OPT_SUPPORTED
  400.   case huff_opt_pass:
  401.     /* Do Huffman optimization for a scan after the first one. */
  402.     select_scan_parameters(cinfo);
  403.     per_scan_setup(cinfo);
  404.     if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {
  405.       (*cinfo->entropy->start_pass) (cinfo, TRUE);
  406.       (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
  407.       master->pub.call_pass_startup = FALSE;
  408.       break;
  409.     }
  410.     /* Special case: Huffman DC refinement scans need no Huffman table
  411.      * and therefore we can skip the optimization pass for them.
  412.      */
  413.     master->pass_type = output_pass;
  414.     master->pass_number++;
  415.     /*FALLTHROUGH*/
  416. #endif
  417.   case output_pass:
  418.     /* Do a data-output pass. */
  419.     /* We need not repeat per-scan setup if prior optimization pass did it. */
  420.     if (! cinfo->optimize_coding) {
  421.       select_scan_parameters(cinfo);
  422.       per_scan_setup(cinfo);
  423.     }
  424.     (*cinfo->entropy->start_pass) (cinfo, FALSE);
  425.     (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
  426.     /* We emit frame/scan headers now */
  427.     if (master->scan_number == 0)
  428.       (*cinfo->marker->write_frame_header) (cinfo);
  429.     (*cinfo->marker->write_scan_header) (cinfo);
  430.     master->pub.call_pass_startup = FALSE;
  431.     break;
  432.   default:
  433.     ERREXIT(cinfo, JERR_NOT_COMPILED);
  434.   }
  435.   master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
  436.   /* Set up progress monitor's pass info if present */
  437.   if (cinfo->progress != NULL) {
  438.     cinfo->progress->completed_passes = master->pass_number;
  439.     cinfo->progress->total_passes = master->total_passes;
  440.   }
  441. }
  442. /*
  443.  * Special start-of-pass hook.
  444.  * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
  445.  * In single-pass processing, we need this hook because we don't want to
  446.  * write frame/scan headers during jpeg_start_compress; we want to let the
  447.  * application write COM markers etc. between jpeg_start_compress and the
  448.  * jpeg_write_scanlines loop.
  449.  * In multi-pass processing, this routine is not used.
  450.  */
  451. METHODDEF(void)
  452. pass_startup (j_compress_ptr cinfo)
  453. {
  454.   cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
  455.   (*cinfo->marker->write_frame_header) (cinfo);
  456.   (*cinfo->marker->write_scan_header) (cinfo);
  457. }
  458. /*
  459.  * Finish up at end of pass.
  460.  */
  461. METHODDEF(void)
  462. finish_pass_master (j_compress_ptr cinfo)
  463. {
  464.   my_master_ptr master = (my_master_ptr) cinfo->master;
  465.   /* The entropy coder always needs an end-of-pass call,
  466.    * either to analyze statistics or to flush its output buffer.
  467.    */
  468.   (*cinfo->entropy->finish_pass) (cinfo);
  469.   /* Update state for next pass */
  470.   switch (master->pass_type) {
  471.   case main_pass:
  472.     /* next pass is either output of scan 0 (after optimization)
  473.      * or output of scan 1 (if no optimization).
  474.      */
  475.     master->pass_type = output_pass;
  476.     if (! cinfo->optimize_coding)
  477.       master->scan_number++;
  478.     break;
  479.   case huff_opt_pass:
  480.     /* next pass is always output of current scan */
  481.     master->pass_type = output_pass;
  482.     break;
  483.   case output_pass:
  484.     /* next pass is either optimization or output of next scan */
  485.     if (cinfo->optimize_coding)
  486.       master->pass_type = huff_opt_pass;
  487.     master->scan_number++;
  488.     break;
  489.   }
  490.   master->pass_number++;
  491. }
  492. /*
  493.  * Initialize master compression control.
  494.  */
  495. GLOBAL(void)
  496. jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
  497. {
  498.   my_master_ptr master;
  499.   master = (my_master_ptr)
  500.       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  501.   SIZEOF(my_comp_master));
  502.   cinfo->master = (struct jpeg_comp_master *) master;
  503.   master->pub.prepare_for_pass = prepare_for_pass;
  504.   master->pub.pass_startup = pass_startup;
  505.   master->pub.finish_pass = finish_pass_master;
  506.   master->pub.is_last_pass = FALSE;
  507.   /* Validate parameters, determine derived values */
  508.   initial_setup(cinfo);
  509.   if (cinfo->scan_info != NULL) {
  510. #ifdef C_MULTISCAN_FILES_SUPPORTED
  511.     validate_script(cinfo);
  512. #else
  513.     ERREXIT(cinfo, JERR_NOT_COMPILED);
  514. #endif
  515.   } else {
  516.     cinfo->progressive_mode = FALSE;
  517.     cinfo->num_scans = 1;
  518.   }
  519.   if (cinfo->progressive_mode) /*  TEMPORARY HACK ??? */
  520.     cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */
  521.   /* Initialize my private state */
  522.   if (transcode_only) {
  523.     /* no main pass in transcoding */
  524.     if (cinfo->optimize_coding)
  525.       master->pass_type = huff_opt_pass;
  526.     else
  527.       master->pass_type = output_pass;
  528.   } else {
  529.     /* for normal compression, first pass is always this type: */
  530.     master->pass_type = main_pass;
  531.   }
  532.   master->scan_number = 0;
  533.   master->pass_number = 0;
  534.   if (cinfo->optimize_coding)
  535.     master->total_passes = cinfo->num_scans * 2;
  536.   else
  537.     master->total_passes = cinfo->num_scans;
  538. }