blob: 273b8d2fab7ed3e36e2dc483922cb2ea56fdf87a [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jdmaster.c
3 *
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00004 * Copyright (C) 1991-1997, Thomas G. Lane.
DRC27fb3fc2012-01-28 01:48:07 +00005 * Modified 2002-2009 by Guido Vollbeding.
DRC67ce3b22011-12-19 02:21:03 +00006 * Copyright (C) 2009-2011, D. R. Commander.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00007 * This file is part of the Independent JPEG Group's software.
8 * For conditions of distribution and use, see the accompanying README file.
9 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000010 * This file contains master control logic for the JPEG decompressor.
11 * These routines are concerned with selecting the modules to be executed
12 * and with determining the number of passes and the work to be done in each
13 * pass.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000014 */
15
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000016#define JPEG_INTERNALS
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000017#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000018#include "jpeglib.h"
DRC36a6eec2010-10-08 08:05:44 +000019#include "jpegcomp.h"
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000020
21
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000022/* Private state */
23
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000024typedef struct {
25 struct jpeg_decomp_master pub; /* public fields */
26
Thomas G. Lanebc79e061995-08-02 00:00:00 +000027 int pass_number; /* # of passes completed */
28
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000029 boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
30
Thomas G. Lanebc79e061995-08-02 00:00:00 +000031 /* Saved references to initialized quantizer modules,
32 * in case we need to switch modes.
33 */
34 struct jpeg_color_quantizer * quantizer_1pass;
35 struct jpeg_color_quantizer * quantizer_2pass;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000036} my_decomp_master;
37
38typedef my_decomp_master * my_master_ptr;
39
40
41/*
42 * Determine whether merged upsample/color conversion should be used.
43 * CRUCIAL: this must match the actual capabilities of jdmerge.c!
44 */
45
Thomas G. Lane489583f1996-02-07 00:00:00 +000046LOCAL(boolean)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000047use_merged_upsample (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000048{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000049#ifdef UPSAMPLE_MERGING_SUPPORTED
50 /* Merging is the equivalent of plain box-filter upsampling */
51 if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
52 return FALSE;
53 /* jdmerge.c only supports YCC=>RGB color conversion */
54 if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
DRC720e1612009-04-05 21:51:25 +000055 (cinfo->out_color_space != JCS_RGB &&
56 cinfo->out_color_space != JCS_EXT_RGB &&
57 cinfo->out_color_space != JCS_EXT_RGBX &&
58 cinfo->out_color_space != JCS_EXT_BGR &&
59 cinfo->out_color_space != JCS_EXT_BGRX &&
60 cinfo->out_color_space != JCS_EXT_XBGR &&
DRC67ce3b22011-12-19 02:21:03 +000061 cinfo->out_color_space != JCS_EXT_XRGB &&
62 cinfo->out_color_space != JCS_EXT_RGBA &&
63 cinfo->out_color_space != JCS_EXT_BGRA &&
64 cinfo->out_color_space != JCS_EXT_ABGR &&
65 cinfo->out_color_space != JCS_EXT_ARGB) ||
DRC720e1612009-04-05 21:51:25 +000066 cinfo->out_color_components != rgb_pixelsize[cinfo->out_color_space])
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000067 return FALSE;
68 /* and it only handles 2h1v or 2h2v sampling ratios */
69 if (cinfo->comp_info[0].h_samp_factor != 2 ||
70 cinfo->comp_info[1].h_samp_factor != 1 ||
71 cinfo->comp_info[2].h_samp_factor != 1 ||
72 cinfo->comp_info[0].v_samp_factor > 2 ||
73 cinfo->comp_info[1].v_samp_factor != 1 ||
74 cinfo->comp_info[2].v_samp_factor != 1)
75 return FALSE;
76 /* furthermore, it doesn't work if we've scaled the IDCTs differently */
DRC49967cd2010-10-09 19:57:51 +000077 if (cinfo->comp_info[0]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
78 cinfo->comp_info[1]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
79 cinfo->comp_info[2]._DCT_scaled_size != cinfo->_min_DCT_scaled_size)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000080 return FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000081 /* ??? also need to test for upsample-time rescaling, when & if supported */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000082 return TRUE; /* by golly, it'll work... */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000083#else
84 return FALSE;
85#endif
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000086}
87
88
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000089/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +000090 * Compute output image dimensions and related values.
91 * NOTE: this is exported for possible use by application.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000092 * Hence it mustn't do anything that can't be done twice.
DRC27fb3fc2012-01-28 01:48:07 +000093 */
94
95#if JPEG_LIB_VERSION >= 80
96GLOBAL(void)
97#else
98LOCAL(void)
99#endif
100jpeg_core_output_dimensions (j_decompress_ptr cinfo)
101/* Do computations that are needed before master selection phase.
102 * This function is used for transcoding and full decompression.
103 */
104{
105#ifdef IDCT_SCALING_SUPPORTED
106 int ci;
107 jpeg_component_info *compptr;
108
109 /* Compute actual output image dimensions and DCT scaling choices. */
110 if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom) {
111 /* Provide 1/block_size scaling */
112 cinfo->output_width = (JDIMENSION)
113 jdiv_round_up((long) cinfo->image_width, (long) DCTSIZE);
114 cinfo->output_height = (JDIMENSION)
115 jdiv_round_up((long) cinfo->image_height, (long) DCTSIZE);
116 cinfo->_min_DCT_h_scaled_size = 1;
117 cinfo->_min_DCT_v_scaled_size = 1;
118 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 2) {
119 /* Provide 2/block_size scaling */
120 cinfo->output_width = (JDIMENSION)
121 jdiv_round_up((long) cinfo->image_width * 2L, (long) DCTSIZE);
122 cinfo->output_height = (JDIMENSION)
123 jdiv_round_up((long) cinfo->image_height * 2L, (long) DCTSIZE);
124 cinfo->_min_DCT_h_scaled_size = 2;
125 cinfo->_min_DCT_v_scaled_size = 2;
126 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 3) {
127 /* Provide 3/block_size scaling */
128 cinfo->output_width = (JDIMENSION)
129 jdiv_round_up((long) cinfo->image_width * 3L, (long) DCTSIZE);
130 cinfo->output_height = (JDIMENSION)
131 jdiv_round_up((long) cinfo->image_height * 3L, (long) DCTSIZE);
132 cinfo->_min_DCT_h_scaled_size = 3;
133 cinfo->_min_DCT_v_scaled_size = 3;
134 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 4) {
135 /* Provide 4/block_size scaling */
136 cinfo->output_width = (JDIMENSION)
137 jdiv_round_up((long) cinfo->image_width * 4L, (long) DCTSIZE);
138 cinfo->output_height = (JDIMENSION)
139 jdiv_round_up((long) cinfo->image_height * 4L, (long) DCTSIZE);
140 cinfo->_min_DCT_h_scaled_size = 4;
141 cinfo->_min_DCT_v_scaled_size = 4;
142 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 5) {
143 /* Provide 5/block_size scaling */
144 cinfo->output_width = (JDIMENSION)
145 jdiv_round_up((long) cinfo->image_width * 5L, (long) DCTSIZE);
146 cinfo->output_height = (JDIMENSION)
147 jdiv_round_up((long) cinfo->image_height * 5L, (long) DCTSIZE);
148 cinfo->_min_DCT_h_scaled_size = 5;
149 cinfo->_min_DCT_v_scaled_size = 5;
150 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 6) {
151 /* Provide 6/block_size scaling */
152 cinfo->output_width = (JDIMENSION)
153 jdiv_round_up((long) cinfo->image_width * 6L, (long) DCTSIZE);
154 cinfo->output_height = (JDIMENSION)
155 jdiv_round_up((long) cinfo->image_height * 6L, (long) DCTSIZE);
156 cinfo->_min_DCT_h_scaled_size = 6;
157 cinfo->_min_DCT_v_scaled_size = 6;
158 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 7) {
159 /* Provide 7/block_size scaling */
160 cinfo->output_width = (JDIMENSION)
161 jdiv_round_up((long) cinfo->image_width * 7L, (long) DCTSIZE);
162 cinfo->output_height = (JDIMENSION)
163 jdiv_round_up((long) cinfo->image_height * 7L, (long) DCTSIZE);
164 cinfo->_min_DCT_h_scaled_size = 7;
165 cinfo->_min_DCT_v_scaled_size = 7;
166 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 8) {
167 /* Provide 8/block_size scaling */
168 cinfo->output_width = (JDIMENSION)
169 jdiv_round_up((long) cinfo->image_width * 8L, (long) DCTSIZE);
170 cinfo->output_height = (JDIMENSION)
171 jdiv_round_up((long) cinfo->image_height * 8L, (long) DCTSIZE);
172 cinfo->_min_DCT_h_scaled_size = 8;
173 cinfo->_min_DCT_v_scaled_size = 8;
174 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 9) {
175 /* Provide 9/block_size scaling */
176 cinfo->output_width = (JDIMENSION)
177 jdiv_round_up((long) cinfo->image_width * 9L, (long) DCTSIZE);
178 cinfo->output_height = (JDIMENSION)
179 jdiv_round_up((long) cinfo->image_height * 9L, (long) DCTSIZE);
180 cinfo->_min_DCT_h_scaled_size = 9;
181 cinfo->_min_DCT_v_scaled_size = 9;
182 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 10) {
183 /* Provide 10/block_size scaling */
184 cinfo->output_width = (JDIMENSION)
185 jdiv_round_up((long) cinfo->image_width * 10L, (long) DCTSIZE);
186 cinfo->output_height = (JDIMENSION)
187 jdiv_round_up((long) cinfo->image_height * 10L, (long) DCTSIZE);
188 cinfo->_min_DCT_h_scaled_size = 10;
189 cinfo->_min_DCT_v_scaled_size = 10;
190 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 11) {
191 /* Provide 11/block_size scaling */
192 cinfo->output_width = (JDIMENSION)
193 jdiv_round_up((long) cinfo->image_width * 11L, (long) DCTSIZE);
194 cinfo->output_height = (JDIMENSION)
195 jdiv_round_up((long) cinfo->image_height * 11L, (long) DCTSIZE);
196 cinfo->_min_DCT_h_scaled_size = 11;
197 cinfo->_min_DCT_v_scaled_size = 11;
198 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 12) {
199 /* Provide 12/block_size scaling */
200 cinfo->output_width = (JDIMENSION)
201 jdiv_round_up((long) cinfo->image_width * 12L, (long) DCTSIZE);
202 cinfo->output_height = (JDIMENSION)
203 jdiv_round_up((long) cinfo->image_height * 12L, (long) DCTSIZE);
204 cinfo->_min_DCT_h_scaled_size = 12;
205 cinfo->_min_DCT_v_scaled_size = 12;
206 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 13) {
207 /* Provide 13/block_size scaling */
208 cinfo->output_width = (JDIMENSION)
209 jdiv_round_up((long) cinfo->image_width * 13L, (long) DCTSIZE);
210 cinfo->output_height = (JDIMENSION)
211 jdiv_round_up((long) cinfo->image_height * 13L, (long) DCTSIZE);
212 cinfo->_min_DCT_h_scaled_size = 13;
213 cinfo->_min_DCT_v_scaled_size = 13;
214 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 14) {
215 /* Provide 14/block_size scaling */
216 cinfo->output_width = (JDIMENSION)
217 jdiv_round_up((long) cinfo->image_width * 14L, (long) DCTSIZE);
218 cinfo->output_height = (JDIMENSION)
219 jdiv_round_up((long) cinfo->image_height * 14L, (long) DCTSIZE);
220 cinfo->_min_DCT_h_scaled_size = 14;
221 cinfo->_min_DCT_v_scaled_size = 14;
222 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 15) {
223 /* Provide 15/block_size scaling */
224 cinfo->output_width = (JDIMENSION)
225 jdiv_round_up((long) cinfo->image_width * 15L, (long) DCTSIZE);
226 cinfo->output_height = (JDIMENSION)
227 jdiv_round_up((long) cinfo->image_height * 15L, (long) DCTSIZE);
228 cinfo->_min_DCT_h_scaled_size = 15;
229 cinfo->_min_DCT_v_scaled_size = 15;
230 } else {
231 /* Provide 16/block_size scaling */
232 cinfo->output_width = (JDIMENSION)
233 jdiv_round_up((long) cinfo->image_width * 16L, (long) DCTSIZE);
234 cinfo->output_height = (JDIMENSION)
235 jdiv_round_up((long) cinfo->image_height * 16L, (long) DCTSIZE);
236 cinfo->_min_DCT_h_scaled_size = 16;
237 cinfo->_min_DCT_v_scaled_size = 16;
238 }
239
240 /* Recompute dimensions of components */
241 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
242 ci++, compptr++) {
243 compptr->_DCT_h_scaled_size = cinfo->_min_DCT_h_scaled_size;
244 compptr->_DCT_v_scaled_size = cinfo->_min_DCT_v_scaled_size;
245 }
246
247#else /* !IDCT_SCALING_SUPPORTED */
248
249 /* Hardwire it to "no scaling" */
250 cinfo->output_width = cinfo->image_width;
251 cinfo->output_height = cinfo->image_height;
252 /* jdinput.c has already initialized DCT_scaled_size,
253 * and has computed unscaled downsampled_width and downsampled_height.
254 */
255
256#endif /* IDCT_SCALING_SUPPORTED */
257}
258
259
260/*
261 * Compute output image dimensions and related values.
262 * NOTE: this is exported for possible use by application.
263 * Hence it mustn't do anything that can't be done twice.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000264 * Also note that it may be called before the master module is initialized!
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000265 */
266
Thomas G. Lane489583f1996-02-07 00:00:00 +0000267GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000268jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
269/* Do computations that are needed before master selection phase */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000270{
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000271#ifdef IDCT_SCALING_SUPPORTED
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000272 int ci;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000273 jpeg_component_info *compptr;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000274#endif
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000275
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000276 /* Prevent application from calling me at wrong times */
277 if (cinfo->global_state != DSTATE_READY)
278 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
279
Guido Vollbeding989630f2010-01-10 00:00:00 +0000280 /* Compute core output image dimensions and DCT scaling choices. */
281 jpeg_core_output_dimensions(cinfo);
282
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000283#ifdef IDCT_SCALING_SUPPORTED
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000284
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000285 /* In selecting the actual DCT scaling for each component, we try to
286 * scale up the chroma components via IDCT scaling rather than upsampling.
287 * This saves time if the upsampler gets to use 1:1 scaling.
288 * Note this code assumes that the supported DCT scalings are powers of 2.
289 */
290 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
291 ci++, compptr++) {
DRC49967cd2010-10-09 19:57:51 +0000292 int ssize = cinfo->_min_DCT_scaled_size;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000293 while (ssize < DCTSIZE &&
294 (compptr->h_samp_factor * ssize * 2 <=
DRC49967cd2010-10-09 19:57:51 +0000295 cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) &&
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000296 (compptr->v_samp_factor * ssize * 2 <=
DRC49967cd2010-10-09 19:57:51 +0000297 cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size)) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000298 ssize = ssize * 2;
299 }
DRC36a6eec2010-10-08 08:05:44 +0000300#if JPEG_LIB_VERSION >= 70
301 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = ssize;
302#else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000303 compptr->DCT_scaled_size = ssize;
DRC36a6eec2010-10-08 08:05:44 +0000304#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000305 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000306
307 /* Recompute downsampled dimensions of components;
308 * application needs to know these if using raw downsampled data.
309 */
310 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
311 ci++, compptr++) {
312 /* Size in samples, after IDCT scaling */
313 compptr->downsampled_width = (JDIMENSION)
314 jdiv_round_up((long) cinfo->image_width *
DRC49967cd2010-10-09 19:57:51 +0000315 (long) (compptr->h_samp_factor * compptr->_DCT_scaled_size),
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000316 (long) (cinfo->max_h_samp_factor * DCTSIZE));
317 compptr->downsampled_height = (JDIMENSION)
318 jdiv_round_up((long) cinfo->image_height *
DRC49967cd2010-10-09 19:57:51 +0000319 (long) (compptr->v_samp_factor * compptr->_DCT_scaled_size),
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000320 (long) (cinfo->max_v_samp_factor * DCTSIZE));
321 }
322
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000323#else /* !IDCT_SCALING_SUPPORTED */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000324
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000325 /* Hardwire it to "no scaling" */
326 cinfo->output_width = cinfo->image_width;
327 cinfo->output_height = cinfo->image_height;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000328 /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
329 * and has computed unscaled downsampled_width and downsampled_height.
330 */
331
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000332#endif /* IDCT_SCALING_SUPPORTED */
333
334 /* Report number of components in selected colorspace. */
335 /* Probably this should be in the color conversion module... */
336 switch (cinfo->out_color_space) {
337 case JCS_GRAYSCALE:
338 cinfo->out_color_components = 1;
339 break;
340 case JCS_RGB:
DRC720e1612009-04-05 21:51:25 +0000341 case JCS_EXT_RGB:
342 case JCS_EXT_RGBX:
343 case JCS_EXT_BGR:
344 case JCS_EXT_BGRX:
345 case JCS_EXT_XBGR:
346 case JCS_EXT_XRGB:
DRC67ce3b22011-12-19 02:21:03 +0000347 case JCS_EXT_RGBA:
348 case JCS_EXT_BGRA:
349 case JCS_EXT_ABGR:
350 case JCS_EXT_ARGB:
DRC720e1612009-04-05 21:51:25 +0000351 cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000352 break;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000353 case JCS_YCbCr:
354 cinfo->out_color_components = 3;
355 break;
356 case JCS_CMYK:
357 case JCS_YCCK:
358 cinfo->out_color_components = 4;
359 break;
360 default: /* else must be same colorspace as in file */
361 cinfo->out_color_components = cinfo->num_components;
362 break;
363 }
364 cinfo->output_components = (cinfo->quantize_colors ? 1 :
365 cinfo->out_color_components);
366
367 /* See if upsampler will want to emit more than one row at a time */
368 if (use_merged_upsample(cinfo))
369 cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
370 else
371 cinfo->rec_outbuf_height = 1;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000372}
373
374
375/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000376 * Several decompression processes need to range-limit values to the range
377 * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
378 * due to noise introduced by quantization, roundoff error, etc. These
379 * processes are inner loops and need to be as fast as possible. On most
380 * machines, particularly CPUs with pipelines or instruction prefetch,
381 * a (subscript-check-less) C table lookup
382 * x = sample_range_limit[x];
383 * is faster than explicit tests
384 * if (x < 0) x = 0;
385 * else if (x > MAXJSAMPLE) x = MAXJSAMPLE;
386 * These processes all use a common table prepared by the routine below.
387 *
388 * For most steps we can mathematically guarantee that the initial value
389 * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
390 * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial
391 * limiting step (just after the IDCT), a wildly out-of-range value is
392 * possible if the input data is corrupt. To avoid any chance of indexing
393 * off the end of memory and getting a bad-pointer trap, we perform the
394 * post-IDCT limiting thus:
395 * x = range_limit[x & MASK];
396 * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
397 * samples. Under normal circumstances this is more than enough range and
398 * a correct output will be generated; with bogus input data the mask will
399 * cause wraparound, and we will safely generate a bogus-but-in-range output.
400 * For the post-IDCT step, we want to convert the data from signed to unsigned
401 * representation by adding CENTERJSAMPLE at the same time that we limit it.
402 * So the post-IDCT limiting table ends up looking like this:
403 * CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
404 * MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
405 * 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
406 * 0,1,...,CENTERJSAMPLE-1
407 * Negative inputs select values from the upper half of the table after
408 * masking.
409 *
410 * We can save some space by overlapping the start of the post-IDCT table
411 * with the simpler range limiting table. The post-IDCT table begins at
412 * sample_range_limit + CENTERJSAMPLE.
413 *
414 * Note that the table is allocated in near data space on PCs; it's small
415 * enough and used often enough to justify this.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000416 */
417
Thomas G. Lane489583f1996-02-07 00:00:00 +0000418LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000419prepare_range_limit_table (j_decompress_ptr cinfo)
420/* Allocate and fill in the sample_range_limit table */
421{
422 JSAMPLE * table;
423 int i;
424
425 table = (JSAMPLE *)
426 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
427 (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
428 table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */
429 cinfo->sample_range_limit = table;
430 /* First segment of "simple" table: limit[x] = 0 for x < 0 */
431 MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
432 /* Main part of "simple" table: limit[x] = x */
433 for (i = 0; i <= MAXJSAMPLE; i++)
434 table[i] = (JSAMPLE) i;
435 table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */
436 /* End of simple table, rest of first half of post-IDCT table */
437 for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
438 table[i] = MAXJSAMPLE;
439 /* Second half of post-IDCT table */
440 MEMZERO(table + (2 * (MAXJSAMPLE+1)),
441 (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
442 MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
443 cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
444}
445
446
447/*
448 * Master selection of decompression modules.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000449 * This is done once at jpeg_start_decompress time. We determine
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000450 * which modules will be used and give them appropriate initialization calls.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000451 * We also initialize the decompressor input side to begin consuming data.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000452 *
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000453 * Since jpeg_read_header has finished, we know what is in the SOF
454 * and (first) SOS markers. We also have all the application parameter
455 * settings.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000456 */
457
Thomas G. Lane489583f1996-02-07 00:00:00 +0000458LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000459master_selection (j_decompress_ptr cinfo)
460{
461 my_master_ptr master = (my_master_ptr) cinfo->master;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000462 boolean use_c_buffer;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000463 long samplesperrow;
464 JDIMENSION jd_samplesperrow;
465
466 /* Initialize dimensions and other stuff */
467 jpeg_calc_output_dimensions(cinfo);
468 prepare_range_limit_table(cinfo);
469
470 /* Width of an output scanline must be representable as JDIMENSION. */
471 samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
472 jd_samplesperrow = (JDIMENSION) samplesperrow;
473 if ((long) jd_samplesperrow != samplesperrow)
474 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
475
476 /* Initialize my private state */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000477 master->pass_number = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000478 master->using_merged_upsample = use_merged_upsample(cinfo);
479
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000480 /* Color quantizer selection */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000481 master->quantizer_1pass = NULL;
482 master->quantizer_2pass = NULL;
483 /* No mode changes if not using buffered-image mode. */
484 if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
485 cinfo->enable_1pass_quant = FALSE;
486 cinfo->enable_external_quant = FALSE;
487 cinfo->enable_2pass_quant = FALSE;
488 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000489 if (cinfo->quantize_colors) {
490 if (cinfo->raw_data_out)
491 ERREXIT(cinfo, JERR_NOTIMPL);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000492 /* 2-pass quantizer only works in 3-component color space. */
493 if (cinfo->out_color_components != 3) {
494 cinfo->enable_1pass_quant = TRUE;
495 cinfo->enable_external_quant = FALSE;
496 cinfo->enable_2pass_quant = FALSE;
497 cinfo->colormap = NULL;
498 } else if (cinfo->colormap != NULL) {
499 cinfo->enable_external_quant = TRUE;
500 } else if (cinfo->two_pass_quantize) {
501 cinfo->enable_2pass_quant = TRUE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000502 } else {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000503 cinfo->enable_1pass_quant = TRUE;
504 }
505
506 if (cinfo->enable_1pass_quant) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000507#ifdef QUANT_1PASS_SUPPORTED
508 jinit_1pass_quantizer(cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000509 master->quantizer_1pass = cinfo->cquantize;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000510#else
511 ERREXIT(cinfo, JERR_NOT_COMPILED);
512#endif
513 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000514
515 /* We use the 2-pass code to map to external colormaps. */
516 if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
517#ifdef QUANT_2PASS_SUPPORTED
518 jinit_2pass_quantizer(cinfo);
519 master->quantizer_2pass = cinfo->cquantize;
520#else
521 ERREXIT(cinfo, JERR_NOT_COMPILED);
522#endif
523 }
524 /* If both quantizers are initialized, the 2-pass one is left active;
525 * this is necessary for starting with quantization to an external map.
526 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000527 }
528
529 /* Post-processing: in particular, color conversion first */
530 if (! cinfo->raw_data_out) {
531 if (master->using_merged_upsample) {
532#ifdef UPSAMPLE_MERGING_SUPPORTED
533 jinit_merged_upsampler(cinfo); /* does color conversion too */
534#else
535 ERREXIT(cinfo, JERR_NOT_COMPILED);
536#endif
537 } else {
538 jinit_color_deconverter(cinfo);
539 jinit_upsampler(cinfo);
540 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000541 jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000542 }
543 /* Inverse DCT */
544 jinit_inverse_dct(cinfo);
545 /* Entropy decoding: either Huffman or arithmetic coding. */
546 if (cinfo->arith_code) {
DRC66f97e62010-11-23 05:49:54 +0000547#ifdef D_ARITH_CODING_SUPPORTED
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000548 jinit_arith_decoder(cinfo);
DRC66f97e62010-11-23 05:49:54 +0000549#else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000550 ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
DRC66f97e62010-11-23 05:49:54 +0000551#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000552 } else {
553 if (cinfo->progressive_mode) {
554#ifdef D_PROGRESSIVE_SUPPORTED
555 jinit_phuff_decoder(cinfo);
556#else
557 ERREXIT(cinfo, JERR_NOT_COMPILED);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000558#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000559 } else
560 jinit_huff_decoder(cinfo);
561 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000562
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000563 /* Initialize principal buffer controllers. */
564 use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
565 jinit_d_coef_controller(cinfo, use_c_buffer);
566
567 if (! cinfo->raw_data_out)
568 jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000569
570 /* We can now tell the memory manager to allocate virtual arrays. */
571 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000572
573 /* Initialize input side of decompressor to consume first scan. */
574 (*cinfo->inputctl->start_input_pass) (cinfo);
575
576#ifdef D_MULTISCAN_FILES_SUPPORTED
577 /* If jpeg_start_decompress will read the whole file, initialize
578 * progress monitoring appropriately. The input step is counted
579 * as one pass.
580 */
581 if (cinfo->progress != NULL && ! cinfo->buffered_image &&
582 cinfo->inputctl->has_multiple_scans) {
583 int nscans;
584 /* Estimate number of scans to set pass_limit. */
585 if (cinfo->progressive_mode) {
586 /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
587 nscans = 2 + 3 * cinfo->num_components;
588 } else {
589 /* For a nonprogressive multiscan file, estimate 1 scan per component. */
590 nscans = cinfo->num_components;
591 }
592 cinfo->progress->pass_counter = 0L;
593 cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
594 cinfo->progress->completed_passes = 0;
595 cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
596 /* Count the input pass as done */
597 master->pass_number++;
598 }
599#endif /* D_MULTISCAN_FILES_SUPPORTED */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000600}
601
602
603/*
604 * Per-pass setup.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000605 * This is called at the beginning of each output pass. We determine which
606 * modules will be active during this pass and give them appropriate
607 * start_pass calls. We also set is_dummy_pass to indicate whether this
608 * is a "real" output pass or a dummy pass for color quantization.
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000609 * (In the latter case, jdapistd.c will crank the pass to completion.)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000610 */
611
Thomas G. Lane489583f1996-02-07 00:00:00 +0000612METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000613prepare_for_output_pass (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000614{
615 my_master_ptr master = (my_master_ptr) cinfo->master;
616
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000617 if (master->pub.is_dummy_pass) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000618#ifdef QUANT_2PASS_SUPPORTED
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000619 /* Final pass of 2-pass quantization */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000620 master->pub.is_dummy_pass = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000621 (*cinfo->cquantize->start_pass) (cinfo, FALSE);
622 (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
623 (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000624#else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000625 ERREXIT(cinfo, JERR_NOT_COMPILED);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000626#endif /* QUANT_2PASS_SUPPORTED */
627 } else {
628 if (cinfo->quantize_colors && cinfo->colormap == NULL) {
629 /* Select new quantization method */
630 if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
631 cinfo->cquantize = master->quantizer_2pass;
632 master->pub.is_dummy_pass = TRUE;
633 } else if (cinfo->enable_1pass_quant) {
634 cinfo->cquantize = master->quantizer_1pass;
635 } else {
636 ERREXIT(cinfo, JERR_MODE_CHANGE);
637 }
638 }
639 (*cinfo->idct->start_pass) (cinfo);
640 (*cinfo->coef->start_output_pass) (cinfo);
641 if (! cinfo->raw_data_out) {
642 if (! master->using_merged_upsample)
643 (*cinfo->cconvert->start_pass) (cinfo);
644 (*cinfo->upsample->start_pass) (cinfo);
645 if (cinfo->quantize_colors)
646 (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
647 (*cinfo->post->start_pass) (cinfo,
648 (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
649 (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
650 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000651 }
652
653 /* Set up progress monitor's pass info if present */
654 if (cinfo->progress != NULL) {
655 cinfo->progress->completed_passes = master->pass_number;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000656 cinfo->progress->total_passes = master->pass_number +
657 (master->pub.is_dummy_pass ? 2 : 1);
658 /* In buffered-image mode, we assume one more output pass if EOI not
659 * yet reached, but no more passes if EOI has been reached.
660 */
661 if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
662 cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
663 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000664 }
665}
666
667
668/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000669 * Finish up at end of an output pass.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000670 */
671
Thomas G. Lane489583f1996-02-07 00:00:00 +0000672METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000673finish_output_pass (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000674{
675 my_master_ptr master = (my_master_ptr) cinfo->master;
676
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000677 if (cinfo->quantize_colors)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000678 (*cinfo->cquantize->finish_pass) (cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000679 master->pass_number++;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000680}
681
682
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000683#ifdef D_MULTISCAN_FILES_SUPPORTED
684
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000685/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000686 * Switch to a new external colormap between output passes.
687 */
688
Thomas G. Lane489583f1996-02-07 00:00:00 +0000689GLOBAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000690jpeg_new_colormap (j_decompress_ptr cinfo)
691{
692 my_master_ptr master = (my_master_ptr) cinfo->master;
693
694 /* Prevent application from calling me at wrong times */
695 if (cinfo->global_state != DSTATE_BUFIMAGE)
696 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
697
698 if (cinfo->quantize_colors && cinfo->enable_external_quant &&
699 cinfo->colormap != NULL) {
700 /* Select 2-pass quantizer for external colormap use */
701 cinfo->cquantize = master->quantizer_2pass;
702 /* Notify quantizer of colormap change */
703 (*cinfo->cquantize->new_color_map) (cinfo);
704 master->pub.is_dummy_pass = FALSE; /* just in case */
705 } else
706 ERREXIT(cinfo, JERR_MODE_CHANGE);
707}
708
709#endif /* D_MULTISCAN_FILES_SUPPORTED */
710
711
712/*
713 * Initialize master decompression control and select active modules.
714 * This is performed at the start of jpeg_start_decompress.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000715 */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000716
Thomas G. Lane489583f1996-02-07 00:00:00 +0000717GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000718jinit_master_decompress (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000719{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000720 my_master_ptr master;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000721
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000722 master = (my_master_ptr)
723 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
724 SIZEOF(my_decomp_master));
725 cinfo->master = (struct jpeg_decomp_master *) master;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000726 master->pub.prepare_for_output_pass = prepare_for_output_pass;
727 master->pub.finish_output_pass = finish_output_pass;
728
729 master->pub.is_dummy_pass = FALSE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000730
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000731 master_selection(cinfo);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000732}