home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / jlib / jcmaster.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-16  |  20.5 KB  |  595 lines

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