dbf-halloween2015

diff libs/libjpeg/jcmaster.c @ 1:c3f5c32cb210

barfed all the libraries in the source tree to make porting easier
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Nov 2015 00:36:56 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/libjpeg/jcmaster.c	Sun Nov 01 00:36:56 2015 +0200
     1.3 @@ -0,0 +1,590 @@
     1.4 +/*
     1.5 + * jcmaster.c
     1.6 + *
     1.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
     1.8 + * This file is part of the Independent JPEG Group's software.
     1.9 + * For conditions of distribution and use, see the accompanying README file.
    1.10 + *
    1.11 + * This file contains master control logic for the JPEG compressor.
    1.12 + * These routines are concerned with parameter validation, initial setup,
    1.13 + * and inter-pass control (determining the number of passes and the work 
    1.14 + * to be done in each pass).
    1.15 + */
    1.16 +
    1.17 +#define JPEG_INTERNALS
    1.18 +#include "jinclude.h"
    1.19 +#include "jpeglib.h"
    1.20 +
    1.21 +
    1.22 +/* Private state */
    1.23 +
    1.24 +typedef enum {
    1.25 +	main_pass,		/* input data, also do first output step */
    1.26 +	huff_opt_pass,		/* Huffman code optimization pass */
    1.27 +	output_pass		/* data output pass */
    1.28 +} c_pass_type;
    1.29 +
    1.30 +typedef struct {
    1.31 +  struct jpeg_comp_master pub;	/* public fields */
    1.32 +
    1.33 +  c_pass_type pass_type;	/* the type of the current pass */
    1.34 +
    1.35 +  int pass_number;		/* # of passes completed */
    1.36 +  int total_passes;		/* total # of passes needed */
    1.37 +
    1.38 +  int scan_number;		/* current index in scan_info[] */
    1.39 +} my_comp_master;
    1.40 +
    1.41 +typedef my_comp_master * my_master_ptr;
    1.42 +
    1.43 +
    1.44 +/*
    1.45 + * Support routines that do various essential calculations.
    1.46 + */
    1.47 +
    1.48 +LOCAL(void)
    1.49 +initial_setup (j_compress_ptr cinfo)
    1.50 +/* Do computations that are needed before master selection phase */
    1.51 +{
    1.52 +  int ci;
    1.53 +  jpeg_component_info *compptr;
    1.54 +  long samplesperrow;
    1.55 +  JDIMENSION jd_samplesperrow;
    1.56 +
    1.57 +  /* Sanity check on image dimensions */
    1.58 +  if (cinfo->image_height <= 0 || cinfo->image_width <= 0
    1.59 +      || cinfo->num_components <= 0 || cinfo->input_components <= 0)
    1.60 +    ERREXIT(cinfo, JERR_EMPTY_IMAGE);
    1.61 +
    1.62 +  /* Make sure image isn't bigger than I can handle */
    1.63 +  if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
    1.64 +      (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
    1.65 +    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
    1.66 +
    1.67 +  /* Width of an input scanline must be representable as JDIMENSION. */
    1.68 +  samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
    1.69 +  jd_samplesperrow = (JDIMENSION) samplesperrow;
    1.70 +  if ((long) jd_samplesperrow != samplesperrow)
    1.71 +    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
    1.72 +
    1.73 +  /* For now, precision must match compiled-in value... */
    1.74 +  if (cinfo->data_precision != BITS_IN_JSAMPLE)
    1.75 +    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
    1.76 +
    1.77 +  /* Check that number of components won't exceed internal array sizes */
    1.78 +  if (cinfo->num_components > MAX_COMPONENTS)
    1.79 +    ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
    1.80 +	     MAX_COMPONENTS);
    1.81 +
    1.82 +  /* Compute maximum sampling factors; check factor validity */
    1.83 +  cinfo->max_h_samp_factor = 1;
    1.84 +  cinfo->max_v_samp_factor = 1;
    1.85 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
    1.86 +       ci++, compptr++) {
    1.87 +    if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
    1.88 +	compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
    1.89 +      ERREXIT(cinfo, JERR_BAD_SAMPLING);
    1.90 +    cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
    1.91 +				   compptr->h_samp_factor);
    1.92 +    cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
    1.93 +				   compptr->v_samp_factor);
    1.94 +  }
    1.95 +
    1.96 +  /* Compute dimensions of components */
    1.97 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
    1.98 +       ci++, compptr++) {
    1.99 +    /* Fill in the correct component_index value; don't rely on application */
   1.100 +    compptr->component_index = ci;
   1.101 +    /* For compression, we never do DCT scaling. */
   1.102 +    compptr->DCT_scaled_size = DCTSIZE;
   1.103 +    /* Size in DCT blocks */
   1.104 +    compptr->width_in_blocks = (JDIMENSION)
   1.105 +      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
   1.106 +		    (long) (cinfo->max_h_samp_factor * DCTSIZE));
   1.107 +    compptr->height_in_blocks = (JDIMENSION)
   1.108 +      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
   1.109 +		    (long) (cinfo->max_v_samp_factor * DCTSIZE));
   1.110 +    /* Size in samples */
   1.111 +    compptr->downsampled_width = (JDIMENSION)
   1.112 +      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
   1.113 +		    (long) cinfo->max_h_samp_factor);
   1.114 +    compptr->downsampled_height = (JDIMENSION)
   1.115 +      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
   1.116 +		    (long) cinfo->max_v_samp_factor);
   1.117 +    /* Mark component needed (this flag isn't actually used for compression) */
   1.118 +    compptr->component_needed = TRUE;
   1.119 +  }
   1.120 +
   1.121 +  /* Compute number of fully interleaved MCU rows (number of times that
   1.122 +   * main controller will call coefficient controller).
   1.123 +   */
   1.124 +  cinfo->total_iMCU_rows = (JDIMENSION)
   1.125 +    jdiv_round_up((long) cinfo->image_height,
   1.126 +		  (long) (cinfo->max_v_samp_factor*DCTSIZE));
   1.127 +}
   1.128 +
   1.129 +
   1.130 +#ifdef C_MULTISCAN_FILES_SUPPORTED
   1.131 +
   1.132 +LOCAL(void)
   1.133 +validate_script (j_compress_ptr cinfo)
   1.134 +/* Verify that the scan script in cinfo->scan_info[] is valid; also
   1.135 + * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
   1.136 + */
   1.137 +{
   1.138 +  const jpeg_scan_info * scanptr;
   1.139 +  int scanno, ncomps, ci, coefi, thisi;
   1.140 +  int Ss, Se, Ah, Al;
   1.141 +  boolean component_sent[MAX_COMPONENTS];
   1.142 +#ifdef C_PROGRESSIVE_SUPPORTED
   1.143 +  int * last_bitpos_ptr;
   1.144 +  int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
   1.145 +  /* -1 until that coefficient has been seen; then last Al for it */
   1.146 +#endif
   1.147 +
   1.148 +  if (cinfo->num_scans <= 0)
   1.149 +    ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
   1.150 +
   1.151 +  /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
   1.152 +   * for progressive JPEG, no scan can have this.
   1.153 +   */
   1.154 +  scanptr = cinfo->scan_info;
   1.155 +  if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
   1.156 +#ifdef C_PROGRESSIVE_SUPPORTED
   1.157 +    cinfo->progressive_mode = TRUE;
   1.158 +    last_bitpos_ptr = & last_bitpos[0][0];
   1.159 +    for (ci = 0; ci < cinfo->num_components; ci++) 
   1.160 +      for (coefi = 0; coefi < DCTSIZE2; coefi++)
   1.161 +	*last_bitpos_ptr++ = -1;
   1.162 +#else
   1.163 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
   1.164 +#endif
   1.165 +  } else {
   1.166 +    cinfo->progressive_mode = FALSE;
   1.167 +    for (ci = 0; ci < cinfo->num_components; ci++) 
   1.168 +      component_sent[ci] = FALSE;
   1.169 +  }
   1.170 +
   1.171 +  for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
   1.172 +    /* Validate component indexes */
   1.173 +    ncomps = scanptr->comps_in_scan;
   1.174 +    if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
   1.175 +      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
   1.176 +    for (ci = 0; ci < ncomps; ci++) {
   1.177 +      thisi = scanptr->component_index[ci];
   1.178 +      if (thisi < 0 || thisi >= cinfo->num_components)
   1.179 +	ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
   1.180 +      /* Components must appear in SOF order within each scan */
   1.181 +      if (ci > 0 && thisi <= scanptr->component_index[ci-1])
   1.182 +	ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
   1.183 +    }
   1.184 +    /* Validate progression parameters */
   1.185 +    Ss = scanptr->Ss;
   1.186 +    Se = scanptr->Se;
   1.187 +    Ah = scanptr->Ah;
   1.188 +    Al = scanptr->Al;
   1.189 +    if (cinfo->progressive_mode) {
   1.190 +#ifdef C_PROGRESSIVE_SUPPORTED
   1.191 +      /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that
   1.192 +       * seems wrong: the upper bound ought to depend on data precision.
   1.193 +       * Perhaps they really meant 0..N+1 for N-bit precision.
   1.194 +       * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
   1.195 +       * out-of-range reconstructed DC values during the first DC scan,
   1.196 +       * which might cause problems for some decoders.
   1.197 +       */
   1.198 +#if BITS_IN_JSAMPLE == 8
   1.199 +#define MAX_AH_AL 10
   1.200 +#else
   1.201 +#define MAX_AH_AL 13
   1.202 +#endif
   1.203 +      if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
   1.204 +	  Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
   1.205 +	ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
   1.206 +      if (Ss == 0) {
   1.207 +	if (Se != 0)		/* DC and AC together not OK */
   1.208 +	  ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
   1.209 +      } else {
   1.210 +	if (ncomps != 1)	/* AC scans must be for only one component */
   1.211 +	  ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
   1.212 +      }
   1.213 +      for (ci = 0; ci < ncomps; ci++) {
   1.214 +	last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
   1.215 +	if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
   1.216 +	  ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
   1.217 +	for (coefi = Ss; coefi <= Se; coefi++) {
   1.218 +	  if (last_bitpos_ptr[coefi] < 0) {
   1.219 +	    /* first scan of this coefficient */
   1.220 +	    if (Ah != 0)
   1.221 +	      ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
   1.222 +	  } else {
   1.223 +	    /* not first scan */
   1.224 +	    if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
   1.225 +	      ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
   1.226 +	  }
   1.227 +	  last_bitpos_ptr[coefi] = Al;
   1.228 +	}
   1.229 +      }
   1.230 +#endif
   1.231 +    } else {
   1.232 +      /* For sequential JPEG, all progression parameters must be these: */
   1.233 +      if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
   1.234 +	ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
   1.235 +      /* Make sure components are not sent twice */
   1.236 +      for (ci = 0; ci < ncomps; ci++) {
   1.237 +	thisi = scanptr->component_index[ci];
   1.238 +	if (component_sent[thisi])
   1.239 +	  ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
   1.240 +	component_sent[thisi] = TRUE;
   1.241 +      }
   1.242 +    }
   1.243 +  }
   1.244 +
   1.245 +  /* Now verify that everything got sent. */
   1.246 +  if (cinfo->progressive_mode) {
   1.247 +#ifdef C_PROGRESSIVE_SUPPORTED
   1.248 +    /* For progressive mode, we only check that at least some DC data
   1.249 +     * got sent for each component; the spec does not require that all bits
   1.250 +     * of all coefficients be transmitted.  Would it be wiser to enforce
   1.251 +     * transmission of all coefficient bits??
   1.252 +     */
   1.253 +    for (ci = 0; ci < cinfo->num_components; ci++) {
   1.254 +      if (last_bitpos[ci][0] < 0)
   1.255 +	ERREXIT(cinfo, JERR_MISSING_DATA);
   1.256 +    }
   1.257 +#endif
   1.258 +  } else {
   1.259 +    for (ci = 0; ci < cinfo->num_components; ci++) {
   1.260 +      if (! component_sent[ci])
   1.261 +	ERREXIT(cinfo, JERR_MISSING_DATA);
   1.262 +    }
   1.263 +  }
   1.264 +}
   1.265 +
   1.266 +#endif /* C_MULTISCAN_FILES_SUPPORTED */
   1.267 +
   1.268 +
   1.269 +LOCAL(void)
   1.270 +select_scan_parameters (j_compress_ptr cinfo)
   1.271 +/* Set up the scan parameters for the current scan */
   1.272 +{
   1.273 +  int ci;
   1.274 +
   1.275 +#ifdef C_MULTISCAN_FILES_SUPPORTED
   1.276 +  if (cinfo->scan_info != NULL) {
   1.277 +    /* Prepare for current scan --- the script is already validated */
   1.278 +    my_master_ptr master = (my_master_ptr) cinfo->master;
   1.279 +    const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
   1.280 +
   1.281 +    cinfo->comps_in_scan = scanptr->comps_in_scan;
   1.282 +    for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
   1.283 +      cinfo->cur_comp_info[ci] =
   1.284 +	&cinfo->comp_info[scanptr->component_index[ci]];
   1.285 +    }
   1.286 +    cinfo->Ss = scanptr->Ss;
   1.287 +    cinfo->Se = scanptr->Se;
   1.288 +    cinfo->Ah = scanptr->Ah;
   1.289 +    cinfo->Al = scanptr->Al;
   1.290 +  }
   1.291 +  else
   1.292 +#endif
   1.293 +  {
   1.294 +    /* Prepare for single sequential-JPEG scan containing all components */
   1.295 +    if (cinfo->num_components > MAX_COMPS_IN_SCAN)
   1.296 +      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
   1.297 +	       MAX_COMPS_IN_SCAN);
   1.298 +    cinfo->comps_in_scan = cinfo->num_components;
   1.299 +    for (ci = 0; ci < cinfo->num_components; ci++) {
   1.300 +      cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
   1.301 +    }
   1.302 +    cinfo->Ss = 0;
   1.303 +    cinfo->Se = DCTSIZE2-1;
   1.304 +    cinfo->Ah = 0;
   1.305 +    cinfo->Al = 0;
   1.306 +  }
   1.307 +}
   1.308 +
   1.309 +
   1.310 +LOCAL(void)
   1.311 +per_scan_setup (j_compress_ptr cinfo)
   1.312 +/* Do computations that are needed before processing a JPEG scan */
   1.313 +/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
   1.314 +{
   1.315 +  int ci, mcublks, tmp;
   1.316 +  jpeg_component_info *compptr;
   1.317 +  
   1.318 +  if (cinfo->comps_in_scan == 1) {
   1.319 +    
   1.320 +    /* Noninterleaved (single-component) scan */
   1.321 +    compptr = cinfo->cur_comp_info[0];
   1.322 +    
   1.323 +    /* Overall image size in MCUs */
   1.324 +    cinfo->MCUs_per_row = compptr->width_in_blocks;
   1.325 +    cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
   1.326 +    
   1.327 +    /* For noninterleaved scan, always one block per MCU */
   1.328 +    compptr->MCU_width = 1;
   1.329 +    compptr->MCU_height = 1;
   1.330 +    compptr->MCU_blocks = 1;
   1.331 +    compptr->MCU_sample_width = DCTSIZE;
   1.332 +    compptr->last_col_width = 1;
   1.333 +    /* For noninterleaved scans, it is convenient to define last_row_height
   1.334 +     * as the number of block rows present in the last iMCU row.
   1.335 +     */
   1.336 +    tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
   1.337 +    if (tmp == 0) tmp = compptr->v_samp_factor;
   1.338 +    compptr->last_row_height = tmp;
   1.339 +    
   1.340 +    /* Prepare array describing MCU composition */
   1.341 +    cinfo->blocks_in_MCU = 1;
   1.342 +    cinfo->MCU_membership[0] = 0;
   1.343 +    
   1.344 +  } else {
   1.345 +    
   1.346 +    /* Interleaved (multi-component) scan */
   1.347 +    if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
   1.348 +      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
   1.349 +	       MAX_COMPS_IN_SCAN);
   1.350 +    
   1.351 +    /* Overall image size in MCUs */
   1.352 +    cinfo->MCUs_per_row = (JDIMENSION)
   1.353 +      jdiv_round_up((long) cinfo->image_width,
   1.354 +		    (long) (cinfo->max_h_samp_factor*DCTSIZE));
   1.355 +    cinfo->MCU_rows_in_scan = (JDIMENSION)
   1.356 +      jdiv_round_up((long) cinfo->image_height,
   1.357 +		    (long) (cinfo->max_v_samp_factor*DCTSIZE));
   1.358 +    
   1.359 +    cinfo->blocks_in_MCU = 0;
   1.360 +    
   1.361 +    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
   1.362 +      compptr = cinfo->cur_comp_info[ci];
   1.363 +      /* Sampling factors give # of blocks of component in each MCU */
   1.364 +      compptr->MCU_width = compptr->h_samp_factor;
   1.365 +      compptr->MCU_height = compptr->v_samp_factor;
   1.366 +      compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
   1.367 +      compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
   1.368 +      /* Figure number of non-dummy blocks in last MCU column & row */
   1.369 +      tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
   1.370 +      if (tmp == 0) tmp = compptr->MCU_width;
   1.371 +      compptr->last_col_width = tmp;
   1.372 +      tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
   1.373 +      if (tmp == 0) tmp = compptr->MCU_height;
   1.374 +      compptr->last_row_height = tmp;
   1.375 +      /* Prepare array describing MCU composition */
   1.376 +      mcublks = compptr->MCU_blocks;
   1.377 +      if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
   1.378 +	ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
   1.379 +      while (mcublks-- > 0) {
   1.380 +	cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
   1.381 +      }
   1.382 +    }
   1.383 +    
   1.384 +  }
   1.385 +
   1.386 +  /* Convert restart specified in rows to actual MCU count. */
   1.387 +  /* Note that count must fit in 16 bits, so we provide limiting. */
   1.388 +  if (cinfo->restart_in_rows > 0) {
   1.389 +    long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
   1.390 +    cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
   1.391 +  }
   1.392 +}
   1.393 +
   1.394 +
   1.395 +/*
   1.396 + * Per-pass setup.
   1.397 + * This is called at the beginning of each pass.  We determine which modules
   1.398 + * will be active during this pass and give them appropriate start_pass calls.
   1.399 + * We also set is_last_pass to indicate whether any more passes will be
   1.400 + * required.
   1.401 + */
   1.402 +
   1.403 +METHODDEF(void)
   1.404 +prepare_for_pass (j_compress_ptr cinfo)
   1.405 +{
   1.406 +  my_master_ptr master = (my_master_ptr) cinfo->master;
   1.407 +
   1.408 +  switch (master->pass_type) {
   1.409 +  case main_pass:
   1.410 +    /* Initial pass: will collect input data, and do either Huffman
   1.411 +     * optimization or data output for the first scan.
   1.412 +     */
   1.413 +    select_scan_parameters(cinfo);
   1.414 +    per_scan_setup(cinfo);
   1.415 +    if (! cinfo->raw_data_in) {
   1.416 +      (*cinfo->cconvert->start_pass) (cinfo);
   1.417 +      (*cinfo->downsample->start_pass) (cinfo);
   1.418 +      (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
   1.419 +    }
   1.420 +    (*cinfo->fdct->start_pass) (cinfo);
   1.421 +    (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
   1.422 +    (*cinfo->coef->start_pass) (cinfo,
   1.423 +				(master->total_passes > 1 ?
   1.424 +				 JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
   1.425 +    (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
   1.426 +    if (cinfo->optimize_coding) {
   1.427 +      /* No immediate data output; postpone writing frame/scan headers */
   1.428 +      master->pub.call_pass_startup = FALSE;
   1.429 +    } else {
   1.430 +      /* Will write frame/scan headers at first jpeg_write_scanlines call */
   1.431 +      master->pub.call_pass_startup = TRUE;
   1.432 +    }
   1.433 +    break;
   1.434 +#ifdef ENTROPY_OPT_SUPPORTED
   1.435 +  case huff_opt_pass:
   1.436 +    /* Do Huffman optimization for a scan after the first one. */
   1.437 +    select_scan_parameters(cinfo);
   1.438 +    per_scan_setup(cinfo);
   1.439 +    if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {
   1.440 +      (*cinfo->entropy->start_pass) (cinfo, TRUE);
   1.441 +      (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
   1.442 +      master->pub.call_pass_startup = FALSE;
   1.443 +      break;
   1.444 +    }
   1.445 +    /* Special case: Huffman DC refinement scans need no Huffman table
   1.446 +     * and therefore we can skip the optimization pass for them.
   1.447 +     */
   1.448 +    master->pass_type = output_pass;
   1.449 +    master->pass_number++;
   1.450 +    /*FALLTHROUGH*/
   1.451 +#endif
   1.452 +  case output_pass:
   1.453 +    /* Do a data-output pass. */
   1.454 +    /* We need not repeat per-scan setup if prior optimization pass did it. */
   1.455 +    if (! cinfo->optimize_coding) {
   1.456 +      select_scan_parameters(cinfo);
   1.457 +      per_scan_setup(cinfo);
   1.458 +    }
   1.459 +    (*cinfo->entropy->start_pass) (cinfo, FALSE);
   1.460 +    (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
   1.461 +    /* We emit frame/scan headers now */
   1.462 +    if (master->scan_number == 0)
   1.463 +      (*cinfo->marker->write_frame_header) (cinfo);
   1.464 +    (*cinfo->marker->write_scan_header) (cinfo);
   1.465 +    master->pub.call_pass_startup = FALSE;
   1.466 +    break;
   1.467 +  default:
   1.468 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
   1.469 +  }
   1.470 +
   1.471 +  master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
   1.472 +
   1.473 +  /* Set up progress monitor's pass info if present */
   1.474 +  if (cinfo->progress != NULL) {
   1.475 +    cinfo->progress->completed_passes = master->pass_number;
   1.476 +    cinfo->progress->total_passes = master->total_passes;
   1.477 +  }
   1.478 +}
   1.479 +
   1.480 +
   1.481 +/*
   1.482 + * Special start-of-pass hook.
   1.483 + * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
   1.484 + * In single-pass processing, we need this hook because we don't want to
   1.485 + * write frame/scan headers during jpeg_start_compress; we want to let the
   1.486 + * application write COM markers etc. between jpeg_start_compress and the
   1.487 + * jpeg_write_scanlines loop.
   1.488 + * In multi-pass processing, this routine is not used.
   1.489 + */
   1.490 +
   1.491 +METHODDEF(void)
   1.492 +pass_startup (j_compress_ptr cinfo)
   1.493 +{
   1.494 +  cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
   1.495 +
   1.496 +  (*cinfo->marker->write_frame_header) (cinfo);
   1.497 +  (*cinfo->marker->write_scan_header) (cinfo);
   1.498 +}
   1.499 +
   1.500 +
   1.501 +/*
   1.502 + * Finish up at end of pass.
   1.503 + */
   1.504 +
   1.505 +METHODDEF(void)
   1.506 +finish_pass_master (j_compress_ptr cinfo)
   1.507 +{
   1.508 +  my_master_ptr master = (my_master_ptr) cinfo->master;
   1.509 +
   1.510 +  /* The entropy coder always needs an end-of-pass call,
   1.511 +   * either to analyze statistics or to flush its output buffer.
   1.512 +   */
   1.513 +  (*cinfo->entropy->finish_pass) (cinfo);
   1.514 +
   1.515 +  /* Update state for next pass */
   1.516 +  switch (master->pass_type) {
   1.517 +  case main_pass:
   1.518 +    /* next pass is either output of scan 0 (after optimization)
   1.519 +     * or output of scan 1 (if no optimization).
   1.520 +     */
   1.521 +    master->pass_type = output_pass;
   1.522 +    if (! cinfo->optimize_coding)
   1.523 +      master->scan_number++;
   1.524 +    break;
   1.525 +  case huff_opt_pass:
   1.526 +    /* next pass is always output of current scan */
   1.527 +    master->pass_type = output_pass;
   1.528 +    break;
   1.529 +  case output_pass:
   1.530 +    /* next pass is either optimization or output of next scan */
   1.531 +    if (cinfo->optimize_coding)
   1.532 +      master->pass_type = huff_opt_pass;
   1.533 +    master->scan_number++;
   1.534 +    break;
   1.535 +  }
   1.536 +
   1.537 +  master->pass_number++;
   1.538 +}
   1.539 +
   1.540 +
   1.541 +/*
   1.542 + * Initialize master compression control.
   1.543 + */
   1.544 +
   1.545 +GLOBAL(void)
   1.546 +jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
   1.547 +{
   1.548 +  my_master_ptr master;
   1.549 +
   1.550 +  master = (my_master_ptr)
   1.551 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.552 +				  SIZEOF(my_comp_master));
   1.553 +  cinfo->master = (struct jpeg_comp_master *) master;
   1.554 +  master->pub.prepare_for_pass = prepare_for_pass;
   1.555 +  master->pub.pass_startup = pass_startup;
   1.556 +  master->pub.finish_pass = finish_pass_master;
   1.557 +  master->pub.is_last_pass = FALSE;
   1.558 +
   1.559 +  /* Validate parameters, determine derived values */
   1.560 +  initial_setup(cinfo);
   1.561 +
   1.562 +  if (cinfo->scan_info != NULL) {
   1.563 +#ifdef C_MULTISCAN_FILES_SUPPORTED
   1.564 +    validate_script(cinfo);
   1.565 +#else
   1.566 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
   1.567 +#endif
   1.568 +  } else {
   1.569 +    cinfo->progressive_mode = FALSE;
   1.570 +    cinfo->num_scans = 1;
   1.571 +  }
   1.572 +
   1.573 +  if (cinfo->progressive_mode)	/*  TEMPORARY HACK ??? */
   1.574 +    cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */
   1.575 +
   1.576 +  /* Initialize my private state */
   1.577 +  if (transcode_only) {
   1.578 +    /* no main pass in transcoding */
   1.579 +    if (cinfo->optimize_coding)
   1.580 +      master->pass_type = huff_opt_pass;
   1.581 +    else
   1.582 +      master->pass_type = output_pass;
   1.583 +  } else {
   1.584 +    /* for normal compression, first pass is always this type: */
   1.585 +    master->pass_type = main_pass;
   1.586 +  }
   1.587 +  master->scan_number = 0;
   1.588 +  master->pass_number = 0;
   1.589 +  if (cinfo->optimize_coding)
   1.590 +    master->total_passes = cinfo->num_scans * 2;
   1.591 +  else
   1.592 +    master->total_passes = cinfo->num_scans;
   1.593 +}