diff options
| author | Magnus Auvinen <magnus.auvinen@gmail.com> | 2008-08-17 07:05:16 +0000 |
|---|---|---|
| committer | Magnus Auvinen <magnus.auvinen@gmail.com> | 2008-08-17 07:05:16 +0000 |
| commit | 16912026dbdd7dc9e238492d9d83e37270ae4f06 (patch) | |
| tree | 5250ca25f847735b55cd293e9b632339503bae4a /src/engine/e_huffman.h | |
| parent | d1282138cd8a61442843c26b54c3f9259f4bf098 (diff) | |
| download | zcatch-16912026dbdd7dc9e238492d9d83e37270ae4f06.tar.gz zcatch-16912026dbdd7dc9e238492d9d83e37270ae4f06.zip | |
added cl_layershot. fixed some bugs in the network
Diffstat (limited to 'src/engine/e_huffman.h')
| -rw-r--r-- | src/engine/e_huffman.h | 106 |
1 files changed, 77 insertions, 29 deletions
diff --git a/src/engine/e_huffman.h b/src/engine/e_huffman.h index 84c71e60..c4e20223 100644 --- a/src/engine/e_huffman.h +++ b/src/engine/e_huffman.h @@ -1,43 +1,91 @@ +#ifndef __HUFFMAN_HEADER__ +#define __HUFFMAN_HEADER__ + +#ifdef __cplusplus +extern "C" { +#endif enum { - MAX_SYMBOL_SIZE=8, - MAX_NODES=1024*8 -}; + HUFFMAN_EOF_SYMBOL = 256, -typedef struct -{ - int i; -} HUFFSYMBOL; + HUFFMAN_MAX_SYMBOLS=HUFFMAN_EOF_SYMBOL+1, + HUFFMAN_MAX_NODES=HUFFMAN_MAX_SYMBOLS*2-1, + + HUFFMAN_LUTBITS = 10, + HUFFMAN_LUTSIZE = (1<<HUFFMAN_LUTBITS), + HUFFMAN_LUTMASK = (HUFFMAN_LUTSIZE-1) +}; -typedef struct HUFFNODE_t +typedef struct HUFFMAN_NODE { - int frequency; - - int symbol_size; - unsigned char symbol[MAX_SYMBOL_SIZE]; - - int num_bits; + /* symbol */ unsigned bits; + unsigned num_bits; + + /* don't use pointers for this. shorts are smaller so we can fit more data into the cache */ + unsigned short leafs[2]; - struct HUFFNODE_t *parent; - struct HUFFNODE_t *zero; - struct HUFFNODE_t *one; -} HUFFNODE; + /* what the symbol represents */ + unsigned char symbol; +} HUFFMAN_NODE; -typedef struct +typedef struct HUFFMAN_STATE { - HUFFNODE nodes[MAX_NODES]; - HUFFNODE *start_node; - int num_symbols; + HUFFMAN_NODE nodes[HUFFMAN_MAX_NODES]; + HUFFMAN_NODE *decode_lut[HUFFMAN_LUTSIZE]; + HUFFMAN_NODE *start_node; int num_nodes; -} HUFFSTATE; +} HUFFMAN_STATE; + +/* + Function: huffman_init + Inits the compressor/decompressor. + + Parameters: + huff - Pointer to the state to init + frequencies - A pointer to an array of 256 entries of the frequencies of the bytes + + Remarks: + - Does no allocation what so ever. + - You don't have to call any cleanup functions when you are done with it +*/ +void huffman_init(HUFFMAN_STATE *huff, const unsigned *frequencies); + +/* + Function: huffman_compress + Compresses a buffer and outputs a compressed buffer. + + Parameters: + huff - Pointer to the huffman state + input - Buffer to compress + input_size - Size of the buffer to compress + output - Buffer to put the compressed data into + output_size - Size of the output buffer + + Returns: + Returns the size of the compressed data. Negative value on failure. +*/ +int huffman_compress(HUFFMAN_STATE *huff, const void *input, int input_size, void *output, int output_size); + +/* + Function: huffman_decompress + Decompresses a buffer + + Parameters: + huff - Pointer to the huffman state + input - Buffer to decompress + input_size - Size of the buffer to decompress + output - Buffer to put the uncompressed data into + output_size - Size of the output buffer + Returns: + Returns the size of the uncompressed data. Negative value on failure. +*/ +int huffman_decompress(HUFFMAN_STATE *huff, const void *input, int input_size, void *output, int output_size); -void huffman_add_symbol(HUFFSTATE *huff, int frequency, int size, unsigned char *symbol); -void huffman_init(HUFFSTATE *huff); -void huffman_construct_tree(HUFFSTATE *huff); -int huffman_compress(HUFFSTATE *huff, const void *input, int input_size, void *output, int output_size); -int huffman_decompress(HUFFSTATE *huff, const void *input, int input_size, void *output, int output_size); +#ifdef __cplusplus +} +#endif -int huffman_test(); +#endif /* __HUFFMAN_HEADER__ */ |