PageRenderTime 77ms CodeModel.GetById 9ms app.highlight 62ms RepoModel.GetById 1ms app.codeStats 0ms

/media/libjpeg/jcmaster.c

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