Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
18 const TInt KEightBppPaletteSize = 256;
19 const TInt KEightBppInversePaletteSize = 4096;
21 GLREF_D const TUint8 color256shadowlutab[256];
23 // CDrawEightBppBitmapColor
25 TInt CDrawEightBppBitmapColor::Construct(TSize aSize)
27 return Construct(aSize, (aSize.iWidth + 3) & ~3);
30 TInt CDrawEightBppBitmapColor::Construct(TSize aSize, TInt aStride)
32 iDispMode = EColor256;
33 iShadowIndex = (TUint8*)color256shadowlutab;
35 return CDrawEightBppBitmapCommon::Construct(aSize, aStride);
38 CDrawEightBppBitmapColor::~CDrawEightBppBitmapColor()
41 delete[] iColor4KIndex;
42 if (iShadowIndex != color256shadowlutab)
43 delete[] iShadowIndex;
46 TInt CDrawEightBppBitmapColor::SetCustomPalette(const CPalette* aPalette)
51 iPalette = new TRgb[KEightBppPaletteSize];
53 iColor4KIndex = new TUint8[KEightBppInversePaletteSize];
54 if (iShadowIndex == color256shadowlutab)
55 iShadowIndex = new TUint8[KEightBppPaletteSize];
57 if (iPalette && iColor4KIndex && iShadowIndex)
60 TRgb* palettePtr = iPalette;
61 const TRgb* const palettePtrLimit = iPalette + Min(aPalette->Entries(),KEightBppPaletteSize);
63 while (palettePtr < palettePtrLimit)
64 *palettePtr++ = aPalette->GetEntry(index++);
67 TUint8* inversePtr = iColor4KIndex;
68 const TUint8* const inversePtrLimit = iColor4KIndex + KEightBppInversePaletteSize;
70 while (inversePtr < inversePtrLimit)
71 *inversePtr++ = TUint8(aPalette->NearestIndex(TRgb::_Color4K(index++)));
74 TUint8* shadowPtr = iShadowIndex;
75 const TUint8* const shadowPtrLimit = iShadowIndex + KEightBppPaletteSize;
78 while (shadowPtr < shadowPtrLimit)
80 color = iPalette[index++];
81 const TInt red = Max(color.Red() - 0x33,0) >> 4;
82 const TInt green = Max(color.Green() - 0x33,0) >> 4;
83 const TInt blue = Max(color.Blue() - 0x33,0) >> 4;
84 *shadowPtr++ = iColor4KIndex[(red << 8) | (green << 4) | blue];
90 // Fall through to cleanup
95 delete[] iColor4KIndex;
97 if (iShadowIndex != color256shadowlutab)
99 delete[] iShadowIndex;
100 iShadowIndex = (TUint8*)color256shadowlutab;
103 return aPalette ? KErrNoMemory : KErrNone;
106 TInt CDrawEightBppBitmapColor::GetCustomPalette(CPalette*& aPalette)
108 TRAPD(err, aPalette = CPalette::NewDefaultL(EColor256));
110 if (err == KErrNone && iPalette)
112 for (TInt index = 0; index < KEightBppPaletteSize; index++)
113 aPalette->SetEntry(index, iPalette[index]);
119 TUint8 CDrawEightBppBitmapColor::ColorToIndex(TRgb aColor) const
122 return iColor4KIndex[aColor._Color4K()];
124 return TUint8(aColor.Color256());
127 TRgb CDrawEightBppBitmapColor::IndexToColor(TInt aIndex) const
130 return iPalette[aIndex];
132 return TRgb::Color256(aIndex);
135 void CDrawEightBppBitmapColor::Shadow(TRgb& aColor)
137 if (iShadowMode & EFade)
138 aColor = FadeRgb(IndexToColor(ColorToIndex(aColor)));
140 if (iShadowMode & EShadow)
141 aColor = IndexToColor(iShadowIndex[ColorToIndex(aColor)]);
144 //aX, aY - physical coordinates
145 TRgb CDrawEightBppBitmapColor::ReadRgbNormal(TInt aX,TInt aY) const
147 return IndexToColor(*PixelAddress(aX,aY));
150 //aRect - logical coordinates
151 void CDrawEightBppBitmapColor::ShadowArea(const TRect& aRect)
153 const TRect rect(DeOrientate(aRect));//rect - physical coordinates
155 __ASSERT_DEBUG(rect.iTl.iX>=0 && rect.iBr.iX<=iSize.iWidth,Panic(EScreenDriverPanicOutOfBounds));
156 __ASSERT_DEBUG(rect.iTl.iY>=0 && rect.iBr.iY<=iSize.iHeight,Panic(EScreenDriverPanicOutOfBounds));
158 const TInt longWidth = iLongWidth;
159 TUint8* pixelPtr = PixelAddress(rect.iTl.iX,rect.iTl.iY);
160 const TUint8* pixelRowPtrLimit = pixelPtr + (rect.Height() * longWidth);
162 register const TUint8* bitsEnd =
163 reinterpret_cast <const TUint8*> (iBits) + iLongWidth * iSize.iHeight;
165 if(pixelRowPtrLimit >= bitsEnd)
167 pixelRowPtrLimit = bitsEnd;
170 if (iShadowMode & EFade)
172 TUint8* pixelRowPtr = pixelPtr;
173 TUint8* pixelPtrLimit = pixelPtr + rect.Width();
175 while (pixelRowPtr < pixelRowPtrLimit)
177 TUint8* tempPixelPtr = pixelRowPtr;
179 while (tempPixelPtr < pixelPtrLimit)
181 color = IndexToColor(*tempPixelPtr);
182 color = FadeRgb(color);
183 *tempPixelPtr++ = ColorToIndex(color);
186 pixelRowPtr += longWidth;
187 pixelPtrLimit += longWidth;
191 if (iShadowMode & EShadow)
193 TUint8* pixelRowPtr = pixelPtr;
194 TUint8* pixelPtrLimit = pixelPtr + rect.Width();
196 while (pixelRowPtr < pixelRowPtrLimit)
198 TUint8* tempPixelPtr = pixelRowPtr;
200 while (tempPixelPtr < pixelPtrLimit)
202 *tempPixelPtr = iShadowIndex[*tempPixelPtr];
206 pixelRowPtr += longWidth;
207 pixelPtrLimit += longWidth;
212 void CDrawEightBppBitmapColor::ShadowBuffer(TInt aLength,TUint32* aBuffer)
214 __ASSERT_DEBUG(aLength>0,Panic(EScreenDriverPanicInvalidParameter));
215 __ASSERT_DEBUG(aBuffer!=NULL,Panic(EScreenDriverPanicInvalidParameter));
217 const TUint8* limit = ((TUint8*)aBuffer) + aLength;
219 if (iShadowMode & EFade)
221 TUint8* buffer = (TUint8*)aBuffer;
224 while (buffer < limit)
226 color = FadeRgb(IndexToColor(*buffer));
227 *buffer++ = ColorToIndex(color);
231 if (iShadowMode & EShadow)
233 TUint8* buffer = (TUint8*)aBuffer;
235 while (buffer < limit)
237 *buffer = iShadowIndex[*buffer];
243 //aX, aY - physical coordinates
244 void CDrawEightBppBitmapColor::WriteRgb(TInt aX,TInt aY,TRgb aColor)
246 CDrawEightBppBitmapCommon::WriteRgb(aX,aY,ColorToIndex(aColor));
249 //aX, aY - physical coordinates
250 void CDrawEightBppBitmapColor::WriteBinary(TInt aX,TInt aY,TUint32* aData,TInt aLength,TInt aHeight,TRgb aColor)
252 CDrawEightBppBitmapCommon::WriteBinary(aX,aY,aData,aLength,aHeight,ColorToIndex(aColor));
255 //aX, aY - physical coordinates
256 void CDrawEightBppBitmapColor::WriteBinaryOp(TInt aX,TInt aY,TUint32* aData,TInt aLength,TInt aHeight,TRgb aColor,CGraphicsContext::TDrawMode aDrawMode)
258 CDrawEightBppBitmapCommon::WriteBinaryOp(aX,aY,aData,aLength,aHeight,ColorToIndex(aColor),aDrawMode);
261 //aX, aY - physical coordinates
262 void CDrawEightBppBitmapColor::WriteBinaryLineVertical(TInt aX,TInt aY,TUint32* aData,TInt aLength,TRgb aColor,TBool aUp)
264 CDrawEightBppBitmapCommon::WriteBinaryLineVertical(aX,aY,aData,aLength,ColorToIndex(aColor),aUp);
268 MAlphaBlend::WriteRgbAlphaLine() implementation.
269 @see MAlphaBlend::WriteRgbAlphaLine()
271 void CDrawEightBppBitmapColor::WriteRgbAlphaLine(TInt aX, TInt aY, TInt aLength,
272 const TUint8* aRgbBuffer,
273 const TUint8* aMaskBuffer,
274 MAlphaBlend::TShadowing aShadowing,
275 CGraphicsContext::TDrawMode /*aDrawMode*/)
277 DeOrientate(aX,aY);//aX, aY - physical coordinates
278 TUint8* pixelPtr = PixelAddress(aX,aY);
279 register TInt pixelPtrInc = LogicalPixelAddressIncrement();
280 const TUint8* maskBufferPtrLimit = aMaskBuffer + aLength;
285 while (aMaskBuffer < maskBufferPtrLimit)
287 TRgb srcColor(aRgbBuffer[2],aRgbBuffer[1],aRgbBuffer[0]);
288 if(aShadowing == MAlphaBlend::EShdwBefore)
292 pixelColor = ::AlphaBlend(srcColor,IndexToColor(pixelPtr[0]),aMaskBuffer[0]);
293 if(aShadowing == MAlphaBlend::EShdwAfter)
297 MapColorToUserDisplayMode(pixelColor);
298 pixelPtr[0] = ColorToIndex(pixelColor);
299 pixelPtr += pixelPtrInc;
306 const TUint8* bitsStart = reinterpret_cast <const TUint8*> (iBits);
307 const TUint8* bitsEnd = bitsStart + iLongWidth * iSize.iHeight;
308 while (aMaskBuffer < maskBufferPtrLimit)
310 TRgb srcColor(aRgbBuffer[2],aRgbBuffer[1],aRgbBuffer[0]);
311 if(aShadowing == MAlphaBlend::EShdwBefore)
315 pixelColor = ::AlphaBlend(srcColor,IndexToColor(pixelPtr[0]),aMaskBuffer[0]);
316 if(aShadowing == MAlphaBlend::EShdwAfter)
320 MapColorToUserDisplayMode(pixelColor);
321 const TUint8* pixelRowPtrLimit = bitsStart + (aY + 1) * iLongWidth;
322 SetPixels(pixelPtr, ColorToIndex(pixelColor), pixelRowPtrLimit, bitsStart, bitsEnd);
323 pixelPtr += pixelPtrInc;
331 void CDrawEightBppBitmapColor::WriteRgbMulti(TInt aX,TInt aY,TInt aLength,TInt aRows,TRgb aColor)
333 CDrawEightBppBitmapCommon::WriteRgbMulti(aX,aY,aLength,aRows,ColorToIndex(aColor));
336 void CDrawEightBppBitmapColor::WriteRgbMultiXOR(TInt aX,TInt aY,TInt aLength,TInt aRows,TRgb aColor)
338 CDrawEightBppBitmapCommon::WriteRgbMultiXOR(aX,aY,aLength,aRows,ColorToIndex(aColor));
341 void CDrawEightBppBitmapColor::WriteRgbMultiAND(TInt aX,TInt aY,TInt aLength,TInt aRows,TRgb aColor)
343 CDrawEightBppBitmapCommon::WriteRgbMultiAND(aX,aY,aLength,aRows,ColorToIndex(aColor));
346 void CDrawEightBppBitmapColor::WriteRgbMultiOR(TInt aX,TInt aY,TInt aLength,TInt aRows,TRgb aColor)
348 CDrawEightBppBitmapCommon::WriteRgbMultiOR(aX,aY,aLength,aRows,ColorToIndex(aColor));
351 //aX, aY - not deorientated
352 //aLength - not scaled
353 void CDrawEightBppBitmapColor::WriteRgbAlphaMulti(TInt aX,TInt aY,TInt aLength,
354 TRgb aColor,const TUint8* aMaskBuffer)
356 DeOrientate(aX,aY);//aX, aY - scaled and deorientated
357 register TUint8* pixelPtr = PixelAddress(aX,aY);
358 register TInt pixelPtrInc = LogicalPixelAddressIncrement();
359 const TUint8* maskBufferPtrLimit = aMaskBuffer + aLength;
364 register TInt red = aColor.Red();
365 register TInt green = aColor.Green();
366 register TInt blue = aColor.Blue();
371 while(aMaskBuffer < maskBufferPtrLimit)
373 pixelColor = AlphaBlend(red, green, blue, IndexToColor(pixelPtr[0]), aMaskBuffer[0]);
374 pixelPtr[0] = ColorToIndex(pixelColor);
375 pixelPtr += pixelPtrInc;
381 const TUint8* bitsStart = reinterpret_cast <const TUint8*> (iBits);
382 const TUint8* bitsEnd = bitsStart + iLongWidth * iSize.iHeight;
383 while(aMaskBuffer < maskBufferPtrLimit)
385 pixelColor = AlphaBlend(red, green, blue, IndexToColor(pixelPtr[0]), aMaskBuffer[0]);
386 const TUint8* pixelRowPtrLimit = bitsStart + (aY + 1) * iLongWidth;
387 SetPixels(pixelPtr, ColorToIndex(pixelColor), pixelRowPtrLimit, bitsStart, bitsEnd);
388 pixelPtr += pixelPtrInc;
396 void CDrawEightBppBitmapColor::MapColorToUserDisplayMode(TRgb& aColor)
398 switch (iUserDispMode)
401 aColor = TRgb::_Gray2(aColor._Gray2());
404 aColor = TRgb::_Gray4(aColor._Gray4());
407 case EGray256:// EGray256 can't be done - nearest is EGray16
408 aColor = TRgb::_Gray16(aColor._Gray16());
411 aColor = TRgb::Color16(aColor.Color16());
418 void CDrawEightBppBitmapColor::MapBufferToUserDisplayMode(TInt aLength,TUint32* aBuffer)
420 TUint8* bufferPtr = (TUint8*)aBuffer;
421 const TUint8* bufferLimit = bufferPtr + aLength;
424 switch (iUserDispMode)
427 while (bufferPtr < bufferLimit)
429 color = IndexToColor(*bufferPtr);
430 color = TRgb::_Gray2(color._Gray2());
431 *bufferPtr++ = ColorToIndex(color);
435 while (bufferPtr < bufferLimit)
437 color = IndexToColor(*bufferPtr);
438 color = TRgb::_Gray4(color._Gray4());
439 *bufferPtr++ = ColorToIndex(color);
443 case EGray256:// EGray256 can't be done - nearest is EGray16
444 while (bufferPtr < bufferLimit)
446 color = IndexToColor(*bufferPtr);
447 color = TRgb::_Gray16(color._Gray16());
448 *bufferPtr++ = ColorToIndex(color);
452 while (bufferPtr < bufferLimit)
454 color = IndexToColor(*bufferPtr);
455 color = TRgb::Color16(color.Color16());
456 *bufferPtr++ = ColorToIndex(color);
464 TInt CDrawEightBppBitmapColor::WriteRgbOutlineAndShadow(TInt aX, TInt aY, const TInt aLength,
465 TUint32 aOutlinePenColor, TUint32 aShadowColor,
466 TUint32 aFillColor, const TUint8* aDataBuffer)
468 //This is non-optimised since this screen mode is rarely used and is usually
469 //fast enough without optimisation.
471 TUint8* pixelPtr = PixelAddress(aX,aY);
472 const TInt pixelPtrInc = LogicalPixelAddressIncrement();
473 const TUint8* dataBufferPtrLimit = aDataBuffer + aLength;
474 TInt blendedRedColor;
475 TInt blendedGreenColor;
476 TInt blendedBlueColor;
480 TRgb outlinePenColor;
481 outlinePenColor.SetInternal(aOutlinePenColor);
483 shadowColor.SetInternal(aShadowColor);
485 fillColor.SetInternal(aFillColor);
487 const TInt redOutlinePenColor = outlinePenColor.Red();
488 const TInt redShadowColor = shadowColor.Red();
489 const TInt redFillColor = fillColor.Red();
491 const TInt greenOutlinePenColor = outlinePenColor.Green();
492 const TInt greenShadowColor = shadowColor.Green();
493 const TInt greenFillColor = fillColor.Green();
495 const TInt blueOutlinePenColor = outlinePenColor.Blue();
496 const TInt blueShadowColor = shadowColor.Blue();
497 const TInt blueFillColor = fillColor.Blue();
499 while (aDataBuffer < dataBufferPtrLimit)
501 index = *aDataBuffer++;
502 if (255 == FourColorBlendLookup[index][KBackgroundColorIndex])
505 //No drawing required so move on to next pixel.
506 pixelPtr += pixelPtrInc;
509 else if (255 == FourColorBlendLookup[index][KFillColorIndex])
512 finalColor.SetInternal(aFillColor);
514 else if (255 == FourColorBlendLookup[index][KShadowColorIndex])
517 finalColor.SetInternal(aShadowColor);
519 else if (255 == FourColorBlendLookup[index][KOutlineColorIndex])
522 finalColor.SetInternal(aOutlinePenColor);
526 TRgb backgroundColor = TRgb::Color256(*pixelPtr);
528 blendedRedColor = (redOutlinePenColor * FourColorBlendLookup[index][KOutlineColorIndex] +
529 redShadowColor * FourColorBlendLookup[index][KShadowColorIndex] +
530 redFillColor * FourColorBlendLookup[index][KFillColorIndex] +
531 backgroundColor.Red() * FourColorBlendLookup[index][KBackgroundColorIndex]) >> 8;
533 blendedGreenColor = (greenOutlinePenColor * FourColorBlendLookup[index][KOutlineColorIndex] +
534 greenShadowColor * FourColorBlendLookup[index][KShadowColorIndex] +
535 greenFillColor * FourColorBlendLookup[index][KFillColorIndex] +
536 backgroundColor.Green() * FourColorBlendLookup[index][KBackgroundColorIndex]) >> 8;
538 blendedBlueColor = (blueOutlinePenColor * FourColorBlendLookup[index][KOutlineColorIndex] +
539 blueShadowColor * FourColorBlendLookup[index][KShadowColorIndex] +
540 blueFillColor * FourColorBlendLookup[index][KFillColorIndex] +
541 backgroundColor.Blue() * FourColorBlendLookup[index][KBackgroundColorIndex]) >> 8;
543 finalColor = TRgb(blendedRedColor, blendedGreenColor, blendedBlueColor);
546 *pixelPtr = TUint8(finalColor.Color256());
547 pixelPtr += pixelPtrInc;