imgtools/imglib/e32image/deflate/deflate.cpp
changeset 2 39c28ec933dd
equal deleted inserted replaced
1:820b22e13ff1 2:39c28ec933dd
       
     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\deflate.cpp
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "deflate.h"
       
    21 #include "h_utl.h"
       
    22 #include "panic.h"
       
    23 #define OFFSETOF(c,f) (((TInt)&(((c *)0x1000)->f))-0x1000)
       
    24 
       
    25 class HDeflateHash
       
    26 	{
       
    27 public:
       
    28 	inline static HDeflateHash* NewLC(TInt aLinks);
       
    29 //
       
    30 	inline TInt First(const TUint8* aPtr,TInt aPos);
       
    31 	inline TInt Next(TInt aPos,TInt aOffset) const;
       
    32 private:
       
    33 	inline HDeflateHash();
       
    34 	inline static TInt Hash(const TUint8* aPtr);
       
    35 private:
       
    36 	typedef TUint16 TOffset;
       
    37 private:
       
    38 	TInt iHash[256];
       
    39 	TOffset iOffset[1];	// or more
       
    40 	};
       
    41 
       
    42 class MDeflater
       
    43 	{
       
    44 public:
       
    45 	void DeflateL(const TUint8* aBase,TInt aLength);
       
    46 	inline virtual ~MDeflater() { }; 
       
    47 private:
       
    48 	const TUint8* DoDeflateL(const TUint8* aBase,const TUint8* aEnd,HDeflateHash& aHash);
       
    49 	static TInt Match(const TUint8* aPtr,const TUint8* aEnd,TInt aPos,HDeflateHash& aHas);
       
    50 	void SegmentL(TInt aLength,TInt aDistance);
       
    51 	virtual void LitLenL(TInt aCode) =0;
       
    52 	virtual void OffsetL(TInt aCode) =0;
       
    53 	virtual void ExtraL(TInt aLen,TUint aBits) =0;
       
    54 	};
       
    55 
       
    56 class TDeflateStats : public MDeflater
       
    57 	{
       
    58 public:
       
    59 	inline TDeflateStats(TEncoding& aEncoding);
       
    60 	inline virtual ~TDeflateStats() { } 
       
    61 private:
       
    62 // from MDeflater
       
    63 	void LitLenL(TInt aCode);
       
    64 	void OffsetL(TInt aCode);
       
    65 	void ExtraL(TInt aLen,TUint aBits);
       
    66 private:
       
    67 	TEncoding& iEncoding;
       
    68 	};
       
    69 
       
    70 class TDeflater : public MDeflater
       
    71 	{
       
    72 public:
       
    73 	inline TDeflater(TBitOutput& aOutput,const TEncoding& aEncoding);
       
    74 	inline virtual ~TDeflater() { }; 
       
    75 private:
       
    76 // from MDeflater
       
    77 	void LitLenL(TInt aCode);
       
    78 	void OffsetL(TInt aCode);
       
    79 	void ExtraL(TInt aLen,TUint aBits);
       
    80 private:
       
    81 	TBitOutput& iOutput;
       
    82 	const TEncoding& iEncoding;
       
    83 	};
       
    84 
       
    85 
       
    86 // Class HDeflateHash
       
    87 
       
    88 inline HDeflateHash::HDeflateHash()
       
    89 	{TInt* p=iHash+256;do *--p=-KDeflateMaxDistance-1; while (p>iHash);}
       
    90 
       
    91 inline HDeflateHash* HDeflateHash::NewLC(TInt aLinks)
       
    92 	{
       
    93 	return new(new char[OFFSETOF(HDeflateHash,iOffset[0]) + 
       
    94 		(sizeof(TOffset) * Min(aLinks,KDeflateMaxDistance))]) HDeflateHash;
       
    95 	}
       
    96 
       
    97 inline TInt HDeflateHash::Hash(const TUint8* aPtr)
       
    98 	{
       
    99 	TUint x=aPtr[0]|(aPtr[1]<<8)|(aPtr[2]<<16);
       
   100 	return (x*KDeflateHashMultiplier)>>KDeflateHashShift;
       
   101 	}
       
   102 
       
   103 inline TInt HDeflateHash::First(const TUint8* aPtr,TInt aPos)
       
   104 	{
       
   105 	TInt h=Hash(aPtr);
       
   106 	TInt offset=Min(aPos-iHash[h],KDeflateMaxDistance<<1);
       
   107 	iHash[h]=aPos;
       
   108 	iOffset[aPos&(KDeflateMaxDistance-1)]=TOffset(offset);
       
   109 	return offset;
       
   110 	}
       
   111 
       
   112 inline TInt HDeflateHash::Next(TInt aPos,TInt aOffset) const
       
   113 	{return aOffset+iOffset[(aPos-aOffset)&(KDeflateMaxDistance-1)];}
       
   114 
       
   115 
       
   116 // Class TDeflater
       
   117 //
       
   118 // generic deflation algorithm, can do either statistics and the encoder
       
   119 
       
   120 TInt MDeflater::Match(const TUint8* aPtr,const TUint8* aEnd,TInt aPos,HDeflateHash& aHash)
       
   121 	{
       
   122 	TInt offset=aHash.First(aPtr,aPos);
       
   123 	if (offset>KDeflateMaxDistance)
       
   124 		return 0;
       
   125 	TInt match=0;
       
   126 	aEnd=Min(aEnd,aPtr+KDeflateMaxLength);
       
   127 	TUint8 c=*aPtr;
       
   128 	do
       
   129 		{
       
   130 		const TUint8* p=aPtr-offset;
       
   131 		if (p[match>>16]==c)
       
   132 			{	// might be a better match
       
   133 			const TUint8* m=aPtr;
       
   134 			for (;;)
       
   135 				{
       
   136 				if (*p++!=*m++)
       
   137 					break;
       
   138 				if (m<aEnd)
       
   139 					continue;
       
   140 				return ((m-aPtr)<<16)|offset;
       
   141 				}
       
   142 			TInt l=m-aPtr-1;
       
   143 			if (l>match>>16)
       
   144 				{
       
   145 				match=(l<<16)|offset;
       
   146 				c=m[-1];
       
   147 				}
       
   148 			}
       
   149 		offset=aHash.Next(aPos,offset);
       
   150 		} while (offset<=KDeflateMaxDistance);
       
   151 	return match;
       
   152 	}
       
   153 
       
   154 const TUint8* MDeflater::DoDeflateL(const TUint8* aBase,const TUint8* aEnd,HDeflateHash& aHash)
       
   155 //
       
   156 // Apply the deflation algorithm to the data [aBase,aEnd)
       
   157 // Return a pointer after the last byte that was deflated (which may not be aEnd)
       
   158 //
       
   159 	{
       
   160 	const TUint8* ptr=aBase;
       
   161 	TInt prev=0;		// the previous deflation match
       
   162 	do
       
   163 		{
       
   164 		TInt match=Match(ptr,aEnd,ptr-aBase,aHash);
       
   165 // Extra deflation applies two optimisations which double the time taken
       
   166 // 1. If we have a match at p, then test for a better match at p+1 before using it
       
   167 // 2. When we have a match, add the hash links for all the data which will be skipped 
       
   168 		if (match>>16 < prev>>16)
       
   169 			{	// use the previous match--it was better
       
   170 			TInt len=prev>>16;
       
   171 			SegmentL(len,prev-(len<<16));
       
   172 			// fill in missing hash entries for better compression
       
   173 			const TUint8* e=ptr+len-2;
       
   174 			do 
       
   175 				{
       
   176 				++ptr;
       
   177 				if (ptr + 2 < aEnd)
       
   178 					aHash.First(ptr,ptr-aBase);
       
   179 				} while (ptr<e);
       
   180 			prev=0;
       
   181 			}
       
   182 		else if (match<=(KDeflateMinLength<<16))
       
   183 			LitLenL(*ptr);			// no deflation match here
       
   184 		else
       
   185 			{	// save this match and test the next position
       
   186 			if (prev)	// we had a match at ptr-1, but this is better
       
   187 				LitLenL(ptr[-1]);
       
   188 			prev=match;
       
   189 			}
       
   190 		++ptr;
       
   191 		} while (ptr+KDeflateMinLength-1<aEnd);
       
   192 	if (prev)
       
   193 		{		// emit the stored match
       
   194 		TInt len=prev>>16;
       
   195 		SegmentL(len,prev-(len<<16));
       
   196 		ptr+=len-1;
       
   197 		}
       
   198 	return ptr;
       
   199 	}
       
   200 
       
   201 void MDeflater::DeflateL(const TUint8* aBase,TInt aLength)
       
   202 //
       
   203 // The generic deflation algorithm
       
   204 //
       
   205 	{
       
   206 	const TUint8* end=aBase+aLength;
       
   207 	if (aLength>KDeflateMinLength)
       
   208 		{	// deflation kicks in if there is enough data
       
   209 		HDeflateHash* hash=HDeflateHash::NewLC(aLength);
       
   210 		if(hash==NULL)
       
   211 			Panic(EHuffmanOutOfMemory);
       
   212 
       
   213 		aBase=DoDeflateL(aBase,end,*hash);
       
   214 		delete hash;
       
   215 		}
       
   216 	while (aBase<end)					// emit remaining bytes
       
   217 		LitLenL(*aBase++);
       
   218 	LitLenL(TEncoding::EEos);	// eos marker
       
   219 	}
       
   220 
       
   221 void MDeflater::SegmentL(TInt aLength,TInt aDistance)
       
   222 //
       
   223 // Turn a (length,offset) pair into the deflation codes+extra bits before calling
       
   224 // the specific LitLen(), Offset() and Extra() functions.
       
   225 //
       
   226 	{
       
   227 	aLength-=KDeflateMinLength;
       
   228 	TInt extralen=0;
       
   229 	TUint len=aLength;
       
   230 	while (len>=8)
       
   231 		{
       
   232 		++extralen;
       
   233 		len>>=1;
       
   234 		}
       
   235 	LitLenL((extralen<<2)+len+TEncoding::ELiterals);
       
   236 	if (extralen)
       
   237 		ExtraL(extralen,aLength);
       
   238 //
       
   239 	aDistance--;
       
   240 	extralen=0;
       
   241 	TUint dist=aDistance;
       
   242 	while (dist>=8)
       
   243 		{
       
   244 		++extralen;
       
   245 		dist>>=1;
       
   246 		}
       
   247 	OffsetL((extralen<<2)+dist);
       
   248 	if (extralen)
       
   249 		ExtraL(extralen,aDistance);
       
   250 	}
       
   251 
       
   252 // Class TDeflateStats
       
   253 //
       
   254 // This class analyses the data stream to generate the frequency tables 
       
   255 // for the deflation algorithm
       
   256 
       
   257 inline TDeflateStats::TDeflateStats(TEncoding& aEncoding)
       
   258 	:iEncoding(aEncoding)
       
   259 	{}
       
   260 
       
   261 void TDeflateStats::LitLenL(TInt aCode)
       
   262 	{
       
   263 	++iEncoding.iLitLen[aCode];
       
   264 	}
       
   265 
       
   266 void TDeflateStats::OffsetL(TInt aCode)
       
   267 	{
       
   268 	++iEncoding.iDistance[aCode];
       
   269 	}
       
   270 
       
   271 void TDeflateStats::ExtraL(TInt,TUint)
       
   272 	{}
       
   273 
       
   274 // Class TDeflater
       
   275 //
       
   276 // Extends MDeflater to provide huffman encoding of the output
       
   277 
       
   278 inline TDeflater::TDeflater(TBitOutput& aOutput,const TEncoding& aEncoding)
       
   279 //
       
   280 // construct for encoding
       
   281 //
       
   282 	:iOutput(aOutput),iEncoding(aEncoding)
       
   283 	{}
       
   284 
       
   285 void TDeflater::LitLenL(TInt aCode)
       
   286 	{
       
   287 	iOutput.HuffmanL(iEncoding.iLitLen[aCode]);
       
   288 	}
       
   289 
       
   290 void TDeflater::OffsetL(TInt aCode)
       
   291 	{
       
   292 	iOutput.HuffmanL(iEncoding.iDistance[aCode]);
       
   293 	}
       
   294 
       
   295 void TDeflater::ExtraL(TInt aLen,TUint aBits)
       
   296 	{
       
   297 	iOutput.WriteL(aBits,aLen);
       
   298 	}
       
   299 
       
   300 void DoDeflateL(const TUint8* aBuf,TInt aLength,TBitOutput& aOutput,TEncoding& aEncoding)
       
   301 	{
       
   302 // analyse the data for symbol frequency 
       
   303 	TDeflateStats analyser(aEncoding);
       
   304 	analyser.DeflateL(aBuf,aLength);
       
   305 	
       
   306 // generate the required huffman encodings
       
   307 	Huffman::HuffmanL(aEncoding.iLitLen,TEncoding::ELitLens,aEncoding.iLitLen);
       
   308 	Huffman::HuffmanL(aEncoding.iDistance,TEncoding::EDistances,aEncoding.iDistance);
       
   309 
       
   310 // Store the encoding table
       
   311 	Huffman::ExternalizeL(aOutput,aEncoding.iLitLen,KDeflationCodes);
       
   312 
       
   313 // generate the tables
       
   314 	Huffman::Encoding(aEncoding.iLitLen,TEncoding::ELitLens,aEncoding.iLitLen);
       
   315 	Huffman::Encoding(aEncoding.iDistance,TEncoding::EDistances,aEncoding.iDistance);
       
   316 
       
   317 // now finally deflate the data with the generated encoding
       
   318 	TDeflater deflater(aOutput,aEncoding);
       
   319 	deflater.DeflateL(aBuf,aLength);
       
   320 	aOutput.PadL(1);
       
   321 	}
       
   322 
       
   323 void DeflateL(const TUint8* aBuf, TInt aLength, TBitOutput& aOutput)
       
   324 	{
       
   325 	TEncoding* encoding=new TEncoding;
       
   326 	HMem::FillZ(encoding,sizeof(TEncoding));
       
   327 	DoDeflateL(aBuf,aLength,aOutput,*encoding);
       
   328 	}
       
   329