DEADSOFTWARE

fix link libraries on osx
[d2df-sdl.git] / src / lib / vampimg / JpegLib / imjdphuff.pas
1 unit imjdphuff;
3 { This file contains Huffman entropy decoding routines for progressive JPEG.
5 Much of the complexity here has to do with supporting input suspension.
6 If the data source module demands suspension, we want to be able to back
7 up to the start of the current MCU. To do this, we copy state variables
8 into local working storage, and update them back to the permanent
9 storage only upon successful completion of an MCU. }
11 { Original: jdphuff.c ; Copyright (C) 1995-1997, Thomas G. Lane. }
13 interface
15 {$I imjconfig.inc}
17 uses
18 imjmorecfg,
19 imjinclude,
20 imjpeglib,
21 imjdeferr,
22 imjerror,
23 imjutils,
24 imjdhuff; { Declarations shared with jdhuff.c }
27 {GLOBAL}
28 procedure jinit_phuff_decoder (cinfo : j_decompress_ptr);
30 implementation
32 { Expanded entropy decoder object for progressive Huffman decoding.
34 The savable_state subrecord contains fields that change within an MCU,
35 but must not be updated permanently until we complete the MCU. }
37 type
38 savable_state = record
39 EOBRUN : uInt; { remaining EOBs in EOBRUN }
40 last_dc_val : array[00..MAX_COMPS_IN_SCAN-1] of int;
41 { last DC coef for each component }
42 end;
45 type
46 phuff_entropy_ptr = ^phuff_entropy_decoder;
47 phuff_entropy_decoder = record
48 pub : jpeg_entropy_decoder; { public fields }
50 { These fields are loaded into local variables at start of each MCU.
51 In case of suspension, we exit WITHOUT updating them. }
53 bitstate : bitread_perm_state; { Bit buffer at start of MCU }
54 saved : savable_state; { Other state at start of MCU }
56 { These fields are NOT loaded into local working state. }
57 restarts_to_go : uInt; { MCUs left in this restart interval }
59 { Pointers to derived tables (these workspaces have image lifespan) }
60 derived_tbls : array[0..NUM_HUFF_TBLS-1] of d_derived_tbl_ptr;
62 ac_derived_tbl : d_derived_tbl_ptr; { active table during an AC scan }
63 end;
67 { Forward declarations }
68 {METHODDEF}
69 function decode_mcu_DC_first (cinfo : j_decompress_ptr;
70 var MCU_data : array of JBLOCKROW) : boolean;
71 forward;
72 {METHODDEF}
73 function decode_mcu_AC_first (cinfo : j_decompress_ptr;
74 var MCU_data : array of JBLOCKROW) : boolean;
75 forward;
76 {METHODDEF}
77 function decode_mcu_DC_refine (cinfo : j_decompress_ptr;
78 var MCU_data : array of JBLOCKROW) : boolean;
79 forward;
80 {METHODDEF}
81 function decode_mcu_AC_refine (cinfo : j_decompress_ptr;
82 var MCU_data : array of JBLOCKROW) : boolean;
83 forward;
85 { Initialize for a Huffman-compressed scan. }
87 {METHODDEF}
88 procedure start_pass_phuff_decoder (cinfo : j_decompress_ptr);
89 var
90 entropy : phuff_entropy_ptr;
91 is_DC_band, bad : boolean;
92 ci, coefi, tbl : int;
93 coef_bit_ptr : coef_bits_ptr;
94 compptr : jpeg_component_info_ptr;
95 var
96 cindex : int;
97 expected : int;
98 begin
99 entropy := phuff_entropy_ptr (cinfo^.entropy);
101 is_DC_band := (cinfo^.Ss = 0);
103 { Validate scan parameters }
104 bad := FALSE;
105 if (is_DC_band) then
106 begin
107 if (cinfo^.Se <> 0) then
108 bad := TRUE;
109 end
110 else
111 begin
112 { need not check Ss/Se < 0 since they came from unsigned bytes }
113 if (cinfo^.Ss > cinfo^.Se) or (cinfo^.Se >= DCTSIZE2) then
114 bad := TRUE;
115 { AC scans may have only one component }
116 if (cinfo^.comps_in_scan <> 1) then
117 bad := TRUE;
118 end;
119 if (cinfo^.Ah <> 0) then
120 begin
121 { Successive approximation refinement scan: must have Al = Ah-1. }
122 if (cinfo^.Al <> cinfo^.Ah-1) then
123 bad := TRUE;
124 end;
125 if (cinfo^.Al > 13) then { need not check for < 0 }
126 bad := TRUE;
127 { Arguably the maximum Al value should be less than 13 for 8-bit precision,
128 but the spec doesn't say so, and we try to be liberal about what we
129 accept. Note: large Al values could result in out-of-range DC
130 coefficients during early scans, leading to bizarre displays due to
131 overflows in the IDCT math. But we won't crash. }
133 if (bad) then
134 ERREXIT4(j_common_ptr(cinfo), JERR_BAD_PROGRESSION,
135 cinfo^.Ss, cinfo^.Se, cinfo^.Ah, cinfo^.Al);
136 { Update progression status, and verify that scan order is legal.
137 Note that inter-scan inconsistencies are treated as warnings
138 not fatal errors ... not clear if this is right way to behave. }
140 for ci := 0 to pred(cinfo^.comps_in_scan) do
141 begin
142 cindex := cinfo^.cur_comp_info[ci]^.component_index;
143 coef_bit_ptr := coef_bits_ptr(@(cinfo^.coef_bits^[cindex])); {^[0] ???
144 Nomssi }
145 if (not is_DC_band) and (coef_bit_ptr^[0] < 0) then
146 { AC without prior DC scan }
147 WARNMS2(j_common_ptr(cinfo), JWRN_BOGUS_PROGRESSION, cindex, 0);
148 for coefi := cinfo^.Ss to cinfo^.Se do
149 begin
150 if (coef_bit_ptr^[coefi] < 0) then
151 expected := 0
152 else
153 expected := coef_bit_ptr^[coefi];
154 if (cinfo^.Ah <> expected) then
155 WARNMS2(j_common_ptr(cinfo), JWRN_BOGUS_PROGRESSION, cindex, coefi);
156 coef_bit_ptr^[coefi] := cinfo^.Al;
157 end;
158 end;
160 { Select MCU decoding routine }
161 if (cinfo^.Ah = 0) then
162 begin
163 if (is_DC_band) then
164 entropy^.pub.decode_mcu := decode_mcu_DC_first
165 else
166 entropy^.pub.decode_mcu := decode_mcu_AC_first;
167 end
168 else
169 begin
170 if (is_DC_band) then
171 entropy^.pub.decode_mcu := decode_mcu_DC_refine
172 else
173 entropy^.pub.decode_mcu := decode_mcu_AC_refine;
174 end;
176 for ci := 0 to pred(cinfo^.comps_in_scan) do
177 begin
178 compptr := cinfo^.cur_comp_info[ci];
179 { Make sure requested tables are present, and compute derived tables.
180 We may build same derived table more than once, but it's not expensive. }
182 if (is_DC_band) then
183 begin
184 if (cinfo^.Ah = 0) then
185 begin { DC refinement needs no table }
186 tbl := compptr^.dc_tbl_no;
187 jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
188 entropy^.derived_tbls[tbl]);
189 end;
190 end
191 else
192 begin
193 tbl := compptr^.ac_tbl_no;
194 jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
195 entropy^.derived_tbls[tbl]);
196 { remember the single active table }
197 entropy^.ac_derived_tbl := entropy^.derived_tbls[tbl];
198 end;
199 { Initialize DC predictions to 0 }
200 entropy^.saved.last_dc_val[ci] := 0;
201 end;
203 { Initialize bitread state variables }
204 entropy^.bitstate.bits_left := 0;
205 entropy^.bitstate.get_buffer := 0; { unnecessary, but keeps Purify quiet }
206 entropy^.pub.insufficient_data := FALSE;
208 { Initialize private state variables }
209 entropy^.saved.EOBRUN := 0;
211 { Initialize restart counter }
212 entropy^.restarts_to_go := cinfo^.restart_interval;
213 end;
216 { Figure F.12: extend sign bit.
217 On some machines, a shift and add will be faster than a table lookup. }
219 {$ifdef AVOID_TABLES}
221 #define HUFF_EXTEND(x,s)
222 ((x) < (1shl((s)-1)) ? (x) + (((-1)shl(s)) + 1) : (x))
224 {$else}
226 { #define HUFF_EXTEND(x,s)
227 if (x) < extend_test[s] then
228 (x) + extend_offset[s]
229 else
230 (x)}
232 const
233 extend_test : Array[0..16-1] of int = { entry n is 2**(n-1) }
234 ($0000, $0001, $0002, $0004, $0008, $0010, $0020, $0040,
235 $0080, $0100, $0200, $0400, $0800, $1000, $2000, $4000);
237 const
238 extend_offset : array[0..16-1] of int = { entry n is (-1 shl n) + 1 }
239 ( 0, ((-1) shl 1) + 1, ((-1) shl 2) + 1, ((-1) shl 3) + 1, ((-1) shl 4) + 1,
240 ((-1) shl 5) + 1, ((-1) shl 6) + 1, ((-1) shl 7) + 1, ((-1) shl 8) + 1,
241 ((-1) shl 9) + 1, ((-1) shl 10) + 1, ((-1) shl 11) + 1, ((-1) shl 12) + 1,
242 ((-1) shl 13) + 1, ((-1) shl 14) + 1, ((-1) shl 15) + 1 );
244 {$endif} { AVOID_TABLES }
247 { Check for a restart marker & resynchronize decoder.
248 return:=s FALSE if must suspend. }
250 {LOCAL}
251 function process_restart (cinfo : j_decompress_ptr) : boolean;
252 var
253 entropy : phuff_entropy_ptr;
254 ci : int;
255 begin
256 entropy := phuff_entropy_ptr (cinfo^.entropy);
258 { Throw away any unused bits remaining in bit buffer; }
259 { include any full bytes in next_marker's count of discarded bytes }
260 Inc(cinfo^.marker^.discarded_bytes, entropy^.bitstate.bits_left div 8);
261 entropy^.bitstate.bits_left := 0;
263 { Advance past the RSTn marker }
264 if (not cinfo^.marker^.read_restart_marker (cinfo)) then
265 begin
266 process_restart := FALSE;
267 exit;
268 end;
270 { Re-initialize DC predictions to 0 }
271 for ci := 0 to pred(cinfo^.comps_in_scan) do
272 entropy^.saved.last_dc_val[ci] := 0;
273 { Re-init EOB run count, too }
274 entropy^.saved.EOBRUN := 0;
276 { Reset restart counter }
277 entropy^.restarts_to_go := cinfo^.restart_interval;
279 { Reset out-of-data flag, unless read_restart_marker left us smack up
280 against a marker. In that case we will end up treating the next data
281 segment as empty, and we can avoid producing bogus output pixels by
282 leaving the flag set. }
283 if (cinfo^.unread_marker = 0) then
284 entropy^.pub.insufficient_data := FALSE;
286 process_restart := TRUE;
287 end;
290 { Huffman MCU decoding.
291 Each of these routines decodes and returns one MCU's worth of
292 Huffman-compressed coefficients.
293 The coefficients are reordered from zigzag order into natural array order,
294 but are not dequantized.
296 The i'th block of the MCU is stored into the block pointed to by
297 MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
299 We return FALSE if data source requested suspension. In that case no
300 changes have been made to permanent state. (Exception: some output
301 coefficients may already have been assigned. This is harmless for
302 spectral selection, since we'll just re-assign them on the next call.
303 Successive approximation AC refinement has to be more careful, however.) }
306 { MCU decoding for DC initial scan (either spectral selection,
307 or first pass of successive approximation). }
309 {METHODDEF}
310 function decode_mcu_DC_first (cinfo : j_decompress_ptr;
311 var MCU_data : array of JBLOCKROW) : boolean;
312 label
313 label1;
314 var
315 entropy : phuff_entropy_ptr;
316 Al : int;
317 {register} s, r : int;
318 blkn, ci : int;
319 block : JBLOCK_PTR;
320 {BITREAD_STATE_VARS;}
321 get_buffer : bit_buf_type ; {register}
322 bits_left : int; {register}
323 br_state : bitread_working_state;
325 state : savable_state;
326 tbl : d_derived_tbl_ptr;
327 compptr : jpeg_component_info_ptr;
328 var
329 nb, look : int; {register}
330 begin
331 entropy := phuff_entropy_ptr (cinfo^.entropy);
332 Al := cinfo^.Al;
334 { Process restart marker if needed; may have to suspend }
335 if (cinfo^.restart_interval <> 0) then
336 begin
337 if (entropy^.restarts_to_go = 0) then
338 if (not process_restart(cinfo)) then
339 begin
340 decode_mcu_DC_first := FALSE;
341 exit;
342 end;
343 end;
345 { If we've run out of data, just leave the MCU set to zeroes.
346 This way, we return uniform gray for the remainder of the segment. }
348 if not entropy^.pub.insufficient_data then
349 begin
351 { Load up working state }
352 {BITREAD_LOAD_STATE(cinfo,entropy^.bitstate);}
353 br_state.cinfo := cinfo;
354 br_state.next_input_byte := cinfo^.src^.next_input_byte;
355 br_state.bytes_in_buffer := cinfo^.src^.bytes_in_buffer;
356 get_buffer := entropy^.bitstate.get_buffer;
357 bits_left := entropy^.bitstate.bits_left;
359 {ASSIGN_STATE(state, entropy^.saved);}
360 state := entropy^.saved;
362 { Outer loop handles each block in the MCU }
364 for blkn := 0 to pred(cinfo^.blocks_in_MCU) do
365 begin
366 block := JBLOCK_PTR(MCU_data[blkn]);
367 ci := cinfo^.MCU_membership[blkn];
368 compptr := cinfo^.cur_comp_info[ci];
369 tbl := entropy^.derived_tbls[compptr^.dc_tbl_no];
371 { Decode a single block's worth of coefficients }
373 { Section F.2.2.1: decode the DC coefficient difference }
374 {HUFF_DECODE(s, br_state, tbl, return FALSE, label1);}
375 if (bits_left < HUFF_LOOKAHEAD) then
376 begin
377 if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) then
378 begin
379 decode_mcu_DC_first := FALSE;
380 exit;
381 end;
382 get_buffer := br_state.get_buffer;
383 bits_left := br_state.bits_left;
384 if (bits_left < HUFF_LOOKAHEAD) then
385 begin
386 nb := 1;
387 goto label1;
388 end;
389 end;
390 {look := PEEK_BITS(HUFF_LOOKAHEAD);}
391 look := int(get_buffer shr (bits_left - HUFF_LOOKAHEAD)) and
392 pred(1 shl HUFF_LOOKAHEAD);
394 nb := tbl^.look_nbits[look];
395 if (nb <> 0) then
396 begin
397 {DROP_BITS(nb);}
398 Dec(bits_left, nb);
400 s := tbl^.look_sym[look];
401 end
402 else
403 begin
404 nb := HUFF_LOOKAHEAD+1;
405 label1:
406 s := jpeg_huff_decode(br_state,get_buffer,bits_left,tbl,nb);
407 if (s < 0) then
408 begin
409 decode_mcu_DC_first := FALSE;
410 exit;
411 end;
412 get_buffer := br_state.get_buffer;
413 bits_left := br_state.bits_left;
414 end;
416 if (s <> 0) then
417 begin
418 {CHECK_BIT_BUFFER(br_state, s, return FALSE);}
419 if (bits_left < s) then
420 begin
421 if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,s)) then
422 begin
423 decode_mcu_DC_first := FALSE;
424 exit;
425 end;
426 get_buffer := br_state.get_buffer;
427 bits_left := br_state.bits_left;
428 end;
430 {r := GET_BITS(s);}
431 Dec(bits_left, s);
432 r := (int(get_buffer shr bits_left)) and ( pred(1 shl s) );
434 {s := HUFF_EXTEND(r, s);}
435 if (r < extend_test[s]) then
436 s := r + extend_offset[s]
437 else
438 s := r;
439 end;
441 { Convert DC difference to actual value, update last_dc_val }
442 Inc(s, state.last_dc_val[ci]);
443 state.last_dc_val[ci] := s;
444 { Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) }
445 block^[0] := JCOEF (s shl Al);
446 end;
448 { Completed MCU, so update state }
449 {BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
450 cinfo^.src^.next_input_byte := br_state.next_input_byte;
451 cinfo^.src^.bytes_in_buffer := br_state.bytes_in_buffer;
452 entropy^.bitstate.get_buffer := get_buffer;
453 entropy^.bitstate.bits_left := bits_left;
455 {ASSIGN_STATE(entropy^.saved, state);}
456 entropy^.saved := state;
457 end;
459 { Account for restart interval (no-op if not using restarts) }
460 Dec(entropy^.restarts_to_go);
462 decode_mcu_DC_first := TRUE;
463 end;
466 { MCU decoding for AC initial scan (either spectral selection,
467 or first pass of successive approximation). }
469 {METHODDEF}
470 function decode_mcu_AC_first (cinfo : j_decompress_ptr;
471 var MCU_data : array of JBLOCKROW) : boolean;
472 label
473 label2;
474 var
475 entropy : phuff_entropy_ptr;
476 Se : int;
477 Al : int;
478 {register} s, k, r : int;
479 EOBRUN : uInt;
480 block : JBLOCK_PTR;
481 {BITREAD_STATE_VARS;}
482 get_buffer : bit_buf_type ; {register}
483 bits_left : int; {register}
484 br_state : bitread_working_state;
486 tbl : d_derived_tbl_ptr;
487 var
488 nb, look : int; {register}
489 begin
490 entropy := phuff_entropy_ptr (cinfo^.entropy);
491 Se := cinfo^.Se;
492 Al := cinfo^.Al;
494 { Process restart marker if needed; may have to suspend }
495 if (cinfo^.restart_interval <> 0) then
496 begin
497 if (entropy^.restarts_to_go = 0) then
498 if (not process_restart(cinfo)) then
499 begin
500 decode_mcu_AC_first := FALSE;
501 exit;
502 end;
503 end;
505 { If we've run out of data, just leave the MCU set to zeroes.
506 This way, we return uniform gray for the remainder of the segment. }
507 if not entropy^.pub.insufficient_data then
508 begin
510 { Load up working state.
511 We can avoid loading/saving bitread state if in an EOB run. }
513 EOBRUN := entropy^.saved.EOBRUN; { only part of saved state we care about }
515 { There is always only one block per MCU }
517 if (EOBRUN > 0) then { if it's a band of zeroes... }
518 Dec(EOBRUN) { ...process it now (we do nothing) }
519 else
520 begin
521 {BITREAD_LOAD_STATE(cinfo,entropy^.bitstate);}
522 br_state.cinfo := cinfo;
523 br_state.next_input_byte := cinfo^.src^.next_input_byte;
524 br_state.bytes_in_buffer := cinfo^.src^.bytes_in_buffer;
525 get_buffer := entropy^.bitstate.get_buffer;
526 bits_left := entropy^.bitstate.bits_left;
528 block := JBLOCK_PTR(MCU_data[0]);
529 tbl := entropy^.ac_derived_tbl;
531 k := cinfo^.Ss;
532 while (k <= Se) do
533 begin
534 {HUFF_DECODE(s, br_state, tbl, return FALSE, label2);}
535 if (bits_left < HUFF_LOOKAHEAD) then
536 begin
537 if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) then
538 begin
539 decode_mcu_AC_first := FALSE;
540 exit;
541 end;
542 get_buffer := br_state.get_buffer;
543 bits_left := br_state.bits_left;
544 if (bits_left < HUFF_LOOKAHEAD) then
545 begin
546 nb := 1;
547 goto label2;
548 end;
549 end;
550 {look := PEEK_BITS(HUFF_LOOKAHEAD);}
551 look := int(get_buffer shr (bits_left - HUFF_LOOKAHEAD)) and
552 pred(1 shl HUFF_LOOKAHEAD);
554 nb := tbl^.look_nbits[look];
555 if (nb <> 0) then
556 begin
557 {DROP_BITS(nb);}
558 Dec(bits_left, nb);
560 s := tbl^.look_sym[look];
561 end
562 else
563 begin
564 nb := HUFF_LOOKAHEAD+1;
565 label2:
566 s := jpeg_huff_decode(br_state,get_buffer,bits_left,tbl,nb);
567 if (s < 0) then
568 begin
569 decode_mcu_AC_first := FALSE;
570 exit;
571 end;
572 get_buffer := br_state.get_buffer;
573 bits_left := br_state.bits_left;
574 end;
576 r := s shr 4;
577 s := s and 15;
578 if (s <> 0) then
579 begin
580 Inc(k, r);
581 {CHECK_BIT_BUFFER(br_state, s, return FALSE);}
582 if (bits_left < s) then
583 begin
584 if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,s)) then
585 begin
586 decode_mcu_AC_first := FALSE;
587 exit;
588 end;
589 get_buffer := br_state.get_buffer;
590 bits_left := br_state.bits_left;
591 end;
593 {r := GET_BITS(s);}
594 Dec(bits_left, s);
595 r := (int(get_buffer shr bits_left)) and ( pred(1 shl s) );
597 {s := HUFF_EXTEND(r, s);}
598 if (r < extend_test[s]) then
599 s := r + extend_offset[s]
600 else
601 s := r;
603 { Scale and output coefficient in natural (dezigzagged) order }
604 block^[jpeg_natural_order[k]] := JCOEF (s shl Al);
605 end
606 else
607 begin
608 if (r = 15) then
609 begin { ZRL }
610 Inc(k, 15); { skip 15 zeroes in band }
611 end
612 else
613 begin { EOBr, run length is 2^r + appended bits }
614 EOBRUN := 1 shl r;
615 if (r <> 0) then
616 begin { EOBr, r > 0 }
617 {CHECK_BIT_BUFFER(br_state, r, return FALSE);}
618 if (bits_left < r) then
619 begin
620 if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,r)) then
621 begin
622 decode_mcu_AC_first := FALSE;
623 exit;
624 end;
625 get_buffer := br_state.get_buffer;
626 bits_left := br_state.bits_left;
627 end;
629 {r := GET_BITS(r);}
630 Dec(bits_left, r);
631 r := (int(get_buffer shr bits_left)) and ( pred(1 shl r) );
633 Inc(EOBRUN, r);
634 end;
635 Dec(EOBRUN); { this band is processed at this moment }
636 break; { force end-of-band }
637 end;
638 end;
639 Inc(k);
640 end;
642 {BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
643 cinfo^.src^.next_input_byte := br_state.next_input_byte;
644 cinfo^.src^.bytes_in_buffer := br_state.bytes_in_buffer;
645 entropy^.bitstate.get_buffer := get_buffer;
646 entropy^.bitstate.bits_left := bits_left;
647 end;
649 { Completed MCU, so update state }
650 entropy^.saved.EOBRUN := EOBRUN; { only part of saved state we care about }
651 end;
653 { Account for restart interval (no-op if not using restarts) }
654 Dec(entropy^.restarts_to_go);
656 decode_mcu_AC_first := TRUE;
657 end;
660 { MCU decoding for DC successive approximation refinement scan.
661 Note: we assume such scans can be multi-component, although the spec
662 is not very clear on the point. }
664 {METHODDEF}
665 function decode_mcu_DC_refine (cinfo : j_decompress_ptr;
666 var MCU_data : array of JBLOCKROW) : boolean;
668 var
669 entropy : phuff_entropy_ptr;
670 p1 : int; { 1 in the bit position being coded }
671 blkn : int;
672 block : JBLOCK_PTR;
673 {BITREAD_STATE_VARS;}
674 get_buffer : bit_buf_type ; {register}
675 bits_left : int; {register}
676 br_state : bitread_working_state;
677 begin
678 entropy := phuff_entropy_ptr (cinfo^.entropy);
679 p1 := 1 shl cinfo^.Al;
681 { Process restart marker if needed; may have to suspend }
682 if (cinfo^.restart_interval <> 0) then
683 begin
684 if (entropy^.restarts_to_go = 0) then
685 if (not process_restart(cinfo)) then
686 begin
687 decode_mcu_DC_refine := FALSE;
688 exit;
689 end;
690 end;
692 { Not worth the cycles to check insufficient_data here,
693 since we will not change the data anyway if we read zeroes. }
695 { Load up working state }
696 {BITREAD_LOAD_STATE(cinfo,entropy^.bitstate);}
697 br_state.cinfo := cinfo;
698 br_state.next_input_byte := cinfo^.src^.next_input_byte;
699 br_state.bytes_in_buffer := cinfo^.src^.bytes_in_buffer;
700 get_buffer := entropy^.bitstate.get_buffer;
701 bits_left := entropy^.bitstate.bits_left;
703 { Outer loop handles each block in the MCU }
705 for blkn := 0 to pred(cinfo^.blocks_in_MCU) do
706 begin
707 block := JBLOCK_PTR(MCU_data[blkn]);
709 { Encoded data is simply the next bit of the two's-complement DC value }
710 {CHECK_BIT_BUFFER(br_state, 1, return FALSE);}
711 if (bits_left < 1) then
712 begin
713 if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
714 begin
715 decode_mcu_DC_refine := FALSE;
716 exit;
717 end;
718 get_buffer := br_state.get_buffer;
719 bits_left := br_state.bits_left;
720 end;
722 {if (GET_BITS(1)) then}
723 Dec(bits_left);
724 if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) ) <> 0 then
725 block^[0] := block^[0] or p1;
726 { Note: since we use OR, repeating the assignment later is safe }
727 end;
729 { Completed MCU, so update state }
730 {BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
731 cinfo^.src^.next_input_byte := br_state.next_input_byte;
732 cinfo^.src^.bytes_in_buffer := br_state.bytes_in_buffer;
733 entropy^.bitstate.get_buffer := get_buffer;
734 entropy^.bitstate.bits_left := bits_left;
736 { Account for restart interval (no-op if not using restarts) }
737 Dec(entropy^.restarts_to_go);
739 decode_mcu_DC_refine := TRUE;
740 end;
743 { MCU decoding for AC successive approximation refinement scan. }
745 {METHODDEF}
746 function decode_mcu_AC_refine (cinfo : j_decompress_ptr;
747 var MCU_data : array of JBLOCKROW) : boolean;
748 label
749 undoit, label3;
750 var
751 entropy : phuff_entropy_ptr;
752 Se : int;
753 p1 : int; { 1 in the bit position being coded }
754 m1 : int; { -1 in the bit position being coded }
755 {register} s, k, r : int;
756 EOBRUN : uInt;
757 block : JBLOCK_PTR;
758 thiscoef : JCOEF_PTR;
759 {BITREAD_STATE_VARS;}
760 get_buffer : bit_buf_type ; {register}
761 bits_left : int; {register}
762 br_state : bitread_working_state;
764 tbl : d_derived_tbl_ptr;
765 num_newnz : int;
766 newnz_pos : array[0..DCTSIZE2-1] of int;
767 var
768 pos : int;
769 var
770 nb, look : int; {register}
771 begin
772 num_newnz := 0;
773 block := nil;
775 entropy := phuff_entropy_ptr (cinfo^.entropy);
776 Se := cinfo^.Se;
777 p1 := 1 shl cinfo^.Al; { 1 in the bit position being coded }
778 m1 := (-1) shl cinfo^.Al; { -1 in the bit position being coded }
780 { Process restart marker if needed; may have to suspend }
781 if (cinfo^.restart_interval <> 0) then
782 begin
783 if (entropy^.restarts_to_go = 0) then
784 if (not process_restart(cinfo)) then
785 begin
786 decode_mcu_AC_refine := FALSE;
787 exit;
788 end;
789 end;
791 { If we've run out of data, don't modify the MCU. }
792 if not entropy^.pub.insufficient_data then
793 begin
795 { Load up working state }
796 {BITREAD_LOAD_STATE(cinfo,entropy^.bitstate);}
797 br_state.cinfo := cinfo;
798 br_state.next_input_byte := cinfo^.src^.next_input_byte;
799 br_state.bytes_in_buffer := cinfo^.src^.bytes_in_buffer;
800 get_buffer := entropy^.bitstate.get_buffer;
801 bits_left := entropy^.bitstate.bits_left;
803 EOBRUN := entropy^.saved.EOBRUN; { only part of saved state we care about }
805 { There is always only one block per MCU }
806 block := JBLOCK_PTR(MCU_data[0]);
807 tbl := entropy^.ac_derived_tbl;
809 { If we are forced to suspend, we must undo the assignments to any newly
810 nonzero coefficients in the block, because otherwise we'd get confused
811 next time about which coefficients were already nonzero.
812 But we need not undo addition of bits to already-nonzero coefficients;
813 instead, we can test the current bit position to see if we already did it.}
815 num_newnz := 0;
817 { initialize coefficient loop counter to start of band }
818 k := cinfo^.Ss;
820 if (EOBRUN = 0) then
821 begin
822 while (k <= Se) do
823 begin
824 {HUFF_DECODE(s, br_state, tbl, goto undoit, label3);}
825 if (bits_left < HUFF_LOOKAHEAD) then
826 begin
827 if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) then
828 goto undoit;
829 get_buffer := br_state.get_buffer;
830 bits_left := br_state.bits_left;
831 if (bits_left < HUFF_LOOKAHEAD) then
832 begin
833 nb := 1;
834 goto label3;
835 end;
836 end;
837 {look := PEEK_BITS(HUFF_LOOKAHEAD);}
838 look := int(get_buffer shr (bits_left - HUFF_LOOKAHEAD)) and
839 pred(1 shl HUFF_LOOKAHEAD);
841 nb := tbl^.look_nbits[look];
842 if (nb <> 0) then
843 begin
844 {DROP_BITS(nb);}
845 Dec(bits_left, nb);
847 s := tbl^.look_sym[look];
848 end
849 else
850 begin
851 nb := HUFF_LOOKAHEAD+1;
852 label3:
853 s := jpeg_huff_decode(br_state,get_buffer,bits_left,tbl,nb);
854 if (s < 0) then
855 goto undoit;
856 get_buffer := br_state.get_buffer;
857 bits_left := br_state.bits_left;
858 end;
860 r := s shr 4;
861 s := s and 15;
862 if (s <> 0) then
863 begin
864 if (s <> 1) then { size of new coef should always be 1 }
865 WARNMS(j_common_ptr(cinfo), JWRN_HUFF_BAD_CODE);
866 {CHECK_BIT_BUFFER(br_state, 1, goto undoit);}
867 if (bits_left < 1) then
868 begin
869 if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
870 goto undoit;
871 get_buffer := br_state.get_buffer;
872 bits_left := br_state.bits_left;
873 end;
875 {if (GET_BITS(1)) then}
876 Dec(bits_left);
877 if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) )<>0 then
878 s := p1 { newly nonzero coef is positive }
879 else
880 s := m1; { newly nonzero coef is negative }
881 end
882 else
883 begin
884 if (r <> 15) then
885 begin
886 EOBRUN := 1 shl r; { EOBr, run length is 2^r + appended bits }
887 if (r <> 0) then
888 begin
889 {CHECK_BIT_BUFFER(br_state, r, goto undoit);}
890 if (bits_left < r) then
891 begin
892 if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,r)) then
893 goto undoit;
894 get_buffer := br_state.get_buffer;
895 bits_left := br_state.bits_left;
896 end;
898 {r := GET_BITS(r);}
899 Dec(bits_left, r);
900 r := (int(get_buffer shr bits_left)) and ( pred(1 shl r) );
902 Inc(EOBRUN, r);
903 end;
904 break; { rest of block is handled by EOB logic }
905 end;
906 { note s := 0 for processing ZRL }
907 end;
908 { Advance over already-nonzero coefs and r still-zero coefs,
909 appending correction bits to the nonzeroes. A correction bit is 1
910 if the absolute value of the coefficient must be increased. }
912 repeat
913 thiscoef :=@(block^[jpeg_natural_order[k]]);
914 if (thiscoef^ <> 0) then
915 begin
916 {CHECK_BIT_BUFFER(br_state, 1, goto undoit);}
917 if (bits_left < 1) then
918 begin
919 if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
920 goto undoit;
921 get_buffer := br_state.get_buffer;
922 bits_left := br_state.bits_left;
923 end;
925 {if (GET_BITS(1)) then}
926 Dec(bits_left);
927 if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) )<>0 then
928 begin
929 if ((thiscoef^ and p1) = 0) then
930 begin { do nothing if already set it }
931 if (thiscoef^ >= 0) then
932 Inc(thiscoef^, p1)
933 else
934 Inc(thiscoef^, m1);
935 end;
936 end;
937 end
938 else
939 begin
940 Dec(r);
941 if (r < 0) then
942 break; { reached target zero coefficient }
943 end;
944 Inc(k);
945 until (k > Se);
946 if (s <> 0) then
947 begin
948 pos := jpeg_natural_order[k];
949 { Output newly nonzero coefficient }
950 block^[pos] := JCOEF (s);
951 { Remember its position in case we have to suspend }
952 newnz_pos[num_newnz] := pos;
953 Inc(num_newnz);
954 end;
955 Inc(k);
956 end;
957 end;
959 if (EOBRUN > 0) then
960 begin
961 { Scan any remaining coefficient positions after the end-of-band
962 (the last newly nonzero coefficient, if any). Append a correction
963 bit to each already-nonzero coefficient. A correction bit is 1
964 if the absolute value of the coefficient must be increased. }
966 while (k <= Se) do
967 begin
968 thiscoef := @(block^[jpeg_natural_order[k]]);
969 if (thiscoef^ <> 0) then
970 begin
971 {CHECK_BIT_BUFFER(br_state, 1, goto undoit);}
972 if (bits_left < 1) then
973 begin
974 if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
975 goto undoit;
976 get_buffer := br_state.get_buffer;
977 bits_left := br_state.bits_left;
978 end;
980 {if (GET_BITS(1)) then}
981 Dec(bits_left);
982 if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) )<>0 then
983 begin
984 if ((thiscoef^ and p1) = 0) then
985 begin { do nothing if already changed it }
986 if (thiscoef^ >= 0) then
987 Inc(thiscoef^, p1)
988 else
989 Inc(thiscoef^, m1);
990 end;
991 end;
992 end;
993 Inc(k);
994 end;
995 { Count one block completed in EOB run }
996 Dec(EOBRUN);
997 end;
999 { Completed MCU, so update state }
1000 {BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
1001 cinfo^.src^.next_input_byte := br_state.next_input_byte;
1002 cinfo^.src^.bytes_in_buffer := br_state.bytes_in_buffer;
1003 entropy^.bitstate.get_buffer := get_buffer;
1004 entropy^.bitstate.bits_left := bits_left;
1006 entropy^.saved.EOBRUN := EOBRUN; { only part of saved state we care about }
1007 end;
1009 { Account for restart interval (no-op if not using restarts) }
1010 Dec(entropy^.restarts_to_go);
1012 decode_mcu_AC_refine := TRUE;
1013 exit;
1015 undoit:
1016 { Re-zero any output coefficients that we made newly nonzero }
1017 while (num_newnz > 0) do
1018 begin
1019 Dec(num_newnz);
1020 block^[newnz_pos[num_newnz]] := 0;
1021 end;
1023 decode_mcu_AC_refine := FALSE;
1024 end;
1027 { Module initialization routine for progressive Huffman entropy decoding. }
1029 {GLOBAL}
1030 procedure jinit_phuff_decoder (cinfo : j_decompress_ptr);
1031 var
1032 entropy : phuff_entropy_ptr;
1033 coef_bit_ptr : int_ptr;
1034 ci, i : int;
1035 begin
1036 entropy := phuff_entropy_ptr(
1037 cinfo^.mem^.alloc_small (j_common_ptr (cinfo), JPOOL_IMAGE,
1038 SIZEOF(phuff_entropy_decoder)) );
1039 cinfo^.entropy := jpeg_entropy_decoder_ptr (entropy);
1040 entropy^.pub.start_pass := start_pass_phuff_decoder;
1042 { Mark derived tables unallocated }
1043 for i := 0 to pred(NUM_HUFF_TBLS) do
1044 begin
1045 entropy^.derived_tbls[i] := NIL;
1046 end;
1048 { Create progression status table }
1049 cinfo^.coef_bits := coef_bits_ptrrow (
1050 cinfo^.mem^.alloc_small ( j_common_ptr (cinfo), JPOOL_IMAGE,
1051 cinfo^.num_components*DCTSIZE2*SIZEOF(int)) );
1052 coef_bit_ptr := @cinfo^.coef_bits^[0][0];
1053 for ci := 0 to pred(cinfo^.num_components) do
1054 for i := 0 to pred(DCTSIZE2) do
1055 begin
1056 coef_bit_ptr^ := -1;
1057 Inc(coef_bit_ptr);
1058 end;
1059 end;
1061 end.