vrshoot

annotate libs/libjpeg/jchuff.c @ 3:c179c72369be

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