|
1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // Huffman Class for deflate and inflate |
|
15 // @internalComponent |
|
16 // @released |
|
17 // |
|
18 // |
|
19 |
|
20 #ifndef __HUFFMAN_H__ |
|
21 #define __HUFFMAN_H__ |
|
22 |
|
23 #include "e32defwrap.h" |
|
24 #include <fstream> |
|
25 |
|
26 /** |
|
27 class Bit Output stream |
|
28 @internalComponent |
|
29 @released |
|
30 */ |
|
31 class TBitOutput |
|
32 { |
|
33 public: |
|
34 TBitOutput(); |
|
35 TBitOutput(TUint8* aBuf,TInt aSize); |
|
36 inline void Set(TUint8* aBuf,TInt aSize); |
|
37 inline const TUint8* Ptr() const; |
|
38 inline TInt BufferedBits() const; |
|
39 void WriteL(TUint aValue, TInt aLength); |
|
40 void HuffmanL(TUint aHuffCode); |
|
41 void PadL(TUint aPadding); |
|
42 private: |
|
43 void DoWriteL(TUint aBits, TInt aSize); |
|
44 virtual void OverflowL(); |
|
45 private: |
|
46 TUint iCode; // code in production |
|
47 TInt iBits; |
|
48 TUint8* iPtr; |
|
49 TUint8* iEnd; |
|
50 }; |
|
51 |
|
52 /* |
|
53 Set the memory buffer to use for output |
|
54 |
|
55 Data will be written to this buffer until it is full, at which point OverflowL() will be |
|
56 called. This should handle the data and then can Set() again to reset the buffer for further |
|
57 output. |
|
58 |
|
59 @param "TUint8* aBuf" The buffer for output |
|
60 @param "TInt aSize" The size of the buffer in bytes |
|
61 @internalComponent |
|
62 @released |
|
63 */ |
|
64 inline void TBitOutput::Set(TUint8* aBuf,TInt aSize) |
|
65 { |
|
66 iPtr=aBuf; |
|
67 iEnd=aBuf+aSize; |
|
68 } |
|
69 |
|
70 /* |
|
71 Get the current write position in the output buffer |
|
72 |
|
73 In conjunction with the address of the buffer, which should be known to the caller, this |
|
74 describes the data in the bitstream. |
|
75 @internalComponent |
|
76 @released |
|
77 */ |
|
78 inline const TUint8* TBitOutput::Ptr() const |
|
79 { |
|
80 return iPtr; |
|
81 } |
|
82 |
|
83 /* |
|
84 Get the number of bits that are buffered |
|
85 |
|
86 This reports the number of bits that have not yet been written into the output buffer.It will |
|
87 always lie in the range 0..7. Use PadL() to pad the data out to the next byte and write it to |
|
88 the buffer. |
|
89 @internalComponent |
|
90 @released |
|
91 */ |
|
92 inline TInt TBitOutput::BufferedBits() const |
|
93 { |
|
94 return iBits+8; |
|
95 } |
|
96 |
|
97 /** |
|
98 This class is derived from TBitOutput |
|
99 @internalComponent |
|
100 @released |
|
101 */ |
|
102 class TFileOutput : public TBitOutput |
|
103 { |
|
104 enum {KBufSize=0x1000}; |
|
105 public: |
|
106 TFileOutput(std::ofstream & os); |
|
107 void FlushL(); |
|
108 TUint32 iDataCount; |
|
109 private: |
|
110 void OverflowL(); |
|
111 private: |
|
112 std::ofstream & iOutStream; |
|
113 TUint8 iBuf[KBufSize]; |
|
114 }; |
|
115 |
|
116 /** |
|
117 Class for Bit input stream. |
|
118 Good for reading bit streams for packed, compressed or huffman data algorithms. |
|
119 @since 8.0 |
|
120 @library euser.lib |
|
121 @internalComponent |
|
122 @released |
|
123 */ |
|
124 class TBitInput |
|
125 { |
|
126 public: |
|
127 TBitInput(); |
|
128 TBitInput(const TUint8* aPtr, TInt aLength, TInt aOffset=0); |
|
129 void Set(const TUint8* aPtr, TInt aLength, TInt aOffset=0); |
|
130 TUint ReadL(); |
|
131 TUint ReadL(TInt aSize); |
|
132 TUint HuffmanL(const TUint32* aTree); |
|
133 private: |
|
134 virtual void UnderflowL(); |
|
135 private: |
|
136 TInt iCount; |
|
137 TUint iBits; |
|
138 TInt iRemain; |
|
139 const TUint32* iPtr; |
|
140 }; |
|
141 |
|
142 /** |
|
143 Class derived from TBitInput |
|
144 @internalComponent |
|
145 @released |
|
146 */ |
|
147 class TFileInput : public TBitInput |
|
148 { |
|
149 public: |
|
150 TFileInput(unsigned char* source,int size); |
|
151 ~TFileInput(); |
|
152 private: |
|
153 void UnderflowL(); |
|
154 private: |
|
155 TUint8* iReadBuf; |
|
156 TInt iSize; |
|
157 }; |
|
158 |
|
159 /* |
|
160 Class for Huffman code toolkit. |
|
161 |
|
162 This class builds a huffman encoding from a frequency table and builds a decoding tree from a |
|
163 code-lengths table. |
|
164 |
|
165 The encoding generated is based on the rule that given two symbols s1 and s2, with code |
|
166 length l1 and l2, and huffman codes h1 and h2: |
|
167 if l1<l2 then h1<h2 when compared lexicographically |
|
168 if l1==l2 and s1<s2 then h1<h2 ditto |
|
169 |
|
170 This allows the encoding to be stored compactly as a table of code lengths |
|
171 |
|
172 @since 8.0 |
|
173 @library euser.lib |
|
174 @internalComponent |
|
175 @released |
|
176 */ |
|
177 class Huffman |
|
178 { |
|
179 public: |
|
180 enum {KMaxCodeLength=27}; |
|
181 enum {KMetaCodes=KMaxCodeLength+1}; |
|
182 enum {KMaxCodes=0x8000}; |
|
183 public: |
|
184 static void HuffmanL(const TUint32 aFrequency[],TInt aNumCodes,TUint32 aHuffman[]); |
|
185 static void Encoding(const TUint32 aHuffman[],TInt aNumCodes,TUint32 aEncodeTable[]); |
|
186 static TBool IsValid(const TUint32 aHuffman[],TInt aNumCodes); |
|
187 static void ExternalizeL(TBitOutput& aOutput,const TUint32 aHuffman[],TInt aNumCodes); |
|
188 static void Decoding(const TUint32 aHuffman[],TInt aNumCodes,TUint32 aDecodeTree[],TInt aSymbolBase=0); |
|
189 static void InternalizeL(TBitInput& aInput,TUint32 aHuffman[],TInt aNumCodes); |
|
190 }; |
|
191 |
|
192 // local definitions used for Huffman code generation |
|
193 typedef TUint16 THuff; /** @internal */ |
|
194 const THuff KLeaf=0x8000; /** @internal */ |
|
195 struct TNode |
|
196 /** @internal */ |
|
197 { |
|
198 TUint iCount; |
|
199 THuff iLeft; |
|
200 THuff iRight; |
|
201 }; |
|
202 |
|
203 const TInt KDeflateLengthMag=8; |
|
204 const TInt KDeflateDistanceMag=12; |
|
205 |
|
206 /** |
|
207 class for TEncoding |
|
208 @internalComponent |
|
209 @released |
|
210 */ |
|
211 class TEncoding |
|
212 { |
|
213 public: |
|
214 enum {ELiterals=256,ELengths=(KDeflateLengthMag-1)*4,ESpecials=1,EDistances=(KDeflateDistanceMag-1)*4}; |
|
215 enum {ELitLens=ELiterals+ELengths+ESpecials}; |
|
216 enum {EEos=ELiterals+ELengths}; |
|
217 public: |
|
218 TUint32 iLitLen[ELitLens]; |
|
219 TUint32 iDistance[EDistances]; |
|
220 }; |
|
221 |
|
222 const TInt KDeflationCodes=TEncoding::ELitLens+TEncoding::EDistances; |
|
223 |
|
224 #endif |
|
225 |