home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / jpeg / jcmaster.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  19.6 KB  |  580 lines

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