mmplugins/imagingplugins/codecs/GifCodec/GIFcodec.h
changeset 0 40261b775718
equal deleted inserted replaced
-1:000000000000 0:40261b775718
       
     1 // Copyright (c) 1997-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 // Licensed under US Patent No 4,558,302 and foreign counterparts
       
    15 // 
       
    16 //
       
    17 
       
    18 #ifndef GIFCODEC_H
       
    19 #define GIFCODEC_H
       
    20 
       
    21 #include <icl/imageprocessor.h>
       
    22 #include <icl/imagecodec.h>
       
    23 #include <icl/imagecodecdata.h>
       
    24 
       
    25 #include "GIFConvert.h"
       
    26 #include "GIFFormat.h"
       
    27 
       
    28 // Constants.
       
    29 const TInt KGifMaxBits = 12;
       
    30 const TInt KGifConversionTableSize = 1<<KGifMaxBits; // 4096
       
    31 const TInt KGifHashTableSize = KGifConversionTableSize + 907; // a bit extra for 80% occupancy
       
    32 
       
    33 const TUint8 KGifExtensionId = 0x21;
       
    34 const TUint8 KGifImageDescriptorId = 0x2c;
       
    35 const TUint8 KGifPlainTextExtensionId = 0x01;
       
    36 const TUint8 KGifGraphicControlExtensionId = 0xf9;
       
    37 const TUint8 KGifCommentExtensionId = 0xfe;
       
    38 const TUint8 KGifApplicationExtensionId = 0xff;
       
    39 const TInt KGifInitialCodeLengthSize = 1;
       
    40 const TUint8 KGifFrameTerminatorId = 0x00;
       
    41 const TUint8 KGifTerminatorId = 0x3b;
       
    42 const TInt KGifCentiSecondsToMicroSeconds = 10000;
       
    43 
       
    44 const TInt KMinGifGraphicControlBlkLen = 4;
       
    45 const TInt KGifExtBlkHeaderSize = 3;
       
    46 const TInt KPixelBufMaxSize = 2048;//to get proper optimization must not be less then 1024 
       
    47 // (basically each additional 1024 pixels for the buffer adds about 5% to decoding speed 
       
    48 // untill buffer size increases up to quarter a CPU cash size 
       
    49 
       
    50 _LIT8(KAppIdNetscape, "NETSCAPE");
       
    51 _LIT8(KAppAuthenticCode2_0, "2.0");
       
    52 
       
    53 const TInt KAppIdBlockLength = 11;
       
    54 const TInt KAppIdLength = 8;
       
    55 const TInt KAppAuthenticCodeLength = 3;
       
    56 const TInt KLoopIterationsOffset = 14;
       
    57 
       
    58 
       
    59 // Read codec
       
    60 class CGifReadCodec : public CImageMaskProcessorReadCodec
       
    61 	{
       
    62 public:
       
    63 	virtual ~CGifReadCodec();
       
    64 
       
    65 	static CGifReadCodec* NewL(TRgb* aGlobalPalette,TInt aBackgroundColorIndex, TSize aScreenSize, TBool aFastDecode);
       
    66 
       
    67 	// From CImageReadCodec
       
    68 	virtual void InitFrameHeader(TFrameInfo& aFrameSettings, CFrameImageData& aFrameImageData);
       
    69 	virtual void InitFrameL(TFrameInfo& aFrameInfo, CFrameImageData& aFrameImageData, TBool aDisableErrorDiffusion, CFbsBitmap& aDestination, CFbsBitmap* aDestinationMask);
       
    70 	virtual TFrameState ProcessFrameHeaderL(TBufPtr8& aData);
       
    71 	virtual TFrameState ProcessFrameL(TBufPtr8& aSrc);
       
    72 	void SetUseFrameSizeInPixels(TBool aUseFrameSizeInPixels);
       
    73 private:
       
    74     typedef TUint32 TBitBuffer;
       
    75 	typedef TUint16 T64KPixel;
       
    76 	    
       
    77 	void ConstructL();
       
    78 	CGifReadCodec(TRgb* aGlobalPalette,TInt aBackgroundColorIndex, TSize aScreenSize, TBool aFastDecode);
       
    79 
       
    80 	void DoProcessInfoL(const TUint8*& aDataPtr,const TUint8* aDataPtrLimit);
       
    81 	void DoProcessExtensionL(const TUint8*& aDataPtr,const TUint8* aDataPtrLimit);
       
    82 	void DoProcessImageDescriptorL(const TUint8*& aDataPtr,const TUint8* aDataPtrLimit);
       
    83 	void DoProcessDataL();
       
    84 	void DoSaveCommentsL();
       
    85 	inline TInt ResetTableL();
       
    86 	inline TInt NextCode(TBitBuffer& aBitBuffer, TInt& aBitBufSize, TInt aCurCodeLen);
       
    87 	void WriteCodeL(TInt aCode);
       
    88 	void WriteStringWithoutTransparency(TUint8* aOutputString, TInt aPixSize, TInt aNumOfPixels);
       
    89 	void WriteStringWithTransparency(TUint8* aOutputString, TInt aPixSize, TUint32* aMaskString, TInt aNumOfPixels);
       
    90 	
       
    91 	inline void SetCurrentCodeLengthL(TInt aCodeLength);
       
    92 	inline void UpdateYPos();
       
    93 	void UpdateYPosInterlaced();
       
    94 	void FlushPixBuffer(TInt aPixSize);
       
    95 	
       
    96     template <class TPalType, TInt aPtrDelta> 
       
    97     inline TUint8* WriteGifBuffer(TUint8* aOutputStringPtr, TUint8* aOutputStringLimit);
       
    98     
       
    99 private:
       
   100 	
       
   101 	T64KPixel* i64KPalette;
       
   102 	const TRgb* iPalette;
       
   103 	
       
   104 	TInt iBackgroundColorIndex;
       
   105 	TUint32 iOpaqueMask;
       
   106 	TUint16 iTranspColIdx;
       
   107 	TUint16 iBufMode;
       
   108 	TInt16* iPrefixIndex;
       
   109 	TUint8* iSuffixCode;
       
   110 	TUint8* iOutputString;
       
   111 		
       
   112 	TUint32* iPixelBuffer;
       
   113 	TInt     iPixBufCount;
       
   114 	TInt     iPixRead;
       
   115 	TInt     iPixBufferSize;
       
   116 	
       
   117 	TUint32* iMaskBuffer;
       
   118 
       
   119 // do not change data members layout without a reason
       
   120 // it is to have more efficient CPU caching	
       
   121 	TInt iNextFree;
       
   122 	TInt iBitBuffSize;
       
   123 	TInt iCurrentCodeLength;
       
   124 	TBitBuffer iBitBuffer;	
       
   125 	
       
   126 	const TUint8* iDataPtr;
       
   127 	const TUint8* iDataPtrLimit;
       
   128 	
       
   129 	CImageProcessor* iImgProc;  // not owned
       
   130 	CImageProcessor* iMaskProc; // not owned
       
   131 	TUint8  iFirstChar;
       
   132 	TUint8  iLatestPixSize;
       
   133 	TInt iPreviousCode;
       
   134 	TInt iClearCode;
       
   135 	TInt iEoiCode;
       
   136     TPoint* iPPos;
       
   137     
       
   138 	TBool iComplete;
       
   139 	TInt iPass;
       
   140 	TInt iYPosIncrement;
       
   141 
       
   142 	TPoint iPositionOffset;
       
   143 	TInt iReductionFactor;
       
   144 	const TSize iScreenSize;
       
   145 
       
   146 	TBool iTableReset;
       
   147 	TInt iBlockId;
       
   148 	TBool iReadingExtensionBlock;
       
   149 	TBool iReadingCommentExtensionBlock;
       
   150 	TBool iReadingOtherExtensionBlock;
       
   151 	TBool iReadingLoopIterationExtensionBlock;
       
   152 
       
   153 // Local frame settings.	
       
   154 	TFrameInfo *iFrameInfo;
       
   155 	CFrameImageData *iFrameData;	
       
   156 	TRgb* iGlobalPalette;
       
   157 	TUint8* iStartDataPtr;	//startDataPtr;
       
   158 
       
   159 	RPointerArray<HBufC8> iComment;
       
   160 	TInt iCommentIndex;
       
   161 
       
   162 	// Temporary ImageData pointers. (Not owned by the codec)
       
   163 	// These ones are used by convert only.
       
   164 	TGifImageControl *iGifImageControl;
       
   165 	TGifImageDescriptor *iGifImageDesc;
       
   166 	TGifColorTable *iGifColorTable;
       
   167 	TGifLZWInfo *iGifLZWInfo;
       
   168 
       
   169 	// These ones are used by header processing only.
       
   170 	TGifImageControl *iFrameImageControl;
       
   171 	TGifImageDescriptor *iFrameImageDesc;
       
   172 	TGifLZWInfo *iFrameLZWInfo;
       
   173 
       
   174 	TSize iFirstFrameSize;
       
   175 	TRect iFirstFrameCoords;
       
   176 
       
   177 	TSize iFrameSize;
       
   178 	TRect iFrameCoords;
       
   179 	TPoint iFrameOffset;
       
   180 	TBool iUseFrameSizeInPixels;
       
   181 	TBool iFast64kMode;
       
   182 	const TBool iFastDecode;
       
   183 	TBool iFastAccessMode;
       
   184 	};
       
   185 
       
   186 
       
   187 // Write codec.
       
   188 const TInt KGifBlockSize = 255;
       
   189 const TInt KGifBufferSize = KGifBlockSize + 9;
       
   190 class CGifEncoder;
       
   191 class CGifWriteCodec : public CImageWriteCodec
       
   192 	{
       
   193 public:
       
   194 	static CGifWriteCodec* NewL(const CGifEncoder& aEncoder);
       
   195 
       
   196 	//From CImageWriteCodec
       
   197 	virtual TFrameState ProcessFrameL(TBufPtr8& aDst);
       
   198 protected:
       
   199 	CGifWriteCodec(const CGifEncoder& aEncoder);
       
   200 
       
   201 private:
       
   202 	void InitFrameL(TBufPtr8& aDst, const CFbsBitmap& aSource);
       
   203 	void AddToTableL(TInt aIndex, TUint32 aHashValue);
       
   204 	TBool TableEntry(TInt16 aWaitingCode, TUint8 aNewCode, TInt& aIndex, TUint32& aHashValue);
       
   205 	void ResetTable();
       
   206 	void WriteData(TInt aIndex);
       
   207 	void FillBufferL(const CFbsBitmap& aFrame);
       
   208 	TBool WriteBuffer();
       
   209 	TBool WriteTerminator();
       
   210 private:
       
   211 	TRect iSourceRect;
       
   212 	TPoint iPos;
       
   213 	TInt16 iWaitingCode;
       
   214 	TInt16 iHashCode[KGifHashTableSize];   // must hold -1..(KGifConversionTableSize-1)
       
   215 	TUint32 iHashValue[KGifHashTableSize]; // must hold  0..(KGifConversionTableSize-1)<<8|TUint8
       
   216 	TUint8 iBuffer[KGifBufferSize];
       
   217 	TUint8* iBufferPtr;
       
   218 	TInt iBufferSize;
       
   219 	TBool iBufferFull;
       
   220 	TBool iImageComplete;
       
   221 	TInt iCodeLength;
       
   222 	TInt iNextLimit;
       
   223 	TInt iClearCode;
       
   224 	TInt iNextFree;
       
   225 	TInt iBitsPerPixel;
       
   226 	TInt iBitOffset;
       
   227 	TUint8* iDestStartPtr;
       
   228 	TUint8* iDestPtr;
       
   229 	TUint8* iDestPtrLimit;
       
   230 	const CGifEncoder& iEncoder;
       
   231 	};
       
   232 
       
   233 #endif // GIFCODEC_H
       
   234