www.pudn.com > Example2D.rar > CBitmap.cpp


/* 
* ============================================================================ 
*  Name     : CBitmap from CBitmap.h 
*  Part of  : 2DExample 
*  Created  : 01/01/2005 by Forum Nokia 
*  Version  : 1.0 
*  Copyright: Nokia Corporation 
* ============================================================================ 
*/ 
 
// INCLUDE FILES 
#include "CBitmap.h" 
#include "Fbs.h" 
 
const TInt	KColor16MU = 11; // EColor16MU is not defined in earlier SDK's 
 
CBitmap* CBitmap::NewL( const TSize& aSize ) 
	{ 
	CBitmap* bmp = new( ELeave )CBitmap; 
	bmp->iSize = aSize; 
	bmp->iDrawRect = aSize; 
	bmp->iData = new( ELeave ) TUint16[ aSize.iWidth * aSize.iHeight ]; 
	bmp->iMode = EColor4K; 
	Mem::FillZ( bmp->iData, aSize.iWidth * aSize.iHeight ); 
	return bmp; 
	} 
 
 
 
CBitmap* CBitmap::NewL( TUint16* aData, const TSize& aSize, TDisplayMode aMode ) 
	{ 
	CBitmap* bmp = new( ELeave )CBitmap; 
	bmp->iSize = aSize; 
	bmp->iDrawRect = aSize; 
	bmp->iData = aData; 
	bmp->iMode = aMode; 
	return bmp; 
	} 
 
 
 
CBitmap* CBitmap::NewL( const TFileName& aFileName, TInt aIndex ) 
	{ 
	CBitmap* bmp = new( ELeave )CBitmap; 
	 
	CFbsBitmap tmpb; 
	//load the correct bitmap.. 
	TInt ret; 
 
	ret = tmpb.Load( aFileName, aIndex ); 
	 
	if( ret == KErrNone ) 
		{ 
		bmp->iSize = tmpb.SizeInPixels (); 
		bmp->iDrawRect = bmp->iSize; 
		bmp->iData = new( ELeave ) TUint16[ bmp->iSize.iWidth * bmp->iSize.iHeight ]; 
		bmp->iMode = tmpb.DisplayMode(); 
 
		TUint16* dst = ( TUint16* )bmp->iData; 
		for ( TInt y=0; yiSize.iHeight; y++ ) 
			{ 
			TPtr8 buf( ( TUint8* )dst, bmp->iSize.iWidth*2 ); 
			tmpb.GetScanLine( buf, TPoint( 0, y ), bmp->iSize.iWidth, EColor4K ); 
			dst += bmp->iSize.iWidth; 
			} 
 
		tmpb.Reset(); 
		} 
	else 
		{ 
		// file could not be loaded.. 
		// return a empty bitmap.. 
		bmp->iSize = TSize ( 0, 0 ); 
		bmp->iData = NULL; 
		} 
 
	return bmp;	 
	} 
 
 
 
CBitmap::~CBitmap() 
	{ 
	} 
 
 
 
CBitmap::CBitmap() 
	{ 
	} 
 
 
 
void CBitmap::Destroy() 
	{ 
	delete iData; 
	iData = NULL; 
	iSize = TSize( 0,0 ); 
	} 
 
 
 
TSize CBitmap::Size() 
	{ 
	return iSize; 
	} 
	 
void CBitmap::SetSize(const TSize& aSize) 
	{ 
	iSize = aSize; 
	} 
 
 
 
TUint16* CBitmap::Data() 
	{ 
	return iData; 
	} 
 
 
 
void CBitmap::SetData( TUint16* aData ) 
	{ 
	iData = aData; 
	} 
 
 
 
void CBitmap::Draw( CBitmap& aTarget, const TPoint& aPosition ) 
	{ 
	// 
	// Supports only 4096 color bitmaps, target can be 64K and 16M color aswell 
	// 
	if( iMode != EColor4K ) 
		{ 
		return; 
		} 
 
	TUint16* sp = (TUint16*)iData; 
	 
	TSize tsize = aTarget.Size(); 
 
	TSize drawSize = iDrawRect.Size(); 
	TPoint drawPos = iDrawRect.iTl; 
 
	TInt w = drawSize.iWidth; 
	TInt h = drawSize.iHeight; 
	TInt x = aPosition.iX; 
	TInt y = aPosition.iY; 
	TInt sx = drawPos.iX; 
	TInt sy = drawPos.iY; 
 
	// 
	// Clip 
	// 
	if( x < 0 ) 
		{ 
		w += x; 
		sx -= x; 
		x = 0; 
		} 
	if( y < 0 ) 
		{ 
		h += y; 
		sy -= y; 
		y = 0; 
		} 
	if( x + w >= tsize.iWidth ) 
		{ 
		w = tsize.iWidth - x; 
		} 
	if( y + h >= tsize.iHeight ) 
		{ 
		h = tsize.iHeight - y; 
		} 
 
	// 
	// If clipped away, just return 
	// 
	if( w <= 0 ) 
		{ 
		return; 
		}; 
	if( h <= 0 ) 
		{ 
		return; 
		}; 
 
	// 
	// Add source offset to source pointer 
	// 
	sp += sx; 
	sp += sy * iSize.iWidth; 
 
 
	// 
	// Calculate modulo ( how much add pointers between horizontal lines ) 
	// 
	TInt smod = iSize.iWidth - w; 
	TInt tmod = tsize.iWidth - w; 
 
	TInt tMode = aTarget.Mode(); 
 
	// 
	// Select draw mode by target bitmap color mode and draw. 
	// 
	switch( tMode ) 
		{ 
		case EColor4K: 
			{ 
			TUint16* tp = (TUint16*)aTarget.Data(); 
			tp += x; 
			tp += y * tsize.iWidth; 
 
			for( TInt yy=0; yy rrrr0gggg00bbbb0 ( 4K to 64K color ) 
						*tp = (TUint16) ( ( ( c & 0xf00 ) << 4 ) + ( ( c & 0x0f0 ) << 3 ) + ( ( c & 0x00f ) << 1 ) ); 
						} 
					tp++; 
					} 
				tp += tmod; 
				sp += smod; 
				} 
			break; 
			} 
 
		case KColor16MU:  
			{ 
			TUint32* tp = (TUint32*)aTarget.Data(); 
			tp += x; 
			tp += y * tsize.iWidth; 
 
			for( TInt yy=0; yy rrrr0000gggg0000bbbb0000 ( 4K to 16M color ) 
						*tp = ( ( c & 0xf00 ) << 12 ) + ( ( c & 0x0f0 ) << 8 ) + ( ( c & 0x00f ) << 4 ); 
						} 
					tp++; 
					} 
				tp += tmod; 
				sp += smod; 
				} 
			break; 
			} 
 
 
		default: 
			{ 
			// some unsupported color mode 
			break; 
			} 
		} 
	} 
 
 
 
TDisplayMode CBitmap::Mode() 
	{ 
	return iMode; 
	} 
 
 
 
TUint16 CBitmap::MaskColor() 
	{ 
	return iMaskColor; 
	} 
 
 
 
void CBitmap::SetMaskColor( TUint16 aColor ) 
	{ 
	iMaskColor = aColor; 
	} 
 
 
 
TRect CBitmap::DrawRect() 
	{ 
	return iDrawRect; 
	} 
 
 
 
void CBitmap::SetDrawRect( const TRect& aRect ) 
	{ 
	iDrawRect = aRect; 
	} 
 
 
 
void CBitmap::Clear( TRgb aRgb ) 
	{ 
	TInt longwords = iSize.iWidth * iSize.iHeight / 2; 
	TUint32 longword = 0; 
 
	switch( iMode ) 
		{ 
		case EColor4K: 
			{ 
			longword = aRgb.Color4K(); 
			longword |= longword << 16; 
			break; 
			} 
		case EColor64K: 
			{ 
			longword = aRgb.Color64K(); 
			longword |= longword << 16; 
			break; 
			} 
		default: 
			{ 
			longword = aRgb.Color16M(); 
			longwords *= 2; 
			break; 
			} 
		} 
 
	TUint32* p = (TUint32*)iData; 
	TInt i; 
	for( i=0; i