jcmaster.c
Upload User: zlh9724
Upload Date: 2007-01-04
Package Size: 1991k
Code Size: 19k
Category:

Browser Client

Development Platform:

Unix_Linux

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