2 // implements canonical Huffman
12 { uint max,lim; // maximum symbol (0..max), same excluding zero freqs
13 uint depth; // max symbol length
15 { uint *spos; // symbol positions after sorting by decr freq (enc)
16 uint *symb; // symbols sorted by freq (dec)
18 uint *num; // first pos of each length (dec), number of each length (enc)
19 uint *fst; // first code (numeric) of each length (dec)
20 uint total; // total length to achieve, in bits
23 // Creates Huffman encoder given symbols 0..lim with frequencies
24 // freq[i], ready for compression
25 // sorted --> are the symbols already sorted ?
27 THuff createHuff (uint *freq, uint lim, uint sorted);
29 // Encodes symb using H, over stream[ptr...lim] (ptr and lim are
30 // bit positions of stream). Returns the new ptr.
32 int encodeHuff (THuff H, uint symb, uint *stream, uint ptr);
34 // Decodes *symb using H, over stream[ptr...lim] (ptr and lim are
35 // bit positions of stream). Returns the new ptr.
37 int decodeHuff (THuff *H, uint *symb, uint *stream, uint ptr);
39 //Prepares a Huffman tree for decoding (changes in spos & symb)
41 void prepareToDecode(THuff *H);
45 void saveHuff2 (THuff H, FILE *f);
47 // Size of H written on file
49 uint sizeHuffDisk (THuff H);
52 uint sizeHuff2 (THuff H);
56 void freeHuff2 (THuff H);
58 // Loads H from file f, prepared for encoding or decoding depending
61 THuff loadHuff2 (FILE *f, int enc);
63 //Decodes a code starting in position ptr from stream. Returns the ranking in the
66 #define decodeNormalHuffMacro(H, symbol, stream, ptr) \
71 while (pos < H->fst[d]) \
72 { pos = (pos << 1) | bitget(stream,ptr); \
75 symbol = (H->s.symb[ H->num[d] + pos - H->fst[d] ]); \