os/graphics/graphicsdeviceinterface/gdi/inc/blendingalgorithms.inl
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2007-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 const TUint32 KRBMask = 0x00ff00ff;
    17 const TUint32 KAGMask = 0xff00ff00;
    18 const TUint32 KGMask  = 0x0000ff00;
    19 
    20 //Algoriths for Premultiplied alpha screenmode/pixel format.
    21 
    22 inline TUint32 PMAPixelBlend(TUint32 aDestPixel, TUint32 aSrcPixel, TUint8 aMask)
    23 	{
    24 	if(aMask)
    25 		{
    26 		if(aMask == 0xff) // opaque, so unchanged
    27 			{
    28 			return aSrcPixel;
    29 			}
    30 		else
    31 			{
    32 			return PMABlend_noChecks(aDestPixel, aSrcPixel, aMask);
    33 			}
    34 		}
    35 	 else // completely transparent
    36 		{
    37 		return aDestPixel;
    38  		}
    39 	}
    40 
    41 inline TUint32 PMAPixelBlend(TUint32 aDestPixel, TUint32 aSrcPixel)
    42 	{
    43 	TUint8 mask = (TUint8)(aSrcPixel >> 24);
    44 	return PMAPixelBlend(aDestPixel, aSrcPixel, mask);
    45 	}
    46 
    47 inline TUint32 PMABlend_noChecks(TUint32 aDestPixel, TUint32 aSrcPixel, TUint8 aMaskingFactor)
    48 	{
    49 	TUint32 src_c = aSrcPixel & KRBMask;
    50 	TUint32 dst_c = aDestPixel  & KRBMask;
    51 	const TUint32 mask = 0x0100 - aMaskingFactor;
    52 	dst_c = (src_c +  ((mask * dst_c)>>8)) & KRBMask;
    53 	src_c = (aSrcPixel & KAGMask)>>8;
    54 	TUint32 dst_ag = (aDestPixel & KAGMask)>>8;
    55 	dst_c |= ((src_c +  ((mask * dst_ag)>>8)) & KRBMask)<<8;
    56 	return dst_c;
    57 	}
    58 
    59 inline void PMABlend_noChecksInplace(TUint32& aDest_io, const TUint32& aSrcPixel, TUint8 aMaskingFactor)
    60 	{
    61 	TUint32 src_c = aSrcPixel & KRBMask;
    62 	TUint32 dst_ag = (aDest_io & KAGMask) >> 8;
    63 	aDest_io = aDest_io  & KRBMask;
    64 	const TUint32 mask = 0x0100 - aMaskingFactor;
    65 	aDest_io = (src_c +  ((mask * aDest_io) >> 8)) & KRBMask;
    66 	src_c = (aSrcPixel & KAGMask) >> 8;
    67 	aDest_io |= ((src_c +  ((mask * dst_ag) >> 8)) & KRBMask)<<8;
    68 	}
    69 
    70 inline void PMAInplaceBlend(TUint32& aDest_io, TUint32& aSrc_in)
    71 	{
    72 	TUint8 mask = (TUint8)(aSrc_in >> 24);
    73 	if(mask)
    74 		{
    75 		if(mask == 0xff) // opaque, so dst = src.
    76 			{
    77 			aDest_io = aSrc_in;
    78 			}
    79 		else
    80 			{
    81 			PMABlend_noChecksInplace(aDest_io, aSrc_in, mask);
    82 			}
    83 		}
    84 	//else src completely transparent, so dst unchanged.
    85 	}
    86 
    87 inline TUint32 NonPMA2PMAPixel(TUint32 aPixel)
    88 	{
    89 	TUint8 tA = (TUint8)(aPixel >> 24);
    90 	if (tA==0)
    91 		{ 
    92 		return 0;
    93 		}
    94 	if (tA==0xff) 
    95 		{
    96 		return aPixel;
    97 		}
    98 
    99 	// Use a bias value of 128 rather than 255, but also add 1/256 of the numerator 
   100 	// before dividing the sum by 256.
   101 
   102 	TUint32 tap1=tA+1;
   103 	TUint32 scaledRB = (aPixel & KRBMask) * tap1;
   104 	TUint32 scaledG = (aPixel & KGMask ) * tap1;
   105 	return (aPixel & 0xff000000) | ((scaledRB>>8) & KRBMask) | ((scaledG>>8)& KGMask);
   106 	}
   107 
   108 inline TUint32 PMA2NonPMAPixel(TUint32 aPixel, const TUint16* aNormTable)
   109 	{
   110 	TUint8 alpha = (TUint8)(aPixel >> 24);
   111 	if (alpha==0)
   112 		{ 
   113 		return 0;
   114 		}
   115 	if (alpha==0xff) 
   116 		{
   117 		return aPixel;
   118 		}
   119 	TUint16 norm = aNormTable[alpha];
   120 	TUint32 norm_rb = (((aPixel & KRBMask) * norm) >> 8) & KRBMask;
   121 	TUint32 norm_g =  (((aPixel & KGMask ) * norm) >> 8) & KGMask;
   122 	return ((aPixel & 0xff000000) | norm_rb | norm_g);
   123 	}
   124 
   125 inline void Convert2PMA(TUint32& aInOutValue)
   126 	{
   127 	aInOutValue = NonPMA2PMAPixel(aInOutValue);
   128 	}
   129 
   130 inline void Convert2NonPMA(TUint32& aInOutValue, const TUint16* aNormTable)
   131 	{
   132 	aInOutValue = PMA2NonPMAPixel(aInOutValue, aNormTable);
   133 	}