istereo2

annotate libs/libjpeg/jchuff.c @ 9:64e15874f3bd

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 26 Sep 2015 02:56:07 +0300
parents
children
rev   line source
nuclear@2 1 /*
nuclear@2 2 * jchuff.c
nuclear@2 3 *
nuclear@2 4 * Copyright (C) 1991-1997, Thomas G. Lane.
nuclear@2 5 * This file is part of the Independent JPEG Group's software.
nuclear@2 6 * For conditions of distribution and use, see the accompanying README file.
nuclear@2 7 *
nuclear@2 8 * This file contains Huffman entropy encoding routines.
nuclear@2 9 *
nuclear@2 10 * Much of the complexity here has to do with supporting output suspension.
nuclear@2 11 * If the data destination module demands suspension, we want to be able to
nuclear@2 12 * back up to the start of the current MCU. To do this, we copy state
nuclear@2 13 * variables into local working storage, and update them back to the
nuclear@2 14 * permanent JPEG objects only upon successful completion of an MCU.
nuclear@2 15 */
nuclear@2 16
nuclear@2 17 #define JPEG_INTERNALS
nuclear@2 18 #include "jinclude.h"
nuclear@2 19 #include "jpeglib.h"
nuclear@2 20 #include "jchuff.h" /* Declarations shared with jcphuff.c */
nuclear@2 21
nuclear@2 22
nuclear@2 23 /* Expanded entropy encoder object for Huffman encoding.
nuclear@2 24 *
nuclear@2 25 * The savable_state subrecord contains fields that change within an MCU,
nuclear@2 26 * but must not be updated permanently until we complete the MCU.
nuclear@2 27 */
nuclear@2 28
nuclear@2 29 typedef struct {
nuclear@2 30 INT32 put_buffer; /* current bit-accumulation buffer */
nuclear@2 31 int put_bits; /* # of bits now in it */
nuclear@2 32 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
nuclear@2 33 } savable_state;
nuclear@2 34
nuclear@2 35 /* This macro is to work around compilers with missing or broken
nuclear@2 36 * structure assignment. You'll need to fix this code if you have
nuclear@2 37 * such a compiler and you change MAX_COMPS_IN_SCAN.
nuclear@2 38 */
nuclear@2 39
nuclear@2 40 #ifndef NO_STRUCT_ASSIGN
nuclear@2 41 #define ASSIGN_STATE(dest,src) ((dest) = (src))
nuclear@2 42 #else
nuclear@2 43 #if MAX_COMPS_IN_SCAN == 4
nuclear@2 44 #define ASSIGN_STATE(dest,src) \
nuclear@2 45 ((dest).put_buffer = (src).put_buffer, \
nuclear@2 46 (dest).put_bits = (src).put_bits, \
nuclear@2 47 (dest).last_dc_val[0] = (src).last_dc_val[0], \
nuclear@2 48 (dest).last_dc_val[1] = (src).last_dc_val[1], \
nuclear@2 49 (dest).last_dc_val[2] = (src).last_dc_val[2], \
nuclear@2 50 (dest).last_dc_val[3] = (src).last_dc_val[3])
nuclear@2 51 #endif
nuclear@2 52 #endif
nuclear@2 53
nuclear@2 54
nuclear@2 55 typedef struct {
nuclear@2 56 struct jpeg_entropy_encoder pub; /* public fields */
nuclear@2 57
nuclear@2 58 savable_state saved; /* Bit buffer & DC state at start of MCU */
nuclear@2 59
nuclear@2 60 /* These fields are NOT loaded into local working state. */
nuclear@2 61 unsigned int restarts_to_go; /* MCUs left in this restart interval */
nuclear@2 62 int next_restart_num; /* next restart number to write (0-7) */
nuclear@2 63
nuclear@2 64 /* Pointers to derived tables (these workspaces have image lifespan) */
nuclear@2 65 c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
nuclear@2 66 c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
nuclear@2 67
nuclear@2 68 #ifdef ENTROPY_OPT_SUPPORTED /* Statistics tables for optimization */
nuclear@2 69 long * dc_count_ptrs[NUM_HUFF_TBLS];
nuclear@2 70 long * ac_count_ptrs[NUM_HUFF_TBLS];
nuclear@2 71 #endif
nuclear@2 72 } huff_entropy_encoder;
nuclear@2 73
nuclear@2 74 typedef huff_entropy_encoder * huff_entropy_ptr;
nuclear@2 75
nuclear@2 76 /* Working state while writing an MCU.
nuclear@2 77 * This struct contains all the fields that are needed by subroutines.
nuclear@2 78 */
nuclear@2 79
nuclear@2 80 typedef struct {
nuclear@2 81 JOCTET * next_output_byte; /* => next byte to write in buffer */
nuclear@2 82 size_t free_in_buffer; /* # of byte spaces remaining in buffer */
nuclear@2 83 savable_state cur; /* Current bit buffer & DC state */
nuclear@2 84 j_compress_ptr cinfo; /* dump_buffer needs access to this */
nuclear@2 85 } working_state;
nuclear@2 86
nuclear@2 87
nuclear@2 88 /* Forward declarations */
nuclear@2 89 METHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo,
nuclear@2 90 JBLOCKROW *MCU_data));
nuclear@2 91 METHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo));
nuclear@2 92 #ifdef ENTROPY_OPT_SUPPORTED
nuclear@2 93 METHODDEF(boolean) encode_mcu_gather JPP((j_compress_ptr cinfo,
nuclear@2 94 JBLOCKROW *MCU_data));
nuclear@2 95 METHODDEF(void) finish_pass_gather JPP((j_compress_ptr cinfo));
nuclear@2 96 #endif
nuclear@2 97
nuclear@2 98
nuclear@2 99 /*
nuclear@2 100 * Initialize for a Huffman-compressed scan.
nuclear@2 101 * If gather_statistics is TRUE, we do not output anything during the scan,
nuclear@2 102 * just count the Huffman symbols used and generate Huffman code tables.
nuclear@2 103 */
nuclear@2 104
nuclear@2 105 METHODDEF(void)
nuclear@2 106 start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
nuclear@2 107 {
nuclear@2 108 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
nuclear@2 109 int ci, dctbl, actbl;
nuclear@2 110 jpeg_component_info * compptr;
nuclear@2 111
nuclear@2 112 if (gather_statistics) {
nuclear@2 113 #ifdef ENTROPY_OPT_SUPPORTED
nuclear@2 114 entropy->pub.encode_mcu = encode_mcu_gather;
nuclear@2 115 entropy->pub.finish_pass = finish_pass_gather;
nuclear@2 116 #else
nuclear@2 117 ERREXIT(cinfo, JERR_NOT_COMPILED);
nuclear@2 118 #endif
nuclear@2 119 } else {
nuclear@2 120 entropy->pub.encode_mcu = encode_mcu_huff;
nuclear@2 121 entropy->pub.finish_pass = finish_pass_huff;
nuclear@2 122 }
nuclear@2 123
nuclear@2 124 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
nuclear@2 125 compptr = cinfo->cur_comp_info[ci];
nuclear@2 126 dctbl = compptr->dc_tbl_no;
nuclear@2 127 actbl = compptr->ac_tbl_no;
nuclear@2 128 if (gather_statistics) {
nuclear@2 129 #ifdef ENTROPY_OPT_SUPPORTED
nuclear@2 130 /* Check for invalid table indexes */
nuclear@2 131 /* (make_c_derived_tbl does this in the other path) */
nuclear@2 132 if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
nuclear@2 133 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
nuclear@2 134 if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
nuclear@2 135 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
nuclear@2 136 /* Allocate and zero the statistics tables */
nuclear@2 137 /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
nuclear@2 138 if (entropy->dc_count_ptrs[dctbl] == NULL)
nuclear@2 139 entropy->dc_count_ptrs[dctbl] = (long *)
nuclear@2 140 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
nuclear@2 141 257 * SIZEOF(long));
nuclear@2 142 MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
nuclear@2 143 if (entropy->ac_count_ptrs[actbl] == NULL)
nuclear@2 144 entropy->ac_count_ptrs[actbl] = (long *)
nuclear@2 145 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
nuclear@2 146 257 * SIZEOF(long));
nuclear@2 147 MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
nuclear@2 148 #endif
nuclear@2 149 } else {
nuclear@2 150 /* Compute derived values for Huffman tables */
nuclear@2 151 /* We may do this more than once for a table, but it's not expensive */
nuclear@2 152 jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
nuclear@2 153 & entropy->dc_derived_tbls[dctbl]);
nuclear@2 154 jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
nuclear@2 155 & entropy->ac_derived_tbls[actbl]);
nuclear@2 156 }
nuclear@2 157 /* Initialize DC predictions to 0 */
nuclear@2 158 entropy->saved.last_dc_val[ci] = 0;
nuclear@2 159 }
nuclear@2 160
nuclear@2 161 /* Initialize bit buffer to empty */
nuclear@2 162 entropy->saved.put_buffer = 0;
nuclear@2 163 entropy->saved.put_bits = 0;
nuclear@2 164
nuclear@2 165 /* Initialize restart stuff */
nuclear@2 166 entropy->restarts_to_go = cinfo->restart_interval;
nuclear@2 167 entropy->next_restart_num = 0;
nuclear@2 168 }
nuclear@2 169
nuclear@2 170
nuclear@2 171 /*
nuclear@2 172 * Compute the derived values for a Huffman table.
nuclear@2 173 * This routine also performs some validation checks on the table.
nuclear@2 174 *
nuclear@2 175 * Note this is also used by jcphuff.c.
nuclear@2 176 */
nuclear@2 177
nuclear@2 178 GLOBAL(void)
nuclear@2 179 jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
nuclear@2 180 c_derived_tbl ** pdtbl)
nuclear@2 181 {
nuclear@2 182 JHUFF_TBL *htbl;
nuclear@2 183 c_derived_tbl *dtbl;
nuclear@2 184 int p, i, l, lastp, si, maxsymbol;
nuclear@2 185 char huffsize[257];
nuclear@2 186 unsigned int huffcode[257];
nuclear@2 187 unsigned int code;
nuclear@2 188
nuclear@2 189 /* Note that huffsize[] and huffcode[] are filled in code-length order,
nuclear@2 190 * paralleling the order of the symbols themselves in htbl->huffval[].
nuclear@2 191 */
nuclear@2 192
nuclear@2 193 /* Find the input Huffman table */
nuclear@2 194 if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
nuclear@2 195 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
nuclear@2 196 htbl =
nuclear@2 197 isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
nuclear@2 198 if (htbl == NULL)
nuclear@2 199 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
nuclear@2 200
nuclear@2 201 /* Allocate a workspace if we haven't already done so. */
nuclear@2 202 if (*pdtbl == NULL)
nuclear@2 203 *pdtbl = (c_derived_tbl *)
nuclear@2 204 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
nuclear@2 205 SIZEOF(c_derived_tbl));
nuclear@2 206 dtbl = *pdtbl;
nuclear@2 207
nuclear@2 208 /* Figure C.1: make table of Huffman code length for each symbol */
nuclear@2 209
nuclear@2 210 p = 0;
nuclear@2 211 for (l = 1; l <= 16; l++) {
nuclear@2 212 i = (int) htbl->bits[l];
nuclear@2 213 if (i < 0 || p + i > 256) /* protect against table overrun */
nuclear@2 214 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
nuclear@2 215 while (i--)
nuclear@2 216 huffsize[p++] = (char) l;
nuclear@2 217 }
nuclear@2 218 huffsize[p] = 0;
nuclear@2 219 lastp = p;
nuclear@2 220
nuclear@2 221 /* Figure C.2: generate the codes themselves */
nuclear@2 222 /* We also validate that the counts represent a legal Huffman code tree. */
nuclear@2 223
nuclear@2 224 code = 0;
nuclear@2 225 si = huffsize[0];
nuclear@2 226 p = 0;
nuclear@2 227 while (huffsize[p]) {
nuclear@2 228 while (((int) huffsize[p]) == si) {
nuclear@2 229 huffcode[p++] = code;
nuclear@2 230 code++;
nuclear@2 231 }
nuclear@2 232 /* code is now 1 more than the last code used for codelength si; but
nuclear@2 233 * it must still fit in si bits, since no code is allowed to be all ones.
nuclear@2 234 */
nuclear@2 235 if (((INT32) code) >= (((INT32) 1) << si))
nuclear@2 236 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
nuclear@2 237 code <<= 1;
nuclear@2 238 si++;
nuclear@2 239 }
nuclear@2 240
nuclear@2 241 /* Figure C.3: generate encoding tables */
nuclear@2 242 /* These are code and size indexed by symbol value */
nuclear@2 243
nuclear@2 244 /* Set all codeless symbols to have code length 0;
nuclear@2 245 * this lets us detect duplicate VAL entries here, and later
nuclear@2 246 * allows emit_bits to detect any attempt to emit such symbols.
nuclear@2 247 */
nuclear@2 248 MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
nuclear@2 249
nuclear@2 250 /* This is also a convenient place to check for out-of-range
nuclear@2 251 * and duplicated VAL entries. We allow 0..255 for AC symbols
nuclear@2 252 * but only 0..15 for DC. (We could constrain them further
nuclear@2 253 * based on data depth and mode, but this seems enough.)
nuclear@2 254 */
nuclear@2 255 maxsymbol = isDC ? 15 : 255;
nuclear@2 256
nuclear@2 257 for (p = 0; p < lastp; p++) {
nuclear@2 258 i = htbl->huffval[p];
nuclear@2 259 if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
nuclear@2 260 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
nuclear@2 261 dtbl->ehufco[i] = huffcode[p];
nuclear@2 262 dtbl->ehufsi[i] = huffsize[p];
nuclear@2 263 }
nuclear@2 264 }
nuclear@2 265
nuclear@2 266
nuclear@2 267 /* Outputting bytes to the file */
nuclear@2 268
nuclear@2 269 /* Emit a byte, taking 'action' if must suspend. */
nuclear@2 270 #define emit_byte(state,val,action) \
nuclear@2 271 { *(state)->next_output_byte++ = (JOCTET) (val); \
nuclear@2 272 if (--(state)->free_in_buffer == 0) \
nuclear@2 273 if (! dump_buffer(state)) \
nuclear@2 274 { action; } }
nuclear@2 275
nuclear@2 276
nuclear@2 277 LOCAL(boolean)
nuclear@2 278 dump_buffer (working_state * state)
nuclear@2 279 /* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
nuclear@2 280 {
nuclear@2 281 struct jpeg_destination_mgr * dest = state->cinfo->dest;
nuclear@2 282
nuclear@2 283 if (! (*dest->empty_output_buffer) (state->cinfo))
nuclear@2 284 return FALSE;
nuclear@2 285 /* After a successful buffer dump, must reset buffer pointers */
nuclear@2 286 state->next_output_byte = dest->next_output_byte;
nuclear@2 287 state->free_in_buffer = dest->free_in_buffer;
nuclear@2 288 return TRUE;
nuclear@2 289 }
nuclear@2 290
nuclear@2 291
nuclear@2 292 /* Outputting bits to the file */
nuclear@2 293
nuclear@2 294 /* Only the right 24 bits of put_buffer are used; the valid bits are
nuclear@2 295 * left-justified in this part. At most 16 bits can be passed to emit_bits
nuclear@2 296 * in one call, and we never retain more than 7 bits in put_buffer
nuclear@2 297 * between calls, so 24 bits are sufficient.
nuclear@2 298 */
nuclear@2 299
nuclear@2 300 INLINE
nuclear@2 301 LOCAL(boolean)
nuclear@2 302 emit_bits (working_state * state, unsigned int code, int size)
nuclear@2 303 /* Emit some bits; return TRUE if successful, FALSE if must suspend */
nuclear@2 304 {
nuclear@2 305 /* This routine is heavily used, so it's worth coding tightly. */
nuclear@2 306 register INT32 put_buffer = (INT32) code;
nuclear@2 307 register int put_bits = state->cur.put_bits;
nuclear@2 308
nuclear@2 309 /* if size is 0, caller used an invalid Huffman table entry */
nuclear@2 310 if (size == 0)
nuclear@2 311 ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
nuclear@2 312
nuclear@2 313 put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
nuclear@2 314
nuclear@2 315 put_bits += size; /* new number of bits in buffer */
nuclear@2 316
nuclear@2 317 put_buffer <<= 24 - put_bits; /* align incoming bits */
nuclear@2 318
nuclear@2 319 put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */
nuclear@2 320
nuclear@2 321 while (put_bits >= 8) {
nuclear@2 322 int c = (int) ((put_buffer >> 16) & 0xFF);
nuclear@2 323
nuclear@2 324 emit_byte(state, c, return FALSE);
nuclear@2 325 if (c == 0xFF) { /* need to stuff a zero byte? */
nuclear@2 326 emit_byte(state, 0, return FALSE);
nuclear@2 327 }
nuclear@2 328 put_buffer <<= 8;
nuclear@2 329 put_bits -= 8;
nuclear@2 330 }
nuclear@2 331
nuclear@2 332 state->cur.put_buffer = put_buffer; /* update state variables */
nuclear@2 333 state->cur.put_bits = put_bits;
nuclear@2 334
nuclear@2 335 return TRUE;
nuclear@2 336 }
nuclear@2 337
nuclear@2 338
nuclear@2 339 LOCAL(boolean)
nuclear@2 340 flush_bits (working_state * state)
nuclear@2 341 {
nuclear@2 342 if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */
nuclear@2 343 return FALSE;
nuclear@2 344 state->cur.put_buffer = 0; /* and reset bit-buffer to empty */
nuclear@2 345 state->cur.put_bits = 0;
nuclear@2 346 return TRUE;
nuclear@2 347 }
nuclear@2 348
nuclear@2 349
nuclear@2 350 /* Encode a single block's worth of coefficients */
nuclear@2 351
nuclear@2 352 LOCAL(boolean)
nuclear@2 353 encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
nuclear@2 354 c_derived_tbl *dctbl, c_derived_tbl *actbl)
nuclear@2 355 {
nuclear@2 356 register int temp, temp2;
nuclear@2 357 register int nbits;
nuclear@2 358 register int k, r, i;
nuclear@2 359
nuclear@2 360 /* Encode the DC coefficient difference per section F.1.2.1 */
nuclear@2 361
nuclear@2 362 temp = temp2 = block[0] - last_dc_val;
nuclear@2 363
nuclear@2 364 if (temp < 0) {
nuclear@2 365 temp = -temp; /* temp is abs value of input */
nuclear@2 366 /* For a negative input, want temp2 = bitwise complement of abs(input) */
nuclear@2 367 /* This code assumes we are on a two's complement machine */
nuclear@2 368 temp2--;
nuclear@2 369 }
nuclear@2 370
nuclear@2 371 /* Find the number of bits needed for the magnitude of the coefficient */
nuclear@2 372 nbits = 0;
nuclear@2 373 while (temp) {
nuclear@2 374 nbits++;
nuclear@2 375 temp >>= 1;
nuclear@2 376 }
nuclear@2 377 /* Check for out-of-range coefficient values.
nuclear@2 378 * Since we're encoding a difference, the range limit is twice as much.
nuclear@2 379 */
nuclear@2 380 if (nbits > MAX_COEF_BITS+1)
nuclear@2 381 ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
nuclear@2 382
nuclear@2 383 /* Emit the Huffman-coded symbol for the number of bits */
nuclear@2 384 if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
nuclear@2 385 return FALSE;
nuclear@2 386
nuclear@2 387 /* Emit that number of bits of the value, if positive, */
nuclear@2 388 /* or the complement of its magnitude, if negative. */
nuclear@2 389 if (nbits) /* emit_bits rejects calls with size 0 */
nuclear@2 390 if (! emit_bits(state, (unsigned int) temp2, nbits))
nuclear@2 391 return FALSE;
nuclear@2 392
nuclear@2 393 /* Encode the AC coefficients per section F.1.2.2 */
nuclear@2 394
nuclear@2 395 r = 0; /* r = run length of zeros */
nuclear@2 396
nuclear@2 397 for (k = 1; k < DCTSIZE2; k++) {
nuclear@2 398 if ((temp = block[jpeg_natural_order[k]]) == 0) {
nuclear@2 399 r++;
nuclear@2 400 } else {
nuclear@2 401 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
nuclear@2 402 while (r > 15) {
nuclear@2 403 if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
nuclear@2 404 return FALSE;
nuclear@2 405 r -= 16;
nuclear@2 406 }
nuclear@2 407
nuclear@2 408 temp2 = temp;
nuclear@2 409 if (temp < 0) {
nuclear@2 410 temp = -temp; /* temp is abs value of input */
nuclear@2 411 /* This code assumes we are on a two's complement machine */
nuclear@2 412 temp2--;
nuclear@2 413 }
nuclear@2 414
nuclear@2 415 /* Find the number of bits needed for the magnitude of the coefficient */
nuclear@2 416 nbits = 1; /* there must be at least one 1 bit */
nuclear@2 417 while ((temp >>= 1))
nuclear@2 418 nbits++;
nuclear@2 419 /* Check for out-of-range coefficient values */
nuclear@2 420 if (nbits > MAX_COEF_BITS)
nuclear@2 421 ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
nuclear@2 422
nuclear@2 423 /* Emit Huffman symbol for run length / number of bits */
nuclear@2 424 i = (r << 4) + nbits;
nuclear@2 425 if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))
nuclear@2 426 return FALSE;
nuclear@2 427
nuclear@2 428 /* Emit that number of bits of the value, if positive, */
nuclear@2 429 /* or the complement of its magnitude, if negative. */
nuclear@2 430 if (! emit_bits(state, (unsigned int) temp2, nbits))
nuclear@2 431 return FALSE;
nuclear@2 432
nuclear@2 433 r = 0;
nuclear@2 434 }
nuclear@2 435 }
nuclear@2 436
nuclear@2 437 /* If the last coef(s) were zero, emit an end-of-block code */
nuclear@2 438 if (r > 0)
nuclear@2 439 if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
nuclear@2 440 return FALSE;
nuclear@2 441
nuclear@2 442 return TRUE;
nuclear@2 443 }
nuclear@2 444
nuclear@2 445
nuclear@2 446 /*
nuclear@2 447 * Emit a restart marker & resynchronize predictions.
nuclear@2 448 */
nuclear@2 449
nuclear@2 450 LOCAL(boolean)
nuclear@2 451 emit_restart (working_state * state, int restart_num)
nuclear@2 452 {
nuclear@2 453 int ci;
nuclear@2 454
nuclear@2 455 if (! flush_bits(state))
nuclear@2 456 return FALSE;
nuclear@2 457
nuclear@2 458 emit_byte(state, 0xFF, return FALSE);
nuclear@2 459 emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
nuclear@2 460
nuclear@2 461 /* Re-initialize DC predictions to 0 */
nuclear@2 462 for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
nuclear@2 463 state->cur.last_dc_val[ci] = 0;
nuclear@2 464
nuclear@2 465 /* The restart counter is not updated until we successfully write the MCU. */
nuclear@2 466
nuclear@2 467 return TRUE;
nuclear@2 468 }
nuclear@2 469
nuclear@2 470
nuclear@2 471 /*
nuclear@2 472 * Encode and output one MCU's worth of Huffman-compressed coefficients.
nuclear@2 473 */
nuclear@2 474
nuclear@2 475 METHODDEF(boolean)
nuclear@2 476 encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
nuclear@2 477 {
nuclear@2 478 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
nuclear@2 479 working_state state;
nuclear@2 480 int blkn, ci;
nuclear@2 481 jpeg_component_info * compptr;
nuclear@2 482
nuclear@2 483 /* Load up working state */
nuclear@2 484 state.next_output_byte = cinfo->dest->next_output_byte;
nuclear@2 485 state.free_in_buffer = cinfo->dest->free_in_buffer;
nuclear@2 486 ASSIGN_STATE(state.cur, entropy->saved);
nuclear@2 487 state.cinfo = cinfo;
nuclear@2 488
nuclear@2 489 /* Emit restart marker if needed */
nuclear@2 490 if (cinfo->restart_interval) {
nuclear@2 491 if (entropy->restarts_to_go == 0)
nuclear@2 492 if (! emit_restart(&state, entropy->next_restart_num))
nuclear@2 493 return FALSE;
nuclear@2 494 }
nuclear@2 495
nuclear@2 496 /* Encode the MCU data blocks */
nuclear@2 497 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
nuclear@2 498 ci = cinfo->MCU_membership[blkn];
nuclear@2 499 compptr = cinfo->cur_comp_info[ci];
nuclear@2 500 if (! encode_one_block(&state,
nuclear@2 501 MCU_data[blkn][0], state.cur.last_dc_val[ci],
nuclear@2 502 entropy->dc_derived_tbls[compptr->dc_tbl_no],
nuclear@2 503 entropy->ac_derived_tbls[compptr->ac_tbl_no]))
nuclear@2 504 return FALSE;
nuclear@2 505 /* Update last_dc_val */
nuclear@2 506 state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
nuclear@2 507 }
nuclear@2 508
nuclear@2 509 /* Completed MCU, so update state */
nuclear@2 510 cinfo->dest->next_output_byte = state.next_output_byte;
nuclear@2 511 cinfo->dest->free_in_buffer = state.free_in_buffer;
nuclear@2 512 ASSIGN_STATE(entropy->saved, state.cur);
nuclear@2 513
nuclear@2 514 /* Update restart-interval state too */
nuclear@2 515 if (cinfo->restart_interval) {
nuclear@2 516 if (entropy->restarts_to_go == 0) {
nuclear@2 517 entropy->restarts_to_go = cinfo->restart_interval;
nuclear@2 518 entropy->next_restart_num++;
nuclear@2 519 entropy->next_restart_num &= 7;
nuclear@2 520 }
nuclear@2 521 entropy->restarts_to_go--;
nuclear@2 522 }
nuclear@2 523
nuclear@2 524 return TRUE;
nuclear@2 525 }
nuclear@2 526
nuclear@2 527
nuclear@2 528 /*
nuclear@2 529 * Finish up at the end of a Huffman-compressed scan.
nuclear@2 530 */
nuclear@2 531
nuclear@2 532 METHODDEF(void)
nuclear@2 533 finish_pass_huff (j_compress_ptr cinfo)
nuclear@2 534 {
nuclear@2 535 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
nuclear@2 536 working_state state;
nuclear@2 537
nuclear@2 538 /* Load up working state ... flush_bits needs it */
nuclear@2 539 state.next_output_byte = cinfo->dest->next_output_byte;
nuclear@2 540 state.free_in_buffer = cinfo->dest->free_in_buffer;
nuclear@2 541 ASSIGN_STATE(state.cur, entropy->saved);
nuclear@2 542 state.cinfo = cinfo;
nuclear@2 543
nuclear@2 544 /* Flush out the last data */
nuclear@2 545 if (! flush_bits(&state))
nuclear@2 546 ERREXIT(cinfo, JERR_CANT_SUSPEND);
nuclear@2 547
nuclear@2 548 /* Update state */
nuclear@2 549 cinfo->dest->next_output_byte = state.next_output_byte;
nuclear@2 550 cinfo->dest->free_in_buffer = state.free_in_buffer;
nuclear@2 551 ASSIGN_STATE(entropy->saved, state.cur);
nuclear@2 552 }
nuclear@2 553
nuclear@2 554
nuclear@2 555 /*
nuclear@2 556 * Huffman coding optimization.
nuclear@2 557 *
nuclear@2 558 * We first scan the supplied data and count the number of uses of each symbol
nuclear@2 559 * that is to be Huffman-coded. (This process MUST agree with the code above.)
nuclear@2 560 * Then we build a Huffman coding tree for the observed counts.
nuclear@2 561 * Symbols which are not needed at all for the particular image are not
nuclear@2 562 * assigned any code, which saves space in the DHT marker as well as in
nuclear@2 563 * the compressed data.
nuclear@2 564 */
nuclear@2 565
nuclear@2 566 #ifdef ENTROPY_OPT_SUPPORTED
nuclear@2 567
nuclear@2 568
nuclear@2 569 /* Process a single block's worth of coefficients */
nuclear@2 570
nuclear@2 571 LOCAL(void)
nuclear@2 572 htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
nuclear@2 573 long dc_counts[], long ac_counts[])
nuclear@2 574 {
nuclear@2 575 register int temp;
nuclear@2 576 register int nbits;
nuclear@2 577 register int k, r;
nuclear@2 578
nuclear@2 579 /* Encode the DC coefficient difference per section F.1.2.1 */
nuclear@2 580
nuclear@2 581 temp = block[0] - last_dc_val;
nuclear@2 582 if (temp < 0)
nuclear@2 583 temp = -temp;
nuclear@2 584
nuclear@2 585 /* Find the number of bits needed for the magnitude of the coefficient */
nuclear@2 586 nbits = 0;
nuclear@2 587 while (temp) {
nuclear@2 588 nbits++;
nuclear@2 589 temp >>= 1;
nuclear@2 590 }
nuclear@2 591 /* Check for out-of-range coefficient values.
nuclear@2 592 * Since we're encoding a difference, the range limit is twice as much.
nuclear@2 593 */
nuclear@2 594 if (nbits > MAX_COEF_BITS+1)
nuclear@2 595 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
nuclear@2 596
nuclear@2 597 /* Count the Huffman symbol for the number of bits */
nuclear@2 598 dc_counts[nbits]++;
nuclear@2 599
nuclear@2 600 /* Encode the AC coefficients per section F.1.2.2 */
nuclear@2 601
nuclear@2 602 r = 0; /* r = run length of zeros */
nuclear@2 603
nuclear@2 604 for (k = 1; k < DCTSIZE2; k++) {
nuclear@2 605 if ((temp = block[jpeg_natural_order[k]]) == 0) {
nuclear@2 606 r++;
nuclear@2 607 } else {
nuclear@2 608 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
nuclear@2 609 while (r > 15) {
nuclear@2 610 ac_counts[0xF0]++;
nuclear@2 611 r -= 16;
nuclear@2 612 }
nuclear@2 613
nuclear@2 614 /* Find the number of bits needed for the magnitude of the coefficient */
nuclear@2 615 if (temp < 0)
nuclear@2 616 temp = -temp;
nuclear@2 617
nuclear@2 618 /* Find the number of bits needed for the magnitude of the coefficient */
nuclear@2 619 nbits = 1; /* there must be at least one 1 bit */
nuclear@2 620 while ((temp >>= 1))
nuclear@2 621 nbits++;
nuclear@2 622 /* Check for out-of-range coefficient values */
nuclear@2 623 if (nbits > MAX_COEF_BITS)
nuclear@2 624 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
nuclear@2 625
nuclear@2 626 /* Count Huffman symbol for run length / number of bits */
nuclear@2 627 ac_counts[(r << 4) + nbits]++;
nuclear@2 628
nuclear@2 629 r = 0;
nuclear@2 630 }
nuclear@2 631 }
nuclear@2 632
nuclear@2 633 /* If the last coef(s) were zero, emit an end-of-block code */
nuclear@2 634 if (r > 0)
nuclear@2 635 ac_counts[0]++;
nuclear@2 636 }
nuclear@2 637
nuclear@2 638
nuclear@2 639 /*
nuclear@2 640 * Trial-encode one MCU's worth of Huffman-compressed coefficients.
nuclear@2 641 * No data is actually output, so no suspension return is possible.
nuclear@2 642 */
nuclear@2 643
nuclear@2 644 METHODDEF(boolean)
nuclear@2 645 encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
nuclear@2 646 {
nuclear@2 647 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
nuclear@2 648 int blkn, ci;
nuclear@2 649 jpeg_component_info * compptr;
nuclear@2 650
nuclear@2 651 /* Take care of restart intervals if needed */
nuclear@2 652 if (cinfo->restart_interval) {
nuclear@2 653 if (entropy->restarts_to_go == 0) {
nuclear@2 654 /* Re-initialize DC predictions to 0 */
nuclear@2 655 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
nuclear@2 656 entropy->saved.last_dc_val[ci] = 0;
nuclear@2 657 /* Update restart state */
nuclear@2 658 entropy->restarts_to_go = cinfo->restart_interval;
nuclear@2 659 }
nuclear@2 660 entropy->restarts_to_go--;
nuclear@2 661 }
nuclear@2 662
nuclear@2 663 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
nuclear@2 664 ci = cinfo->MCU_membership[blkn];
nuclear@2 665 compptr = cinfo->cur_comp_info[ci];
nuclear@2 666 htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
nuclear@2 667 entropy->dc_count_ptrs[compptr->dc_tbl_no],
nuclear@2 668 entropy->ac_count_ptrs[compptr->ac_tbl_no]);
nuclear@2 669 entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
nuclear@2 670 }
nuclear@2 671
nuclear@2 672 return TRUE;
nuclear@2 673 }
nuclear@2 674
nuclear@2 675
nuclear@2 676 /*
nuclear@2 677 * Generate the best Huffman code table for the given counts, fill htbl.
nuclear@2 678 * Note this is also used by jcphuff.c.
nuclear@2 679 *
nuclear@2 680 * The JPEG standard requires that no symbol be assigned a codeword of all
nuclear@2 681 * one bits (so that padding bits added at the end of a compressed segment
nuclear@2 682 * can't look like a valid code). Because of the canonical ordering of
nuclear@2 683 * codewords, this just means that there must be an unused slot in the
nuclear@2 684 * longest codeword length category. Section K.2 of the JPEG spec suggests
nuclear@2 685 * reserving such a slot by pretending that symbol 256 is a valid symbol
nuclear@2 686 * with count 1. In theory that's not optimal; giving it count zero but
nuclear@2 687 * including it in the symbol set anyway should give a better Huffman code.
nuclear@2 688 * But the theoretically better code actually seems to come out worse in
nuclear@2 689 * practice, because it produces more all-ones bytes (which incur stuffed
nuclear@2 690 * zero bytes in the final file). In any case the difference is tiny.
nuclear@2 691 *
nuclear@2 692 * The JPEG standard requires Huffman codes to be no more than 16 bits long.
nuclear@2 693 * If some symbols have a very small but nonzero probability, the Huffman tree
nuclear@2 694 * must be adjusted to meet the code length restriction. We currently use
nuclear@2 695 * the adjustment method suggested in JPEG section K.2. This method is *not*
nuclear@2 696 * optimal; it may not choose the best possible limited-length code. But
nuclear@2 697 * typically only very-low-frequency symbols will be given less-than-optimal
nuclear@2 698 * lengths, so the code is almost optimal. Experimental comparisons against
nuclear@2 699 * an optimal limited-length-code algorithm indicate that the difference is
nuclear@2 700 * microscopic --- usually less than a hundredth of a percent of total size.
nuclear@2 701 * So the extra complexity of an optimal algorithm doesn't seem worthwhile.
nuclear@2 702 */
nuclear@2 703
nuclear@2 704 GLOBAL(void)
nuclear@2 705 jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
nuclear@2 706 {
nuclear@2 707 #define MAX_CLEN 32 /* assumed maximum initial code length */
nuclear@2 708 UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */
nuclear@2 709 int codesize[257]; /* codesize[k] = code length of symbol k */
nuclear@2 710 int others[257]; /* next symbol in current branch of tree */
nuclear@2 711 int c1, c2;
nuclear@2 712 int p, i, j;
nuclear@2 713 long v;
nuclear@2 714
nuclear@2 715 /* This algorithm is explained in section K.2 of the JPEG standard */
nuclear@2 716
nuclear@2 717 MEMZERO(bits, SIZEOF(bits));
nuclear@2 718 MEMZERO(codesize, SIZEOF(codesize));
nuclear@2 719 for (i = 0; i < 257; i++)
nuclear@2 720 others[i] = -1; /* init links to empty */
nuclear@2 721
nuclear@2 722 freq[256] = 1; /* make sure 256 has a nonzero count */
nuclear@2 723 /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
nuclear@2 724 * that no real symbol is given code-value of all ones, because 256
nuclear@2 725 * will be placed last in the largest codeword category.
nuclear@2 726 */
nuclear@2 727
nuclear@2 728 /* Huffman's basic algorithm to assign optimal code lengths to symbols */
nuclear@2 729
nuclear@2 730 for (;;) {
nuclear@2 731 /* Find the smallest nonzero frequency, set c1 = its symbol */
nuclear@2 732 /* In case of ties, take the larger symbol number */
nuclear@2 733 c1 = -1;
nuclear@2 734 v = 1000000000L;
nuclear@2 735 for (i = 0; i <= 256; i++) {
nuclear@2 736 if (freq[i] && freq[i] <= v) {
nuclear@2 737 v = freq[i];
nuclear@2 738 c1 = i;
nuclear@2 739 }
nuclear@2 740 }
nuclear@2 741
nuclear@2 742 /* Find the next smallest nonzero frequency, set c2 = its symbol */
nuclear@2 743 /* In case of ties, take the larger symbol number */
nuclear@2 744 c2 = -1;
nuclear@2 745 v = 1000000000L;
nuclear@2 746 for (i = 0; i <= 256; i++) {
nuclear@2 747 if (freq[i] && freq[i] <= v && i != c1) {
nuclear@2 748 v = freq[i];
nuclear@2 749 c2 = i;
nuclear@2 750 }
nuclear@2 751 }
nuclear@2 752
nuclear@2 753 /* Done if we've merged everything into one frequency */
nuclear@2 754 if (c2 < 0)
nuclear@2 755 break;
nuclear@2 756
nuclear@2 757 /* Else merge the two counts/trees */
nuclear@2 758 freq[c1] += freq[c2];
nuclear@2 759 freq[c2] = 0;
nuclear@2 760
nuclear@2 761 /* Increment the codesize of everything in c1's tree branch */
nuclear@2 762 codesize[c1]++;
nuclear@2 763 while (others[c1] >= 0) {
nuclear@2 764 c1 = others[c1];
nuclear@2 765 codesize[c1]++;
nuclear@2 766 }
nuclear@2 767
nuclear@2 768 others[c1] = c2; /* chain c2 onto c1's tree branch */
nuclear@2 769
nuclear@2 770 /* Increment the codesize of everything in c2's tree branch */
nuclear@2 771 codesize[c2]++;
nuclear@2 772 while (others[c2] >= 0) {
nuclear@2 773 c2 = others[c2];
nuclear@2 774 codesize[c2]++;
nuclear@2 775 }
nuclear@2 776 }
nuclear@2 777
nuclear@2 778 /* Now count the number of symbols of each code length */
nuclear@2 779 for (i = 0; i <= 256; i++) {
nuclear@2 780 if (codesize[i]) {
nuclear@2 781 /* The JPEG standard seems to think that this can't happen, */
nuclear@2 782 /* but I'm paranoid... */
nuclear@2 783 if (codesize[i] > MAX_CLEN)
nuclear@2 784 ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
nuclear@2 785
nuclear@2 786 bits[codesize[i]]++;
nuclear@2 787 }
nuclear@2 788 }
nuclear@2 789
nuclear@2 790 /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
nuclear@2 791 * Huffman procedure assigned any such lengths, we must adjust the coding.
nuclear@2 792 * Here is what the JPEG spec says about how this next bit works:
nuclear@2 793 * Since symbols are paired for the longest Huffman code, the symbols are
nuclear@2 794 * removed from this length category two at a time. The prefix for the pair
nuclear@2 795 * (which is one bit shorter) is allocated to one of the pair; then,
nuclear@2 796 * skipping the BITS entry for that prefix length, a code word from the next
nuclear@2 797 * shortest nonzero BITS entry is converted into a prefix for two code words
nuclear@2 798 * one bit longer.
nuclear@2 799 */
nuclear@2 800
nuclear@2 801 for (i = MAX_CLEN; i > 16; i--) {
nuclear@2 802 while (bits[i] > 0) {
nuclear@2 803 j = i - 2; /* find length of new prefix to be used */
nuclear@2 804 while (bits[j] == 0)
nuclear@2 805 j--;
nuclear@2 806
nuclear@2 807 bits[i] -= 2; /* remove two symbols */
nuclear@2 808 bits[i-1]++; /* one goes in this length */
nuclear@2 809 bits[j+1] += 2; /* two new symbols in this length */
nuclear@2 810 bits[j]--; /* symbol of this length is now a prefix */
nuclear@2 811 }
nuclear@2 812 }
nuclear@2 813
nuclear@2 814 /* Remove the count for the pseudo-symbol 256 from the largest codelength */
nuclear@2 815 while (bits[i] == 0) /* find largest codelength still in use */
nuclear@2 816 i--;
nuclear@2 817 bits[i]--;
nuclear@2 818
nuclear@2 819 /* Return final symbol counts (only for lengths 0..16) */
nuclear@2 820 MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
nuclear@2 821
nuclear@2 822 /* Return a list of the symbols sorted by code length */
nuclear@2 823 /* It's not real clear to me why we don't need to consider the codelength
nuclear@2 824 * changes made above, but the JPEG spec seems to think this works.
nuclear@2 825 */
nuclear@2 826 p = 0;
nuclear@2 827 for (i = 1; i <= MAX_CLEN; i++) {
nuclear@2 828 for (j = 0; j <= 255; j++) {
nuclear@2 829 if (codesize[j] == i) {
nuclear@2 830 htbl->huffval[p] = (UINT8) j;
nuclear@2 831 p++;
nuclear@2 832 }
nuclear@2 833 }
nuclear@2 834 }
nuclear@2 835
nuclear@2 836 /* Set sent_table FALSE so updated table will be written to JPEG file. */
nuclear@2 837 htbl->sent_table = FALSE;
nuclear@2 838 }
nuclear@2 839
nuclear@2 840
nuclear@2 841 /*
nuclear@2 842 * Finish up a statistics-gathering pass and create the new Huffman tables.
nuclear@2 843 */
nuclear@2 844
nuclear@2 845 METHODDEF(void)
nuclear@2 846 finish_pass_gather (j_compress_ptr cinfo)
nuclear@2 847 {
nuclear@2 848 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
nuclear@2 849 int ci, dctbl, actbl;
nuclear@2 850 jpeg_component_info * compptr;
nuclear@2 851 JHUFF_TBL **htblptr;
nuclear@2 852 boolean did_dc[NUM_HUFF_TBLS];
nuclear@2 853 boolean did_ac[NUM_HUFF_TBLS];
nuclear@2 854
nuclear@2 855 /* It's important not to apply jpeg_gen_optimal_table more than once
nuclear@2 856 * per table, because it clobbers the input frequency counts!
nuclear@2 857 */
nuclear@2 858 MEMZERO(did_dc, SIZEOF(did_dc));
nuclear@2 859 MEMZERO(did_ac, SIZEOF(did_ac));
nuclear@2 860
nuclear@2 861 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
nuclear@2 862 compptr = cinfo->cur_comp_info[ci];
nuclear@2 863 dctbl = compptr->dc_tbl_no;
nuclear@2 864 actbl = compptr->ac_tbl_no;
nuclear@2 865 if (! did_dc[dctbl]) {
nuclear@2 866 htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
nuclear@2 867 if (*htblptr == NULL)
nuclear@2 868 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
nuclear@2 869 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
nuclear@2 870 did_dc[dctbl] = TRUE;
nuclear@2 871 }
nuclear@2 872 if (! did_ac[actbl]) {
nuclear@2 873 htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
nuclear@2 874 if (*htblptr == NULL)
nuclear@2 875 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
nuclear@2 876 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
nuclear@2 877 did_ac[actbl] = TRUE;
nuclear@2 878 }
nuclear@2 879 }
nuclear@2 880 }
nuclear@2 881
nuclear@2 882
nuclear@2 883 #endif /* ENTROPY_OPT_SUPPORTED */
nuclear@2 884
nuclear@2 885
nuclear@2 886 /*
nuclear@2 887 * Module initialization routine for Huffman entropy encoding.
nuclear@2 888 */
nuclear@2 889
nuclear@2 890 GLOBAL(void)
nuclear@2 891 jinit_huff_encoder (j_compress_ptr cinfo)
nuclear@2 892 {
nuclear@2 893 huff_entropy_ptr entropy;
nuclear@2 894 int i;
nuclear@2 895
nuclear@2 896 entropy = (huff_entropy_ptr)
nuclear@2 897 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
nuclear@2 898 SIZEOF(huff_entropy_encoder));
nuclear@2 899 cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
nuclear@2 900 entropy->pub.start_pass = start_pass_huff;
nuclear@2 901
nuclear@2 902 /* Mark tables unallocated */
nuclear@2 903 for (i = 0; i < NUM_HUFF_TBLS; i++) {
nuclear@2 904 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
nuclear@2 905 #ifdef ENTROPY_OPT_SUPPORTED
nuclear@2 906 entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
nuclear@2 907 #endif
nuclear@2 908 }
nuclear@2 909 }