|
1 /* |
|
2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * e32tools\petran\Szip\huffman.h |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 #ifndef __HUFFMAN_H__ |
|
21 #define __HUFFMAN_H__ |
|
22 |
|
23 #include <e32std.h> |
|
24 |
|
25 /** Bit output stream. |
|
26 Good for writing bit streams for packed, compressed or huffman data algorithms. |
|
27 |
|
28 This class must be derived from and OverflowL() reimplemented if the bitstream data |
|
29 cannot be generated into a single memory buffer. |
|
30 |
|
31 @since 8.0 |
|
32 @library euser.lib |
|
33 */ |
|
34 class TBitOutput |
|
35 { |
|
36 public: |
|
37 TBitOutput(); |
|
38 TBitOutput(TUint8* aBuf,TInt aSize); |
|
39 inline virtual ~TBitOutput() { } |
|
40 inline void Set(TUint8* aBuf,TInt aSize); |
|
41 inline const TUint8* Ptr() const; |
|
42 inline TInt BufferedBits() const; |
|
43 // |
|
44 void WriteL(TUint aValue, TInt aLength); |
|
45 void HuffmanL(TUint aHuffCode); |
|
46 void PadL(TUint aPadding); |
|
47 private: |
|
48 void DoWriteL(TUint aBits, TInt aSize); |
|
49 virtual void OverflowL(); |
|
50 private: |
|
51 TUint iCode; // code in production |
|
52 TInt iBits; |
|
53 TUint8* iPtr; |
|
54 TUint8* iEnd; |
|
55 }; |
|
56 |
|
57 /** Set the memory buffer to use for output |
|
58 |
|
59 Data will be written to this buffer until it is full, at which point OverflowL() will |
|
60 be called. This should handle the data and then can Set() again to reset the buffer |
|
61 for further output. |
|
62 |
|
63 @param "TUint8* aBuf" The buffer for output |
|
64 @param "TInt aSize" The size of the buffer in bytes |
|
65 */ |
|
66 inline void TBitOutput::Set(TUint8* aBuf,TInt aSize) |
|
67 {iPtr=aBuf;iEnd=aBuf+aSize;} |
|
68 /** Get the current write position in the output buffer |
|
69 |
|
70 In conjunction with the address of the buffer, which should be known to the |
|
71 caller, this describes the data in the bitstream. |
|
72 */ |
|
73 inline const TUint8* TBitOutput::Ptr() const |
|
74 {return iPtr;} |
|
75 /** Get the number of bits that are buffered |
|
76 |
|
77 This reports the number of bits that have not yet been written into the |
|
78 output buffer. It will always lie in the range 0..7. Use PadL() to |
|
79 pad the data out to the next byte and write it to the buffer. |
|
80 */ |
|
81 inline TInt TBitOutput::BufferedBits() const |
|
82 {return iBits+8;} |
|
83 |
|
84 |
|
85 /** Bit input stream. |
|
86 Good for reading bit streams for packed, compressed or huffman data algorithms. |
|
87 @since 8.0 |
|
88 @library euser.lib |
|
89 */ |
|
90 class TBitInput |
|
91 { |
|
92 public: |
|
93 TBitInput(); |
|
94 TBitInput(const TUint8* aPtr, TInt aLength, TInt aOffset=0); |
|
95 void Set(const TUint8* aPtr, TInt aLength, TInt aOffset=0); |
|
96 inline virtual ~TBitInput() { } |
|
97 TUint ReadL(); |
|
98 TUint ReadL(TInt aSize); |
|
99 TUint HuffmanL(const TUint32* aTree); |
|
100 private: |
|
101 virtual void UnderflowL(); |
|
102 private: |
|
103 TInt iCount; |
|
104 TUint iBits; |
|
105 TInt iRemain; |
|
106 const TUint32* iPtr; |
|
107 }; |
|
108 |
|
109 /** Huffman code toolkit. |
|
110 |
|
111 This class builds a huffman encoding from a frequency table and builds |
|
112 a decoding tree from a code-lengths table |
|
113 |
|
114 The encoding generated is based on the rule that given two symbols s1 and s2, with |
|
115 code length l1 and l2, and huffman codes h1 and h2: |
|
116 |
|
117 if l1<l2 then h1<h2 when compared lexicographically |
|
118 if l1==l2 and s1<s2 then h1<h2 ditto |
|
119 |
|
120 This allows the encoding to be stored compactly as a table of code lengths |
|
121 |
|
122 @since 8.0 |
|
123 @library euser.lib |
|
124 */ |
|
125 class Huffman |
|
126 { |
|
127 public: |
|
128 enum {KMaxCodeLength=27}; |
|
129 enum {KMetaCodes=KMaxCodeLength+1}; |
|
130 enum {KMaxCodes=0x8000}; |
|
131 public: |
|
132 static void HuffmanL(const TUint32 aFrequency[],TInt aNumCodes,TUint32 aHuffman[]); |
|
133 static void Encoding(const TUint32 aHuffman[],TInt aNumCodes,TUint32 aEncodeTable[]); |
|
134 static void Decoding(const TUint32 aHuffman[],TInt aNumCodes,TUint32 aDecodeTree[],TInt aSymbolBase=0); |
|
135 static TBool IsValid(const TUint32 aHuffman[],TInt aNumCodes); |
|
136 // |
|
137 static void ExternalizeL(TBitOutput& aOutput,const TUint32 aHuffman[],TInt aNumCodes); |
|
138 static void InternalizeL(TBitInput& aInput,TUint32 aHuffman[],TInt aNumCodes); |
|
139 }; |
|
140 |
|
141 #endif |
|
142 |