1 /*
2  * jdmaster.c
3  *
4  * Copyright (C) 1991-1994, 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 decompressor.
9  * These routines are concerned with selecting the modules to be executed
10  * and with determining the number of passes and the work to be done in each
11  * 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,		/* read and process a single-scan file */
23 	preread_pass,		/* read one scan of a multi-scan file */
24 	output_pass,		/* primary processing pass for multi-scan */
25 	post_pass		/* optional post-pass for 2-pass quant. */
26 } D_PASS_TYPE;
27 
28 typedef struct {
29   struct jpeg_decomp_master pub; /* public fields */
30 
31   boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
32 
33   D_PASS_TYPE pass_type;	/* the type of the current pass */
34 
35   int pass_number;		/* # of passes completed */
36   int total_passes;		/* estimated total # of passes needed */
37 
38   boolean need_post_pass;	/* are we using full two-pass quantization? */
39 } my_decomp_master;
40 
41 typedef my_decomp_master * my_master_ptr;
42 
43 
44 /*
45  * Determine whether merged upsample/color conversion should be used.
46  * CRUCIAL: this must match the actual capabilities of jdmerge.c!
47  */
48 
49 LOCAL boolean
use_merged_upsample(j_decompress_ptr cinfo)50 use_merged_upsample (j_decompress_ptr cinfo)
51 {
52 #ifdef UPSAMPLE_MERGING_SUPPORTED
53   /* Merging is the equivalent of plain box-filter upsampling */
54   if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
55     return FALSE;
56   /* jdmerge.c only supports YCC=>RGB color conversion */
57   if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
58       cinfo->out_color_space != JCS_RGB ||
59       cinfo->out_color_components != RGB_PIXELSIZE)
60     return FALSE;
61   /* and it only handles 2h1v or 2h2v sampling ratios */
62   if (cinfo->comp_info[0].h_samp_factor != 2 ||
63       cinfo->comp_info[1].h_samp_factor != 1 ||
64       cinfo->comp_info[2].h_samp_factor != 1 ||
65       cinfo->comp_info[0].v_samp_factor >  2 ||
66       cinfo->comp_info[1].v_samp_factor != 1 ||
67       cinfo->comp_info[2].v_samp_factor != 1)
68     return FALSE;
69   /* furthermore, it doesn't work if we've scaled the IDCTs differently */
70   if (cinfo->comp_info[0].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
71       cinfo->comp_info[1].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
72       cinfo->comp_info[2].DCT_scaled_size != cinfo->min_DCT_scaled_size)
73     return FALSE;
74   /* ??? also need to test for upsample-time rescaling, when & if supported */
75   /* by golly, it'll work... */
76   return TRUE;
77 #else
78   return FALSE;
79 #endif
80 }
81 
82 
83 /*
84  * Support routines that do various essential calculations.
85  *
86  * jpeg_calc_output_dimensions is exported for possible use by application.
87  * Hence it mustn't do anything that can't be done twice.
88  */
89 
90 GLOBAL void
jpeg_calc_output_dimensions(j_decompress_ptr cinfo)91 jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
92 /* Do computations that are needed before master selection phase */
93 {
94   int ci;
95   jpeg_component_info *compptr;
96 
97   if (cinfo->num_components == 1) {
98     /* subsampling is a no-op, so it's safe to ignore it */
99     cinfo->comp_info[0].h_samp_factor = 1;
100     cinfo->comp_info[0].v_samp_factor = 1;
101   }
102 
103   /* Compute maximum sampling factors; check factor validity */
104   cinfo->max_h_samp_factor = 1;
105   cinfo->max_v_samp_factor = 1;
106   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
107        ci++, compptr++) {
108     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
109 	compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
110       ERREXIT(cinfo, JERR_BAD_SAMPLING);
111     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
112 				   compptr->h_samp_factor);
113     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
114 				   compptr->v_samp_factor);
115   }
116 
117   /* Compute actual output image dimensions and DCT scaling choices. */
118 #ifdef IDCT_SCALING_SUPPORTED
119   if (cinfo->scale_num * 8 <= cinfo->scale_denom) {
120     /* Provide 1/8 scaling */
121     cinfo->output_width = (JDIMENSION)
122       jdiv_round_up((long) cinfo->image_width, 8L);
123     cinfo->output_height = (JDIMENSION)
124       jdiv_round_up((long) cinfo->image_height, 8L);
125     cinfo->min_DCT_scaled_size = 1;
126   } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {
127     /* Provide 1/4 scaling */
128     cinfo->output_width = (JDIMENSION)
129       jdiv_round_up((long) cinfo->image_width, 4L);
130     cinfo->output_height = (JDIMENSION)
131       jdiv_round_up((long) cinfo->image_height, 4L);
132     cinfo->min_DCT_scaled_size = 2;
133   } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) {
134     /* Provide 1/2 scaling */
135     cinfo->output_width = (JDIMENSION)
136       jdiv_round_up((long) cinfo->image_width, 2L);
137     cinfo->output_height = (JDIMENSION)
138       jdiv_round_up((long) cinfo->image_height, 2L);
139     cinfo->min_DCT_scaled_size = 4;
140   } else {
141     /* Provide 1/1 scaling */
142     cinfo->output_width = cinfo->image_width;
143     cinfo->output_height = cinfo->image_height;
144     cinfo->min_DCT_scaled_size = DCTSIZE;
145   }
146   /* In selecting the actual DCT scaling for each component, we try to
147    * scale up the chroma components via IDCT scaling rather than upsampling.
148    * This saves time if the upsampler gets to use 1:1 scaling.
149    * Note this code assumes that the supported DCT scalings are powers of 2.
150    */
151   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
152        ci++, compptr++) {
153     int ssize = cinfo->min_DCT_scaled_size;
154     while (ssize < DCTSIZE &&
155 	   (compptr->h_samp_factor * ssize * 2 <=
156 	    cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) &&
157 	   (compptr->v_samp_factor * ssize * 2 <=
158 	    cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) {
159       ssize = ssize * 2;
160     }
161     compptr->DCT_scaled_size = ssize;
162   }
163 #else /* !IDCT_SCALING_SUPPORTED */
164   /* Hardwire it to "no scaling" */
165   cinfo->output_width = cinfo->image_width;
166   cinfo->output_height = cinfo->image_height;
167   cinfo->min_DCT_scaled_size = DCTSIZE;
168   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
169        ci++, compptr++) {
170     compptr->DCT_scaled_size = DCTSIZE;
171   }
172 #endif /* IDCT_SCALING_SUPPORTED */
173 
174   /* Report number of components in selected colorspace. */
175   /* Probably this should be in the color conversion module... */
176   switch (cinfo->out_color_space) {
177   case JCS_GRAYSCALE:
178     cinfo->out_color_components = 1;
179     break;
180   case JCS_RGB:
181 #if RGB_PIXELSIZE != 3
182     cinfo->out_color_components = RGB_PIXELSIZE;
183     break;
184 #endif /* else share code with YCbCr */
185   case JCS_YCbCr:
186     cinfo->out_color_components = 3;
187     break;
188   case JCS_CMYK:
189   case JCS_YCCK:
190     cinfo->out_color_components = 4;
191     break;
192   default:			/* else must be same colorspace as in file */
193     cinfo->out_color_components = cinfo->num_components;
194     break;
195   }
196   cinfo->output_components = (cinfo->quantize_colors ? 1 :
197 			      cinfo->out_color_components);
198 
199   /* See if upsampler will want to emit more than one row at a time */
200   if (use_merged_upsample(cinfo))
201     cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
202   else
203     cinfo->rec_outbuf_height = 1;
204 
205   /* Compute various sampling-related dimensions.
206    * Some of these are of interest to the application if it is dealing with
207    * "raw" (not upsampled) output, so we do the calculations here.
208    */
209 
210   /* Compute dimensions of components */
211   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
212        ci++, compptr++) {
213     /* Size in DCT blocks */
214     compptr->width_in_blocks = (JDIMENSION)
215       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
216 		    (long) (cinfo->max_h_samp_factor * DCTSIZE));
217     compptr->height_in_blocks = (JDIMENSION)
218       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
219 		    (long) (cinfo->max_v_samp_factor * DCTSIZE));
220     /* Size in samples, after IDCT scaling */
221     compptr->downsampled_width = (JDIMENSION)
222       jdiv_round_up((long) cinfo->image_width *
223 		    (long) (compptr->h_samp_factor * compptr->DCT_scaled_size),
224 		    (long) (cinfo->max_h_samp_factor * DCTSIZE));
225     compptr->downsampled_height = (JDIMENSION)
226       jdiv_round_up((long) cinfo->image_height *
227 		    (long) (compptr->v_samp_factor * compptr->DCT_scaled_size),
228 		    (long) (cinfo->max_v_samp_factor * DCTSIZE));
229     /* Mark component needed, until color conversion says otherwise */
230     compptr->component_needed = TRUE;
231   }
232 
233   /* Compute number of fully interleaved MCU rows (number of times that
234    * main controller will call coefficient controller).
235    */
236   cinfo->total_iMCU_rows = (JDIMENSION)
237     jdiv_round_up((long) cinfo->image_height,
238 		  (long) (cinfo->max_v_samp_factor*DCTSIZE));
239 }
240 
241 
242 LOCAL void
per_scan_setup(j_decompress_ptr cinfo)243 per_scan_setup (j_decompress_ptr cinfo)
244 /* Do computations that are needed before processing a JPEG scan */
245 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
246 {
247   int ci, mcublks, tmp;
248   jpeg_component_info *compptr;
249 
250   if (cinfo->comps_in_scan == 1) {
251 
252     /* Noninterleaved (single-component) scan */
253     compptr = cinfo->cur_comp_info[0];
254 
255     /* Overall image size in MCUs */
256     cinfo->MCUs_per_row = compptr->width_in_blocks;
257     cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
258 
259     /* For noninterleaved scan, always one block per MCU */
260     compptr->MCU_width = 1;
261     compptr->MCU_height = 1;
262     compptr->MCU_blocks = 1;
263     compptr->MCU_sample_width = compptr->DCT_scaled_size;
264     compptr->last_col_width = 1;
265     compptr->last_row_height = 1;
266 
267     /* Prepare array describing MCU composition */
268     cinfo->blocks_in_MCU = 1;
269     cinfo->MCU_membership[0] = 0;
270 
271   } else {
272 
273     /* Interleaved (multi-component) scan */
274     if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
275       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
276 	       MAX_COMPS_IN_SCAN);
277 
278     /* Overall image size in MCUs */
279     cinfo->MCUs_per_row = (JDIMENSION)
280       jdiv_round_up((long) cinfo->image_width,
281 		    (long) (cinfo->max_h_samp_factor*DCTSIZE));
282     cinfo->MCU_rows_in_scan = (JDIMENSION)
283       jdiv_round_up((long) cinfo->image_height,
284 		    (long) (cinfo->max_v_samp_factor*DCTSIZE));
285 
286     cinfo->blocks_in_MCU = 0;
287 
288     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
289       compptr = cinfo->cur_comp_info[ci];
290       /* Sampling factors give # of blocks of component in each MCU */
291       compptr->MCU_width = compptr->h_samp_factor;
292       compptr->MCU_height = compptr->v_samp_factor;
293       compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
294       compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;
295       /* Figure number of non-dummy blocks in last MCU column & row */
296       tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
297       if (tmp == 0) tmp = compptr->MCU_width;
298       compptr->last_col_width = tmp;
299       tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
300       if (tmp == 0) tmp = compptr->MCU_height;
301       compptr->last_row_height = tmp;
302       /* Prepare array describing MCU composition */
303       mcublks = compptr->MCU_blocks;
304       if (cinfo->blocks_in_MCU + mcublks > MAX_BLOCKS_IN_MCU)
305 	ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
306       while (mcublks-- > 0) {
307 	cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
308       }
309     }
310 
311   }
312 }
313 
314 
315 /*
316  * Several decompression processes need to range-limit values to the range
317  * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
318  * due to noise introduced by quantization, roundoff error, etc.  These
319  * processes are inner loops and need to be as fast as possible.  On most
320  * machines, particularly CPUs with pipelines or instruction prefetch,
321  * a (subscript-check-less) C table lookup
322  *		x = sample_range_limit[x];
323  * is faster than explicit tests
324  *		if (x < 0)  x = 0;
325  *		else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;
326  * These processes all use a common table prepared by the routine below.
327  *
328  * For most steps we can mathematically guarantee that the initial value
329  * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
330  * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient.  But for the initial
331  * limiting step (just after the IDCT), a wildly out-of-range value is
332  * possible if the input data is corrupt.  To avoid any chance of indexing
333  * off the end of memory and getting a bad-pointer trap, we perform the
334  * post-IDCT limiting thus:
335  *		x = range_limit[x & MASK];
336  * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
337  * samples.  Under normal circumstances this is more than enough range and
338  * a correct output will be generated; with bogus input data the mask will
339  * cause wraparound, and we will safely generate a bogus-but-in-range output.
340  * For the post-IDCT step, we want to convert the data from signed to unsigned
341  * representation by adding CENTERJSAMPLE at the same time that we limit it.
342  * So the post-IDCT limiting table ends up looking like this:
343  *   CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
344  *   MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
345  *   0          (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
346  *   0,1,...,CENTERJSAMPLE-1
347  * Negative inputs select values from the upper half of the table after
348  * masking.
349  *
350  * We can save some space by overlapping the start of the post-IDCT table
351  * with the simpler range limiting table.  The post-IDCT table begins at
352  * sample_range_limit + CENTERJSAMPLE.
353  *
354  * Note that the table is allocated in near data space on PCs; it's small
355  * enough and used often enough to justify this.
356  */
357 
358 LOCAL void
prepare_range_limit_table(j_decompress_ptr cinfo)359 prepare_range_limit_table (j_decompress_ptr cinfo)
360 /* Allocate and fill in the sample_range_limit table */
361 {
362   JSAMPLE * table;
363   int i;
364 
365   table = (JSAMPLE *)
366     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
367 		(5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
368   table += (MAXJSAMPLE+1);	/* allow negative subscripts of simple table */
369   cinfo->sample_range_limit = table;
370   /* First segment of "simple" table: limit[x] = 0 for x < 0 */
371   MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
372   /* Main part of "simple" table: limit[x] = x */
373   for (i = 0; i <= MAXJSAMPLE; i++)
374     table[i] = (JSAMPLE) i;
375   table += CENTERJSAMPLE;	/* Point to where post-IDCT table starts */
376   /* End of simple table, rest of first half of post-IDCT table */
377   for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
378     table[i] = MAXJSAMPLE;
379   /* Second half of post-IDCT table */
380   MEMZERO(table + (2 * (MAXJSAMPLE+1)),
381 	  (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
382   MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
383 	  cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
384 }
385 
386 
387 /*
388  * Master selection of decompression modules.
389  * This is done once at the start of processing an image.  We determine
390  * which modules will be used and give them appropriate initialization calls.
391  *
392  * Note that this is called only after jpeg_read_header has finished.
393  * We therefore know what is in the SOF and (first) SOS markers.
394  */
395 
396 LOCAL void
master_selection(j_decompress_ptr cinfo)397 master_selection (j_decompress_ptr cinfo)
398 {
399   my_master_ptr master = (my_master_ptr) cinfo->master;
400   long samplesperrow;
401   JDIMENSION jd_samplesperrow;
402 
403   /* Initialize dimensions and other stuff */
404   jpeg_calc_output_dimensions(cinfo);
405   prepare_range_limit_table(cinfo);
406 
407   /* Width of an output scanline must be representable as JDIMENSION. */
408   samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
409   jd_samplesperrow = (JDIMENSION) samplesperrow;
410   if ((long) jd_samplesperrow != samplesperrow)
411     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
412 
413   /* Initialize my private state */
414   master->pub.eoi_processed = FALSE;
415   master->pass_number = 0;
416   master->need_post_pass = FALSE;
417   if (cinfo->comps_in_scan == cinfo->num_components) {
418     master->pass_type = main_pass;
419     master->total_passes = 1;
420   } else {
421 #ifdef D_MULTISCAN_FILES_SUPPORTED
422     master->pass_type = preread_pass;
423     /* Assume there is a separate scan for each component; */
424     /* if partially interleaved, we'll increment pass_number appropriately */
425     master->total_passes = cinfo->num_components + 1;
426 #else
427     ERREXIT(cinfo, JERR_NOT_COMPILED);
428 #endif
429   }
430   master->using_merged_upsample = use_merged_upsample(cinfo);
431 
432   /* There's not a lot of smarts here right now, but it'll get more
433    * complicated when we have multiple implementations available...
434    */
435 
436   /* Color quantizer selection */
437   if (cinfo->quantize_colors) {
438     if (cinfo->raw_data_out)
439       ERREXIT(cinfo, JERR_NOTIMPL);
440 #ifdef QUANT_2PASS_SUPPORTED
441     /* 2-pass quantizer only works in 3-component color space.
442      * We use the "2-pass" code in a single pass if a colormap is given.
443      */
444     if (cinfo->out_color_components != 3)
445       cinfo->two_pass_quantize = FALSE;
446     else if (cinfo->colormap != NULL)
447       cinfo->two_pass_quantize = TRUE;
448 #else
449     /* Force 1-pass quantize if we don't have 2-pass code compiled. */
450     cinfo->two_pass_quantize = FALSE;
451 #endif
452 
453     if (cinfo->two_pass_quantize) {
454 #ifdef QUANT_2PASS_SUPPORTED
455       if (cinfo->colormap == NULL) {
456 	master->need_post_pass = TRUE;
457 	master->total_passes++;
458       }
459       jinit_2pass_quantizer(cinfo);
460 #else
461       ERREXIT(cinfo, JERR_NOT_COMPILED);
462 #endif
463     } else {
464 #ifdef QUANT_1PASS_SUPPORTED
465       jinit_1pass_quantizer(cinfo);
466 #else
467       ERREXIT(cinfo, JERR_NOT_COMPILED);
468 #endif
469     }
470   }
471 
472   /* Post-processing: in particular, color conversion first */
473   if (! cinfo->raw_data_out) {
474     if (master->using_merged_upsample) {
475 #ifdef UPSAMPLE_MERGING_SUPPORTED
476       jinit_merged_upsampler(cinfo); /* does color conversion too */
477 #else
478       ERREXIT(cinfo, JERR_NOT_COMPILED);
479 #endif
480     } else {
481       jinit_color_deconverter(cinfo);
482       jinit_upsampler(cinfo);
483     }
484     jinit_d_post_controller(cinfo, master->need_post_pass);
485   }
486   /* Inverse DCT */
487   jinit_inverse_dct(cinfo);
488   /* Entropy decoding: either Huffman or arithmetic coding. */
489   if (cinfo->arith_code) {
490 #ifdef D_ARITH_CODING_SUPPORTED
491     jinit_arith_decoder(cinfo);
492 #else
493     ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
494 #endif
495   } else
496     jinit_huff_decoder(cinfo);
497 
498   jinit_d_coef_controller(cinfo, (master->pass_type == preread_pass));
499   jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
500   /* Note that main controller is initialized even in raw-data mode. */
501 
502   /* We can now tell the memory manager to allocate virtual arrays. */
503   (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
504 }
505 
506 
507 /*
508  * Per-pass setup.
509  * This is called at the beginning of each pass.  We determine which modules
510  * will be active during this pass and give them appropriate start_pass calls.
511  * We also set is_last_pass to indicate whether any more passes will be
512  * required.
513  */
514 
515 METHODDEF void
prepare_for_pass(j_decompress_ptr cinfo)516 prepare_for_pass (j_decompress_ptr cinfo)
517 {
518   my_master_ptr master = (my_master_ptr) cinfo->master;
519 
520   switch (master->pass_type) {
521   case main_pass:
522     /* Set up to read and decompress single-scan file in one pass */
523     per_scan_setup(cinfo);
524     master->pub.is_last_pass = ! master->need_post_pass;
525     if (! cinfo->raw_data_out) {
526       if (! master->using_merged_upsample)
527 	(*cinfo->cconvert->start_pass) (cinfo);
528       (*cinfo->upsample->start_pass) (cinfo);
529       if (cinfo->quantize_colors)
530 	(*cinfo->cquantize->start_pass) (cinfo, master->need_post_pass);
531       (*cinfo->post->start_pass) (cinfo,
532 	    (master->need_post_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
533     }
534     (*cinfo->idct->start_input_pass) (cinfo);
535     (*cinfo->idct->start_output_pass) (cinfo);
536     (*cinfo->entropy->start_pass) (cinfo);
537     (*cinfo->coef->start_pass) (cinfo, JBUF_PASS_THRU);
538     (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
539     break;
540 #ifdef D_MULTISCAN_FILES_SUPPORTED
541   case preread_pass:
542     /* Read (another) scan of a multi-scan file */
543     per_scan_setup(cinfo);
544     master->pub.is_last_pass = FALSE;
545     (*cinfo->idct->start_input_pass) (cinfo);
546     (*cinfo->entropy->start_pass) (cinfo);
547     (*cinfo->coef->start_pass) (cinfo, JBUF_SAVE_SOURCE);
548     (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_SOURCE);
549     break;
550   case output_pass:
551     /* All scans read, now do the IDCT and subsequent processing */
552     master->pub.is_last_pass = ! master->need_post_pass;
553     if (! cinfo->raw_data_out) {
554       if (! master->using_merged_upsample)
555 	(*cinfo->cconvert->start_pass) (cinfo);
556       (*cinfo->upsample->start_pass) (cinfo);
557       if (cinfo->quantize_colors)
558 	(*cinfo->cquantize->start_pass) (cinfo, master->need_post_pass);
559       (*cinfo->post->start_pass) (cinfo,
560 	    (master->need_post_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
561     }
562     (*cinfo->idct->start_output_pass) (cinfo);
563     (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
564     (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
565     break;
566 #endif /* D_MULTISCAN_FILES_SUPPORTED */
567 #ifdef QUANT_2PASS_SUPPORTED
568   case post_pass:
569     /* Final pass of 2-pass quantization */
570     master->pub.is_last_pass = TRUE;
571     (*cinfo->cquantize->start_pass) (cinfo, FALSE);
572     (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
573     (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
574     break;
575 #endif /* QUANT_2PASS_SUPPORTED */
576   default:
577     ERREXIT(cinfo, JERR_NOT_COMPILED);
578   }
579 
580   /* Set up progress monitor's pass info if present */
581   if (cinfo->progress != NULL) {
582     cinfo->progress->completed_passes = master->pass_number;
583     cinfo->progress->total_passes = master->total_passes;
584   }
585 }
586 
587 
588 /*
589  * Finish up at end of pass.
590  * In multi-scan mode, we must read next scan header and set the next
591  * pass_type correctly for prepare_for_pass.
592  */
593 
594 METHODDEF void
finish_pass_master(j_decompress_ptr cinfo)595 finish_pass_master (j_decompress_ptr cinfo)
596 {
597   my_master_ptr master = (my_master_ptr) cinfo->master;
598 
599   switch (master->pass_type) {
600   case main_pass:
601   case output_pass:
602     if (cinfo->quantize_colors)
603       (*cinfo->cquantize->finish_pass) (cinfo);
604     master->pass_number++;
605     master->pass_type = post_pass; /* in case need_post_pass is true */
606     break;
607 #ifdef D_MULTISCAN_FILES_SUPPORTED
608   case preread_pass:
609     /* Count one pass done for each component in this scan */
610     master->pass_number += cinfo->comps_in_scan;
611     switch ((*cinfo->marker->read_markers) (cinfo)) {
612     case JPEG_HEADER_OK:	/* Found SOS, do another preread pass */
613       break;
614     case JPEG_HEADER_TABLES_ONLY: /* Found EOI, no more preread passes */
615       master->pub.eoi_processed = TRUE;
616       master->pass_type = output_pass;
617       break;
618     case JPEG_SUSPENDED:
619       ERREXIT(cinfo, JERR_CANT_SUSPEND);
620     }
621     break;
622 #endif /* D_MULTISCAN_FILES_SUPPORTED */
623 #ifdef QUANT_2PASS_SUPPORTED
624   case post_pass:
625     (*cinfo->cquantize->finish_pass) (cinfo);
626     /* there will be no more passes, don't bother to change state */
627     break;
628 #endif /* QUANT_2PASS_SUPPORTED */
629   default:
630     ERREXIT(cinfo, JERR_NOT_COMPILED);
631   }
632 }
633 
634 
635 /*
636  * Initialize master decompression control.
637  * This creates my own subrecord and also performs the master selection phase,
638  * which causes other modules to create their subrecords.
639  */
640 
641 GLOBAL void
jinit_master_decompress(j_decompress_ptr cinfo)642 jinit_master_decompress (j_decompress_ptr cinfo)
643 {
644   my_master_ptr master;
645 
646   master = (my_master_ptr)
647       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
648 				  SIZEOF(my_decomp_master));
649   cinfo->master = (struct jpeg_decomp_master *) master;
650   master->pub.prepare_for_pass = prepare_for_pass;
651   master->pub.finish_pass = finish_pass_master;
652 
653   master_selection(cinfo);
654 }
655