1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/graphics/graphicsdeviceinterface/gdi/inc/blendingalgorithms.inl Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,133 @@
1.4 +// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +const TUint32 KRBMask = 0x00ff00ff;
1.20 +const TUint32 KAGMask = 0xff00ff00;
1.21 +const TUint32 KGMask = 0x0000ff00;
1.22 +
1.23 +//Algoriths for Premultiplied alpha screenmode/pixel format.
1.24 +
1.25 +inline TUint32 PMAPixelBlend(TUint32 aDestPixel, TUint32 aSrcPixel, TUint8 aMask)
1.26 + {
1.27 + if(aMask)
1.28 + {
1.29 + if(aMask == 0xff) // opaque, so unchanged
1.30 + {
1.31 + return aSrcPixel;
1.32 + }
1.33 + else
1.34 + {
1.35 + return PMABlend_noChecks(aDestPixel, aSrcPixel, aMask);
1.36 + }
1.37 + }
1.38 + else // completely transparent
1.39 + {
1.40 + return aDestPixel;
1.41 + }
1.42 + }
1.43 +
1.44 +inline TUint32 PMAPixelBlend(TUint32 aDestPixel, TUint32 aSrcPixel)
1.45 + {
1.46 + TUint8 mask = (TUint8)(aSrcPixel >> 24);
1.47 + return PMAPixelBlend(aDestPixel, aSrcPixel, mask);
1.48 + }
1.49 +
1.50 +inline TUint32 PMABlend_noChecks(TUint32 aDestPixel, TUint32 aSrcPixel, TUint8 aMaskingFactor)
1.51 + {
1.52 + TUint32 src_c = aSrcPixel & KRBMask;
1.53 + TUint32 dst_c = aDestPixel & KRBMask;
1.54 + const TUint32 mask = 0x0100 - aMaskingFactor;
1.55 + dst_c = (src_c + ((mask * dst_c)>>8)) & KRBMask;
1.56 + src_c = (aSrcPixel & KAGMask)>>8;
1.57 + TUint32 dst_ag = (aDestPixel & KAGMask)>>8;
1.58 + dst_c |= ((src_c + ((mask * dst_ag)>>8)) & KRBMask)<<8;
1.59 + return dst_c;
1.60 + }
1.61 +
1.62 +inline void PMABlend_noChecksInplace(TUint32& aDest_io, const TUint32& aSrcPixel, TUint8 aMaskingFactor)
1.63 + {
1.64 + TUint32 src_c = aSrcPixel & KRBMask;
1.65 + TUint32 dst_ag = (aDest_io & KAGMask) >> 8;
1.66 + aDest_io = aDest_io & KRBMask;
1.67 + const TUint32 mask = 0x0100 - aMaskingFactor;
1.68 + aDest_io = (src_c + ((mask * aDest_io) >> 8)) & KRBMask;
1.69 + src_c = (aSrcPixel & KAGMask) >> 8;
1.70 + aDest_io |= ((src_c + ((mask * dst_ag) >> 8)) & KRBMask)<<8;
1.71 + }
1.72 +
1.73 +inline void PMAInplaceBlend(TUint32& aDest_io, TUint32& aSrc_in)
1.74 + {
1.75 + TUint8 mask = (TUint8)(aSrc_in >> 24);
1.76 + if(mask)
1.77 + {
1.78 + if(mask == 0xff) // opaque, so dst = src.
1.79 + {
1.80 + aDest_io = aSrc_in;
1.81 + }
1.82 + else
1.83 + {
1.84 + PMABlend_noChecksInplace(aDest_io, aSrc_in, mask);
1.85 + }
1.86 + }
1.87 + //else src completely transparent, so dst unchanged.
1.88 + }
1.89 +
1.90 +inline TUint32 NonPMA2PMAPixel(TUint32 aPixel)
1.91 + {
1.92 + TUint8 tA = (TUint8)(aPixel >> 24);
1.93 + if (tA==0)
1.94 + {
1.95 + return 0;
1.96 + }
1.97 + if (tA==0xff)
1.98 + {
1.99 + return aPixel;
1.100 + }
1.101 +
1.102 + // Use a bias value of 128 rather than 255, but also add 1/256 of the numerator
1.103 + // before dividing the sum by 256.
1.104 +
1.105 + TUint32 tap1=tA+1;
1.106 + TUint32 scaledRB = (aPixel & KRBMask) * tap1;
1.107 + TUint32 scaledG = (aPixel & KGMask ) * tap1;
1.108 + return (aPixel & 0xff000000) | ((scaledRB>>8) & KRBMask) | ((scaledG>>8)& KGMask);
1.109 + }
1.110 +
1.111 +inline TUint32 PMA2NonPMAPixel(TUint32 aPixel, const TUint16* aNormTable)
1.112 + {
1.113 + TUint8 alpha = (TUint8)(aPixel >> 24);
1.114 + if (alpha==0)
1.115 + {
1.116 + return 0;
1.117 + }
1.118 + if (alpha==0xff)
1.119 + {
1.120 + return aPixel;
1.121 + }
1.122 + TUint16 norm = aNormTable[alpha];
1.123 + TUint32 norm_rb = (((aPixel & KRBMask) * norm) >> 8) & KRBMask;
1.124 + TUint32 norm_g = (((aPixel & KGMask ) * norm) >> 8) & KGMask;
1.125 + return ((aPixel & 0xff000000) | norm_rb | norm_g);
1.126 + }
1.127 +
1.128 +inline void Convert2PMA(TUint32& aInOutValue)
1.129 + {
1.130 + aInOutValue = NonPMA2PMAPixel(aInOutValue);
1.131 + }
1.132 +
1.133 +inline void Convert2NonPMA(TUint32& aInOutValue, const TUint16* aNormTable)
1.134 + {
1.135 + aInOutValue = PMA2NonPMAPixel(aInOutValue, aNormTable);
1.136 + }
1.137 \ No newline at end of file