4 { This file is part of the Independent JPEG Group's software.
5 For conditions of distribution and use, see the accompanying README file.
7 This file contains the main buffer controller for decompression.
8 The main buffer lies between the JPEG decompressor proper and the
9 post-processor; it holds downsampled data in the JPEG colorspace.
11 Note that this code is bypassed in raw-data mode, since the application
12 supplies the equivalent of the main buffer in that case. }
14 { Original: jdmainct.c ; Copyright (C) 1994-1996, Thomas G. Lane. }
17 { In the current system design, the main buffer need never be a full-image
18 buffer; any full-height buffers will be found inside the coefficient or
19 postprocessing controllers. Nonetheless, the main controller is not
20 trivial. Its responsibility is to provide context rows for upsampling/
21 rescaling, and doing this in an efficient fashion is a bit tricky.
23 Postprocessor input data is counted in "row groups". A row group
24 is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
25 sample rows of each component. (We require DCT_scaled_size values to be
26 chosen such that these numbers are integers. In practice DCT_scaled_size
27 values will likely be powers of two, so we actually have the stronger
28 condition that DCT_scaled_size / min_DCT_scaled_size is an integer.)
29 Upsampling will typically produce max_v_samp_factor pixel rows from each
30 row group (times any additional scale factor that the upsampler is
31 applying).
33 The coefficient controller will deliver data to us one iMCU row at a time;
34 each iMCU row contains v_samp_factor * DCT_scaled_size sample rows, or
35 exactly min_DCT_scaled_size row groups. (This amount of data corresponds
36 to one row of MCUs when the image is fully interleaved.) Note that the
37 number of sample rows varies across components, but the number of row
38 groups does not. Some garbage sample rows may be included in the last iMCU
39 row at the bottom of the image.
41 Depending on the vertical scaling algorithm used, the upsampler may need
42 access to the sample row(s) above and below its current input row group.
43 The upsampler is required to set need_context_rows TRUE at global
44 selection
45 time if so. When need_context_rows is FALSE, this controller can simply
46 obtain one iMCU row at a time from the coefficient controller and dole it
47 out as row groups to the postprocessor.
49 When need_context_rows is TRUE, this controller guarantees that the buffer
50 passed to postprocessing contains at least one row group's worth of samples
51 above and below the row group(s) being processed. Note that the context
52 rows "above" the first passed row group appear at negative row offsets in
53 the passed buffer. At the top and bottom of the image, the required
54 context rows are manufactured by duplicating the first or last real sample
55 row; this avoids having special cases in the upsampling inner loops.
57 The amount of context is fixed at one row group just because that's a
58 convenient number for this controller to work with. The existing
59 upsamplers really only need one sample row of context. An upsampler
60 supporting arbitrary output rescaling might wish for more than one row
61 group of context when shrinking the image; tough, we don't handle that.
62 (This is justified by the assumption that downsizing will be handled mostly
63 by adjusting the DCT_scaled_size values, so that the actual scale factor at
64 the upsample step needn't be much less than one.)
66 To provide the desired context, we have to retain the last two row groups
67 of one iMCU row while reading in the next iMCU row. (The last row group
68 can't be processed until we have another row group for its below-context,
69 and so we have to save the next-to-last group too for its above-context.)
70 We could do this most simply by copying data around in our buffer, but
71 that'd be very slow. We can avoid copying any data by creating a rather
72 strange pointer structure. Here's how it works. We allocate a workspace
73 consisting of M+2 row groups (where M = min_DCT_scaled_size is the number
74 of row groups per iMCU row). We create two sets of redundant pointers to
75 the workspace. Labeling the physical row groups 0 to M+1, the synthesized
76 pointer lists look like this:
77 M+1 M-1
78 master pointer --> 0 master pointer --> 0
79 1 1
80 ... ...
81 M-3 M-3
82 M-2 M
83 M-1 M+1
84 M M-2
85 M+1 M-1
86 0 0
87 We read alternate iMCU rows using each master pointer; thus the last two
88 row groups of the previous iMCU row remain un-overwritten in the workspace.
89 The pointer lists are set up so that the required context rows appear to
90 be adjacent to the proper places when we pass the pointer lists to the
91 upsampler.
93 The above pictures describe the normal state of the pointer lists.
94 At top and bottom of the image, we diddle the pointer lists to duplicate
95 the first or last sample row as necessary (this is cheaper than copying
96 sample rows around).
98 This scheme breaks down if M < 2, ie, min_DCT_scaled_size is 1. In that
99 situation each iMCU row provides only one row group so the buffering logic
100 must be different (eg, we must read two iMCU rows before we can emit the
101 first row group). For now, we simply do not support providing context
102 rows when min_DCT_scaled_size is 1. That combination seems unlikely to
103 be worth providing --- if someone wants a 1/8th-size preview, they probably
104 want it quick and dirty, so a context-free upsampler is sufficient. }
106 interface
108 {$I imjconfig.inc}
110 uses
111 imjmorecfg,
112 imjinclude,
113 {$ifdef QUANT_2PASS_SUPPORTED}
114 imjquant2,
115 {$endif}
116 imjdeferr,
117 imjerror,
118 imjpeglib;
121 {GLOBAL}
126 implementation
128 { Private buffer controller object }
130 type
135 { Pointer to allocated workspace (M or M+2 row groups). }
141 { Remaining fields are only used in the context case. }
143 { These are the master pointers to the funny-order pointer lists. }
153 { context_state values: }
154 const
160 { Forward declarations }
161 {METHODDEF}
166 {METHODDEF}
172 {$ifdef QUANT_2PASS_SUPPORTED}
173 {METHODDEF}
178 {$endif}
181 {LOCAL}
183 { Allocate space for the funny pointer lists.
184 This is done only once, not once per pass. }
185 var
191 begin
195 { Get top-level space for component array pointers.
196 We alloc both arrays with one call to save a few cycles. }
205 begin
208 { Get space for pointer lists --- M+4 row groups in each list.
209 We alloc both pointer lists with one call to save a few cycles. }
222 {LOCAL}
224 { Create the funny pointer lists discussed in the comments above.
225 The actual workspace is already allocated (in main^.buffer),
226 and the space for the pointer lists is allocated too.
227 This routine just fills in the curiously ordered lists.
228 This will be repeated at the beginning of each pass. }
229 var
235 var
237 begin
243 begin
248 { First copy the workspace pointers as-is }
251 begin
255 { In the second list, put the last four row groups in swapped order }
257 begin
261 { The wraparound pointers at top and bottom will be filled later
262 (see set_wraparound_pointers, below). Initially we want the "above"
263 pointers to duplicate the first actual data line. This only needs
264 to happen in xbuffer[0]. }
270 begin
271 {xbuf0^[i - rgroup] := xbuf0^[0];}
279 {LOCAL}
281 { Set up the "wraparound" pointers at top and bottom of the pointer lists.
282 This changes the pointer list state from top-of-image to the normal state. }
283 var
289 var
290 help_xbuf0,
292 begin
298 begin
310 begin
311 {xbuf0^[i - rgroup] := xbuf0^[rgroup*(M+1) + i];
312 xbuf1^[i - rgroup] := xbuf1^[rgroup*(M+1) + i];}
325 {LOCAL}
327 { Change the pointer lists to duplicate the last sample row at the bottom
328 of the image. whichptr indicates which xbuffer holds the final iMCU row.
329 Also sets rowgroups_avail to indicate number of nondummy row groups in row. }
330 var
335 begin
340 begin
341 { Count sample rows in one iMCU row and in one row group }
344 { Count nondummy sample rows remaining for this component }
348 { Count nondummy row groups. Should get same answer for each component,
349 so we need only do it once. }
351 begin
354 { Duplicate the last real sample row rgroup*2 times; this pads out the
355 last partial rowgroup and ensures at least one full rowgroup of context. }
359 begin
367 { Initialize for a processing pass. }
369 {METHODDEF}
372 var
374 begin
378 JBUF_PASS_THRU:
379 begin
381 begin
387 end
388 else
389 begin
390 { Simple case with no context needed }
396 {$ifdef QUANT_2PASS_SUPPORTED}
397 JBUF_CRANK_DEST:
398 { For last pass of 2-pass quantization, just crank the postprocessor }
400 {$endif}
401 else
407 { Process some data.
408 This handles the simple case where no context is required. }
410 {METHODDEF}
415 var
418 var
420 begin
424 { Read input data if we haven't filled the main buffer yet }
426 begin
432 { There are always min_DCT_scaled_size row groups in an iMCU row. }
434 { Note: at the bottom of the image, we may pass extra garbage row groups
435 to the postprocessor. The postprocessor has to check for bottom
436 of image anyway (at row resolution), so no point in us doing it too. }
438 { Feed the postprocessor }
443 { Has postprocessor consumed all the data yet? If so, mark buffer empty }
445 begin
452 { Process some data.
453 This handles the case where context rows must be provided. }
455 {METHODDEF}
460 var
462 begin
465 { Read input data if we haven't filled the main buffer yet }
467 begin
475 { Postprocessor typically will not swallow all the input data it is handed
476 in one call (due to filling the output buffer first). Must be prepared
477 to exit and restart. This switch lets us keep track of how far we got.
478 Note that each case falls through to the next on successful completion. }
481 CTX_POSTPONED_ROW:
482 begin
483 { Call postprocessor using previously set pointers for postponed row }
495 CTX_POSTPONED_ROW,
497 begin
498 { Prepare to process first M-1 row groups of this iMCU row }
501 { Check for bottom of image: if so, tweak pointers to "duplicate"
502 the last sample row, and adjust rowgroups_avail to ignore padding rows. }
511 CTX_POSTPONED_ROW,
513 CTX_PROCESS_IMCU:
514 begin
515 { Call postprocessor using previously set pointers }
521 { After the first iMCU, change wraparound pointers to normal state }
524 { Prepare to load new iMCU row using other xbuffer list }
527 { Still need to process last row group of this iMCU row, }
528 { which is saved at index M+1 of the other xbuffer }
537 { Process some data.
538 Final pass of two-pass quantization: just call the postprocessor.
539 Source data will be the postprocessor controller's internal buffer. }
541 {$ifdef QUANT_2PASS_SUPPORTED}
543 {METHODDEF}
548 var
550 begin
553 in_row_group_ctr,
555 output_buf,
556 out_row_ctr,
557 out_rows_avail);
563 { Initialize main buffer controller. }
565 {GLOBAL}
568 var
572 begin
582 { Allocate the workspace.
583 ngroups is the number of row groups we need.}
586 begin
591 end
592 else
593 begin
599 begin