MCPcopy Create free account
hub / github.com/ValveSoftware/openvr / lodepng_huffman_code_lengths

Function lodepng_huffman_code_lengths

samples/shared/lodepng.cpp:731–848  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

729}
730
731unsigned lodepng_huffman_code_lengths(unsigned* lengths, const unsigned* frequencies,
732 size_t numcodes, unsigned maxbitlen)
733{
734 unsigned i, j;
735 size_t sum = 0, numpresent = 0;
736 unsigned error = 0;
737 Coin* coins; /*the coins of the currently calculated row*/
738 Coin* prev_row; /*the previous row of coins*/
739 size_t numcoins;
740 size_t coinmem;
741
742 if(numcodes == 0) return 80; /*error: a tree of 0 symbols is not supposed to be made*/
743
744 for(i = 0; i < numcodes; i++)
745 {
746 if(frequencies[i] > 0)
747 {
748 numpresent++;
749 sum += frequencies[i];
750 }
751 }
752
753 for(i = 0; i < numcodes; i++) lengths[i] = 0;
754
755 /*ensure at least two present symbols. There should be at least one symbol
756 according to RFC 1951 section 3.2.7. To decoders incorrectly require two. To
757 make these work as well ensure there are at least two symbols. The
758 Package-Merge code below also doesn't work correctly if there's only one
759 symbol, it'd give it the theoritical 0 bits but in practice zlib wants 1 bit*/
760 if(numpresent == 0)
761 {
762 lengths[0] = lengths[1] = 1; /*note that for RFC 1951 section 3.2.7, only lengths[0] = 1 is needed*/
763 }
764 else if(numpresent == 1)
765 {
766 for(i = 0; i < numcodes; i++)
767 {
768 if(frequencies[i])
769 {
770 lengths[i] = 1;
771 lengths[i == 0 ? 1 : 0] = 1;
772 break;
773 }
774 }
775 }
776 else
777 {
778 /*Package-Merge algorithm represented by coin collector's problem
779 For every symbol, maxbitlen coins will be created*/
780
781 coinmem = numpresent * 2; /*max amount of coins needed with the current algo*/
782 coins = (Coin*)lodepng_malloc(sizeof(Coin) * coinmem);
783 prev_row = (Coin*)lodepng_malloc(sizeof(Coin) * coinmem);
784 if(!coins || !prev_row)
785 {
786 lodepng_free(coins);
787 lodepng_free(prev_row);
788 return 83; /*alloc fail*/

Callers 1

Calls 7

lodepng_mallocFunction · 0.85
lodepng_freeFunction · 0.85
init_coinsFunction · 0.85
append_symbol_coinsFunction · 0.85
cleanup_coinsFunction · 0.85
coin_copyFunction · 0.85
add_coinsFunction · 0.85

Tested by

no test coverage detected