DEADSOFTWARE

new code for blood particles (other particles are turned off temporarily): almost...
[d2df-sdl.git] / src / lib / vampimg / JpegLib / imjdcoefct.pas
1 unit imjdcoefct;
3 { This file contains the coefficient buffer controller for decompression.
4 This controller is the top level of the JPEG decompressor proper.
5 The coefficient buffer lies between entropy decoding and inverse-DCT steps.
7 In buffered-image mode, this controller is the interface between
8 input-oriented processing and output-oriented processing.
9 Also, the input side (only) is used when reading a file for transcoding. }
11 { Original: jdcoefct.c ; Copyright (C) 1994-1997, Thomas G. Lane. }
13 interface
15 {$I imjconfig.inc}
17 uses
18 imjmorecfg,
19 imjinclude,
20 imjdeferr,
21 imjerror,
22 imjutils,
23 imjpeglib;
25 {GLOBAL}
26 procedure jinit_d_coef_controller (cinfo : j_decompress_ptr;
27 need_full_buffer : boolean);
30 implementation
33 { Block smoothing is only applicable for progressive JPEG, so: }
34 {$ifndef D_PROGRESSIVE_SUPPORTED}
35 {$undef BLOCK_SMOOTHING_SUPPORTED}
36 {$endif}
38 { Private buffer controller object }
40 {$ifdef BLOCK_SMOOTHING_SUPPORTED}
41 const
42 SAVED_COEFS = 6; { we save coef_bits[0..5] }
43 type
44 Latch = array[0..SAVED_COEFS-1] of int;
45 Latch_ptr = ^Latch;
46 {$endif}
48 type
49 my_coef_ptr = ^my_coef_controller;
50 my_coef_controller = record
51 pub : jpeg_d_coef_controller; { public fields }
53 { These variables keep track of the current location of the input side. }
54 { cinfo^.input_iMCU_row is also used for this. }
55 MCU_ctr : JDIMENSION; { counts MCUs processed in current row }
56 MCU_vert_offset : int; { counts MCU rows within iMCU row }
57 MCU_rows_per_iMCU_row : int; { number of such rows needed }
59 { The output side's location is represented by cinfo^.output_iMCU_row. }
61 { In single-pass modes, it's sufficient to buffer just one MCU.
62 We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
63 and let the entropy decoder write into that workspace each time.
64 (On 80x86, the workspace is FAR even though it's not really very big;
65 this is to keep the module interfaces unchanged when a large coefficient
66 buffer is necessary.)
67 In multi-pass modes, this array points to the current MCU's blocks
68 within the virtual arrays; it is used only by the input side. }
70 MCU_buffer : array[0..D_MAX_BLOCKS_IN_MCU-1] of JBLOCKROW;
72 {$ifdef D_MULTISCAN_FILES_SUPPORTED}
73 { In multi-pass modes, we need a virtual block array for each component. }
74 whole_image : jvirt_barray_tbl;
75 {$endif}
77 {$ifdef BLOCK_SMOOTHING_SUPPORTED}
78 { When doing block smoothing, we latch coefficient Al values here }
79 coef_bits_latch : Latch_Ptr;
80 {$endif}
81 end;
83 { Forward declarations }
84 {METHODDEF}
85 function decompress_onepass (cinfo : j_decompress_ptr;
86 output_buf : JSAMPIMAGE) : int; forward;
87 {$ifdef D_MULTISCAN_FILES_SUPPORTED}
88 {METHODDEF}
89 function decompress_data (cinfo : j_decompress_ptr;
90 output_buf : JSAMPIMAGE) : int; forward;
91 {$endif}
92 {$ifdef BLOCK_SMOOTHING_SUPPORTED}
93 {LOCAL}
94 function smoothing_ok (cinfo : j_decompress_ptr) : boolean; forward;
96 {METHODDEF}
97 function decompress_smooth_data (cinfo : j_decompress_ptr;
98 output_buf : JSAMPIMAGE) : int; forward;
99 {$endif}
102 {LOCAL}
103 procedure start_iMCU_row (cinfo : j_decompress_ptr);
104 { Reset within-iMCU-row counters for a new row (input side) }
105 var
106 coef : my_coef_ptr;
107 begin
108 coef := my_coef_ptr (cinfo^.coef);
110 { In an interleaved scan, an MCU row is the same as an iMCU row.
111 In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
112 But at the bottom of the image, process only what's left. }
114 if (cinfo^.comps_in_scan > 1) then
115 begin
116 coef^.MCU_rows_per_iMCU_row := 1;
117 end
118 else
119 begin
120 if (cinfo^.input_iMCU_row < (cinfo^.total_iMCU_rows-1)) then
121 coef^.MCU_rows_per_iMCU_row := cinfo^.cur_comp_info[0]^.v_samp_factor
122 else
123 coef^.MCU_rows_per_iMCU_row := cinfo^.cur_comp_info[0]^.last_row_height;
124 end;
126 coef^.MCU_ctr := 0;
127 coef^.MCU_vert_offset := 0;
128 end;
131 { Initialize for an input processing pass. }
133 {METHODDEF}
134 procedure start_input_pass (cinfo : j_decompress_ptr);
135 begin
136 cinfo^.input_iMCU_row := 0;
137 start_iMCU_row(cinfo);
138 end;
141 { Initialize for an output processing pass. }
143 {METHODDEF}
144 procedure start_output_pass (cinfo : j_decompress_ptr);
145 var
146 coef : my_coef_ptr;
147 begin
148 {$ifdef BLOCK_SMOOTHING_SUPPORTED}
149 coef := my_coef_ptr (cinfo^.coef);
151 { If multipass, check to see whether to use block smoothing on this pass }
152 if (coef^.pub.coef_arrays <> NIL) then
153 begin
154 if (cinfo^.do_block_smoothing) and smoothing_ok(cinfo) then
155 coef^.pub.decompress_data := decompress_smooth_data
156 else
157 coef^.pub.decompress_data := decompress_data;
158 end;
159 {$endif}
160 cinfo^.output_iMCU_row := 0;
161 end;
164 { Decompress and return some data in the single-pass case.
165 Always attempts to emit one fully interleaved MCU row ("iMCU" row).
166 Input and output must run in lockstep since we have only a one-MCU buffer.
167 Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
169 NB: output_buf contains a plane for each component in image,
170 which we index according to the component's SOF position.}
172 {METHODDEF}
173 function decompress_onepass (cinfo : j_decompress_ptr;
174 output_buf : JSAMPIMAGE) : int;
175 var
176 coef : my_coef_ptr;
177 MCU_col_num : JDIMENSION; { index of current MCU within row }
178 last_MCU_col : JDIMENSION;
179 last_iMCU_row : JDIMENSION;
180 blkn, ci, xindex, yindex, yoffset, useful_width : int;
181 output_ptr : JSAMPARRAY;
182 start_col, output_col : JDIMENSION;
183 compptr : jpeg_component_info_ptr;
184 inverse_DCT : inverse_DCT_method_ptr;
185 begin
186 coef := my_coef_ptr (cinfo^.coef);
187 last_MCU_col := cinfo^.MCUs_per_row - 1;
188 last_iMCU_row := cinfo^.total_iMCU_rows - 1;
190 { Loop to process as much as one whole iMCU row }
191 for yoffset := coef^.MCU_vert_offset to pred(coef^.MCU_rows_per_iMCU_row) do
192 begin
193 for MCU_col_num := coef^.MCU_ctr to last_MCU_col do
194 begin
195 { Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. }
196 jzero_far( coef^.MCU_buffer[0],
197 size_t (cinfo^.blocks_in_MCU * SIZEOF(JBLOCK)));
198 if (not cinfo^.entropy^.decode_mcu (cinfo, coef^.MCU_buffer)) then
199 begin
200 { Suspension forced; update state counters and exit }
201 coef^.MCU_vert_offset := yoffset;
202 coef^.MCU_ctr := MCU_col_num;
203 decompress_onepass := JPEG_SUSPENDED;
204 exit;
205 end;
206 { Determine where data should go in output_buf and do the IDCT thing.
207 We skip dummy blocks at the right and bottom edges (but blkn gets
208 incremented past them!). Note the inner loop relies on having
209 allocated the MCU_buffer[] blocks sequentially. }
211 blkn := 0; { index of current DCT block within MCU }
212 for ci := 0 to pred(cinfo^.comps_in_scan) do
213 begin
214 compptr := cinfo^.cur_comp_info[ci];
215 { Don't bother to IDCT an uninteresting component. }
216 if (not compptr^.component_needed) then
217 begin
218 Inc(blkn, compptr^.MCU_blocks);
219 continue;
220 end;
221 inverse_DCT := cinfo^.idct^.inverse_DCT[compptr^.component_index];
222 if (MCU_col_num < last_MCU_col) then
223 useful_width := compptr^.MCU_width
224 else
225 useful_width := compptr^.last_col_width;
227 output_ptr := JSAMPARRAY(@ output_buf^[compptr^.component_index]^
228 [yoffset * compptr^.DCT_scaled_size]);
229 start_col := LongInt(MCU_col_num) * compptr^.MCU_sample_width;
230 for yindex := 0 to pred(compptr^.MCU_height) do
231 begin
232 if (cinfo^.input_iMCU_row < last_iMCU_row) or
233 (yoffset+yindex < compptr^.last_row_height) then
234 begin
235 output_col := start_col;
236 for xindex := 0 to pred(useful_width) do
237 begin
238 inverse_DCT (cinfo, compptr,
239 JCOEFPTR(coef^.MCU_buffer[blkn+xindex]),
240 output_ptr, output_col);
241 Inc(output_col, compptr^.DCT_scaled_size);
242 end;
243 end;
244 Inc(blkn, compptr^.MCU_width);
245 Inc(JSAMPROW_PTR(output_ptr), compptr^.DCT_scaled_size);
246 end;
247 end;
248 end;
249 { Completed an MCU row, but perhaps not an iMCU row }
250 coef^.MCU_ctr := 0;
251 end;
252 { Completed the iMCU row, advance counters for next one }
253 Inc(cinfo^.output_iMCU_row);
255 Inc(cinfo^.input_iMCU_row);
256 if (cinfo^.input_iMCU_row < cinfo^.total_iMCU_rows) then
257 begin
258 start_iMCU_row(cinfo);
259 decompress_onepass := JPEG_ROW_COMPLETED;
260 exit;
261 end;
262 { Completed the scan }
263 cinfo^.inputctl^.finish_input_pass (cinfo);
264 decompress_onepass := JPEG_SCAN_COMPLETED;
265 end;
267 { Dummy consume-input routine for single-pass operation. }
269 {METHODDEF}
270 function dummy_consume_data (cinfo : j_decompress_ptr) : int;
271 begin
272 dummy_consume_data := JPEG_SUSPENDED; { Always indicate nothing was done }
273 end;
276 {$ifdef D_MULTISCAN_FILES_SUPPORTED}
278 { Consume input data and store it in the full-image coefficient buffer.
279 We read as much as one fully interleaved MCU row ("iMCU" row) per call,
280 ie, v_samp_factor block rows for each component in the scan.
281 Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.}
283 {METHODDEF}
284 function consume_data (cinfo : j_decompress_ptr) : int;
285 var
286 coef : my_coef_ptr;
287 MCU_col_num : JDIMENSION; { index of current MCU within row }
288 blkn, ci, xindex, yindex, yoffset : int;
289 start_col : JDIMENSION;
290 buffer : array[0..MAX_COMPS_IN_SCAN-1] of JBLOCKARRAY;
291 buffer_ptr : JBLOCKROW;
292 compptr : jpeg_component_info_ptr;
293 begin
294 coef := my_coef_ptr (cinfo^.coef);
296 { Align the virtual buffers for the components used in this scan. }
297 for ci := 0 to pred(cinfo^.comps_in_scan) do
298 begin
299 compptr := cinfo^.cur_comp_info[ci];
300 buffer[ci] := cinfo^.mem^.access_virt_barray
301 (j_common_ptr (cinfo), coef^.whole_image[compptr^.component_index],
302 LongInt(cinfo^.input_iMCU_row) * compptr^.v_samp_factor,
303 JDIMENSION (compptr^.v_samp_factor), TRUE);
304 { Note: entropy decoder expects buffer to be zeroed,
305 but this is handled automatically by the memory manager
306 because we requested a pre-zeroed array. }
308 end;
310 { Loop to process one whole iMCU row }
311 for yoffset := coef^.MCU_vert_offset to pred(coef^.MCU_rows_per_iMCU_row) do
312 begin
313 for MCU_col_num := coef^.MCU_ctr to pred(cinfo^.MCUs_per_row) do
314 begin
315 { Construct list of pointers to DCT blocks belonging to this MCU }
316 blkn := 0; { index of current DCT block within MCU }
317 for ci := 0 to pred(cinfo^.comps_in_scan) do
318 begin
319 compptr := cinfo^.cur_comp_info[ci];
320 start_col := LongInt(MCU_col_num) * compptr^.MCU_width;
321 for yindex := 0 to pred(compptr^.MCU_height) do
322 begin
323 buffer_ptr := JBLOCKROW(@ buffer[ci]^[yindex+yoffset]^[start_col]);
324 for xindex := 0 to pred(compptr^.MCU_width) do
325 begin
326 coef^.MCU_buffer[blkn] := buffer_ptr;
327 Inc(blkn);
328 Inc(JBLOCK_PTR(buffer_ptr));
329 end;
330 end;
331 end;
332 { Try to fetch the MCU. }
333 if (not cinfo^.entropy^.decode_mcu (cinfo, coef^.MCU_buffer)) then
334 begin
335 { Suspension forced; update state counters and exit }
336 coef^.MCU_vert_offset := yoffset;
337 coef^.MCU_ctr := MCU_col_num;
338 consume_data := JPEG_SUSPENDED;
339 exit;
340 end;
341 end;
342 { Completed an MCU row, but perhaps not an iMCU row }
343 coef^.MCU_ctr := 0;
344 end;
345 { Completed the iMCU row, advance counters for next one }
346 Inc(cinfo^.input_iMCU_row);
347 if (cinfo^.input_iMCU_row < cinfo^.total_iMCU_rows) then
348 begin
349 start_iMCU_row(cinfo);
350 consume_data := JPEG_ROW_COMPLETED;
351 exit;
352 end;
353 { Completed the scan }
354 cinfo^.inputctl^.finish_input_pass (cinfo);
355 consume_data := JPEG_SCAN_COMPLETED;
356 end;
359 { Decompress and return some data in the multi-pass case.
360 Always attempts to emit one fully interleaved MCU row ("iMCU" row).
361 Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
363 NB: output_buf contains a plane for each component in image. }
365 {METHODDEF}
366 function decompress_data (cinfo : j_decompress_ptr;
367 output_buf : JSAMPIMAGE) : int;
368 var
369 coef : my_coef_ptr;
370 last_iMCU_row : JDIMENSION;
371 block_num : JDIMENSION;
372 ci, block_row, block_rows : int;
373 buffer : JBLOCKARRAY;
374 buffer_ptr : JBLOCKROW;
375 output_ptr : JSAMPARRAY;
376 output_col : JDIMENSION;
377 compptr : jpeg_component_info_ptr;
378 inverse_DCT : inverse_DCT_method_ptr;
379 begin
380 coef := my_coef_ptr (cinfo^.coef);
381 last_iMCU_row := cinfo^.total_iMCU_rows - 1;
383 { Force some input to be done if we are getting ahead of the input. }
384 while (cinfo^.input_scan_number < cinfo^.output_scan_number) or
385 ((cinfo^.input_scan_number = cinfo^.output_scan_number) and
386 (LongInt(cinfo^.input_iMCU_row) <= cinfo^.output_iMCU_row)) do
387 begin
388 if (cinfo^.inputctl^.consume_input(cinfo) = JPEG_SUSPENDED) then
389 begin
390 decompress_data := JPEG_SUSPENDED;
391 exit;
392 end;
393 end;
395 { OK, output from the virtual arrays. }
396 compptr := jpeg_component_info_ptr(cinfo^.comp_info);
397 for ci := 0 to pred(cinfo^.num_components) do
398 begin
399 { Don't bother to IDCT an uninteresting component. }
400 if (not compptr^.component_needed) then
401 continue;
402 { Align the virtual buffer for this component. }
403 buffer := cinfo^.mem^.access_virt_barray
404 (j_common_ptr (cinfo), coef^.whole_image[ci],
405 cinfo^.output_iMCU_row * compptr^.v_samp_factor,
406 JDIMENSION (compptr^.v_samp_factor), FALSE);
407 { Count non-dummy DCT block rows in this iMCU row. }
408 if (cinfo^.output_iMCU_row < LongInt(last_iMCU_row)) then
409 block_rows := compptr^.v_samp_factor
410 else
411 begin
412 { NB: can't use last_row_height here; it is input-side-dependent! }
413 block_rows := int(LongInt(compptr^.height_in_blocks) mod compptr^.v_samp_factor);
414 if (block_rows = 0) then
415 block_rows := compptr^.v_samp_factor;
416 end;
417 inverse_DCT := cinfo^.idct^.inverse_DCT[ci];
418 output_ptr := output_buf^[ci];
419 { Loop over all DCT blocks to be processed. }
420 for block_row := 0 to pred(block_rows) do
421 begin
422 buffer_ptr := buffer^[block_row];
423 output_col := 0;
424 for block_num := 0 to pred(compptr^.width_in_blocks) do
425 begin
426 inverse_DCT (cinfo, compptr, JCOEFPTR (buffer_ptr),
427 output_ptr, output_col);
428 Inc(JBLOCK_PTR(buffer_ptr));
429 Inc(output_col, compptr^.DCT_scaled_size);
430 end;
431 Inc(JSAMPROW_PTR(output_ptr), compptr^.DCT_scaled_size);
432 end;
433 Inc(compptr);
434 end;
436 Inc(cinfo^.output_iMCU_row);
437 if (cinfo^.output_iMCU_row < LongInt(cinfo^.total_iMCU_rows)) then
438 begin
439 decompress_data := JPEG_ROW_COMPLETED;
440 exit;
441 end;
442 decompress_data := JPEG_SCAN_COMPLETED;
443 end;
445 {$endif} { D_MULTISCAN_FILES_SUPPORTED }
448 {$ifdef BLOCK_SMOOTHING_SUPPORTED}
450 { This code applies interblock smoothing as described by section K.8
451 of the JPEG standard: the first 5 AC coefficients are estimated from
452 the DC values of a DCT block and its 8 neighboring blocks.
453 We apply smoothing only for progressive JPEG decoding, and only if
454 the coefficients it can estimate are not yet known to full precision. }
456 { Natural-order array positions of the first 5 zigzag-order coefficients }
457 const
458 Q01_POS = 1;
459 Q10_POS = 8;
460 Q20_POS = 16;
461 Q11_POS = 9;
462 Q02_POS = 2;
464 { Determine whether block smoothing is applicable and safe.
465 We also latch the current states of the coef_bits[] entries for the
466 AC coefficients; otherwise, if the input side of the decompressor
467 advances into a new scan, we might think the coefficients are known
468 more accurately than they really are. }
470 {LOCAL}
471 function smoothing_ok (cinfo : j_decompress_ptr) : boolean;
472 var
473 coef : my_coef_ptr;
474 smoothing_useful : boolean;
475 ci, coefi : int;
476 compptr : jpeg_component_info_ptr;
477 qtable : JQUANT_TBL_PTR;
478 coef_bits : coef_bits_ptr;
479 coef_bits_latch : Latch_Ptr;
480 begin
481 coef := my_coef_ptr (cinfo^.coef);
482 smoothing_useful := FALSE;
484 if (not cinfo^.progressive_mode) or (cinfo^.coef_bits = NIL) then
485 begin
486 smoothing_ok := FALSE;
487 exit;
488 end;
490 { Allocate latch area if not already done }
491 if (coef^.coef_bits_latch = NIL) then
492 coef^.coef_bits_latch := Latch_Ptr(
493 cinfo^.mem^.alloc_small (j_common_ptr (cinfo), JPOOL_IMAGE,
494 cinfo^.num_components *
495 (SAVED_COEFS * SIZEOF(int))) );
496 coef_bits_latch := (coef^.coef_bits_latch);
498 compptr := jpeg_component_info_ptr(cinfo^.comp_info);
499 for ci := 0 to pred(cinfo^.num_components) do
500 begin
501 { All components' quantization values must already be latched. }
502 qtable := compptr^.quant_table;
503 if (qtable = NIL) then
504 begin
505 smoothing_ok := FALSE;
506 exit;
507 end;
508 { Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. }
509 if (qtable^.quantval[0] = 0) or
510 (qtable^.quantval[Q01_POS] = 0) or
511 (qtable^.quantval[Q10_POS] = 0) or
512 (qtable^.quantval[Q20_POS] = 0) or
513 (qtable^.quantval[Q11_POS] = 0) or
514 (qtable^.quantval[Q02_POS] = 0) then
515 begin
516 smoothing_ok := FALSE;
517 exit;
518 end;
519 { DC values must be at least partly known for all components. }
520 coef_bits := @cinfo^.coef_bits^[ci]; { Nomssi }
521 if (coef_bits^[0] < 0) then
522 begin
523 smoothing_ok := FALSE;
524 exit;
525 end;
526 { Block smoothing is helpful if some AC coefficients remain inaccurate. }
527 for coefi := 1 to 5 do
528 begin
529 coef_bits_latch^[coefi] := coef_bits^[coefi];
530 if (coef_bits^[coefi] <> 0) then
531 smoothing_useful := TRUE;
532 end;
533 Inc(coef_bits_latch {SAVED_COEFS});
534 Inc(compptr);
535 end;
537 smoothing_ok := smoothing_useful;
538 end;
541 { Variant of decompress_data for use when doing block smoothing. }
543 {METHODDEF}
544 function decompress_smooth_data (cinfo : j_decompress_ptr;
545 output_buf : JSAMPIMAGE) : int;
546 var
547 coef : my_coef_ptr;
548 last_iMCU_row : JDIMENSION;
549 block_num, last_block_column : JDIMENSION;
550 ci, block_row, block_rows, access_rows : int;
551 buffer : JBLOCKARRAY;
552 buffer_ptr, prev_block_row, next_block_row : JBLOCKROW;
553 output_ptr : JSAMPARRAY;
554 output_col : JDIMENSION;
555 compptr : jpeg_component_info_ptr;
556 inverse_DCT : inverse_DCT_method_ptr;
557 first_row, last_row : boolean;
558 workspace : JBLOCK;
559 coef_bits : Latch_Ptr; { coef_bits_ptr; }
560 quanttbl : JQUANT_TBL_PTR;
561 Q00,Q01,Q02,Q10,Q11,Q20, num : INT32;
562 DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9 : int;
563 Al, pred : int;
564 var
565 delta : JDIMENSION;
566 begin
567 coef := my_coef_ptr (cinfo^.coef);
568 last_iMCU_row := cinfo^.total_iMCU_rows - 1;
570 { Force some input to be done if we are getting ahead of the input. }
571 while (cinfo^.input_scan_number <= cinfo^.output_scan_number) and
572 (not cinfo^.inputctl^.eoi_reached) do
573 begin
574 if (cinfo^.input_scan_number = cinfo^.output_scan_number) then
575 begin
576 { If input is working on current scan, we ordinarily want it to
577 have completed the current row. But if input scan is DC,
578 we want it to keep one row ahead so that next block row's DC
579 values are up to date. }
581 if (cinfo^.Ss = 0) then
582 delta := 1
583 else
584 delta := 0;
585 if (LongInt(cinfo^.input_iMCU_row) > cinfo^.output_iMCU_row+LongInt(delta)) then
586 break;
587 end;
588 if (cinfo^.inputctl^.consume_input(cinfo) = JPEG_SUSPENDED) then
589 begin
590 decompress_smooth_data := JPEG_SUSPENDED;
591 exit;
592 end;
593 end;
595 { OK, output from the virtual arrays. }
596 compptr := jpeg_component_info_ptr(cinfo^.comp_info);
597 for ci := 0 to (cinfo^.num_components-1) do
598 begin
599 { Don't bother to IDCT an uninteresting component. }
600 if (not compptr^.component_needed) then
601 continue;
602 { Count non-dummy DCT block rows in this iMCU row. }
603 if (cinfo^.output_iMCU_row < LongInt(last_iMCU_row)) then
604 begin
605 block_rows := compptr^.v_samp_factor;
606 access_rows := block_rows * 2; { this and next iMCU row }
607 last_row := FALSE;
608 end
609 else
610 begin
611 { NB: can't use last_row_height here; it is input-side-dependent! }
612 block_rows := int (compptr^.height_in_blocks) mod compptr^.v_samp_factor;
613 if (block_rows = 0) then
614 block_rows := compptr^.v_samp_factor;
615 access_rows := block_rows; { this iMCU row only }
616 last_row := TRUE;
617 end;
618 { Align the virtual buffer for this component. }
619 if (cinfo^.output_iMCU_row > 0) then
620 begin
621 Inc(access_rows, compptr^.v_samp_factor); { prior iMCU row too }
622 buffer := cinfo^.mem^.access_virt_barray
623 (j_common_ptr (cinfo), coef^.whole_image[ci],
624 (cinfo^.output_iMCU_row - 1) * compptr^.v_samp_factor,
625 JDIMENSION (access_rows), FALSE);
626 Inc(JBLOCKROW_PTR(buffer), compptr^.v_samp_factor); { point to current iMCU row }
627 first_row := FALSE;
628 end
629 else
630 begin
631 buffer := cinfo^.mem^.access_virt_barray
632 (j_common_ptr (cinfo), coef^.whole_image[ci],
633 JDIMENSION (0), JDIMENSION (access_rows), FALSE);
634 first_row := TRUE;
635 end;
636 { Fetch component-dependent info }
637 coef_bits := coef^.coef_bits_latch;
638 Inc(coef_bits, ci); { ci * SAVED_COEFS}
639 quanttbl := compptr^.quant_table;
640 Q00 := quanttbl^.quantval[0];
641 Q01 := quanttbl^.quantval[Q01_POS];
642 Q10 := quanttbl^.quantval[Q10_POS];
643 Q20 := quanttbl^.quantval[Q20_POS];
644 Q11 := quanttbl^.quantval[Q11_POS];
645 Q02 := quanttbl^.quantval[Q02_POS];
646 inverse_DCT := cinfo^.idct^.inverse_DCT[ci];
647 output_ptr := output_buf^[ci];
648 { Loop over all DCT blocks to be processed. }
649 for block_row := 0 to (block_rows-1) do
650 begin
651 buffer_ptr := buffer^[block_row];
652 if (first_row) and (block_row = 0) then
653 prev_block_row := buffer_ptr
654 else
655 prev_block_row := buffer^[block_row-1];
656 if (last_row) and (block_row = block_rows-1) then
657 next_block_row := buffer_ptr
658 else
659 next_block_row := buffer^[block_row+1];
660 { We fetch the surrounding DC values using a sliding-register approach.
661 Initialize all nine here so as to do the right thing on narrow pics.}
663 DC3 := int(prev_block_row^[0][0]);
664 DC2 := DC3;
665 DC1 := DC2;
666 DC6 := int(buffer_ptr^[0][0]);
667 DC5 := DC6;
668 DC4 := DC5;
669 DC9 := int(next_block_row^[0][0]);
670 DC8 := DC9;
671 DC7 := DC8 ;
672 output_col := 0;
673 last_block_column := compptr^.width_in_blocks - 1;
674 for block_num := 0 to last_block_column do
675 begin
676 { Fetch current DCT block into workspace so we can modify it. }
677 jcopy_block_row(buffer_ptr, JBLOCKROW (@workspace), JDIMENSION(1));
678 { Update DC values }
679 if (block_num < last_block_column) then
680 begin
681 DC3 := int (prev_block_row^[1][0]);
682 DC6 := int (buffer_ptr^[1][0]);
683 DC9 := int (next_block_row^[1][0]);
684 end;
685 { Compute coefficient estimates per K.8.
686 An estimate is applied only if coefficient is still zero,
687 and is not known to be fully accurate. }
689 { AC01 }
690 Al := coef_bits^[1];
691 if (Al <> 0) and (workspace[1] = 0) then
692 begin
693 num := 36 * Q00 * (DC4 - DC6);
694 if (num >= 0) then
695 begin
696 pred := int (((Q01 shl 7) + num) div (Q01 shl 8));
697 if (Al > 0) and (pred >= (1 shl Al)) then
698 pred := (1 shl Al)-1;
699 end
700 else
701 begin
702 pred := int (((Q01 shl 7) - num) div (Q01 shl 8));
703 if (Al > 0) and (pred >= (1 shl Al)) then
704 pred := (1 shl Al)-1;
705 pred := -pred;
706 end;
707 workspace[1] := JCOEF (pred);
708 end;
709 { AC10 }
710 Al := coef_bits^[2];
711 if (Al <> 0) and (workspace[8] = 0) then
712 begin
713 num := 36 * Q00 * (DC2 - DC8);
714 if (num >= 0) then
715 begin
716 pred := int (((Q10 shl 7) + num) div (Q10 shl 8));
717 if (Al > 0) and (pred >= (1 shl Al)) then
718 pred := (1 shl Al)-1;
719 end
720 else
721 begin
722 pred := int (((Q10 shl 7) - num) div (Q10 shl 8));
723 if (Al > 0) and (pred >= (1 shl Al)) then
724 pred := (1 shl Al)-1;
725 pred := -pred;
726 end;
727 workspace[8] := JCOEF (pred);
728 end;
729 { AC20 }
730 Al := coef_bits^[3];
731 if (Al <> 0) and (workspace[16] = 0) then
732 begin
733 num := 9 * Q00 * (DC2 + DC8 - 2*DC5);
734 if (num >= 0) then
735 begin
736 pred := int (((Q20 shl 7) + num) div (Q20 shl 8));
737 if (Al > 0) and (pred >= (1 shl Al)) then
738 pred := (1 shl Al)-1;
739 end
740 else
741 begin
742 pred := int (((Q20 shl 7) - num) div (Q20 shl 8));
743 if (Al > 0) and (pred >= (1 shl Al)) then
744 pred := (1 shl Al)-1;
745 pred := -pred;
746 end;
747 workspace[16] := JCOEF (pred);
748 end;
749 { AC11 }
750 Al := coef_bits^[4];
751 if (Al <> 0) and (workspace[9] = 0) then
752 begin
753 num := 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
754 if (num >= 0) then
755 begin
756 pred := int (((Q11 shl 7) + num) div (Q11 shl 8));
757 if (Al > 0) and (pred >= (1 shl Al)) then
758 pred := (1 shl Al)-1;
759 end
760 else
761 begin
762 pred := int (((Q11 shl 7) - num) div (Q11 shl 8));
763 if (Al > 0) and (pred >= (1 shl Al)) then
764 pred := (1 shl Al)-1;
765 pred := -pred;
766 end;
767 workspace[9] := JCOEF (pred);
768 end;
769 { AC02 }
770 Al := coef_bits^[5];
771 if (Al <> 0) and (workspace[2] = 0) then
772 begin
773 num := 9 * Q00 * (DC4 + DC6 - 2*DC5);
774 if (num >= 0) then
775 begin
776 pred := int (((Q02 shl 7) + num) div (Q02 shl 8));
777 if (Al > 0) and (pred >= (1 shl Al)) then
778 pred := (1 shl Al)-1;
779 end
780 else
781 begin
782 pred := int (((Q02 shl 7) - num) div (Q02 shl 8));
783 if (Al > 0) and (pred >= (1 shl Al)) then
784 pred := (1 shl Al)-1;
785 pred := -pred;
786 end;
787 workspace[2] := JCOEF (pred);
788 end;
789 { OK, do the IDCT }
790 inverse_DCT (cinfo, compptr, JCOEFPTR (@workspace),
791 output_ptr, output_col);
792 { Advance for next column }
793 DC1 := DC2; DC2 := DC3;
794 DC4 := DC5; DC5 := DC6;
795 DC7 := DC8; DC8 := DC9;
796 Inc(JBLOCK_PTR(buffer_ptr));
797 Inc(JBLOCK_PTR(prev_block_row));
798 Inc(JBLOCK_PTR(next_block_row));
799 Inc(output_col, compptr^.DCT_scaled_size);
800 end;
801 Inc(JSAMPROW_PTR(output_ptr), compptr^.DCT_scaled_size);
802 end;
803 Inc(compptr);
804 end;
806 Inc(cinfo^.output_iMCU_row);
807 if (cinfo^.output_iMCU_row < LongInt(cinfo^.total_iMCU_rows)) then
808 begin
809 decompress_smooth_data := JPEG_ROW_COMPLETED;
810 exit;
811 end;
812 decompress_smooth_data := JPEG_SCAN_COMPLETED;
813 end;
815 {$endif} { BLOCK_SMOOTHING_SUPPORTED }
818 { Initialize coefficient buffer controller. }
820 {GLOBAL}
821 procedure jinit_d_coef_controller (cinfo : j_decompress_ptr;
822 need_full_buffer : boolean);
823 var
824 coef : my_coef_ptr;
825 {$ifdef D_MULTISCAN_FILES_SUPPORTED}
826 var
827 ci, access_rows : int;
828 compptr : jpeg_component_info_ptr;
829 {$endif}
830 var
831 buffer : JBLOCK_PTR;
832 i : int;
833 begin
834 coef := my_coef_ptr(
835 cinfo^.mem^.alloc_small (j_common_ptr (cinfo), JPOOL_IMAGE,
836 SIZEOF(my_coef_controller)) );
837 cinfo^.coef := jpeg_d_coef_controller_ptr(coef);
838 coef^.pub.start_input_pass := start_input_pass;
839 coef^.pub.start_output_pass := start_output_pass;
840 {$ifdef BLOCK_SMOOTHING_SUPPORTED}
841 coef^.coef_bits_latch := NIL;
842 {$endif}
844 { Create the coefficient buffer. }
845 if (need_full_buffer) then
846 begin
847 {$ifdef D_MULTISCAN_FILES_SUPPORTED}
848 { Allocate a full-image virtual array for each component, }
849 { padded to a multiple of samp_factor DCT blocks in each direction. }
850 { Note we ask for a pre-zeroed array. }
852 compptr := jpeg_component_info_ptr(cinfo^.comp_info);
853 for ci := 0 to pred(cinfo^.num_components) do
854 begin
855 access_rows := compptr^.v_samp_factor;
856 {$ifdef BLOCK_SMOOTHING_SUPPORTED}
857 { If block smoothing could be used, need a bigger window }
858 if (cinfo^.progressive_mode) then
859 access_rows := access_rows * 3;
860 {$endif}
861 coef^.whole_image[ci] := cinfo^.mem^.request_virt_barray
862 (j_common_ptr (cinfo), JPOOL_IMAGE, TRUE,
863 JDIMENSION (jround_up( long(compptr^.width_in_blocks),
864 long(compptr^.h_samp_factor) )),
865 JDIMENSION (jround_up( long(compptr^.height_in_blocks),
866 long(compptr^.v_samp_factor) )),
867 JDIMENSION (access_rows));
868 Inc(compptr);
869 end;
870 coef^.pub.consume_data := consume_data;
871 coef^.pub.decompress_data := decompress_data;
872 coef^.pub.coef_arrays := @(coef^.whole_image);
873 { link to virtual arrays }
874 {$else}
875 ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
876 {$endif}
877 end
878 else
879 begin
880 { We only need a single-MCU buffer. }
881 buffer := JBLOCK_PTR (
882 cinfo^.mem^.alloc_large (j_common_ptr (cinfo), JPOOL_IMAGE,
883 D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)) );
884 for i := 0 to pred(D_MAX_BLOCKS_IN_MCU) do
885 begin
886 coef^.MCU_buffer[i] := JBLOCKROW(buffer);
887 Inc(buffer);
888 end;
889 coef^.pub.consume_data := dummy_consume_data;
890 coef^.pub.decompress_data := decompress_onepass;
891 coef^.pub.coef_arrays := NIL; { flag for no virtual arrays }
892 end;
893 end;
895 end.