1 /*
   2  * jcmaster.c
   3  *
   4  * Copyright (C) 1991-1997, Thomas G. Lane.
   5  * Modified 2003-2009 by Guido Vollbeding.
   6  * This file is part of the Independent JPEG Group's software.
   7  * For conditions of distribution and use, see the accompanying README file.
   8  *
   9  * This file contains master control logic for the JPEG compressor.
  10  * These routines are concerned with parameter validation, initial setup,
  11  * and inter-pass control (determining the number of passes and the work
  12  * to be done in each pass).
  13  */
  14 
  15 #define JPEG_INTERNALS
  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   int pass_number;              /* # of passes completed */
  34   int total_passes;             /* total # of passes needed */
  35 
  36   int 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 /*
  47  * Compute JPEG image dimensions and related values.
  48  * NOTE: this is exported for possible use by application.
  49  * Hence it mustn't do anything that can't be done twice.
  50  */
  51 
  52 GLOBAL(void)
  53 jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo)
  54 /* Do computations that are needed before master selection phase */
  55 {
  56 #ifdef DCT_SCALING_SUPPORTED
  57 
  58   /* Compute actual JPEG image dimensions and DCT scaling choices. */
  59   if (cinfo->scale_num >= cinfo->scale_denom * 8) {
  60     /* Provide 8/1 scaling */
  61     cinfo->jpeg_width = cinfo->image_width << 3;
  62     cinfo->jpeg_height = cinfo->image_height << 3;
  63     cinfo->min_DCT_h_scaled_size = 1;
  64     cinfo->min_DCT_v_scaled_size = 1;
  65   } else if (cinfo->scale_num >= cinfo->scale_denom * 4) {
  66     /* Provide 4/1 scaling */
  67     cinfo->jpeg_width = cinfo->image_width << 2;
  68     cinfo->jpeg_height = cinfo->image_height << 2;
  69     cinfo->min_DCT_h_scaled_size = 2;
  70     cinfo->min_DCT_v_scaled_size = 2;
  71   } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 8) {
  72     /* Provide 8/3 scaling */
  73     cinfo->jpeg_width = (cinfo->image_width << 1) + (JDIMENSION)
  74       jdiv_round_up((long) cinfo->image_width * 2, 3L);
  75     cinfo->jpeg_height = (cinfo->image_height << 1) + (JDIMENSION)
  76       jdiv_round_up((long) cinfo->image_height * 2, 3L);
  77     cinfo->min_DCT_h_scaled_size = 3;
  78     cinfo->min_DCT_v_scaled_size = 3;
  79   } else if (cinfo->scale_num >= cinfo->scale_denom * 2) {
  80     /* Provide 2/1 scaling */
  81     cinfo->jpeg_width = cinfo->image_width << 1;
  82     cinfo->jpeg_height = cinfo->image_height << 1;
  83     cinfo->min_DCT_h_scaled_size = 4;
  84     cinfo->min_DCT_v_scaled_size = 4;
  85   } else if (cinfo->scale_num * 5 >= cinfo->scale_denom * 8) {
  86     /* Provide 8/5 scaling */
  87     cinfo->jpeg_width = cinfo->image_width + (JDIMENSION)
  88       jdiv_round_up((long) cinfo->image_width * 3, 5L);
  89     cinfo->jpeg_height = cinfo->image_height + (JDIMENSION)
  90       jdiv_round_up((long) cinfo->image_height * 3, 5L);
  91     cinfo->min_DCT_h_scaled_size = 5;
  92     cinfo->min_DCT_v_scaled_size = 5;
  93   } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 4) {
  94     /* Provide 4/3 scaling */
  95     cinfo->jpeg_width = cinfo->image_width + (JDIMENSION)
  96       jdiv_round_up((long) cinfo->image_width, 3L);
  97     cinfo->jpeg_height = cinfo->image_height + (JDIMENSION)
  98       jdiv_round_up((long) cinfo->image_height, 3L);
  99     cinfo->min_DCT_h_scaled_size = 6;
 100     cinfo->min_DCT_v_scaled_size = 6;
 101   } else if (cinfo->scale_num * 7 >= cinfo->scale_denom * 8) {
 102     /* Provide 8/7 scaling */
 103     cinfo->jpeg_width = cinfo->image_width + (JDIMENSION)
 104       jdiv_round_up((long) cinfo->image_width, 7L);
 105     cinfo->jpeg_height = cinfo->image_height + (JDIMENSION)
 106       jdiv_round_up((long) cinfo->image_height, 7L);
 107     cinfo->min_DCT_h_scaled_size = 7;
 108     cinfo->min_DCT_v_scaled_size = 7;
 109   } else if (cinfo->scale_num >= cinfo->scale_denom) {
 110     /* Provide 1/1 scaling */
 111     cinfo->jpeg_width = cinfo->image_width;
 112     cinfo->jpeg_height = cinfo->image_height;
 113     cinfo->min_DCT_h_scaled_size = DCTSIZE;
 114     cinfo->min_DCT_v_scaled_size = DCTSIZE;
 115   } else if (cinfo->scale_num * 9 >= cinfo->scale_denom * 8) {
 116     /* Provide 8/9 scaling */
 117     cinfo->jpeg_width = (JDIMENSION)
 118       jdiv_round_up((long) cinfo->image_width * 8, 9L);
 119     cinfo->jpeg_height = (JDIMENSION)
 120       jdiv_round_up((long) cinfo->image_height * 8, 9L);
 121     cinfo->min_DCT_h_scaled_size = 9;
 122     cinfo->min_DCT_v_scaled_size = 9;
 123   } else if (cinfo->scale_num * 5 >= cinfo->scale_denom * 4) {
 124     /* Provide 4/5 scaling */
 125     cinfo->jpeg_width = (JDIMENSION)
 126       jdiv_round_up((long) cinfo->image_width * 4, 5L);
 127     cinfo->jpeg_height = (JDIMENSION)
 128       jdiv_round_up((long) cinfo->image_height * 4, 5L);
 129     cinfo->min_DCT_h_scaled_size = 10;
 130     cinfo->min_DCT_v_scaled_size = 10;
 131   } else if (cinfo->scale_num * 11 >= cinfo->scale_denom * 8) {
 132     /* Provide 8/11 scaling */
 133     cinfo->jpeg_width = (JDIMENSION)
 134       jdiv_round_up((long) cinfo->image_width * 8, 11L);
 135     cinfo->jpeg_height = (JDIMENSION)
 136       jdiv_round_up((long) cinfo->image_height * 8, 11L);
 137     cinfo->min_DCT_h_scaled_size = 11;
 138     cinfo->min_DCT_v_scaled_size = 11;
 139   } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 2) {
 140     /* Provide 2/3 scaling */
 141     cinfo->jpeg_width = (JDIMENSION)
 142       jdiv_round_up((long) cinfo->image_width * 2, 3L);
 143     cinfo->jpeg_height = (JDIMENSION)
 144       jdiv_round_up((long) cinfo->image_height * 2, 3L);
 145     cinfo->min_DCT_h_scaled_size = 12;
 146     cinfo->min_DCT_v_scaled_size = 12;
 147   } else if (cinfo->scale_num * 13 >= cinfo->scale_denom * 8) {
 148     /* Provide 8/13 scaling */
 149     cinfo->jpeg_width = (JDIMENSION)
 150       jdiv_round_up((long) cinfo->image_width * 8, 13L);
 151     cinfo->jpeg_height = (JDIMENSION)
 152       jdiv_round_up((long) cinfo->image_height * 8, 13L);
 153     cinfo->min_DCT_h_scaled_size = 13;
 154     cinfo->min_DCT_v_scaled_size = 13;
 155   } else if (cinfo->scale_num * 7 >= cinfo->scale_denom * 4) {
 156     /* Provide 4/7 scaling */
 157     cinfo->jpeg_width = (JDIMENSION)
 158       jdiv_round_up((long) cinfo->image_width * 4, 7L);
 159     cinfo->jpeg_height = (JDIMENSION)
 160       jdiv_round_up((long) cinfo->image_height * 4, 7L);
 161     cinfo->min_DCT_h_scaled_size = 14;
 162     cinfo->min_DCT_v_scaled_size = 14;
 163   } else if (cinfo->scale_num * 15 >= cinfo->scale_denom * 8) {
 164     /* Provide 8/15 scaling */
 165     cinfo->jpeg_width = (JDIMENSION)
 166       jdiv_round_up((long) cinfo->image_width * 8, 15L);
 167     cinfo->jpeg_height = (JDIMENSION)
 168       jdiv_round_up((long) cinfo->image_height * 8, 15L);
 169     cinfo->min_DCT_h_scaled_size = 15;
 170     cinfo->min_DCT_v_scaled_size = 15;
 171   } else {
 172     /* Provide 1/2 scaling */
 173     cinfo->jpeg_width = (JDIMENSION)
 174       jdiv_round_up((long) cinfo->image_width, 2L);
 175     cinfo->jpeg_height = (JDIMENSION)
 176       jdiv_round_up((long) cinfo->image_height, 2L);
 177     cinfo->min_DCT_h_scaled_size = 16;
 178     cinfo->min_DCT_v_scaled_size = 16;
 179   }
 180 
 181 #else /* !DCT_SCALING_SUPPORTED */
 182 
 183   /* Hardwire it to "no scaling" */
 184   cinfo->jpeg_width = cinfo->image_width;
 185   cinfo->jpeg_height = cinfo->image_height;
 186   cinfo->min_DCT_h_scaled_size = DCTSIZE;
 187   cinfo->min_DCT_v_scaled_size = DCTSIZE;
 188 
 189 #endif /* DCT_SCALING_SUPPORTED */
 190 }
 191 
 192 
 193 LOCAL(void)
 194 initial_setup (j_compress_ptr cinfo)
 195 /* Do computations that are needed before master selection phase */
 196 {
 197   int ci, ssize;
 198   jpeg_component_info *compptr;
 199   long samplesperrow;
 200   JDIMENSION jd_samplesperrow;
 201 
 202   jpeg_calc_jpeg_dimensions(cinfo);
 203 
 204   /* Sanity check on image dimensions */
 205   if (cinfo->jpeg_height <= 0 || cinfo->jpeg_width <= 0
 206       || cinfo->num_components <= 0 || cinfo->input_components <= 0)
 207     ERREXIT(cinfo, JERR_EMPTY_IMAGE);
 208 
 209   /* Make sure image isn't bigger than I can handle */
 210   if ((long) cinfo->jpeg_height > (long) JPEG_MAX_DIMENSION ||
 211       (long) cinfo->jpeg_width > (long) JPEG_MAX_DIMENSION)
 212     ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
 213 
 214   /* Width of an input scanline must be representable as JDIMENSION. */
 215   samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
 216   jd_samplesperrow = (JDIMENSION) samplesperrow;
 217   if ((long) jd_samplesperrow != samplesperrow)
 218     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
 219 
 220   /* For now, precision must match compiled-in value... */
 221   if (cinfo->data_precision != BITS_IN_JSAMPLE)
 222     ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
 223 
 224   /* Check that number of components won't exceed internal array sizes */
 225   if (cinfo->num_components > MAX_COMPONENTS)
 226     ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
 227              MAX_COMPONENTS);
 228 
 229   /* Compute maximum sampling factors; check factor validity */
 230   cinfo->max_h_samp_factor = 1;
 231   cinfo->max_v_samp_factor = 1;
 232   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
 233        ci++, compptr++) {
 234     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
 235         compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
 236       ERREXIT(cinfo, JERR_BAD_SAMPLING);
 237     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
 238                                    compptr->h_samp_factor);
 239     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
 240                                    compptr->v_samp_factor);
 241   }
 242 
 243   /* Compute dimensions of components */
 244   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
 245        ci++, compptr++) {
 246     /* Fill in the correct component_index value; don't rely on application */
 247     compptr->component_index = ci;
 248     /* In selecting the actual DCT scaling for each component, we try to
 249      * scale down the chroma components via DCT scaling rather than downsampling.
 250      * This saves time if the downsampler gets to use 1:1 scaling.
 251      * Note this code adapts subsampling ratios which are powers of 2.
 252      */
 253     ssize = 1;
 254 #ifdef DCT_SCALING_SUPPORTED
 255     while (cinfo->min_DCT_h_scaled_size * ssize <=
 256            (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
 257            (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) {
 258       ssize = ssize * 2;
 259     }
 260 #endif
 261     compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize;
 262     ssize = 1;
 263 #ifdef DCT_SCALING_SUPPORTED
 264     while (cinfo->min_DCT_v_scaled_size * ssize <=
 265            (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
 266            (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) {
 267       ssize = ssize * 2;
 268     }
 269 #endif
 270     compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize;
 271 
 272     /* We don't support DCT ratios larger than 2. */
 273     if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2)
 274         compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;
 275     else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2)
 276         compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;
 277 
 278     /* Size in DCT blocks */
 279     compptr->width_in_blocks = (JDIMENSION)
 280       jdiv_round_up((long) cinfo->jpeg_width * (long) compptr->h_samp_factor,
 281                     (long) (cinfo->max_h_samp_factor * DCTSIZE));
 282     compptr->height_in_blocks = (JDIMENSION)
 283       jdiv_round_up((long) cinfo->jpeg_height * (long) compptr->v_samp_factor,
 284                     (long) (cinfo->max_v_samp_factor * DCTSIZE));
 285     /* Size in samples */
 286     compptr->downsampled_width = (JDIMENSION)
 287       jdiv_round_up((long) cinfo->jpeg_width *
 288                     (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size),
 289                     (long) (cinfo->max_h_samp_factor * DCTSIZE));
 290     compptr->downsampled_height = (JDIMENSION)
 291       jdiv_round_up((long) cinfo->jpeg_height *
 292                     (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size),
 293                     (long) (cinfo->max_v_samp_factor * DCTSIZE));
 294     /* Mark component needed (this flag isn't actually used for compression) */
 295     compptr->component_needed = TRUE;
 296   }
 297 
 298   /* Compute number of fully interleaved MCU rows (number of times that
 299    * main controller will call coefficient controller).
 300    */
 301   cinfo->total_iMCU_rows = (JDIMENSION)
 302     jdiv_round_up((long) cinfo->jpeg_height,
 303                   (long) (cinfo->max_v_samp_factor*DCTSIZE));
 304 }
 305 
 306 
 307 #ifdef C_MULTISCAN_FILES_SUPPORTED
 308 
 309 LOCAL(void)
 310 validate_script (j_compress_ptr cinfo)
 311 /* Verify that the scan script in cinfo->scan_info[] is valid; also
 312  * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
 313  */
 314 {
 315   const jpeg_scan_info * scanptr;
 316   int scanno, ncomps, ci, coefi, thisi;
 317   int Ss, Se, Ah, Al;
 318   boolean component_sent[MAX_COMPONENTS];
 319 #ifdef C_PROGRESSIVE_SUPPORTED
 320   int * last_bitpos_ptr;
 321   int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
 322   /* -1 until that coefficient has been seen; then last Al for it */
 323   MEMZERO(last_bitpos, SIZEOF(last_bitpos));
 324 #endif
 325 
 326   MEMZERO(component_sent, SIZEOF(component_sent));
 327 
 328   if (cinfo->num_scans <= 0)
 329     ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
 330 
 331   /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
 332    * for progressive JPEG, no scan can have this.
 333    */
 334   scanptr = cinfo->scan_info;
 335   if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
 336 #ifdef C_PROGRESSIVE_SUPPORTED
 337     cinfo->progressive_mode = TRUE;
 338     last_bitpos_ptr = & last_bitpos[0][0];
 339     for (ci = 0; ci < cinfo->num_components; ci++)
 340       for (coefi = 0; coefi < DCTSIZE2; coefi++)
 341         *last_bitpos_ptr++ = -1;
 342 #else
 343     ERREXIT(cinfo, JERR_NOT_COMPILED);
 344 #endif
 345   } else {
 346     cinfo->progressive_mode = FALSE;
 347     for (ci = 0; ci < cinfo->num_components; ci++)
 348       component_sent[ci] = FALSE;
 349   }
 350 
 351   for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
 352     /* Validate component indexes */
 353     ncomps = scanptr->comps_in_scan;
 354     if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
 355       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
 356     for (ci = 0; ci < ncomps; ci++) {
 357       thisi = scanptr->component_index[ci];
 358       if (thisi < 0 || thisi >= cinfo->num_components)
 359         ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
 360       /* Components must appear in SOF order within each scan */
 361       if (ci > 0 && thisi <= scanptr->component_index[ci-1])
 362         ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
 363     }
 364     /* Validate progression parameters */
 365     Ss = scanptr->Ss;
 366     Se = scanptr->Se;
 367     Ah = scanptr->Ah;
 368     Al = scanptr->Al;
 369     if (cinfo->progressive_mode) {
 370 #ifdef C_PROGRESSIVE_SUPPORTED
 371       /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that
 372        * seems wrong: the upper bound ought to depend on data precision.
 373        * Perhaps they really meant 0..N+1 for N-bit precision.
 374        * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
 375        * out-of-range reconstructed DC values during the first DC scan,
 376        * which might cause problems for some decoders.
 377        */
 378 #if BITS_IN_JSAMPLE == 8
 379 #define MAX_AH_AL 10
 380 #else
 381 #define MAX_AH_AL 13
 382 #endif
 383       if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
 384           Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
 385         ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
 386       if (Ss == 0) {
 387         if (Se != 0)            /* DC and AC together not OK */
 388           ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
 389       } else {
 390         if (ncomps != 1)        /* AC scans must be for only one component */
 391           ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
 392       }
 393       for (ci = 0; ci < ncomps; ci++) {
 394         last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
 395         if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
 396           ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
 397         for (coefi = Ss; coefi <= Se; coefi++) {
 398           if (last_bitpos_ptr[coefi] < 0) {
 399             /* first scan of this coefficient */
 400             if (Ah != 0)
 401               ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
 402           } else {
 403             /* not first scan */
 404             if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
 405               ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
 406           }
 407           last_bitpos_ptr[coefi] = Al;
 408         }
 409       }
 410 #endif
 411     } else {
 412       /* For sequential JPEG, all progression parameters must be these: */
 413       if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
 414         ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
 415       /* Make sure components are not sent twice */
 416       for (ci = 0; ci < ncomps; ci++) {
 417         thisi = scanptr->component_index[ci];
 418         if (component_sent[thisi])
 419           ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
 420         component_sent[thisi] = TRUE;
 421       }
 422     }
 423   }
 424 
 425   /* Now verify that everything got sent. */
 426   if (cinfo->progressive_mode) {
 427 #ifdef C_PROGRESSIVE_SUPPORTED
 428     /* For progressive mode, we only check that at least some DC data
 429      * got sent for each component; the spec does not require that all bits
 430      * of all coefficients be transmitted.  Would it be wiser to enforce
 431      * transmission of all coefficient bits??
 432      */
 433     for (ci = 0; ci < cinfo->num_components; ci++) {
 434       if (last_bitpos[ci][0] < 0)
 435         ERREXIT(cinfo, JERR_MISSING_DATA);
 436     }
 437 #endif
 438   } else {
 439     for (ci = 0; ci < cinfo->num_components; ci++) {
 440       if (! component_sent[ci])
 441         ERREXIT(cinfo, JERR_MISSING_DATA);
 442     }
 443   }
 444 }
 445 
 446 #endif /* C_MULTISCAN_FILES_SUPPORTED */
 447 
 448 
 449 LOCAL(void)
 450 select_scan_parameters (j_compress_ptr cinfo)
 451 /* Set up the scan parameters for the current scan */
 452 {
 453   int ci;
 454 
 455 #ifdef C_MULTISCAN_FILES_SUPPORTED
 456   if (cinfo->scan_info != NULL) {
 457     /* Prepare for current scan --- the script is already validated */
 458     my_master_ptr master = (my_master_ptr) cinfo->master;
 459     const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
 460 
 461     cinfo->comps_in_scan = scanptr->comps_in_scan;
 462     for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
 463       cinfo->cur_comp_info[ci] =
 464         &cinfo->comp_info[scanptr->component_index[ci]];
 465     }
 466     cinfo->Ss = scanptr->Ss;
 467     cinfo->Se = scanptr->Se;
 468     cinfo->Ah = scanptr->Ah;
 469     cinfo->Al = scanptr->Al;
 470   }
 471   else
 472 #endif
 473   {
 474     /* Prepare for single sequential-JPEG scan containing all components */
 475     if (cinfo->num_components > MAX_COMPS_IN_SCAN)
 476       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
 477                MAX_COMPS_IN_SCAN);
 478     cinfo->comps_in_scan = cinfo->num_components;
 479     for (ci = 0; ci < cinfo->num_components; ci++) {
 480       cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
 481     }
 482     cinfo->Ss = 0;
 483     cinfo->Se = DCTSIZE2-1;
 484     cinfo->Ah = 0;
 485     cinfo->Al = 0;
 486   }
 487 }
 488 
 489 
 490 LOCAL(void)
 491 per_scan_setup (j_compress_ptr cinfo)
 492 /* Do computations that are needed before processing a JPEG scan */
 493 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
 494 {
 495   int ci, mcublks, tmp;
 496   jpeg_component_info *compptr;
 497 
 498   if (cinfo->comps_in_scan == 1) {
 499 
 500     /* Noninterleaved (single-component) scan */
 501     compptr = cinfo->cur_comp_info[0];
 502 
 503     /* Overall image size in MCUs */
 504     cinfo->MCUs_per_row = compptr->width_in_blocks;
 505     cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
 506 
 507     /* For noninterleaved scan, always one block per MCU */
 508     compptr->MCU_width = 1;
 509     compptr->MCU_height = 1;
 510     compptr->MCU_blocks = 1;
 511     compptr->MCU_sample_width = compptr->DCT_h_scaled_size;
 512     compptr->last_col_width = 1;
 513     /* For noninterleaved scans, it is convenient to define last_row_height
 514      * as the number of block rows present in the last iMCU row.
 515      */
 516     tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
 517     if (tmp == 0) tmp = compptr->v_samp_factor;
 518     compptr->last_row_height = tmp;
 519 
 520     /* Prepare array describing MCU composition */
 521     cinfo->blocks_in_MCU = 1;
 522     cinfo->MCU_membership[0] = 0;
 523 
 524   } else {
 525 
 526     /* Interleaved (multi-component) scan */
 527     if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
 528       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
 529                MAX_COMPS_IN_SCAN);
 530 
 531     /* Overall image size in MCUs */
 532     cinfo->MCUs_per_row = (JDIMENSION)
 533       jdiv_round_up((long) cinfo->jpeg_width,
 534                     (long) (cinfo->max_h_samp_factor*DCTSIZE));
 535     cinfo->MCU_rows_in_scan = (JDIMENSION)
 536       jdiv_round_up((long) cinfo->jpeg_height,
 537                     (long) (cinfo->max_v_samp_factor*DCTSIZE));
 538 
 539     cinfo->blocks_in_MCU = 0;
 540 
 541     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
 542       compptr = cinfo->cur_comp_info[ci];
 543       /* Sampling factors give # of blocks of component in each MCU */
 544       compptr->MCU_width = compptr->h_samp_factor;
 545       compptr->MCU_height = compptr->v_samp_factor;
 546       compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
 547       compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size;
 548       /* Figure number of non-dummy blocks in last MCU column & row */
 549       tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
 550       if (tmp == 0) tmp = compptr->MCU_width;
 551       compptr->last_col_width = tmp;
 552       tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
 553       if (tmp == 0) tmp = compptr->MCU_height;
 554       compptr->last_row_height = tmp;
 555       /* Prepare array describing MCU composition */
 556       mcublks = compptr->MCU_blocks;
 557       if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
 558         ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
 559       while (mcublks-- > 0) {
 560         cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
 561       }
 562     }
 563 
 564   }
 565 
 566   /* Convert restart specified in rows to actual MCU count. */
 567   /* Note that count must fit in 16 bits, so we provide limiting. */
 568   if (cinfo->restart_in_rows > 0) {
 569     long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
 570     cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
 571   }
 572 }
 573 
 574 
 575 /*
 576  * Per-pass setup.
 577  * This is called at the beginning of each pass.  We determine which modules
 578  * will be active during this pass and give them appropriate start_pass calls.
 579  * We also set is_last_pass to indicate whether any more passes will be
 580  * required.
 581  */
 582 
 583 METHODDEF(void)
 584 prepare_for_pass (j_compress_ptr cinfo)
 585 {
 586   my_master_ptr master = (my_master_ptr) cinfo->master;
 587 
 588   switch (master->pass_type) {
 589   case main_pass:
 590     /* Initial pass: will collect input data, and do either Huffman
 591      * optimization or data output for the first scan.
 592      */
 593     select_scan_parameters(cinfo);
 594     per_scan_setup(cinfo);
 595     if (! cinfo->raw_data_in) {
 596       (*cinfo->cconvert->start_pass) (cinfo);
 597       (*cinfo->downsample->start_pass) (cinfo);
 598       (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
 599     }
 600     (*cinfo->fdct->start_pass) (cinfo);
 601     (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
 602     (*cinfo->coef->start_pass) (cinfo,
 603                                 (master->total_passes > 1 ?
 604                                  JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
 605     (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
 606     if (cinfo->optimize_coding) {
 607       /* No immediate data output; postpone writing frame/scan headers */
 608       master->pub.call_pass_startup = FALSE;
 609     } else {
 610       /* Will write frame/scan headers at first jpeg_write_scanlines call */
 611       master->pub.call_pass_startup = TRUE;
 612     }
 613     break;
 614 #ifdef ENTROPY_OPT_SUPPORTED
 615   case huff_opt_pass:
 616     /* Do Huffman optimization for a scan after the first one. */
 617     select_scan_parameters(cinfo);
 618     per_scan_setup(cinfo);
 619     if (cinfo->Ss != 0 || cinfo->Ah == 0) {
 620       (*cinfo->entropy->start_pass) (cinfo, TRUE);
 621       (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
 622       master->pub.call_pass_startup = FALSE;
 623       break;
 624     }
 625     /* Special case: Huffman DC refinement scans need no Huffman table
 626      * and therefore we can skip the optimization pass for them.
 627      */
 628     master->pass_type = output_pass;
 629     master->pass_number++;
 630     /*FALLTHROUGH*/
 631 #endif
 632   case output_pass:
 633     /* Do a data-output pass. */
 634     /* We need not repeat per-scan setup if prior optimization pass did it. */
 635     if (! cinfo->optimize_coding) {
 636       select_scan_parameters(cinfo);
 637       per_scan_setup(cinfo);
 638     }
 639     (*cinfo->entropy->start_pass) (cinfo, FALSE);
 640     (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
 641     /* We emit frame/scan headers now */
 642     if (master->scan_number == 0)
 643       (*cinfo->marker->write_frame_header) (cinfo);
 644     (*cinfo->marker->write_scan_header) (cinfo);
 645     master->pub.call_pass_startup = FALSE;
 646     break;
 647   default:
 648     ERREXIT(cinfo, JERR_NOT_COMPILED);
 649   }
 650 
 651   master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
 652 
 653   /* Set up progress monitor's pass info if present */
 654   if (cinfo->progress != NULL) {
 655     cinfo->progress->completed_passes = master->pass_number;
 656     cinfo->progress->total_passes = master->total_passes;
 657   }
 658 }
 659 
 660 
 661 /*
 662  * Special start-of-pass hook.
 663  * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
 664  * In single-pass processing, we need this hook because we don't want to
 665  * write frame/scan headers during jpeg_start_compress; we want to let the
 666  * application write COM markers etc. between jpeg_start_compress and the
 667  * jpeg_write_scanlines loop.
 668  * In multi-pass processing, this routine is not used.
 669  */
 670 
 671 METHODDEF(void)
 672 pass_startup (j_compress_ptr cinfo)
 673 {
 674   cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
 675 
 676   (*cinfo->marker->write_frame_header) (cinfo);
 677   (*cinfo->marker->write_scan_header) (cinfo);
 678 }
 679 
 680 
 681 /*
 682  * Finish up at end of pass.
 683  */
 684 
 685 METHODDEF(void)
 686 finish_pass_master (j_compress_ptr cinfo)
 687 {
 688   my_master_ptr master = (my_master_ptr) cinfo->master;
 689 
 690   /* The entropy coder always needs an end-of-pass call,
 691    * either to analyze statistics or to flush its output buffer.
 692    */
 693   (*cinfo->entropy->finish_pass) (cinfo);
 694 
 695   /* Update state for next pass */
 696   switch (master->pass_type) {
 697   case main_pass:
 698     /* next pass is either output of scan 0 (after optimization)
 699      * or output of scan 1 (if no optimization).
 700      */
 701     master->pass_type = output_pass;
 702     if (! cinfo->optimize_coding)
 703       master->scan_number++;
 704     break;
 705   case huff_opt_pass:
 706     /* next pass is always output of current scan */
 707     master->pass_type = output_pass;
 708     break;
 709   case output_pass:
 710     /* next pass is either optimization or output of next scan */
 711     if (cinfo->optimize_coding)
 712       master->pass_type = huff_opt_pass;
 713     master->scan_number++;
 714     break;
 715   }
 716 
 717   master->pass_number++;
 718 }
 719 
 720 
 721 /*
 722  * Initialize master compression control.
 723  */
 724 
 725 GLOBAL(void)
 726 jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
 727 {
 728   my_master_ptr master;
 729 
 730   master = (my_master_ptr)
 731       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
 732                                   SIZEOF(my_comp_master));
 733   cinfo->master = (struct jpeg_comp_master *) master;
 734   master->pub.prepare_for_pass = prepare_for_pass;
 735   master->pub.pass_startup = pass_startup;
 736   master->pub.finish_pass = finish_pass_master;
 737   master->pub.is_last_pass = FALSE;
 738 
 739   /* Validate parameters, determine derived values */
 740   initial_setup(cinfo);
 741 
 742   if (cinfo->scan_info != NULL) {
 743 #ifdef C_MULTISCAN_FILES_SUPPORTED
 744     validate_script(cinfo);
 745 #else
 746     ERREXIT(cinfo, JERR_NOT_COMPILED);
 747 #endif
 748   } else {
 749     cinfo->progressive_mode = FALSE;
 750     cinfo->num_scans = 1;
 751   }
 752 
 753   if (cinfo->progressive_mode && cinfo->arith_code == 0)        /*  TEMPORARY HACK ??? */
 754     cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */
 755 
 756   /* Initialize my private state */
 757   if (transcode_only) {
 758     /* no main pass in transcoding */
 759     if (cinfo->optimize_coding)
 760       master->pass_type = huff_opt_pass;
 761     else
 762       master->pass_type = output_pass;
 763   } else {
 764     /* for normal compression, first pass is always this type: */
 765     master->pass_type = main_pass;
 766   }
 767   master->scan_number = 0;
 768   master->pass_number = 0;
 769   if (cinfo->optimize_coding)
 770     master->total_passes = cinfo->num_scans * 2;
 771   else
 772     master->total_passes = cinfo->num_scans;
 773 }