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