~ubuntu-branches/ubuntu/dapper/fpc/dapper

« back to all changes in this revision

Viewing changes to packages/base/pasjpeg/jdphuff.pas

  • Committer: Bazaar Package Importer
  • Author(s): Carlos Laviola
  • Date: 2004-08-12 16:29:37 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040812162937-moo8ulvysp1ln771
Tags: 1.9.4-5
fp-compiler: needs ld, adding dependency on binutils.  (Closes: #265265)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Unit JdpHuff;
 
2
 
 
3
{ This file contains Huffman entropy decoding routines for progressive JPEG.
 
4
 
 
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. }
 
10
 
 
11
{ Original: jdphuff.c ; Copyright (C) 1995-1997, Thomas G. Lane. }
 
12
 
 
13
interface
 
14
 
 
15
{$I jconfig.inc}
 
16
 
 
17
uses
 
18
  jmorecfg,
 
19
  jinclude,
 
20
  jpeglib,
 
21
  jdeferr,
 
22
  jerror,
 
23
  jutils,
 
24
  jdhuff;               { Declarations shared with jdhuff.c }
 
25
 
 
26
 
 
27
{GLOBAL}
 
28
procedure jinit_phuff_decoder (cinfo : j_decompress_ptr);
 
29
 
 
30
implementation
 
31
 
 
32
{ Expanded entropy decoder object for progressive Huffman decoding.
 
33
 
 
34
  The savable_state subrecord contains fields that change within an MCU,
 
35
  but must not be updated permanently until we complete the MCU. }
 
36
 
 
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;
 
43
 
 
44
 
 
45
type
 
46
  phuff_entropy_ptr  = ^phuff_entropy_decoder;
 
47
  phuff_entropy_decoder = record
 
48
    pub : jpeg_entropy_decoder; { public fields }
 
49
 
 
50
    { These fields are loaded into local variables at start of each MCU.
 
51
      In case of suspension, we exit WITHOUT updating them. }
 
52
 
 
53
    bitstate : bitread_perm_state;      { Bit buffer at start of MCU }
 
54
    saved : savable_state;              { Other state at start of MCU }
 
55
 
 
56
    { These fields are NOT loaded into local working state. }
 
57
    restarts_to_go : uInt;              { MCUs left in this restart interval }
 
58
 
 
59
    { Pointers to derived tables (these workspaces have image lifespan) }
 
60
    derived_tbls : array[0..NUM_HUFF_TBLS-1] of d_derived_tbl_ptr;
 
61
 
 
62
    ac_derived_tbl : d_derived_tbl_ptr; { active table during an AC scan }
 
63
  end;
 
64
 
 
65
 
 
66
 
 
67
{ Forward declarations }
 
68
{METHODDEF}
 
69
function decode_mcu_DC_first (cinfo : j_decompress_ptr;
 
70
                              var MCU_data : array of JBLOCKROW) : boolean;
 
71
                              far; forward;
 
72
{METHODDEF}
 
73
function decode_mcu_AC_first (cinfo : j_decompress_ptr;
 
74
                              var MCU_data : array of JBLOCKROW) : boolean;
 
75
                              far; forward;
 
76
{METHODDEF}
 
77
function decode_mcu_DC_refine (cinfo : j_decompress_ptr;
 
78
                               var MCU_data : array of JBLOCKROW) : boolean;
 
79
                               far; forward;
 
80
{METHODDEF}
 
81
function decode_mcu_AC_refine (cinfo : j_decompress_ptr;
 
82
                               var MCU_data : array of JBLOCKROW) : boolean;
 
83
                               far; forward;
 
84
 
 
85
{ Initialize for a Huffman-compressed scan. }
 
86
 
 
87
{METHODDEF}
 
88
procedure start_pass_phuff_decoder (cinfo : j_decompress_ptr); far;
 
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);
 
100
 
 
101
  is_DC_band := (cinfo^.Ss = 0);
 
102
 
 
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. }
 
132
 
 
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. }
 
139
 
 
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;
 
159
 
 
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;
 
175
 
 
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. }
 
181
 
 
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;
 
202
 
 
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;
 
207
 
 
208
  { Initialize private state variables }
 
209
  entropy^.saved.EOBRUN := 0;
 
210
 
 
211
  { Initialize restart counter }
 
212
  entropy^.restarts_to_go := cinfo^.restart_interval;
 
213
end;
 
214
 
 
215
 
 
216
{ Figure F.12: extend sign bit.
 
217
  On some machines, a shift and add will be faster than a table lookup. }
 
218
 
 
219
{$ifdef AVOID_TABLES}
 
220
 
 
221
#define HUFF_EXTEND(x,s)
 
222
  ((x) < (1shl((s)-1)) ? (x) + (((-1)shl(s)) + 1) : (x))
 
223
 
 
224
{$else}
 
225
 
 
226
{ #define HUFF_EXTEND(x,s)
 
227
  if (x) < extend_test[s] then
 
228
    (x) + extend_offset[s]
 
229
  else
 
230
    (x)}
 
231
 
 
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);
 
236
 
 
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 );
 
243
 
 
244
{$endif} { AVOID_TABLES }
 
245
 
 
246
 
 
247
{ Check for a restart marker & resynchronize decoder.
 
248
  return:=s FALSE if must suspend. }
 
249
 
 
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);
 
257
 
 
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;
 
262
 
 
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;
 
269
 
 
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;
 
275
 
 
276
  { Reset restart counter }
 
277
  entropy^.restarts_to_go := cinfo^.restart_interval;
 
278
 
 
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;
 
285
 
 
286
  process_restart := TRUE;
 
287
end;
 
288
 
 
289
 
 
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.
 
295
 
 
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.
 
298
 
 
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.) }
 
304
 
 
305
 
 
306
{ MCU decoding for DC initial scan (either spectral selection,
 
307
  or first pass of successive approximation). }
 
308
 
 
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;
 
324
 
 
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;
 
333
 
 
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;
 
344
 
 
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. }
 
347
 
 
348
  if not entropy^.pub.insufficient_data then
 
349
  begin
 
350
 
 
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;
 
358
 
 
359
    {ASSIGN_STATE(state, entropy^.saved);}
 
360
    state := entropy^.saved;
 
361
 
 
362
    { Outer loop handles each block in the MCU }
 
363
 
 
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];
 
370
 
 
371
      { Decode a single block's worth of coefficients }
 
372
 
 
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);
 
393
 
 
394
      nb := tbl^.look_nbits[look];
 
395
      if (nb <> 0) then
 
396
      begin
 
397
        {DROP_BITS(nb);}
 
398
        Dec(bits_left, nb);
 
399
 
 
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;
 
415
 
 
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;
 
429
 
 
430
        {r := GET_BITS(s);}
 
431
        Dec(bits_left, s);
 
432
        r := (int(get_buffer shr bits_left)) and ( pred(1 shl s) );
 
433
 
 
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;
 
440
 
 
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;
 
447
 
 
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;
 
454
 
 
455
    {ASSIGN_STATE(entropy^.saved, state);}
 
456
    entropy^.saved := state;
 
457
  end;
 
458
 
 
459
  { Account for restart interval (no-op if not using restarts) }
 
460
  Dec(entropy^.restarts_to_go);
 
461
 
 
462
  decode_mcu_DC_first := TRUE;
 
463
end;
 
464
 
 
465
 
 
466
{ MCU decoding for AC initial scan (either spectral selection,
 
467
  or first pass of successive approximation). }
 
468
 
 
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;
 
485
 
 
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;
 
493
 
 
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;
 
504
 
 
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
 
509
 
 
510
    { Load up working state.
 
511
      We can avoid loading/saving bitread state if in an EOB run. }
 
512
 
 
513
    EOBRUN := entropy^.saved.EOBRUN; { only part of saved state we care about }
 
514
 
 
515
    { There is always only one block per MCU }
 
516
 
 
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;
 
527
 
 
528
      block := JBLOCK_PTR(MCU_data[0]);
 
529
      tbl := entropy^.ac_derived_tbl;
 
530
 
 
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);
 
553
 
 
554
        nb := tbl^.look_nbits[look];
 
555
        if (nb <> 0) then
 
556
        begin
 
557
          {DROP_BITS(nb);}
 
558
          Dec(bits_left, nb);
 
559
 
 
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;
 
575
 
 
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;
 
592
 
 
593
          {r := GET_BITS(s);}
 
594
          Dec(bits_left, s);
 
595
          r := (int(get_buffer shr bits_left)) and ( pred(1 shl s) );
 
596
 
 
597
          {s := HUFF_EXTEND(r, s);}
 
598
          if (r < extend_test[s]) then
 
599
            s := r + extend_offset[s]
 
600
          else
 
601
            s := r;
 
602
 
 
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;
 
628
 
 
629
              {r := GET_BITS(r);}
 
630
              Dec(bits_left, r);
 
631
              r := (int(get_buffer shr bits_left)) and ( pred(1 shl r) );
 
632
 
 
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;
 
641
 
 
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;
 
648
 
 
649
    { Completed MCU, so update state }
 
650
    entropy^.saved.EOBRUN := EOBRUN; { only part of saved state we care about }
 
651
  end;
 
652
 
 
653
  { Account for restart interval (no-op if not using restarts) }
 
654
  Dec(entropy^.restarts_to_go);
 
655
 
 
656
  decode_mcu_AC_first := TRUE;
 
657
end;
 
658
 
 
659
 
 
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. }
 
663
 
 
664
{METHODDEF}
 
665
function decode_mcu_DC_refine (cinfo : j_decompress_ptr;
 
666
                               var MCU_data : array of JBLOCKROW) : boolean;
 
667
 
 
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;
 
680
 
 
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;
 
691
 
 
692
  { Not worth the cycles to check insufficient_data here,
 
693
    since we will not change the data anyway if we read zeroes. }
 
694
 
 
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;
 
702
 
 
703
  { Outer loop handles each block in the MCU }
 
704
 
 
705
  for blkn := 0 to pred(cinfo^.blocks_in_MCU) do
 
706
  begin
 
707
    block := JBLOCK_PTR(MCU_data[blkn]);
 
708
 
 
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;
 
721
 
 
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;
 
728
 
 
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;
 
735
 
 
736
  { Account for restart interval (no-op if not using restarts) }
 
737
  Dec(entropy^.restarts_to_go);
 
738
 
 
739
  decode_mcu_DC_refine := TRUE;
 
740
end;
 
741
 
 
742
 
 
743
{ MCU decoding for AC successive approximation refinement scan. }
 
744
 
 
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;
 
763
 
 
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
  entropy := phuff_entropy_ptr (cinfo^.entropy);
 
773
  Se := cinfo^.Se;
 
774
  p1 := 1 shl cinfo^.Al;        { 1 in the bit position being coded }
 
775
  m1 := (-1) shl cinfo^.Al;     { -1 in the bit position being coded }
 
776
 
 
777
  { Process restart marker if needed; may have to suspend }
 
778
  if (cinfo^.restart_interval <> 0) then
 
779
  begin
 
780
    if (entropy^.restarts_to_go = 0) then
 
781
      if (not process_restart(cinfo)) then
 
782
      begin
 
783
        decode_mcu_AC_refine := FALSE;
 
784
        exit;
 
785
      end;
 
786
  end;
 
787
 
 
788
  { If we've run out of data, don't modify the MCU. }
 
789
  if not entropy^.pub.insufficient_data then
 
790
  begin
 
791
 
 
792
    { Load up working state }
 
793
    {BITREAD_LOAD_STATE(cinfo,entropy^.bitstate);}
 
794
    br_state.cinfo := cinfo;
 
795
    br_state.next_input_byte := cinfo^.src^.next_input_byte;
 
796
    br_state.bytes_in_buffer := cinfo^.src^.bytes_in_buffer;
 
797
    get_buffer := entropy^.bitstate.get_buffer;
 
798
    bits_left := entropy^.bitstate.bits_left;
 
799
 
 
800
    EOBRUN := entropy^.saved.EOBRUN; { only part of saved state we care about }
 
801
 
 
802
    { There is always only one block per MCU }
 
803
    block := JBLOCK_PTR(MCU_data[0]);
 
804
    tbl := entropy^.ac_derived_tbl;
 
805
 
 
806
    { If we are forced to suspend, we must undo the assignments to any newly
 
807
      nonzero coefficients in the block, because otherwise we'd get confused
 
808
      next time about which coefficients were already nonzero.
 
809
      But we need not undo addition of bits to already-nonzero coefficients;
 
810
      instead, we can test the current bit position to see if we already did it.}
 
811
 
 
812
    num_newnz := 0;
 
813
 
 
814
    { initialize coefficient loop counter to start of band }
 
815
    k := cinfo^.Ss;
 
816
 
 
817
    if (EOBRUN = 0) then
 
818
    begin
 
819
      while (k <= Se) do
 
820
      begin
 
821
        {HUFF_DECODE(s, br_state, tbl, goto undoit, label3);}
 
822
        if (bits_left < HUFF_LOOKAHEAD) then
 
823
        begin
 
824
          if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) then
 
825
            goto undoit;
 
826
          get_buffer := br_state.get_buffer;
 
827
          bits_left := br_state.bits_left;
 
828
          if (bits_left < HUFF_LOOKAHEAD) then
 
829
          begin
 
830
            nb := 1;
 
831
            goto label3;
 
832
          end;
 
833
        end;
 
834
        {look := PEEK_BITS(HUFF_LOOKAHEAD);}
 
835
        look := int(get_buffer shr (bits_left -  HUFF_LOOKAHEAD)) and
 
836
                       pred(1 shl HUFF_LOOKAHEAD);
 
837
 
 
838
        nb := tbl^.look_nbits[look];
 
839
        if (nb <> 0) then
 
840
        begin
 
841
          {DROP_BITS(nb);}
 
842
          Dec(bits_left, nb);
 
843
 
 
844
          s := tbl^.look_sym[look];
 
845
        end
 
846
        else
 
847
        begin
 
848
          nb := HUFF_LOOKAHEAD+1;
 
849
      label3:
 
850
          s := jpeg_huff_decode(br_state,get_buffer,bits_left,tbl,nb);
 
851
          if (s < 0) then
 
852
            goto undoit;
 
853
          get_buffer := br_state.get_buffer;
 
854
          bits_left := br_state.bits_left;
 
855
        end;
 
856
 
 
857
        r := s shr 4;
 
858
        s := s and 15;
 
859
        if (s <> 0) then
 
860
        begin
 
861
          if (s <> 1) then      { size of new coef should always be 1 }
 
862
            WARNMS(j_common_ptr(cinfo), JWRN_HUFF_BAD_CODE);
 
863
          {CHECK_BIT_BUFFER(br_state, 1, goto undoit);}
 
864
          if (bits_left < 1) then
 
865
          begin
 
866
            if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
 
867
              goto undoit;
 
868
            get_buffer := br_state.get_buffer;
 
869
            bits_left := br_state.bits_left;
 
870
          end;
 
871
 
 
872
          {if (GET_BITS(1)) then}
 
873
          Dec(bits_left);
 
874
          if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) )<>0 then
 
875
            s := p1             { newly nonzero coef is positive }
 
876
          else
 
877
            s := m1;            { newly nonzero coef is negative }
 
878
        end
 
879
        else
 
880
        begin
 
881
          if (r <> 15) then
 
882
          begin
 
883
            EOBRUN := 1 shl r;  { EOBr, run length is 2^r + appended bits }
 
884
            if (r <> 0) then
 
885
            begin
 
886
              {CHECK_BIT_BUFFER(br_state, r, goto undoit);}
 
887
              if (bits_left < r) then
 
888
              begin
 
889
                if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,r)) then
 
890
                  goto undoit;
 
891
                get_buffer := br_state.get_buffer;
 
892
                bits_left := br_state.bits_left;
 
893
              end;
 
894
 
 
895
              {r := GET_BITS(r);}
 
896
              Dec(bits_left, r);
 
897
              r := (int(get_buffer shr bits_left)) and ( pred(1 shl r) );
 
898
 
 
899
              Inc(EOBRUN, r);
 
900
            end;
 
901
            break;              { rest of block is handled by EOB logic }
 
902
          end;
 
903
          { note s := 0 for processing ZRL }
 
904
        end;
 
905
        { Advance over already-nonzero coefs and r still-zero coefs,
 
906
          appending correction bits to the nonzeroes.  A correction bit is 1
 
907
          if the absolute value of the coefficient must be increased. }
 
908
 
 
909
        repeat
 
910
          thiscoef :=@(block^[jpeg_natural_order[k]]);
 
911
          if (thiscoef^ <> 0) then
 
912
          begin
 
913
            {CHECK_BIT_BUFFER(br_state, 1, goto undoit);}
 
914
            if (bits_left < 1) then
 
915
            begin
 
916
              if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
 
917
                goto undoit;
 
918
              get_buffer := br_state.get_buffer;
 
919
              bits_left := br_state.bits_left;
 
920
            end;
 
921
 
 
922
            {if (GET_BITS(1)) then}
 
923
            Dec(bits_left);
 
924
            if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) )<>0 then
 
925
            begin
 
926
              if ((thiscoef^ and p1) = 0) then
 
927
              begin { do nothing if already set it }
 
928
                if (thiscoef^ >= 0) then
 
929
                  Inc(thiscoef^, p1)
 
930
                else
 
931
                  Inc(thiscoef^, m1);
 
932
              end;
 
933
            end;
 
934
          end
 
935
          else
 
936
          begin
 
937
            Dec(r);
 
938
            if (r < 0) then
 
939
              break;            { reached target zero coefficient }
 
940
          end;
 
941
          Inc(k);
 
942
        until (k > Se);
 
943
        if (s <> 0) then
 
944
        begin
 
945
          pos := jpeg_natural_order[k];
 
946
          { Output newly nonzero coefficient }
 
947
          block^[pos] := JCOEF (s);
 
948
          { Remember its position in case we have to suspend }
 
949
          newnz_pos[num_newnz] := pos;
 
950
          Inc(num_newnz);
 
951
        end;
 
952
        Inc(k);
 
953
      end;
 
954
    end;
 
955
 
 
956
    if (EOBRUN > 0) then
 
957
    begin
 
958
      { Scan any remaining coefficient positions after the end-of-band
 
959
        (the last newly nonzero coefficient, if any).  Append a correction
 
960
        bit to each already-nonzero coefficient.  A correction bit is 1
 
961
        if the absolute value of the coefficient must be increased. }
 
962
 
 
963
      while (k <= Se) do
 
964
      begin
 
965
        thiscoef := @(block^[jpeg_natural_order[k]]);
 
966
        if (thiscoef^ <> 0) then
 
967
        begin
 
968
          {CHECK_BIT_BUFFER(br_state, 1, goto undoit);}
 
969
          if (bits_left < 1) then
 
970
          begin
 
971
            if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
 
972
              goto undoit;
 
973
            get_buffer := br_state.get_buffer;
 
974
            bits_left := br_state.bits_left;
 
975
          end;
 
976
 
 
977
          {if (GET_BITS(1)) then}
 
978
          Dec(bits_left);
 
979
          if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) )<>0 then
 
980
          begin
 
981
            if ((thiscoef^ and p1) = 0) then
 
982
            begin { do nothing if already changed it }
 
983
              if (thiscoef^ >= 0) then
 
984
                Inc(thiscoef^, p1)
 
985
              else
 
986
                Inc(thiscoef^, m1);
 
987
            end;
 
988
          end;
 
989
        end;
 
990
        Inc(k);
 
991
      end;
 
992
      { Count one block completed in EOB run }
 
993
      Dec(EOBRUN);
 
994
    end;
 
995
 
 
996
    { Completed MCU, so update state }
 
997
    {BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
 
998
    cinfo^.src^.next_input_byte := br_state.next_input_byte;
 
999
    cinfo^.src^.bytes_in_buffer := br_state.bytes_in_buffer;
 
1000
    entropy^.bitstate.get_buffer := get_buffer;
 
1001
    entropy^.bitstate.bits_left := bits_left;
 
1002
 
 
1003
    entropy^.saved.EOBRUN := EOBRUN; { only part of saved state we care about }
 
1004
  end;
 
1005
 
 
1006
  { Account for restart interval (no-op if not using restarts) }
 
1007
  Dec(entropy^.restarts_to_go);
 
1008
 
 
1009
  decode_mcu_AC_refine := TRUE;
 
1010
  exit;
 
1011
 
 
1012
undoit:
 
1013
  { Re-zero any output coefficients that we made newly nonzero }
 
1014
  while (num_newnz > 0) do
 
1015
  begin
 
1016
    Dec(num_newnz);
 
1017
    block^[newnz_pos[num_newnz]] := 0;
 
1018
  end;
 
1019
 
 
1020
  decode_mcu_AC_refine := FALSE;
 
1021
end;
 
1022
 
 
1023
 
 
1024
{ Module initialization routine for progressive Huffman entropy decoding. }
 
1025
 
 
1026
{GLOBAL}
 
1027
procedure jinit_phuff_decoder (cinfo : j_decompress_ptr);
 
1028
var
 
1029
  entropy : phuff_entropy_ptr;
 
1030
  coef_bit_ptr : int_ptr;
 
1031
  ci, i : int;
 
1032
begin
 
1033
  entropy := phuff_entropy_ptr(
 
1034
    cinfo^.mem^.alloc_small (j_common_ptr (cinfo), JPOOL_IMAGE,
 
1035
                                SIZEOF(phuff_entropy_decoder)) );
 
1036
  cinfo^.entropy := jpeg_entropy_decoder_ptr (entropy);
 
1037
  entropy^.pub.start_pass := start_pass_phuff_decoder;
 
1038
 
 
1039
  { Mark derived tables unallocated }
 
1040
  for i := 0 to pred(NUM_HUFF_TBLS) do
 
1041
  begin
 
1042
    entropy^.derived_tbls[i] := NIL;
 
1043
  end;
 
1044
 
 
1045
  { Create progression status table }
 
1046
  cinfo^.coef_bits := coef_bits_ptrrow (
 
1047
     cinfo^.mem^.alloc_small ( j_common_ptr (cinfo), JPOOL_IMAGE,
 
1048
                                cinfo^.num_components*DCTSIZE2*SIZEOF(int)) );
 
1049
  coef_bit_ptr := @cinfo^.coef_bits^[0][0];
 
1050
  for ci := 0 to pred(cinfo^.num_components) do
 
1051
    for i := 0 to pred(DCTSIZE2) do
 
1052
    begin
 
1053
      coef_bit_ptr^ := -1;
 
1054
      Inc(coef_bit_ptr);
 
1055
    end;
 
1056
end;
 
1057
 
 
1058
end.
 
 
b'\\ No newline at end of file'