2 Copyright (C) 2008, Gonzalo Navarro, all rights reserved.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 { uint max,lim; // maximum symbol (0..max), same excluding zero freqs
29 uint depth; // max symbol length
31 { uint *spos; // symbol positions after sorting by decr freq (enc)
32 uint *symb; // symbols sorted by freq (dec)
34 uint *num; // first pos of each length (dec), number of each length (enc)
35 uint *fst; // first code (numeric) of each length (dec)
36 ulong total; // total length to achieve, in bits
40 /** Creates Huffman encoder given symbols 0..lim with frequencies
41 * freq[i], ready for compression
43 * @author Gonzalo Navarro
45 THuff createHuff (uint *freq, uint lim);
47 /** Encodes symb using H, over stream[ptr...lim] (ptr and lim are
48 * bit positions of stream). Returns the new ptr.
50 * @author Gonzalo Navarro
52 ulong encodeHuff (THuff H, uint symb, uint *stream, ulong ptr);
54 /** Decodes *symb using H, over stream[ptr...lim] (ptr and lim are
55 * bit positions of stream). Returns the new ptr.
57 * @author Gonzalo Navarro
59 ulong decodeHuff (THuff H, uint *symb, uint *stream, ulong ptr);
61 /** Writes H in file f
63 * @author Gonzalo Navarro
65 void saveHuff (THuff H, FILE *f);
67 /** Size of H written on file
69 * @author Gonzalo Navarro
71 uint sizeHuff (THuff H);
75 * @author Gonzalo Navarro
77 void freeHuff (THuff H);
79 /** Loads H from file f, prepared for encoding or decoding depending
82 * @author Gonzalo Navarro
84 THuff loadHuff (FILE *f, int enc);