Doxygen Source Code Documentation
jchuff.c File Reference
#include "jinclude.h"
#include "jpeglib.h"
#include "jchuff.h"
Go to the source code of this file.
Define Documentation
|
Definition at line 41 of file jchuff.c. Referenced by decode_mcu(), decode_mcu_DC_first(), encode_mcu_huff(), and finish_pass_huff(). |
|
Value: { *(state)->next_output_byte++ = (JOCTET) (val); \ if (--(state)->free_in_buffer == 0) \ if (! dump_buffer(state)) \ { action; } } Definition at line 270 of file jchuff.c. Referenced by emit_2bytes(), emit_adobe_app14(), emit_bits(), emit_dac(), emit_dht(), emit_dqt(), emit_jfif_app0(), emit_marker(), emit_restart(), emit_sof(), emit_sos(), and write_marker_byte(). |
|
|
|
|
Typedef Documentation
|
|
Function Documentation
|
Definition at line 278 of file jchuff.c. References working_state::cinfo, jpeg_compress_struct::dest, jpeg_destination_mgr::free_in_buffer, working_state::free_in_buffer, jpeg_destination_mgr::next_output_byte, and working_state::next_output_byte.
00280 { 00281 struct jpeg_destination_mgr * dest = state->cinfo->dest; 00282 00283 if (! (*dest->empty_output_buffer) (state->cinfo)) 00284 return FALSE; 00285 /* After a successful buffer dump, must reset buffer pointers */ 00286 state->next_output_byte = dest->next_output_byte; 00287 state->free_in_buffer = dest->free_in_buffer; 00288 return TRUE; 00289 } |
|
Definition at line 302 of file jchuff.c. References c, working_state::cinfo, working_state::cur, emit_byte, ERREXIT, INT32, savable_state::put_bits, and savable_state::put_buffer.
00304 { 00305 /* This routine is heavily used, so it's worth coding tightly. */ 00306 register INT32 put_buffer = (INT32) code; 00307 register int put_bits = state->cur.put_bits; 00308 00309 /* if size is 0, caller used an invalid Huffman table entry */ 00310 if (size == 0) 00311 ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE); 00312 00313 put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */ 00314 00315 put_bits += size; /* new number of bits in buffer */ 00316 00317 put_buffer <<= 24 - put_bits; /* align incoming bits */ 00318 00319 put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */ 00320 00321 while (put_bits >= 8) { 00322 int c = (int) ((put_buffer >> 16) & 0xFF); 00323 00324 emit_byte(state, c, return FALSE); 00325 if (c == 0xFF) { /* need to stuff a zero byte? */ 00326 emit_byte(state, 0, return FALSE); 00327 } 00328 put_buffer <<= 8; 00329 put_bits -= 8; 00330 } 00331 00332 state->cur.put_buffer = put_buffer; /* update state variables */ 00333 state->cur.put_bits = put_bits; 00334 00335 return TRUE; 00336 } |
|
Definition at line 451 of file jchuff.c. References working_state::cinfo, jpeg_compress_struct::comps_in_scan, working_state::cur, emit_byte, flush_bits(), JPEG_RST0, and savable_state::last_dc_val.
00452 { 00453 int ci; 00454 00455 if (! flush_bits(state)) 00456 return FALSE; 00457 00458 emit_byte(state, 0xFF, return FALSE); 00459 emit_byte(state, JPEG_RST0 + restart_num, return FALSE); 00460 00461 /* Re-initialize DC predictions to 0 */ 00462 for (ci = 0; ci < state->cinfo->comps_in_scan; ci++) 00463 state->cur.last_dc_val[ci] = 0; 00464 00465 /* The restart counter is not updated until we successfully write the MCU. */ 00466 00467 return TRUE; 00468 } |
|
Definition at line 645 of file jchuff.c. References jpeg_component_info::ac_tbl_no, jpeg_compress_struct::blocks_in_MCU, compptr, jpeg_compress_struct::comps_in_scan, jpeg_compress_struct::cur_comp_info, jpeg_component_info::dc_tbl_no, jpeg_compress_struct::entropy, htest_one_block(), JBLOCKROW, savable_state::last_dc_val, jpeg_compress_struct::MCU_membership, jpeg_compress_struct::restart_interval, huff_entropy_decoder::restarts_to_go, and huff_entropy_decoder::saved. Referenced by start_pass_huff().
00646 { 00647 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 00648 int blkn, ci; 00649 jpeg_component_info * compptr; 00650 00651 /* Take care of restart intervals if needed */ 00652 if (cinfo->restart_interval) { 00653 if (entropy->restarts_to_go == 0) { 00654 /* Re-initialize DC predictions to 0 */ 00655 for (ci = 0; ci < cinfo->comps_in_scan; ci++) 00656 entropy->saved.last_dc_val[ci] = 0; 00657 /* Update restart state */ 00658 entropy->restarts_to_go = cinfo->restart_interval; 00659 } 00660 entropy->restarts_to_go--; 00661 } 00662 00663 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 00664 ci = cinfo->MCU_membership[blkn]; 00665 compptr = cinfo->cur_comp_info[ci]; 00666 htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci], 00667 entropy->dc_count_ptrs[compptr->dc_tbl_no], 00668 entropy->ac_count_ptrs[compptr->ac_tbl_no]); 00669 entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0]; 00670 } 00671 00672 return TRUE; 00673 } |
|
Definition at line 476 of file jchuff.c. References huff_entropy_decoder::ac_derived_tbls, jpeg_component_info::ac_tbl_no, ASSIGN_STATE, jpeg_compress_struct::blocks_in_MCU, working_state::cinfo, compptr, working_state::cur, jpeg_compress_struct::cur_comp_info, huff_entropy_decoder::dc_derived_tbls, jpeg_component_info::dc_tbl_no, jpeg_compress_struct::dest, emit_restart(), encode_one_block(), jpeg_compress_struct::entropy, jpeg_destination_mgr::free_in_buffer, working_state::free_in_buffer, JBLOCKROW, savable_state::last_dc_val, jpeg_compress_struct::MCU_membership, jpeg_destination_mgr::next_output_byte, working_state::next_output_byte, jpeg_compress_struct::restart_interval, huff_entropy_decoder::restarts_to_go, and huff_entropy_decoder::saved. Referenced by start_pass_huff().
00477 { 00478 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 00479 working_state state; 00480 int blkn, ci; 00481 jpeg_component_info * compptr; 00482 00483 /* Load up working state */ 00484 state.next_output_byte = cinfo->dest->next_output_byte; 00485 state.free_in_buffer = cinfo->dest->free_in_buffer; 00486 ASSIGN_STATE(state.cur, entropy->saved); 00487 state.cinfo = cinfo; 00488 00489 /* Emit restart marker if needed */ 00490 if (cinfo->restart_interval) { 00491 if (entropy->restarts_to_go == 0) 00492 if (! emit_restart(&state, entropy->next_restart_num)) 00493 return FALSE; 00494 } 00495 00496 /* Encode the MCU data blocks */ 00497 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 00498 ci = cinfo->MCU_membership[blkn]; 00499 compptr = cinfo->cur_comp_info[ci]; 00500 if (! encode_one_block(&state, 00501 MCU_data[blkn][0], state.cur.last_dc_val[ci], 00502 entropy->dc_derived_tbls[compptr->dc_tbl_no], 00503 entropy->ac_derived_tbls[compptr->ac_tbl_no])) 00504 return FALSE; 00505 /* Update last_dc_val */ 00506 state.cur.last_dc_val[ci] = MCU_data[blkn][0][0]; 00507 } 00508 00509 /* Completed MCU, so update state */ 00510 cinfo->dest->next_output_byte = state.next_output_byte; 00511 cinfo->dest->free_in_buffer = state.free_in_buffer; 00512 ASSIGN_STATE(entropy->saved, state.cur); 00513 00514 /* Update restart-interval state too */ 00515 if (cinfo->restart_interval) { 00516 if (entropy->restarts_to_go == 0) { 00517 entropy->restarts_to_go = cinfo->restart_interval; 00518 entropy->next_restart_num++; 00519 entropy->next_restart_num &= 7; 00520 } 00521 entropy->restarts_to_go--; 00522 } 00523 00524 return TRUE; 00525 } |
|
Definition at line 353 of file jchuff.c. References working_state::cinfo, c_derived_tbl::ehufco, c_derived_tbl::ehufsi, emit_bits(), ERREXIT, i, JCOEFPTR, MAX_COEF_BITS, and r. Referenced by encode_mcu_huff().
00355 { 00356 register int temp, temp2; 00357 register int nbits; 00358 register int k, r, i; 00359 00360 /* Encode the DC coefficient difference per section F.1.2.1 */ 00361 00362 temp = temp2 = block[0] - last_dc_val; 00363 00364 if (temp < 0) { 00365 temp = -temp; /* temp is abs value of input */ 00366 /* For a negative input, want temp2 = bitwise complement of abs(input) */ 00367 /* This code assumes we are on a two's complement machine */ 00368 temp2--; 00369 } 00370 00371 /* Find the number of bits needed for the magnitude of the coefficient */ 00372 nbits = 0; 00373 while (temp) { 00374 nbits++; 00375 temp >>= 1; 00376 } 00377 /* Check for out-of-range coefficient values. 00378 * Since we're encoding a difference, the range limit is twice as much. 00379 */ 00380 if (nbits > MAX_COEF_BITS+1) 00381 ERREXIT(state->cinfo, JERR_BAD_DCT_COEF); 00382 00383 /* Emit the Huffman-coded symbol for the number of bits */ 00384 if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits])) 00385 return FALSE; 00386 00387 /* Emit that number of bits of the value, if positive, */ 00388 /* or the complement of its magnitude, if negative. */ 00389 if (nbits) /* emit_bits rejects calls with size 0 */ 00390 if (! emit_bits(state, (unsigned int) temp2, nbits)) 00391 return FALSE; 00392 00393 /* Encode the AC coefficients per section F.1.2.2 */ 00394 00395 r = 0; /* r = run length of zeros */ 00396 00397 for (k = 1; k < DCTSIZE2; k++) { 00398 if ((temp = block[jpeg_natural_order[k]]) == 0) { 00399 r++; 00400 } else { 00401 /* if run length > 15, must emit special run-length-16 codes (0xF0) */ 00402 while (r > 15) { 00403 if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0])) 00404 return FALSE; 00405 r -= 16; 00406 } 00407 00408 temp2 = temp; 00409 if (temp < 0) { 00410 temp = -temp; /* temp is abs value of input */ 00411 /* This code assumes we are on a two's complement machine */ 00412 temp2--; 00413 } 00414 00415 /* Find the number of bits needed for the magnitude of the coefficient */ 00416 nbits = 1; /* there must be at least one 1 bit */ 00417 while ((temp >>= 1)) 00418 nbits++; 00419 /* Check for out-of-range coefficient values */ 00420 if (nbits > MAX_COEF_BITS) 00421 ERREXIT(state->cinfo, JERR_BAD_DCT_COEF); 00422 00423 /* Emit Huffman symbol for run length / number of bits */ 00424 i = (r << 4) + nbits; 00425 if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i])) 00426 return FALSE; 00427 00428 /* Emit that number of bits of the value, if positive, */ 00429 /* or the complement of its magnitude, if negative. */ 00430 if (! emit_bits(state, (unsigned int) temp2, nbits)) 00431 return FALSE; 00432 00433 r = 0; 00434 } 00435 } 00436 00437 /* If the last coef(s) were zero, emit an end-of-block code */ 00438 if (r > 0) 00439 if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0])) 00440 return FALSE; 00441 00442 return TRUE; 00443 } |
|
Definition at line 846 of file jchuff.c. References jpeg_compress_struct::ac_huff_tbl_ptrs, jpeg_component_info::ac_tbl_no, compptr, jpeg_compress_struct::comps_in_scan, jpeg_compress_struct::cur_comp_info, jpeg_compress_struct::dc_huff_tbl_ptrs, jpeg_component_info::dc_tbl_no, jpeg_compress_struct::entropy, jpeg_alloc_huff_table(), jpeg_gen_optimal_table(), MEMZERO, NUM_HUFF_TBLS, and SIZEOF. Referenced by start_pass_huff().
00847 { 00848 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 00849 int ci, dctbl, actbl; 00850 jpeg_component_info * compptr; 00851 JHUFF_TBL **htblptr; 00852 boolean did_dc[NUM_HUFF_TBLS]; 00853 boolean did_ac[NUM_HUFF_TBLS]; 00854 00855 /* It's important not to apply jpeg_gen_optimal_table more than once 00856 * per table, because it clobbers the input frequency counts! 00857 */ 00858 MEMZERO(did_dc, SIZEOF(did_dc)); 00859 MEMZERO(did_ac, SIZEOF(did_ac)); 00860 00861 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 00862 compptr = cinfo->cur_comp_info[ci]; 00863 dctbl = compptr->dc_tbl_no; 00864 actbl = compptr->ac_tbl_no; 00865 if (! did_dc[dctbl]) { 00866 htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl]; 00867 if (*htblptr == NULL) 00868 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo); 00869 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]); 00870 did_dc[dctbl] = TRUE; 00871 } 00872 if (! did_ac[actbl]) { 00873 htblptr = & cinfo->ac_huff_tbl_ptrs[actbl]; 00874 if (*htblptr == NULL) 00875 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo); 00876 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]); 00877 did_ac[actbl] = TRUE; 00878 } 00879 } 00880 } |
|
|
Definition at line 340 of file jchuff.c. References working_state::cur, emit_bits(), savable_state::put_bits, and savable_state::put_buffer.
00341 { 00342 if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */ 00343 return FALSE; 00344 state->cur.put_buffer = 0; /* and reset bit-buffer to empty */ 00345 state->cur.put_bits = 0; 00346 return TRUE; 00347 } |
|
Definition at line 572 of file jchuff.c. References ERREXIT, JCOEFPTR, MAX_COEF_BITS, and r. Referenced by encode_mcu_gather().
00574 { 00575 register int temp; 00576 register int nbits; 00577 register int k, r; 00578 00579 /* Encode the DC coefficient difference per section F.1.2.1 */ 00580 00581 temp = block[0] - last_dc_val; 00582 if (temp < 0) 00583 temp = -temp; 00584 00585 /* Find the number of bits needed for the magnitude of the coefficient */ 00586 nbits = 0; 00587 while (temp) { 00588 nbits++; 00589 temp >>= 1; 00590 } 00591 /* Check for out-of-range coefficient values. 00592 * Since we're encoding a difference, the range limit is twice as much. 00593 */ 00594 if (nbits > MAX_COEF_BITS+1) 00595 ERREXIT(cinfo, JERR_BAD_DCT_COEF); 00596 00597 /* Count the Huffman symbol for the number of bits */ 00598 dc_counts[nbits]++; 00599 00600 /* Encode the AC coefficients per section F.1.2.2 */ 00601 00602 r = 0; /* r = run length of zeros */ 00603 00604 for (k = 1; k < DCTSIZE2; k++) { 00605 if ((temp = block[jpeg_natural_order[k]]) == 0) { 00606 r++; 00607 } else { 00608 /* if run length > 15, must emit special run-length-16 codes (0xF0) */ 00609 while (r > 15) { 00610 ac_counts[0xF0]++; 00611 r -= 16; 00612 } 00613 00614 /* Find the number of bits needed for the magnitude of the coefficient */ 00615 if (temp < 0) 00616 temp = -temp; 00617 00618 /* Find the number of bits needed for the magnitude of the coefficient */ 00619 nbits = 1; /* there must be at least one 1 bit */ 00620 while ((temp >>= 1)) 00621 nbits++; 00622 /* Check for out-of-range coefficient values */ 00623 if (nbits > MAX_COEF_BITS) 00624 ERREXIT(cinfo, JERR_BAD_DCT_COEF); 00625 00626 /* Count Huffman symbol for run length / number of bits */ 00627 ac_counts[(r << 4) + nbits]++; 00628 00629 r = 0; 00630 } 00631 } 00632 00633 /* If the last coef(s) were zero, emit an end-of-block code */ 00634 if (r > 0) 00635 ac_counts[0]++; 00636 } |
|
Definition at line 891 of file jchuff.c. References i, JPOOL_IMAGE, NUM_HUFF_TBLS, SIZEOF, and start_pass_huff(). Referenced by jinit_compress_master(), and transencode_master_selection().
00892 { 00893 huff_entropy_ptr entropy; 00894 int i; 00895 00896 entropy = (huff_entropy_ptr) 00897 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 00898 SIZEOF(huff_entropy_encoder)); 00899 cinfo->entropy = (struct jpeg_entropy_encoder *) entropy; 00900 entropy->pub.start_pass = start_pass_huff; 00901 00902 /* Mark tables unallocated */ 00903 for (i = 0; i < NUM_HUFF_TBLS; i++) { 00904 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; 00905 #ifdef ENTROPY_OPT_SUPPORTED 00906 entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL; 00907 #endif 00908 } 00909 } |
|
Definition at line 705 of file jchuff.c. References JHUFF_TBL::bits, ERREXIT, freq, JHUFF_TBL::huffval, i, L, MEMCOPY, MEMZERO, p, JHUFF_TBL::sent_table, SIZEOF, UINT8, and v. Referenced by finish_pass_gather(), and finish_pass_gather_phuff().
00706 { 00707 #define MAX_CLEN 32 /* assumed maximum initial code length */ 00708 UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */ 00709 int codesize[257]; /* codesize[k] = code length of symbol k */ 00710 int others[257]; /* next symbol in current branch of tree */ 00711 int c1, c2; 00712 int p, i, j; 00713 long v; 00714 00715 /* This algorithm is explained in section K.2 of the JPEG standard */ 00716 00717 MEMZERO(bits, SIZEOF(bits)); 00718 MEMZERO(codesize, SIZEOF(codesize)); 00719 for (i = 0; i < 257; i++) 00720 others[i] = -1; /* init links to empty */ 00721 00722 freq[256] = 1; /* make sure 256 has a nonzero count */ 00723 /* Including the pseudo-symbol 256 in the Huffman procedure guarantees 00724 * that no real symbol is given code-value of all ones, because 256 00725 * will be placed last in the largest codeword category. 00726 */ 00727 00728 /* Huffman's basic algorithm to assign optimal code lengths to symbols */ 00729 00730 for (;;) { 00731 /* Find the smallest nonzero frequency, set c1 = its symbol */ 00732 /* In case of ties, take the larger symbol number */ 00733 c1 = -1; 00734 v = 1000000000L; 00735 for (i = 0; i <= 256; i++) { 00736 if (freq[i] && freq[i] <= v) { 00737 v = freq[i]; 00738 c1 = i; 00739 } 00740 } 00741 00742 /* Find the next smallest nonzero frequency, set c2 = its symbol */ 00743 /* In case of ties, take the larger symbol number */ 00744 c2 = -1; 00745 v = 1000000000L; 00746 for (i = 0; i <= 256; i++) { 00747 if (freq[i] && freq[i] <= v && i != c1) { 00748 v = freq[i]; 00749 c2 = i; 00750 } 00751 } 00752 00753 /* Done if we've merged everything into one frequency */ 00754 if (c2 < 0) 00755 break; 00756 00757 /* Else merge the two counts/trees */ 00758 freq[c1] += freq[c2]; 00759 freq[c2] = 0; 00760 00761 /* Increment the codesize of everything in c1's tree branch */ 00762 codesize[c1]++; 00763 while (others[c1] >= 0) { 00764 c1 = others[c1]; 00765 codesize[c1]++; 00766 } 00767 00768 others[c1] = c2; /* chain c2 onto c1's tree branch */ 00769 00770 /* Increment the codesize of everything in c2's tree branch */ 00771 codesize[c2]++; 00772 while (others[c2] >= 0) { 00773 c2 = others[c2]; 00774 codesize[c2]++; 00775 } 00776 } 00777 00778 /* Now count the number of symbols of each code length */ 00779 for (i = 0; i <= 256; i++) { 00780 if (codesize[i]) { 00781 /* The JPEG standard seems to think that this can't happen, */ 00782 /* but I'm paranoid... */ 00783 if (codesize[i] > MAX_CLEN) 00784 ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW); 00785 00786 bits[codesize[i]]++; 00787 } 00788 } 00789 00790 /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure 00791 * Huffman procedure assigned any such lengths, we must adjust the coding. 00792 * Here is what the JPEG spec says about how this next bit works: 00793 * Since symbols are paired for the longest Huffman code, the symbols are 00794 * removed from this length category two at a time. The prefix for the pair 00795 * (which is one bit shorter) is allocated to one of the pair; then, 00796 * skipping the BITS entry for that prefix length, a code word from the next 00797 * shortest nonzero BITS entry is converted into a prefix for two code words 00798 * one bit longer. 00799 */ 00800 00801 for (i = MAX_CLEN; i > 16; i--) { 00802 while (bits[i] > 0) { 00803 j = i - 2; /* find length of new prefix to be used */ 00804 while (bits[j] == 0) 00805 j--; 00806 00807 bits[i] -= 2; /* remove two symbols */ 00808 bits[i-1]++; /* one goes in this length */ 00809 bits[j+1] += 2; /* two new symbols in this length */ 00810 bits[j]--; /* symbol of this length is now a prefix */ 00811 } 00812 } 00813 00814 /* Remove the count for the pseudo-symbol 256 from the largest codelength */ 00815 while (bits[i] == 0) /* find largest codelength still in use */ 00816 i--; 00817 bits[i]--; 00818 00819 /* Return final symbol counts (only for lengths 0..16) */ 00820 MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits)); 00821 00822 /* Return a list of the symbols sorted by code length */ 00823 /* It's not real clear to me why we don't need to consider the codelength 00824 * changes made above, but the JPEG spec seems to think this works. 00825 */ 00826 p = 0; 00827 for (i = 1; i <= MAX_CLEN; i++) { 00828 for (j = 0; j <= 255; j++) { 00829 if (codesize[j] == i) { 00830 htbl->huffval[p] = (UINT8) j; 00831 p++; 00832 } 00833 } 00834 } 00835 00836 /* Set sent_table FALSE so updated table will be written to JPEG file. */ 00837 htbl->sent_table = FALSE; 00838 } |
|
Definition at line 179 of file jchuff.c. References jpeg_compress_struct::ac_huff_tbl_ptrs, JHUFF_TBL::bits, jpeg_compress_struct::dc_huff_tbl_ptrs, c_derived_tbl::ehufco, c_derived_tbl::ehufsi, ERREXIT, ERREXIT1, JHUFF_TBL::huffval, i, JPOOL_IMAGE, l, MEMZERO, NUM_HUFF_TBLS, p, and SIZEOF. Referenced by start_pass_huff(), and start_pass_phuff().
00181 { 00182 JHUFF_TBL *htbl; 00183 c_derived_tbl *dtbl; 00184 int p, i, l, lastp, si, maxsymbol; 00185 char huffsize[257]; 00186 unsigned int huffcode[257]; 00187 unsigned int code; 00188 00189 /* Note that huffsize[] and huffcode[] are filled in code-length order, 00190 * paralleling the order of the symbols themselves in htbl->huffval[]. 00191 */ 00192 00193 /* Find the input Huffman table */ 00194 if (tblno < 0 || tblno >= NUM_HUFF_TBLS) 00195 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); 00196 htbl = 00197 isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno]; 00198 if (htbl == NULL) 00199 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); 00200 00201 /* Allocate a workspace if we haven't already done so. */ 00202 if (*pdtbl == NULL) 00203 *pdtbl = (c_derived_tbl *) 00204 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 00205 SIZEOF(c_derived_tbl)); 00206 dtbl = *pdtbl; 00207 00208 /* Figure C.1: make table of Huffman code length for each symbol */ 00209 00210 p = 0; 00211 for (l = 1; l <= 16; l++) { 00212 i = (int) htbl->bits[l]; 00213 if (i < 0 || p + i > 256) /* protect against table overrun */ 00214 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); 00215 while (i--) 00216 huffsize[p++] = (char) l; 00217 } 00218 huffsize[p] = 0; 00219 lastp = p; 00220 00221 /* Figure C.2: generate the codes themselves */ 00222 /* We also validate that the counts represent a legal Huffman code tree. */ 00223 00224 code = 0; 00225 si = huffsize[0]; 00226 p = 0; 00227 while (huffsize[p]) { 00228 while (((int) huffsize[p]) == si) { 00229 huffcode[p++] = code; 00230 code++; 00231 } 00232 /* code is now 1 more than the last code used for codelength si; but 00233 * it must still fit in si bits, since no code is allowed to be all ones. 00234 */ 00235 if (((INT32) code) >= (((INT32) 1) << si)) 00236 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); 00237 code <<= 1; 00238 si++; 00239 } 00240 00241 /* Figure C.3: generate encoding tables */ 00242 /* These are code and size indexed by symbol value */ 00243 00244 /* Set all codeless symbols to have code length 0; 00245 * this lets us detect duplicate VAL entries here, and later 00246 * allows emit_bits to detect any attempt to emit such symbols. 00247 */ 00248 MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi)); 00249 00250 /* This is also a convenient place to check for out-of-range 00251 * and duplicated VAL entries. We allow 0..255 for AC symbols 00252 * but only 0..15 for DC. (We could constrain them further 00253 * based on data depth and mode, but this seems enough.) 00254 */ 00255 maxsymbol = isDC ? 15 : 255; 00256 00257 for (p = 0; p < lastp; p++) { 00258 i = htbl->huffval[p]; 00259 if (i < 0 || i > maxsymbol || dtbl->ehufsi[i]) 00260 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); 00261 dtbl->ehufco[i] = huffcode[p]; 00262 dtbl->ehufsi[i] = huffsize[p]; 00263 } 00264 } |
|
|
|
|
|
Definition at line 106 of file jchuff.c. References huff_entropy_decoder::ac_derived_tbls, jpeg_component_info::ac_tbl_no, compptr, jpeg_compress_struct::comps_in_scan, jpeg_compress_struct::cur_comp_info, huff_entropy_decoder::dc_derived_tbls, jpeg_component_info::dc_tbl_no, encode_mcu_gather(), encode_mcu_huff(), jpeg_compress_struct::entropy, ERREXIT, ERREXIT1, finish_pass_gather(), finish_pass_huff(), jpeg_make_c_derived_tbl(), JPOOL_IMAGE, savable_state::last_dc_val, MEMZERO, NUM_HUFF_TBLS, huff_entropy_decoder::pub, savable_state::put_bits, savable_state::put_buffer, jpeg_compress_struct::restart_interval, huff_entropy_decoder::restarts_to_go, huff_entropy_decoder::saved, and SIZEOF. Referenced by jinit_huff_encoder().
00107 { 00108 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 00109 int ci, dctbl, actbl; 00110 jpeg_component_info * compptr; 00111 00112 if (gather_statistics) { 00113 #ifdef ENTROPY_OPT_SUPPORTED 00114 entropy->pub.encode_mcu = encode_mcu_gather; 00115 entropy->pub.finish_pass = finish_pass_gather; 00116 #else 00117 ERREXIT(cinfo, JERR_NOT_COMPILED); 00118 #endif 00119 } else { 00120 entropy->pub.encode_mcu = encode_mcu_huff; 00121 entropy->pub.finish_pass = finish_pass_huff; 00122 } 00123 00124 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 00125 compptr = cinfo->cur_comp_info[ci]; 00126 dctbl = compptr->dc_tbl_no; 00127 actbl = compptr->ac_tbl_no; 00128 if (gather_statistics) { 00129 #ifdef ENTROPY_OPT_SUPPORTED 00130 /* Check for invalid table indexes */ 00131 /* (make_c_derived_tbl does this in the other path) */ 00132 if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS) 00133 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl); 00134 if (actbl < 0 || actbl >= NUM_HUFF_TBLS) 00135 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl); 00136 /* Allocate and zero the statistics tables */ 00137 /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */ 00138 if (entropy->dc_count_ptrs[dctbl] == NULL) 00139 entropy->dc_count_ptrs[dctbl] = (long *) 00140 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 00141 257 * SIZEOF(long)); 00142 MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long)); 00143 if (entropy->ac_count_ptrs[actbl] == NULL) 00144 entropy->ac_count_ptrs[actbl] = (long *) 00145 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 00146 257 * SIZEOF(long)); 00147 MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long)); 00148 #endif 00149 } else { 00150 /* Compute derived values for Huffman tables */ 00151 /* We may do this more than once for a table, but it's not expensive */ 00152 jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl, 00153 & entropy->dc_derived_tbls[dctbl]); 00154 jpeg_make_c_derived_tbl(cinfo, FALSE, actbl, 00155 & entropy->ac_derived_tbls[actbl]); 00156 } 00157 /* Initialize DC predictions to 0 */ 00158 entropy->saved.last_dc_val[ci] = 0; 00159 } 00160 00161 /* Initialize bit buffer to empty */ 00162 entropy->saved.put_buffer = 0; 00163 entropy->saved.put_bits = 0; 00164 00165 /* Initialize restart stuff */ 00166 entropy->restarts_to_go = cinfo->restart_interval; 00167 entropy->next_restart_num = 0; 00168 } |
Variable Documentation
|
Definition at line 84 of file jdphuff.c. Referenced by decode_mcu_AC_first(), decode_mcu_AC_refine(), decode_mcu_DC_first(), and decode_mcu_DC_refine(). |