ddnet/src/engine/shared/huffman.h

88 lines
2.3 KiB
C
Raw Normal View History

2010-11-20 10:37:14 +00:00
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
2010-05-29 07:25:38 +00:00
#ifndef ENGINE_SHARED_HUFFMAN_H
#define ENGINE_SHARED_HUFFMAN_H
class CHuffman
{
enum
{
HUFFMAN_EOF_SYMBOL = 256,
HUFFMAN_MAX_SYMBOLS = HUFFMAN_EOF_SYMBOL + 1,
HUFFMAN_MAX_NODES = HUFFMAN_MAX_SYMBOLS * 2 - 1,
2010-05-29 07:25:38 +00:00
HUFFMAN_LUTBITS = 10,
HUFFMAN_LUTSIZE = (1 << HUFFMAN_LUTBITS),
HUFFMAN_LUTMASK = (HUFFMAN_LUTSIZE - 1)
2010-05-29 07:25:38 +00:00
};
struct CNode
{
// symbol
unsigned m_Bits;
unsigned m_NumBits;
// don't use pointers for this. shorts are smaller so we can fit more data into the cache
unsigned short m_aLeafs[2];
// what the symbol represents
unsigned char m_Symbol;
};
static const unsigned ms_aFreqTable[HUFFMAN_MAX_SYMBOLS];
2010-05-29 07:25:38 +00:00
CNode m_aNodes[HUFFMAN_MAX_NODES];
CNode *m_apDecodeLut[HUFFMAN_LUTSIZE];
CNode *m_pStartNode;
int m_NumNodes;
void Setbits_r(CNode *pNode, int Bits, unsigned Depth);
2010-05-29 07:25:38 +00:00
void ConstructTree(const unsigned *pFrequencies);
2010-05-29 07:25:38 +00:00
public:
/*
2022-03-05 09:42:19 +00:00
Function: Init
2010-05-29 07:25:38 +00:00
Inits the compressor/decompressor.
Parameters:
2022-03-05 09:42:19 +00:00
pFrequencies - A pointer to an array of 256 entries of the frequencies of the bytes
2010-05-29 07:25:38 +00:00
Remarks:
2022-03-05 09:42:19 +00:00
- Does no allocation whatsoever.
- You don't have to call any cleanup functions when you are done with it.
2010-05-29 07:25:38 +00:00
*/
void Init(const unsigned *pFrequencies = ms_aFreqTable);
2010-05-29 07:25:38 +00:00
/*
2022-03-05 09:42:19 +00:00
Function: Compress
2010-05-29 07:25:38 +00:00
Compresses a buffer and outputs a compressed buffer.
Parameters:
2022-03-05 09:42:19 +00:00
pInput - Buffer to compress
InputSize - Size of the buffer to compress
pOutput - Buffer to put the compressed data into
OutputSize - Size of the output buffer
2010-05-29 07:25:38 +00:00
Returns:
Returns the size of the compressed data. Negative value on failure.
*/
int Compress(const void *pInput, int InputSize, void *pOutput, int OutputSize) const;
2010-05-29 07:25:38 +00:00
/*
2022-03-05 09:42:19 +00:00
Function: Decompress
2010-05-29 07:25:38 +00:00
Decompresses a buffer
Parameters:
2022-03-05 09:42:19 +00:00
pInput - Buffer to decompress
InputSize - Size of the buffer to decompress
pOutput - Buffer to put the uncompressed data into
OutputSize - Size of the output buffer
2010-05-29 07:25:38 +00:00
Returns:
Returns the size of the uncompressed data. Negative value on failure.
*/
int Decompress(const void *pInput, int InputSize, void *pOutput, int OutputSize) const;
2010-05-29 07:25:38 +00:00
};
2022-03-05 09:42:19 +00:00
#endif // ENGINE_SHARED_HUFFMAN_H