Coverage Report

Created: 2026-08-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dcmtk/dcmjpeg/libijg8/jcshuff.c
Line
Count
Source
1
/*
2
 * jcshuff.c
3
 *
4
 * Copyright (C) 1991-1998, Thomas G. Lane.
5
 * This file is part of the Independent JPEG Group's software.
6
 * For conditions of distribution and use, see the accompanying README file.
7
 *
8
 * This file contains Huffman entropy encoding routines for sequential JPEG.
9
 *
10
 * Much of the complexity here has to do with supporting output suspension.
11
 * If the data destination module demands suspension, we want to be able to
12
 * back up to the start of the current MCU.  To do this, we copy state
13
 * variables into local working storage, and update them back to the
14
 * permanent JPEG objects only upon successful completion of an MCU.
15
 */
16
17
#define JPEG_INTERNALS
18
#include "jinclude8.h"
19
#include "jpeglib8.h"
20
#include "jlossy8.h"    /* Private declarations for lossy codec */
21
#include "jchuff8.h"    /* Declarations shared with jc*huff.c */
22
23
24
/* Expanded entropy encoder object for Huffman encoding.
25
 *
26
 * The savable_state subrecord contains fields that change within an MCU,
27
 * but must not be updated permanently until we complete the MCU.
28
 */
29
30
typedef struct {
31
  IJG_INT32 put_buffer;   /* current bit-accumulation buffer */
32
  int put_bits;     /* # of bits now in it */
33
  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
34
} savable_state;
35
36
/* This macro is to work around compilers with missing or broken
37
 * structure assignment.  You'll need to fix this code if you have
38
 * such a compiler and you change MAX_COMPS_IN_SCAN.
39
 */
40
41
#ifndef NO_STRUCT_ASSIGN
42
0
#define ASSIGN_STATE(dest,src)  ((dest) = (src))
43
#else
44
#if MAX_COMPS_IN_SCAN == 4
45
#define ASSIGN_STATE(dest,src)  \
46
  ((dest).put_buffer = (src).put_buffer, \
47
   (dest).put_bits = (src).put_bits, \
48
   (dest).last_dc_val[0] = (src).last_dc_val[0], \
49
   (dest).last_dc_val[1] = (src).last_dc_val[1], \
50
   (dest).last_dc_val[2] = (src).last_dc_val[2], \
51
   (dest).last_dc_val[3] = (src).last_dc_val[3])
52
#endif
53
#endif
54
55
56
typedef struct {
57
  savable_state saved;    /* Bit buffer & DC state at start of MCU */
58
59
  /* These fields are NOT loaded into local working state. */
60
  unsigned int restarts_to_go;  /* MCUs left in this restart interval */
61
  int next_restart_num;   /* next restart number to write (0-7) */
62
63
  /* Pointers to derived tables (these workspaces have image lifespan) */
64
  c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
65
  c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
66
67
#ifdef ENTROPY_OPT_SUPPORTED  /* Statistics tables for optimization */
68
  long * dc_count_ptrs[NUM_HUFF_TBLS];
69
  long * ac_count_ptrs[NUM_HUFF_TBLS];
70
#endif
71
} shuff_entropy_encoder;
72
73
typedef shuff_entropy_encoder * shuff_entropy_ptr;
74
75
/* Working state while writing an MCU.
76
 * This struct contains all the fields that are needed by subroutines.
77
 */
78
79
typedef struct {
80
  JOCTET * next_output_byte;  /* => next byte to write in buffer */
81
  size_t free_in_buffer;  /* # of byte spaces remaining in buffer */
82
  savable_state cur;    /* Current bit buffer & DC state */
83
  j_compress_ptr cinfo;   /* dump_buffer needs access to this */
84
} working_state;
85
86
87
/* Forward declarations */
88
METHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo,
89
          JBLOCKROW *MCU_data));
90
METHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo));
91
#ifdef ENTROPY_OPT_SUPPORTED
92
METHODDEF(boolean) encode_mcu_gather JPP((j_compress_ptr cinfo,
93
            JBLOCKROW *MCU_data));
94
METHODDEF(void) finish_pass_gather JPP((j_compress_ptr cinfo));
95
#endif
96
97
98
/*
99
 * Initialize for a Huffman-compressed scan.
100
 * If gather_statistics is TRUE, we do not output anything during the scan,
101
 * just count the Huffman symbols used and generate Huffman code tables.
102
 */
103
104
METHODDEF(void)
105
start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
106
0
{
107
0
  j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
108
0
  shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyc->entropy_private;
109
0
  int ci, dctbl, actbl;
110
0
  jpeg_component_info * compptr;
111
112
0
  if (gather_statistics) {
113
0
#ifdef ENTROPY_OPT_SUPPORTED
114
0
    lossyc->entropy_encode_mcu = encode_mcu_gather;
115
0
    lossyc->pub.entropy_finish_pass = finish_pass_gather;
116
#else
117
    ERREXIT(cinfo, JERR_NOT_COMPILED);
118
#endif
119
0
  } else {
120
0
    lossyc->entropy_encode_mcu = encode_mcu_huff;
121
0
    lossyc->pub.entropy_finish_pass = finish_pass_huff;
122
0
  }
123
124
0
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
125
0
    compptr = cinfo->cur_comp_info[ci];
126
0
    dctbl = compptr->dc_tbl_no;
127
0
    actbl = compptr->ac_tbl_no;
128
0
    if (gather_statistics) {
129
0
#ifdef ENTROPY_OPT_SUPPORTED
130
      /* Check for invalid table indexes */
131
      /* (make_c_derived_tbl does this in the other path) */
132
0
      if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
133
0
  ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
134
0
      if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
135
0
  ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
136
      /* Allocate and zero the statistics tables */
137
      /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
138
0
      if (entropy->dc_count_ptrs[dctbl] == NULL)
139
0
  entropy->dc_count_ptrs[dctbl] = (long *)
140
0
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
141
0
              257 * SIZEOF(long));
142
0
      MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
143
0
      if (entropy->ac_count_ptrs[actbl] == NULL)
144
0
  entropy->ac_count_ptrs[actbl] = (long *)
145
0
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
146
0
              257 * SIZEOF(long));
147
0
      MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
148
0
#endif
149
0
    } else {
150
      /* Compute derived values for Huffman tables */
151
      /* We may do this more than once for a table, but it's not expensive */
152
0
      jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
153
0
            & entropy->dc_derived_tbls[dctbl]);
154
0
      jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
155
0
            & entropy->ac_derived_tbls[actbl]);
156
0
    }
157
    /* Initialize DC predictions to 0 */
158
0
    entropy->saved.last_dc_val[ci] = 0;
159
0
  }
160
161
  /* Initialize bit buffer to empty */
162
0
  entropy->saved.put_buffer = 0;
163
0
  entropy->saved.put_bits = 0;
164
165
  /* Initialize restart stuff */
166
0
  entropy->restarts_to_go = cinfo->restart_interval;
167
0
  entropy->next_restart_num = 0;
168
0
}
169
170
171
/* Outputting bytes to the file */
172
173
/* Emit a byte, taking 'action' if must suspend. */
174
#define emit_byte(state,val,action)  \
175
0
  { *(state)->next_output_byte++ = (JOCTET) (val);  \
176
0
    if (--(state)->free_in_buffer == 0)  \
177
0
      if (! dump_buffer(state))  \
178
0
        { action; } }
179
180
181
LOCAL(boolean)
182
dump_buffer (working_state * state)
183
/* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
184
0
{
185
0
  struct jpeg_destination_mgr * dest = state->cinfo->dest;
186
187
0
  if (! (*dest->empty_output_buffer) (state->cinfo))
188
0
    return FALSE;
189
  /* After a successful buffer dump, must reset buffer pointers */
190
0
  state->next_output_byte = dest->next_output_byte;
191
0
  state->free_in_buffer = dest->free_in_buffer;
192
0
  return TRUE;
193
0
}
194
195
196
/* Outputting bits to the file */
197
198
/* Only the right 24 bits of put_buffer are used; the valid bits are
199
 * left-justified in this part.  At most 16 bits can be passed to emit_bits
200
 * in one call, and we never retain more than 7 bits in put_buffer
201
 * between calls, so 24 bits are sufficient.
202
 */
203
204
INLINE
205
LOCAL(boolean)
206
emit_bits (working_state * state, unsigned int code, int size)
207
/* Emit some bits; return TRUE if successful, FALSE if must suspend */
208
0
{
209
  /* This routine is heavily used, so it's worth coding tightly. */
210
0
  register IJG_INT32 put_buffer = (IJG_INT32) code;
211
0
  register int put_bits = state->cur.put_bits;
212
213
  /* if size is 0, caller used an invalid Huffman table entry */
214
0
  if (size == 0)
215
0
    ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
216
217
0
  put_buffer &= (((IJG_INT32) 1)<<size) - 1; /* mask off any extra bits in code */
218
  
219
0
  put_bits += size;   /* new number of bits in buffer */
220
  
221
0
  put_buffer <<= 24 - put_bits; /* align incoming bits */
222
223
0
  put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */
224
  
225
0
  while (put_bits >= 8) {
226
0
    int c = (int) ((put_buffer >> 16) & 0xFF);
227
    
228
0
    emit_byte(state, c, return FALSE);
229
0
    if (c == 0xFF) {   /* need to stuff a zero byte? */
230
0
      emit_byte(state, 0, return FALSE);
231
0
    }
232
0
    put_buffer <<= 8;
233
0
    put_bits -= 8;
234
0
  }
235
236
0
  state->cur.put_buffer = put_buffer; /* update state variables */
237
0
  state->cur.put_bits = put_bits;
238
239
0
  return TRUE;
240
0
}
241
242
243
LOCAL(boolean)
244
flush_bits (working_state * state)
245
0
{
246
0
  if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */
247
0
    return FALSE;
248
0
  state->cur.put_buffer = 0;  /* and reset bit-buffer to empty */
249
0
  state->cur.put_bits = 0;
250
0
  return TRUE;
251
0
}
252
253
254
/* Encode a single block's worth of coefficients */
255
256
LOCAL(boolean)
257
encode_one_block (working_state * state, const JCOEFPTR block, int last_dc_val,
258
      c_derived_tbl *dctbl, c_derived_tbl *actbl)
259
0
{
260
0
  register int temp, temp2;
261
0
  register int nbits;
262
0
  register int k, r, i;
263
  
264
  /* Encode the DC coefficient difference per section F.1.2.1 */
265
  
266
0
  temp = temp2 = block[0] - last_dc_val;
267
268
0
  if (temp < 0) {
269
0
    temp = -temp;   /* temp is abs value of input */
270
    /* For a negative input, want temp2 = bitwise complement of abs(input) */
271
    /* This code assumes we are on a two's complement machine */
272
0
    temp2--;
273
0
  }
274
  
275
  /* Find the number of bits needed for the magnitude of the coefficient */
276
0
  nbits = 0;
277
0
  while (temp) {
278
0
    nbits++;
279
0
    temp >>= 1;
280
0
  }
281
  /* Check for out-of-range coefficient values.
282
   * Since we're encoding a difference, the range limit is twice as much.
283
   */
284
0
  if (nbits > MAX_COEF_BITS+1)
285
0
    ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
286
  
287
  /* Emit the Huffman-coded symbol for the number of bits */
288
0
  if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
289
0
    return FALSE;
290
291
  /* Emit that number of bits of the value, if positive, */
292
  /* or the complement of its magnitude, if negative. */
293
0
  if (nbits)     /* emit_bits rejects calls with size 0 */
294
0
    if (! emit_bits(state, (unsigned int) temp2, nbits))
295
0
      return FALSE;
296
297
  /* Encode the AC coefficients per section F.1.2.2 */
298
  
299
0
  r = 0;      /* r = run length of zeros */
300
  
301
0
  for (k = 1; k < DCTSIZE2; k++) {
302
0
    if ((temp = block[jpeg_natural_order[k]]) == 0) {
303
0
      r++;
304
0
    } else {
305
      /* if run length > 15, must emit special run-length-16 codes (0xF0) */
306
0
      while (r > 15) {
307
0
  if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
308
0
    return FALSE;
309
0
  r -= 16;
310
0
      }
311
312
0
      temp2 = temp;
313
0
      if (temp < 0) {
314
0
  temp = -temp;   /* temp is abs value of input */
315
  /* This code assumes we are on a two's complement machine */
316
0
  temp2--;
317
0
      }
318
      
319
      /* Find the number of bits needed for the magnitude of the coefficient */
320
0
      nbits = 1;    /* there must be at least one 1 bit */
321
0
      while ((temp >>= 1))
322
0
  nbits++;
323
      /* Check for out-of-range coefficient values */
324
0
      if (nbits > MAX_COEF_BITS)
325
0
  ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
326
      
327
      /* Emit Huffman symbol for run length / number of bits */
328
0
      i = (r << 4) + nbits;
329
0
      if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))
330
0
  return FALSE;
331
332
      /* Emit that number of bits of the value, if positive, */
333
      /* or the complement of its magnitude, if negative. */
334
0
      if (! emit_bits(state, (unsigned int) temp2, nbits))
335
0
  return FALSE;
336
      
337
0
      r = 0;
338
0
    }
339
0
  }
340
341
  /* If the last coef(s) were zero, emit an end-of-block code */
342
0
  if (r > 0)
343
0
    if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
344
0
      return FALSE;
345
346
0
  return TRUE;
347
0
}
348
349
350
/*
351
 * Emit a restart marker & resynchronize predictions.
352
 */
353
354
LOCAL(boolean)
355
emit_restart (working_state * state, int restart_num)
356
0
{
357
0
  int ci;
358
359
0
  if (! flush_bits(state))
360
0
    return FALSE;
361
362
0
  emit_byte(state, 0xFF, return FALSE);
363
0
  emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
364
365
  /* Re-initialize DC predictions to 0 */
366
0
  for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
367
0
    state->cur.last_dc_val[ci] = 0;
368
369
  /* The restart counter is not updated until we successfully write the MCU. */
370
371
0
  return TRUE;
372
0
}
373
374
375
/*
376
 * Encode and output one MCU's worth of Huffman-compressed coefficients.
377
 */
378
379
METHODDEF(boolean)
380
encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
381
0
{
382
0
  j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
383
0
  shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyc->entropy_private;
384
0
  working_state state;
385
0
  int blkn, ci;
386
0
  jpeg_component_info * compptr;
387
388
  /* Load up working state */
389
0
  state.next_output_byte = cinfo->dest->next_output_byte;
390
0
  state.free_in_buffer = cinfo->dest->free_in_buffer;
391
0
  ASSIGN_STATE(state.cur, entropy->saved);
392
0
  state.cinfo = cinfo;
393
394
  /* Emit restart marker if needed */
395
0
  if (cinfo->restart_interval) {
396
0
    if (entropy->restarts_to_go == 0)
397
0
      if (! emit_restart(&state, entropy->next_restart_num))
398
0
  return FALSE;
399
0
  }
400
401
  /* Encode the MCU data blocks */
402
0
  for (blkn = 0; blkn < cinfo->data_units_in_MCU; blkn++) {
403
0
    ci = cinfo->MCU_membership[blkn];
404
0
    compptr = cinfo->cur_comp_info[ci];
405
0
    if (! encode_one_block(&state,
406
0
         MCU_data[blkn][0], state.cur.last_dc_val[ci],
407
0
         entropy->dc_derived_tbls[compptr->dc_tbl_no],
408
0
         entropy->ac_derived_tbls[compptr->ac_tbl_no]))
409
0
      return FALSE;
410
    /* Update last_dc_val */
411
0
    state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
412
0
  }
413
414
  /* Completed MCU, so update state */
415
0
  cinfo->dest->next_output_byte = state.next_output_byte;
416
0
  cinfo->dest->free_in_buffer = state.free_in_buffer;
417
0
  ASSIGN_STATE(entropy->saved, state.cur);
418
419
  /* Update restart-interval state too */
420
0
  if (cinfo->restart_interval) {
421
0
    if (entropy->restarts_to_go == 0) {
422
0
      entropy->restarts_to_go = cinfo->restart_interval;
423
0
      entropy->next_restart_num++;
424
0
      entropy->next_restart_num &= 7;
425
0
    }
426
0
    entropy->restarts_to_go--;
427
0
  }
428
429
0
  return TRUE;
430
0
}
431
432
433
/*
434
 * Finish up at the end of a Huffman-compressed scan.
435
 */
436
437
METHODDEF(void)
438
finish_pass_huff (j_compress_ptr cinfo)
439
0
{
440
0
  j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
441
0
  shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyc->entropy_private;
442
0
  working_state state;
443
444
  /* Load up working state ... flush_bits needs it */
445
0
  state.next_output_byte = cinfo->dest->next_output_byte;
446
0
  state.free_in_buffer = cinfo->dest->free_in_buffer;
447
0
  ASSIGN_STATE(state.cur, entropy->saved);
448
0
  state.cinfo = cinfo;
449
450
  /* Flush out the last data */
451
0
  if (! flush_bits(&state))
452
0
    ERREXIT(cinfo, JERR_CANT_SUSPEND);
453
454
  /* Update state */
455
0
  cinfo->dest->next_output_byte = state.next_output_byte;
456
0
  cinfo->dest->free_in_buffer = state.free_in_buffer;
457
0
  ASSIGN_STATE(entropy->saved, state.cur);
458
0
}
459
460
461
/*
462
 * Huffman coding optimization.
463
 *
464
 * We first scan the supplied data and count the number of uses of each symbol
465
 * that is to be Huffman-coded. (This process MUST agree with the code above.)
466
 * Then we build a Huffman coding tree for the observed counts.
467
 * Symbols which are not needed at all for the particular image are not
468
 * assigned any code, which saves space in the DHT marker as well as in
469
 * the compressed data.
470
 */
471
472
#ifdef ENTROPY_OPT_SUPPORTED
473
474
475
/* Process a single block's worth of coefficients */
476
477
LOCAL(void)
478
htest_one_block (j_compress_ptr cinfo, const JCOEFPTR block, int last_dc_val,
479
     long dc_counts[], long ac_counts[])
480
0
{
481
0
  register int temp;
482
0
  register int nbits;
483
0
  register int k, r;
484
  
485
  /* Encode the DC coefficient difference per section F.1.2.1 */
486
  
487
0
  temp = block[0] - last_dc_val;
488
0
  if (temp < 0)
489
0
    temp = -temp;
490
  
491
  /* Find the number of bits needed for the magnitude of the coefficient */
492
0
  nbits = 0;
493
0
  while (temp) {
494
0
    nbits++;
495
0
    temp >>= 1;
496
0
  }
497
  /* Check for out-of-range coefficient values.
498
   * Since we're encoding a difference, the range limit is twice as much.
499
   */
500
0
  if (nbits > MAX_COEF_BITS+1)
501
0
    ERREXIT(cinfo, JERR_BAD_DCT_COEF);
502
503
  /* Count the Huffman symbol for the number of bits */
504
0
  dc_counts[nbits]++;
505
  
506
  /* Encode the AC coefficients per section F.1.2.2 */
507
  
508
0
  r = 0;      /* r = run length of zeros */
509
  
510
0
  for (k = 1; k < DCTSIZE2; k++) {
511
0
    if ((temp = block[jpeg_natural_order[k]]) == 0) {
512
0
      r++;
513
0
    } else {
514
      /* if run length > 15, must emit special run-length-16 codes (0xF0) */
515
0
      while (r > 15) {
516
0
  ac_counts[0xF0]++;
517
0
  r -= 16;
518
0
      }
519
      
520
      /* Find the number of bits needed for the magnitude of the coefficient */
521
0
      if (temp < 0)
522
0
  temp = -temp;
523
      
524
      /* Find the number of bits needed for the magnitude of the coefficient */
525
0
      nbits = 1;    /* there must be at least one 1 bit */
526
0
      while ((temp >>= 1))
527
0
  nbits++;
528
      /* Check for out-of-range coefficient values */
529
0
      if (nbits > MAX_COEF_BITS)
530
0
  ERREXIT(cinfo, JERR_BAD_DCT_COEF);
531
      
532
      /* Count Huffman symbol for run length / number of bits */
533
0
      ac_counts[(r << 4) + nbits]++;
534
      
535
0
      r = 0;
536
0
    }
537
0
  }
538
539
  /* If the last coef(s) were zero, emit an end-of-block code */
540
0
  if (r > 0)
541
0
    ac_counts[0]++;
542
0
}
543
544
545
/*
546
 * Trial-encode one MCU's worth of Huffman-compressed coefficients.
547
 * No data is actually output, so no suspension return is possible.
548
 */
549
550
METHODDEF(boolean)
551
encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
552
0
{
553
0
  j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
554
0
  shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyc->entropy_private;
555
0
  int blkn, ci;
556
0
  jpeg_component_info * compptr;
557
558
  /* Take care of restart intervals if needed */
559
0
  if (cinfo->restart_interval) {
560
0
    if (entropy->restarts_to_go == 0) {
561
      /* Re-initialize DC predictions to 0 */
562
0
      for (ci = 0; ci < cinfo->comps_in_scan; ci++)
563
0
  entropy->saved.last_dc_val[ci] = 0;
564
      /* Update restart state */
565
0
      entropy->restarts_to_go = cinfo->restart_interval;
566
0
    }
567
0
    entropy->restarts_to_go--;
568
0
  }
569
570
0
  for (blkn = 0; blkn < cinfo->data_units_in_MCU; blkn++) {
571
0
    ci = cinfo->MCU_membership[blkn];
572
0
    compptr = cinfo->cur_comp_info[ci];
573
0
    htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
574
0
        entropy->dc_count_ptrs[compptr->dc_tbl_no],
575
0
        entropy->ac_count_ptrs[compptr->ac_tbl_no]);
576
0
    entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
577
0
  }
578
579
0
  return TRUE;
580
0
}
581
582
583
/*
584
 * Finish up a statistics-gathering pass and create the new Huffman tables.
585
 */
586
587
METHODDEF(void)
588
finish_pass_gather (j_compress_ptr cinfo)
589
0
{
590
0
  j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
591
0
  shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyc->entropy_private;
592
0
  int ci, dctbl, actbl;
593
0
  jpeg_component_info * compptr;
594
0
  JHUFF_TBL **htblptr;
595
0
  boolean did_dc[NUM_HUFF_TBLS];
596
0
  boolean did_ac[NUM_HUFF_TBLS];
597
598
  /* It's important not to apply jpeg_gen_optimal_table more than once
599
   * per table, because it clobbers the input frequency counts!
600
   */
601
0
  MEMZERO(did_dc, SIZEOF(did_dc));
602
0
  MEMZERO(did_ac, SIZEOF(did_ac));
603
604
0
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
605
0
    compptr = cinfo->cur_comp_info[ci];
606
0
    dctbl = compptr->dc_tbl_no;
607
0
    actbl = compptr->ac_tbl_no;
608
0
    if (! did_dc[dctbl]) {
609
0
      htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
610
0
      if (*htblptr == NULL)
611
0
  *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
612
0
      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
613
0
      did_dc[dctbl] = TRUE;
614
0
    }
615
0
    if (! did_ac[actbl]) {
616
0
      htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
617
0
      if (*htblptr == NULL)
618
0
  *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
619
0
      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
620
0
      did_ac[actbl] = TRUE;
621
0
    }
622
0
  }
623
0
}
624
625
626
#endif /* ENTROPY_OPT_SUPPORTED */
627
628
629
METHODDEF(boolean)
630
need_optimization_pass (j_compress_ptr cinfo)
631
0
{
632
0
  (void)cinfo;
633
0
  return TRUE;
634
0
}
635
636
637
/*
638
 * Module initialization routine for Huffman entropy encoding.
639
 */
640
641
GLOBAL(void)
642
jinit_shuff_encoder (j_compress_ptr cinfo)
643
0
{
644
0
  j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
645
0
  shuff_entropy_ptr entropy;
646
0
  int i;
647
648
0
  entropy = (shuff_entropy_ptr)
649
0
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
650
0
        SIZEOF(shuff_entropy_encoder));
651
0
  lossyc->entropy_private = (void *) entropy;
652
0
  lossyc->pub.entropy_start_pass = start_pass_huff;
653
0
  lossyc->pub.need_optimization_pass = need_optimization_pass;
654
655
  /* Mark tables unallocated */
656
0
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
657
0
    entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
658
0
#ifdef ENTROPY_OPT_SUPPORTED
659
    entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
660
0
#endif
661
0
  }
662
0
}