imageeditorengine/inc/CJpeg.h
changeset 1 edfc90759b9f
equal deleted inserted replaced
0:57d4cdd99204 1:edfc90759b9f
       
     1 /*
       
     2 * Copyright (c) 2010 Ixonos Plc.
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the "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 * Ixonos Plc
       
    14 *
       
    15 * Description:  
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 //
       
    21 // Jpeg decoder class
       
    22 
       
    23 #ifndef __CJPEG_H__
       
    24 #define __CJPEG_H__
       
    25 
       
    26 #include <e32base.h>
       
    27 #include <f32file.h>
       
    28 #include "TBitmapHandle.h"
       
    29 #include "MJpegLoad.h"
       
    30 
       
    31 class THuffman;
       
    32 
       
    33 
       
    34 
       
    35 
       
    36 /// Main Jpeg decode class
       
    37 class CJpeg
       
    38 	: public CBase
       
    39 	, public MJpegLoad
       
    40 	{
       
    41 
       
    42 	public:
       
    43 		static CJpeg* NewL();
       
    44 		static CJpeg* NewLC();
       
    45 		~CJpeg();
       
    46 		
       
    47 	private:
       
    48 		CJpeg();
       
    49 		void ConstructL();
       
    50 
       
    51 	public: // MJpegLoad
       
    52 		
       
    53 		void OpenL( const TFileName& aFile );
       
    54 		void OpenL( const TPtr8& aData );
       
    55 
       
    56 		void SetScale( TJpegScale aScale );
       
    57 		TBitmapHandle LoadImageL( TRect& aRect );
       
    58 
       
    59 		void ScanRandomL();			// scans file, creates random access tables
       
    60 		TBitmapHandle LoadBlockL( const TPoint& aBlock );
       
    61 
       
    62 		const TJpegData& Info();
       
    63 		TPtrC8 ExifData();
       
    64 
       
    65 	public:
       
    66 
       
    67 		void EnableRgvConv();
       
    68 		void DisableRgbConv();
       
    69 		
       
    70 	private:
       
    71 		inline void BufFwd( TInt aBits );
       
    72 		inline TInt BufBits( TInt aBits );
       
    73 		inline TInt Buf16();
       
    74 		inline void BufLoad8();
       
    75 		void DecodeBlock();
       
    76 		void DecodeBlock2();
       
    77 		void DecodeBlock3();
       
    78 		void Idct();
       
    79 		void IdctHalf();
       
    80 		void CreateHuffmanL( THuffman* aHuffman, const TUint8* aBits, const TUint8* aVal );
       
    81 		void CreateDefaultHuffmanL();
       
    82 		void PrepareLoadBlockL();	// must be called before first LoadBlock()
       
    83 		
       
    84 		//
       
    85 		// Scale load functions:
       
    86 		//
       
    87 		void DecRgb1_1L( const TBitmapHandle& aBitmap, const TRect& aBlockRect );
       
    88 		void DecRgb1_2L( const TBitmapHandle& aBitmap, const TRect& aBlockRect );
       
    89 		void DecRgb1_4L( const TBitmapHandle& aBitmap, const TRect& aBlockRect );
       
    90 		void DecRgb1_8L( const TBitmapHandle& aBitmap, const TRect& aBlockRect );
       
    91 
       
    92 		//
       
    93 		// Yuv -> Rgb functions
       
    94 		// 
       
    95 		
       
    96 		// YUV420
       
    97 		static void Yuv2Rgb22_11_11( TAny* aPtr );
       
    98 		
       
    99 		// YUV422
       
   100 		static void Yuv2Rgb21_11_11( TAny* aPtr );
       
   101 
       
   102 		// Any other YUV format
       
   103 		static void Yuv2RgbFree( TAny* aPtr );
       
   104 		
       
   105 	private:
       
   106 
       
   107 		/// Jpeg component information struct
       
   108 		/// one for each color component
       
   109 		class TComponent
       
   110 			{
       
   111 			public:
       
   112 				char iType;
       
   113 				char iXFactor;
       
   114 				char iYFactor;
       
   115 				char iQuantTable;
       
   116 				char iID;
       
   117 			};
       
   118 
       
   119 		/// Current jpeg block search / use struct
       
   120 		/// will change to type where the actual huffman data is also contained
       
   121 		class TJpegBlock
       
   122 			{
       
   123 			public:
       
   124 				TUint32 iOffset;
       
   125 				TUint32 iBuf;
       
   126 				TInt iBufBits;
       
   127 				TInt iY;
       
   128 				TInt iU;
       
   129 				TInt iV;
       
   130 			};
       
   131 
       
   132 		class TYuvConv
       
   133 			{
       
   134 			public:
       
   135 				TUint32* iRgb;
       
   136 				TSize iBlkSize;
       
   137 				TInt iRgbWidth;
       
   138 				TInt iBlkPixels;
       
   139 			};
       
   140 
       
   141 		TUint8* iBuffer;			// contains whole jpeg file now
       
   142 		TInt iBufPos;
       
   143 
       
   144 		TUint32 iBuf;				// Huffman bit buffer
       
   145 		TInt iBufBits;				// number of bits in bitbuffer
       
   146 
       
   147 		THuffman* iHuffman[ 4 ];	// huffman lookup tables
       
   148 		TInt iCurrentHuffman;
       
   149 
       
   150 		TUint8* iQt[ 4 ];			// Quantization tables
       
   151 		TUint8* iCurrentQt;
       
   152 
       
   153 		TInt iDct[ 64 ];			// DCT coefficients
       
   154 		TInt iBlk[ 64 ];			// IDCT result
       
   155 
       
   156 		TBool iRst;					// RST flag, might occur on some jpegs
       
   157 		
       
   158 		bool iEOF;
       
   159 
       
   160 		TJpegData iData;
       
   161 		
       
   162 		TComponent iComponent[ 4 ];
       
   163 		TInt iNumComponents;
       
   164 
       
   165 		//RArray< TJpegBlock >iBlock;
       
   166 		TJpegBlock* iBlock;
       
   167 		TInt		iNumBlocks;
       
   168 
       
   169 		TUint8* iExifData;
       
   170 		TInt iExifDataLength;
       
   171 
       
   172 		// LoadBlock variables:
       
   173 		TBitmapHandle	iBm;			// rgb block bitmap
       
   174 		TInt			iBlkPixels;		// number of pixels in rgb block
       
   175 
       
   176 		TUint8*			iC[ 3 ];		// Y,U,V buffer
       
   177 
       
   178 		TInt iYxa;
       
   179 		TInt iYya;
       
   180 		TInt iUxa;
       
   181 		TInt iUya;
       
   182 		TInt iVxa;
       
   183 		TInt iVya;
       
   184 
       
   185 		bool iRgbConv;
       
   186 
       
   187 		//RFs iFs;
       
   188 		//RFile iDebug;
       
   189 
       
   190 		TYuvConv iYuvConv;
       
   191 		void (*iYuv2rgbFunc)(TAny*);
       
   192 
       
   193 
       
   194 		TInt iImageDataStart;
       
   195 		bool iRandomScanned;
       
   196 		bool iLoadBlockPrepared;
       
   197 		
       
   198 		TJpegScale iScale;
       
   199 		
       
   200 		TInt iResetInterval;
       
   201 
       
   202 		
       
   203 	};
       
   204 
       
   205 
       
   206 inline void CJpeg::BufFwd( TInt aBits )
       
   207 	{
       
   208 	iBuf <<= aBits;
       
   209 	iBufBits -= aBits;
       
   210 	}
       
   211 
       
   212 
       
   213 
       
   214 inline TInt CJpeg::BufBits( TInt aBits )
       
   215 	{
       
   216 	if( aBits == 0 ) return 0;
       
   217 	while( iBufBits < aBits )
       
   218 		{
       
   219 		BufLoad8();
       
   220 		}
       
   221 	TInt val = iBuf >> ( 32-aBits );
       
   222 	BufFwd( aBits );
       
   223 	return val;
       
   224 	}
       
   225 
       
   226 
       
   227 
       
   228 inline TInt CJpeg::Buf16()
       
   229 	{
       
   230 	while( iBufBits < 16 )
       
   231 		{
       
   232 		BufLoad8();
       
   233 		}
       
   234 	TInt val = iBuf >> ( 32-16 );
       
   235 	return val;
       
   236 	}
       
   237 
       
   238 
       
   239 
       
   240 inline void CJpeg::BufLoad8()
       
   241 	{
       
   242 	TInt v;
       
   243 	v = iBuffer[ iBufPos++ ];
       
   244 	if( v == 255 )
       
   245 		{
       
   246 		v = iBuffer[ iBufPos++ ]; // escaped 0xff ?
       
   247 		if( v == 0 )
       
   248 			{
       
   249 			v = 255;
       
   250 			}
       
   251 		else
       
   252 			{
       
   253 			//RDebug::Print( _L("Discard %x - position=%d bits=%d"), v, iBufPos, iBufBits );
       
   254 			// here we have probably discarded a RST0..7
       
   255 			iRst = ETrue;		// inform decoder of reset
       
   256 			iBufBits = 255;		// decoder doesn't need any more real data until next mcu
       
   257 			return;
       
   258 			}
       
   259 		}
       
   260 
       
   261 	v <<= ( 24 - iBufBits );
       
   262 	iBuf |= v;
       
   263 	iBufBits += 8;		
       
   264 	}
       
   265 
       
   266 
       
   267 
       
   268 
       
   269 #endif