3dphotoshoot

annotate libs/libjpeg/jcphuff.c @ 27:3d082c566b53

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