~ubuntu-branches/debian/lenny/fpc/lenny

« back to all changes in this revision

Viewing changes to fpcsrc/packages/base/pasjpeg/jcphuff.pas

  • Committer: Bazaar Package Importer
  • Author(s): Mazen Neifer, Torsten Werner, Mazen Neifer
  • Date: 2008-05-17 17:12:11 UTC
  • mfrom: (3.1.9 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080517171211-9qi33xhd9evfa0kg
Tags: 2.2.0-dfsg1-9
[ Torsten Werner ]
* Add Mazen Neifer to Uploaders field.

[ Mazen Neifer ]
* Moved FPC sources into a version dependent directory from /usr/share/fpcsrc
  to /usr/share/fpcsrc/${FPCVERSION}. This allow installing more than on FPC
  release.
* Fixed far call issue in compiler preventing building huge binearies.
  (closes: #477743)
* Updated building dependencies, recomennded and suggested packages.
* Moved fppkg to fp-utils as it is just a helper tool and is not required by
  compiler.

Show diffs side-by-side

added added

removed removed

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