1 /*
2 * jdmaster.c
3 *
4 * Copyright (C) 1991-1997, Thomas G. Lane.
5 * Modified 2002-2008 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 */
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 {
47 #ifdef UPSAMPLE_MERGING_SUPPORTED
48 /* Merging is the equivalent of plain box-filter upsampling */
49 if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
50 return FALSE;
51 /* jdmerge.c only supports YCC=>RGB color conversion */
52 if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
53 cinfo->out_color_space != JCS_RGB ||
54 cinfo->out_color_components != RGB_PIXELSIZE)
55 return FALSE;
56 /* and it only handles 2h1v or 2h2v sampling ratios */
57 if (cinfo->comp_info[0].h_samp_factor != 2 ||
58 cinfo->comp_info[1].h_samp_factor != 1 ||
59 cinfo->comp_info[2].h_samp_factor != 1 ||
60 cinfo->comp_info[0].v_samp_factor > 2 ||
61 cinfo->comp_info[1].v_samp_factor != 1 ||
62 cinfo->comp_info[2].v_samp_factor != 1)
63 return FALSE;
64 /* furthermore, it doesn't work if we've scaled the IDCTs differently */
65 if (cinfo->comp_info[0].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||
66 cinfo->comp_info[1].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||
67 cinfo->comp_info[2].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||
68 cinfo->comp_info[0].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size ||
69 cinfo->comp_info[1].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size ||
70 cinfo->comp_info[2].DCT_v_scaled_size != cinfo->min_DCT_v_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 * Also note that it may be called before the master module is initialized!
85 */
86
87 GLOBAL(void)
88 jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
89 /* Do computations that are needed before master selection phase */
90 {
91 #ifdef IDCT_SCALING_SUPPORTED
92 int ci;
93 jpeg_component_info *compptr;
94 #endif
95
96 /* Prevent application from calling me at wrong times */
97 if (cinfo->global_state != DSTATE_READY)
98 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
99
100 #ifdef IDCT_SCALING_SUPPORTED
101
102 /* Compute actual output image dimensions and DCT scaling choices. */
103 if (cinfo->scale_num * 8 <= cinfo->scale_denom) {
104 /* Provide 1/8 scaling */
105 cinfo->output_width = (JDIMENSION)
106 jdiv_round_up((long) cinfo->image_width, 8L);
107 cinfo->output_height = (JDIMENSION)
108 jdiv_round_up((long) cinfo->image_height, 8L);
109 cinfo->min_DCT_h_scaled_size = 1;
110 cinfo->min_DCT_v_scaled_size = 1;
111 } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {
112 /* Provide 1/4 scaling */
113 cinfo->output_width = (JDIMENSION)
114 jdiv_round_up((long) cinfo->image_width, 4L);
115 cinfo->output_height = (JDIMENSION)
116 jdiv_round_up((long) cinfo->image_height, 4L);
117 cinfo->min_DCT_h_scaled_size = 2;
118 cinfo->min_DCT_v_scaled_size = 2;
119 } else if (cinfo->scale_num * 8 <= cinfo->scale_denom * 3) {
120 /* Provide 3/8 scaling */
121 cinfo->output_width = (JDIMENSION)
122 jdiv_round_up((long) cinfo->image_width * 3L, 8L);
123 cinfo->output_height = (JDIMENSION)
124 jdiv_round_up((long) cinfo->image_height * 3L, 8L);
125 cinfo->min_DCT_h_scaled_size = 3;
126 cinfo->min_DCT_v_scaled_size = 3;
127 } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) {
128 /* Provide 1/2 scaling */
129 cinfo->output_width = (JDIMENSION)
130 jdiv_round_up((long) cinfo->image_width, 2L);
131 cinfo->output_height = (JDIMENSION)
132 jdiv_round_up((long) cinfo->image_height, 2L);
133 cinfo->min_DCT_h_scaled_size = 4;
134 cinfo->min_DCT_v_scaled_size = 4;
135 } else if (cinfo->scale_num * 8 <= cinfo->scale_denom * 5) {
136 /* Provide 5/8 scaling */
137 cinfo->output_width = (JDIMENSION)
138 jdiv_round_up((long) cinfo->image_width * 5L, 8L);
139 cinfo->output_height = (JDIMENSION)
140 jdiv_round_up((long) cinfo->image_height * 5L, 8L);
141 cinfo->min_DCT_h_scaled_size = 5;
142 cinfo->min_DCT_v_scaled_size = 5;
143 } else if (cinfo->scale_num * 4 <= cinfo->scale_denom * 3) {
144 /* Provide 3/4 scaling */
145 cinfo->output_width = (JDIMENSION)
146 jdiv_round_up((long) cinfo->image_width * 3L, 4L);
147 cinfo->output_height = (JDIMENSION)
148 jdiv_round_up((long) cinfo->image_height * 3L, 4L);
149 cinfo->min_DCT_h_scaled_size = 6;
150 cinfo->min_DCT_v_scaled_size = 6;
151 } else if (cinfo->scale_num * 8 <= cinfo->scale_denom * 7) {
152 /* Provide 7/8 scaling */
153 cinfo->output_width = (JDIMENSION)
154 jdiv_round_up((long) cinfo->image_width * 7L, 8L);
155 cinfo->output_height = (JDIMENSION)
156 jdiv_round_up((long) cinfo->image_height * 7L, 8L);
157 cinfo->min_DCT_h_scaled_size = 7;
158 cinfo->min_DCT_v_scaled_size = 7;
159 } else if (cinfo->scale_num <= cinfo->scale_denom) {
160 /* Provide 1/1 scaling */
161 cinfo->output_width = cinfo->image_width;
162 cinfo->output_height = cinfo->image_height;
163 cinfo->min_DCT_h_scaled_size = DCTSIZE;
164 cinfo->min_DCT_v_scaled_size = DCTSIZE;
165 } else if (cinfo->scale_num * 8 <= cinfo->scale_denom * 9) {
166 /* Provide 9/8 scaling */
167 cinfo->output_width = cinfo->image_width + (JDIMENSION)
168 jdiv_round_up((long) cinfo->image_width, 8L);
169 cinfo->output_height = cinfo->image_height + (JDIMENSION)
170 jdiv_round_up((long) cinfo->image_height, 8L);
171 cinfo->min_DCT_h_scaled_size = 9;
172 cinfo->min_DCT_v_scaled_size = 9;
173 } else if (cinfo->scale_num * 4 <= cinfo->scale_denom * 5) {
174 /* Provide 5/4 scaling */
175 cinfo->output_width = cinfo->image_width + (JDIMENSION)
176 jdiv_round_up((long) cinfo->image_width, 4L);
177 cinfo->output_height = cinfo->image_height + (JDIMENSION)
178 jdiv_round_up((long) cinfo->image_height, 4L);
179 cinfo->min_DCT_h_scaled_size = 10;
180 cinfo->min_DCT_v_scaled_size = 10;
181 } else if (cinfo->scale_num * 8 <= cinfo->scale_denom * 11) {
182 /* Provide 11/8 scaling */
183 cinfo->output_width = cinfo->image_width + (JDIMENSION)
184 jdiv_round_up((long) cinfo->image_width * 3L, 8L);
185 cinfo->output_height = cinfo->image_height + (JDIMENSION)
186 jdiv_round_up((long) cinfo->image_height * 3L, 8L);
187 cinfo->min_DCT_h_scaled_size = 11;
188 cinfo->min_DCT_v_scaled_size = 11;
189 } else if (cinfo->scale_num * 2 <= cinfo->scale_denom * 3) {
190 /* Provide 3/2 scaling */
191 cinfo->output_width = cinfo->image_width + (JDIMENSION)
192 jdiv_round_up((long) cinfo->image_width, 2L);
193 cinfo->output_height = cinfo->image_height + (JDIMENSION)
194 jdiv_round_up((long) cinfo->image_height, 2L);
195 cinfo->min_DCT_h_scaled_size = 12;
196 cinfo->min_DCT_v_scaled_size = 12;
197 } else if (cinfo->scale_num * 8 <= cinfo->scale_denom * 13) {
198 /* Provide 13/8 scaling */
199 cinfo->output_width = cinfo->image_width + (JDIMENSION)
200 jdiv_round_up((long) cinfo->image_width * 5L, 8L);
201 cinfo->output_height = cinfo->image_height + (JDIMENSION)
202 jdiv_round_up((long) cinfo->image_height * 5L, 8L);
203 cinfo->min_DCT_h_scaled_size = 13;
204 cinfo->min_DCT_v_scaled_size = 13;
205 } else if (cinfo->scale_num * 4 <= cinfo->scale_denom * 7) {
206 /* Provide 7/4 scaling */
207 cinfo->output_width = cinfo->image_width + (JDIMENSION)
208 jdiv_round_up((long) cinfo->image_width * 3L, 4L);
209 cinfo->output_height = cinfo->image_height + (JDIMENSION)
210 jdiv_round_up((long) cinfo->image_height * 3L, 4L);
211 cinfo->min_DCT_h_scaled_size = 14;
212 cinfo->min_DCT_v_scaled_size = 14;
213 } else if (cinfo->scale_num * 8 <= cinfo->scale_denom * 15) {
214 /* Provide 15/8 scaling */
215 cinfo->output_width = cinfo->image_width + (JDIMENSION)
216 jdiv_round_up((long) cinfo->image_width * 7L, 8L);
217 cinfo->output_height = cinfo->image_height + (JDIMENSION)
218 jdiv_round_up((long) cinfo->image_height * 7L, 8L);
219 cinfo->min_DCT_h_scaled_size = 15;
220 cinfo->min_DCT_v_scaled_size = 15;
221 } else {
222 /* Provide 2/1 scaling */
223 cinfo->output_width = cinfo->image_width << 1;
224 cinfo->output_height = cinfo->image_height << 1;
225 cinfo->min_DCT_h_scaled_size = 16;
226 cinfo->min_DCT_v_scaled_size = 16;
227 }
228 /* In selecting the actual DCT scaling for each component, we try to
229 * scale up the chroma components via IDCT scaling rather than upsampling.
230 * This saves time if the upsampler gets to use 1:1 scaling.
231 * Note this code adapts subsampling ratios which are powers of 2.
232 */
233 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
234 ci++, compptr++) {
235 int ssize = 1;
236 while (cinfo->min_DCT_h_scaled_size * ssize <=
237 (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) &&
238 (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) {
239 ssize = ssize * 2;
240 }
241 compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize;
242 ssize = 1;
243 while (cinfo->min_DCT_v_scaled_size * ssize <=
244 (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) &&
245 (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) {
246 ssize = ssize * 2;
247 }
248 compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize;
249
250 /* We don't support IDCT ratios larger than 2. */
251 if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2)
252 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;
253 else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2)
254 compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;
255 }
256
257 /* Recompute downsampled dimensions of components;
258 * application needs to know these if using raw downsampled data.
259 */
260 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
261 ci++, compptr++) {
262 /* Size in samples, after IDCT scaling */
263 compptr->downsampled_width = (JDIMENSION)
264 jdiv_round_up((long) cinfo->image_width *
265 (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size),
266 (long) (cinfo->max_h_samp_factor * DCTSIZE));
267 compptr->downsampled_height = (JDIMENSION)
268 jdiv_round_up((long) cinfo->image_height *
269 (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size),
270 (long) (cinfo->max_v_samp_factor * DCTSIZE));
271 }
272
273 #else /* !IDCT_SCALING_SUPPORTED */
274
275 /* Hardwire it to "no scaling" */
276 cinfo->output_width = cinfo->image_width;
277 cinfo->output_height = cinfo->image_height;
278 /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
279 * and has computed unscaled downsampled_width and downsampled_height.
280 */
281
282 #endif /* IDCT_SCALING_SUPPORTED */
283
284 /* Report number of components in selected colorspace. */
285 /* Probably this should be in the color conversion module... */
286 switch (cinfo->out_color_space) {
287 case JCS_GRAYSCALE:
288 cinfo->out_color_components = 1;
289 break;
290 case JCS_RGB:
291 #if RGB_PIXELSIZE != 3
292 cinfo->out_color_components = RGB_PIXELSIZE;
293 break;
294 #endif /* else share code with YCbCr */
295 case JCS_YCbCr:
296 cinfo->out_color_components = 3;
297 break;
298 case JCS_CMYK:
299 case JCS_YCCK:
300 cinfo->out_color_components = 4;
301 break;
302 default: /* else must be same colorspace as in file */
303 cinfo->out_color_components = cinfo->num_components;
304 break;
305 }
306 cinfo->output_components = (cinfo->quantize_colors ? 1 :
307 cinfo->out_color_components);
308
309 /* See if upsampler will want to emit more than one row at a time */
310 if (use_merged_upsample(cinfo))
311 cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
312 else
313 cinfo->rec_outbuf_height = 1;
314 }
315
316
317 /*
318 * Several decompression processes need to range-limit values to the range
319 * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
320 * due to noise introduced by quantization, roundoff error, etc. These
321 * processes are inner loops and need to be as fast as possible. On most
322 * machines, particularly CPUs with pipelines or instruction prefetch,
323 * a (subscript-check-less) C table lookup
324 * x = sample_range_limit[x];
325 * is faster than explicit tests
326 * if (x < 0) x = 0;
327 * else if (x > MAXJSAMPLE) x = MAXJSAMPLE;
328 * These processes all use a common table prepared by the routine below.
329 *
330 * For most steps we can mathematically guarantee that the initial value
331 * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
332 * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial
333 * limiting step (just after the IDCT), a wildly out-of-range value is
334 * possible if the input data is corrupt. To avoid any chance of indexing
335 * off the end of memory and getting a bad-pointer trap, we perform the
336 * post-IDCT limiting thus:
337 * x = range_limit[x & MASK];
338 * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
339 * samples. Under normal circumstances this is more than enough range and
340 * a correct output will be generated; with bogus input data the mask will
341 * cause wraparound, and we will safely generate a bogus-but-in-range output.
342 * For the post-IDCT step, we want to convert the data from signed to unsigned
343 * representation by adding CENTERJSAMPLE at the same time that we limit it.
344 * So the post-IDCT limiting table ends up looking like this:
345 * CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
346 * MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
347 * 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
348 * 0,1,...,CENTERJSAMPLE-1
349 * Negative inputs select values from the upper half of the table after
350 * masking.
351 *
352 * We can save some space by overlapping the start of the post-IDCT table
353 * with the simpler range limiting table. The post-IDCT table begins at
354 * sample_range_limit + CENTERJSAMPLE.
355 *
356 * Note that the table is allocated in near data space on PCs; it's small
357 * enough and used often enough to justify this.
358 */
359
360 LOCAL(void)
361 prepare_range_limit_table (j_decompress_ptr cinfo)
362 /* Allocate and fill in the sample_range_limit table */
363 {
364 JSAMPLE * table;
365 int i;
366
367 table = (JSAMPLE *)
368 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
369 (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
370 table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */
371 cinfo->sample_range_limit = table;
372 /* First segment of "simple" table: limit[x] = 0 for x < 0 */
373 MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
374 /* Main part of "simple" table: limit[x] = x */
375 for (i = 0; i <= MAXJSAMPLE; i++)
376 table[i] = (JSAMPLE) i;
377 table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */
378 /* End of simple table, rest of first half of post-IDCT table */
379 for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
380 table[i] = MAXJSAMPLE;
381 /* Second half of post-IDCT table */
382 MEMZERO(table + (2 * (MAXJSAMPLE+1)),
383 (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
384 MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
385 cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
386 }
387
388
389 /*
390 * Master selection of decompression modules.
391 * This is done once at jpeg_start_decompress time. We determine
392 * which modules will be used and give them appropriate initialization calls.
393 * We also initialize the decompressor input side to begin consuming data.
394 *
395 * Since jpeg_read_header has finished, we know what is in the SOF
396 * and (first) SOS markers. We also have all the application parameter
397 * settings.
398 */
399
400 LOCAL(void)
401 master_selection (j_decompress_ptr cinfo)
402 {
403 my_master_ptr master = (my_master_ptr) cinfo->master;
404 boolean use_c_buffer;
405 long samplesperrow;
406 JDIMENSION jd_samplesperrow;
407
408 /* Initialize dimensions and other stuff */
409 jpeg_calc_output_dimensions(cinfo);
410 prepare_range_limit_table(cinfo);
411
412 /* Width of an output scanline must be representable as JDIMENSION. */
413 samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
414 jd_samplesperrow = (JDIMENSION) samplesperrow;
415 if ((long) jd_samplesperrow != samplesperrow)
416 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
417
418 /* Initialize my private state */
419 master->pass_number = 0;
420 master->using_merged_upsample = use_merged_upsample(cinfo);
421
422 /* Color quantizer selection */
423 master->quantizer_1pass = NULL;
424 master->quantizer_2pass = NULL;
425 /* No mode changes if not using buffered-image mode. */
426 if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
427 cinfo->enable_1pass_quant = FALSE;
428 cinfo->enable_external_quant = FALSE;
429 cinfo->enable_2pass_quant = FALSE;
430 }
431 if (cinfo->quantize_colors) {
636 } else
637 ERREXIT(cinfo, JERR_MODE_CHANGE);
638 }
639
640 #endif /* D_MULTISCAN_FILES_SUPPORTED */
641
642
643 /*
644 * Initialize master decompression control and select active modules.
645 * This is performed at the start of jpeg_start_decompress.
646 */
647
648 GLOBAL(void)
649 jinit_master_decompress (j_decompress_ptr cinfo)
650 {
651 my_master_ptr master;
652
653 master = (my_master_ptr)
654 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
655 SIZEOF(my_decomp_master));
656 cinfo->master = (struct jpeg_decomp_master *) master;
657 master->pub.prepare_for_output_pass = prepare_for_output_pass;
658 master->pub.finish_output_pass = finish_output_pass;
659
660 master->pub.is_dummy_pass = FALSE;
661
662 master_selection(cinfo);
663 }
|
1 /*
2 * jdmaster.c
3 *
4 * Copyright (C) 1991-1997, Thomas G. Lane.
5 * Modified 2002-2017 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 */
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 {
47 #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 if (cinfo->CCIR601_sampling)
60 return FALSE;
61 /* jdmerge.c only supports YCC=>RGB color conversion */
62 if ((cinfo->jpeg_color_space != JCS_YCbCr &&
63 cinfo->jpeg_color_space != JCS_BG_YCC) ||
64 cinfo->num_components != 3 ||
65 cinfo->out_color_space != JCS_RGB ||
66 cinfo->out_color_components != RGB_PIXELSIZE ||
67 cinfo->color_transform)
68 return FALSE;
69 /* and it only handles 2h1v or 2h2v sampling ratios */
70 if (cinfo->comp_info[0].h_samp_factor != 2 ||
71 cinfo->comp_info[1].h_samp_factor != 1 ||
72 cinfo->comp_info[2].h_samp_factor != 1 ||
73 cinfo->comp_info[0].v_samp_factor > 2 ||
74 cinfo->comp_info[1].v_samp_factor != 1 ||
75 cinfo->comp_info[2].v_samp_factor != 1)
76 return FALSE;
77 /* furthermore, it doesn't work if we've scaled the IDCTs differently */
78 if (cinfo->comp_info[0].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||
79 cinfo->comp_info[1].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||
80 cinfo->comp_info[2].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||
81 cinfo->comp_info[0].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size ||
82 cinfo->comp_info[1].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size ||
83 cinfo->comp_info[2].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size)
84 return FALSE;
85 /* ??? also need to test for upsample-time rescaling, when & if supported */
86 return TRUE; /* by golly, it'll work... */
87 #else
88 return FALSE;
89 #endif
90 }
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 {
106 #ifdef IDCT_SCALING_SUPPORTED
107 int ci;
108 jpeg_component_info *compptr;
109 #endif
110
111 /* Prevent application from calling me at wrong times */
112 if (cinfo->global_state != DSTATE_READY)
113 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
114
115 /* Compute core output image dimensions and DCT scaling choices. */
116 jpeg_core_output_dimensions(cinfo);
117
118 #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 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
126 ci++, compptr++) {
127 int ssize = 1;
128 while (cinfo->min_DCT_h_scaled_size * ssize <=
129 (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) &&
130 (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) {
131 ssize = ssize * 2;
132 }
133 compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize;
134 ssize = 1;
135 while (cinfo->min_DCT_v_scaled_size * ssize <=
136 (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) &&
137 (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) {
138 ssize = ssize * 2;
139 }
140 compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize;
141
142 /* We don't support IDCT ratios larger than 2. */
143 if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2)
144 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;
145 else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2)
146 compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;
147 }
148
149 /* Recompute downsampled dimensions of components;
150 * application needs to know these if using raw downsampled data.
151 */
152 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
153 ci++, compptr++) {
154 /* Size in samples, after IDCT scaling */
155 compptr->downsampled_width = (JDIMENSION)
156 jdiv_round_up((long) cinfo->image_width *
157 (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size),
158 (long) (cinfo->max_h_samp_factor * cinfo->block_size));
159 compptr->downsampled_height = (JDIMENSION)
160 jdiv_round_up((long) cinfo->image_height *
161 (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size),
162 (long) (cinfo->max_v_samp_factor * cinfo->block_size));
163 }
164
165 #endif /* IDCT_SCALING_SUPPORTED */
166
167 /* Report number of components in selected colorspace. */
168 /* Probably this should be in the color conversion module... */
169 switch (cinfo->out_color_space) {
170 case JCS_GRAYSCALE:
171 cinfo->out_color_components = 1;
172 break;
173 case JCS_RGB:
174 case JCS_BG_RGB:
175 cinfo->out_color_components = RGB_PIXELSIZE;
176 break;
177 case JCS_YCbCr:
178 case JCS_BG_YCC:
179 cinfo->out_color_components = 3;
180 break;
181 case JCS_CMYK:
182 case JCS_YCCK:
183 cinfo->out_color_components = 4;
184 break;
185 default: /* else must be same colorspace as in file */
186 cinfo->out_color_components = cinfo->num_components;
187 break;
188 }
189 cinfo->output_components = (cinfo->quantize_colors ? 1 :
190 cinfo->out_color_components);
191
192 /* See if upsampler will want to emit more than one row at a time */
193 if (use_merged_upsample(cinfo))
194 cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
195 else
196 cinfo->rec_outbuf_height = 1;
197 }
198
199
200 /*
201 * Several decompression processes need to range-limit values to the range
202 * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
203 * due to noise introduced by quantization, roundoff error, etc. These
204 * processes are inner loops and need to be as fast as possible. On most
205 * machines, particularly CPUs with pipelines or instruction prefetch,
206 * a (subscript-check-less) C table lookup
207 * x = sample_range_limit[x];
208 * is faster than explicit tests
209 * if (x < 0) x = 0;
210 * else if (x > MAXJSAMPLE) x = MAXJSAMPLE;
211 * These processes all use a common table prepared by the routine below.
212 *
213 * For most steps we can mathematically guarantee that the initial value
214 * of x is within 2*(MAXJSAMPLE+1) of the legal range, so a table running
215 * from -2*(MAXJSAMPLE+1) to 3*MAXJSAMPLE+2 is sufficient. But for the
216 * initial limiting step (just after the IDCT), a wildly out-of-range value
217 * is possible if the input data is corrupt. To avoid any chance of indexing
218 * off the end of memory and getting a bad-pointer trap, we perform the
219 * post-IDCT limiting thus:
220 * x = (sample_range_limit - SUBSET)[(x + CENTER) & MASK];
221 * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
222 * samples. Under normal circumstances this is more than enough range and
223 * a correct output will be generated; with bogus input data the mask will
224 * cause wraparound, and we will safely generate a bogus-but-in-range output.
225 * For the post-IDCT step, we want to convert the data from signed to unsigned
226 * representation by adding CENTERJSAMPLE at the same time that we limit it.
227 * This is accomplished with SUBSET = CENTER - CENTERJSAMPLE.
228 *
229 * Note that the table is allocated in near data space on PCs; it's small
230 * enough and used often enough to justify this.
231 */
232
233 LOCAL(void)
234 prepare_range_limit_table (j_decompress_ptr cinfo)
235 /* Allocate and fill in the sample_range_limit table */
236 {
237 JSAMPLE * table;
238 int i;
239
240 table = (JSAMPLE *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,
241 JPOOL_IMAGE, (RANGE_CENTER * 2 + MAXJSAMPLE + 1) * SIZEOF(JSAMPLE));
242 /* First segment of range limit table: limit[x] = 0 for x < 0 */
243 MEMZERO(table, RANGE_CENTER * SIZEOF(JSAMPLE));
244 table += RANGE_CENTER; /* allow negative subscripts of table */
245 cinfo->sample_range_limit = table;
246 /* Main part of range limit table: limit[x] = x */
247 for (i = 0; i <= MAXJSAMPLE; i++)
248 table[i] = (JSAMPLE) i;
249 /* End of range limit table: limit[x] = MAXJSAMPLE for x > MAXJSAMPLE */
250 for (; i <= MAXJSAMPLE + RANGE_CENTER; i++)
251 table[i] = MAXJSAMPLE;
252 }
253
254
255 /*
256 * Master selection of decompression modules.
257 * This is done once at jpeg_start_decompress time. We determine
258 * which modules will be used and give them appropriate initialization calls.
259 * We also initialize the decompressor input side to begin consuming data.
260 *
261 * Since jpeg_read_header has finished, we know what is in the SOF
262 * and (first) SOS markers. We also have all the application parameter
263 * settings.
264 */
265
266 LOCAL(void)
267 master_selection (j_decompress_ptr cinfo)
268 {
269 my_master_ptr master = (my_master_ptr) cinfo->master;
270 boolean use_c_buffer;
271 long samplesperrow;
272 JDIMENSION jd_samplesperrow;
273
274 /* For now, precision must match compiled-in value... */
275 if (cinfo->data_precision != BITS_IN_JSAMPLE)
276 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
277
278 /* Initialize dimensions and other stuff */
279 jpeg_calc_output_dimensions(cinfo);
280 prepare_range_limit_table(cinfo);
281
282 /* Sanity check on image dimensions */
283 if (cinfo->output_height <= 0 || cinfo->output_width <= 0 ||
284 cinfo->out_color_components <= 0)
285 ERREXIT(cinfo, JERR_EMPTY_IMAGE);
286
287 /* Width of an output scanline must be representable as JDIMENSION. */
288 samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
289 jd_samplesperrow = (JDIMENSION) samplesperrow;
290 if ((long) jd_samplesperrow != samplesperrow)
291 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
292
293 /* Initialize my private state */
294 master->pass_number = 0;
295 master->using_merged_upsample = use_merged_upsample(cinfo);
296
297 /* Color quantizer selection */
298 master->quantizer_1pass = NULL;
299 master->quantizer_2pass = NULL;
300 /* No mode changes if not using buffered-image mode. */
301 if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
302 cinfo->enable_1pass_quant = FALSE;
303 cinfo->enable_external_quant = FALSE;
304 cinfo->enable_2pass_quant = FALSE;
305 }
306 if (cinfo->quantize_colors) {
511 } else
512 ERREXIT(cinfo, JERR_MODE_CHANGE);
513 }
514
515 #endif /* D_MULTISCAN_FILES_SUPPORTED */
516
517
518 /*
519 * Initialize master decompression control and select active modules.
520 * This is performed at the start of jpeg_start_decompress.
521 */
522
523 GLOBAL(void)
524 jinit_master_decompress (j_decompress_ptr cinfo)
525 {
526 my_master_ptr master;
527
528 master = (my_master_ptr)
529 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
530 SIZEOF(my_decomp_master));
531 cinfo->master = &master->pub;
532 master->pub.prepare_for_output_pass = prepare_for_output_pass;
533 master->pub.finish_output_pass = finish_output_pass;
534
535 master->pub.is_dummy_pass = FALSE;
536
537 master_selection(cinfo);
538 }
|