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