1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef ENGINE_SHARED_HUFFMAN_H
4#define ENGINE_SHARED_HUFFMAN_H
5
6class CHuffman
7{
8 enum
9 {
10 HUFFMAN_EOF_SYMBOL = 256,
11
12 HUFFMAN_MAX_SYMBOLS = HUFFMAN_EOF_SYMBOL + 1,
13 HUFFMAN_MAX_NODES = HUFFMAN_MAX_SYMBOLS * 2 - 1,
14
15 HUFFMAN_LUTBITS = 10,
16 HUFFMAN_LUTSIZE = (1 << HUFFMAN_LUTBITS),
17 HUFFMAN_LUTMASK = (HUFFMAN_LUTSIZE - 1)
18 };
19
20 class CNode
21 {
22 public:
23 // symbol
24 unsigned m_Bits;
25 unsigned m_NumBits;
26
27 // don't use pointers for this. shorts are smaller so we can fit more data into the cache
28 unsigned short m_aLeaves[2];
29
30 // what the symbol represents
31 unsigned char m_Symbol;
32 };
33
34 static const unsigned ms_aFreqTable[HUFFMAN_MAX_SYMBOLS];
35
36 CNode m_aNodes[HUFFMAN_MAX_NODES];
37 CNode *m_apDecodeLut[HUFFMAN_LUTSIZE];
38 CNode *m_pStartNode;
39 int m_NumNodes;
40
41 void SetBitsRecursive(CNode *pNode, int Bits, unsigned Depth);
42 void ConstructTree(const unsigned *pFrequencies);
43
44public:
45 /*
46 Function: Init
47 Inits the compressor/decompressor.
48
49 Remarks:
50 - Does no allocation whatsoever.
51 - You don't have to call any cleanup functions when you are done with it.
52 */
53 void Init();
54
55 /*
56 Function: Compress
57 Compresses a buffer and outputs a compressed buffer.
58
59 Parameters:
60 pInput - Buffer to compress
61 InputSize - Size of the buffer to compress
62 pOutput - Buffer to put the compressed data into
63 OutputSize - Size of the output buffer
64
65 Returns:
66 Returns the size of the compressed data. Negative value on failure.
67 */
68 int Compress(const void *pInput, int InputSize, void *pOutput, int OutputSize) const;
69
70 /*
71 Function: Decompress
72 Decompresses a buffer
73
74 Parameters:
75 pInput - Buffer to decompress
76 InputSize - Size of the buffer to decompress
77 pOutput - Buffer to put the uncompressed data into
78 OutputSize - Size of the output buffer
79
80 Returns:
81 Returns the size of the uncompressed data. Negative value on failure.
82 */
83 int Decompress(const void *pInput, int InputSize, void *pOutput, int OutputSize) const;
84};
85#endif // ENGINE_SHARED_HUFFMAN_H
86