| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#define JPEG_INTERNALS |
| 16 |
#include "jinclude.h" |
| 17 |
#include "jpeglib.h" |
| 18 |
#include "jchuff.h" |
| 19 |
|
| 20 |
#ifdef C_PROGRESSIVE_SUPPORTED |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
typedef struct { |
| 25 |
struct jpeg_entropy_encoder pub; |
| 26 |
|
| 27 |
|
| 28 |
boolean gather_statistics; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
JOCTET * next_output_byte; |
| 34 |
size_t free_in_buffer; |
| 35 |
INT32 put_buffer; |
| 36 |
int put_bits; |
| 37 |
j_compress_ptr cinfo; |
| 38 |
|
| 39 |
|
| 40 |
int last_dc_val[MAX_COMPS_IN_SCAN]; |
| 41 |
|
| 42 |
|
| 43 |
int ac_tbl_no; |
| 44 |
unsigned int EOBRUN; |
| 45 |
unsigned int BE; |
| 46 |
char * bit_buffer; |
| 47 |
|
| 48 |
|
| 49 |
unsigned int restarts_to_go; |
| 50 |
int next_restart_num; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
c_derived_tbl * derived_tbls[NUM_HUFF_TBLS]; |
| 57 |
|
| 58 |
|
| 59 |
long * count_ptrs[NUM_HUFF_TBLS]; |
| 60 |
} phuff_entropy_encoder; |
| 61 |
|
| 62 |
typedef phuff_entropy_encoder * phuff_entropy_ptr; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
#define MAX_CORR_BITS 1000 |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
#ifdef RIGHT_SHIFT_IS_UNSIGNED |
| 78 |
#define ISHIFT_TEMPS int ishift_temp; |
| 79 |
#define IRIGHT_SHIFT(x,shft) \ |
| 80 |
((ishift_temp = (x)) < 0 ? \ |
| 81 |
(ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \ |
| 82 |
(ishift_temp >> (shft))) |
| 83 |
#else |
| 84 |
#define ISHIFT_TEMPS |
| 85 |
#define IRIGHT_SHIFT(x,shft) ((x) >> (shft)) |
| 86 |
#endif |
| 87 |
|
| 88 |
|
| 89 |
METHODDEF(boolean) encode_mcu_DC_first JPP((j_compress_ptr cinfo, |
| 90 |
JBLOCKROW *MCU_data)); |
| 91 |
METHODDEF(boolean) encode_mcu_AC_first JPP((j_compress_ptr cinfo, |
| 92 |
JBLOCKROW *MCU_data)); |
| 93 |
METHODDEF(boolean) encode_mcu_DC_refine JPP((j_compress_ptr cinfo, |
| 94 |
JBLOCKROW *MCU_data)); |
| 95 |
METHODDEF(boolean) encode_mcu_AC_refine JPP((j_compress_ptr cinfo, |
| 96 |
JBLOCKROW *MCU_data)); |
| 97 |
METHODDEF(void) finish_pass_phuff JPP((j_compress_ptr cinfo)); |
| 98 |
METHODDEF(void) finish_pass_gather_phuff JPP((j_compress_ptr cinfo)); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
METHODDEF(void) |
| 106 |
start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics) |
| 107 |
{ |
| 108 |
phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; |
| 109 |
boolean is_DC_band; |
| 110 |
int ci, tbl; |
| 111 |
jpeg_component_info * compptr; |
| 112 |
|
| 113 |
entropy->cinfo = cinfo; |
| 114 |
entropy->gather_statistics = gather_statistics; |
| 115 |
|
| 116 |
is_DC_band = (cinfo->Ss == 0); |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
if (cinfo->Ah == 0) { |
| 122 |
if (is_DC_band) |
| 123 |
entropy->pub.encode_mcu = encode_mcu_DC_first; |
| 124 |
else |
| 125 |
entropy->pub.encode_mcu = encode_mcu_AC_first; |
| 126 |
} else { |
| 127 |
if (is_DC_band) |
| 128 |
entropy->pub.encode_mcu = encode_mcu_DC_refine; |
| 129 |
else { |
| 130 |
entropy->pub.encode_mcu = encode_mcu_AC_refine; |
| 131 |
|
| 132 |
if (entropy->bit_buffer == NULL) |
| 133 |
entropy->bit_buffer = (char *) |
| 134 |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 135 |
MAX_CORR_BITS * SIZEOF(char)); |
| 136 |
} |
| 137 |
} |
| 138 |
if (gather_statistics) |
| 139 |
entropy->pub.finish_pass = finish_pass_gather_phuff; |
| 140 |
else |
| 141 |
entropy->pub.finish_pass = finish_pass_phuff; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
| 147 |
compptr = cinfo->cur_comp_info[ci]; |
| 148 |
|
| 149 |
entropy->last_dc_val[ci] = 0; |
| 150 |
|
| 151 |
if (is_DC_band) { |
| 152 |
if (cinfo->Ah != 0) |
| 153 |
continue; |
| 154 |
tbl = compptr->dc_tbl_no; |
| 155 |
} else { |
| 156 |
entropy->ac_tbl_no = tbl = compptr->ac_tbl_no; |
| 157 |
} |
| 158 |
if (gather_statistics) { |
| 159 |
|
| 160 |
|
| 161 |
if (tbl < 0 || tbl >= NUM_HUFF_TBLS) |
| 162 |
ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl); |
| 163 |
|
| 164 |
|
| 165 |
if (entropy->count_ptrs[tbl] == NULL) |
| 166 |
entropy->count_ptrs[tbl] = (long *) |
| 167 |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 168 |
257 * SIZEOF(long)); |
| 169 |
MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long)); |
| 170 |
} else { |
| 171 |
|
| 172 |
|
| 173 |
jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl, |
| 174 |
& entropy->derived_tbls[tbl]); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
entropy->EOBRUN = 0; |
| 180 |
entropy->BE = 0; |
| 181 |
|
| 182 |
|
| 183 |
entropy->put_buffer = 0; |
| 184 |
entropy->put_bits = 0; |
| 185 |
|
| 186 |
|
| 187 |
entropy->restarts_to_go = cinfo->restart_interval; |
| 188 |
entropy->next_restart_num = 0; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
#define emit_byte(entropy,val) \ |
| 199 |
{ *(entropy)->next_output_byte++ = (JOCTET) (val); \ |
| 200 |
if (--(entropy)->free_in_buffer == 0) \ |
| 201 |
dump_buffer(entropy); } |
| 202 |
|
| 203 |
|
| 204 |
LOCAL(void) |
| 205 |
dump_buffer (phuff_entropy_ptr entropy) |
| 206 |
|
| 207 |
{ |
| 208 |
struct jpeg_destination_mgr * dest = entropy->cinfo->dest; |
| 209 |
|
| 210 |
if (! (*dest->empty_output_buffer) (entropy->cinfo)) |
| 211 |
ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND); |
| 212 |
|
| 213 |
entropy->next_output_byte = dest->next_output_byte; |
| 214 |
entropy->free_in_buffer = dest->free_in_buffer; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
INLINE |
| 227 |
LOCAL(void) |
| 228 |
emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size) |
| 229 |
|
| 230 |
{ |
| 231 |
|
| 232 |
register INT32 put_buffer = (INT32) code; |
| 233 |
register int put_bits = entropy->put_bits; |
| 234 |
|
| 235 |
|
| 236 |
if (size == 0) |
| 237 |
ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE); |
| 238 |
|
| 239 |
if (entropy->gather_statistics) |
| 240 |
return; |
| 241 |
|
| 242 |
put_buffer &= (((INT32) 1)<<size) - 1; |
| 243 |
|
| 244 |
put_bits += size; |
| 245 |
|
| 246 |
put_buffer <<= 24 - put_bits; |
| 247 |
|
| 248 |
put_buffer |= entropy->put_buffer; |
| 249 |
|
| 250 |
while (put_bits >= 8) { |
| 251 |
int c = (int) ((put_buffer >> 16) & 0xFF); |
| 252 |
|
| 253 |
emit_byte(entropy, c); |
| 254 |
if (c == 0xFF) { |
| 255 |
emit_byte(entropy, 0); |
| 256 |
} |
| 257 |
put_buffer <<= 8; |
| 258 |
put_bits -= 8; |
| 259 |
} |
| 260 |
|
| 261 |
entropy->put_buffer = put_buffer; |
| 262 |
entropy->put_bits = put_bits; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
LOCAL(void) |
| 267 |
flush_bits (phuff_entropy_ptr entropy) |
| 268 |
{ |
| 269 |
emit_bits(entropy, 0x7F, 7); |
| 270 |
entropy->put_buffer = 0; |
| 271 |
entropy->put_bits = 0; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
INLINE |
| 280 |
LOCAL(void) |
| 281 |
emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol) |
| 282 |
{ |
| 283 |
if (entropy->gather_statistics) |
| 284 |
entropy->count_ptrs[tbl_no][symbol]++; |
| 285 |
else { |
| 286 |
c_derived_tbl * tbl = entropy->derived_tbls[tbl_no]; |
| 287 |
emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
LOCAL(void) |
| 297 |
emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart, |
| 298 |
unsigned int nbits) |
| 299 |
{ |
| 300 |
if (entropy->gather_statistics) |
| 301 |
return; |
| 302 |
|
| 303 |
while (nbits > 0) { |
| 304 |
emit_bits(entropy, (unsigned int) (*bufstart), 1); |
| 305 |
bufstart++; |
| 306 |
nbits--; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
LOCAL(void) |
| 316 |
emit_eobrun (phuff_entropy_ptr entropy) |
| 317 |
{ |
| 318 |
register int temp, nbits; |
| 319 |
|
| 320 |
if (entropy->EOBRUN > 0) { |
| 321 |
temp = entropy->EOBRUN; |
| 322 |
nbits = 0; |
| 323 |
while ((temp >>= 1)) |
| 324 |
nbits++; |
| 325 |
|
| 326 |
if (nbits > 14) |
| 327 |
ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE); |
| 328 |
|
| 329 |
emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4); |
| 330 |
if (nbits) |
| 331 |
emit_bits(entropy, entropy->EOBRUN, nbits); |
| 332 |
|
| 333 |
entropy->EOBRUN = 0; |
| 334 |
|
| 335 |
|
| 336 |
emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE); |
| 337 |
entropy->BE = 0; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
LOCAL(void) |
| 347 |
emit_restart (phuff_entropy_ptr entropy, int restart_num) |
| 348 |
{ |
| 349 |
int ci; |
| 350 |
|
| 351 |
emit_eobrun(entropy); |
| 352 |
|
| 353 |
if (! entropy->gather_statistics) { |
| 354 |
flush_bits(entropy); |
| 355 |
emit_byte(entropy, 0xFF); |
| 356 |
emit_byte(entropy, JPEG_RST0 + restart_num); |
| 357 |
} |
| 358 |
|
| 359 |
if (entropy->cinfo->Ss == 0) { |
| 360 |
|
| 361 |
for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++) |
| 362 |
entropy->last_dc_val[ci] = 0; |
| 363 |
} else { |
| 364 |
|
| 365 |
entropy->EOBRUN = 0; |
| 366 |
entropy->BE = 0; |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
METHODDEF(boolean) |
| 377 |
encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
| 378 |
{ |
| 379 |
phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; |
| 380 |
register int temp, temp2; |
| 381 |
register int nbits; |
| 382 |
int blkn, ci; |
| 383 |
int Al = cinfo->Al; |
| 384 |
JBLOCKROW block; |
| 385 |
jpeg_component_info * compptr; |
| 386 |
ISHIFT_TEMPS |
| 387 |
|
| 388 |
entropy->next_output_byte = cinfo->dest->next_output_byte; |
| 389 |
entropy->free_in_buffer = cinfo->dest->free_in_buffer; |
| 390 |
|
| 391 |
|
| 392 |
if (cinfo->restart_interval) |
| 393 |
if (entropy->restarts_to_go == 0) |
| 394 |
emit_restart(entropy, entropy->next_restart_num); |
| 395 |
|
| 396 |
|
| 397 |
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
| 398 |
block = MCU_data[blkn]; |
| 399 |
ci = cinfo->MCU_membership[blkn]; |
| 400 |
compptr = cinfo->cur_comp_info[ci]; |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al); |
| 406 |
|
| 407 |
|
| 408 |
temp = temp2 - entropy->last_dc_val[ci]; |
| 409 |
entropy->last_dc_val[ci] = temp2; |
| 410 |
|
| 411 |
|
| 412 |
temp2 = temp; |
| 413 |
if (temp < 0) { |
| 414 |
temp = -temp; |
| 415 |
|
| 416 |
|
| 417 |
temp2--; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
nbits = 0; |
| 422 |
while (temp) { |
| 423 |
nbits++; |
| 424 |
temp >>= 1; |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
if (nbits > MAX_COEF_BITS+1) |
| 430 |
ERREXIT(cinfo, JERR_BAD_DCT_COEF); |
| 431 |
|
| 432 |
|
| 433 |
emit_symbol(entropy, compptr->dc_tbl_no, nbits); |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
if (nbits) |
| 438 |
emit_bits(entropy, (unsigned int) temp2, nbits); |
| 439 |
} |
| 440 |
|
| 441 |
cinfo->dest->next_output_byte = entropy->next_output_byte; |
| 442 |
cinfo->dest->free_in_buffer = entropy->free_in_buffer; |
| 443 |
|
| 444 |
|
| 445 |
if (cinfo->restart_interval) { |
| 446 |
if (entropy->restarts_to_go == 0) { |
| 447 |
entropy->restarts_to_go = cinfo->restart_interval; |
| 448 |
entropy->next_restart_num++; |
| 449 |
entropy->next_restart_num &= 7; |
| 450 |
} |
| 451 |
entropy->restarts_to_go--; |
| 452 |
} |
| 453 |
|
| 454 |
return TRUE; |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
METHODDEF(boolean) |
| 464 |
encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
| 465 |
{ |
| 466 |
phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; |
| 467 |
register int temp, temp2; |
| 468 |
register int nbits; |
| 469 |
register int r, k; |
| 470 |
int Se = cinfo->Se; |
| 471 |
int Al = cinfo->Al; |
| 472 |
JBLOCKROW block; |
| 473 |
|
| 474 |
entropy->next_output_byte = cinfo->dest->next_output_byte; |
| 475 |
entropy->free_in_buffer = cinfo->dest->free_in_buffer; |
| 476 |
|
| 477 |
|
| 478 |
if (cinfo->restart_interval) |
| 479 |
if (entropy->restarts_to_go == 0) |
| 480 |
emit_restart(entropy, entropy->next_restart_num); |
| 481 |
|
| 482 |
|
| 483 |
block = MCU_data[0]; |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
r = 0; |
| 488 |
|
| 489 |
for (k = cinfo->Ss; k <= Se; k++) { |
| 490 |
if ((temp = (*block)[jpeg_natural_order[k]]) == 0) { |
| 491 |
r++; |
| 492 |
continue; |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
if (temp < 0) { |
| 500 |
temp = -temp; |
| 501 |
temp >>= Al; |
| 502 |
|
| 503 |
temp2 = ~temp; |
| 504 |
} else { |
| 505 |
temp >>= Al; |
| 506 |
temp2 = temp; |
| 507 |
} |
| 508 |
|
| 509 |
if (temp == 0) { |
| 510 |
r++; |
| 511 |
continue; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
if (entropy->EOBRUN > 0) |
| 516 |
emit_eobrun(entropy); |
| 517 |
|
| 518 |
while (r > 15) { |
| 519 |
emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); |
| 520 |
r -= 16; |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
nbits = 1; |
| 525 |
while ((temp >>= 1)) |
| 526 |
nbits++; |
| 527 |
|
| 528 |
if (nbits > MAX_COEF_BITS) |
| 529 |
ERREXIT(cinfo, JERR_BAD_DCT_COEF); |
| 530 |
|
| 531 |
|
| 532 |
emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits); |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
emit_bits(entropy, (unsigned int) temp2, nbits); |
| 537 |
|
| 538 |
r = 0; |
| 539 |
} |
| 540 |
|
| 541 |
if (r > 0) { |
| 542 |
entropy->EOBRUN++; |
| 543 |
if (entropy->EOBRUN == 0x7FFF) |
| 544 |
emit_eobrun(entropy); |
| 545 |
} |
| 546 |
|
| 547 |
cinfo->dest->next_output_byte = entropy->next_output_byte; |
| 548 |
cinfo->dest->free_in_buffer = entropy->free_in_buffer; |
| 549 |
|
| 550 |
|
| 551 |
if (cinfo->restart_interval) { |
| 552 |
if (entropy->restarts_to_go == 0) { |
| 553 |
entropy->restarts_to_go = cinfo->restart_interval; |
| 554 |
entropy->next_restart_num++; |
| 555 |
entropy->next_restart_num &= 7; |
| 556 |
} |
| 557 |
entropy->restarts_to_go--; |
| 558 |
} |
| 559 |
|
| 560 |
return TRUE; |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
METHODDEF(boolean) |
| 571 |
encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
| 572 |
{ |
| 573 |
phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; |
| 574 |
register int temp; |
| 575 |
int blkn; |
| 576 |
int Al = cinfo->Al; |
| 577 |
JBLOCKROW block; |
| 578 |
|
| 579 |
entropy->next_output_byte = cinfo->dest->next_output_byte; |
| 580 |
entropy->free_in_buffer = cinfo->dest->free_in_buffer; |
| 581 |
|
| 582 |
|
| 583 |
if (cinfo->restart_interval) |
| 584 |
if (entropy->restarts_to_go == 0) |
| 585 |
emit_restart(entropy, entropy->next_restart_num); |
| 586 |
|
| 587 |
|
| 588 |
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
| 589 |
block = MCU_data[blkn]; |
| 590 |
|
| 591 |
|
| 592 |
temp = (*block)[0]; |
| 593 |
emit_bits(entropy, (unsigned int) (temp >> Al), 1); |
| 594 |
} |
| 595 |
|
| 596 |
cinfo->dest->next_output_byte = entropy->next_output_byte; |
| 597 |
cinfo->dest->free_in_buffer = entropy->free_in_buffer; |
| 598 |
|
| 599 |
|
| 600 |
if (cinfo->restart_interval) { |
| 601 |
if (entropy->restarts_to_go == 0) { |
| 602 |
entropy->restarts_to_go = cinfo->restart_interval; |
| 603 |
entropy->next_restart_num++; |
| 604 |
entropy->next_restart_num &= 7; |
| 605 |
} |
| 606 |
entropy->restarts_to_go--; |
| 607 |
} |
| 608 |
|
| 609 |
return TRUE; |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
METHODDEF(boolean) |
| 618 |
encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
| 619 |
{ |
| 620 |
phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; |
| 621 |
register int temp; |
| 622 |
register int r, k; |
| 623 |
int EOB; |
| 624 |
char *BR_buffer; |
| 625 |
unsigned int BR; |
| 626 |
int Se = cinfo->Se; |
| 627 |
int Al = cinfo->Al; |
| 628 |
JBLOCKROW block; |
| 629 |
int absvalues[DCTSIZE2]; |
| 630 |
|
| 631 |
entropy->next_output_byte = cinfo->dest->next_output_byte; |
| 632 |
entropy->free_in_buffer = cinfo->dest->free_in_buffer; |
| 633 |
|
| 634 |
|
| 635 |
if (cinfo->restart_interval) |
| 636 |
if (entropy->restarts_to_go == 0) |
| 637 |
emit_restart(entropy, entropy->next_restart_num); |
| 638 |
|
| 639 |
|
| 640 |
block = MCU_data[0]; |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
EOB = 0; |
| 646 |
for (k = cinfo->Ss; k <= Se; k++) { |
| 647 |
temp = (*block)[jpeg_natural_order[k]]; |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
if (temp < 0) |
| 653 |
temp = -temp; |
| 654 |
temp >>= Al; |
| 655 |
absvalues[k] = temp; |
| 656 |
if (temp == 1) |
| 657 |
EOB = k; |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
r = 0; |
| 663 |
BR = 0; |
| 664 |
BR_buffer = entropy->bit_buffer + entropy->BE; |
| 665 |
|
| 666 |
for (k = cinfo->Ss; k <= Se; k++) { |
| 667 |
if ((temp = absvalues[k]) == 0) { |
| 668 |
r++; |
| 669 |
continue; |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
while (r > 15 && k <= EOB) { |
| 674 |
|
| 675 |
emit_eobrun(entropy); |
| 676 |
|
| 677 |
emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); |
| 678 |
r -= 16; |
| 679 |
|
| 680 |
emit_buffered_bits(entropy, BR_buffer, BR); |
| 681 |
BR_buffer = entropy->bit_buffer; |
| 682 |
BR = 0; |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
if (temp > 1) { |
| 691 |
|
| 692 |
BR_buffer[BR++] = (char) (temp & 1); |
| 693 |
continue; |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
emit_eobrun(entropy); |
| 698 |
|
| 699 |
|
| 700 |
emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1); |
| 701 |
|
| 702 |
|
| 703 |
temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1; |
| 704 |
emit_bits(entropy, (unsigned int) temp, 1); |
| 705 |
|
| 706 |
|
| 707 |
emit_buffered_bits(entropy, BR_buffer, BR); |
| 708 |
BR_buffer = entropy->bit_buffer; |
| 709 |
BR = 0; |
| 710 |
r = 0; |
| 711 |
} |
| 712 |
|
| 713 |
if (r > 0 || BR > 0) { |
| 714 |
entropy->EOBRUN++; |
| 715 |
entropy->BE += BR; |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1)) |
| 721 |
emit_eobrun(entropy); |
| 722 |
} |
| 723 |
|
| 724 |
cinfo->dest->next_output_byte = entropy->next_output_byte; |
| 725 |
cinfo->dest->free_in_buffer = entropy->free_in_buffer; |
| 726 |
|
| 727 |
|
| 728 |
if (cinfo->restart_interval) { |
| 729 |
if (entropy->restarts_to_go == 0) { |
| 730 |
entropy->restarts_to_go = cinfo->restart_interval; |
| 731 |
entropy->next_restart_num++; |
| 732 |
entropy->next_restart_num &= 7; |
| 733 |
} |
| 734 |
entropy->restarts_to_go--; |
| 735 |
} |
| 736 |
|
| 737 |
return TRUE; |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
METHODDEF(void) |
| 746 |
finish_pass_phuff (j_compress_ptr cinfo) |
| 747 |
{ |
| 748 |
phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; |
| 749 |
|
| 750 |
entropy->next_output_byte = cinfo->dest->next_output_byte; |
| 751 |
entropy->free_in_buffer = cinfo->dest->free_in_buffer; |
| 752 |
|
| 753 |
|
| 754 |
emit_eobrun(entropy); |
| 755 |
flush_bits(entropy); |
| 756 |
|
| 757 |
cinfo->dest->next_output_byte = entropy->next_output_byte; |
| 758 |
cinfo->dest->free_in_buffer = entropy->free_in_buffer; |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
METHODDEF(void) |
| 767 |
finish_pass_gather_phuff (j_compress_ptr cinfo) |
| 768 |
{ |
| 769 |
phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; |
| 770 |
boolean is_DC_band; |
| 771 |
int ci, tbl; |
| 772 |
jpeg_component_info * compptr; |
| 773 |
JHUFF_TBL **htblptr; |
| 774 |
boolean did[NUM_HUFF_TBLS]; |
| 775 |
|
| 776 |
|
| 777 |
emit_eobrun(entropy); |
| 778 |
|
| 779 |
is_DC_band = (cinfo->Ss == 0); |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
MEMZERO(did, SIZEOF(did)); |
| 785 |
|
| 786 |
for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
| 787 |
compptr = cinfo->cur_comp_info[ci]; |
| 788 |
if (is_DC_band) { |
| 789 |
if (cinfo->Ah != 0) |
| 790 |
continue; |
| 791 |
tbl = compptr->dc_tbl_no; |
| 792 |
} else { |
| 793 |
tbl = compptr->ac_tbl_no; |
| 794 |
} |
| 795 |
if (! did[tbl]) { |
| 796 |
if (is_DC_band) |
| 797 |
htblptr = & cinfo->dc_huff_tbl_ptrs[tbl]; |
| 798 |
else |
| 799 |
htblptr = & cinfo->ac_huff_tbl_ptrs[tbl]; |
| 800 |
if (*htblptr == NULL) |
| 801 |
*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo); |
| 802 |
jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]); |
| 803 |
did[tbl] = TRUE; |
| 804 |
} |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
GLOBAL(void) |
| 814 |
jinit_phuff_encoder (j_compress_ptr cinfo) |
| 815 |
{ |
| 816 |
phuff_entropy_ptr entropy; |
| 817 |
int i; |
| 818 |
|
| 819 |
entropy = (phuff_entropy_ptr) |
| 820 |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 821 |
SIZEOF(phuff_entropy_encoder)); |
| 822 |
cinfo->entropy = (struct jpeg_entropy_encoder *) entropy; |
| 823 |
entropy->pub.start_pass = start_pass_phuff; |
| 824 |
|
| 825 |
|
| 826 |
for (i = 0; i < NUM_HUFF_TBLS; i++) { |
| 827 |
entropy->derived_tbls[i] = NULL; |
| 828 |
entropy->count_ptrs[i] = NULL; |
| 829 |
} |
| 830 |
entropy->bit_buffer = NULL; |
| 831 |
} |
| 832 |
|
| 833 |
#endif |