1 /*
2  * jdmaster.c
3  *
4  * This file was part of the Independent JPEG Group's software:
5  * Copyright (C) 1991-1997, Thomas G. Lane.
6  * Modified 2002-2009 by Guido Vollbeding.
7  * libjpeg-turbo Modifications:
8  * Copyright (C) 2009-2011, 2016, D. R. Commander.
9  * Copyright (C) 2013, Linaro Limited.
10  * Copyright (C) 2015, Google, Inc.
11  * For conditions of distribution and use, see the accompanying README.ijg
12  * file.
13  *
14  * This file contains master control logic for the JPEG decompressor.
15  * These routines are concerned with selecting the modules to be executed
16  * and with determining the number of passes and the work to be done in each
17  * pass.
18  */
19 
20 #define JPEG_INTERNALS
21 #include "jinclude.h"
22 #include "jpeglib.h"
23 #include "jpegcomp.h"
24 #include "jdmaster.h"
25 
26 
27 /*
28  * Determine whether merged upsample/color conversion should be used.
29  * CRUCIAL: this must match the actual capabilities of jdmerge.c!
30  */
31 
32 LOCAL(boolean)
use_merged_upsample(j_decompress_ptr cinfo)33 use_merged_upsample (j_decompress_ptr cinfo)
34 {
35 #ifdef UPSAMPLE_MERGING_SUPPORTED
36   /* Merging is the equivalent of plain box-filter upsampling */
37   if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
38     return FALSE;
39   /* jdmerge.c only supports YCC=>RGB and YCC=>RGB565 color conversion */
40   if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
41       (cinfo->out_color_space != JCS_RGB &&
42       cinfo->out_color_space != JCS_RGB565 &&
43       cinfo->out_color_space != JCS_EXT_RGB &&
44       cinfo->out_color_space != JCS_EXT_RGBX &&
45       cinfo->out_color_space != JCS_EXT_BGR &&
46       cinfo->out_color_space != JCS_EXT_BGRX &&
47       cinfo->out_color_space != JCS_EXT_XBGR &&
48       cinfo->out_color_space != JCS_EXT_XRGB &&
49       cinfo->out_color_space != JCS_EXT_RGBA &&
50       cinfo->out_color_space != JCS_EXT_BGRA &&
51       cinfo->out_color_space != JCS_EXT_ABGR &&
52       cinfo->out_color_space != JCS_EXT_ARGB))
53     return FALSE;
54   if ((cinfo->out_color_space == JCS_RGB565 &&
55       cinfo->out_color_components != 3) ||
56       (cinfo->out_color_space != JCS_RGB565 &&
57       cinfo->out_color_components != rgb_pixelsize[cinfo->out_color_space]))
58     return FALSE;
59   /* and it only handles 2h1v or 2h2v sampling ratios */
60   if (cinfo->comp_info[0].h_samp_factor != 2 ||
61       cinfo->comp_info[1].h_samp_factor != 1 ||
62       cinfo->comp_info[2].h_samp_factor != 1 ||
63       cinfo->comp_info[0].v_samp_factor >  2 ||
64       cinfo->comp_info[1].v_samp_factor != 1 ||
65       cinfo->comp_info[2].v_samp_factor != 1)
66     return FALSE;
67   /* furthermore, it doesn't work if we've scaled the IDCTs differently */
68   if (cinfo->comp_info[0]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
69       cinfo->comp_info[1]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
70       cinfo->comp_info[2]._DCT_scaled_size != cinfo->_min_DCT_scaled_size)
71     return FALSE;
72   /* ??? also need to test for upsample-time rescaling, when & if supported */
73   return TRUE;                  /* by golly, it'll work... */
74 #else
75   return FALSE;
76 #endif
77 }
78 
79 
80 /*
81  * Compute output image dimensions and related values.
82  * NOTE: this is exported for possible use by application.
83  * Hence it mustn't do anything that can't be done twice.
84  */
85 
86 #if JPEG_LIB_VERSION >= 80
87 GLOBAL(void)
88 #else
89 LOCAL(void)
90 #endif
jpeg_core_output_dimensions(j_decompress_ptr cinfo)91 jpeg_core_output_dimensions (j_decompress_ptr cinfo)
92 /* Do computations that are needed before master selection phase.
93  * This function is used for transcoding and full decompression.
94  */
95 {
96 #ifdef IDCT_SCALING_SUPPORTED
97   int ci;
98   jpeg_component_info *compptr;
99 
100   /* Compute actual output image dimensions and DCT scaling choices. */
101   if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom) {
102     /* Provide 1/block_size scaling */
103     cinfo->output_width = (JDIMENSION)
104       jdiv_round_up((long) cinfo->image_width, (long) DCTSIZE);
105     cinfo->output_height = (JDIMENSION)
106       jdiv_round_up((long) cinfo->image_height, (long) DCTSIZE);
107     cinfo->_min_DCT_h_scaled_size = 1;
108     cinfo->_min_DCT_v_scaled_size = 1;
109   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 2) {
110     /* Provide 2/block_size scaling */
111     cinfo->output_width = (JDIMENSION)
112       jdiv_round_up((long) cinfo->image_width * 2L, (long) DCTSIZE);
113     cinfo->output_height = (JDIMENSION)
114       jdiv_round_up((long) cinfo->image_height * 2L, (long) DCTSIZE);
115     cinfo->_min_DCT_h_scaled_size = 2;
116     cinfo->_min_DCT_v_scaled_size = 2;
117   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 3) {
118     /* Provide 3/block_size scaling */
119     cinfo->output_width = (JDIMENSION)
120       jdiv_round_up((long) cinfo->image_width * 3L, (long) DCTSIZE);
121     cinfo->output_height = (JDIMENSION)
122       jdiv_round_up((long) cinfo->image_height * 3L, (long) DCTSIZE);
123     cinfo->_min_DCT_h_scaled_size = 3;
124     cinfo->_min_DCT_v_scaled_size = 3;
125   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 4) {
126     /* Provide 4/block_size scaling */
127     cinfo->output_width = (JDIMENSION)
128       jdiv_round_up((long) cinfo->image_width * 4L, (long) DCTSIZE);
129     cinfo->output_height = (JDIMENSION)
130       jdiv_round_up((long) cinfo->image_height * 4L, (long) DCTSIZE);
131     cinfo->_min_DCT_h_scaled_size = 4;
132     cinfo->_min_DCT_v_scaled_size = 4;
133   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 5) {
134     /* Provide 5/block_size scaling */
135     cinfo->output_width = (JDIMENSION)
136       jdiv_round_up((long) cinfo->image_width * 5L, (long) DCTSIZE);
137     cinfo->output_height = (JDIMENSION)
138       jdiv_round_up((long) cinfo->image_height * 5L, (long) DCTSIZE);
139     cinfo->_min_DCT_h_scaled_size = 5;
140     cinfo->_min_DCT_v_scaled_size = 5;
141   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 6) {
142     /* Provide 6/block_size scaling */
143     cinfo->output_width = (JDIMENSION)
144       jdiv_round_up((long) cinfo->image_width * 6L, (long) DCTSIZE);
145     cinfo->output_height = (JDIMENSION)
146       jdiv_round_up((long) cinfo->image_height * 6L, (long) DCTSIZE);
147     cinfo->_min_DCT_h_scaled_size = 6;
148     cinfo->_min_DCT_v_scaled_size = 6;
149   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 7) {
150     /* Provide 7/block_size scaling */
151     cinfo->output_width = (JDIMENSION)
152       jdiv_round_up((long) cinfo->image_width * 7L, (long) DCTSIZE);
153     cinfo->output_height = (JDIMENSION)
154       jdiv_round_up((long) cinfo->image_height * 7L, (long) DCTSIZE);
155     cinfo->_min_DCT_h_scaled_size = 7;
156     cinfo->_min_DCT_v_scaled_size = 7;
157   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 8) {
158     /* Provide 8/block_size scaling */
159     cinfo->output_width = (JDIMENSION)
160       jdiv_round_up((long) cinfo->image_width * 8L, (long) DCTSIZE);
161     cinfo->output_height = (JDIMENSION)
162       jdiv_round_up((long) cinfo->image_height * 8L, (long) DCTSIZE);
163     cinfo->_min_DCT_h_scaled_size = 8;
164     cinfo->_min_DCT_v_scaled_size = 8;
165   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 9) {
166     /* Provide 9/block_size scaling */
167     cinfo->output_width = (JDIMENSION)
168       jdiv_round_up((long) cinfo->image_width * 9L, (long) DCTSIZE);
169     cinfo->output_height = (JDIMENSION)
170       jdiv_round_up((long) cinfo->image_height * 9L, (long) DCTSIZE);
171     cinfo->_min_DCT_h_scaled_size = 9;
172     cinfo->_min_DCT_v_scaled_size = 9;
173   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 10) {
174     /* Provide 10/block_size scaling */
175     cinfo->output_width = (JDIMENSION)
176       jdiv_round_up((long) cinfo->image_width * 10L, (long) DCTSIZE);
177     cinfo->output_height = (JDIMENSION)
178       jdiv_round_up((long) cinfo->image_height * 10L, (long) DCTSIZE);
179     cinfo->_min_DCT_h_scaled_size = 10;
180     cinfo->_min_DCT_v_scaled_size = 10;
181   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 11) {
182     /* Provide 11/block_size scaling */
183     cinfo->output_width = (JDIMENSION)
184       jdiv_round_up((long) cinfo->image_width * 11L, (long) DCTSIZE);
185     cinfo->output_height = (JDIMENSION)
186       jdiv_round_up((long) cinfo->image_height * 11L, (long) DCTSIZE);
187     cinfo->_min_DCT_h_scaled_size = 11;
188     cinfo->_min_DCT_v_scaled_size = 11;
189   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 12) {
190     /* Provide 12/block_size scaling */
191     cinfo->output_width = (JDIMENSION)
192       jdiv_round_up((long) cinfo->image_width * 12L, (long) DCTSIZE);
193     cinfo->output_height = (JDIMENSION)
194       jdiv_round_up((long) cinfo->image_height * 12L, (long) DCTSIZE);
195     cinfo->_min_DCT_h_scaled_size = 12;
196     cinfo->_min_DCT_v_scaled_size = 12;
197   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 13) {
198     /* Provide 13/block_size scaling */
199     cinfo->output_width = (JDIMENSION)
200       jdiv_round_up((long) cinfo->image_width * 13L, (long) DCTSIZE);
201     cinfo->output_height = (JDIMENSION)
202       jdiv_round_up((long) cinfo->image_height * 13L, (long) DCTSIZE);
203     cinfo->_min_DCT_h_scaled_size = 13;
204     cinfo->_min_DCT_v_scaled_size = 13;
205   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 14) {
206     /* Provide 14/block_size scaling */
207     cinfo->output_width = (JDIMENSION)
208       jdiv_round_up((long) cinfo->image_width * 14L, (long) DCTSIZE);
209     cinfo->output_height = (JDIMENSION)
210       jdiv_round_up((long) cinfo->image_height * 14L, (long) DCTSIZE);
211     cinfo->_min_DCT_h_scaled_size = 14;
212     cinfo->_min_DCT_v_scaled_size = 14;
213   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 15) {
214     /* Provide 15/block_size scaling */
215     cinfo->output_width = (JDIMENSION)
216       jdiv_round_up((long) cinfo->image_width * 15L, (long) DCTSIZE);
217     cinfo->output_height = (JDIMENSION)
218       jdiv_round_up((long) cinfo->image_height * 15L, (long) DCTSIZE);
219     cinfo->_min_DCT_h_scaled_size = 15;
220     cinfo->_min_DCT_v_scaled_size = 15;
221   } else {
222     /* Provide 16/block_size scaling */
223     cinfo->output_width = (JDIMENSION)
224       jdiv_round_up((long) cinfo->image_width * 16L, (long) DCTSIZE);
225     cinfo->output_height = (JDIMENSION)
226       jdiv_round_up((long) cinfo->image_height * 16L, (long) DCTSIZE);
227     cinfo->_min_DCT_h_scaled_size = 16;
228     cinfo->_min_DCT_v_scaled_size = 16;
229   }
230 
231   /* Recompute dimensions of components */
232   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
233        ci++, compptr++) {
234     compptr->_DCT_h_scaled_size = cinfo->_min_DCT_h_scaled_size;
235     compptr->_DCT_v_scaled_size = cinfo->_min_DCT_v_scaled_size;
236   }
237 
238 #else /* !IDCT_SCALING_SUPPORTED */
239 
240   /* Hardwire it to "no scaling" */
241   cinfo->output_width = cinfo->image_width;
242   cinfo->output_height = cinfo->image_height;
243   /* jdinput.c has already initialized DCT_scaled_size,
244    * and has computed unscaled downsampled_width and downsampled_height.
245    */
246 
247 #endif /* IDCT_SCALING_SUPPORTED */
248 }
249 
250 
251 /*
252  * Compute output image dimensions and related values.
253  * NOTE: this is exported for possible use by application.
254  * Hence it mustn't do anything that can't be done twice.
255  * Also note that it may be called before the master module is initialized!
256  */
257 
258 GLOBAL(void)
jpeg_calc_output_dimensions(j_decompress_ptr cinfo)259 jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
260 /* Do computations that are needed before master selection phase */
261 {
262 #ifdef IDCT_SCALING_SUPPORTED
263   int ci;
264   jpeg_component_info *compptr;
265 #endif
266 
267   /* Prevent application from calling me at wrong times */
268   if (cinfo->global_state != DSTATE_READY)
269     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
270 
271   /* Compute core output image dimensions and DCT scaling choices. */
272   jpeg_core_output_dimensions(cinfo);
273 
274 #ifdef IDCT_SCALING_SUPPORTED
275 
276   /* In selecting the actual DCT scaling for each component, we try to
277    * scale up the chroma components via IDCT scaling rather than upsampling.
278    * This saves time if the upsampler gets to use 1:1 scaling.
279    * Note this code adapts subsampling ratios which are powers of 2.
280    */
281   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
282        ci++, compptr++) {
283     int ssize = cinfo->_min_DCT_scaled_size;
284     while (ssize < DCTSIZE &&
285            ((cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) %
286             (compptr->h_samp_factor * ssize * 2) == 0) &&
287            ((cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size) %
288             (compptr->v_samp_factor * ssize * 2) == 0)) {
289       ssize = ssize * 2;
290     }
291 #if JPEG_LIB_VERSION >= 70
292     compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = ssize;
293 #else
294     compptr->DCT_scaled_size = ssize;
295 #endif
296   }
297 
298   /* Recompute downsampled dimensions of components;
299    * application needs to know these if using raw downsampled data.
300    */
301   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
302        ci++, compptr++) {
303     /* Size in samples, after IDCT scaling */
304     compptr->downsampled_width = (JDIMENSION)
305       jdiv_round_up((long) cinfo->image_width *
306                     (long) (compptr->h_samp_factor * compptr->_DCT_scaled_size),
307                     (long) (cinfo->max_h_samp_factor * DCTSIZE));
308     compptr->downsampled_height = (JDIMENSION)
309       jdiv_round_up((long) cinfo->image_height *
310                     (long) (compptr->v_samp_factor * compptr->_DCT_scaled_size),
311                     (long) (cinfo->max_v_samp_factor * DCTSIZE));
312   }
313 
314 #else /* !IDCT_SCALING_SUPPORTED */
315 
316   /* Hardwire it to "no scaling" */
317   cinfo->output_width = cinfo->image_width;
318   cinfo->output_height = cinfo->image_height;
319   /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
320    * and has computed unscaled downsampled_width and downsampled_height.
321    */
322 
323 #endif /* IDCT_SCALING_SUPPORTED */
324 
325   /* Report number of components in selected colorspace. */
326   /* Probably this should be in the color conversion module... */
327   switch (cinfo->out_color_space) {
328   case JCS_GRAYSCALE:
329     cinfo->out_color_components = 1;
330     break;
331   case JCS_RGB:
332   case JCS_EXT_RGB:
333   case JCS_EXT_RGBX:
334   case JCS_EXT_BGR:
335   case JCS_EXT_BGRX:
336   case JCS_EXT_XBGR:
337   case JCS_EXT_XRGB:
338   case JCS_EXT_RGBA:
339   case JCS_EXT_BGRA:
340   case JCS_EXT_ABGR:
341   case JCS_EXT_ARGB:
342     cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
343     break;
344   case JCS_YCbCr:
345   case JCS_RGB565:
346     cinfo->out_color_components = 3;
347     break;
348   case JCS_CMYK:
349   case JCS_YCCK:
350     cinfo->out_color_components = 4;
351     break;
352   default:                      /* else must be same colorspace as in file */
353     cinfo->out_color_components = cinfo->num_components;
354     break;
355   }
356   cinfo->output_components = (cinfo->quantize_colors ? 1 :
357                               cinfo->out_color_components);
358 
359   /* See if upsampler will want to emit more than one row at a time */
360   if (use_merged_upsample(cinfo))
361     cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
362   else
363     cinfo->rec_outbuf_height = 1;
364 }
365 
366 
367 /*
368  * Several decompression processes need to range-limit values to the range
369  * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
370  * due to noise introduced by quantization, roundoff error, etc.  These
371  * processes are inner loops and need to be as fast as possible.  On most
372  * machines, particularly CPUs with pipelines or instruction prefetch,
373  * a (subscript-check-less) C table lookup
374  *              x = sample_range_limit[x];
375  * is faster than explicit tests
376  *              if (x < 0)  x = 0;
377  *              else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;
378  * These processes all use a common table prepared by the routine below.
379  *
380  * For most steps we can mathematically guarantee that the initial value
381  * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
382  * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient.  But for the initial
383  * limiting step (just after the IDCT), a wildly out-of-range value is
384  * possible if the input data is corrupt.  To avoid any chance of indexing
385  * off the end of memory and getting a bad-pointer trap, we perform the
386  * post-IDCT limiting thus:
387  *              x = range_limit[x & MASK];
388  * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
389  * samples.  Under normal circumstances this is more than enough range and
390  * a correct output will be generated; with bogus input data the mask will
391  * cause wraparound, and we will safely generate a bogus-but-in-range output.
392  * For the post-IDCT step, we want to convert the data from signed to unsigned
393  * representation by adding CENTERJSAMPLE at the same time that we limit it.
394  * So the post-IDCT limiting table ends up looking like this:
395  *   CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
396  *   MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
397  *   0          (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
398  *   0,1,...,CENTERJSAMPLE-1
399  * Negative inputs select values from the upper half of the table after
400  * masking.
401  *
402  * We can save some space by overlapping the start of the post-IDCT table
403  * with the simpler range limiting table.  The post-IDCT table begins at
404  * sample_range_limit + CENTERJSAMPLE.
405  */
406 
407 LOCAL(void)
prepare_range_limit_table(j_decompress_ptr cinfo)408 prepare_range_limit_table (j_decompress_ptr cinfo)
409 /* Allocate and fill in the sample_range_limit table */
410 {
411   JSAMPLE * table;
412   int i;
413 
414   table = (JSAMPLE *)
415     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
416                 (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * sizeof(JSAMPLE));
417   table += (MAXJSAMPLE+1);      /* allow negative subscripts of simple table */
418   cinfo->sample_range_limit = table;
419   /* First segment of "simple" table: limit[x] = 0 for x < 0 */
420   MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * sizeof(JSAMPLE));
421   /* Main part of "simple" table: limit[x] = x */
422   for (i = 0; i <= MAXJSAMPLE; i++)
423     table[i] = (JSAMPLE) i;
424   table += CENTERJSAMPLE;       /* Point to where post-IDCT table starts */
425   /* End of simple table, rest of first half of post-IDCT table */
426   for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
427     table[i] = MAXJSAMPLE;
428   /* Second half of post-IDCT table */
429   MEMZERO(table + (2 * (MAXJSAMPLE+1)),
430           (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * sizeof(JSAMPLE));
431   MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
432           cinfo->sample_range_limit, CENTERJSAMPLE * sizeof(JSAMPLE));
433 }
434 
435 
436 /*
437  * Master selection of decompression modules.
438  * This is done once at jpeg_start_decompress time.  We determine
439  * which modules will be used and give them appropriate initialization calls.
440  * We also initialize the decompressor input side to begin consuming data.
441  *
442  * Since jpeg_read_header has finished, we know what is in the SOF
443  * and (first) SOS markers.  We also have all the application parameter
444  * settings.
445  */
446 
447 LOCAL(void)
master_selection(j_decompress_ptr cinfo)448 master_selection (j_decompress_ptr cinfo)
449 {
450   my_master_ptr master = (my_master_ptr) cinfo->master;
451   boolean use_c_buffer;
452   long samplesperrow;
453   JDIMENSION jd_samplesperrow;
454 
455   /* Initialize dimensions and other stuff */
456   jpeg_calc_output_dimensions(cinfo);
457   prepare_range_limit_table(cinfo);
458 
459   /* Width of an output scanline must be representable as JDIMENSION. */
460   samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
461   jd_samplesperrow = (JDIMENSION) samplesperrow;
462   if ((long) jd_samplesperrow != samplesperrow)
463     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
464 
465   /* Initialize my private state */
466   master->pass_number = 0;
467   master->using_merged_upsample = use_merged_upsample(cinfo);
468 
469   /* Color quantizer selection */
470   master->quantizer_1pass = NULL;
471   master->quantizer_2pass = NULL;
472   /* No mode changes if not using buffered-image mode. */
473   if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
474     cinfo->enable_1pass_quant = FALSE;
475     cinfo->enable_external_quant = FALSE;
476     cinfo->enable_2pass_quant = FALSE;
477   }
478   if (cinfo->quantize_colors) {
479     if (cinfo->raw_data_out)
480       ERREXIT(cinfo, JERR_NOTIMPL);
481     /* 2-pass quantizer only works in 3-component color space. */
482     if (cinfo->out_color_components != 3) {
483       cinfo->enable_1pass_quant = TRUE;
484       cinfo->enable_external_quant = FALSE;
485       cinfo->enable_2pass_quant = FALSE;
486       cinfo->colormap = NULL;
487     } else if (cinfo->colormap != NULL) {
488       cinfo->enable_external_quant = TRUE;
489     } else if (cinfo->two_pass_quantize) {
490       cinfo->enable_2pass_quant = TRUE;
491     } else {
492       cinfo->enable_1pass_quant = TRUE;
493     }
494 
495     if (cinfo->enable_1pass_quant) {
496 #ifdef QUANT_1PASS_SUPPORTED
497       jinit_1pass_quantizer(cinfo);
498       master->quantizer_1pass = cinfo->cquantize;
499 #else
500       ERREXIT(cinfo, JERR_NOT_COMPILED);
501 #endif
502     }
503 
504     /* We use the 2-pass code to map to external colormaps. */
505     if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
506 #ifdef QUANT_2PASS_SUPPORTED
507       jinit_2pass_quantizer(cinfo);
508       master->quantizer_2pass = cinfo->cquantize;
509 #else
510       ERREXIT(cinfo, JERR_NOT_COMPILED);
511 #endif
512     }
513     /* If both quantizers are initialized, the 2-pass one is left active;
514      * this is necessary for starting with quantization to an external map.
515      */
516   }
517 
518   /* Post-processing: in particular, color conversion first */
519   if (! cinfo->raw_data_out) {
520     if (master->using_merged_upsample) {
521 #ifdef UPSAMPLE_MERGING_SUPPORTED
522       jinit_merged_upsampler(cinfo); /* does color conversion too */
523 #else
524       ERREXIT(cinfo, JERR_NOT_COMPILED);
525 #endif
526     } else {
527       jinit_color_deconverter(cinfo);
528       jinit_upsampler(cinfo);
529     }
530     jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
531   }
532   /* Inverse DCT */
533   jinit_inverse_dct(cinfo);
534   /* Entropy decoding: either Huffman or arithmetic coding. */
535   if (cinfo->arith_code) {
536 #ifdef D_ARITH_CODING_SUPPORTED
537     jinit_arith_decoder(cinfo);
538 #else
539     ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
540 #endif
541   } else {
542     if (cinfo->progressive_mode) {
543 #ifdef D_PROGRESSIVE_SUPPORTED
544       jinit_phuff_decoder(cinfo);
545 #else
546       ERREXIT(cinfo, JERR_NOT_COMPILED);
547 #endif
548     } else
549       jinit_huff_decoder(cinfo);
550   }
551 
552   /* Initialize principal buffer controllers. */
553   use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
554   jinit_d_coef_controller(cinfo, use_c_buffer);
555 
556   if (! cinfo->raw_data_out)
557     jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
558 
559   /* We can now tell the memory manager to allocate virtual arrays. */
560   (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
561 
562   /* Initialize input side of decompressor to consume first scan. */
563   (*cinfo->inputctl->start_input_pass) (cinfo);
564 
565   /* Set the first and last iMCU columns to decompress from single-scan images.
566    * By default, decompress all of the iMCU columns.
567    */
568   cinfo->master->first_iMCU_col = 0;
569   cinfo->master->last_iMCU_col = cinfo->MCUs_per_row - 1;
570 
571 #ifdef D_MULTISCAN_FILES_SUPPORTED
572   /* If jpeg_start_decompress will read the whole file, initialize
573    * progress monitoring appropriately.  The input step is counted
574    * as one pass.
575    */
576   if (cinfo->progress != NULL && ! cinfo->buffered_image &&
577       cinfo->inputctl->has_multiple_scans) {
578     int nscans;
579     /* Estimate number of scans to set pass_limit. */
580     if (cinfo->progressive_mode) {
581       /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
582       nscans = 2 + 3 * cinfo->num_components;
583     } else {
584       /* For a nonprogressive multiscan file, estimate 1 scan per component. */
585       nscans = cinfo->num_components;
586     }
587     cinfo->progress->pass_counter = 0L;
588     cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
589     cinfo->progress->completed_passes = 0;
590     cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
591     /* Count the input pass as done */
592     master->pass_number++;
593   }
594 #endif /* D_MULTISCAN_FILES_SUPPORTED */
595 }
596 
597 
598 /*
599  * Per-pass setup.
600  * This is called at the beginning of each output pass.  We determine which
601  * modules will be active during this pass and give them appropriate
602  * start_pass calls.  We also set is_dummy_pass to indicate whether this
603  * is a "real" output pass or a dummy pass for color quantization.
604  * (In the latter case, jdapistd.c will crank the pass to completion.)
605  */
606 
607 METHODDEF(void)
prepare_for_output_pass(j_decompress_ptr cinfo)608 prepare_for_output_pass (j_decompress_ptr cinfo)
609 {
610   my_master_ptr master = (my_master_ptr) cinfo->master;
611 
612   if (master->pub.is_dummy_pass) {
613 #ifdef QUANT_2PASS_SUPPORTED
614     /* Final pass of 2-pass quantization */
615     master->pub.is_dummy_pass = FALSE;
616     (*cinfo->cquantize->start_pass) (cinfo, FALSE);
617     (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
618     (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
619 #else
620     ERREXIT(cinfo, JERR_NOT_COMPILED);
621 #endif /* QUANT_2PASS_SUPPORTED */
622   } else {
623     if (cinfo->quantize_colors && cinfo->colormap == NULL) {
624       /* Select new quantization method */
625       if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
626         cinfo->cquantize = master->quantizer_2pass;
627         master->pub.is_dummy_pass = TRUE;
628       } else if (cinfo->enable_1pass_quant) {
629         cinfo->cquantize = master->quantizer_1pass;
630       } else {
631         ERREXIT(cinfo, JERR_MODE_CHANGE);
632       }
633     }
634     (*cinfo->idct->start_pass) (cinfo);
635     (*cinfo->coef->start_output_pass) (cinfo);
636     if (! cinfo->raw_data_out) {
637       if (! master->using_merged_upsample)
638         (*cinfo->cconvert->start_pass) (cinfo);
639       (*cinfo->upsample->start_pass) (cinfo);
640       if (cinfo->quantize_colors)
641         (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
642       (*cinfo->post->start_pass) (cinfo,
643             (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
644       (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
645     }
646   }
647 
648   /* Set up progress monitor's pass info if present */
649   if (cinfo->progress != NULL) {
650     cinfo->progress->completed_passes = master->pass_number;
651     cinfo->progress->total_passes = master->pass_number +
652                                     (master->pub.is_dummy_pass ? 2 : 1);
653     /* In buffered-image mode, we assume one more output pass if EOI not
654      * yet reached, but no more passes if EOI has been reached.
655      */
656     if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
657       cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
658     }
659   }
660 }
661 
662 
663 /*
664  * Finish up at end of an output pass.
665  */
666 
667 METHODDEF(void)
finish_output_pass(j_decompress_ptr cinfo)668 finish_output_pass (j_decompress_ptr cinfo)
669 {
670   my_master_ptr master = (my_master_ptr) cinfo->master;
671 
672   if (cinfo->quantize_colors)
673     (*cinfo->cquantize->finish_pass) (cinfo);
674   master->pass_number++;
675 }
676 
677 
678 #ifdef D_MULTISCAN_FILES_SUPPORTED
679 
680 /*
681  * Switch to a new external colormap between output passes.
682  */
683 
684 GLOBAL(void)
jpeg_new_colormap(j_decompress_ptr cinfo)685 jpeg_new_colormap (j_decompress_ptr cinfo)
686 {
687   my_master_ptr master = (my_master_ptr) cinfo->master;
688 
689   /* Prevent application from calling me at wrong times */
690   if (cinfo->global_state != DSTATE_BUFIMAGE)
691     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
692 
693   if (cinfo->quantize_colors && cinfo->enable_external_quant &&
694       cinfo->colormap != NULL) {
695     /* Select 2-pass quantizer for external colormap use */
696     cinfo->cquantize = master->quantizer_2pass;
697     /* Notify quantizer of colormap change */
698     (*cinfo->cquantize->new_color_map) (cinfo);
699     master->pub.is_dummy_pass = FALSE; /* just in case */
700   } else
701     ERREXIT(cinfo, JERR_MODE_CHANGE);
702 }
703 
704 #endif /* D_MULTISCAN_FILES_SUPPORTED */
705 
706 
707 /*
708  * Initialize master decompression control and select active modules.
709  * This is performed at the start of jpeg_start_decompress.
710  */
711 
712 GLOBAL(void)
jinit_master_decompress(j_decompress_ptr cinfo)713 jinit_master_decompress (j_decompress_ptr cinfo)
714 {
715   my_master_ptr master = (my_master_ptr) cinfo->master;
716 
717   master->pub.prepare_for_output_pass = prepare_for_output_pass;
718   master->pub.finish_output_pass = finish_output_pass;
719 
720   master->pub.is_dummy_pass = FALSE;
721   master->pub.jinit_upsampler_no_alloc = FALSE;
722 
723   master_selection(cinfo);
724 }
725