3 { This file contains master control logic for the JPEG decompressor.
4 These routines are concerned with selecting the modules to be executed
5 and with determining the number of passes and the work to be done in each
6 pass. }
8 { Original: jdmaster.c ; Copyright (C) 1991-1998, Thomas G. Lane. }
10 interface
12 {$I imjconfig.inc}
14 uses
15 imjmorecfg,
16 imjinclude,
17 imjutils,
18 imjerror,
19 imjdeferr,
22 {$ifdef QUANT_1PASS_SUPPORTED}
23 imjquant1,
24 {$endif}
25 {$ifdef QUANT_2PASS_SUPPORTED}
26 imjquant2,
27 {$endif}
28 {$ifdef UPSAMPLE_MERGING_SUPPORTED}
29 imjdmerge,
30 {$endif}
31 imjpeglib;
34 { Compute output image dimensions and related values.
35 NOTE: this is exported for possible use by application.
36 Hence it mustn't do anything that can't be done twice.
37 Also note that it may be called before the master module is initialized! }
39 {GLOBAL}
41 { Do computations that are needed before master selection phase }
44 {$ifdef D_MULTISCAN_FILES_SUPPORTED}
46 {GLOBAL}
49 {$endif}
51 { Initialize master decompression control and select active modules.
52 This is performed at the start of jpeg_start_decompress. }
54 {GLOBAL}
57 implementation
59 { Private state }
61 type
70 { Saved references to initialized quantizer modules,
71 in case we need to switch modes. }
77 { Determine whether merged upsample/color conversion should be used.
78 CRUCIAL: this must match the actual capabilities of jdmerge.c! }
80 {LOCAL}
82 var
84 begin
87 {$ifdef UPSAMPLE_MERGING_SUPPORTED}
88 { Merging is the equivalent of plain box-filter upsampling }
90 begin
92 exit;
94 { jdmerge.c only supports YCC=>RGB color conversion }
98 begin
100 exit;
103 { and it only handles 2h1v or 2h2v sampling ratios }
110 begin
112 exit;
114 { furthermore, it doesn't work if we've scaled the IDCTs differently }
118 begin
120 exit;
122 { ??? also need to test for upsample-time rescaling, when & if supported }
124 {$else}
126 {$endif}
130 { Compute output image dimensions and related values.
131 NOTE: this is exported for possible use by application.
132 Hence it mustn't do anything that can't be done twice.
133 Also note that it may be called before the master module is initialized! }
135 {GLOBAL}
137 { Do computations that are needed before master selection phase }
138 {$ifdef IDCT_SCALING_SUPPORTED}
139 var
142 {$endif}
143 var
145 begin
146 { Prevent application from calling me at wrong times }
150 {$ifdef IDCT_SCALING_SUPPORTED}
152 { Compute actual output image dimensions and DCT scaling choices. }
154 begin
155 { Provide 1/8 scaling }
161 end
162 else
164 begin
165 { Provide 1/4 scaling }
171 end
172 else
174 begin
175 { Provide 1/2 scaling }
181 end
182 else
183 begin
184 { Provide 1/1 scaling }
189 { In selecting the actual DCT scaling for each component, we try to
190 scale up the chroma components via IDCT scaling rather than upsampling.
191 This saves time if the upsampler gets to use 1:1 scaling.
192 Note this code assumes that the supported DCT scalings are powers of 2. }
196 begin
203 begin
210 { Recompute downsampled dimensions of components;
211 application needs to know these if using raw downsampled data. }
215 begin
216 { Size in samples, after IDCT scaling }
230 { Hardwire it to "no scaling" }
233 { jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
234 and has computed unscaled downsampled_width and downsampled_height. }
238 { Report number of components in selected colorspace. }
239 { Probably this should be in the color conversion module... }
241 JCS_GRAYSCALE:
243 {$ifndef RGB_PIXELSIZE_IS_3}
244 JCS_RGB:
246 {$else}
247 JCS_RGB,
249 JCS_YCbCr:
251 JCS_CMYK,
252 JCS_YCCK:
259 else
262 { See if upsampler will want to emit more than one row at a time }
265 else
270 { Several decompression processes need to range-limit values to the range
271 0..MAXJSAMPLE; the input value may fall somewhat outside this range
272 due to noise introduced by quantization, roundoff error, etc. These
273 processes are inner loops and need to be as fast as possible. On most
274 machines, particularly CPUs with pipelines or instruction prefetch,
275 a (subscript-check-less) C table lookup
276 x := sample_range_limit[x];
277 is faster than explicit tests
278 if (x < 0) x := 0;
279 else if (x > MAXJSAMPLE) x := MAXJSAMPLE;
280 These processes all use a common table prepared by the routine below.
282 For most steps we can mathematically guarantee that the initial value
283 of x is within MAXJSAMPLE+1 of the legal range, so a table running from
284 -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial
285 limiting step (just after the IDCT), a wildly out-of-range value is
286 possible if the input data is corrupt. To avoid any chance of indexing
287 off the end of memory and getting a bad-pointer trap, we perform the
288 post-IDCT limiting thus:
289 x := range_limit[x & MASK];
290 where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
291 samples. Under normal circumstances this is more than enough range and
292 a correct output will be generated; with bogus input data the mask will
293 cause wraparound, and we will safely generate a bogus-but-in-range output.
294 For the post-IDCT step, we want to convert the data from signed to unsigned
295 representation by adding CENTERJSAMPLE at the same time that we limit it.
296 So the post-IDCT limiting table ends up looking like this:
297 CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
298 MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
299 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
300 0,1,...,CENTERJSAMPLE-1
301 Negative inputs select values from the upper half of the table after
302 masking.
304 We can save some space by overlapping the start of the post-IDCT table
305 with the simpler range limiting table. The post-IDCT table begins at
306 sample_range_limit + CENTERJSAMPLE.
308 Note that the table is allocated in near data space on PCs; it's small
309 enough and used often enough to justify this. }
311 {LOCAL}
313 { Allocate and fill in the sample_range_limit table }
314 var
318 begin
323 { First segment of "simple" table: limit[x] := 0 for x < 0 }
327 { allow negative subscripts of simple table }
328 { is noop, handled via type definition (Nomssi) }
329 { Main part of "simple" table: limit[x] := x }
333 { Point to where post-IDCT table starts }
334 { End of simple table, rest of first half of post-IDCT table }
337 { Second half of post-IDCT table }
346 { Master selection of decompression modules.
347 This is done once at jpeg_start_decompress time. We determine
348 which modules will be used and give them appropriate initialization calls.
349 We also initialize the decompressor input side to begin consuming data.
351 Since jpeg_read_header has finished, we know what is in the SOF
352 and (first) SOS markers. We also have all the application parameter
353 settings. }
355 {LOCAL}
357 var
362 var
364 begin
367 { Initialize dimensions and other stuff }
371 { Width of an output scanline must be representable as JDIMENSION. }
377 { Initialize my private state }
381 { Color quantizer selection }
384 { No mode changes if not using buffered-image mode. }
386 begin
392 begin
395 { 2-pass quantizer only works in 3-component color space. }
397 begin
402 end
403 else
405 begin
407 end
408 else
410 begin
412 end
413 else
414 begin
419 begin
420 {$ifdef QUANT_1PASS_SUPPORTED}
423 {$else}
425 {$endif}
428 { We use the 2-pass code to map to external colormaps. }
430 begin
431 {$ifdef QUANT_2PASS_SUPPORTED}
434 {$else}
436 {$endif}
438 { If both quantizers are initialized, the 2-pass one is left active;
439 this is necessary for starting with quantization to an external map. }
442 { Post-processing: in particular, color conversion first }
444 begin
446 begin
447 {$ifdef UPSAMPLE_MERGING_SUPPORTED}
449 {$else}
451 {$endif}
452 end
453 else
454 begin
460 { Inverse DCT }
462 { Entropy decoding: either Huffman or arithmetic coding. }
464 begin
466 end
467 else
468 begin
470 begin
471 {$ifdef D_PROGRESSIVE_SUPPORTED}
473 {$else}
475 {$endif}
476 end
477 else
481 { Initialize principal buffer controllers. }
488 { We can now tell the memory manager to allocate virtual arrays. }
491 { Initialize input side of decompressor to consume first scan. }
494 {$ifdef D_MULTISCAN_FILES_SUPPORTED}
495 { If jpeg_start_decompress will read the whole file, initialize
496 progress monitoring appropriately. The input step is counted
497 as one pass. }
501 begin
503 { Estimate number of scans to set pass_limit. }
505 begin
506 { Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. }
508 end
509 else
510 begin
511 { For a nonprogressive multiscan file, estimate 1 scan per component. }
519 else
521 { Count the input pass as done }
528 { Per-pass setup.
529 This is called at the beginning of each output pass. We determine which
530 modules will be active during this pass and give them appropriate
531 start_pass calls. We also set is_dummy_pass to indicate whether this
532 is a "real" output pass or a dummy pass for color quantization.
533 (In the latter case, jdapistd.c will crank the pass to completion.) }
535 {METHODDEF}
537 var
539 begin
543 begin
544 {$ifdef QUANT_2PASS_SUPPORTED}
545 { Final pass of 2-pass quantization }
550 {$else}
553 end
554 else
555 begin
557 begin
558 { Select new quantization method }
560 begin
563 end
564 else
566 begin
568 end
569 else
570 begin
577 begin
585 else
591 { Set up progress monitor's pass info if present }
593 begin
597 else
599 { In buffered-image mode, we assume one more output pass if EOI not
600 yet reached, but no more passes if EOI has been reached. }
603 begin
606 else
613 { Finish up at end of an output pass. }
615 {METHODDEF}
617 var
619 begin
628 {$ifdef D_MULTISCAN_FILES_SUPPORTED}
630 { Switch to a new external colormap between output passes. }
632 {GLOBAL}
634 var
636 begin
639 { Prevent application from calling me at wrong times }
645 begin
646 { Select 2-pass quantizer for external colormap use }
648 { Notify quantizer of colormap change }
651 end
652 else
659 { Initialize master decompression control and select active modules.
660 This is performed at the start of jpeg_start_decompress. }
662 {GLOBAL}
664 var
666 begin