/src/freeimage-svn/FreeImage/trunk/Source/LibJPEG/jdmaster.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * jdmaster.c  | 
3  |  |  *  | 
4  |  |  * Copyright (C) 1991-1997, Thomas G. Lane.  | 
5  |  |  * Modified 2002-2019 by Guido Vollbeding.  | 
6  |  |  * This file is part of the Independent JPEG Group's software.  | 
7  |  |  * For conditions of distribution and use, see the accompanying README file.  | 
8  |  |  *  | 
9  |  |  * This file contains master control logic for the JPEG decompressor.  | 
10  |  |  * These routines are concerned with selecting the modules to be executed  | 
11  |  |  * and with determining the number of passes and the work to be done in each  | 
12  |  |  * pass.  | 
13  |  |  */  | 
14  |  |  | 
15  |  | #define JPEG_INTERNALS  | 
16  |  | #include "jinclude.h"  | 
17  |  | #include "jpeglib.h"  | 
18  |  |  | 
19  |  |  | 
20  |  | /* Private state */  | 
21  |  |  | 
22  |  | typedef struct { | 
23  |  |   struct jpeg_decomp_master pub; /* public fields */  | 
24  |  |  | 
25  |  |   int pass_number;    /* # of passes completed */  | 
26  |  |  | 
27  |  |   boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */  | 
28  |  |  | 
29  |  |   /* Saved references to initialized quantizer modules,  | 
30  |  |    * in case we need to switch modes.  | 
31  |  |    */  | 
32  |  |   struct jpeg_color_quantizer * quantizer_1pass;  | 
33  |  |   struct jpeg_color_quantizer * quantizer_2pass;  | 
34  |  | } my_decomp_master;  | 
35  |  |  | 
36  |  | typedef my_decomp_master * my_master_ptr;  | 
37  |  |  | 
38  |  |  | 
39  |  | /*  | 
40  |  |  * Determine whether merged upsample/color conversion should be used.  | 
41  |  |  * CRUCIAL: this must match the actual capabilities of jdmerge.c!  | 
42  |  |  */  | 
43  |  |  | 
44  |  | LOCAL(boolean)  | 
45  |  | use_merged_upsample (j_decompress_ptr cinfo)  | 
46  | 0  | { | 
47  | 0  | #ifdef UPSAMPLE_MERGING_SUPPORTED  | 
48  |  |   /* Merging is the equivalent of plain box-filter upsampling. */  | 
49  |  |   /* The following condition is only needed if fancy shall select  | 
50  |  |    * a different upsampling method.  In our current implementation  | 
51  |  |    * fancy only affects the DCT scaling, thus we can use fancy  | 
52  |  |    * upsampling and merged upsample simultaneously, in particular  | 
53  |  |    * with scaled DCT sizes larger than the default DCTSIZE.  | 
54  |  |    */  | 
55  |  | #if 0  | 
56  |  |   if (cinfo->do_fancy_upsampling)  | 
57  |  |     return FALSE;  | 
58  |  | #endif  | 
59  | 0  |   if (cinfo->CCIR601_sampling)  | 
60  | 0  |     return FALSE;  | 
61  |  |   /* jdmerge.c only supports YCC=>RGB color conversion */  | 
62  | 0  |   if ((cinfo->jpeg_color_space != JCS_YCbCr &&  | 
63  | 0  |        cinfo->jpeg_color_space != JCS_BG_YCC) ||  | 
64  | 0  |       cinfo->num_components != 3 ||  | 
65  | 0  |       cinfo->out_color_space != JCS_RGB ||  | 
66  | 0  |       cinfo->out_color_components != RGB_PIXELSIZE ||  | 
67  | 0  |       cinfo->color_transform)  | 
68  | 0  |     return FALSE;  | 
69  |  |   /* and it only handles 2h1v or 2h2v sampling ratios */  | 
70  | 0  |   if (cinfo->comp_info[0].h_samp_factor != 2 ||  | 
71  | 0  |       cinfo->comp_info[1].h_samp_factor != 1 ||  | 
72  | 0  |       cinfo->comp_info[2].h_samp_factor != 1 ||  | 
73  | 0  |       cinfo->comp_info[0].v_samp_factor >  2 ||  | 
74  | 0  |       cinfo->comp_info[1].v_samp_factor != 1 ||  | 
75  | 0  |       cinfo->comp_info[2].v_samp_factor != 1)  | 
76  | 0  |     return FALSE;  | 
77  |  |   /* furthermore, it doesn't work if we've scaled the IDCTs differently */  | 
78  | 0  |   if (cinfo->comp_info[0].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||  | 
79  | 0  |       cinfo->comp_info[1].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||  | 
80  | 0  |       cinfo->comp_info[2].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||  | 
81  | 0  |       cinfo->comp_info[0].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size ||  | 
82  | 0  |       cinfo->comp_info[1].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size ||  | 
83  | 0  |       cinfo->comp_info[2].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size)  | 
84  | 0  |     return FALSE;  | 
85  |  |   /* ??? also need to test for upsample-time rescaling, when & if supported */  | 
86  | 0  |   return TRUE;     /* by golly, it'll work... */  | 
87  |  | #else  | 
88  |  |   return FALSE;  | 
89  |  | #endif  | 
90  | 0  | }  | 
91  |  |  | 
92  |  |  | 
93  |  | /*  | 
94  |  |  * Compute output image dimensions and related values.  | 
95  |  |  * NOTE: this is exported for possible use by application.  | 
96  |  |  * Hence it mustn't do anything that can't be done twice.  | 
97  |  |  * Also note that it may be called before the master module is initialized!  | 
98  |  |  */  | 
99  |  |  | 
100  |  | GLOBAL(void)  | 
101  |  | jpeg_calc_output_dimensions (j_decompress_ptr cinfo)  | 
102  |  | /* Do computations that are needed before master selection phase.  | 
103  |  |  * This function is used for full decompression.  | 
104  |  |  */  | 
105  | 0  | { | 
106  | 0  | #ifdef IDCT_SCALING_SUPPORTED  | 
107  | 0  |   int ci, ssize;  | 
108  | 0  |   jpeg_component_info *compptr;  | 
109  | 0  | #endif  | 
110  |  |  | 
111  |  |   /* Prevent application from calling me at wrong times */  | 
112  | 0  |   if (cinfo->global_state != DSTATE_READY)  | 
113  | 0  |     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);  | 
114  |  |  | 
115  |  |   /* Compute core output image dimensions and DCT scaling choices. */  | 
116  | 0  |   jpeg_core_output_dimensions(cinfo);  | 
117  |  | 
  | 
118  | 0  | #ifdef IDCT_SCALING_SUPPORTED  | 
119  |  |  | 
120  |  |   /* In selecting the actual DCT scaling for each component, we try to  | 
121  |  |    * scale up the chroma components via IDCT scaling rather than upsampling.  | 
122  |  |    * This saves time if the upsampler gets to use 1:1 scaling.  | 
123  |  |    * Note this code adapts subsampling ratios which are powers of 2.  | 
124  |  |    */  | 
125  | 0  |   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;  | 
126  | 0  |        ci++, compptr++) { | 
127  | 0  |     ssize = 1;  | 
128  | 0  |     if (! cinfo->raw_data_out)  | 
129  | 0  |       while (cinfo->min_DCT_h_scaled_size * ssize <=  | 
130  | 0  |        (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) &&  | 
131  | 0  |        (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) ==  | 
132  | 0  |        0) { | 
133  | 0  |   ssize = ssize * 2;  | 
134  | 0  |       }  | 
135  | 0  |     compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize;  | 
136  | 0  |     ssize = 1;  | 
137  | 0  |     if (! cinfo->raw_data_out)  | 
138  | 0  |       while (cinfo->min_DCT_v_scaled_size * ssize <=  | 
139  | 0  |        (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) &&  | 
140  | 0  |        (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) ==  | 
141  | 0  |        0) { | 
142  | 0  |   ssize = ssize * 2;  | 
143  | 0  |       }  | 
144  | 0  |     compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize;  | 
145  |  |  | 
146  |  |     /* We don't support IDCT ratios larger than 2. */  | 
147  | 0  |     if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2)  | 
148  | 0  |   compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;  | 
149  | 0  |     else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2)  | 
150  | 0  |   compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;  | 
151  |  |  | 
152  |  |     /* Recompute downsampled dimensions of components;  | 
153  |  |      * application needs to know these if using raw downsampled data.  | 
154  |  |      */  | 
155  |  |     /* Size in samples, after IDCT scaling */  | 
156  | 0  |     compptr->downsampled_width = (JDIMENSION)  | 
157  | 0  |       jdiv_round_up((long) cinfo->image_width *  | 
158  | 0  |         (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size),  | 
159  | 0  |         (long) (cinfo->max_h_samp_factor * cinfo->block_size));  | 
160  | 0  |     compptr->downsampled_height = (JDIMENSION)  | 
161  | 0  |       jdiv_round_up((long) cinfo->image_height *  | 
162  | 0  |         (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size),  | 
163  | 0  |         (long) (cinfo->max_v_samp_factor * cinfo->block_size));  | 
164  | 0  |   }  | 
165  |  | 
  | 
166  | 0  | #endif /* IDCT_SCALING_SUPPORTED */  | 
167  |  |  | 
168  |  |   /* Report number of components in selected colorspace. */  | 
169  |  |   /* Probably this should be in the color conversion module... */  | 
170  | 0  |   switch (cinfo->out_color_space) { | 
171  | 0  |   case JCS_GRAYSCALE:  | 
172  | 0  |     cinfo->out_color_components = 1;  | 
173  | 0  |     break;  | 
174  | 0  |   case JCS_RGB:  | 
175  | 0  |   case JCS_BG_RGB:  | 
176  |  | #if RGB_PIXELSIZE != 3  | 
177  |  |     cinfo->out_color_components = RGB_PIXELSIZE;  | 
178  |  |     break;  | 
179  |  | #endif /* else share code with YCbCr */  | 
180  | 0  |   case JCS_YCbCr:  | 
181  | 0  |   case JCS_BG_YCC:  | 
182  | 0  |     cinfo->out_color_components = 3;  | 
183  | 0  |     break;  | 
184  | 0  |   case JCS_CMYK:  | 
185  | 0  |   case JCS_YCCK:  | 
186  | 0  |     cinfo->out_color_components = 4;  | 
187  | 0  |     break;  | 
188  | 0  |   default:      /* else must be same colorspace as in file */  | 
189  | 0  |     cinfo->out_color_components = cinfo->num_components;  | 
190  | 0  |   }  | 
191  | 0  |   cinfo->output_components = (cinfo->quantize_colors ? 1 :  | 
192  | 0  |             cinfo->out_color_components);  | 
193  |  |  | 
194  |  |   /* See if upsampler will want to emit more than one row at a time */  | 
195  | 0  |   if (use_merged_upsample(cinfo))  | 
196  | 0  |     cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;  | 
197  | 0  |   else  | 
198  | 0  |     cinfo->rec_outbuf_height = 1;  | 
199  | 0  | }  | 
200  |  |  | 
201  |  |  | 
202  |  | /*  | 
203  |  |  * Several decompression processes need to range-limit values to the range  | 
204  |  |  * 0..MAXJSAMPLE; the input value may fall somewhat outside this range  | 
205  |  |  * due to noise introduced by quantization, roundoff error, etc.  These  | 
206  |  |  * processes are inner loops and need to be as fast as possible.  On most  | 
207  |  |  * machines, particularly CPUs with pipelines or instruction prefetch,  | 
208  |  |  * a (subscript-check-less) C table lookup  | 
209  |  |  *    x = sample_range_limit[x];  | 
210  |  |  * is faster than explicit tests  | 
211  |  |  *    if (x < 0)  x = 0;  | 
212  |  |  *    else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;  | 
213  |  |  * These processes all use a common table prepared by the routine below.  | 
214  |  |  *  | 
215  |  |  * For most steps we can mathematically guarantee that the initial value  | 
216  |  |  * of x is within 2*(MAXJSAMPLE+1) of the legal range, so a table running  | 
217  |  |  * from -2*(MAXJSAMPLE+1) to 3*MAXJSAMPLE+2 is sufficient.  But for the  | 
218  |  |  * initial limiting step (just after the IDCT), a wildly out-of-range value  | 
219  |  |  * is possible if the input data is corrupt.  To avoid any chance of indexing  | 
220  |  |  * off the end of memory and getting a bad-pointer trap, we perform the  | 
221  |  |  * post-IDCT limiting thus:  | 
222  |  |  *    x = (sample_range_limit - SUBSET)[(x + CENTER) & MASK];  | 
223  |  |  * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit  | 
224  |  |  * samples.  Under normal circumstances this is more than enough range and  | 
225  |  |  * a correct output will be generated; with bogus input data the mask will  | 
226  |  |  * cause wraparound, and we will safely generate a bogus-but-in-range output.  | 
227  |  |  * For the post-IDCT step, we want to convert the data from signed to unsigned  | 
228  |  |  * representation by adding CENTERJSAMPLE at the same time that we limit it.  | 
229  |  |  * This is accomplished with SUBSET = CENTER - CENTERJSAMPLE.  | 
230  |  |  *  | 
231  |  |  * Note that the table is allocated in near data space on PCs; it's small  | 
232  |  |  * enough and used often enough to justify this.  | 
233  |  |  */  | 
234  |  |  | 
235  |  | LOCAL(void)  | 
236  |  | prepare_range_limit_table (j_decompress_ptr cinfo)  | 
237  |  | /* Allocate and fill in the sample_range_limit table */  | 
238  | 0  | { | 
239  | 0  |   JSAMPLE * table;  | 
240  | 0  |   int i;  | 
241  |  | 
  | 
242  | 0  |   table = (JSAMPLE *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,  | 
243  | 0  |     JPOOL_IMAGE, (RANGE_CENTER * 2 + MAXJSAMPLE + 1) * SIZEOF(JSAMPLE));  | 
244  |  |   /* First segment of range limit table: limit[x] = 0 for x < 0 */  | 
245  | 0  |   MEMZERO(table, RANGE_CENTER * SIZEOF(JSAMPLE));  | 
246  | 0  |   table += RANGE_CENTER; /* allow negative subscripts of table */  | 
247  | 0  |   cinfo->sample_range_limit = table;  | 
248  |  |   /* Main part of range limit table: limit[x] = x */  | 
249  | 0  |   for (i = 0; i <= MAXJSAMPLE; i++)  | 
250  | 0  |     table[i] = (JSAMPLE) i;  | 
251  |  |   /* End of range limit table: limit[x] = MAXJSAMPLE for x > MAXJSAMPLE */  | 
252  | 0  |   for (; i <=  MAXJSAMPLE + RANGE_CENTER; i++)  | 
253  | 0  |     table[i] = MAXJSAMPLE;  | 
254  | 0  | }  | 
255  |  |  | 
256  |  |  | 
257  |  | /*  | 
258  |  |  * Master selection of decompression modules.  | 
259  |  |  * This is done once at jpeg_start_decompress time.  We determine  | 
260  |  |  * which modules will be used and give them appropriate initialization calls.  | 
261  |  |  * We also initialize the decompressor input side to begin consuming data.  | 
262  |  |  *  | 
263  |  |  * Since jpeg_read_header has finished, we know what is in the SOF  | 
264  |  |  * and (first) SOS markers.  We also have all the application parameter  | 
265  |  |  * settings.  | 
266  |  |  */  | 
267  |  |  | 
268  |  | LOCAL(void)  | 
269  |  | master_selection (j_decompress_ptr cinfo)  | 
270  | 0  | { | 
271  | 0  |   my_master_ptr master = (my_master_ptr) cinfo->master;  | 
272  | 0  |   boolean use_c_buffer;  | 
273  | 0  |   long samplesperrow;  | 
274  | 0  |   JDIMENSION jd_samplesperrow;  | 
275  |  |  | 
276  |  |   /* For now, precision must match compiled-in value... */  | 
277  | 0  |   if (cinfo->data_precision != BITS_IN_JSAMPLE)  | 
278  | 0  |     ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);  | 
279  |  |  | 
280  |  |   /* Initialize dimensions and other stuff */  | 
281  | 0  |   jpeg_calc_output_dimensions(cinfo);  | 
282  | 0  |   prepare_range_limit_table(cinfo);  | 
283  |  |  | 
284  |  |   /* Sanity check on image dimensions */  | 
285  | 0  |   if (cinfo->output_height <= 0 || cinfo->output_width <= 0 ||  | 
286  | 0  |       cinfo->out_color_components <= 0)  | 
287  | 0  |     ERREXIT(cinfo, JERR_EMPTY_IMAGE);  | 
288  |  |  | 
289  |  |   /* Width of an output scanline must be representable as JDIMENSION. */  | 
290  | 0  |   samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;  | 
291  | 0  |   jd_samplesperrow = (JDIMENSION) samplesperrow;  | 
292  | 0  |   if ((long) jd_samplesperrow != samplesperrow)  | 
293  | 0  |     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);  | 
294  |  |  | 
295  |  |   /* Initialize my private state */  | 
296  | 0  |   master->pass_number = 0;  | 
297  | 0  |   master->using_merged_upsample = use_merged_upsample(cinfo);  | 
298  |  |  | 
299  |  |   /* Color quantizer selection */  | 
300  | 0  |   master->quantizer_1pass = NULL;  | 
301  | 0  |   master->quantizer_2pass = NULL;  | 
302  |  |   /* No mode changes if not using buffered-image mode. */  | 
303  | 0  |   if (! cinfo->quantize_colors || ! cinfo->buffered_image) { | 
304  | 0  |     cinfo->enable_1pass_quant = FALSE;  | 
305  | 0  |     cinfo->enable_external_quant = FALSE;  | 
306  | 0  |     cinfo->enable_2pass_quant = FALSE;  | 
307  | 0  |   }  | 
308  | 0  |   if (cinfo->quantize_colors) { | 
309  | 0  |     if (cinfo->raw_data_out)  | 
310  | 0  |       ERREXIT(cinfo, JERR_NOTIMPL);  | 
311  |  |     /* 2-pass quantizer only works in 3-component color space. */  | 
312  | 0  |     if (cinfo->out_color_components != 3) { | 
313  | 0  |       cinfo->enable_1pass_quant = TRUE;  | 
314  | 0  |       cinfo->enable_external_quant = FALSE;  | 
315  | 0  |       cinfo->enable_2pass_quant = FALSE;  | 
316  | 0  |       cinfo->colormap = NULL;  | 
317  | 0  |     } else if (cinfo->colormap != NULL) { | 
318  | 0  |       cinfo->enable_external_quant = TRUE;  | 
319  | 0  |     } else if (cinfo->two_pass_quantize) { | 
320  | 0  |       cinfo->enable_2pass_quant = TRUE;  | 
321  | 0  |     } else { | 
322  | 0  |       cinfo->enable_1pass_quant = TRUE;  | 
323  | 0  |     }  | 
324  |  | 
  | 
325  | 0  |     if (cinfo->enable_1pass_quant) { | 
326  | 0  | #ifdef QUANT_1PASS_SUPPORTED  | 
327  | 0  |       jinit_1pass_quantizer(cinfo);  | 
328  | 0  |       master->quantizer_1pass = cinfo->cquantize;  | 
329  |  | #else  | 
330  |  |       ERREXIT(cinfo, JERR_NOT_COMPILED);  | 
331  |  | #endif  | 
332  | 0  |     }  | 
333  |  |  | 
334  |  |     /* We use the 2-pass code to map to external colormaps. */  | 
335  | 0  |     if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) { | 
336  | 0  | #ifdef QUANT_2PASS_SUPPORTED  | 
337  | 0  |       jinit_2pass_quantizer(cinfo);  | 
338  | 0  |       master->quantizer_2pass = cinfo->cquantize;  | 
339  |  | #else  | 
340  |  |       ERREXIT(cinfo, JERR_NOT_COMPILED);  | 
341  |  | #endif  | 
342  | 0  |     }  | 
343  |  |     /* If both quantizers are initialized, the 2-pass one is left active;  | 
344  |  |      * this is necessary for starting with quantization to an external map.  | 
345  |  |      */  | 
346  | 0  |   }  | 
347  |  |  | 
348  |  |   /* Post-processing: in particular, color conversion first */  | 
349  | 0  |   if (! cinfo->raw_data_out) { | 
350  | 0  |     if (master->using_merged_upsample) { | 
351  | 0  | #ifdef UPSAMPLE_MERGING_SUPPORTED  | 
352  | 0  |       jinit_merged_upsampler(cinfo); /* does color conversion too */  | 
353  |  | #else  | 
354  |  |       ERREXIT(cinfo, JERR_NOT_COMPILED);  | 
355  |  | #endif  | 
356  | 0  |     } else { | 
357  | 0  |       jinit_color_deconverter(cinfo);  | 
358  | 0  |       jinit_upsampler(cinfo);  | 
359  | 0  |     }  | 
360  | 0  |     jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);  | 
361  | 0  |   }  | 
362  |  |   /* Inverse DCT */  | 
363  | 0  |   jinit_inverse_dct(cinfo);  | 
364  |  |   /* Entropy decoding: either Huffman or arithmetic coding. */  | 
365  | 0  |   if (cinfo->arith_code)  | 
366  | 0  |     jinit_arith_decoder(cinfo);  | 
367  | 0  |   else { | 
368  | 0  |     jinit_huff_decoder(cinfo);  | 
369  | 0  |   }  | 
370  |  |  | 
371  |  |   /* Initialize principal buffer controllers. */  | 
372  | 0  |   use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;  | 
373  | 0  |   jinit_d_coef_controller(cinfo, use_c_buffer);  | 
374  |  | 
  | 
375  | 0  |   if (! cinfo->raw_data_out)  | 
376  | 0  |     jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);  | 
377  |  |  | 
378  |  |   /* We can now tell the memory manager to allocate virtual arrays. */  | 
379  | 0  |   (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);  | 
380  |  |  | 
381  |  |   /* Initialize input side of decompressor to consume first scan. */  | 
382  | 0  |   (*cinfo->inputctl->start_input_pass) (cinfo);  | 
383  |  | 
  | 
384  | 0  | #ifdef D_MULTISCAN_FILES_SUPPORTED  | 
385  |  |   /* If jpeg_start_decompress will read the whole file, initialize  | 
386  |  |    * progress monitoring appropriately.  The input step is counted  | 
387  |  |    * as one pass.  | 
388  |  |    */  | 
389  | 0  |   if (cinfo->progress != NULL && ! cinfo->buffered_image &&  | 
390  | 0  |       cinfo->inputctl->has_multiple_scans) { | 
391  | 0  |     int nscans;  | 
392  |  |     /* Estimate number of scans to set pass_limit. */  | 
393  | 0  |     if (cinfo->progressive_mode) { | 
394  |  |       /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */  | 
395  | 0  |       nscans = 2 + 3 * cinfo->num_components;  | 
396  | 0  |     } else { | 
397  |  |       /* For a nonprogressive multiscan file, estimate 1 scan per component. */  | 
398  | 0  |       nscans = cinfo->num_components;  | 
399  | 0  |     }  | 
400  | 0  |     cinfo->progress->pass_counter = 0L;  | 
401  | 0  |     cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;  | 
402  | 0  |     cinfo->progress->completed_passes = 0;  | 
403  | 0  |     cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);  | 
404  |  |     /* Count the input pass as done */  | 
405  | 0  |     master->pass_number++;  | 
406  | 0  |   }  | 
407  | 0  | #endif /* D_MULTISCAN_FILES_SUPPORTED */  | 
408  | 0  | }  | 
409  |  |  | 
410  |  |  | 
411  |  | /*  | 
412  |  |  * Per-pass setup.  | 
413  |  |  * This is called at the beginning of each output pass.  We determine which  | 
414  |  |  * modules will be active during this pass and give them appropriate  | 
415  |  |  * start_pass calls.  We also set is_dummy_pass to indicate whether this  | 
416  |  |  * is a "real" output pass or a dummy pass for color quantization.  | 
417  |  |  * (In the latter case, jdapistd.c will crank the pass to completion.)  | 
418  |  |  */  | 
419  |  |  | 
420  |  | METHODDEF(void)  | 
421  |  | prepare_for_output_pass (j_decompress_ptr cinfo)  | 
422  | 0  | { | 
423  | 0  |   my_master_ptr master = (my_master_ptr) cinfo->master;  | 
424  |  | 
  | 
425  | 0  |   if (master->pub.is_dummy_pass) { | 
426  | 0  | #ifdef QUANT_2PASS_SUPPORTED  | 
427  |  |     /* Final pass of 2-pass quantization */  | 
428  | 0  |     master->pub.is_dummy_pass = FALSE;  | 
429  | 0  |     (*cinfo->cquantize->start_pass) (cinfo, FALSE);  | 
430  | 0  |     (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);  | 
431  | 0  |     (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);  | 
432  |  | #else  | 
433  |  |     ERREXIT(cinfo, JERR_NOT_COMPILED);  | 
434  |  | #endif /* QUANT_2PASS_SUPPORTED */  | 
435  | 0  |   } else { | 
436  | 0  |     if (cinfo->quantize_colors && cinfo->colormap == NULL) { | 
437  |  |       /* Select new quantization method */  | 
438  | 0  |       if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) { | 
439  | 0  |   cinfo->cquantize = master->quantizer_2pass;  | 
440  | 0  |   master->pub.is_dummy_pass = TRUE;  | 
441  | 0  |       } else if (cinfo->enable_1pass_quant) { | 
442  | 0  |   cinfo->cquantize = master->quantizer_1pass;  | 
443  | 0  |       } else { | 
444  | 0  |   ERREXIT(cinfo, JERR_MODE_CHANGE);  | 
445  | 0  |       }  | 
446  | 0  |     }  | 
447  | 0  |     (*cinfo->idct->start_pass) (cinfo);  | 
448  | 0  |     (*cinfo->coef->start_output_pass) (cinfo);  | 
449  | 0  |     if (! cinfo->raw_data_out) { | 
450  | 0  |       if (! master->using_merged_upsample)  | 
451  | 0  |   (*cinfo->cconvert->start_pass) (cinfo);  | 
452  | 0  |       (*cinfo->upsample->start_pass) (cinfo);  | 
453  | 0  |       if (cinfo->quantize_colors)  | 
454  | 0  |   (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);  | 
455  | 0  |       (*cinfo->post->start_pass) (cinfo,  | 
456  | 0  |       (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));  | 
457  | 0  |       (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);  | 
458  | 0  |     }  | 
459  | 0  |   }  | 
460  |  |  | 
461  |  |   /* Set up progress monitor's pass info if present */  | 
462  | 0  |   if (cinfo->progress != NULL) { | 
463  | 0  |     cinfo->progress->completed_passes = master->pass_number;  | 
464  | 0  |     cinfo->progress->total_passes = master->pass_number +  | 
465  | 0  |             (master->pub.is_dummy_pass ? 2 : 1);  | 
466  |  |     /* In buffered-image mode, we assume one more output pass if EOI not  | 
467  |  |      * yet reached, but no more passes if EOI has been reached.  | 
468  |  |      */  | 
469  | 0  |     if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) { | 
470  | 0  |       cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);  | 
471  | 0  |     }  | 
472  | 0  |   }  | 
473  | 0  | }  | 
474  |  |  | 
475  |  |  | 
476  |  | /*  | 
477  |  |  * Finish up at end of an output pass.  | 
478  |  |  */  | 
479  |  |  | 
480  |  | METHODDEF(void)  | 
481  |  | finish_output_pass (j_decompress_ptr cinfo)  | 
482  | 0  | { | 
483  | 0  |   my_master_ptr master = (my_master_ptr) cinfo->master;  | 
484  |  | 
  | 
485  | 0  |   if (cinfo->quantize_colors)  | 
486  | 0  |     (*cinfo->cquantize->finish_pass) (cinfo);  | 
487  | 0  |   master->pass_number++;  | 
488  | 0  | }  | 
489  |  |  | 
490  |  |  | 
491  |  | #ifdef D_MULTISCAN_FILES_SUPPORTED  | 
492  |  |  | 
493  |  | /*  | 
494  |  |  * Switch to a new external colormap between output passes.  | 
495  |  |  */  | 
496  |  |  | 
497  |  | GLOBAL(void)  | 
498  |  | jpeg_new_colormap (j_decompress_ptr cinfo)  | 
499  | 0  | { | 
500  | 0  |   my_master_ptr master = (my_master_ptr) cinfo->master;  | 
501  |  |  | 
502  |  |   /* Prevent application from calling me at wrong times */  | 
503  | 0  |   if (cinfo->global_state != DSTATE_BUFIMAGE)  | 
504  | 0  |     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);  | 
505  |  | 
  | 
506  | 0  |   if (cinfo->quantize_colors && cinfo->enable_external_quant &&  | 
507  | 0  |       cinfo->colormap != NULL) { | 
508  |  |     /* Select 2-pass quantizer for external colormap use */  | 
509  | 0  |     cinfo->cquantize = master->quantizer_2pass;  | 
510  |  |     /* Notify quantizer of colormap change */  | 
511  | 0  |     (*cinfo->cquantize->new_color_map) (cinfo);  | 
512  | 0  |     master->pub.is_dummy_pass = FALSE; /* just in case */  | 
513  | 0  |   } else  | 
514  | 0  |     ERREXIT(cinfo, JERR_MODE_CHANGE);  | 
515  | 0  | }  | 
516  |  |  | 
517  |  | #endif /* D_MULTISCAN_FILES_SUPPORTED */  | 
518  |  |  | 
519  |  |  | 
520  |  | /*  | 
521  |  |  * Initialize master decompression control and select active modules.  | 
522  |  |  * This is performed at the start of jpeg_start_decompress.  | 
523  |  |  */  | 
524  |  |  | 
525  |  | GLOBAL(void)  | 
526  |  | jinit_master_decompress (j_decompress_ptr cinfo)  | 
527  | 0  | { | 
528  | 0  |   my_master_ptr master;  | 
529  |  | 
  | 
530  | 0  |   master = (my_master_ptr) (*cinfo->mem->alloc_small)  | 
531  | 0  |     ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_decomp_master));  | 
532  | 0  |   cinfo->master = &master->pub;  | 
533  | 0  |   master->pub.prepare_for_output_pass = prepare_for_output_pass;  | 
534  | 0  |   master->pub.finish_output_pass = finish_output_pass;  | 
535  |  | 
  | 
536  | 0  |   master->pub.is_dummy_pass = FALSE;  | 
537  |  | 
  | 
538  | 0  |   master_selection(cinfo);  | 
539  | 0  | }  |