dbf-halloween2015

annotate libs/libjpeg/jcphuff.c @ 4:4316c0c879e9

fixed RUN script for macosx
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Nov 2015 06:18:18 +0200
parents
children
rev   line source
nuclear@1 1 /*
nuclear@1 2 * jcphuff.c
nuclear@1 3 *
nuclear@1 4 * Copyright (C) 1995-1997, Thomas G. Lane.
nuclear@1 5 * This file is part of the Independent JPEG Group's software.
nuclear@1 6 * For conditions of distribution and use, see the accompanying README file.
nuclear@1 7 *
nuclear@1 8 * This file contains Huffman entropy encoding routines for progressive JPEG.
nuclear@1 9 *
nuclear@1 10 * We do not support output suspension in this module, since the library
nuclear@1 11 * currently does not allow multiple-scan files to be written with output
nuclear@1 12 * suspension.
nuclear@1 13 */
nuclear@1 14
nuclear@1 15 #define JPEG_INTERNALS
nuclear@1 16 #include "jinclude.h"
nuclear@1 17 #include "jpeglib.h"
nuclear@1 18 #include "jchuff.h" /* Declarations shared with jchuff.c */
nuclear@1 19
nuclear@1 20 #ifdef C_PROGRESSIVE_SUPPORTED
nuclear@1 21
nuclear@1 22 /* Expanded entropy encoder object for progressive Huffman encoding. */
nuclear@1 23
nuclear@1 24 typedef struct {
nuclear@1 25 struct jpeg_entropy_encoder pub; /* public fields */
nuclear@1 26
nuclear@1 27 /* Mode flag: TRUE for optimization, FALSE for actual data output */
nuclear@1 28 boolean gather_statistics;
nuclear@1 29
nuclear@1 30 /* Bit-level coding status.
nuclear@1 31 * next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
nuclear@1 32 */
nuclear@1 33 JOCTET * next_output_byte; /* => next byte to write in buffer */
nuclear@1 34 size_t free_in_buffer; /* # of byte spaces remaining in buffer */
nuclear@1 35 INT32 put_buffer; /* current bit-accumulation buffer */
nuclear@1 36 int put_bits; /* # of bits now in it */
nuclear@1 37 j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */
nuclear@1 38
nuclear@1 39 /* Coding status for DC components */
nuclear@1 40 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
nuclear@1 41
nuclear@1 42 /* Coding status for AC components */
nuclear@1 43 int ac_tbl_no; /* the table number of the single component */
nuclear@1 44 unsigned int EOBRUN; /* run length of EOBs */
nuclear@1 45 unsigned int BE; /* # of buffered correction bits before MCU */
nuclear@1 46 char * bit_buffer; /* buffer for correction bits (1 per char) */
nuclear@1 47 /* packing correction bits tightly would save some space but cost time... */
nuclear@1 48
nuclear@1 49 unsigned int restarts_to_go; /* MCUs left in this restart interval */
nuclear@1 50 int next_restart_num; /* next restart number to write (0-7) */
nuclear@1 51
nuclear@1 52 /* Pointers to derived tables (these workspaces have image lifespan).
nuclear@1 53 * Since any one scan codes only DC or only AC, we only need one set
nuclear@1 54 * of tables, not one for DC and one for AC.
nuclear@1 55 */
nuclear@1 56 c_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
nuclear@1 57
nuclear@1 58 /* Statistics tables for optimization; again, one set is enough */
nuclear@1 59 long * count_ptrs[NUM_HUFF_TBLS];
nuclear@1 60 } phuff_entropy_encoder;
nuclear@1 61
nuclear@1 62 typedef phuff_entropy_encoder * phuff_entropy_ptr;
nuclear@1 63
nuclear@1 64 /* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
nuclear@1 65 * buffer can hold. Larger sizes may slightly improve compression, but
nuclear@1 66 * 1000 is already well into the realm of overkill.
nuclear@1 67 * The minimum safe size is 64 bits.
nuclear@1 68 */
nuclear@1 69
nuclear@1 70 #define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */
nuclear@1 71
nuclear@1 72 /* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
nuclear@1 73 * We assume that int right shift is unsigned if INT32 right shift is,
nuclear@1 74 * which should be safe.
nuclear@1 75 */
nuclear@1 76
nuclear@1 77 #ifdef RIGHT_SHIFT_IS_UNSIGNED
nuclear@1 78 #define ISHIFT_TEMPS int ishift_temp;
nuclear@1 79 #define IRIGHT_SHIFT(x,shft) \
nuclear@1 80 ((ishift_temp = (x)) < 0 ? \
nuclear@1 81 (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
nuclear@1 82 (ishift_temp >> (shft)))
nuclear@1 83 #else
nuclear@1 84 #define ISHIFT_TEMPS
nuclear@1 85 #define IRIGHT_SHIFT(x,shft) ((x) >> (shft))
nuclear@1 86 #endif
nuclear@1 87
nuclear@1 88 /* Forward declarations */
nuclear@1 89 METHODDEF(boolean) encode_mcu_DC_first JPP((j_compress_ptr cinfo,
nuclear@1 90 JBLOCKROW *MCU_data));
nuclear@1 91 METHODDEF(boolean) encode_mcu_AC_first JPP((j_compress_ptr cinfo,
nuclear@1 92 JBLOCKROW *MCU_data));
nuclear@1 93 METHODDEF(boolean) encode_mcu_DC_refine JPP((j_compress_ptr cinfo,
nuclear@1 94 JBLOCKROW *MCU_data));
nuclear@1 95 METHODDEF(boolean) encode_mcu_AC_refine JPP((j_compress_ptr cinfo,
nuclear@1 96 JBLOCKROW *MCU_data));
nuclear@1 97 METHODDEF(void) finish_pass_phuff JPP((j_compress_ptr cinfo));
nuclear@1 98 METHODDEF(void) finish_pass_gather_phuff JPP((j_compress_ptr cinfo));
nuclear@1 99
nuclear@1 100
nuclear@1 101 /*
nuclear@1 102 * Initialize for a Huffman-compressed scan using progressive JPEG.
nuclear@1 103 */
nuclear@1 104
nuclear@1 105 METHODDEF(void)
nuclear@1 106 start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics)
nuclear@1 107 {
nuclear@1 108 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
nuclear@1 109 boolean is_DC_band;
nuclear@1 110 int ci, tbl;
nuclear@1 111 jpeg_component_info * compptr;
nuclear@1 112
nuclear@1 113 entropy->cinfo = cinfo;
nuclear@1 114 entropy->gather_statistics = gather_statistics;
nuclear@1 115
nuclear@1 116 is_DC_band = (cinfo->Ss == 0);
nuclear@1 117
nuclear@1 118 /* We assume jcmaster.c already validated the scan parameters. */
nuclear@1 119
nuclear@1 120 /* Select execution routines */
nuclear@1 121 if (cinfo->Ah == 0) {
nuclear@1 122 if (is_DC_band)
nuclear@1 123 entropy->pub.encode_mcu = encode_mcu_DC_first;
nuclear@1 124 else
nuclear@1 125 entropy->pub.encode_mcu = encode_mcu_AC_first;
nuclear@1 126 } else {
nuclear@1 127 if (is_DC_band)
nuclear@1 128 entropy->pub.encode_mcu = encode_mcu_DC_refine;
nuclear@1 129 else {
nuclear@1 130 entropy->pub.encode_mcu = encode_mcu_AC_refine;
nuclear@1 131 /* AC refinement needs a correction bit buffer */
nuclear@1 132 if (entropy->bit_buffer == NULL)
nuclear@1 133 entropy->bit_buffer = (char *)
nuclear@1 134 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
nuclear@1 135 MAX_CORR_BITS * SIZEOF(char));
nuclear@1 136 }
nuclear@1 137 }
nuclear@1 138 if (gather_statistics)
nuclear@1 139 entropy->pub.finish_pass = finish_pass_gather_phuff;
nuclear@1 140 else
nuclear@1 141 entropy->pub.finish_pass = finish_pass_phuff;
nuclear@1 142
nuclear@1 143 /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
nuclear@1 144 * for AC coefficients.
nuclear@1 145 */
nuclear@1 146 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
nuclear@1 147 compptr = cinfo->cur_comp_info[ci];
nuclear@1 148 /* Initialize DC predictions to 0 */
nuclear@1 149 entropy->last_dc_val[ci] = 0;
nuclear@1 150 /* Get table index */
nuclear@1 151 if (is_DC_band) {
nuclear@1 152 if (cinfo->Ah != 0) /* DC refinement needs no table */
nuclear@1 153 continue;
nuclear@1 154 tbl = compptr->dc_tbl_no;
nuclear@1 155 } else {
nuclear@1 156 entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
nuclear@1 157 }
nuclear@1 158 if (gather_statistics) {
nuclear@1 159 /* Check for invalid table index */
nuclear@1 160 /* (make_c_derived_tbl does this in the other path) */
nuclear@1 161 if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
nuclear@1 162 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
nuclear@1 163 /* Allocate and zero the statistics tables */
nuclear@1 164 /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
nuclear@1 165 if (entropy->count_ptrs[tbl] == NULL)
nuclear@1 166 entropy->count_ptrs[tbl] = (long *)
nuclear@1 167 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
nuclear@1 168 257 * SIZEOF(long));
nuclear@1 169 MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long));
nuclear@1 170 } else {
nuclear@1 171 /* Compute derived values for Huffman table */
nuclear@1 172 /* We may do this more than once for a table, but it's not expensive */
nuclear@1 173 jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
nuclear@1 174 & entropy->derived_tbls[tbl]);
nuclear@1 175 }
nuclear@1 176 }
nuclear@1 177
nuclear@1 178 /* Initialize AC stuff */
nuclear@1 179 entropy->EOBRUN = 0;
nuclear@1 180 entropy->BE = 0;
nuclear@1 181
nuclear@1 182 /* Initialize bit buffer to empty */
nuclear@1 183 entropy->put_buffer = 0;
nuclear@1 184 entropy->put_bits = 0;
nuclear@1 185
nuclear@1 186 /* Initialize restart stuff */
nuclear@1 187 entropy->restarts_to_go = cinfo->restart_interval;
nuclear@1 188 entropy->next_restart_num = 0;
nuclear@1 189 }
nuclear@1 190
nuclear@1 191
nuclear@1 192 /* Outputting bytes to the file.
nuclear@1 193 * NB: these must be called only when actually outputting,
nuclear@1 194 * that is, entropy->gather_statistics == FALSE.
nuclear@1 195 */
nuclear@1 196
nuclear@1 197 /* Emit a byte */
nuclear@1 198 #define emit_byte(entropy,val) \
nuclear@1 199 { *(entropy)->next_output_byte++ = (JOCTET) (val); \
nuclear@1 200 if (--(entropy)->free_in_buffer == 0) \
nuclear@1 201 dump_buffer(entropy); }
nuclear@1 202
nuclear@1 203
nuclear@1 204 LOCAL(void)
nuclear@1 205 dump_buffer (phuff_entropy_ptr entropy)
nuclear@1 206 /* Empty the output buffer; we do not support suspension in this module. */
nuclear@1 207 {
nuclear@1 208 struct jpeg_destination_mgr * dest = entropy->cinfo->dest;
nuclear@1 209
nuclear@1 210 if (! (*dest->empty_output_buffer) (entropy->cinfo))
nuclear@1 211 ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
nuclear@1 212 /* After a successful buffer dump, must reset buffer pointers */
nuclear@1 213 entropy->next_output_byte = dest->next_output_byte;
nuclear@1 214 entropy->free_in_buffer = dest->free_in_buffer;
nuclear@1 215 }
nuclear@1 216
nuclear@1 217
nuclear@1 218 /* Outputting bits to the file */
nuclear@1 219
nuclear@1 220 /* Only the right 24 bits of put_buffer are used; the valid bits are
nuclear@1 221 * left-justified in this part. At most 16 bits can be passed to emit_bits
nuclear@1 222 * in one call, and we never retain more than 7 bits in put_buffer
nuclear@1 223 * between calls, so 24 bits are sufficient.
nuclear@1 224 */
nuclear@1 225
nuclear@1 226 INLINE
nuclear@1 227 LOCAL(void)
nuclear@1 228 emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size)
nuclear@1 229 /* Emit some bits, unless we are in gather mode */
nuclear@1 230 {
nuclear@1 231 /* This routine is heavily used, so it's worth coding tightly. */
nuclear@1 232 register INT32 put_buffer = (INT32) code;
nuclear@1 233 register int put_bits = entropy->put_bits;
nuclear@1 234
nuclear@1 235 /* if size is 0, caller used an invalid Huffman table entry */
nuclear@1 236 if (size == 0)
nuclear@1 237 ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
nuclear@1 238
nuclear@1 239 if (entropy->gather_statistics)
nuclear@1 240 return; /* do nothing if we're only getting stats */
nuclear@1 241
nuclear@1 242 put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
nuclear@1 243
nuclear@1 244 put_bits += size; /* new number of bits in buffer */
nuclear@1 245
nuclear@1 246 put_buffer <<= 24 - put_bits; /* align incoming bits */
nuclear@1 247
nuclear@1 248 put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */
nuclear@1 249
nuclear@1 250 while (put_bits >= 8) {
nuclear@1 251 int c = (int) ((put_buffer >> 16) & 0xFF);
nuclear@1 252
nuclear@1 253 emit_byte(entropy, c);
nuclear@1 254 if (c == 0xFF) { /* need to stuff a zero byte? */
nuclear@1 255 emit_byte(entropy, 0);
nuclear@1 256 }
nuclear@1 257 put_buffer <<= 8;
nuclear@1 258 put_bits -= 8;
nuclear@1 259 }
nuclear@1 260
nuclear@1 261 entropy->put_buffer = put_buffer; /* update variables */
nuclear@1 262 entropy->put_bits = put_bits;
nuclear@1 263 }
nuclear@1 264
nuclear@1 265
nuclear@1 266 LOCAL(void)
nuclear@1 267 flush_bits (phuff_entropy_ptr entropy)
nuclear@1 268 {
nuclear@1 269 emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */
nuclear@1 270 entropy->put_buffer = 0; /* and reset bit-buffer to empty */
nuclear@1 271 entropy->put_bits = 0;
nuclear@1 272 }
nuclear@1 273
nuclear@1 274
nuclear@1 275 /*
nuclear@1 276 * Emit (or just count) a Huffman symbol.
nuclear@1 277 */
nuclear@1 278
nuclear@1 279 INLINE
nuclear@1 280 LOCAL(void)
nuclear@1 281 emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol)
nuclear@1 282 {
nuclear@1 283 if (entropy->gather_statistics)
nuclear@1 284 entropy->count_ptrs[tbl_no][symbol]++;
nuclear@1 285 else {
nuclear@1 286 c_derived_tbl * tbl = entropy->derived_tbls[tbl_no];
nuclear@1 287 emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
nuclear@1 288 }
nuclear@1 289 }
nuclear@1 290
nuclear@1 291
nuclear@1 292 /*
nuclear@1 293 * Emit bits from a correction bit buffer.
nuclear@1 294 */
nuclear@1 295
nuclear@1 296 LOCAL(void)
nuclear@1 297 emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart,
nuclear@1 298 unsigned int nbits)
nuclear@1 299 {
nuclear@1 300 if (entropy->gather_statistics)
nuclear@1 301 return; /* no real work */
nuclear@1 302
nuclear@1 303 while (nbits > 0) {
nuclear@1 304 emit_bits(entropy, (unsigned int) (*bufstart), 1);
nuclear@1 305 bufstart++;
nuclear@1 306 nbits--;
nuclear@1 307 }
nuclear@1 308 }
nuclear@1 309
nuclear@1 310
nuclear@1 311 /*
nuclear@1 312 * Emit any pending EOBRUN symbol.
nuclear@1 313 */
nuclear@1 314
nuclear@1 315 LOCAL(void)
nuclear@1 316 emit_eobrun (phuff_entropy_ptr entropy)
nuclear@1 317 {
nuclear@1 318 register int temp, nbits;
nuclear@1 319
nuclear@1 320 if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */
nuclear@1 321 temp = entropy->EOBRUN;
nuclear@1 322 nbits = 0;
nuclear@1 323 while ((temp >>= 1))
nuclear@1 324 nbits++;
nuclear@1 325 /* safety check: shouldn't happen given limited correction-bit buffer */
nuclear@1 326 if (nbits > 14)
nuclear@1 327 ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
nuclear@1 328
nuclear@1 329 emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
nuclear@1 330 if (nbits)
nuclear@1 331 emit_bits(entropy, entropy->EOBRUN, nbits);
nuclear@1 332
nuclear@1 333 entropy->EOBRUN = 0;
nuclear@1 334
nuclear@1 335 /* Emit any buffered correction bits */
nuclear@1 336 emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
nuclear@1 337 entropy->BE = 0;
nuclear@1 338 }
nuclear@1 339 }
nuclear@1 340
nuclear@1 341
nuclear@1 342 /*
nuclear@1 343 * Emit a restart marker & resynchronize predictions.
nuclear@1 344 */
nuclear@1 345
nuclear@1 346 LOCAL(void)
nuclear@1 347 emit_restart (phuff_entropy_ptr entropy, int restart_num)
nuclear@1 348 {
nuclear@1 349 int ci;
nuclear@1 350
nuclear@1 351 emit_eobrun(entropy);
nuclear@1 352
nuclear@1 353 if (! entropy->gather_statistics) {
nuclear@1 354 flush_bits(entropy);
nuclear@1 355 emit_byte(entropy, 0xFF);
nuclear@1 356 emit_byte(entropy, JPEG_RST0 + restart_num);
nuclear@1 357 }
nuclear@1 358
nuclear@1 359 if (entropy->cinfo->Ss == 0) {
nuclear@1 360 /* Re-initialize DC predictions to 0 */
nuclear@1 361 for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
nuclear@1 362 entropy->last_dc_val[ci] = 0;
nuclear@1 363 } else {
nuclear@1 364 /* Re-initialize all AC-related fields to 0 */
nuclear@1 365 entropy->EOBRUN = 0;
nuclear@1 366 entropy->BE = 0;
nuclear@1 367 }
nuclear@1 368 }
nuclear@1 369
nuclear@1 370
nuclear@1 371 /*
nuclear@1 372 * MCU encoding for DC initial scan (either spectral selection,
nuclear@1 373 * or first pass of successive approximation).
nuclear@1 374 */
nuclear@1 375
nuclear@1 376 METHODDEF(boolean)
nuclear@1 377 encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
nuclear@1 378 {
nuclear@1 379 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
nuclear@1 380 register int temp, temp2;
nuclear@1 381 register int nbits;
nuclear@1 382 int blkn, ci;
nuclear@1 383 int Al = cinfo->Al;
nuclear@1 384 JBLOCKROW block;
nuclear@1 385 jpeg_component_info * compptr;
nuclear@1 386 ISHIFT_TEMPS
nuclear@1 387
nuclear@1 388 entropy->next_output_byte = cinfo->dest->next_output_byte;
nuclear@1 389 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
nuclear@1 390
nuclear@1 391 /* Emit restart marker if needed */
nuclear@1 392 if (cinfo->restart_interval)
nuclear@1 393 if (entropy->restarts_to_go == 0)
nuclear@1 394 emit_restart(entropy, entropy->next_restart_num);
nuclear@1 395
nuclear@1 396 /* Encode the MCU data blocks */
nuclear@1 397 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
nuclear@1 398 block = MCU_data[blkn];
nuclear@1 399 ci = cinfo->MCU_membership[blkn];
nuclear@1 400 compptr = cinfo->cur_comp_info[ci];
nuclear@1 401
nuclear@1 402 /* Compute the DC value after the required point transform by Al.
nuclear@1 403 * This is simply an arithmetic right shift.
nuclear@1 404 */
nuclear@1 405 temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);
nuclear@1 406
nuclear@1 407 /* DC differences are figured on the point-transformed values. */
nuclear@1 408 temp = temp2 - entropy->last_dc_val[ci];
nuclear@1 409 entropy->last_dc_val[ci] = temp2;
nuclear@1 410
nuclear@1 411 /* Encode the DC coefficient difference per section G.1.2.1 */
nuclear@1 412 temp2 = temp;
nuclear@1 413 if (temp < 0) {
nuclear@1 414 temp = -temp; /* temp is abs value of input */
nuclear@1 415 /* For a negative input, want temp2 = bitwise complement of abs(input) */
nuclear@1 416 /* This code assumes we are on a two's complement machine */
nuclear@1 417 temp2--;
nuclear@1 418 }
nuclear@1 419
nuclear@1 420 /* Find the number of bits needed for the magnitude of the coefficient */
nuclear@1 421 nbits = 0;
nuclear@1 422 while (temp) {
nuclear@1 423 nbits++;
nuclear@1 424 temp >>= 1;
nuclear@1 425 }
nuclear@1 426 /* Check for out-of-range coefficient values.
nuclear@1 427 * Since we're encoding a difference, the range limit is twice as much.
nuclear@1 428 */
nuclear@1 429 if (nbits > MAX_COEF_BITS+1)
nuclear@1 430 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
nuclear@1 431
nuclear@1 432 /* Count/emit the Huffman-coded symbol for the number of bits */
nuclear@1 433 emit_symbol(entropy, compptr->dc_tbl_no, nbits);
nuclear@1 434
nuclear@1 435 /* Emit that number of bits of the value, if positive, */
nuclear@1 436 /* or the complement of its magnitude, if negative. */
nuclear@1 437 if (nbits) /* emit_bits rejects calls with size 0 */
nuclear@1 438 emit_bits(entropy, (unsigned int) temp2, nbits);
nuclear@1 439 }
nuclear@1 440
nuclear@1 441 cinfo->dest->next_output_byte = entropy->next_output_byte;
nuclear@1 442 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
nuclear@1 443
nuclear@1 444 /* Update restart-interval state too */
nuclear@1 445 if (cinfo->restart_interval) {
nuclear@1 446 if (entropy->restarts_to_go == 0) {
nuclear@1 447 entropy->restarts_to_go = cinfo->restart_interval;
nuclear@1 448 entropy->next_restart_num++;
nuclear@1 449 entropy->next_restart_num &= 7;
nuclear@1 450 }
nuclear@1 451 entropy->restarts_to_go--;
nuclear@1 452 }
nuclear@1 453
nuclear@1 454 return TRUE;
nuclear@1 455 }
nuclear@1 456
nuclear@1 457
nuclear@1 458 /*
nuclear@1 459 * MCU encoding for AC initial scan (either spectral selection,
nuclear@1 460 * or first pass of successive approximation).
nuclear@1 461 */
nuclear@1 462
nuclear@1 463 METHODDEF(boolean)
nuclear@1 464 encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
nuclear@1 465 {
nuclear@1 466 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
nuclear@1 467 register int temp, temp2;
nuclear@1 468 register int nbits;
nuclear@1 469 register int r, k;
nuclear@1 470 int Se = cinfo->Se;
nuclear@1 471 int Al = cinfo->Al;
nuclear@1 472 JBLOCKROW block;
nuclear@1 473
nuclear@1 474 entropy->next_output_byte = cinfo->dest->next_output_byte;
nuclear@1 475 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
nuclear@1 476
nuclear@1 477 /* Emit restart marker if needed */
nuclear@1 478 if (cinfo->restart_interval)
nuclear@1 479 if (entropy->restarts_to_go == 0)
nuclear@1 480 emit_restart(entropy, entropy->next_restart_num);
nuclear@1 481
nuclear@1 482 /* Encode the MCU data block */
nuclear@1 483 block = MCU_data[0];
nuclear@1 484
nuclear@1 485 /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
nuclear@1 486
nuclear@1 487 r = 0; /* r = run length of zeros */
nuclear@1 488
nuclear@1 489 for (k = cinfo->Ss; k <= Se; k++) {
nuclear@1 490 if ((temp = (*block)[jpeg_natural_order[k]]) == 0) {
nuclear@1 491 r++;
nuclear@1 492 continue;
nuclear@1 493 }
nuclear@1 494 /* We must apply the point transform by Al. For AC coefficients this
nuclear@1 495 * is an integer division with rounding towards 0. To do this portably
nuclear@1 496 * in C, we shift after obtaining the absolute value; so the code is
nuclear@1 497 * interwoven with finding the abs value (temp) and output bits (temp2).
nuclear@1 498 */
nuclear@1 499 if (temp < 0) {
nuclear@1 500 temp = -temp; /* temp is abs value of input */
nuclear@1 501 temp >>= Al; /* apply the point transform */
nuclear@1 502 /* For a negative coef, want temp2 = bitwise complement of abs(coef) */
nuclear@1 503 temp2 = ~temp;
nuclear@1 504 } else {
nuclear@1 505 temp >>= Al; /* apply the point transform */
nuclear@1 506 temp2 = temp;
nuclear@1 507 }
nuclear@1 508 /* Watch out for case that nonzero coef is zero after point transform */
nuclear@1 509 if (temp == 0) {
nuclear@1 510 r++;
nuclear@1 511 continue;
nuclear@1 512 }
nuclear@1 513
nuclear@1 514 /* Emit any pending EOBRUN */
nuclear@1 515 if (entropy->EOBRUN > 0)
nuclear@1 516 emit_eobrun(entropy);
nuclear@1 517 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
nuclear@1 518 while (r > 15) {
nuclear@1 519 emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
nuclear@1 520 r -= 16;
nuclear@1 521 }
nuclear@1 522
nuclear@1 523 /* Find the number of bits needed for the magnitude of the coefficient */
nuclear@1 524 nbits = 1; /* there must be at least one 1 bit */
nuclear@1 525 while ((temp >>= 1))
nuclear@1 526 nbits++;
nuclear@1 527 /* Check for out-of-range coefficient values */
nuclear@1 528 if (nbits > MAX_COEF_BITS)
nuclear@1 529 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
nuclear@1 530
nuclear@1 531 /* Count/emit Huffman symbol for run length / number of bits */
nuclear@1 532 emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);
nuclear@1 533
nuclear@1 534 /* Emit that number of bits of the value, if positive, */
nuclear@1 535 /* or the complement of its magnitude, if negative. */
nuclear@1 536 emit_bits(entropy, (unsigned int) temp2, nbits);
nuclear@1 537
nuclear@1 538 r = 0; /* reset zero run length */
nuclear@1 539 }
nuclear@1 540
nuclear@1 541 if (r > 0) { /* If there are trailing zeroes, */
nuclear@1 542 entropy->EOBRUN++; /* count an EOB */
nuclear@1 543 if (entropy->EOBRUN == 0x7FFF)
nuclear@1 544 emit_eobrun(entropy); /* force it out to avoid overflow */
nuclear@1 545 }
nuclear@1 546
nuclear@1 547 cinfo->dest->next_output_byte = entropy->next_output_byte;
nuclear@1 548 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
nuclear@1 549
nuclear@1 550 /* Update restart-interval state too */
nuclear@1 551 if (cinfo->restart_interval) {
nuclear@1 552 if (entropy->restarts_to_go == 0) {
nuclear@1 553 entropy->restarts_to_go = cinfo->restart_interval;
nuclear@1 554 entropy->next_restart_num++;
nuclear@1 555 entropy->next_restart_num &= 7;
nuclear@1 556 }
nuclear@1 557 entropy->restarts_to_go--;
nuclear@1 558 }
nuclear@1 559
nuclear@1 560 return TRUE;
nuclear@1 561 }
nuclear@1 562
nuclear@1 563
nuclear@1 564 /*
nuclear@1 565 * MCU encoding for DC successive approximation refinement scan.
nuclear@1 566 * Note: we assume such scans can be multi-component, although the spec
nuclear@1 567 * is not very clear on the point.
nuclear@1 568 */
nuclear@1 569
nuclear@1 570 METHODDEF(boolean)
nuclear@1 571 encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
nuclear@1 572 {
nuclear@1 573 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
nuclear@1 574 register int temp;
nuclear@1 575 int blkn;
nuclear@1 576 int Al = cinfo->Al;
nuclear@1 577 JBLOCKROW block;
nuclear@1 578
nuclear@1 579 entropy->next_output_byte = cinfo->dest->next_output_byte;
nuclear@1 580 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
nuclear@1 581
nuclear@1 582 /* Emit restart marker if needed */
nuclear@1 583 if (cinfo->restart_interval)
nuclear@1 584 if (entropy->restarts_to_go == 0)
nuclear@1 585 emit_restart(entropy, entropy->next_restart_num);
nuclear@1 586
nuclear@1 587 /* Encode the MCU data blocks */
nuclear@1 588 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
nuclear@1 589 block = MCU_data[blkn];
nuclear@1 590
nuclear@1 591 /* We simply emit the Al'th bit of the DC coefficient value. */
nuclear@1 592 temp = (*block)[0];
nuclear@1 593 emit_bits(entropy, (unsigned int) (temp >> Al), 1);
nuclear@1 594 }
nuclear@1 595
nuclear@1 596 cinfo->dest->next_output_byte = entropy->next_output_byte;
nuclear@1 597 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
nuclear@1 598
nuclear@1 599 /* Update restart-interval state too */
nuclear@1 600 if (cinfo->restart_interval) {
nuclear@1 601 if (entropy->restarts_to_go == 0) {
nuclear@1 602 entropy->restarts_to_go = cinfo->restart_interval;
nuclear@1 603 entropy->next_restart_num++;
nuclear@1 604 entropy->next_restart_num &= 7;
nuclear@1 605 }
nuclear@1 606 entropy->restarts_to_go--;
nuclear@1 607 }
nuclear@1 608
nuclear@1 609 return TRUE;
nuclear@1 610 }
nuclear@1 611
nuclear@1 612
nuclear@1 613 /*
nuclear@1 614 * MCU encoding for AC successive approximation refinement scan.
nuclear@1 615 */
nuclear@1 616
nuclear@1 617 METHODDEF(boolean)
nuclear@1 618 encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
nuclear@1 619 {
nuclear@1 620 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
nuclear@1 621 register int temp;
nuclear@1 622 register int r, k;
nuclear@1 623 int EOB;
nuclear@1 624 char *BR_buffer;
nuclear@1 625 unsigned int BR;
nuclear@1 626 int Se = cinfo->Se;
nuclear@1 627 int Al = cinfo->Al;
nuclear@1 628 JBLOCKROW block;
nuclear@1 629 int absvalues[DCTSIZE2];
nuclear@1 630
nuclear@1 631 entropy->next_output_byte = cinfo->dest->next_output_byte;
nuclear@1 632 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
nuclear@1 633
nuclear@1 634 /* Emit restart marker if needed */
nuclear@1 635 if (cinfo->restart_interval)
nuclear@1 636 if (entropy->restarts_to_go == 0)
nuclear@1 637 emit_restart(entropy, entropy->next_restart_num);
nuclear@1 638
nuclear@1 639 /* Encode the MCU data block */
nuclear@1 640 block = MCU_data[0];
nuclear@1 641
nuclear@1 642 /* It is convenient to make a pre-pass to determine the transformed
nuclear@1 643 * coefficients' absolute values and the EOB position.
nuclear@1 644 */
nuclear@1 645 EOB = 0;
nuclear@1 646 for (k = cinfo->Ss; k <= Se; k++) {
nuclear@1 647 temp = (*block)[jpeg_natural_order[k]];
nuclear@1 648 /* We must apply the point transform by Al. For AC coefficients this
nuclear@1 649 * is an integer division with rounding towards 0. To do this portably
nuclear@1 650 * in C, we shift after obtaining the absolute value.
nuclear@1 651 */
nuclear@1 652 if (temp < 0)
nuclear@1 653 temp = -temp; /* temp is abs value of input */
nuclear@1 654 temp >>= Al; /* apply the point transform */
nuclear@1 655 absvalues[k] = temp; /* save abs value for main pass */
nuclear@1 656 if (temp == 1)
nuclear@1 657 EOB = k; /* EOB = index of last newly-nonzero coef */
nuclear@1 658 }
nuclear@1 659
nuclear@1 660 /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
nuclear@1 661
nuclear@1 662 r = 0; /* r = run length of zeros */
nuclear@1 663 BR = 0; /* BR = count of buffered bits added now */
nuclear@1 664 BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
nuclear@1 665
nuclear@1 666 for (k = cinfo->Ss; k <= Se; k++) {
nuclear@1 667 if ((temp = absvalues[k]) == 0) {
nuclear@1 668 r++;
nuclear@1 669 continue;
nuclear@1 670 }
nuclear@1 671
nuclear@1 672 /* Emit any required ZRLs, but not if they can be folded into EOB */
nuclear@1 673 while (r > 15 && k <= EOB) {
nuclear@1 674 /* emit any pending EOBRUN and the BE correction bits */
nuclear@1 675 emit_eobrun(entropy);
nuclear@1 676 /* Emit ZRL */
nuclear@1 677 emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
nuclear@1 678 r -= 16;
nuclear@1 679 /* Emit buffered correction bits that must be associated with ZRL */
nuclear@1 680 emit_buffered_bits(entropy, BR_buffer, BR);
nuclear@1 681 BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
nuclear@1 682 BR = 0;
nuclear@1 683 }
nuclear@1 684
nuclear@1 685 /* If the coef was previously nonzero, it only needs a correction bit.
nuclear@1 686 * NOTE: a straight translation of the spec's figure G.7 would suggest
nuclear@1 687 * that we also need to test r > 15. But if r > 15, we can only get here
nuclear@1 688 * if k > EOB, which implies that this coefficient is not 1.
nuclear@1 689 */
nuclear@1 690 if (temp > 1) {
nuclear@1 691 /* The correction bit is the next bit of the absolute value. */
nuclear@1 692 BR_buffer[BR++] = (char) (temp & 1);
nuclear@1 693 continue;
nuclear@1 694 }
nuclear@1 695
nuclear@1 696 /* Emit any pending EOBRUN and the BE correction bits */
nuclear@1 697 emit_eobrun(entropy);
nuclear@1 698
nuclear@1 699 /* Count/emit Huffman symbol for run length / number of bits */
nuclear@1 700 emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);
nuclear@1 701
nuclear@1 702 /* Emit output bit for newly-nonzero coef */
nuclear@1 703 temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1;
nuclear@1 704 emit_bits(entropy, (unsigned int) temp, 1);
nuclear@1 705
nuclear@1 706 /* Emit buffered correction bits that must be associated with this code */
nuclear@1 707 emit_buffered_bits(entropy, BR_buffer, BR);
nuclear@1 708 BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
nuclear@1 709 BR = 0;
nuclear@1 710 r = 0; /* reset zero run length */
nuclear@1 711 }
nuclear@1 712
nuclear@1 713 if (r > 0 || BR > 0) { /* If there are trailing zeroes, */
nuclear@1 714 entropy->EOBRUN++; /* count an EOB */
nuclear@1 715 entropy->BE += BR; /* concat my correction bits to older ones */
nuclear@1 716 /* We force out the EOB if we risk either:
nuclear@1 717 * 1. overflow of the EOB counter;
nuclear@1 718 * 2. overflow of the correction bit buffer during the next MCU.
nuclear@1 719 */
nuclear@1 720 if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))
nuclear@1 721 emit_eobrun(entropy);
nuclear@1 722 }
nuclear@1 723
nuclear@1 724 cinfo->dest->next_output_byte = entropy->next_output_byte;
nuclear@1 725 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
nuclear@1 726
nuclear@1 727 /* Update restart-interval state too */
nuclear@1 728 if (cinfo->restart_interval) {
nuclear@1 729 if (entropy->restarts_to_go == 0) {
nuclear@1 730 entropy->restarts_to_go = cinfo->restart_interval;
nuclear@1 731 entropy->next_restart_num++;
nuclear@1 732 entropy->next_restart_num &= 7;
nuclear@1 733 }
nuclear@1 734 entropy->restarts_to_go--;
nuclear@1 735 }
nuclear@1 736
nuclear@1 737 return TRUE;
nuclear@1 738 }
nuclear@1 739
nuclear@1 740
nuclear@1 741 /*
nuclear@1 742 * Finish up at the end of a Huffman-compressed progressive scan.
nuclear@1 743 */
nuclear@1 744
nuclear@1 745 METHODDEF(void)
nuclear@1 746 finish_pass_phuff (j_compress_ptr cinfo)
nuclear@1 747 {
nuclear@1 748 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
nuclear@1 749
nuclear@1 750 entropy->next_output_byte = cinfo->dest->next_output_byte;
nuclear@1 751 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
nuclear@1 752
nuclear@1 753 /* Flush out any buffered data */
nuclear@1 754 emit_eobrun(entropy);
nuclear@1 755 flush_bits(entropy);
nuclear@1 756
nuclear@1 757 cinfo->dest->next_output_byte = entropy->next_output_byte;
nuclear@1 758 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
nuclear@1 759 }
nuclear@1 760
nuclear@1 761
nuclear@1 762 /*
nuclear@1 763 * Finish up a statistics-gathering pass and create the new Huffman tables.
nuclear@1 764 */
nuclear@1 765
nuclear@1 766 METHODDEF(void)
nuclear@1 767 finish_pass_gather_phuff (j_compress_ptr cinfo)
nuclear@1 768 {
nuclear@1 769 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
nuclear@1 770 boolean is_DC_band;
nuclear@1 771 int ci, tbl;
nuclear@1 772 jpeg_component_info * compptr;
nuclear@1 773 JHUFF_TBL **htblptr;
nuclear@1 774 boolean did[NUM_HUFF_TBLS];
nuclear@1 775
nuclear@1 776 /* Flush out buffered data (all we care about is counting the EOB symbol) */
nuclear@1 777 emit_eobrun(entropy);
nuclear@1 778
nuclear@1 779 is_DC_band = (cinfo->Ss == 0);
nuclear@1 780
nuclear@1 781 /* It's important not to apply jpeg_gen_optimal_table more than once
nuclear@1 782 * per table, because it clobbers the input frequency counts!
nuclear@1 783 */
nuclear@1 784 MEMZERO(did, SIZEOF(did));
nuclear@1 785
nuclear@1 786 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
nuclear@1 787 compptr = cinfo->cur_comp_info[ci];
nuclear@1 788 if (is_DC_band) {
nuclear@1 789 if (cinfo->Ah != 0) /* DC refinement needs no table */
nuclear@1 790 continue;
nuclear@1 791 tbl = compptr->dc_tbl_no;
nuclear@1 792 } else {
nuclear@1 793 tbl = compptr->ac_tbl_no;
nuclear@1 794 }
nuclear@1 795 if (! did[tbl]) {
nuclear@1 796 if (is_DC_band)
nuclear@1 797 htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
nuclear@1 798 else
nuclear@1 799 htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
nuclear@1 800 if (*htblptr == NULL)
nuclear@1 801 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
nuclear@1 802 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);
nuclear@1 803 did[tbl] = TRUE;
nuclear@1 804 }
nuclear@1 805 }
nuclear@1 806 }
nuclear@1 807
nuclear@1 808
nuclear@1 809 /*
nuclear@1 810 * Module initialization routine for progressive Huffman entropy encoding.
nuclear@1 811 */
nuclear@1 812
nuclear@1 813 GLOBAL(void)
nuclear@1 814 jinit_phuff_encoder (j_compress_ptr cinfo)
nuclear@1 815 {
nuclear@1 816 phuff_entropy_ptr entropy;
nuclear@1 817 int i;
nuclear@1 818
nuclear@1 819 entropy = (phuff_entropy_ptr)
nuclear@1 820 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
nuclear@1 821 SIZEOF(phuff_entropy_encoder));
nuclear@1 822 cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
nuclear@1 823 entropy->pub.start_pass = start_pass_phuff;
nuclear@1 824
nuclear@1 825 /* Mark tables unallocated */
nuclear@1 826 for (i = 0; i < NUM_HUFF_TBLS; i++) {
nuclear@1 827 entropy->derived_tbls[i] = NULL;
nuclear@1 828 entropy->count_ptrs[i] = NULL;
nuclear@1 829 }
nuclear@1 830 entropy->bit_buffer = NULL; /* needed only in AC refinement scan */
nuclear@1 831 }
nuclear@1 832
nuclear@1 833 #endif /* C_PROGRESSIVE_SUPPORTED */