blob: 94a66c5a4c8907b46ca8e7a8a7ac7bd8f7c039ce [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jchuff.c
3 *
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00004 * Copyright (C) 1991-1997, Thomas G. Lane.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00005 * 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.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00009 *
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.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000015 */
16
DRC99313382009-03-12 17:24:27 +000017/* Modifications:
18 * Copyright (C)2007 Sun Microsystems, Inc.
19 * Copyright (C)2009 D. R. Commander
20 *
21 * This library is free software and may be redistributed and/or modified under
22 * the terms of the wxWindows Library License, Version 3.1 or (at your option)
23 * any later version. The full license is in the LICENSE.txt file included
24 * with this distribution.
25 *
26 * This library is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * wxWindows Library License for more details.
30 */
31
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000032#define JPEG_INTERNALS
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000033#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000034#include "jpeglib.h"
Thomas G. Lanebc79e061995-08-02 00:00:00 +000035#include "jchuff.h" /* Declarations shared with jcphuff.c */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000036
37
DRC99313382009-03-12 17:24:27 +000038static unsigned char jpeg_first_bit_table[65536];
39int jpeg_first_bit_table_init=0;
40
41#define CALC_FIRST_BIT(nbits, t) \
42 nbits = jpeg_first_bit_table[t&255]; \
43 if (t > 255) nbits = jpeg_first_bit_table[t>>8] + 8;
44
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000045/* Expanded entropy encoder object for Huffman encoding.
46 *
47 * The savable_state subrecord contains fields that change within an MCU,
48 * but must not be updated permanently until we complete the MCU.
49 */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000050
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000051typedef struct {
52 INT32 put_buffer; /* current bit-accumulation buffer */
53 int put_bits; /* # of bits now in it */
54 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
55} savable_state;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000056
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000057/* This macro is to work around compilers with missing or broken
58 * structure assignment. You'll need to fix this code if you have
59 * such a compiler and you change MAX_COMPS_IN_SCAN.
60 */
61
62#ifndef NO_STRUCT_ASSIGN
63#define ASSIGN_STATE(dest,src) ((dest) = (src))
64#else
65#if MAX_COMPS_IN_SCAN == 4
66#define ASSIGN_STATE(dest,src) \
67 ((dest).put_buffer = (src).put_buffer, \
68 (dest).put_bits = (src).put_bits, \
69 (dest).last_dc_val[0] = (src).last_dc_val[0], \
70 (dest).last_dc_val[1] = (src).last_dc_val[1], \
71 (dest).last_dc_val[2] = (src).last_dc_val[2], \
72 (dest).last_dc_val[3] = (src).last_dc_val[3])
73#endif
74#endif
75
76
77typedef struct {
78 struct jpeg_entropy_encoder pub; /* public fields */
79
80 savable_state saved; /* Bit buffer & DC state at start of MCU */
81
82 /* These fields are NOT loaded into local working state. */
83 unsigned int restarts_to_go; /* MCUs left in this restart interval */
84 int next_restart_num; /* next restart number to write (0-7) */
85
86 /* Pointers to derived tables (these workspaces have image lifespan) */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000087 c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
88 c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000089
90#ifdef ENTROPY_OPT_SUPPORTED /* Statistics tables for optimization */
91 long * dc_count_ptrs[NUM_HUFF_TBLS];
92 long * ac_count_ptrs[NUM_HUFF_TBLS];
93#endif
94} huff_entropy_encoder;
95
96typedef huff_entropy_encoder * huff_entropy_ptr;
97
98/* Working state while writing an MCU.
99 * This struct contains all the fields that are needed by subroutines.
100 */
101
102typedef struct {
103 JOCTET * next_output_byte; /* => next byte to write in buffer */
104 size_t free_in_buffer; /* # of byte spaces remaining in buffer */
105 savable_state cur; /* Current bit buffer & DC state */
106 j_compress_ptr cinfo; /* dump_buffer needs access to this */
107} working_state;
108
109
110/* Forward declarations */
Thomas G. Lane489583f1996-02-07 00:00:00 +0000111METHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo,
112 JBLOCKROW *MCU_data));
113METHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000114#ifdef ENTROPY_OPT_SUPPORTED
Thomas G. Lane489583f1996-02-07 00:00:00 +0000115METHODDEF(boolean) encode_mcu_gather JPP((j_compress_ptr cinfo,
116 JBLOCKROW *MCU_data));
117METHODDEF(void) finish_pass_gather JPP((j_compress_ptr cinfo));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000118#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000119
120
121/*
122 * Initialize for a Huffman-compressed scan.
123 * If gather_statistics is TRUE, we do not output anything during the scan,
124 * just count the Huffman symbols used and generate Huffman code tables.
125 */
126
Thomas G. Lane489583f1996-02-07 00:00:00 +0000127METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000128start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
129{
130 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
131 int ci, dctbl, actbl;
132 jpeg_component_info * compptr;
133
134 if (gather_statistics) {
135#ifdef ENTROPY_OPT_SUPPORTED
136 entropy->pub.encode_mcu = encode_mcu_gather;
137 entropy->pub.finish_pass = finish_pass_gather;
138#else
139 ERREXIT(cinfo, JERR_NOT_COMPILED);
140#endif
141 } else {
142 entropy->pub.encode_mcu = encode_mcu_huff;
143 entropy->pub.finish_pass = finish_pass_huff;
144 }
145
146 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
147 compptr = cinfo->cur_comp_info[ci];
148 dctbl = compptr->dc_tbl_no;
149 actbl = compptr->ac_tbl_no;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000150 if (gather_statistics) {
151#ifdef ENTROPY_OPT_SUPPORTED
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000152 /* Check for invalid table indexes */
153 /* (make_c_derived_tbl does this in the other path) */
154 if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
155 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
156 if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
157 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000158 /* Allocate and zero the statistics tables */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000159 /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000160 if (entropy->dc_count_ptrs[dctbl] == NULL)
161 entropy->dc_count_ptrs[dctbl] = (long *)
162 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
163 257 * SIZEOF(long));
164 MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
165 if (entropy->ac_count_ptrs[actbl] == NULL)
166 entropy->ac_count_ptrs[actbl] = (long *)
167 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
168 257 * SIZEOF(long));
169 MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
170#endif
171 } else {
172 /* Compute derived values for Huffman tables */
173 /* We may do this more than once for a table, but it's not expensive */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000174 jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000175 & entropy->dc_derived_tbls[dctbl]);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000176 jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000177 & entropy->ac_derived_tbls[actbl]);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000178 }
179 /* Initialize DC predictions to 0 */
180 entropy->saved.last_dc_val[ci] = 0;
181 }
182
183 /* Initialize bit buffer to empty */
184 entropy->saved.put_buffer = 0;
185 entropy->saved.put_bits = 0;
186
187 /* Initialize restart stuff */
188 entropy->restarts_to_go = cinfo->restart_interval;
189 entropy->next_restart_num = 0;
190}
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000191
192
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000193/*
194 * Compute the derived values for a Huffman table.
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000195 * This routine also performs some validation checks on the table.
196 *
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000197 * Note this is also used by jcphuff.c.
198 */
199
Thomas G. Lane489583f1996-02-07 00:00:00 +0000200GLOBAL(void)
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000201jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000202 c_derived_tbl ** pdtbl)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000203{
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000204 JHUFF_TBL *htbl;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000205 c_derived_tbl *dtbl;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000206 int p, i, l, lastp, si, maxsymbol;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000207 char huffsize[257];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000208 unsigned int huffcode[257];
209 unsigned int code;
210
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000211 /* Note that huffsize[] and huffcode[] are filled in code-length order,
212 * paralleling the order of the symbols themselves in htbl->huffval[].
213 */
214
215 /* Find the input Huffman table */
216 if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
217 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
218 htbl =
219 isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
220 if (htbl == NULL)
221 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
222
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000223 /* Allocate a workspace if we haven't already done so. */
224 if (*pdtbl == NULL)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000225 *pdtbl = (c_derived_tbl *)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000226 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000227 SIZEOF(c_derived_tbl));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000228 dtbl = *pdtbl;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000229
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000230 /* Figure C.1: make table of Huffman code length for each symbol */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000231
232 p = 0;
233 for (l = 1; l <= 16; l++) {
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000234 i = (int) htbl->bits[l];
235 if (i < 0 || p + i > 256) /* protect against table overrun */
236 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
237 while (i--)
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000238 huffsize[p++] = (char) l;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000239 }
240 huffsize[p] = 0;
241 lastp = p;
242
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000243 /* Figure C.2: generate the codes themselves */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000244 /* We also validate that the counts represent a legal Huffman code tree. */
245
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000246 code = 0;
247 si = huffsize[0];
248 p = 0;
249 while (huffsize[p]) {
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000250 while (((int) huffsize[p]) == si) {
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000251 huffcode[p++] = code;
252 code++;
253 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000254 /* code is now 1 more than the last code used for codelength si; but
255 * it must still fit in si bits, since no code is allowed to be all ones.
256 */
257 if (((INT32) code) >= (((INT32) 1) << si))
258 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000259 code <<= 1;
260 si++;
261 }
262
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000263 /* Figure C.3: generate encoding tables */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000264 /* These are code and size indexed by symbol value */
265
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000266 /* Set all codeless symbols to have code length 0;
267 * this lets us detect duplicate VAL entries here, and later
268 * allows emit_bits to detect any attempt to emit such symbols.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000269 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000270 MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000271
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000272 /* This is also a convenient place to check for out-of-range
273 * and duplicated VAL entries. We allow 0..255 for AC symbols
274 * but only 0..15 for DC. (We could constrain them further
275 * based on data depth and mode, but this seems enough.)
276 */
277 maxsymbol = isDC ? 15 : 255;
278
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000279 for (p = 0; p < lastp; p++) {
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000280 i = htbl->huffval[p];
281 if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
282 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
283 dtbl->ehufco[i] = huffcode[p];
284 dtbl->ehufsi[i] = huffsize[p];
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000285 }
DRC99313382009-03-12 17:24:27 +0000286
287 if(!jpeg_first_bit_table_init) {
288 for(i = 0; i < 65536; i++) {
289 int bit = 0, val = i;
290 while (val) {val >>= 1; bit++;}
291 jpeg_first_bit_table[i] = bit;
292 }
293 jpeg_first_bit_table_init = 1;
294 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000295}
296
297
298/* Outputting bytes to the file */
299
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000300/* Emit a byte, taking 'action' if must suspend. */
301#define emit_byte(state,val,action) \
302 { *(state)->next_output_byte++ = (JOCTET) (val); \
303 if (--(state)->free_in_buffer == 0) \
304 if (! dump_buffer(state)) \
305 { action; } }
306
307
Thomas G. Lane489583f1996-02-07 00:00:00 +0000308LOCAL(boolean)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000309dump_buffer (working_state * state)
310/* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000311{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000312 struct jpeg_destination_mgr * dest = state->cinfo->dest;
313
314 if (! (*dest->empty_output_buffer) (state->cinfo))
315 return FALSE;
316 /* After a successful buffer dump, must reset buffer pointers */
317 state->next_output_byte = dest->next_output_byte;
318 state->free_in_buffer = dest->free_in_buffer;
319 return TRUE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000320}
321
322
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000323/* Outputting bits to the file */
324
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000325/* Only the right 24 bits of put_buffer are used; the valid bits are
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000326 * left-justified in this part. At most 16 bits can be passed to emit_bits
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000327 * in one call, and we never retain more than 7 bits in put_buffer
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000328 * between calls, so 24 bits are sufficient.
329 */
330
DRC99313382009-03-12 17:24:27 +0000331/***************************************************************/
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000332
DRC99313382009-03-12 17:24:27 +0000333#define DUMP_BITS_(code, size) { \
334 put_bits += size; \
335 put_buffer = (put_buffer << size) | code; \
336 if (put_bits > 7) \
337 while(put_bits > 7) \
338 if (0xFF == (*buffer++ = put_buffer >> (put_bits -= 8))) \
339 *buffer++ = 0; \
340 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000341
DRC99313382009-03-12 17:24:27 +0000342/***************************************************************/
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000343
DRC99313382009-03-12 17:24:27 +0000344#define DUMP_BITS(code, size) { \
345 put_bits += size; \
346 put_buffer = (put_buffer << size) | code; \
347 if (put_bits > 15) { \
348 if (0xFF == (*buffer++ = put_buffer >> (put_bits -= 8))) \
349 *buffer++ = 0; \
350 if (0xFF == (*buffer++ = put_buffer >> (put_bits -= 8))) \
351 *buffer++ = 0; \
352 } \
353 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000354
DRC99313382009-03-12 17:24:27 +0000355/***************************************************************/
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000356
DRC99313382009-03-12 17:24:27 +0000357#define DUMP_SINGLE_VALUE(ht, codevalue) { \
358 size = ht->ehufsi[codevalue]; \
359 code = ht->ehufco[codevalue]; \
360 \
361 DUMP_BITS(code, size) \
362 }
363
364/***************************************************************/
365
366#define DUMP_VALUE(ht, codevalue, t, nbits) { \
367 size = ht->ehufsi[codevalue]; \
368 code = ht->ehufco[codevalue]; \
369 t &= ~(-1 << nbits); \
370 DUMP_BITS(code, size) \
371 DUMP_BITS(t, nbits) \
372 }
373
374/***************************************************************/
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000375
376
Thomas G. Lane489583f1996-02-07 00:00:00 +0000377LOCAL(boolean)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000378flush_bits (working_state * state)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000379{
DRC99313382009-03-12 17:24:27 +0000380 unsigned char *buffer;
381 int put_buffer, put_bits;
382
383 if ((state)->free_in_buffer < DCTSIZE2 * 2)
384 if (! dump_buffer(state)) return FALSE;
385
386 buffer = state->next_output_byte;
387 put_buffer = state->cur.put_buffer;
388 put_bits = state->cur.put_bits;
389
390 DUMP_BITS_(0x7F, 7)
391
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000392 state->cur.put_buffer = 0; /* and reset bit-buffer to empty */
393 state->cur.put_bits = 0;
DRC99313382009-03-12 17:24:27 +0000394 state->free_in_buffer -= (buffer - state->next_output_byte);
395 state->next_output_byte = buffer;
396
397 if ((state)->free_in_buffer < DCTSIZE2 * 2)
398 if (! dump_buffer(state)) return FALSE;
399
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000400 return TRUE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000401}
402
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000403/* Encode a single block's worth of coefficients */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000404
Thomas G. Lane489583f1996-02-07 00:00:00 +0000405LOCAL(boolean)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000406encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000407 c_derived_tbl *dctbl, c_derived_tbl *actbl)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000408{
DRC99313382009-03-12 17:24:27 +0000409 int temp, temp2;
410 int nbits;
411 int r, sflag, size, code;
412 unsigned char *buffer;
413 int put_buffer, put_bits;
414 int code_0xf0 = actbl->ehufco[0xf0], size_0xf0 = actbl->ehufsi[0xf0];
415
416 if ((state)->free_in_buffer < DCTSIZE2 * 2)
417 if (! dump_buffer(state)) return FALSE;
418
419 buffer = state->next_output_byte;
420 put_buffer = state->cur.put_buffer;
421 put_bits = state->cur.put_bits;
422
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000423 /* Encode the DC coefficient difference per section F.1.2.1 */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000424
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000425 temp = temp2 = block[0] - last_dc_val;
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000426
DRC99313382009-03-12 17:24:27 +0000427 sflag = temp >> 31;
428 temp -= ((temp + temp) & sflag);
429 temp2 += sflag;
430 CALC_FIRST_BIT(nbits, temp)
431 DUMP_VALUE(dctbl, nbits, temp2, nbits)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000432
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000433 /* Encode the AC coefficients per section F.1.2.2 */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000434
435 r = 0; /* r = run length of zeros */
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000436
DRC99313382009-03-12 17:24:27 +0000437#define innerloop(order) { \
438 temp2 = *(JCOEF*)((unsigned char*)block + order); \
439 if(temp2 == 0) r++; \
440 else { \
441 temp = (JCOEF)temp2; \
442 sflag = temp >> 31; \
443 temp = (temp ^ sflag) - sflag; \
444 temp2 += sflag; \
445 nbits = jpeg_first_bit_table[temp]; \
446 for(; r > 15; r -= 16) DUMP_BITS(code_0xf0, size_0xf0) \
447 sflag = (r << 4) + nbits; \
448 DUMP_VALUE(actbl, sflag, temp2, nbits) \
449 r = 0; \
450 }}
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000451
DRC99313382009-03-12 17:24:27 +0000452 innerloop(2*1); innerloop(2*8); innerloop(2*16); innerloop(2*9);
453 innerloop(2*2); innerloop(2*3); innerloop(2*10); innerloop(2*17);
454 innerloop(2*24); innerloop(2*32); innerloop(2*25); innerloop(2*18);
455 innerloop(2*11); innerloop(2*4); innerloop(2*5); innerloop(2*12);
456 innerloop(2*19); innerloop(2*26); innerloop(2*33); innerloop(2*40);
457 innerloop(2*48); innerloop(2*41); innerloop(2*34); innerloop(2*27);
458 innerloop(2*20); innerloop(2*13); innerloop(2*6); innerloop(2*7);
459 innerloop(2*14); innerloop(2*21); innerloop(2*28); innerloop(2*35);
460 innerloop(2*42); innerloop(2*49); innerloop(2*56); innerloop(2*57);
461 innerloop(2*50); innerloop(2*43); innerloop(2*36); innerloop(2*29);
462 innerloop(2*22); innerloop(2*15); innerloop(2*23); innerloop(2*30);
463 innerloop(2*37); innerloop(2*44); innerloop(2*51); innerloop(2*58);
464 innerloop(2*59); innerloop(2*52); innerloop(2*45); innerloop(2*38);
465 innerloop(2*31); innerloop(2*39); innerloop(2*46); innerloop(2*53);
466 innerloop(2*60); innerloop(2*61); innerloop(2*54); innerloop(2*47);
467 innerloop(2*55); innerloop(2*62); innerloop(2*63);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000468
469 /* If the last coef(s) were zero, emit an end-of-block code */
DRC99313382009-03-12 17:24:27 +0000470 if (r > 0) DUMP_SINGLE_VALUE(actbl, 0x0)
471
472 state->cur.put_buffer = put_buffer;
473 state->cur.put_bits = put_bits;
474 state->free_in_buffer -= (buffer - state->next_output_byte);
475 state->next_output_byte = buffer;
476
477 if ((state)->free_in_buffer < DCTSIZE2 * 2)
478 if (! dump_buffer(state)) return FALSE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000479
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000480 return TRUE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000481}
482
483
484/*
485 * Emit a restart marker & resynchronize predictions.
486 */
487
Thomas G. Lane489583f1996-02-07 00:00:00 +0000488LOCAL(boolean)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000489emit_restart (working_state * state, int restart_num)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000490{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000491 int ci;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000492
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000493 if (! flush_bits(state))
494 return FALSE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000495
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000496 emit_byte(state, 0xFF, return FALSE);
497 emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000498
499 /* Re-initialize DC predictions to 0 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000500 for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
501 state->cur.last_dc_val[ci] = 0;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000502
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000503 /* The restart counter is not updated until we successfully write the MCU. */
504
505 return TRUE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000506}
507
508
509/*
510 * Encode and output one MCU's worth of Huffman-compressed coefficients.
511 */
512
Thomas G. Lane489583f1996-02-07 00:00:00 +0000513METHODDEF(boolean)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000514encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000515{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000516 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
517 working_state state;
518 int blkn, ci;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000519 jpeg_component_info * compptr;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000520
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000521 /* Load up working state */
522 state.next_output_byte = cinfo->dest->next_output_byte;
523 state.free_in_buffer = cinfo->dest->free_in_buffer;
524 ASSIGN_STATE(state.cur, entropy->saved);
525 state.cinfo = cinfo;
526
527 /* Emit restart marker if needed */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000528 if (cinfo->restart_interval) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000529 if (entropy->restarts_to_go == 0)
530 if (! emit_restart(&state, entropy->next_restart_num))
531 return FALSE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000532 }
533
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000534 /* Encode the MCU data blocks */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000535 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
536 ci = cinfo->MCU_membership[blkn];
537 compptr = cinfo->cur_comp_info[ci];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000538 if (! encode_one_block(&state,
539 MCU_data[blkn][0], state.cur.last_dc_val[ci],
540 entropy->dc_derived_tbls[compptr->dc_tbl_no],
541 entropy->ac_derived_tbls[compptr->ac_tbl_no]))
542 return FALSE;
543 /* Update last_dc_val */
544 state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000545 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000546
547 /* Completed MCU, so update state */
548 cinfo->dest->next_output_byte = state.next_output_byte;
549 cinfo->dest->free_in_buffer = state.free_in_buffer;
550 ASSIGN_STATE(entropy->saved, state.cur);
551
552 /* Update restart-interval state too */
553 if (cinfo->restart_interval) {
554 if (entropy->restarts_to_go == 0) {
555 entropy->restarts_to_go = cinfo->restart_interval;
556 entropy->next_restart_num++;
557 entropy->next_restart_num &= 7;
558 }
559 entropy->restarts_to_go--;
560 }
561
562 return TRUE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000563}
564
565
566/*
567 * Finish up at the end of a Huffman-compressed scan.
568 */
569
Thomas G. Lane489583f1996-02-07 00:00:00 +0000570METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000571finish_pass_huff (j_compress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000572{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000573 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
574 working_state state;
575
576 /* Load up working state ... flush_bits needs it */
577 state.next_output_byte = cinfo->dest->next_output_byte;
578 state.free_in_buffer = cinfo->dest->free_in_buffer;
579 ASSIGN_STATE(state.cur, entropy->saved);
580 state.cinfo = cinfo;
581
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000582 /* Flush out the last data */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000583 if (! flush_bits(&state))
584 ERREXIT(cinfo, JERR_CANT_SUSPEND);
585
586 /* Update state */
587 cinfo->dest->next_output_byte = state.next_output_byte;
588 cinfo->dest->free_in_buffer = state.free_in_buffer;
589 ASSIGN_STATE(entropy->saved, state.cur);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000590}
591
592
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000593/*
594 * Huffman coding optimization.
595 *
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000596 * We first scan the supplied data and count the number of uses of each symbol
597 * that is to be Huffman-coded. (This process MUST agree with the code above.)
598 * Then we build a Huffman coding tree for the observed counts.
599 * Symbols which are not needed at all for the particular image are not
600 * assigned any code, which saves space in the DHT marker as well as in
601 * the compressed data.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000602 */
603
604#ifdef ENTROPY_OPT_SUPPORTED
605
606
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000607/* Process a single block's worth of coefficients */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000608
Thomas G. Lane489583f1996-02-07 00:00:00 +0000609LOCAL(void)
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000610htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000611 long dc_counts[], long ac_counts[])
612{
613 register int temp;
614 register int nbits;
615 register int k, r;
616
617 /* Encode the DC coefficient difference per section F.1.2.1 */
618
619 temp = block[0] - last_dc_val;
620 if (temp < 0)
621 temp = -temp;
622
623 /* Find the number of bits needed for the magnitude of the coefficient */
624 nbits = 0;
625 while (temp) {
626 nbits++;
627 temp >>= 1;
628 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000629 /* Check for out-of-range coefficient values.
630 * Since we're encoding a difference, the range limit is twice as much.
631 */
632 if (nbits > MAX_COEF_BITS+1)
633 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000634
635 /* Count the Huffman symbol for the number of bits */
636 dc_counts[nbits]++;
637
638 /* Encode the AC coefficients per section F.1.2.2 */
639
640 r = 0; /* r = run length of zeros */
641
642 for (k = 1; k < DCTSIZE2; k++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000643 if ((temp = block[jpeg_natural_order[k]]) == 0) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000644 r++;
645 } else {
646 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
647 while (r > 15) {
648 ac_counts[0xF0]++;
649 r -= 16;
650 }
651
652 /* Find the number of bits needed for the magnitude of the coefficient */
653 if (temp < 0)
654 temp = -temp;
655
656 /* Find the number of bits needed for the magnitude of the coefficient */
657 nbits = 1; /* there must be at least one 1 bit */
658 while ((temp >>= 1))
659 nbits++;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000660 /* Check for out-of-range coefficient values */
661 if (nbits > MAX_COEF_BITS)
662 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000663
664 /* Count Huffman symbol for run length / number of bits */
665 ac_counts[(r << 4) + nbits]++;
666
667 r = 0;
668 }
669 }
670
671 /* If the last coef(s) were zero, emit an end-of-block code */
672 if (r > 0)
673 ac_counts[0]++;
674}
675
676
677/*
678 * Trial-encode one MCU's worth of Huffman-compressed coefficients.
679 * No data is actually output, so no suspension return is possible.
680 */
681
Thomas G. Lane489583f1996-02-07 00:00:00 +0000682METHODDEF(boolean)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000683encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
684{
685 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
686 int blkn, ci;
687 jpeg_component_info * compptr;
688
689 /* Take care of restart intervals if needed */
690 if (cinfo->restart_interval) {
691 if (entropy->restarts_to_go == 0) {
692 /* Re-initialize DC predictions to 0 */
693 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
694 entropy->saved.last_dc_val[ci] = 0;
695 /* Update restart state */
696 entropy->restarts_to_go = cinfo->restart_interval;
697 }
698 entropy->restarts_to_go--;
699 }
700
701 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
702 ci = cinfo->MCU_membership[blkn];
703 compptr = cinfo->cur_comp_info[ci];
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000704 htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000705 entropy->dc_count_ptrs[compptr->dc_tbl_no],
706 entropy->ac_count_ptrs[compptr->ac_tbl_no]);
707 entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
708 }
709
710 return TRUE;
711}
712
713
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000714/*
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000715 * Generate the best Huffman code table for the given counts, fill htbl.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000716 * Note this is also used by jcphuff.c.
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000717 *
718 * The JPEG standard requires that no symbol be assigned a codeword of all
719 * one bits (so that padding bits added at the end of a compressed segment
720 * can't look like a valid code). Because of the canonical ordering of
721 * codewords, this just means that there must be an unused slot in the
722 * longest codeword length category. Section K.2 of the JPEG spec suggests
723 * reserving such a slot by pretending that symbol 256 is a valid symbol
724 * with count 1. In theory that's not optimal; giving it count zero but
725 * including it in the symbol set anyway should give a better Huffman code.
726 * But the theoretically better code actually seems to come out worse in
727 * practice, because it produces more all-ones bytes (which incur stuffed
728 * zero bytes in the final file). In any case the difference is tiny.
729 *
730 * The JPEG standard requires Huffman codes to be no more than 16 bits long.
731 * If some symbols have a very small but nonzero probability, the Huffman tree
732 * must be adjusted to meet the code length restriction. We currently use
733 * the adjustment method suggested in JPEG section K.2. This method is *not*
734 * optimal; it may not choose the best possible limited-length code. But
735 * typically only very-low-frequency symbols will be given less-than-optimal
736 * lengths, so the code is almost optimal. Experimental comparisons against
737 * an optimal limited-length-code algorithm indicate that the difference is
738 * microscopic --- usually less than a hundredth of a percent of total size.
739 * So the extra complexity of an optimal algorithm doesn't seem worthwhile.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000740 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000741
Thomas G. Lane489583f1996-02-07 00:00:00 +0000742GLOBAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000743jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000744{
745#define MAX_CLEN 32 /* assumed maximum initial code length */
746 UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000747 int codesize[257]; /* codesize[k] = code length of symbol k */
748 int others[257]; /* next symbol in current branch of tree */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000749 int c1, c2;
750 int p, i, j;
751 long v;
752
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000753 /* This algorithm is explained in section K.2 of the JPEG standard */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000754
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000755 MEMZERO(bits, SIZEOF(bits));
756 MEMZERO(codesize, SIZEOF(codesize));
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000757 for (i = 0; i < 257; i++)
758 others[i] = -1; /* init links to empty */
759
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000760 freq[256] = 1; /* make sure 256 has a nonzero count */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000761 /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000762 * that no real symbol is given code-value of all ones, because 256
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000763 * will be placed last in the largest codeword category.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000764 */
765
766 /* Huffman's basic algorithm to assign optimal code lengths to symbols */
767
768 for (;;) {
769 /* Find the smallest nonzero frequency, set c1 = its symbol */
770 /* In case of ties, take the larger symbol number */
771 c1 = -1;
772 v = 1000000000L;
773 for (i = 0; i <= 256; i++) {
774 if (freq[i] && freq[i] <= v) {
775 v = freq[i];
776 c1 = i;
777 }
778 }
779
780 /* Find the next smallest nonzero frequency, set c2 = its symbol */
781 /* In case of ties, take the larger symbol number */
782 c2 = -1;
783 v = 1000000000L;
784 for (i = 0; i <= 256; i++) {
785 if (freq[i] && freq[i] <= v && i != c1) {
786 v = freq[i];
787 c2 = i;
788 }
789 }
790
791 /* Done if we've merged everything into one frequency */
792 if (c2 < 0)
793 break;
794
795 /* Else merge the two counts/trees */
796 freq[c1] += freq[c2];
797 freq[c2] = 0;
798
799 /* Increment the codesize of everything in c1's tree branch */
800 codesize[c1]++;
801 while (others[c1] >= 0) {
802 c1 = others[c1];
803 codesize[c1]++;
804 }
805
806 others[c1] = c2; /* chain c2 onto c1's tree branch */
807
808 /* Increment the codesize of everything in c2's tree branch */
809 codesize[c2]++;
810 while (others[c2] >= 0) {
811 c2 = others[c2];
812 codesize[c2]++;
813 }
814 }
815
816 /* Now count the number of symbols of each code length */
817 for (i = 0; i <= 256; i++) {
818 if (codesize[i]) {
819 /* The JPEG standard seems to think that this can't happen, */
820 /* but I'm paranoid... */
821 if (codesize[i] > MAX_CLEN)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000822 ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000823
824 bits[codesize[i]]++;
825 }
826 }
827
828 /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
829 * Huffman procedure assigned any such lengths, we must adjust the coding.
830 * Here is what the JPEG spec says about how this next bit works:
831 * Since symbols are paired for the longest Huffman code, the symbols are
832 * removed from this length category two at a time. The prefix for the pair
833 * (which is one bit shorter) is allocated to one of the pair; then,
834 * skipping the BITS entry for that prefix length, a code word from the next
835 * shortest nonzero BITS entry is converted into a prefix for two code words
836 * one bit longer.
837 */
838
839 for (i = MAX_CLEN; i > 16; i--) {
840 while (bits[i] > 0) {
841 j = i - 2; /* find length of new prefix to be used */
842 while (bits[j] == 0)
843 j--;
844
845 bits[i] -= 2; /* remove two symbols */
846 bits[i-1]++; /* one goes in this length */
847 bits[j+1] += 2; /* two new symbols in this length */
848 bits[j]--; /* symbol of this length is now a prefix */
849 }
850 }
851
852 /* Remove the count for the pseudo-symbol 256 from the largest codelength */
853 while (bits[i] == 0) /* find largest codelength still in use */
854 i--;
855 bits[i]--;
856
857 /* Return final symbol counts (only for lengths 0..16) */
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000858 MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000859
860 /* Return a list of the symbols sorted by code length */
861 /* It's not real clear to me why we don't need to consider the codelength
862 * changes made above, but the JPEG spec seems to think this works.
863 */
864 p = 0;
865 for (i = 1; i <= MAX_CLEN; i++) {
866 for (j = 0; j <= 255; j++) {
867 if (codesize[j] == i) {
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000868 htbl->huffval[p] = (UINT8) j;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000869 p++;
870 }
871 }
872 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000873
874 /* Set sent_table FALSE so updated table will be written to JPEG file. */
875 htbl->sent_table = FALSE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000876}
877
878
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000879/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000880 * Finish up a statistics-gathering pass and create the new Huffman tables.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000881 */
882
Thomas G. Lane489583f1996-02-07 00:00:00 +0000883METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000884finish_pass_gather (j_compress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000885{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000886 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
887 int ci, dctbl, actbl;
888 jpeg_component_info * compptr;
889 JHUFF_TBL **htblptr;
890 boolean did_dc[NUM_HUFF_TBLS];
891 boolean did_ac[NUM_HUFF_TBLS];
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000892
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000893 /* It's important not to apply jpeg_gen_optimal_table more than once
894 * per table, because it clobbers the input frequency counts!
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000895 */
896 MEMZERO(did_dc, SIZEOF(did_dc));
897 MEMZERO(did_ac, SIZEOF(did_ac));
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000898
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000899 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
900 compptr = cinfo->cur_comp_info[ci];
901 dctbl = compptr->dc_tbl_no;
902 actbl = compptr->ac_tbl_no;
903 if (! did_dc[dctbl]) {
904 htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000905 if (*htblptr == NULL)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000906 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000907 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000908 did_dc[dctbl] = TRUE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000909 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000910 if (! did_ac[actbl]) {
911 htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000912 if (*htblptr == NULL)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000913 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000914 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000915 did_ac[actbl] = TRUE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000916 }
917 }
918}
919
920
921#endif /* ENTROPY_OPT_SUPPORTED */
922
923
924/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000925 * Module initialization routine for Huffman entropy encoding.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000926 */
927
Thomas G. Lane489583f1996-02-07 00:00:00 +0000928GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000929jinit_huff_encoder (j_compress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000930{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000931 huff_entropy_ptr entropy;
932 int i;
933
934 entropy = (huff_entropy_ptr)
935 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
936 SIZEOF(huff_entropy_encoder));
937 cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
938 entropy->pub.start_pass = start_pass_huff;
939
940 /* Mark tables unallocated */
941 for (i = 0; i < NUM_HUFF_TBLS; i++) {
942 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000943#ifdef ENTROPY_OPT_SUPPORTED
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000944 entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000945#endif
946 }
947}