3dphotoshoot

annotate libs/libjpeg/jchuff.c @ 17:aef7f51f6397

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