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