mmplugins/imagingplugins/imagedisplay/plugins/mng/PixelConsumer.cpp
changeset 0 40261b775718
equal deleted inserted replaced
-1:000000000000 0:40261b775718
       
     1 // Copyright (c) 2004-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 /** 	@file
       
    17 	@internalTechnology */
       
    18 #include <fbs.h>
       
    19 
       
    20 #include "PixelConsumer.h"
       
    21 
       
    22 inline 
       
    23 void SetRgbPixel(TUint8* ptr,const TUint aR, const TUint aG,const TUint aB)
       
    24 	{
       
    25 	*ptr   	= TUint8(aB);
       
    26 	ptr[1]	= TUint8(aG);
       
    27 	ptr[2]  = TUint8(aR);
       
    28 	}
       
    29 
       
    30 inline 
       
    31 void SetRgbaPixel(TUint8* ptr,const TUint aR, const TUint aG,const TUint aB, const TUint aA)
       
    32 	{
       
    33 	*ptr	= TUint8(aB);
       
    34 	ptr[1]	= TUint8(aG);
       
    35 	ptr[2]	= TUint8(aR);
       
    36 	ptr[3]  = TUint8(aA);
       
    37 	}
       
    38 
       
    39 void CPixelConsumerBase::InitL(const TSize& aFrameSize)
       
    40 	{
       
    41 	ASSERT(iFrameBuffer == NULL);
       
    42 	iFrameSize	= aFrameSize;
       
    43 	iFrameBufferSize = iFrameSize.iHeight*iFrameSize.iWidth*iPixelSize;
       
    44 	
       
    45 	iFrameBuffer= new (ELeave) TUint8[ iFrameBufferSize ];
       
    46 	
       
    47 	if(iPixelSize == KRgbaPixelSize)
       
    48 		{
       
    49 		TUint32* ptr = reinterpret_cast<TUint32*>(iFrameBuffer);
       
    50 		TUint32* end = ptr+(iFrameBufferSize/iPixelSize);
       
    51 		while(ptr < end)
       
    52 			{
       
    53 			*ptr++ = 0xFF000000; // initialise 32bit RGB buffer with opaque alpha (for both 16MU and 16MA)
       
    54 			}
       
    55 		}
       
    56 	else
       
    57 		{
       
    58 		Mem::Fill(iFrameBuffer, iFrameBufferSize, 0);	
       
    59 		}
       
    60 
       
    61 	iImageRect = TRect(TPoint(0,0),iFrameSize.AsPoint());
       
    62 	SetClippingRect(iImageRect);
       
    63 	iNeedBufferFlush = ETrue;
       
    64 	}
       
    65 
       
    66 CPixelConsumerBase::~CPixelConsumerBase()
       
    67 	{
       
    68 	delete [] iFrameBuffer;
       
    69 	}
       
    70 
       
    71 void CPixelConsumerBase::Prepare()
       
    72 	{
       
    73 	iNeedBufferFlush = ETrue;
       
    74 	GetBuffer();
       
    75 	iCurrentPos.SetXY(0,0);
       
    76 	}
       
    77 
       
    78 void CPixelConsumerBase::SetClippingRect(const TRect& aRect)
       
    79 	{
       
    80 	iClippingRect	= aRect;
       
    81 	iRealOutputRect	= aRect;
       
    82 	iRealOutputRect.Intersection(iImageRect);
       
    83 	}
       
    84 
       
    85 void CPixelConsumerBase::GetBitmap(CFbsBitmap& aBitmap, const TRect& aSourceRect)
       
    86 	{
       
    87 	ASSERT(aBitmap.DisplayMode()==EColor16M || aBitmap.DisplayMode()==EColor16MA || 
       
    88 			aBitmap.DisplayMode()==EColor16MU || aBitmap.DisplayMode()==EGray256);
       
    89 	
       
    90 	TRect imgRect(TPoint(0,0), iFrameSize.AsPoint() );
       
    91 	imgRect.Intersection(aSourceRect);
       
    92 	ASSERT(imgRect == aSourceRect); // ensure that source rect is within the buffer
       
    93 
       
    94 	if (iNeedBufferFlush)
       
    95 		{
       
    96 		TUint8* DataAddr=reinterpret_cast<TUint8*>( aBitmap.DataAddress() );
       
    97 		const TUint8* FrameBufAddr	=iFrameBuffer + iPixelSize*( aSourceRect.iTl.iY*iFrameSize.iWidth+aSourceRect.iTl.iX );
       
    98 		const TInt KLineSize		=iPixelSize*aSourceRect.Width();
       
    99 		const TInt KFrameBufLineSize=iPixelSize*iFrameSize.iWidth;
       
   100 		const TInt KBmpScanLineLength= CFbsBitmap::ScanLineLength(aBitmap.SizeInPixels().iWidth, aBitmap.DisplayMode() );
       
   101 		for (TInt y=aSourceRect.iBr.iY-aSourceRect.iTl.iY; y; --y, DataAddr +=KBmpScanLineLength, FrameBufAddr+=KFrameBufLineSize)
       
   102 			{
       
   103 			Mem::Copy(DataAddr, FrameBufAddr, KLineSize);
       
   104 			}
       
   105 		}
       
   106 	iNeedBufferFlush = EFalse;
       
   107 	}
       
   108 
       
   109 void CPixelConsumerBase::Clear(TUint aColour, const TRect& aRect)
       
   110 	{
       
   111 	GetBuffer();
       
   112 	const TInt width=aRect.Width();
       
   113 	if (width<1)
       
   114 		{
       
   115 		return;
       
   116 		}
       
   117 	
       
   118 	switch(iPixelSize)
       
   119 		{
       
   120 		case KRgbPixelSize:
       
   121 			{
       
   122 			const TUint r=Red(aColour);
       
   123 			const TUint g=Green(aColour);
       
   124 			const TUint b=Blue(aColour);
       
   125 			for (TInt y=aRect.iTl.iY; y < aRect.iBr.iY; ++y)
       
   126 				{
       
   127 				TUint8* pPixel=(iFrameBuffer+KRgbPixelSize*(y*iFrameSize.iWidth+aRect.iTl.iX));
       
   128 				TInt x=width;
       
   129 				do {
       
   130 					SetRgbPixel(pPixel, r, g, b);
       
   131 					pPixel+=KRgbPixelSize;
       
   132 					} while (--x);
       
   133 				}
       
   134 			break;
       
   135 			}
       
   136 		case KRgbaPixelSize:
       
   137 			{
       
   138 			const TUint r=Red(aColour);
       
   139 			const TUint g=Green(aColour);
       
   140 			const TUint b=Blue(aColour);
       
   141 			const TUint a=Alpha(aColour);
       
   142 			for (TInt y=aRect.iTl.iY; y < aRect.iBr.iY; ++y)
       
   143 				{
       
   144 				TUint8* pPixel=(iFrameBuffer+KRgbaPixelSize*(y*iFrameSize.iWidth+aRect.iTl.iX));
       
   145 				TInt x=width;
       
   146 				do {
       
   147 					SetRgbaPixel(pPixel, r, g, b, a);
       
   148 					pPixel+=KRgbaPixelSize;
       
   149 					} while (--x);
       
   150 				}
       
   151 			break;
       
   152 			}
       
   153 		case KAlphaPixelSize:
       
   154 			{
       
   155 			TUint8* pPixel=iFrameBuffer+KAlphaPixelSize*(aRect.iTl.iY*iFrameSize.iWidth+aRect.iTl.iX);
       
   156 			for (TInt y=aRect.iTl.iY; y < aRect.iBr.iY; ++y)
       
   157 				{
       
   158 				Mem::Fill(pPixel, width, aColour );
       
   159 				pPixel += iFrameSize.iWidth;
       
   160 				}
       
   161 			break;
       
   162 			}
       
   163 		default:
       
   164 			ASSERT(EFalse);
       
   165 		}
       
   166 	}
       
   167 
       
   168 void CPixelConsumerBase::ClearAlpha(TUint aColour, const TRect& aRect)
       
   169 	{
       
   170 	ASSERT(iPixelSize == KRgbaPixelSize);
       
   171 	
       
   172 	GetBuffer();
       
   173 	const TInt width=aRect.Width();
       
   174 	if (width<1)
       
   175 		{
       
   176 		return;
       
   177 		}
       
   178 
       
   179 	const TUint a=Alpha(aColour);
       
   180 	for (TInt y=aRect.iTl.iY; y < aRect.iBr.iY; ++y)
       
   181 		{
       
   182 		TUint8* pPixel=(iFrameBuffer+KRgbaPixelSize*(y*iFrameSize.iWidth+aRect.iTl.iX));
       
   183 		TInt x=width;
       
   184 		do 
       
   185 			{
       
   186 			pPixel[3] = a;
       
   187 			pPixel+=KRgbaPixelSize;
       
   188 			} while (--x);
       
   189 		}
       
   190 	}
       
   191 
       
   192 void CRgbPixelConsumer::SetRGBPixel(TRgbaColour aPixelColour)
       
   193 	{
       
   194 	const TPoint RealPos( iOrigin.iX + iCurrentPos.iX, iOrigin.iY + iCurrentPos.iY );
       
   195 	if (Contains(iRealOutputRect, RealPos))
       
   196 		{
       
   197 		
       
   198 		if (NULL != iAlphaConsumer)
       
   199 			{
       
   200 // IF AlphaConsumer (if trans or alpha), then get alpha value
       
   201 			const TUint alpha= (iAlphaConsumer->Enabled()? Alpha(aPixelColour) : KOpaqueAlpha );
       
   202 //
       
   203 // alpha blending is: 
       
   204 //			Aresult * Cresult	= Afg*Cfg + Abg*Cbg - Afg*Abg*Cbg OR Afg*Cfg + Cbg * Abg*(1 - Afg)
       
   205 //			Aresult				= Afg + (1 - Afg) * Abg
       
   206 //			where 0 <= Alpha < 1
       
   207 //			However we are using fixed point where
       
   208 //				  0 <= Alpha <= 0xff
       
   209 //
       
   210 			if (alpha)
       
   211 				{
       
   212 				const TInt bufferOffset=RealPos.iY*iFrameSize.iWidth+RealPos.iX;
       
   213 				TUint8* ptr=iFrameBuffer+KRgbPixelSize*bufferOffset;
       
   214 
       
   215 				if (alpha < KOpaqueAlpha)
       
   216 					{
       
   217 					const TUint oldA = iAlphaConsumer->GetPixel(bufferOffset);
       
   218 					if (oldA)
       
   219 						{
       
   220 						const TUint a2=(oldA*(0xffu-alpha)+0x80)>>8;
       
   221 						
       
   222 						TUint oldC=TUint(*ptr);
       
   223 						*ptr++ = TUint8(( Blue(aPixelColour)*alpha + oldC*a2)>>8);
       
   224 												
       
   225 						oldC=TUint(*ptr);
       
   226 						*ptr++ = TUint8(( Green(aPixelColour)*alpha + oldC*a2)>>8);
       
   227 
       
   228 						oldC=*ptr;
       
   229 						*ptr   = TUint8(( Red(aPixelColour)*alpha + a2*oldC )>>8 );
       
   230 
       
   231 						iAlphaConsumer->SetPixelByBufferOffset(bufferOffset, alpha + a2 );
       
   232 						}
       
   233 					else
       
   234 						{
       
   235 						// "old" pixel alpha is fully transparent, so set the pixel and its alpha
       
   236 						// in the first pass, it will be 0x00 so set both pixel and alpha
       
   237 						iAlphaConsumer->SetPixelByBufferOffset(bufferOffset, alpha);
       
   238 						SetRgbPixel(ptr, 	Red(aPixelColour), Green(aPixelColour), Blue(aPixelColour));
       
   239 						}
       
   240 					}
       
   241 				else
       
   242 					{
       
   243 					// pixel alpha is opaque, so set the pixel and its alpha
       
   244 					iAlphaConsumer->SetPixelByBufferOffset(bufferOffset, alpha);
       
   245 					SetRgbPixel(ptr, 	Red(aPixelColour), Green(aPixelColour), Blue(aPixelColour));
       
   246 					}
       
   247 				}
       
   248 			}
       
   249 		else
       
   250 			{
       
   251 			// no Alpha blending just set the pixel
       
   252 			TUint8* ptr=iFrameBuffer+KRgbPixelSize*(RealPos.iY*iFrameSize.iWidth+RealPos.iX);
       
   253 			SetRgbPixel(ptr, 	Red(aPixelColour), Green(aPixelColour), Blue(aPixelColour));
       
   254 			}
       
   255 		}
       
   256 
       
   257 	if (++iCurrentPos.iX >= iFrameSize.iWidth)
       
   258 		{
       
   259 		iCurrentPos.iX = 0;
       
   260 		++iCurrentPos.iY;
       
   261 		}
       
   262 	}
       
   263 
       
   264 void CRgbPixelConsumer::SetRGBAPixel(TRgbaColour aPixelColour)
       
   265 	{
       
   266 		const TPoint RealPos( iOrigin.iX + iCurrentPos.iX, iOrigin.iY + iCurrentPos.iY );
       
   267 	if (Contains(iRealOutputRect, RealPos))
       
   268 		{
       
   269 		const TInt bufferOffset=RealPos.iY*iFrameSize.iWidth+RealPos.iX;
       
   270 		TUint8* ptr=iFrameBuffer+KRgbaPixelSize*bufferOffset;
       
   271 		
       
   272 		if (NULL != iAlphaConsumer)
       
   273 			{
       
   274 // IF AlphaConsumer (if trans or alpha), then get alpha value
       
   275 			const TUint alpha= (iAlphaConsumer->Enabled()? Alpha(aPixelColour) : KOpaqueAlpha );
       
   276 // alpha blending is: 
       
   277 //			Aresult * Cresult	= Afg*Cfg + Abg*Cbg - Afg*Abg*Cbg OR Afg*Cfg + Cbg * Abg*(1 - Afg)
       
   278 //			Aresult				= Afg + (1 - Afg) * Abg
       
   279 //			where 0 <= Alpha < 1
       
   280 //			However we are using fixed point where
       
   281 //				  0 <= Alpha <= 0xff
       
   282 
       
   283 			if (alpha)
       
   284 				{
       
   285 				if (alpha < KOpaqueAlpha)
       
   286 					{
       
   287 					const TUint oldA = iAlphaConsumer->GetPixel(bufferOffset);
       
   288 					if (oldA)
       
   289 						{
       
   290 						const TUint a2=(oldA*(0xffu-alpha)+0x80)>>8;
       
   291 						
       
   292 						TUint oldC=TUint(*ptr);
       
   293 						*ptr++ = TUint8(( Blue(aPixelColour)*alpha + oldC*a2)>>8);
       
   294 												
       
   295 						oldC=TUint(*ptr);
       
   296 						*ptr++ = TUint8(( Green(aPixelColour)*alpha + oldC*a2)>>8);
       
   297 
       
   298 						oldC=*ptr;
       
   299 						*ptr++   = TUint8(( Red(aPixelColour)*alpha + a2*oldC )>>8 );
       
   300 						
       
   301 						*ptr	= iAlphaEnabled ? TUint8(Alpha((alpha + a2) << KAlphaShift)) : KOpaqueAlpha;
       
   302 						
       
   303 						iAlphaConsumer->SetPixelByBufferOffset(bufferOffset, alpha + a2);
       
   304 						}
       
   305 					else
       
   306 						{
       
   307 						// "old" pixel alpha is fully transparent, so set the pixel and its alpha
       
   308 						// in the first pass, it will be 0x00 so set both pixel and alpha
       
   309 						iAlphaConsumer->SetPixelByBufferOffset(bufferOffset, alpha);
       
   310 						SetRgbaPixel(ptr, 	Red(aPixelColour), Green(aPixelColour), Blue(aPixelColour), iAlphaEnabled ? Alpha((alpha) << KAlphaShift) : KOpaqueAlpha);
       
   311 						}
       
   312 					}
       
   313 				else
       
   314 					{
       
   315 					iAlphaConsumer->SetPixelByBufferOffset(bufferOffset, alpha);
       
   316 					SetRgbaPixel(ptr, 	Red(aPixelColour), Green(aPixelColour), Blue(aPixelColour), KOpaqueAlpha);
       
   317 					}
       
   318 				}
       
   319 			}
       
   320 		else // dealing with source with no transparency
       
   321 			{
       
   322 			// Just set the pixel to fully opaque
       
   323 			SetRgbaPixel(ptr, Red(aPixelColour), Green(aPixelColour), Blue(aPixelColour), KOpaqueAlpha);
       
   324 			}
       
   325 		}
       
   326 
       
   327 	if (++iCurrentPos.iX >= iFrameSize.iWidth)
       
   328 		{
       
   329 		iCurrentPos.iX = 0;
       
   330 		++iCurrentPos.iY;
       
   331 		}
       
   332 	}
       
   333