|
1 /* |
|
2 * Copyright (c) 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 "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 * |
|
16 */ |
|
17 |
|
18 #ifndef __HUFFMAN_H__ |
|
19 #define __HUFFMAN_H__ |
|
20 |
|
21 #include "e32defwrap.h" |
|
22 #include <fstream> |
|
23 |
|
24 |
|
25 |
|
26 /** |
|
27 Base class for E32Image Compression Errors. |
|
28 @internalComponent |
|
29 @released |
|
30 */ |
|
31 class E32ImageCompressionError |
|
32 { |
|
33 public: |
|
34 enum |
|
35 { |
|
36 HUFFMANBUFFEROVERFLOWERROR = -1, |
|
37 HUFFMANBUFFERUNDERFLOWERROR = -2, |
|
38 HUFFMANTOOMANYCODESERROR = -3, |
|
39 HUFFMANINVALIDCODINGERROR = -4, |
|
40 }; |
|
41 public: |
|
42 E32ImageCompressionError(int aError) |
|
43 { |
|
44 iError = aError; |
|
45 } |
|
46 |
|
47 public: |
|
48 int iError; |
|
49 }; |
|
50 |
|
51 |
|
52 |
|
53 /** |
|
54 Class for Bit input stream. |
|
55 Good for reading bit streams for packed, compressed or huffman data algorithms. |
|
56 @since 8.0 |
|
57 @library euser.lib |
|
58 @internalComponent |
|
59 @released |
|
60 */ |
|
61 class TBitInput |
|
62 { |
|
63 public: |
|
64 TBitInput(); |
|
65 TBitInput(const TUint8* aPtr, TInt aLength, TInt aOffset=0); |
|
66 void Set(const TUint8* aPtr, TInt aLength, TInt aOffset=0); |
|
67 TUint ReadL(); |
|
68 TUint ReadL(TInt aSize); |
|
69 TUint HuffmanL(const TUint32* aTree); |
|
70 private: |
|
71 virtual void UnderflowL(); |
|
72 private: |
|
73 TInt iCount; |
|
74 TUint iBits; |
|
75 TInt iRemain; |
|
76 const TUint32* iPtr; |
|
77 }; |
|
78 |
|
79 /** |
|
80 Class derived from TBitInput |
|
81 @internalComponent |
|
82 @released |
|
83 */ |
|
84 class TFileInput : public TBitInput |
|
85 { |
|
86 public: |
|
87 TFileInput(unsigned char* source,int size); |
|
88 ~TFileInput(); |
|
89 private: |
|
90 void UnderflowL(); |
|
91 private: |
|
92 TUint8* iReadBuf; |
|
93 TInt iSize; |
|
94 }; |
|
95 /* |
|
96 Class for Huffman code toolkit. |
|
97 |
|
98 This class builds a huffman encoding from a frequency table and builds a decoding tree from a |
|
99 code-lengths table. |
|
100 |
|
101 The encoding generated is based on the rule that given two symbols s1 and s2, with code |
|
102 length l1 and l2, and huffman codes h1 and h2: |
|
103 if l1<l2 then h1<h2 when compared lexicographically |
|
104 if l1==l2 and s1<s2 then h1<h2 ditto |
|
105 |
|
106 This allows the encoding to be stored compactly as a table of code lengths |
|
107 |
|
108 @since 8.0 |
|
109 @library euser.lib |
|
110 @internalComponent |
|
111 @released |
|
112 */ |
|
113 class Huffman |
|
114 { |
|
115 public: |
|
116 enum {KMaxCodeLength=27}; |
|
117 enum {KMetaCodes=KMaxCodeLength+1}; |
|
118 enum {KMaxCodes=0x8000}; |
|
119 public: |
|
120 static void HuffmanL(const TUint32 aFrequency[],TInt aNumCodes,TUint32 aHuffman[]); |
|
121 static void Encoding(const TUint32 aHuffman[],TInt aNumCodes,TUint32 aEncodeTable[]); |
|
122 static TBool IsValid(const TUint32 aHuffman[],TInt aNumCodes); |
|
123 static void Decoding(const TUint32 aHuffman[],TInt aNumCodes,TUint32 aDecodeTree[],TInt aSymbolBase=0); |
|
124 static void InternalizeL(TBitInput& aInput,TUint32 aHuffman[],TInt aNumCodes); |
|
125 }; |
|
126 |
|
127 // local definitions used for Huffman code generation |
|
128 typedef TUint16 THuff; /** @internal */ |
|
129 const THuff KLeaf=0x8000; /** @internal */ |
|
130 struct TNode |
|
131 /** @internal */ |
|
132 { |
|
133 TUint iCount; |
|
134 THuff iLeft; |
|
135 THuff iRight; |
|
136 }; |
|
137 |
|
138 const TInt KDeflateLengthMag=8; |
|
139 const TInt KDeflateDistanceMag=12; |
|
140 |
|
141 /** |
|
142 class for TEncoding |
|
143 @internalComponent |
|
144 @released |
|
145 */ |
|
146 class TEncoding |
|
147 { |
|
148 public: |
|
149 enum {ELiterals=256,ELengths=(KDeflateLengthMag-1)*4,ESpecials=1,EDistances=(KDeflateDistanceMag-1)*4}; |
|
150 enum {ELitLens=ELiterals+ELengths+ESpecials}; |
|
151 enum {EEos=ELiterals+ELengths}; |
|
152 public: |
|
153 TUint32 iLitLen[ELitLens]; |
|
154 TUint32 iDistance[EDistances]; |
|
155 }; |
|
156 |
|
157 const TInt KDeflationCodes=TEncoding::ELitLens+TEncoding::EDistances; |
|
158 |
|
159 #endif |
|
160 |