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