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