DEADSOFTWARE

hopefully no more windows
[d2df-editor.git] / src / lib / vampimg / JpegLib / imjcphuff.pas
1 unit imjcphuff;
3 { This file contains Huffman entropy encoding routines for progressive JPEG.
5 We do not support output suspension in this module, since the library
6 currently does not allow multiple-scan files to be written with output
7 suspension. }
9 { Original: jcphuff.c; Copyright (C) 1995-1997, Thomas G. Lane. }
11 interface
13 {$I imjconfig.inc}
15 uses
16 imjmorecfg,
17 imjinclude,
18 imjpeglib,
19 imjdeferr,
20 imjerror,
21 imjutils,
22 imjcomapi,
23 imjchuff; { Declarations shared with jchuff.c }
25 { Module initialization routine for progressive Huffman entropy encoding. }
27 {GLOBAL}
28 procedure jinit_phuff_encoder (cinfo : j_compress_ptr);
30 implementation
32 { Expanded entropy encoder object for progressive Huffman encoding. }
33 type
34 phuff_entropy_ptr = ^phuff_entropy_encoder;
35 phuff_entropy_encoder = record
36 pub : jpeg_entropy_encoder; { public fields }
38 { Mode flag: TRUE for optimization, FALSE for actual data output }
39 gather_statistics : boolean;
41 { Bit-level coding status.
42 next_output_byte/free_in_buffer are local copies of cinfo^.dest fields.}
44 next_output_byte : JOCTETptr; { => next byte to write in buffer }
45 free_in_buffer : size_t; { # of byte spaces remaining in buffer }
46 put_buffer : INT32; { current bit-accumulation buffer }
47 put_bits : int; { # of bits now in it }
48 cinfo : j_compress_ptr; { link to cinfo (needed for dump_buffer) }
50 { Coding status for DC components }
51 last_dc_val : array[0..MAX_COMPS_IN_SCAN-1] of int;
52 { last DC coef for each component }
54 { Coding status for AC components }
55 ac_tbl_no : int; { the table number of the single component }
56 EOBRUN : uInt; { run length of EOBs }
57 BE : uInt; { # of buffered correction bits before MCU }
58 bit_buffer : JBytePtr; { buffer for correction bits (1 per char) }
59 { packing correction bits tightly would save some space but cost time... }
61 restarts_to_go : uInt; { MCUs left in this restart interval }
62 next_restart_num : int; { next restart number to write (0-7) }
64 { Pointers to derived tables (these workspaces have image lifespan).
65 Since any one scan codes only DC or only AC, we only need one set
66 of tables, not one for DC and one for AC. }
68 derived_tbls : array[0..NUM_HUFF_TBLS-1] of c_derived_tbl_ptr;
70 { Statistics tables for optimization; again, one set is enough }
71 count_ptrs : array[0..NUM_HUFF_TBLS-1] of TLongTablePtr;
72 end;
75 { MAX_CORR_BITS is the number of bits the AC refinement correction-bit
76 buffer can hold. Larger sizes may slightly improve compression, but
77 1000 is already well into the realm of overkill.
78 The minimum safe size is 64 bits. }
80 const
81 MAX_CORR_BITS = 1000; { Max # of correction bits I can buffer }
84 { Forward declarations }
85 {METHODDEF}
86 function encode_mcu_DC_first (cinfo : j_compress_ptr;
87 const MCU_data: array of JBLOCKROW) : boolean;
88 forward;
89 {METHODDEF}
90 function encode_mcu_AC_first (cinfo : j_compress_ptr;
91 const MCU_data: array of JBLOCKROW) : boolean;
92 forward;
93 {METHODDEF}
94 function encode_mcu_DC_refine (cinfo : j_compress_ptr;
95 const MCU_data: array of JBLOCKROW) : boolean;
96 forward;
97 {METHODDEF}
98 function encode_mcu_AC_refine (cinfo : j_compress_ptr;
99 const MCU_data: array of JBLOCKROW) : boolean;
100 forward;
102 {METHODDEF}
103 procedure finish_pass_phuff (cinfo : j_compress_ptr); forward;
105 {METHODDEF}
106 procedure finish_pass_gather_phuff (cinfo : j_compress_ptr); forward;
109 { Initialize for a Huffman-compressed scan using progressive JPEG. }
111 {METHODDEF}
112 procedure start_pass_phuff (cinfo : j_compress_ptr;
113 gather_statistics : boolean);
114 var
115 entropy : phuff_entropy_ptr;
116 is_DC_band : boolean;
117 ci, tbl : int;
118 compptr : jpeg_component_info_ptr;
119 begin
120 tbl := 0;
121 entropy := phuff_entropy_ptr (cinfo^.entropy);
123 entropy^.cinfo := cinfo;
124 entropy^.gather_statistics := gather_statistics;
126 is_DC_band := (cinfo^.Ss = 0);
128 { We assume jcmaster.c already validated the scan parameters. }
130 { Select execution routines }
131 if (cinfo^.Ah = 0) then
132 begin
133 if (is_DC_band) then
134 entropy^.pub.encode_mcu := encode_mcu_DC_first
135 else
136 entropy^.pub.encode_mcu := encode_mcu_AC_first;
137 end
138 else
139 begin
140 if (is_DC_band) then
141 entropy^.pub.encode_mcu := encode_mcu_DC_refine
142 else
143 begin
144 entropy^.pub.encode_mcu := encode_mcu_AC_refine;
145 { AC refinement needs a correction bit buffer }
146 if (entropy^.bit_buffer = NIL) then
147 entropy^.bit_buffer := JBytePtr(
148 cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
149 MAX_CORR_BITS * SIZEOF(byte)) );
150 end;
151 end;
152 if (gather_statistics) then
153 entropy^.pub.finish_pass := finish_pass_gather_phuff
154 else
155 entropy^.pub.finish_pass := finish_pass_phuff;
157 { Only DC coefficients may be interleaved, so cinfo^.comps_in_scan = 1
158 for AC coefficients. }
160 for ci := 0 to pred(cinfo^.comps_in_scan) do
161 begin
162 compptr := cinfo^.cur_comp_info[ci];
163 { Initialize DC predictions to 0 }
164 entropy^.last_dc_val[ci] := 0;
165 { Get table index }
166 if (is_DC_band) then
167 begin
168 if (cinfo^.Ah <> 0) then { DC refinement needs no table }
169 continue;
170 tbl := compptr^.dc_tbl_no;
171 end
172 else
173 begin
174 tbl := compptr^.ac_tbl_no;
175 entropy^.ac_tbl_no := tbl;
176 end;
177 if (gather_statistics) then
178 begin
179 { Check for invalid table index }
180 { (make_c_derived_tbl does this in the other path) }
181 if (tbl < 0) or (tbl >= NUM_HUFF_TBLS) then
182 ERREXIT1(j_common_ptr(cinfo), JERR_NO_HUFF_TABLE, tbl);
183 { Allocate and zero the statistics tables }
184 { Note that jpeg_gen_optimal_table expects 257 entries in each table! }
185 if (entropy^.count_ptrs[tbl] = NIL) then
186 entropy^.count_ptrs[tbl] := TLongTablePtr(
187 cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
188 257 * SIZEOF(long)) );
189 MEMZERO(entropy^.count_ptrs[tbl], 257 * SIZEOF(long));
190 end else
191 begin
192 { Compute derived values for Huffman table }
193 { We may do this more than once for a table, but it's not expensive }
194 jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
195 entropy^.derived_tbls[tbl]);
196 end;
197 end;
199 { Initialize AC stuff }
200 entropy^.EOBRUN := 0;
201 entropy^.BE := 0;
203 { Initialize bit buffer to empty }
204 entropy^.put_buffer := 0;
205 entropy^.put_bits := 0;
207 { Initialize restart stuff }
208 entropy^.restarts_to_go := cinfo^.restart_interval;
209 entropy^.next_restart_num := 0;
210 end;
215 {LOCAL}
216 procedure dump_buffer (entropy : phuff_entropy_ptr);
217 { Empty the output buffer; we do not support suspension in this module. }
218 var
219 dest : jpeg_destination_mgr_ptr;
220 begin
221 dest := entropy^.cinfo^.dest;
223 if (not dest^.empty_output_buffer (entropy^.cinfo)) then
224 ERREXIT(j_common_ptr(entropy^.cinfo), JERR_CANT_SUSPEND);
225 { After a successful buffer dump, must reset buffer pointers }
226 entropy^.next_output_byte := dest^.next_output_byte;
227 entropy^.free_in_buffer := dest^.free_in_buffer;
228 end;
231 { Outputting bits to the file }
233 { Only the right 24 bits of put_buffer are used; the valid bits are
234 left-justified in this part. At most 16 bits can be passed to emit_bits
235 in one call, and we never retain more than 7 bits in put_buffer
236 between calls, so 24 bits are sufficient. }
239 {LOCAL}
240 procedure emit_bits (entropy : phuff_entropy_ptr;
241 code : uInt;
242 size : int); {INLINE}
243 { Emit some bits, unless we are in gather mode }
244 var
245 {register} put_buffer : INT32;
246 {register} put_bits : int;
247 var
248 c : int;
249 begin
250 { This routine is heavily used, so it's worth coding tightly. }
251 put_buffer := INT32 (code);
252 put_bits := entropy^.put_bits;
254 { if size is 0, caller used an invalid Huffman table entry }
255 if (size = 0) then
256 ERREXIT(j_common_ptr(entropy^.cinfo), JERR_HUFF_MISSING_CODE);
258 if (entropy^.gather_statistics) then
259 exit; { do nothing if we're only getting stats }
261 put_buffer := put_buffer and ((INT32(1) shl size) - 1);
262 { mask off any extra bits in code }
264 Inc(put_bits, size); { new number of bits in buffer }
266 put_buffer := put_buffer shl (24 - put_bits); { align incoming bits }
268 put_buffer := put_buffer or entropy^.put_buffer;
269 { and merge with old buffer contents }
271 while (put_bits >= 8) do
272 begin
273 c := int ((put_buffer shr 16) and $FF);
275 {emit_byte(entropy, c);}
276 { Outputting bytes to the file.
277 NB: these must be called only when actually outputting,
278 that is, entropy^.gather_statistics = FALSE. }
279 { Emit a byte }
280 entropy^.next_output_byte^ := JOCTET(c);
281 Inc(entropy^.next_output_byte);
282 Dec(entropy^.free_in_buffer);
283 if (entropy^.free_in_buffer = 0) then
284 dump_buffer(entropy);
286 if (c = $FF) then
287 begin { need to stuff a zero byte? }
288 {emit_byte(entropy, 0);}
289 entropy^.next_output_byte^ := JOCTET(0);
290 Inc(entropy^.next_output_byte);
291 Dec(entropy^.free_in_buffer);
292 if (entropy^.free_in_buffer = 0) then
293 dump_buffer(entropy);
294 end;
295 put_buffer := put_buffer shl 8;
296 Dec(put_bits, 8);
297 end;
299 entropy^.put_buffer := put_buffer; { update variables }
300 entropy^.put_bits := put_bits;
301 end;
304 {LOCAL}
305 procedure flush_bits (entropy : phuff_entropy_ptr);
306 begin
307 emit_bits(entropy, $7F, 7); { fill any partial byte with ones }
308 entropy^.put_buffer := 0; { and reset bit-buffer to empty }
309 entropy^.put_bits := 0;
310 end;
312 { Emit (or just count) a Huffman symbol. }
315 {LOCAL}
316 procedure emit_symbol (entropy : phuff_entropy_ptr;
317 tbl_no : int;
318 symbol : int); {INLINE}
319 var
320 tbl : c_derived_tbl_ptr;
321 begin
322 if (entropy^.gather_statistics) then
323 Inc(entropy^.count_ptrs[tbl_no]^[symbol])
324 else
325 begin
326 tbl := entropy^.derived_tbls[tbl_no];
327 emit_bits(entropy, tbl^.ehufco[symbol], tbl^.ehufsi[symbol]);
328 end;
329 end;
332 { Emit bits from a correction bit buffer. }
334 {LOCAL}
335 procedure emit_buffered_bits (entropy : phuff_entropy_ptr;
336 bufstart : JBytePtr;
337 nbits : uInt);
338 var
339 bufptr : byteptr;
340 begin
341 if (entropy^.gather_statistics) then
342 exit; { no real work }
344 bufptr := byteptr(bufstart);
345 while (nbits > 0) do
346 begin
347 emit_bits(entropy, uInt(bufptr^), 1);
348 Inc(bufptr);
349 Dec(nbits);
350 end;
351 end;
354 { Emit any pending EOBRUN symbol. }
356 {LOCAL}
357 procedure emit_eobrun (entropy : phuff_entropy_ptr);
358 var
359 {register} temp, nbits : int;
360 begin
361 if (entropy^.EOBRUN > 0) then
362 begin { if there is any pending EOBRUN }
363 temp := entropy^.EOBRUN;
364 nbits := 0;
365 temp := temp shr 1;
366 while (temp <> 0) do
367 begin
368 Inc(nbits);
369 temp := temp shr 1;
370 end;
372 { safety check: shouldn't happen given limited correction-bit buffer }
373 if (nbits > 14) then
374 ERREXIT(j_common_ptr(entropy^.cinfo), JERR_HUFF_MISSING_CODE);
376 emit_symbol(entropy, entropy^.ac_tbl_no, nbits shl 4);
377 if (nbits <> 0) then
378 emit_bits(entropy, entropy^.EOBRUN, nbits);
380 entropy^.EOBRUN := 0;
382 { Emit any buffered correction bits }
383 emit_buffered_bits(entropy, entropy^.bit_buffer, entropy^.BE);
384 entropy^.BE := 0;
385 end;
386 end;
389 { Emit a restart marker & resynchronize predictions. }
391 {LOCAL}
392 procedure emit_restart (entropy : phuff_entropy_ptr;
393 restart_num : int);
394 var
395 ci : int;
396 begin
397 emit_eobrun(entropy);
399 if (not entropy^.gather_statistics) then
400 begin
401 flush_bits(entropy);
402 {emit_byte(entropy, $FF);}
403 { Outputting bytes to the file.
404 NB: these must be called only when actually outputting,
405 that is, entropy^.gather_statistics = FALSE. }
407 entropy^.next_output_byte^ := JOCTET($FF);
408 Inc(entropy^.next_output_byte);
409 Dec(entropy^.free_in_buffer);
410 if (entropy^.free_in_buffer = 0) then
411 dump_buffer(entropy);
413 {emit_byte(entropy, JPEG_RST0 + restart_num);}
414 entropy^.next_output_byte^ := JOCTET(JPEG_RST0 + restart_num);
415 Inc(entropy^.next_output_byte);
416 Dec(entropy^.free_in_buffer);
417 if (entropy^.free_in_buffer = 0) then
418 dump_buffer(entropy);
419 end;
421 if (entropy^.cinfo^.Ss = 0) then
422 begin
423 { Re-initialize DC predictions to 0 }
424 for ci := 0 to pred(entropy^.cinfo^.comps_in_scan) do
425 entropy^.last_dc_val[ci] := 0;
426 end
427 else
428 begin
429 { Re-initialize all AC-related fields to 0 }
430 entropy^.EOBRUN := 0;
431 entropy^.BE := 0;
432 end;
433 end;
436 { MCU encoding for DC initial scan (either spectral selection,
437 or first pass of successive approximation). }
439 {METHODDEF}
440 function encode_mcu_DC_first (cinfo : j_compress_ptr;
441 const MCU_data: array of JBLOCKROW) : boolean;
442 var
443 entropy : phuff_entropy_ptr;
444 {register} temp, temp2 : int;
445 {register} nbits : int;
446 blkn, ci : int;
447 Al : int;
448 block : JBLOCK_PTR;
449 compptr : jpeg_component_info_ptr;
450 ishift_temp : int;
451 begin
452 entropy := phuff_entropy_ptr (cinfo^.entropy);
453 Al := cinfo^.Al;
455 entropy^.next_output_byte := cinfo^.dest^.next_output_byte;
456 entropy^.free_in_buffer := cinfo^.dest^.free_in_buffer;
458 { Emit restart marker if needed }
459 if (cinfo^.restart_interval <> 0) then
460 if (entropy^.restarts_to_go = 0) then
461 emit_restart(entropy, entropy^.next_restart_num);
463 { Encode the MCU data blocks }
464 for blkn := 0 to pred(cinfo^.blocks_in_MCU) do
465 begin
466 block := JBLOCK_PTR(MCU_data[blkn]);
467 ci := cinfo^.MCU_membership[blkn];
468 compptr := cinfo^.cur_comp_info[ci];
470 { Compute the DC value after the required point transform by Al.
471 This is simply an arithmetic right shift. }
473 {temp2 := IRIGHT_SHIFT( int(block^[0]), Al);}
474 {IRIGHT_SHIFT_IS_UNSIGNED}
475 ishift_temp := int(block^[0]);
476 if ishift_temp < 0 then
477 temp2 := (ishift_temp shr Al) or ((not 0) shl (16-Al))
478 else
479 temp2 := ishift_temp shr Al;
482 { DC differences are figured on the point-transformed values. }
483 temp := temp2 - entropy^.last_dc_val[ci];
484 entropy^.last_dc_val[ci] := temp2;
486 { Encode the DC coefficient difference per section G.1.2.1 }
487 temp2 := temp;
488 if (temp < 0) then
489 begin
490 temp := -temp; { temp is abs value of input }
491 { For a negative input, want temp2 := bitwise complement of abs(input) }
492 { This code assumes we are on a two's complement machine }
493 Dec(temp2);
494 end;
496 { Find the number of bits needed for the magnitude of the coefficient }
497 nbits := 0;
498 while (temp <> 0) do
499 begin
500 Inc(nbits);
501 temp := temp shr 1;
502 end;
504 { Check for out-of-range coefficient values.
505 Since we're encoding a difference, the range limit is twice as much. }
507 if (nbits > MAX_COEF_BITS+1) then
508 ERREXIT(j_common_ptr(cinfo), JERR_BAD_DCT_COEF);
510 { Count/emit the Huffman-coded symbol for the number of bits }
511 emit_symbol(entropy, compptr^.dc_tbl_no, nbits);
513 { Emit that number of bits of the value, if positive, }
514 { or the complement of its magnitude, if negative. }
515 if (nbits <> 0) then { emit_bits rejects calls with size 0 }
516 emit_bits(entropy, uInt(temp2), nbits);
517 end;
519 cinfo^.dest^.next_output_byte := entropy^.next_output_byte;
520 cinfo^.dest^.free_in_buffer := entropy^.free_in_buffer;
522 { Update restart-interval state too }
523 if (cinfo^.restart_interval <> 0) then
524 begin
525 if (entropy^.restarts_to_go = 0) then
526 begin
527 entropy^.restarts_to_go := cinfo^.restart_interval;
528 Inc(entropy^.next_restart_num);
529 with entropy^ do
530 next_restart_num := next_restart_num and 7;
531 end;
532 Dec(entropy^.restarts_to_go);
533 end;
535 encode_mcu_DC_first := TRUE;
536 end;
539 { MCU encoding for AC initial scan (either spectral selection,
540 or first pass of successive approximation). }
542 {METHODDEF}
543 function encode_mcu_AC_first (cinfo : j_compress_ptr;
544 const MCU_data: array of JBLOCKROW) : boolean;
545 var
546 entropy : phuff_entropy_ptr;
547 {register} temp, temp2 : int;
548 {register} nbits : int;
549 {register} r, k : int;
550 Se : int;
551 Al : int;
552 block : JBLOCK_PTR;
553 begin
554 entropy := phuff_entropy_ptr (cinfo^.entropy);
555 Se := cinfo^.Se;
556 Al := cinfo^.Al;
558 entropy^.next_output_byte := cinfo^.dest^.next_output_byte;
559 entropy^.free_in_buffer := cinfo^.dest^.free_in_buffer;
561 { Emit restart marker if needed }
562 if (cinfo^.restart_interval <> 0) then
563 if (entropy^.restarts_to_go = 0) then
564 emit_restart(entropy, entropy^.next_restart_num);
566 { Encode the MCU data block }
567 block := JBLOCK_PTR(MCU_data[0]);
569 { Encode the AC coefficients per section G.1.2.2, fig. G.3 }
571 r := 0; { r := run length of zeros }
573 for k := cinfo^.Ss to Se do
574 begin
575 temp := (block^[jpeg_natural_order[k]]);
576 if (temp = 0) then
577 begin
578 Inc(r);
579 continue;
580 end;
581 { We must apply the point transform by Al. For AC coefficients this
582 is an integer division with rounding towards 0. To do this portably
583 in C, we shift after obtaining the absolute value; so the code is
584 interwoven with finding the abs value (temp) and output bits (temp2). }
586 if (temp < 0) then
587 begin
588 temp := -temp; { temp is abs value of input }
589 temp := temp shr Al; { apply the point transform }
590 { For a negative coef, want temp2 := bitwise complement of abs(coef) }
591 temp2 := not temp;
592 end
593 else
594 begin
595 temp := temp shr Al; { apply the point transform }
596 temp2 := temp;
597 end;
598 { Watch out for case that nonzero coef is zero after point transform }
599 if (temp = 0) then
600 begin
601 Inc(r);
602 continue;
603 end;
605 { Emit any pending EOBRUN }
606 if (entropy^.EOBRUN > 0) then
607 emit_eobrun(entropy);
608 { if run length > 15, must emit special run-length-16 codes ($F0) }
609 while (r > 15) do
610 begin
611 emit_symbol(entropy, entropy^.ac_tbl_no, $F0);
612 Dec(r, 16);
613 end;
615 { Find the number of bits needed for the magnitude of the coefficient }
616 nbits := 0; { there must be at least one 1 bit }
617 repeat
618 Inc(nbits);
619 temp := temp shr 1;
620 until (temp = 0);
622 { Check for out-of-range coefficient values }
623 if (nbits > MAX_COEF_BITS) then
624 ERREXIT(j_common_ptr(cinfo), JERR_BAD_DCT_COEF);
626 { Count/emit Huffman symbol for run length / number of bits }
627 emit_symbol(entropy, entropy^.ac_tbl_no, (r shl 4) + nbits);
629 { Emit that number of bits of the value, if positive, }
630 { or the complement of its magnitude, if negative. }
631 emit_bits(entropy, uInt(temp2), nbits);
633 r := 0; { reset zero run length }
634 end;
636 if (r > 0) then
637 begin { If there are trailing zeroes, }
638 Inc(entropy^.EOBRUN); { count an EOB }
639 if (entropy^.EOBRUN = $7FFF) then
640 emit_eobrun(entropy); { force it out to avoid overflow }
641 end;
643 cinfo^.dest^.next_output_byte := entropy^.next_output_byte;
644 cinfo^.dest^.free_in_buffer := entropy^.free_in_buffer;
646 { Update restart-interval state too }
647 if (cinfo^.restart_interval <> 0) then
648 begin
649 if (entropy^.restarts_to_go = 0) then
650 begin
651 entropy^.restarts_to_go := cinfo^.restart_interval;
652 Inc(entropy^.next_restart_num);
653 with entropy^ do
654 next_restart_num := next_restart_num and 7;
655 end;
656 Dec(entropy^.restarts_to_go);
657 end;
659 encode_mcu_AC_first := TRUE;
660 end;
663 { MCU encoding for DC successive approximation refinement scan.
664 Note: we assume such scans can be multi-component, although the spec
665 is not very clear on the point. }
667 {METHODDEF}
668 function encode_mcu_DC_refine (cinfo : j_compress_ptr;
669 const MCU_data: array of JBLOCKROW) : boolean;
670 var
671 entropy : phuff_entropy_ptr;
672 {register} temp : int;
673 blkn : int;
674 Al : int;
675 block : JBLOCK_PTR;
676 begin
677 entropy := phuff_entropy_ptr (cinfo^.entropy);
678 Al := cinfo^.Al;
680 entropy^.next_output_byte := cinfo^.dest^.next_output_byte;
681 entropy^.free_in_buffer := cinfo^.dest^.free_in_buffer;
683 { Emit restart marker if needed }
684 if (cinfo^.restart_interval <> 0) then
685 if (entropy^.restarts_to_go = 0) then
686 emit_restart(entropy, entropy^.next_restart_num);
688 { Encode the MCU data blocks }
689 for blkn := 0 to pred(cinfo^.blocks_in_MCU) do
690 begin
691 block := JBLOCK_PTR(MCU_data[blkn]);
693 { We simply emit the Al'th bit of the DC coefficient value. }
694 temp := block^[0];
695 emit_bits(entropy, uInt(temp shr Al), 1);
696 end;
698 cinfo^.dest^.next_output_byte := entropy^.next_output_byte;
699 cinfo^.dest^.free_in_buffer := entropy^.free_in_buffer;
701 { Update restart-interval state too }
702 if (cinfo^.restart_interval <> 0) then
703 begin
704 if (entropy^.restarts_to_go = 0) then
705 begin
706 entropy^.restarts_to_go := cinfo^.restart_interval;
707 Inc(entropy^.next_restart_num);
708 with entropy^ do
709 next_restart_num := next_restart_num and 7;
710 end;
711 Dec(entropy^.restarts_to_go);
712 end;
714 encode_mcu_DC_refine := TRUE;
715 end;
718 { MCU encoding for AC successive approximation refinement scan. }
720 {METHODDEF}
721 function encode_mcu_AC_refine (cinfo : j_compress_ptr;
722 const MCU_data: array of JBLOCKROW) : boolean;
724 var
725 entropy : phuff_entropy_ptr;
726 {register} temp : int;
727 {register} r, k : int;
728 EOB : int;
729 BR_buffer : JBytePtr;
730 BR : uInt;
731 Se : int;
732 Al : int;
733 block : JBLOCK_PTR;
734 absvalues : array[0..DCTSIZE2-1] of int;
735 begin
736 entropy := phuff_entropy_ptr(cinfo^.entropy);
737 Se := cinfo^.Se;
738 Al := cinfo^.Al;
740 entropy^.next_output_byte := cinfo^.dest^.next_output_byte;
741 entropy^.free_in_buffer := cinfo^.dest^.free_in_buffer;
743 { Emit restart marker if needed }
744 if (cinfo^.restart_interval <> 0) then
745 if (entropy^.restarts_to_go = 0) then
746 emit_restart(entropy, entropy^.next_restart_num);
748 { Encode the MCU data block }
749 block := JBLOCK_PTR(MCU_data[0]);
751 { It is convenient to make a pre-pass to determine the transformed
752 coefficients' absolute values and the EOB position. }
754 EOB := 0;
755 for k := cinfo^.Ss to Se do
756 begin
757 temp := block^[jpeg_natural_order[k]];
758 { We must apply the point transform by Al. For AC coefficients this
759 is an integer division with rounding towards 0. To do this portably
760 in C, we shift after obtaining the absolute value. }
762 if (temp < 0) then
763 temp := -temp; { temp is abs value of input }
764 temp := temp shr Al; { apply the point transform }
765 absvalues[k] := temp; { save abs value for main pass }
766 if (temp = 1) then
767 EOB := k; { EOB := index of last newly-nonzero coef }
768 end;
770 { Encode the AC coefficients per section G.1.2.3, fig. G.7 }
772 r := 0; { r := run length of zeros }
773 BR := 0; { BR := count of buffered bits added now }
774 BR_buffer := JBytePtr(@(entropy^.bit_buffer^[entropy^.BE]));
775 { Append bits to buffer }
777 for k := cinfo^.Ss to Se do
778 begin
779 temp := absvalues[k];
780 if (temp = 0) then
781 begin
782 Inc(r);
783 continue;
784 end;
786 { Emit any required ZRLs, but not if they can be folded into EOB }
787 while (r > 15) and (k <= EOB) do
788 begin
789 { emit any pending EOBRUN and the BE correction bits }
790 emit_eobrun(entropy);
791 { Emit ZRL }
792 emit_symbol(entropy, entropy^.ac_tbl_no, $F0);
793 Dec(r, 16);
794 { Emit buffered correction bits that must be associated with ZRL }
795 emit_buffered_bits(entropy, BR_buffer, BR);
796 BR_buffer := entropy^.bit_buffer; { BE bits are gone now }
797 BR := 0;
798 end;
800 { If the coef was previously nonzero, it only needs a correction bit.
801 NOTE: a straight translation of the spec's figure G.7 would suggest
802 that we also need to test r > 15. But if r > 15, we can only get here
803 if k > EOB, which implies that this coefficient is not 1. }
804 if (temp > 1) then
805 begin
806 { The correction bit is the next bit of the absolute value. }
807 BR_buffer^[BR] := byte (temp and 1);
808 Inc(BR);
809 continue;
810 end;
812 { Emit any pending EOBRUN and the BE correction bits }
813 emit_eobrun(entropy);
815 { Count/emit Huffman symbol for run length / number of bits }
816 emit_symbol(entropy, entropy^.ac_tbl_no, (r shl 4) + 1);
818 { Emit output bit for newly-nonzero coef }
819 if (block^[jpeg_natural_order[k]] < 0) then
820 temp := 0
821 else
822 temp := 1;
823 emit_bits(entropy, uInt(temp), 1);
825 { Emit buffered correction bits that must be associated with this code }
826 emit_buffered_bits(entropy, BR_buffer, BR);
827 BR_buffer := entropy^.bit_buffer; { BE bits are gone now }
828 BR := 0;
829 r := 0; { reset zero run length }
830 end;
832 if (r > 0) or (BR > 0) then
833 begin { If there are trailing zeroes, }
834 Inc(entropy^.EOBRUN); { count an EOB }
835 Inc(entropy^.BE, BR); { concat my correction bits to older ones }
836 { We force out the EOB if we risk either:
837 1. overflow of the EOB counter;
838 2. overflow of the correction bit buffer during the next MCU. }
840 if (entropy^.EOBRUN = $7FFF) or
841 (entropy^.BE > (MAX_CORR_BITS-DCTSIZE2+1)) then
842 emit_eobrun(entropy);
843 end;
845 cinfo^.dest^.next_output_byte := entropy^.next_output_byte;
846 cinfo^.dest^.free_in_buffer := entropy^.free_in_buffer;
848 { Update restart-interval state too }
849 if (cinfo^.restart_interval <> 0) then
850 begin
851 if (entropy^.restarts_to_go = 0) then
852 begin
853 entropy^.restarts_to_go := cinfo^.restart_interval;
854 Inc(entropy^.next_restart_num);
855 with entropy^ do
856 next_restart_num := next_restart_num and 7;
857 end;
858 Dec(entropy^.restarts_to_go);
859 end;
861 encode_mcu_AC_refine := TRUE;
862 end;
865 { Finish up at the end of a Huffman-compressed progressive scan. }
867 {METHODDEF}
868 procedure finish_pass_phuff (cinfo : j_compress_ptr);
869 var
870 entropy : phuff_entropy_ptr;
871 begin
872 entropy := phuff_entropy_ptr (cinfo^.entropy);
874 entropy^.next_output_byte := cinfo^.dest^.next_output_byte;
875 entropy^.free_in_buffer := cinfo^.dest^.free_in_buffer;
877 { Flush out any buffered data }
878 emit_eobrun(entropy);
879 flush_bits(entropy);
881 cinfo^.dest^.next_output_byte := entropy^.next_output_byte;
882 cinfo^.dest^.free_in_buffer := entropy^.free_in_buffer;
883 end;
886 { Finish up a statistics-gathering pass and create the new Huffman tables. }
888 {METHODDEF}
889 procedure finish_pass_gather_phuff (cinfo : j_compress_ptr);
890 var
891 entropy : phuff_entropy_ptr;
892 is_DC_band : boolean;
893 ci, tbl : int;
894 compptr : jpeg_component_info_ptr;
895 htblptr : ^JHUFF_TBL_PTR;
896 did : array[0..NUM_HUFF_TBLS-1] of boolean;
897 begin
898 tbl := 0;
899 entropy := phuff_entropy_ptr (cinfo^.entropy);
901 { Flush out buffered data (all we care about is counting the EOB symbol) }
902 emit_eobrun(entropy);
904 is_DC_band := (cinfo^.Ss = 0);
906 { It's important not to apply jpeg_gen_optimal_table more than once
907 per table, because it clobbers the input frequency counts! }
909 MEMZERO(@did, SIZEOF(did));
911 for ci := 0 to pred(cinfo^.comps_in_scan) do
912 begin
913 compptr := cinfo^.cur_comp_info[ci];
914 if (is_DC_band) then
915 begin
916 if (cinfo^.Ah <> 0) then { DC refinement needs no table }
917 continue;
918 tbl := compptr^.dc_tbl_no;
919 end
920 else
921 begin
922 tbl := compptr^.ac_tbl_no;
923 end;
924 if (not did[tbl]) then
925 begin
926 if (is_DC_band) then
927 htblptr := @(cinfo^.dc_huff_tbl_ptrs[tbl])
928 else
929 htblptr := @(cinfo^.ac_huff_tbl_ptrs[tbl]);
930 if (htblptr^ = NIL) then
931 htblptr^ := jpeg_alloc_huff_table(j_common_ptr(cinfo));
932 jpeg_gen_optimal_table(cinfo, htblptr^, entropy^.count_ptrs[tbl]^);
933 did[tbl] := TRUE;
934 end;
935 end;
936 end;
939 { Module initialization routine for progressive Huffman entropy encoding. }
941 {GLOBAL}
942 procedure jinit_phuff_encoder (cinfo : j_compress_ptr);
943 var
944 entropy : phuff_entropy_ptr;
945 i : int;
946 begin
947 entropy := phuff_entropy_ptr(
948 cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
949 SIZEOF(phuff_entropy_encoder)) );
950 cinfo^.entropy := jpeg_entropy_encoder_ptr(entropy);
951 entropy^.pub.start_pass := start_pass_phuff;
953 { Mark tables unallocated }
954 for i := 0 to pred(NUM_HUFF_TBLS) do
955 begin
956 entropy^.derived_tbls[i] := NIL;
957 entropy^.count_ptrs[i] := NIL;
958 end;
959 entropy^.bit_buffer := NIL; { needed only in AC refinement scan }
960 end;
962 end.