int QccENTHuffmanTableInitialize(QccENTHuffmanTable *table);
int QccENTHuffmanTableAlloc(QccENTHuffmanTable
*table);
void QccENTHuffmanTableFree(QccENTHuffmanTable *table);
int
QccENTHuffmanTablePrint(const QccENTHuffmanTable *table);
int QccENTHuffmanTableRead(QccENTHuffmanTable
*table);
int QccENTHuffmanTableWrite(QccENTHuffmanTable *table);
The main component of a QccENTHuffmanTable is an array of QccENTHuffmanTableEntry entries (see below). Each of these entries contain a source symbol (an integer) and its correspoding Huffman codeword.
typedef struct
{
QccString filename;
QccString magic_num;
int major_version;
int minor_version;
int table_type;
int table_length;
QccENTHuffmanTableEntry *table;
int *num_codewords_list;
int num_codewords_list_length;
int *symbol_list;
int symbol_list_length;
int codeword_max[QCCENTHUFFMAN_MAXCODEWORDLEN];
int codeword_min[QCCENTHUFFMAN_MAXCODEWORDLEN];
int codeword_ptr[QCCENTHUFFMAN_MAXCODEWORDLEN];
} QccENTHuffmanTable;
The fields of QccENTHuffmanTable are as follows:
The QccENTHuffmanTableEntry data structure, used to hold source symbols and their corresponding codewords is defined as:
typedef struct
{
int symbol;
QccENTHuffmanCodeword codeword;
} QccENTHuffmanTableEntry;
The fields of QccENTHuffmanTableEntry are as follows:
The QccENTHuffmanCodeword data structure, used to hold the Huffman codeword itself, is defined as:
typedef struct
{
int length;
int codeword;
} QccENTHuffmanCodeword;
The fields of
QccENTHuffmanCodeword are as
follows:
The HUF file format consists of the following information:
where HUF is the magic number, X.X is the version number, <white space> is white space and/or comment lines, num_codewords_list_length is the length of the num_codewords_list to appear later in the file, and symbol_list_length is the length of the symbol_list to appear later in the file. num_codewords_list is the list of the number of codewords, starting with length 1. symbol_list is the list of symbols (in decimal integers) corresponding to the codewords.HUFX.X
<white space>
num_codewords_list_length
symbol_list_length
num_codewords_list
.
.
.
symbol_list
.
.
.
For example, if the Huffman table has the following symbols and codewords,
the corresponding HUF-format file would be:Symbol Codeword Length Codeword
-------------------------------------------
'a' 2 00
'b' 2 01
'c' 3 100
'd' 4 1010
'e' 4 1011
Note: the stuff starting with the "<----" arrows is merely an annotation and is not actually a legal part of the file.HUFX.X
# Optional comment(s) here
4
5
0 <----- Start with length = 1
2
1
2
97 <----- Decimal integer representation of symbols
98
99
100
101
table_type: QCCENTHUFFMAN_DECODETABLE
table: NULL
table_length: 0
QccENTHuffmanTableAlloc() allocates the Huffman table. table_length must be set before calling QccENTHuffmanTableAlloc().
QccENTHuffmanTableFree() frees the table array.
QccENTHuffmanTablePrint() prints the contents of table to stdout.
QccENTHuffmanTableRead() reads a Huffman table from a HUF-format file. table is returned as either a decoding or encoding table; set table->table_type to either QCCENTHUFFMAN_ENCODETABLE or QCCENTHUFFMAN_DECODETABLE, respectively, before calling QccENTHuffmanTableRead(). QccENTHuffmanTableRead() reads the list of numbers of codewords and the list of symbols from the file whose name is given by table->filename (must be set before calling QccENTHuffmanTableRead()) and then calls either QccENTHuffmanTableCreateEncodeTable(3) or QccENTHuffmanTableCreateDecodeTable(3) , as appropriate, to generate the actual table, table->table, of Huffman codewords.
QccENTHuffmanTableWrite() writes the Huffman table, table, to the HUF-format file whose name is given in table->filename. Usually, table will be either an encoding table (table->table_type = QCCENTHUFFMAN_ENCODETABLE) or a decoding table (table->table_type = QCCENTHUFFMAN_DECODETABLE) that was created by a call to QccENTHuffmanDesign(3) .
D. A. Huffman, "A Method for the Construction of Minimum-Redundancy Codes," Proceedings of the IRE, vol. 40, pp. 1098-1101, September 1952.