~jeanfrancois.roy/mpqkit/1.0b1

« back to all changes in this revision

Viewing changes to stormlib2/huffman/huffman.h

  • Committer: bahamut
  • Date: 2007-04-03 01:12:37 UTC
  • Revision ID: svn-v4:08a27de9-96f8-0310-9aec-cd9f9b5b01a8:trunk:158
- svn refactor phase 2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************/
 
2
/* huffman.h                              Copyright (c) Ladislav Zezula 2003 */
 
3
/*---------------------------------------------------------------------------*/
 
4
/* Description :                                                             */
 
5
/*---------------------------------------------------------------------------*/
 
6
/*   Date    Ver   Who  Comment                                              */
 
7
/* --------  ----  ---  -------                                              */
 
8
/* xx.xx.xx  1.00  Lad  The first version of huffman.h                       */
 
9
/* 03.05.03  2.00  Lad  Added compression                                    */
 
10
/*****************************************************************************/
 
11
 
 
12
#ifndef __HUFFMAN_H__
 
13
#define __HUFFMAN_H__
 
14
 
 
15
#include <stdio.h>
 
16
 
 
17
//-----------------------------------------------------------------------------
 
18
// Defines
 
19
 
 
20
#define INSERT_ITEM    1                   
 
21
#define SWITCH_ITEMS   2                    // Switch the item1 and item2
 
22
 
 
23
#define PTR_NOT(ptr)  (THTreeItem *)(~(uint32_t)(ptr))
 
24
#define PTR_PTR(ptr)  ((THTreeItem *)(ptr))
 
25
#define PTR_INT(ptr)  (long)(ptr)
 
26
 
 
27
#ifndef NULL
 
28
#define NULL 0
 
29
#endif
 
30
 
 
31
//-----------------------------------------------------------------------------
 
32
// Structures and classes
 
33
 
 
34
// Input stream for Huffmann decompression
 
35
class TInputStream {
 
36
public:
 
37
    TInputStream(uint8_t *data, uint32_t data_size) {
 
38
        this->buffer = data;
 
39
        this->buffer_bit_size = ((int64_t)data_size) << 3;
 
40
        
 
41
        this->bit_bucket = 0;
 
42
        this->bit_count = 0;
 
43
    }
 
44
    
 
45
    uint32_t GetBit();
 
46
    uint32_t Get8Bits();
 
47
    
 
48
    uint32_t Peek7Bits();
 
49
    
 
50
    void ConsumeBits(uint32_t count);
 
51
 
 
52
private:
 
53
    uint8_t* buffer;
 
54
    int64_t buffer_bit_size;
 
55
    
 
56
    uint32_t bit_bucket;
 
57
    uint32_t bit_count;
 
58
};
 
59
 
 
60
// Output stream for Huffmann compression
 
61
class TOutputStream
 
62
{
 
63
    public:
 
64
 
 
65
    void PutBits(unsigned long dwBuff, unsigned int nPutBits);
 
66
 
 
67
    unsigned char * pbOutBuffer;        // 00 : Output buffer
 
68
    unsigned long   dwOutSize;          // 04 : Size of output buffer
 
69
    unsigned char * pbOutPos;           // 08 : Current output position
 
70
    unsigned long   dwBitBuff;          // 0C : Bit buffer
 
71
    unsigned long   nBits;              // 10 : Number of bits in the bit buffer
 
72
};
 
73
 
 
74
// Huffmann tree item (?)
 
75
struct THTreeItem
 
76
{
 
77
    public:
 
78
    
 
79
    THTreeItem * Call1501DB70(THTreeItem * pLast);
 
80
    THTreeItem * GetPrevItem(long value);
 
81
    void         ClearItemLinks();
 
82
    void         RemoveItem();
 
83
 
 
84
    THTreeItem  * next;                 // 00 - Pointer to next THTreeItem
 
85
    THTreeItem  * prev;                 // 04 - Pointer to prev THTreeItem (< 0 if none)
 
86
    unsigned long dcmpByte;             // 08 - Index of this item in item pointer array, decompressed byte value
 
87
    unsigned long byteValue;            // 0C - Some byte value
 
88
    THTreeItem  * parent;               // 10 - Pointer to parent THTreeItem (NULL if none)
 
89
    THTreeItem  * child;                // 14 - Pointer to child  THTreeItem
 
90
};
 
91
 
 
92
// Structure used for quick decompress. The 'bitCount' contains number of bits
 
93
// and byte value contains result decompressed byte value.
 
94
// After each walk through Huffman tree are filled all entries which are
 
95
// multiplies of number of bits loaded from input stream. These entries
 
96
// contain number of bits and result value. At the next 7 bits is tested this
 
97
// structure first. If corresponding entry found, decompression routine will
 
98
// not walk through Huffman tree and directly stores output byte to output stream.
 
99
struct TQDecompress
 
100
{
 
101
    unsigned long offs00;               // 00 - 1 if resolved
 
102
    unsigned long nBits;                // 04 - Bit count
 
103
    union
 
104
    {
 
105
        unsigned long dcmpByte;         // 08 - Byte value for decompress (if bitCount <= 7)
 
106
        THTreeItem  * pItem;            // 08 - THTreeItem (if number of bits is greater than 7
 
107
    };
 
108
};
 
109
 
 
110
// Structure for Huffman tree (Size 0x3674 bytes). Because I'm not expert
 
111
// for the decompression, I do not know actually if the class is really a Hufmann
 
112
// tree. If someone knows the decompression details, please let me know
 
113
class THuffmannTree
 
114
{
 
115
    public:
 
116
    
 
117
    void  InitTree(bool bCompression);
 
118
    void  BuildTree(unsigned int nCmpType);
 
119
//  void  ModifyTree(unsigned long dwIndex);
 
120
//  void  UninitTree();
 
121
 
 
122
//  void  Call15007010(Bit32 dwInLength, THTreeItem * item);
 
123
    THTreeItem * Call1500E740(unsigned int nValue);
 
124
    void         Call1500E820(THTreeItem * pItem);
 
125
    unsigned int DoCompression(TOutputStream * os, unsigned char * pbInBuffer, int nInLength, int nCmpType);
 
126
    unsigned int DoDecompression(unsigned char * pbOutBuffer, unsigned int dwOutLength, TInputStream * is);
 
127
 
 
128
    unsigned long bIsCmp0;              // 0000 - 1 if compression type 0
 
129
    unsigned long offs0004;             // 0004 - Some flag
 
130
    THTreeItem    items0008[0x203];     // 0008 - HTree items
 
131
 
 
132
    //- Sometimes used as HTree item -----------
 
133
    THTreeItem  * pItem3050;            // 3050 - Always NULL (?)
 
134
    THTreeItem  * pItem3054;            // 3054 - Pointer to Huffman tree item
 
135
    THTreeItem  * pItem3058;            // 3058 - Pointer to Huffman tree item (< 0 if invalid)
 
136
 
 
137
    //- Sometimes used as HTree item -----------
 
138
    THTreeItem  * pItem305C;            // 305C - Usually NULL
 
139
    THTreeItem  * pFirst;               // 3060 - Pointer to top (first) Huffman tree item
 
140
    THTreeItem  * pLast;                // 3064 - Pointer to bottom (last) Huffman tree item (< 0 if invalid)
 
141
    unsigned long nItems;               // 3068 - Number of used HTree items
 
142
 
 
143
    //-------------------------------------------
 
144
    THTreeItem * items306C[0x102];      // 306C - THTreeItem pointer array
 
145
    TQDecompress qd3474[0x80];          // 3474 - Array for quick decompression
 
146
 
 
147
    static unsigned char Table1502A630[];// Some table
 
148
};
 
149
 
 
150
#endif // __HUFFMAN_H__