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