00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #define JPEG_INTERNALS
00024 #include "jinclude.h"
00025 #include "jpeglib.h"
00026
00027
00028
00029
00030
00031
00032
00033
00034 #if BITS_IN_JSAMPLE == 8
00035 #define MAX_COEF_BITS 10
00036 #else
00037 #define MAX_COEF_BITS 14
00038 #endif
00039
00040
00041
00042 typedef struct {
00043 unsigned int ehufco[256];
00044 char ehufsi[256];
00045
00046 } c_derived_tbl;
00047
00048
00049
00050
00051
00052
00053
00054
00055 typedef struct {
00056 INT32 put_buffer;
00057 int put_bits;
00058 int last_dc_val[MAX_COMPS_IN_SCAN];
00059 } savable_state;
00060
00061
00062
00063
00064
00065
00066 #ifndef NO_STRUCT_ASSIGN
00067 #define ASSIGN_STATE(dest,src) ((dest) = (src))
00068 #else
00069 #if MAX_COMPS_IN_SCAN == 4
00070 #define ASSIGN_STATE(dest,src) \
00071 ((dest).put_buffer = (src).put_buffer, \
00072 (dest).put_bits = (src).put_bits, \
00073 (dest).last_dc_val[0] = (src).last_dc_val[0], \
00074 (dest).last_dc_val[1] = (src).last_dc_val[1], \
00075 (dest).last_dc_val[2] = (src).last_dc_val[2], \
00076 (dest).last_dc_val[3] = (src).last_dc_val[3])
00077 #endif
00078 #endif
00079
00080
00081 typedef struct {
00082 struct jpeg_entropy_encoder pub;
00083
00084 savable_state saved;
00085
00086
00087 unsigned int restarts_to_go;
00088 int next_restart_num;
00089
00090
00091 c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
00092 c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
00093
00094
00095 long * dc_count_ptrs[NUM_HUFF_TBLS];
00096 long * ac_count_ptrs[NUM_HUFF_TBLS];
00097
00098
00099
00100
00101 boolean gather_statistics;
00102
00103
00104
00105 JOCTET * next_output_byte;
00106 size_t free_in_buffer;
00107 j_compress_ptr cinfo;
00108
00109
00110 int ac_tbl_no;
00111 unsigned int EOBRUN;
00112 unsigned int BE;
00113 char * bit_buffer;
00114
00115 } huff_entropy_encoder;
00116
00117 typedef huff_entropy_encoder * huff_entropy_ptr;
00118
00119
00120
00121
00122
00123 typedef struct {
00124 JOCTET * next_output_byte;
00125 size_t free_in_buffer;
00126 savable_state cur;
00127 j_compress_ptr cinfo;
00128 } working_state;
00129
00130
00131
00132
00133
00134
00135
00136 #define MAX_CORR_BITS 1000
00137
00138
00139
00140
00141
00142
00143 #ifdef RIGHT_SHIFT_IS_UNSIGNED
00144 #define ISHIFT_TEMPS int ishift_temp;
00145 #define IRIGHT_SHIFT(x,shft) \
00146 ((ishift_temp = (x)) < 0 ? \
00147 (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
00148 (ishift_temp >> (shft)))
00149 #else
00150 #define ISHIFT_TEMPS
00151 #define IRIGHT_SHIFT(x,shft) ((x) >> (shft))
00152 #endif
00153
00154
00155
00156
00157
00158
00159
00160 LOCAL(void)
00161 jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
00162 c_derived_tbl ** pdtbl)
00163 {
00164 JHUFF_TBL *htbl;
00165 c_derived_tbl *dtbl;
00166 int p, i, l, lastp, si, maxsymbol;
00167 char huffsize[257];
00168 unsigned int huffcode[257];
00169 unsigned int code;
00170
00171
00172
00173
00174
00175
00176 if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
00177 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
00178 htbl =
00179 isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
00180 if (htbl == NULL)
00181 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
00182
00183
00184 if (*pdtbl == NULL)
00185 *pdtbl = (c_derived_tbl *)
00186 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00187 SIZEOF(c_derived_tbl));
00188 dtbl = *pdtbl;
00189
00190
00191
00192 p = 0;
00193 for (l = 1; l <= 16; l++) {
00194 i = (int) htbl->bits[l];
00195 if (i < 0 || p + i > 256)
00196 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
00197 while (i--)
00198 huffsize[p++] = (char) l;
00199 }
00200 huffsize[p] = 0;
00201 lastp = p;
00202
00203
00204
00205
00206 code = 0;
00207 si = huffsize[0];
00208 p = 0;
00209 while (huffsize[p]) {
00210 while (((int) huffsize[p]) == si) {
00211 huffcode[p++] = code;
00212 code++;
00213 }
00214
00215
00216
00217 if (((INT32) code) >= (((INT32) 1) << si))
00218 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
00219 code <<= 1;
00220 si++;
00221 }
00222
00223
00224
00225
00226
00227
00228
00229
00230 MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
00231
00232
00233
00234
00235
00236
00237 maxsymbol = isDC ? 15 : 255;
00238
00239 for (p = 0; p < lastp; p++) {
00240 i = htbl->huffval[p];
00241 if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
00242 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
00243 dtbl->ehufco[i] = huffcode[p];
00244 dtbl->ehufsi[i] = huffsize[p];
00245 }
00246 }
00247
00248
00249
00250
00251
00252
00253
00254
00255 #define emit_byte_s(state,val,action) \
00256 { *(state)->next_output_byte++ = (JOCTET) (val); \
00257 if (--(state)->free_in_buffer == 0) \
00258 if (! dump_buffer_s(state)) \
00259 { action; } }
00260
00261
00262 #define emit_byte_e(entropy,val) \
00263 { *(entropy)->next_output_byte++ = (JOCTET) (val); \
00264 if (--(entropy)->free_in_buffer == 0) \
00265 dump_buffer_e(entropy); }
00266
00267
00268 LOCAL(boolean)
00269 dump_buffer_s (working_state * state)
00270
00271 {
00272 struct jpeg_destination_mgr * dest = state->cinfo->dest;
00273
00274 if (! (*dest->empty_output_buffer) (state->cinfo))
00275 return FALSE;
00276
00277 state->next_output_byte = dest->next_output_byte;
00278 state->free_in_buffer = dest->free_in_buffer;
00279 return TRUE;
00280 }
00281
00282
00283 LOCAL(void)
00284 dump_buffer_e (huff_entropy_ptr entropy)
00285
00286 {
00287 struct jpeg_destination_mgr * dest = entropy->cinfo->dest;
00288
00289 if (! (*dest->empty_output_buffer) (entropy->cinfo))
00290 ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
00291
00292 entropy->next_output_byte = dest->next_output_byte;
00293 entropy->free_in_buffer = dest->free_in_buffer;
00294 }
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305 INLINE
00306 LOCAL(boolean)
00307 emit_bits_s (working_state * state, unsigned int code, int size)
00308
00309 {
00310
00311 register INT32 put_buffer = (INT32) code;
00312 register int put_bits = state->cur.put_bits;
00313
00314
00315 if (size == 0)
00316 ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
00317
00318 put_buffer &= (((INT32) 1)<<size) - 1;
00319
00320 put_bits += size;
00321
00322 put_buffer <<= 24 - put_bits;
00323
00324 put_buffer |= state->cur.put_buffer;
00325
00326 while (put_bits >= 8) {
00327 int c = (int) ((put_buffer >> 16) & 0xFF);
00328
00329 emit_byte_s(state, c, return FALSE);
00330 if (c == 0xFF) {
00331 emit_byte_s(state, 0, return FALSE);
00332 }
00333 put_buffer <<= 8;
00334 put_bits -= 8;
00335 }
00336
00337 state->cur.put_buffer = put_buffer;
00338 state->cur.put_bits = put_bits;
00339
00340 return TRUE;
00341 }
00342
00343
00344 INLINE
00345 LOCAL(void)
00346 emit_bits_e (huff_entropy_ptr entropy, unsigned int code, int size)
00347
00348 {
00349
00350 register INT32 put_buffer = (INT32) code;
00351 register int put_bits = entropy->saved.put_bits;
00352
00353
00354 if (size == 0)
00355 ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
00356
00357 if (entropy->gather_statistics)
00358 return;
00359
00360 put_buffer &= (((INT32) 1)<<size) - 1;
00361
00362 put_bits += size;
00363
00364 put_buffer <<= 24 - put_bits;
00365
00366
00367 put_buffer |= entropy->saved.put_buffer;
00368
00369 while (put_bits >= 8) {
00370 int c = (int) ((put_buffer >> 16) & 0xFF);
00371
00372 emit_byte_e(entropy, c);
00373 if (c == 0xFF) {
00374 emit_byte_e(entropy, 0);
00375 }
00376 put_buffer <<= 8;
00377 put_bits -= 8;
00378 }
00379
00380 entropy->saved.put_buffer = put_buffer;
00381 entropy->saved.put_bits = put_bits;
00382 }
00383
00384
00385 LOCAL(boolean)
00386 flush_bits_s (working_state * state)
00387 {
00388 if (! emit_bits_s(state, 0x7F, 7))
00389 return FALSE;
00390 state->cur.put_buffer = 0;
00391 state->cur.put_bits = 0;
00392 return TRUE;
00393 }
00394
00395
00396 LOCAL(void)
00397 flush_bits_e (huff_entropy_ptr entropy)
00398 {
00399 emit_bits_e(entropy, 0x7F, 7);
00400 entropy->saved.put_buffer = 0;
00401 entropy->saved.put_bits = 0;
00402 }
00403
00404
00405
00406
00407
00408
00409 INLINE
00410 LOCAL(void)
00411 emit_dc_symbol (huff_entropy_ptr entropy, int tbl_no, int symbol)
00412 {
00413 if (entropy->gather_statistics)
00414 entropy->dc_count_ptrs[tbl_no][symbol]++;
00415 else {
00416 c_derived_tbl * tbl = entropy->dc_derived_tbls[tbl_no];
00417 emit_bits_e(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
00418 }
00419 }
00420
00421
00422 INLINE
00423 LOCAL(void)
00424 emit_ac_symbol (huff_entropy_ptr entropy, int tbl_no, int symbol)
00425 {
00426 if (entropy->gather_statistics)
00427 entropy->ac_count_ptrs[tbl_no][symbol]++;
00428 else {
00429 c_derived_tbl * tbl = entropy->ac_derived_tbls[tbl_no];
00430 emit_bits_e(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
00431 }
00432 }
00433
00434
00435
00436
00437
00438
00439 LOCAL(void)
00440 emit_buffered_bits (huff_entropy_ptr entropy, char * bufstart,
00441 unsigned int nbits)
00442 {
00443 if (entropy->gather_statistics)
00444 return;
00445
00446 while (nbits > 0) {
00447 emit_bits_e(entropy, (unsigned int) (*bufstart), 1);
00448 bufstart++;
00449 nbits--;
00450 }
00451 }
00452
00453
00454
00455
00456
00457
00458 LOCAL(void)
00459 emit_eobrun (huff_entropy_ptr entropy)
00460 {
00461 register int temp, nbits;
00462
00463 if (entropy->EOBRUN > 0) {
00464 temp = entropy->EOBRUN;
00465 nbits = 0;
00466 while ((temp >>= 1))
00467 nbits++;
00468
00469 if (nbits > 14)
00470 ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
00471
00472 emit_ac_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
00473 if (nbits)
00474 emit_bits_e(entropy, entropy->EOBRUN, nbits);
00475
00476 entropy->EOBRUN = 0;
00477
00478
00479 emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
00480 entropy->BE = 0;
00481 }
00482 }
00483
00484
00485
00486
00487
00488
00489 LOCAL(boolean)
00490 emit_restart_s (working_state * state, int restart_num)
00491 {
00492 int ci;
00493
00494 if (! flush_bits_s(state))
00495 return FALSE;
00496
00497 emit_byte_s(state, 0xFF, return FALSE);
00498 emit_byte_s(state, JPEG_RST0 + restart_num, return FALSE);
00499
00500
00501 for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
00502 state->cur.last_dc_val[ci] = 0;
00503
00504
00505
00506 return TRUE;
00507 }
00508
00509
00510 LOCAL(void)
00511 emit_restart_e (huff_entropy_ptr entropy, int restart_num)
00512 {
00513 int ci;
00514
00515 emit_eobrun(entropy);
00516
00517 if (! entropy->gather_statistics) {
00518 flush_bits_e(entropy);
00519 emit_byte_e(entropy, 0xFF);
00520 emit_byte_e(entropy, JPEG_RST0 + restart_num);
00521 }
00522
00523 if (entropy->cinfo->Ss == 0) {
00524
00525 for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
00526 entropy->saved.last_dc_val[ci] = 0;
00527 } else {
00528
00529 entropy->EOBRUN = 0;
00530 entropy->BE = 0;
00531 }
00532 }
00533
00534
00535
00536
00537
00538
00539
00540 METHODDEF(boolean)
00541 encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
00542 {
00543 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
00544 register int temp, temp2;
00545 register int nbits;
00546 int blkn, ci;
00547 int Al = cinfo->Al;
00548 JBLOCKROW block;
00549 jpeg_component_info * compptr;
00550 ISHIFT_TEMPS
00551
00552 entropy->next_output_byte = cinfo->dest->next_output_byte;
00553 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
00554
00555
00556 if (cinfo->restart_interval)
00557 if (entropy->restarts_to_go == 0)
00558 emit_restart_e(entropy, entropy->next_restart_num);
00559
00560
00561 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
00562 block = MCU_data[blkn];
00563 ci = cinfo->MCU_membership[blkn];
00564 compptr = cinfo->cur_comp_info[ci];
00565
00566
00567
00568
00569 temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);
00570
00571
00572 temp = temp2 - entropy->saved.last_dc_val[ci];
00573 entropy->saved.last_dc_val[ci] = temp2;
00574
00575
00576 temp2 = temp;
00577 if (temp < 0) {
00578 temp = -temp;
00579
00580
00581 temp2--;
00582 }
00583
00584
00585 nbits = 0;
00586 while (temp) {
00587 nbits++;
00588 temp >>= 1;
00589 }
00590
00591
00592
00593 if (nbits > MAX_COEF_BITS+1)
00594 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
00595
00596
00597 emit_dc_symbol(entropy, compptr->dc_tbl_no, nbits);
00598
00599
00600
00601 if (nbits)
00602 emit_bits_e(entropy, (unsigned int) temp2, nbits);
00603 }
00604
00605 cinfo->dest->next_output_byte = entropy->next_output_byte;
00606 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
00607
00608
00609 if (cinfo->restart_interval) {
00610 if (entropy->restarts_to_go == 0) {
00611 entropy->restarts_to_go = cinfo->restart_interval;
00612 entropy->next_restart_num++;
00613 entropy->next_restart_num &= 7;
00614 }
00615 entropy->restarts_to_go--;
00616 }
00617
00618 return TRUE;
00619 }
00620
00621
00622
00623
00624
00625
00626
00627 METHODDEF(boolean)
00628 encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
00629 {
00630 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
00631 register int temp, temp2;
00632 register int nbits;
00633 register int r, k;
00634 int Se, Al;
00635 const int * natural_order;
00636 JBLOCKROW block;
00637
00638 entropy->next_output_byte = cinfo->dest->next_output_byte;
00639 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
00640
00641
00642 if (cinfo->restart_interval)
00643 if (entropy->restarts_to_go == 0)
00644 emit_restart_e(entropy, entropy->next_restart_num);
00645
00646 Se = cinfo->Se;
00647 Al = cinfo->Al;
00648 natural_order = cinfo->natural_order;
00649
00650
00651 block = MCU_data[0];
00652
00653
00654
00655 r = 0;
00656
00657 for (k = cinfo->Ss; k <= Se; k++) {
00658 if ((temp = (*block)[natural_order[k]]) == 0) {
00659 r++;
00660 continue;
00661 }
00662
00663
00664
00665
00666
00667 if (temp < 0) {
00668 temp = -temp;
00669 temp >>= Al;
00670
00671 temp2 = ~temp;
00672 } else {
00673 temp >>= Al;
00674 temp2 = temp;
00675 }
00676
00677 if (temp == 0) {
00678 r++;
00679 continue;
00680 }
00681
00682
00683 if (entropy->EOBRUN > 0)
00684 emit_eobrun(entropy);
00685
00686 while (r > 15) {
00687 emit_ac_symbol(entropy, entropy->ac_tbl_no, 0xF0);
00688 r -= 16;
00689 }
00690
00691
00692 nbits = 1;
00693 while ((temp >>= 1))
00694 nbits++;
00695
00696 if (nbits > MAX_COEF_BITS)
00697 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
00698
00699
00700 emit_ac_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);
00701
00702
00703
00704 emit_bits_e(entropy, (unsigned int) temp2, nbits);
00705
00706 r = 0;
00707 }
00708
00709 if (r > 0) {
00710 entropy->EOBRUN++;
00711 if (entropy->EOBRUN == 0x7FFF)
00712 emit_eobrun(entropy);
00713 }
00714
00715 cinfo->dest->next_output_byte = entropy->next_output_byte;
00716 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
00717
00718
00719 if (cinfo->restart_interval) {
00720 if (entropy->restarts_to_go == 0) {
00721 entropy->restarts_to_go = cinfo->restart_interval;
00722 entropy->next_restart_num++;
00723 entropy->next_restart_num &= 7;
00724 }
00725 entropy->restarts_to_go--;
00726 }
00727
00728 return TRUE;
00729 }
00730
00731
00732
00733
00734
00735
00736
00737
00738 METHODDEF(boolean)
00739 encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
00740 {
00741 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
00742 register int temp;
00743 int blkn;
00744 int Al = cinfo->Al;
00745 JBLOCKROW block;
00746
00747 entropy->next_output_byte = cinfo->dest->next_output_byte;
00748 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
00749
00750
00751 if (cinfo->restart_interval)
00752 if (entropy->restarts_to_go == 0)
00753 emit_restart_e(entropy, entropy->next_restart_num);
00754
00755
00756 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
00757 block = MCU_data[blkn];
00758
00759
00760 temp = (*block)[0];
00761 emit_bits_e(entropy, (unsigned int) (temp >> Al), 1);
00762 }
00763
00764 cinfo->dest->next_output_byte = entropy->next_output_byte;
00765 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
00766
00767
00768 if (cinfo->restart_interval) {
00769 if (entropy->restarts_to_go == 0) {
00770 entropy->restarts_to_go = cinfo->restart_interval;
00771 entropy->next_restart_num++;
00772 entropy->next_restart_num &= 7;
00773 }
00774 entropy->restarts_to_go--;
00775 }
00776
00777 return TRUE;
00778 }
00779
00780
00781
00782
00783
00784
00785 METHODDEF(boolean)
00786 encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
00787 {
00788 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
00789 register int temp;
00790 register int r, k;
00791 int EOB;
00792 char *BR_buffer;
00793 unsigned int BR;
00794 int Se, Al;
00795 const int * natural_order;
00796 JBLOCKROW block;
00797 int absvalues[DCTSIZE2];
00798
00799 entropy->next_output_byte = cinfo->dest->next_output_byte;
00800 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
00801
00802
00803 if (cinfo->restart_interval)
00804 if (entropy->restarts_to_go == 0)
00805 emit_restart_e(entropy, entropy->next_restart_num);
00806
00807 Se = cinfo->Se;
00808 Al = cinfo->Al;
00809 natural_order = cinfo->natural_order;
00810
00811
00812 block = MCU_data[0];
00813
00814
00815
00816
00817 EOB = 0;
00818 for (k = cinfo->Ss; k <= Se; k++) {
00819 temp = (*block)[natural_order[k]];
00820
00821
00822
00823
00824 if (temp < 0)
00825 temp = -temp;
00826 temp >>= Al;
00827 absvalues[k] = temp;
00828 if (temp == 1)
00829 EOB = k;
00830 }
00831
00832
00833
00834 r = 0;
00835 BR = 0;
00836 BR_buffer = entropy->bit_buffer + entropy->BE;
00837
00838 for (k = cinfo->Ss; k <= Se; k++) {
00839 if ((temp = absvalues[k]) == 0) {
00840 r++;
00841 continue;
00842 }
00843
00844
00845 while (r > 15 && k <= EOB) {
00846
00847 emit_eobrun(entropy);
00848
00849 emit_ac_symbol(entropy, entropy->ac_tbl_no, 0xF0);
00850 r -= 16;
00851
00852 emit_buffered_bits(entropy, BR_buffer, BR);
00853 BR_buffer = entropy->bit_buffer;
00854 BR = 0;
00855 }
00856
00857
00858
00859
00860
00861
00862 if (temp > 1) {
00863
00864 BR_buffer[BR++] = (char) (temp & 1);
00865 continue;
00866 }
00867
00868
00869 emit_eobrun(entropy);
00870
00871
00872 emit_ac_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);
00873
00874
00875 temp = ((*block)[natural_order[k]] < 0) ? 0 : 1;
00876 emit_bits_e(entropy, (unsigned int) temp, 1);
00877
00878
00879 emit_buffered_bits(entropy, BR_buffer, BR);
00880 BR_buffer = entropy->bit_buffer;
00881 BR = 0;
00882 r = 0;
00883 }
00884
00885 if (r > 0 || BR > 0) {
00886 entropy->EOBRUN++;
00887 entropy->BE += BR;
00888
00889
00890
00891
00892 if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))
00893 emit_eobrun(entropy);
00894 }
00895
00896 cinfo->dest->next_output_byte = entropy->next_output_byte;
00897 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
00898
00899
00900 if (cinfo->restart_interval) {
00901 if (entropy->restarts_to_go == 0) {
00902 entropy->restarts_to_go = cinfo->restart_interval;
00903 entropy->next_restart_num++;
00904 entropy->next_restart_num &= 7;
00905 }
00906 entropy->restarts_to_go--;
00907 }
00908
00909 return TRUE;
00910 }
00911
00912
00913
00914
00915 LOCAL(boolean)
00916 encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
00917 c_derived_tbl *dctbl, c_derived_tbl *actbl)
00918 {
00919 register int temp, temp2;
00920 register int nbits;
00921 register int k, r, i;
00922 int Se = state->cinfo->lim_Se;
00923 const int * natural_order = state->cinfo->natural_order;
00924
00925
00926
00927 temp = temp2 = block[0] - last_dc_val;
00928
00929 if (temp < 0) {
00930 temp = -temp;
00931
00932
00933 temp2--;
00934 }
00935
00936
00937 nbits = 0;
00938 while (temp) {
00939 nbits++;
00940 temp >>= 1;
00941 }
00942
00943
00944
00945 if (nbits > MAX_COEF_BITS+1)
00946 ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
00947
00948
00949 if (! emit_bits_s(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
00950 return FALSE;
00951
00952
00953
00954 if (nbits)
00955 if (! emit_bits_s(state, (unsigned int) temp2, nbits))
00956 return FALSE;
00957
00958
00959
00960 r = 0;
00961
00962 for (k = 1; k <= Se; k++) {
00963 if ((temp = block[natural_order[k]]) == 0) {
00964 r++;
00965 } else {
00966
00967 while (r > 15) {
00968 if (! emit_bits_s(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
00969 return FALSE;
00970 r -= 16;
00971 }
00972
00973 temp2 = temp;
00974 if (temp < 0) {
00975 temp = -temp;
00976
00977 temp2--;
00978 }
00979
00980
00981 nbits = 1;
00982 while ((temp >>= 1))
00983 nbits++;
00984
00985 if (nbits > MAX_COEF_BITS)
00986 ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
00987
00988
00989 i = (r << 4) + nbits;
00990 if (! emit_bits_s(state, actbl->ehufco[i], actbl->ehufsi[i]))
00991 return FALSE;
00992
00993
00994
00995 if (! emit_bits_s(state, (unsigned int) temp2, nbits))
00996 return FALSE;
00997
00998 r = 0;
00999 }
01000 }
01001
01002
01003 if (r > 0)
01004 if (! emit_bits_s(state, actbl->ehufco[0], actbl->ehufsi[0]))
01005 return FALSE;
01006
01007 return TRUE;
01008 }
01009
01010
01011
01012
01013
01014
01015 METHODDEF(boolean)
01016 encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
01017 {
01018 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
01019 working_state state;
01020 int blkn, ci;
01021 jpeg_component_info * compptr;
01022
01023
01024 state.next_output_byte = cinfo->dest->next_output_byte;
01025 state.free_in_buffer = cinfo->dest->free_in_buffer;
01026 ASSIGN_STATE(state.cur, entropy->saved);
01027 state.cinfo = cinfo;
01028
01029
01030 if (cinfo->restart_interval) {
01031 if (entropy->restarts_to_go == 0)
01032 if (! emit_restart_s(&state, entropy->next_restart_num))
01033 return FALSE;
01034 }
01035
01036
01037 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
01038 ci = cinfo->MCU_membership[blkn];
01039 compptr = cinfo->cur_comp_info[ci];
01040 if (! encode_one_block(&state,
01041 MCU_data[blkn][0], state.cur.last_dc_val[ci],
01042 entropy->dc_derived_tbls[compptr->dc_tbl_no],
01043 entropy->ac_derived_tbls[compptr->ac_tbl_no]))
01044 return FALSE;
01045
01046 state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
01047 }
01048
01049
01050 cinfo->dest->next_output_byte = state.next_output_byte;
01051 cinfo->dest->free_in_buffer = state.free_in_buffer;
01052 ASSIGN_STATE(entropy->saved, state.cur);
01053
01054
01055 if (cinfo->restart_interval) {
01056 if (entropy->restarts_to_go == 0) {
01057 entropy->restarts_to_go = cinfo->restart_interval;
01058 entropy->next_restart_num++;
01059 entropy->next_restart_num &= 7;
01060 }
01061 entropy->restarts_to_go--;
01062 }
01063
01064 return TRUE;
01065 }
01066
01067
01068
01069
01070
01071
01072 METHODDEF(void)
01073 finish_pass_huff (j_compress_ptr cinfo)
01074 {
01075 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
01076 working_state state;
01077
01078 if (cinfo->progressive_mode) {
01079 entropy->next_output_byte = cinfo->dest->next_output_byte;
01080 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
01081
01082
01083 emit_eobrun(entropy);
01084 flush_bits_e(entropy);
01085
01086 cinfo->dest->next_output_byte = entropy->next_output_byte;
01087 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
01088 } else {
01089
01090 state.next_output_byte = cinfo->dest->next_output_byte;
01091 state.free_in_buffer = cinfo->dest->free_in_buffer;
01092 ASSIGN_STATE(state.cur, entropy->saved);
01093 state.cinfo = cinfo;
01094
01095
01096 if (! flush_bits_s(&state))
01097 ERREXIT(cinfo, JERR_CANT_SUSPEND);
01098
01099
01100 cinfo->dest->next_output_byte = state.next_output_byte;
01101 cinfo->dest->free_in_buffer = state.free_in_buffer;
01102 ASSIGN_STATE(entropy->saved, state.cur);
01103 }
01104 }
01105
01106
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116
01117
01118
01119
01120
01121 LOCAL(void)
01122 htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
01123 long dc_counts[], long ac_counts[])
01124 {
01125 register int temp;
01126 register int nbits;
01127 register int k, r;
01128 int Se = cinfo->lim_Se;
01129 const int * natural_order = cinfo->natural_order;
01130
01131
01132
01133 temp = block[0] - last_dc_val;
01134 if (temp < 0)
01135 temp = -temp;
01136
01137
01138 nbits = 0;
01139 while (temp) {
01140 nbits++;
01141 temp >>= 1;
01142 }
01143
01144
01145
01146 if (nbits > MAX_COEF_BITS+1)
01147 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
01148
01149
01150 dc_counts[nbits]++;
01151
01152
01153
01154 r = 0;
01155
01156 for (k = 1; k <= Se; k++) {
01157 if ((temp = block[natural_order[k]]) == 0) {
01158 r++;
01159 } else {
01160
01161 while (r > 15) {
01162 ac_counts[0xF0]++;
01163 r -= 16;
01164 }
01165
01166
01167 if (temp < 0)
01168 temp = -temp;
01169
01170
01171 nbits = 1;
01172 while ((temp >>= 1))
01173 nbits++;
01174
01175 if (nbits > MAX_COEF_BITS)
01176 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
01177
01178
01179 ac_counts[(r << 4) + nbits]++;
01180
01181 r = 0;
01182 }
01183 }
01184
01185
01186 if (r > 0)
01187 ac_counts[0]++;
01188 }
01189
01190
01191
01192
01193
01194
01195
01196 METHODDEF(boolean)
01197 encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
01198 {
01199 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
01200 int blkn, ci;
01201 jpeg_component_info * compptr;
01202
01203
01204 if (cinfo->restart_interval) {
01205 if (entropy->restarts_to_go == 0) {
01206
01207 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
01208 entropy->saved.last_dc_val[ci] = 0;
01209
01210 entropy->restarts_to_go = cinfo->restart_interval;
01211 }
01212 entropy->restarts_to_go--;
01213 }
01214
01215 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
01216 ci = cinfo->MCU_membership[blkn];
01217 compptr = cinfo->cur_comp_info[ci];
01218 htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
01219 entropy->dc_count_ptrs[compptr->dc_tbl_no],
01220 entropy->ac_count_ptrs[compptr->ac_tbl_no]);
01221 entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
01222 }
01223
01224 return TRUE;
01225 }
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255 LOCAL(void)
01256 jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
01257 {
01258 #define MAX_CLEN 32
01259 UINT8 bits[MAX_CLEN+1];
01260 int codesize[257];
01261 int others[257];
01262 int c1, c2;
01263 int p, i, j;
01264 long v;
01265
01266
01267
01268 MEMZERO(bits, SIZEOF(bits));
01269 MEMZERO(codesize, SIZEOF(codesize));
01270 for (i = 0; i < 257; i++)
01271 others[i] = -1;
01272
01273 freq[256] = 1;
01274
01275
01276
01277
01278
01279
01280
01281 for (;;) {
01282
01283
01284 c1 = -1;
01285 v = 1000000000L;
01286 for (i = 0; i <= 256; i++) {
01287 if (freq[i] && freq[i] <= v) {
01288 v = freq[i];
01289 c1 = i;
01290 }
01291 }
01292
01293
01294
01295 c2 = -1;
01296 v = 1000000000L;
01297 for (i = 0; i <= 256; i++) {
01298 if (freq[i] && freq[i] <= v && i != c1) {
01299 v = freq[i];
01300 c2 = i;
01301 }
01302 }
01303
01304
01305 if (c2 < 0)
01306 break;
01307
01308
01309 freq[c1] += freq[c2];
01310 freq[c2] = 0;
01311
01312
01313 codesize[c1]++;
01314 while (others[c1] >= 0) {
01315 c1 = others[c1];
01316 codesize[c1]++;
01317 }
01318
01319 others[c1] = c2;
01320
01321
01322 codesize[c2]++;
01323 while (others[c2] >= 0) {
01324 c2 = others[c2];
01325 codesize[c2]++;
01326 }
01327 }
01328
01329
01330 for (i = 0; i <= 256; i++) {
01331 if (codesize[i]) {
01332
01333
01334 if (codesize[i] > MAX_CLEN)
01335 ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
01336
01337 bits[codesize[i]]++;
01338 }
01339 }
01340
01341
01342
01343
01344
01345
01346
01347
01348
01349
01350
01351
01352 for (i = MAX_CLEN; i > 16; i--) {
01353 while (bits[i] > 0) {
01354 j = i - 2;
01355 while (bits[j] == 0)
01356 j--;
01357
01358 bits[i] -= 2;
01359 bits[i-1]++;
01360 bits[j+1] += 2;
01361 bits[j]--;
01362 }
01363 }
01364
01365
01366 while (bits[i] == 0)
01367 i--;
01368 bits[i]--;
01369
01370
01371 MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
01372
01373
01374
01375
01376
01377 p = 0;
01378 for (i = 1; i <= MAX_CLEN; i++) {
01379 for (j = 0; j <= 255; j++) {
01380 if (codesize[j] == i) {
01381 htbl->huffval[p] = (UINT8) j;
01382 p++;
01383 }
01384 }
01385 }
01386
01387
01388 htbl->sent_table = FALSE;
01389 }
01390
01391
01392
01393
01394
01395
01396 METHODDEF(void)
01397 finish_pass_gather (j_compress_ptr cinfo)
01398 {
01399 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
01400 int ci, tbl;
01401 jpeg_component_info * compptr;
01402 JHUFF_TBL **htblptr;
01403 boolean did_dc[NUM_HUFF_TBLS];
01404 boolean did_ac[NUM_HUFF_TBLS];
01405
01406
01407
01408
01409 if (cinfo->progressive_mode)
01410
01411 emit_eobrun(entropy);
01412
01413 MEMZERO(did_dc, SIZEOF(did_dc));
01414 MEMZERO(did_ac, SIZEOF(did_ac));
01415
01416 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
01417 compptr = cinfo->cur_comp_info[ci];
01418
01419 if (cinfo->Ss == 0 && cinfo->Ah == 0) {
01420 tbl = compptr->dc_tbl_no;
01421 if (! did_dc[tbl]) {
01422 htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
01423 if (*htblptr == NULL)
01424 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
01425 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[tbl]);
01426 did_dc[tbl] = TRUE;
01427 }
01428 }
01429
01430 if (cinfo->Se) {
01431 tbl = compptr->ac_tbl_no;
01432 if (! did_ac[tbl]) {
01433 htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
01434 if (*htblptr == NULL)
01435 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
01436 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[tbl]);
01437 did_ac[tbl] = TRUE;
01438 }
01439 }
01440 }
01441 }
01442
01443
01444
01445
01446
01447
01448
01449
01450 METHODDEF(void)
01451 start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
01452 {
01453 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
01454 int ci, tbl;
01455 jpeg_component_info * compptr;
01456
01457 if (gather_statistics)
01458 entropy->pub.finish_pass = finish_pass_gather;
01459 else
01460 entropy->pub.finish_pass = finish_pass_huff;
01461
01462 if (cinfo->progressive_mode) {
01463 entropy->cinfo = cinfo;
01464 entropy->gather_statistics = gather_statistics;
01465
01466
01467
01468
01469 if (cinfo->Ah == 0) {
01470 if (cinfo->Ss == 0)
01471 entropy->pub.encode_mcu = encode_mcu_DC_first;
01472 else
01473 entropy->pub.encode_mcu = encode_mcu_AC_first;
01474 } else {
01475 if (cinfo->Ss == 0)
01476 entropy->pub.encode_mcu = encode_mcu_DC_refine;
01477 else {
01478 entropy->pub.encode_mcu = encode_mcu_AC_refine;
01479
01480 if (entropy->bit_buffer == NULL)
01481 entropy->bit_buffer = (char *)
01482 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
01483 MAX_CORR_BITS * SIZEOF(char));
01484 }
01485 }
01486
01487
01488 entropy->ac_tbl_no = cinfo->cur_comp_info[0]->ac_tbl_no;
01489 entropy->EOBRUN = 0;
01490 entropy->BE = 0;
01491 } else {
01492 if (gather_statistics)
01493 entropy->pub.encode_mcu = encode_mcu_gather;
01494 else
01495 entropy->pub.encode_mcu = encode_mcu_huff;
01496 }
01497
01498 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
01499 compptr = cinfo->cur_comp_info[ci];
01500
01501 if (cinfo->Ss == 0 && cinfo->Ah == 0) {
01502 tbl = compptr->dc_tbl_no;
01503 if (gather_statistics) {
01504
01505
01506 if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
01507 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
01508
01509
01510 if (entropy->dc_count_ptrs[tbl] == NULL)
01511 entropy->dc_count_ptrs[tbl] = (long *)
01512 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
01513 257 * SIZEOF(long));
01514 MEMZERO(entropy->dc_count_ptrs[tbl], 257 * SIZEOF(long));
01515 } else {
01516
01517
01518 jpeg_make_c_derived_tbl(cinfo, TRUE, tbl,
01519 & entropy->dc_derived_tbls[tbl]);
01520 }
01521
01522 entropy->saved.last_dc_val[ci] = 0;
01523 }
01524
01525 if (cinfo->Se) {
01526 tbl = compptr->ac_tbl_no;
01527 if (gather_statistics) {
01528 if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
01529 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
01530 if (entropy->ac_count_ptrs[tbl] == NULL)
01531 entropy->ac_count_ptrs[tbl] = (long *)
01532 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
01533 257 * SIZEOF(long));
01534 MEMZERO(entropy->ac_count_ptrs[tbl], 257 * SIZEOF(long));
01535 } else {
01536 jpeg_make_c_derived_tbl(cinfo, FALSE, tbl,
01537 & entropy->ac_derived_tbls[tbl]);
01538 }
01539 }
01540 }
01541
01542
01543 entropy->saved.put_buffer = 0;
01544 entropy->saved.put_bits = 0;
01545
01546
01547 entropy->restarts_to_go = cinfo->restart_interval;
01548 entropy->next_restart_num = 0;
01549 }
01550
01551
01552
01553
01554
01555
01556 GLOBAL(void)
01557 jinit_huff_encoder (j_compress_ptr cinfo)
01558 {
01559 huff_entropy_ptr entropy;
01560 int i;
01561
01562 entropy = (huff_entropy_ptr)
01563 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
01564 SIZEOF(huff_entropy_encoder));
01565 cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
01566 entropy->pub.start_pass = start_pass_huff;
01567
01568
01569 for (i = 0; i < NUM_HUFF_TBLS; i++) {
01570 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
01571 entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
01572 }
01573
01574 if (cinfo->progressive_mode)
01575 entropy->bit_buffer = NULL;
01576 }