mmplugins/imagingplugins/codecs/JPEGCodec/JPEGCodec.h
changeset 0 40261b775718
equal deleted inserted replaced
-1:000000000000 0:40261b775718
       
     1 // Copyright (c) 2001-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 //
       
    15 
       
    16 #ifndef JPEGCODEC_H
       
    17 #define JPEGCODEC_H
       
    18 
       
    19 #include "JPEGConvert.h"
       
    20 #include "pluginextensionmanager.h"
       
    21 #include "fwextconstants.h"
       
    22 
       
    23 #ifdef JPEG_DEBUG_OUTPUT
       
    24 
       
    25 #define JPEG_DEBUG_LEAVES
       
    26 
       
    27 #define JPEG_ASSERT(a)													\
       
    28 	do																	\
       
    29 		{																\
       
    30 		if (!(a))														\
       
    31 			{															\
       
    32 			RDebug::Printf("*** Assertion failed: " #a);				\
       
    33 			RDebug::Printf("*** " __FILE__ " at line %d", __LINE__);	\
       
    34 			ASSERT(EFalse);												\
       
    35 			}															\
       
    36 		}																\
       
    37 	while (EFalse)
       
    38 
       
    39 // Parameter 'a' should be a C-style string.
       
    40 #define JPEG_DEBUG1(a)			RDebug::Printf("JPEG: " a)
       
    41 #define JPEG_DEBUG2(a, b)		RDebug::Printf("JPEG: " a, b)
       
    42 #define JPEG_DEBUG3(a, b, c)	RDebug::Printf("JPEG: " a, b, c)
       
    43 #define JPEG_DEBUG4(a, b, c, d)	RDebug::Printf("JPEG: " a, b, c, d)
       
    44 #define JPEG_DEBUG5(a, b, c, d, e)	RDebug::Printf("JPEG: " a, b, c, d, e)
       
    45 #define JPEG_DEBUG6(a, b, c, d, e, f) RDebug::Printf("JPEG: " a, b, c, d, e, f)
       
    46 #define JPEG_DEBUG7(a, b, c, d, e, f, g) RDebug::Printf("JPEG: " a, b, c, d, e, f, g)
       
    47 #define JPEG_DEBUG8(a, b, c, d, e, f, g, h) RDebug::Printf("JPEG: " a, b, c, d, e, f, g, h)
       
    48 #else
       
    49 #define JPEG_ASSERT(a)
       
    50 #define JPEG_DEBUG1(a)
       
    51 #define JPEG_DEBUG2(a, b)
       
    52 #define JPEG_DEBUG3(a, b, c)
       
    53 #define JPEG_DEBUG4(a, b, c, d)
       
    54 #define JPEG_DEBUG5(a, b, c, d, e)
       
    55 #define JPEG_DEBUG6(a, b, c, d, e, f)
       
    56 #define JPEG_DEBUG7(a, b, c, d, e, f, g)
       
    57 #define JPEG_DEBUG8(a, b, c, d, e, f, g, h)
       
    58 #endif
       
    59 
       
    60 #ifdef JPEG_DEBUG_LEAVES
       
    61 
       
    62 // A more debugging friendly way of leaving.
       
    63 // The temporary variable is absolutely necessary and will break code if removed.
       
    64 #define JPEG_LEAVE_IF_ERROR(err, reason)									\
       
    65 	do																		\
       
    66 		{																	\
       
    67 		TInt temp = err;													\
       
    68 		if (temp != KErrNone)												\
       
    69 			{																\
       
    70 			RDebug::Printf("JPEG: *** Leave(%d): %d \"" reason "\"", __LINE__, temp);	\
       
    71 			User::Leave(temp);												\
       
    72 			}																\
       
    73 		}																	\
       
    74 	while (EFalse)
       
    75 
       
    76 // A more debugging friendly way of leaving.
       
    77 #define JPEG_LEAVE(err, reason) 										\
       
    78 	do																	\
       
    79 		{																\
       
    80 		TInt temp = err;												\
       
    81 		RDebug::Printf("JPEG: *** Leave(%d): %d \"" reason "\"", __LINE__, temp);	\
       
    82 		User::Leave(temp);												\
       
    83 		}																\
       
    84 	while (EFalse)
       
    85 
       
    86 #else
       
    87 #define JPEG_LEAVE_IF_ERROR(err, reason)	User::LeaveIfError(err)
       
    88 #define JPEG_LEAVE(err, reason)			User::Leave(err)
       
    89 #endif
       
    90 
       
    91 class CImageFrame;
       
    92 class CRawColorProcessor;
       
    93 class CJpgImageFrameProcessor;
       
    94 class TJpgFrameInfo;
       
    95 class TDataUnit;
       
    96 class TJpgScanInfo;
       
    97 class CJpegDecoder;
       
    98 
       
    99 // CJpgImageFrameReadCodec
       
   100 class CJpgImageFrameReadCodec : public CBase
       
   101 	{
       
   102 public:
       
   103 	static CJpgImageFrameReadCodec* NewL(CImageFrame* aFrame);
       
   104 	~CJpgImageFrameReadCodec();
       
   105 
       
   106 	void CreateImageProcessorL(const TJpgFrameInfo& aFrameInfo);		
       
   107 	void ProcessL(const RArray<const TDataUnit*>& compPtr);
       
   108 	CImageFrame* Destination();
       
   109 	void SetImageFrameBlocksL(CImageFrame* aFrame, const TJpgFrameInfo& aFrameInfo);
       
   110 
       
   111 private:
       
   112 	CJpgImageFrameReadCodec(CImageFrame* aFrame);
       
   113 
       
   114 private:
       
   115 	CImageFrame* iDestination;
       
   116 	CJpgImageFrameProcessor* iImageFrameProcessorPtr;
       
   117 	};
       
   118 
       
   119 
       
   120 //
       
   121 // CJpgReadCodec
       
   122 // The base class for all JPEG decoding.
       
   123 //
       
   124 class CJpgReadCodec : public CImageProcessorReadCodecExtension
       
   125 	{
       
   126 public:
       
   127 	friend class CSequentialJpgReadCodec;
       
   128 	friend class CMultiScanSequentialJpgReadCodec;
       
   129 	//
       
   130 	// Values and explanations are based on the book
       
   131 	// "JPEG Still Image Data Compression Standard" by W.B.Pennebaker and J.L.Mitchell.
       
   132 	//
       
   133 	enum
       
   134 		{
       
   135 		EMarkerEscape = 0xFF00,	// Not really a marker but used to escape valid 0xFF bitstream sequences.
       
   136 		EMarkerTEM = 0xFF01,	// For temporary use in arithmetic coding.
       
   137 		EMarkerRES0 = 0xFF02,	// Reserved.
       
   138 		EMarkerRESn = 0xFFBF,	// ...
       
   139 		
       
   140 		EMarkerSOF0 = 0xFFC0,	// Baseline DCT.
       
   141 		EMarkerSOF1 = 0xFFC1,	// Extended sequential DCT.
       
   142 		EMarkerSOF2 = 0xFFC2,	// Progressive DCT.
       
   143 		EMarkerSOF3 = 0xFFC3,	// Lossless (sequential).
       
   144 		EMarkerDHT = 0xFFC4,	// Define Huffman table(s).
       
   145 		EMarkerSOF5 = 0xFFC5,	// Differential sequential DCT.
       
   146 		EMarkerSOF6 = 0xFFC6,	// Differential progressive DCT.
       
   147 		EMarkerSOF7 = 0xFFC7,	// Differential lossless.
       
   148 		EMarkerJPG = 0xFFC8,	// Reserved.
       
   149 		EMarkerSOF9 = 0xFFC9,	// Extended sequential DCT.
       
   150 		EMarkerSOF10 = 0xFFCA,	// Progressive DCT.
       
   151 		EMarkerSOF11 = 0xFFCB,	// Lossless (sequential).
       
   152 		EMarkerDAC = 0xFFCC,	// Define arithmetic conditioning table(s).
       
   153 		EMarkerSOF13 = 0xFFCD,	// Differential sequential DCT.
       
   154 		EMarkerSOF14 = 0xFFCE,	// Differential progressive DCT.
       
   155 		EMarkerSOF15 = 0xFFCF,	// Differential lossless.
       
   156 		
       
   157 		EMarkerRST0 = 0xFFD0,	// First restart marker.
       
   158 		EMarkerRST1 = 0xFFD1,	// ...
       
   159 		EMarkerRST2 = 0xFFD2,	// ...
       
   160 		EMarkerRST3 = 0xFFD3,	// ...
       
   161 		EMarkerRST4 = 0xFFD4,	// ...
       
   162 		EMarkerRST5 = 0xFFD5,	// ...
       
   163 		EMarkerRST6 = 0xFFD6,	// ...
       
   164 		EMarkerRST7 = 0xFFD7,	// Last restart marker.
       
   165 		EMarkerSOI = 0xFFD8,	// Start of image.
       
   166 		EMarkerEOI = 0xFFD9,	// End of image.
       
   167 		EMarkerSOS = 0xFFDA,	// Start of scan.
       
   168 		EMarkerDQT = 0xFFDB,	// Define quantization table(s).
       
   169 		EMarkerDNL = 0xFFDC,	// Define number of lines.
       
   170 		EMarkerDRI = 0xFFDD,	// Define restart interval.
       
   171 		EMarkerDHP = 0xFFDE,	// Define hierarchical progression.
       
   172 		EMarkerEXP = 0xFFDF,	// Expand reference image(s).
       
   173 	
       
   174 		EMarkerAPP0 = 0xFFE0,	// Reserved for application use.
       
   175 		EMarkerAPP1 = 0xFFE1,	// ...
       
   176 		EMarkerAPP2 = 0xFFE2,	// ...
       
   177 		EMarkerAPP3 = 0xFFE3,	// ...
       
   178 		EMarkerAPP4 = 0xFFE4,	// ...
       
   179 		EMarkerAPP5 = 0xFFE5,	// ...
       
   180 		EMarkerAPP6 = 0xFFE6,	// ...
       
   181 		EMarkerAPP7 = 0xFFE7,	// ...
       
   182 		EMarkerAPP8 = 0xFFE8,	// ...
       
   183 		EMarkerAPP9 = 0xFFE9,	// ...
       
   184 		EMarkerAPP10 = 0xFFEA,	// ...
       
   185 		EMarkerAPP11 = 0xFFEB,	// ...
       
   186 		EMarkerAPP12 = 0xFFEC,	// ...
       
   187 		EMarkerAPP13 = 0xFFED,	// ...
       
   188 		EMarkerAPP14 = 0xFFEE,	// ...
       
   189 		EMarkerAPP15 = 0xFFEF,	// ...
       
   190 			
       
   191 		EMarkerJPG0 = 0xFFF0,	// Reserved for JPEG extensions.
       
   192 		EMarkerJPG1 = 0xFFF1,	// ...
       
   193 		EMarkerJPG2 = 0xFFF2,	// ...
       
   194 		EMarkerJPG3 = 0xFFF3,	// ...
       
   195 		EMarkerJPG4 = 0xFFF4,	// ...
       
   196 		EMarkerJPG5 = 0xFFF5,	// ...
       
   197 		EMarkerJPG6 = 0xFFF6,	// ...
       
   198 		EMarkerJPG7 = 0xFFF7,	// ...
       
   199 		EMarkerJPG8 = 0xFFF8,	// ...
       
   200 		EMarkerJPG9 = 0xFFF9,	// ...
       
   201 		EMarkerJPG10 = 0xFFFA,	// ...
       
   202 		EMarkerJPG11 = 0xFFFB,	// ...
       
   203 		EMarkerJPG12 = 0xFFFC,	// ...
       
   204 		EMarkerJPG13 = 0xFFFD,	// ...
       
   205 		EMarkerCOM = 0xFFFE,	// Comment.
       
   206 		}
       
   207 	TMarker;
       
   208 	
       
   209 	void SetYuvDecode(TBool aYuvDecode);
       
   210 	void SetHighSpeedMode(TBool aHighSpeedMode);
       
   211 	TInt RecommendBufferSizeL(TUid aFormatCode);
       
   212 	TInt GetStreamBufferSizeL(TUid aFormatCode, TSize& aBlockSizeInPixels, TInt aNumBlocks);
       
   213 	virtual void InitDrawFrame();
       
   214 	virtual TBool DrawFrameL();
       
   215 
       
   216 	//From CImageReadCodec
       
   217 	virtual void InitFrameL(TFrameInfo& aFrameInfo, CFrameImageData& aFrameImageData, TBool aDisableErrorDiffusion, CFbsBitmap& aFrame, CFbsBitmap* aFrameMask);
       
   218 	CJpgReadCodec(const TJpgFrameInfo& aFrameInfo,const TJpgScanInfo& aScanInfo);
       
   219 	~CJpgReadCodec();
       
   220 	virtual void InitFrameHeader(TFrameInfo& aFrameInfo, CFrameImageData& aFrameData);
       
   221 	//Jpg codec support for CImageFrame
       
   222 	virtual void InitFrameL(CImageFrame& aFrame);
       
   223 	virtual void PreInitFrameL();
       
   224 	virtual void PostInitFrameL();
       
   225 	
       
   226 	//JPEG codec support for streaming
       
   227 	virtual void InitFrameL(TUid aFormat, TDecodeStreamCaps::TNavigation aNavigation);
       
   228 	virtual void GetBlocksL(CImageFrame* aFrame, TInt aSeqPosition, TInt aNumBlocksToGet, TInt* aNumBlocksRead);
       
   229 	virtual void GetNextBlocksL(CImageFrame* aFrame, TInt aNumBlocksToGet, TInt* aNumBlocksRead, TBool* aHaveMoreBlocks);
       
   230 	
       
   231 	void ValidateImageFrameBlockL(CImageFrame* aFrame);
       
   232 	void ValidateFormatL(const TJpgFrameInfo& aFrameInfo, TUid aFormatCode);
       
   233 	
       
   234 	// INC037134 - free buffers used in codec
       
   235 	virtual void CleanupBuffers();
       
   236 	
       
   237 	CJpgImageFrameReadCodec* ImageFrameCodec();
       
   238 	virtual TInt ReducedSize(const TSize& aOriginalSize, TInt aReductionFactor, TSize& aReducedSize) const;
       
   239 	void SetExtensionManager(CPluginExtensionManager* aExtensionManager);
       
   240 	static TInt ScaleFactorToDivisorL(TInt aScalingFactor);
       
   241 
       
   242 	void SetAutoRotateFlag(TUint16 aAutoRotateFlag);
       
   243 
       
   244 protected:
       
   245 	void InitComponentsL();
       
   246 	TFrameState ProcessFrameL(TBufPtr8& aSrc);
       
   247 	virtual TFrameState DoProcessL() = 0;
       
   248 	void StoreState();
       
   249 	void RestoreState();
       
   250 	void ResetState();
       
   251 	TInt RestartStateL();
       
   252 	void GetComponentBlockL(TDataUnit& aDestination,TInt& aNumValues,TInt& aDCPrediction,const TDecHuffmanTable& aDCTable,const TDecHuffmanTable& aACTable);
       
   253 	TInt GetHuffmanCodeL(const TDecHuffmanTable& aTable);
       
   254 	inline void SkipBitsQuickL(TInt aNumOfBits);
       
   255 	inline TInt GetBinaryNumberQuickL(TInt aLength);
       
   256 	inline TInt16 GetBinaryNumberL(TInt aLength);
       
   257 	inline TInt16 GetPositiveBinaryNumberL(TInt aLength);
       
   258 	void CalculateRgbIndeces();					// prepares UV indeces for de-sampling process
       
   259 	void ConfigureUVComponentDCT(TInt aCompIdx);// cofigures UV iDCT parameters according to the scaling factor
       
   260 	void WriteMCU();
       
   261 	void WriteMonoMCU();
       
   262 	void CalculateRenderingParams(const TInt aScale);
       
   263 	virtual void CalculateMCUBoundingRectL(TInt aMCUsPerLine);
       
   264 	TInt GetMCURenderOffset();
       
   265 
       
   266 	template<class T>
       
   267 	inline void WriteDiv8MCUImpl(); //templatized implementation for writting MCU with 1/8 scaling factor
       
   268 	
       
   269 	template<class T>
       
   270 	void WriteDiv4ScaledMCUImpl(); //templatized implementation for writting MCU with 1/4 scaling factor
       
   271 	
       
   272 	template<class T>
       
   273 	void WriteDiv2ScaledMCUImpl(); //templatized implementation for writting MCU with 1/2 scaling factor
       
   274 	
       
   275 	template<class T>
       
   276 	inline void WriteUnScaledMCUImpl(); //templatized implementation for writting MCU with 1/1 scaling factor
       
   277 
       
   278 	void WriteUnScaledMCU();		// implementation for writing MCU with 1/1 scaling factor to TRgb output buffer
       
   279 	void WriteDiv2ScaledMCU();		// implementation for writing MCU with 1/2 scaling factor to TRgb output buffer
       
   280 	void WriteDiv4ScaledMCU();		// implementation for writing MCU with 1/4 scaling factor to TRgb output buffer
       
   281 	void WriteDiv8ScaledMCU();		// implementation for writing MCU with 1/8 scaling factor to TRgb output buffer
       
   282 	
       
   283 	void WriteUnScaledMCU16M();		// implementation for writing MCU with 1/1 scaling factor to 24bpp output buffer
       
   284 	void WriteDiv2ScaledMCU16M(); 	// implementation for writing MCU with 1/2 scaling factor to 24bpp output buffer
       
   285 	void WriteDiv4ScaledMCU16M(); 	// implementation for writing MCU with 1/4 scaling factor to 24bpp output buffer
       
   286 	void WriteDiv8ScaledMCU16M(); 	// implementation for writing MCU with 1/8 scaling factor to 24bpp output buffer
       
   287 	
       
   288 	void WriteDiv8ScaledMCU64K(); 	// implementation for writing MCU with 1/8 scaling factor to 16bpp output buffer
       
   289 	
       
   290 	TInt ComponentIndexL(TInt aComponentId) const;
       
   291 
       
   292 	inline TBool NextBitL();					// returns value of next bit off the bit buffers
       
   293 	inline TInt FillBitBufferL(TInt aBLeft);					// fetches up to 4 bytes into bit-buffer
       
   294 	inline const TUint8* JpegFillBuffer(const TUint8* aDataPtr);				// detects safe number of bytes which can be read from input buffer
       
   295 	virtual TInt MCUBlockPerRgbBuffer() const; 	// returns number of MCU block which make full horizontal line
       
   296 	TInt GetHorzMCUCount();
       
   297 	TInt GetVertMCUCount();
       
   298 	void ConfigureAndAllocateBufferL(const TInt aScale);
       
   299 	void ConfigureImageProcessorL(CImageProcessorExtension* aImageProc, CFbsBitmap& aFrame, const TInt aScaleDivisor, const TSize& aPrepareLSize);
       
   300 
       
   301 protected:
       
   302 	class TCompConf // configuration parameters for color component
       
   303 		{
       
   304 	public:
       
   305 		TDCT* 					iDCT;			// (i)DCT implementation pointer
       
   306 		TQTable::TDequantFunc	iDequantFunc;   // DeQuantization function pointer
       
   307 		};
       
   308 		
       
   309 	TJpgFrameInfo iFrameInfo;
       
   310 	TJpgScanInfo iScanInfo;
       
   311 
       
   312 	const TUint8* iDataPtr;
       
   313 	const TUint8* iDataPtrLimit;
       
   314 // NOTE: there are multiple sections, which are created to achieve best CPU cache effectiveness
       
   315 private://		
       
   316     typedef TUint TBitBuffer;
       
   317     
       
   318 	const TUint8* iBitBufferPtrLimit;	// upper value for data buffer pointer which can be safely read without dealing with 0xFF
       
   319 	TUint iDataValue;
       
   320 	TUint iBitsLeft;
       
   321 	
       
   322 protected:		
       
   323 	TInt iDCPredictor[KJpgNumberOfComponents];
       
   324 	TCompConf iCompConf[KJpgNumberOfComponents];	// color component decoding implementation parameters
       
   325 	
       
   326 	TInt iMCUDataUnitCount[KJpgNumberOfComponents];
       
   327 	TDataUnit* iComponent[KJpgNumberOfComponents];
       
   328 	TBool iProcessing;
       
   329 	TInt iScalingFactor;
       
   330 	TInt iRestartMCUCount;
       
   331 	TInt iHorzMCUBlkCount;	// current MCU number within the horizontal line
       
   332 	TInt iHorzSampleFactor[KJpgNumberOfComponents];
       
   333 	TInt iVertSampleFactor[KJpgNumberOfComponents];
       
   334 	TInt iHorzSampleRatio[KJpgNumberOfComponents];
       
   335 	TInt iVertSampleRatio[KJpgNumberOfComponents];
       
   336 	TInt iMaxHorzSampleFactor;
       
   337 	TInt iMaxVertSampleFactor;
       
   338 	TInt iHorzSampleRatioSh[2];	// "shift" factor for calculating U/V horizontal pixel corrdinate during de-sampling
       
   339 	TInt iVertSampleRatioSh[2];	// "shift" factor for calculating U/V vertical pixel corrdinate during de-sampling
       
   340 	
       
   341 private:	
       
   342 	const TUint8* iInitialDataPtr;
       
   343 	TBitBuffer iInitialDataValue;
       
   344 	TUint iInitialBitsLeft;
       
   345 	TInt iInitialDCPredictor[KJpgNumberOfComponents];
       
   346 	typedef TUint8 TUVidxElemType;
       
   347 	TInt iRgbBufNextLineOffs; // pre-calculated data offset that can be used to step to next MCU address within the iRgbBuffer
       
   348 	TUVidxElemType iUVIndeces[2* KJpgMaxSampleFactor * KJpgMaxSampleFactor * KJpgDCTBlockWidth];
       
   349 		// iUVIndeces are used to speed up MCU de-sampling
       
   350 protected:
       
   351 	TRgb* iRgbBuffer;						// RGB output buffer
       
   352 	TBool iMonochrome;
       
   353 	TBool iYuvDecode;
       
   354 	TInt iP1;
       
   355 	TInt iM1;
       
   356 	TInt iPreviousDataLeft;					// used to detect if the last available data block is being processed
       
   357 	CRawColorProcessor* iRawColorProcessor;	// Fast and simple Color Processor, not owned
       
   358 	// Jpg codec support for Image Frame
       
   359 	CJpgImageFrameReadCodec* iImageFrameCodecPtr;	
       
   360 	TBool iProgressive;				// TRUE if we're dealing with progressively-encoded image
       
   361 	TInt iBlocksPerLine;			// number of MCU blocks per image horizontal line
       
   362 
       
   363 private:	
       
   364 	TBool iHighSpeedMode;			// flag of "fast possbile decoding mode"
       
   365 	TDCT iFullDCT;					// implemetation for the (Inverse) Discrete Cosine Transformer
       
   366 	THalfDCT iHalfDCT;				// implemetation for the 1/2-sized (Inverse) Discrete Cosine Transformer
       
   367 	TFastHalfDCT iFastHalfDCT;		// implemetation for the "fast" 1/2-sized (Inverse) Discrete Cosine Transformer
       
   368 	TQuarterDCT iQuarterDCT; 		// implemetation for the 1/4-sized (Inverse) Discrete Cosine Transformer
       
   369 	TFastQuarterDCT iFastQuarterDCT;// implemetation for the "fast" 1/4-sized (Inverse) Discrete Cosine Transformer
       
   370 	TEighthDCT iEighthDCT;			// implemetation for the 1/8-sized (Inverse) Discrete Cosine Transformer
       
   371 
       
   372 	typedef void (CJpgReadCodec::*TMCUWriteFunc)();
       
   373 	TMCUWriteFunc iMcuWriteFunc;	// function pointer for the MCU to Bitmap writer implementation
       
   374 
       
   375 protected:
       
   376 	TSize iBufSize;		// The size, in pixels, of the intermediate buffer.
       
   377 	TInt iPixelSize;
       
   378 	TInt iCurrentMCUBitOffset;
       
   379 	TBool iUseClipRect;	// Set if SetClippingRectL has been called.
       
   380 	TRect iMCUClipRect;	// The clipping rect represented in MCU indeces.
       
   381 	TInt iPixelIncrement;
       
   382 	TInt iFirstPixelOffset;
       
   383 	CPluginExtensionManager* iExtensionManager; // not owned
       
   384 	TTransformOptions iOperation;
       
   385 	TBool iFillBufferBackwards;	// How to fill iRgbBuffer.
       
   386 	TInt iMCUHorizExtent;	// Flipping-aware pixel width of an MCU.
       
   387 
       
   388 	//streaming information 
       
   389     class TStreamConfig
       
   390         {
       
   391     public:
       
   392     	TInt iSeqPosition;	//sequence postion of MCU
       
   393 		TInt iNumBlocksToGet;	//number of blocks to decode
       
   394 		TInt* iNumBlocksRead;	//number of blocks decoded
       
   395 		TBool* iHaveMoreBlocks;	//are more blocks available to decode
       
   396         };
       
   397     
       
   398 	TStreamConfig iStreamDecodeConfig;
       
   399 	TBool iIsBlockStreaming;
       
   400 	TDecodeStreamCaps::TNavigation iNavigation;
       
   401 	TUid iStreamFormat;
       
   402 	
       
   403 	// These are members of this class as it greatly helps
       
   404 	// debugging if you know what MCUs you're dealing with.
       
   405 	TInt iNeededMCU;	// The MCU number that's next needed for rendering.
       
   406 	TInt iStreamMCU;	// The MCU that the bit stream is currently representing.
       
   407 	TBool iSeekDone;	// Is DoProcessL being called after a reposition request.
       
   408 	TBool iEscapeAtEnd;	// Set if the 0xFF00 sequence occurs at the buffer boundary.
       
   409 	
       
   410 	TInt iMCUsPerBuffer;
       
   411 	TSize iRubbish;		// The amount of 'pixels' of an MCU that aren't part of the final image.
       
   412 	TUint16 iAutoRotateFlag;
       
   413 	};
       
   414 
       
   415 //
       
   416 // Utility class for finding out what
       
   417 // MCUs are needed to render a clipping rect.
       
   418 //
       
   419 class CMCUStore : public CBase
       
   420 	{
       
   421 public:
       
   422 	static CMCUStore* NewL(TJpgFrameInfo& aFrameInfo);
       
   423 	~CMCUStore();
       
   424 	void SetClippingRect(const TInt aFirstMCU, const TInt aTotalMCUs);
       
   425 	void SetOperation(const TTransformOptions aOperation);
       
   426 	void SetMCUsPerBuffer(const TInt aMCUsPerBuffer);
       
   427 	TInt GetNextMCU();
       
   428 	void NextLine();
       
   429 	void Reset();
       
   430 		
       
   431 private:
       
   432 	CMCUStore(TJpgFrameInfo& aFrameInfo);
       
   433 	void Prepare();
       
   434 
       
   435 	TBool iClipped;
       
   436 	TJpgFrameInfo& iFrameInfo;
       
   437 	TInt iCurrentMCU;
       
   438 	TTransformOptions iOperation;
       
   439 	TInt iNextRowOffset;
       
   440 	TInt iMCUIncrement;
       
   441 	TInt iLastMCU;
       
   442 	TInt iReads;
       
   443 	TInt iMaxReads;
       
   444 	TInt iMCUsPerBuffer;
       
   445 	TBool iPrepared;
       
   446 	};
       
   447 
       
   448 //
       
   449 // CSequentialJpgReadCodec
       
   450 //
       
   451 class CSequentialJpgReadCodec : public CJpgReadCodec
       
   452 	{
       
   453 
       
   454 	//
       
   455 	// This is the structure of a lookup-table entry.
       
   456 	// 
       
   457 	typedef struct
       
   458 		{
       
   459 		TUint32 iPosition;			// The bit position of the MCU.
       
   460 		TInt16 iDCPredictor[3];		// The DC predictors (needed for correct rendering).
       
   461 		TInt16 iRestartMCUCount;	// Images with restart intervals will require this.
       
   462 		}
       
   463 	TMCUEntry;
       
   464 	
       
   465 public:
       
   466 	static CSequentialJpgReadCodec* NewL(
       
   467 			const TJpgFrameInfo& aFrameInfo,
       
   468 			const TJpgScanInfo& aScanInfo,
       
   469 			TDecHuffmanTable aDCHuffmanTable[KJpgMaxNumberOfTables],
       
   470 			TDecHuffmanTable aACHuffmanTable[KJpgMaxNumberOfTables],
       
   471 			const TQTable aQTable[KJpgMaxNumberOfTables]
       
   472 			);
       
   473 	~CSequentialJpgReadCodec();
       
   474 	virtual void PreInitFrameL();
       
   475 	virtual void PostInitFrameL();
       
   476 	virtual void GetBlocksL(CImageFrame* aFrame, TInt aSeqPosition, TInt aNumBlocksToGet, TInt* aNumBlocksRead);
       
   477 
       
   478 protected:
       
   479 #ifdef JPEG_DEBUG_OUTPUT
       
   480 	void DumpCache();
       
   481 #endif
       
   482 	
       
   483 	virtual TFrameState DoProcessL();
       
   484 	void CalculateMCUBoundingRectL(TInt aMCUsPerLine);
       
   485 	void ConstructL(TBool aUseCache = ETrue);
       
   486 	TInt GetNextMCUPosition();
       
   487 	void PostProcessMCUL(TBool aForceBufferFlush);
       
   488 	TInt ProcessMCUL();
       
   489     TFrameState ProcessStreaming();
       
   490 	
       
   491 private:
       
   492 	// Functions related to caching and seeking.
       
   493 	void CacheMCULocation();
       
   494 	virtual void GetNewDataPosition(TInt& aPosition, TInt& aLength);
       
   495 	TBool QueryCache();
       
   496 	void RestoreAfterSeekL();
       
   497 
       
   498 	
       
   499 protected:
       
   500 	CSequentialJpgReadCodec(
       
   501 			const TJpgFrameInfo& aFrameInfo,
       
   502 			const TJpgScanInfo& aScanInfo,
       
   503 			TDecHuffmanTable aDCHuffmanTable[KJpgMaxNumberOfTables],
       
   504 			TDecHuffmanTable aACHuffmanTable[KJpgMaxNumberOfTables],
       
   505 			const TQTable aQTable[KJpgMaxNumberOfTables]
       
   506 			);
       
   507 	
       
   508 	TDecHuffmanTable* iDCHuffmanTable;
       
   509 	TDecHuffmanTable* iACHuffmanTable;
       
   510 	const TQTable* iQTable;
       
   511 	
       
   512 	// A pointer to the lookup table.
       
   513 	TMCUEntry* iMCULookup;
       
   514 	
       
   515 	// The number of MCUs in the source image.
       
   516 	TInt iTotalMCUCount;
       
   517 
       
   518 	// Keeps track of what MCUs are needed for rendering.
       
   519 	CMCUStore* iMCUStore;
       
   520 	};
       
   521 
       
   522 	
       
   523 //
       
   524 // CMultiScanSequentialJpgReadCodec
       
   525 // =================================
       
   526 //
       
   527 class CMultiScanSequentialJpgReadCodec : public CSequentialJpgReadCodec
       
   528 	{
       
   529 public:
       
   530 	static CMultiScanSequentialJpgReadCodec* NewL(
       
   531 			const TJpgFrameInfo& aFrameInfo,
       
   532 			const TJpgScanInfo& aScanInfo,
       
   533 			TDecHuffmanTable aDCHuffmanTable[KJpgMaxNumberOfTables],
       
   534 			TDecHuffmanTable aACHuffmanTable[KJpgMaxNumberOfTables],
       
   535 			const TQTable aQTable[KJpgMaxNumberOfTables]);
       
   536 	virtual void PreInitFrameL();
       
   537 	virtual void InitFrameL(TFrameInfo& aFrameInfo, CFrameImageData& aFrameImageData, TBool aDisableErrorDiffusion, CFbsBitmap& aFrame, CFbsBitmap* aFrameMask);
       
   538 	~CMultiScanSequentialJpgReadCodec();
       
   539 	
       
   540 private:
       
   541 	virtual TFrameState DoProcessL();
       
   542 	TFrameState ProcessFrameL(TBufPtr8& aSrc);
       
   543 	TBool SearchTableOrSosMarker(TBufPtr8& aSourceData, TInt& aPos, TUint16& aMarker);
       
   544 	TBool SwitchScanL(const TUint8*& aScan, const TUint8* aDataLimit);		
       
   545 	void RenderMCUsL();
       
   546 	CMultiScanSequentialJpgReadCodec(
       
   547 			const TJpgFrameInfo& aFrameInfo,
       
   548 			const TJpgScanInfo& aScanInfo,
       
   549 			TDecHuffmanTable aDCHuffmanTable[KJpgMaxNumberOfTables],
       
   550 			TDecHuffmanTable aACHuffmanTable[KJpgMaxNumberOfTables],
       
   551 			const TQTable aQTable[KJpgMaxNumberOfTables]);
       
   552 	void CopyMCUs(TInt aCompIdx);
       
   553 	void FillEmptyMCUs();
       
   554 	void FillEmptyDU(TDataUnit* pDU);
       
   555 	void ResetOnNewScan();
       
   556 	TBool ProcessHuffmanTableL(const TUint8*& aData, const TUint8* aDataLimit);
       
   557 	void PrepareToRenderMCU(TInt aMCUIndex);
       
   558 
       
   559 private:
       
   560 	TInt iTotalMCUBlocks;
       
   561 	TDataUnit* iCompBuf[KJpgNumberOfComponents];
       
   562 	TInt iCurrentMcuIdx;
       
   563 	TBool iNewTableOrScan;
       
   564 	RChunk iMCUChunk;
       
   565 	TUint8* iMCUMemoryOffset;
       
   566 	//available components
       
   567 	TBool iCompAvailable[KJpgNumberOfComponents];
       
   568 	//count of MCUs decoded
       
   569 	TInt iCompMcuCount[KJpgNumberOfComponents];
       
   570 	//copy of pointer held by iComponents; this value is overwritten in rendering.
       
   571 	TDataUnit* iComponentCpy[KJpgNumberOfComponents];
       
   572 	
       
   573 	TJpgScanInfo iFirstScan;
       
   574 	};
       
   575 
       
   576 
       
   577 //
       
   578 // CProgressiveJpgReadCodec
       
   579 // =========================
       
   580 //
       
   581 class CProgressiveJpgReadCodec : public CJpgReadCodec
       
   582 	{
       
   583 public:
       
   584 	static CProgressiveJpgReadCodec* NewL(const TJpgFrameInfo& aFrameInfo,const TJpgScanInfo& aScanInfo,const TDecHuffmanTable aDCHuffmanTable[KJpgMaxNumberOfTables],const TDecHuffmanTable aACHuffmanTable[KJpgMaxNumberOfTables],const TQTable aQTable[KJpgMaxNumberOfTables]);
       
   585 	
       
   586 	virtual void PreInitFrameL();
       
   587 	virtual void InitFrameL(TFrameInfo& aFrameInfo, CFrameImageData& aFrameImageData, TBool aDisableErrorDiffusion, CFbsBitmap& aFrame, CFbsBitmap* aFrameMask);
       
   588 	virtual ~CProgressiveJpgReadCodec();
       
   589 
       
   590 	//From CJpgReadCodec
       
   591 	void InitDrawFrame();
       
   592 	TBool DrawFrameL();
       
   593 	void InitFrameL(CImageFrame& aFrame);
       
   594 
       
   595 	// INC037134 - free buffers used in codec
       
   596 	virtual void CleanupBuffers();
       
   597 
       
   598 private:
       
   599 	CProgressiveJpgReadCodec(const TJpgFrameInfo& aFrameInfo,const TJpgScanInfo& aScanInfo,const TDecHuffmanTable aDCHuffmanTable[KJpgMaxNumberOfTables],const TDecHuffmanTable aACHuffmanTable[KJpgMaxNumberOfTables],const TQTable aQTable[KJpgMaxNumberOfTables]);
       
   600 	virtual TFrameState DoProcessL();
       
   601 	void ProcessFrameL();
       
   602 	void InitDCL();
       
   603 	void RefineDCL();
       
   604 	void InitACL();
       
   605 	void RefineACL();
       
   606 	void GetDCValueL(TDataUnit& aDataUnit,const TDecHuffmanTable& aHuffmanTable,TInt& aDCPredictor);
       
   607 	void SkipDCValueL(const TDecHuffmanTable& aHuffmanTable);
       
   608 	TInt GetACValuesL(TDataUnit& aDataUnit,const TDecHuffmanTable& aHuffmanTable);
       
   609 	void RefineACValuesL(TDataUnit& aDataUnit,const TDecHuffmanTable& aHuffmanTable);
       
   610 	void LoadHuffmanTableL();
       
   611 	void LoadSOSL();
       
   612 	void LoadRestartIntervalL();
       
   613 	void DoInitFrameL();
       
   614 private:
       
   615 	const TJpgFrameInfo iOriginalFrameInfo;
       
   616 	const TJpgScanInfo iOriginalScanInfo;
       
   617 
       
   618 	TDecHuffmanTable iDCHuffmanTable[KJpgMaxNumberOfTables];
       
   619 	TDecHuffmanTable iACHuffmanTable[KJpgMaxNumberOfTables];
       
   620 	TQTable iQTable[KJpgMaxNumberOfTables];
       
   621 
       
   622 	TInt iHorzMCUCount;
       
   623 	TInt iVertMCUCount;
       
   624 	TInt iTotalMCUCount;
       
   625 	TInt iCurrentMCUCount;
       
   626 	TInt iCurrentMCUHorzCount;
       
   627 	TInt iCurrentMCUVertCount;
       
   628 	TInt iSkipCount;
       
   629 
       
   630 	TInt16 iRefinedDCValue;
       
   631 	TInt iEobRun;
       
   632 
       
   633 	TDataUnit* iMCUBuffer[KJpgNumberOfComponents];
       
   634 	TDataUnit* iMCUBufferPtr[KJpgNumberOfComponents];
       
   635 	TDataUnit* iMCUBufferPtrLimit[KJpgNumberOfComponents];
       
   636 	
       
   637 	TUint8* iMCUMemoryBuffer;
       
   638 	RChunk iMCUChunk;
       
   639 	TBool iMCUChunkAllocated;
       
   640 
       
   641 	TInt iIndividualHorzMCUCount[KJpgNumberOfComponents];
       
   642 	TInt iIndividualVertMCUCount[KJpgNumberOfComponents];
       
   643 
       
   644 	//used only by InitDrawFrame()/DrawFrame()
       
   645 	TInt iCurrentDrawMCU;
       
   646 	TDataUnit* iDrawMCUPtr[KJpgNumberOfComponents];
       
   647 	};
       
   648 
       
   649 #endif // JPEGCODEC_H
       
   650