os/graphics/graphicsdeviceinterface/gdi/sgdi/PALETTE.CPP
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/graphics/graphicsdeviceinterface/gdi/sgdi/PALETTE.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,218 @@
     1.4 +// Copyright (c) 1998-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 +#include <gdi.h>
    1.20 +#include "GDIPANIC.h"
    1.21 +
    1.22 +//
    1.23 +// CPalette
    1.24 +//
    1.25 +
    1.26 +_LIT(KGdiCPalettePanicCategory,"CPalette");
    1.27 +
    1.28 +EXPORT_C CPalette::CPalette():
    1.29 +	CBase(),
    1.30 +	iArray(NULL),
    1.31 +	iNumEntries(0)
    1.32 +	{}
    1.33 +
    1.34 +
    1.35 +EXPORT_C CPalette::~CPalette()
    1.36 +/** Destructor. */
    1.37 +	{
    1.38 +	delete [] iArray;
    1.39 +	}
    1.40 +
    1.41 +
    1.42 +EXPORT_C void CPalette::Clear()
    1.43 +/** Clears all the entries in the palette to TRgb(0). */
    1.44 +	{
    1.45 +	TRgb blank(0);
    1.46 +	for(TInt count=0;count<iNumEntries;count++)
    1.47 +		iArray[count]=blank;
    1.48 +	}
    1.49 +
    1.50 +
    1.51 +EXPORT_C CPalette* CPalette::NewL(TInt aNumberOfEntries)
    1.52 +/** Creates a new palette with the specified number of entries. 
    1.53 +
    1.54 +@param aNumberOfEntries The number of entries in the palette being created.
    1.55 +@return The newly created palette. */
    1.56 +	{
    1.57 +	CPalette* thisptr=new(ELeave) CPalette;
    1.58 +	CleanupStack::PushL(thisptr);
    1.59 +	thisptr->ConstructL(aNumberOfEntries);
    1.60 +	CleanupStack::Pop();
    1.61 +	return(thisptr);
    1.62 +	}
    1.63 +
    1.64 +
    1.65 +EXPORT_C CPalette* CPalette::NewDefaultL(TDisplayMode aDispMode)
    1.66 +/** Creates a new default palette for the specified display mode. The default palette 
    1.67 +for a particular display mode has one entry for each possible colour in that 
    1.68 +display mode (2 entries for EGray2, 16 entries for EColor16 etc.); the colour 
    1.69 +of each index p in the default palette is set to its default value according 
    1.70 +to its display mode (e.g. if the mode is EColor16 then palette[p]==TRgb::Color16(p); 
    1.71 +if the mode is EGray4 then palette[p]==TRgb::_Gray4(p)). 
    1.72 +
    1.73 +@param aDispMode The display mode for which the palette is to be created. 
    1.74 +@return The newly created palette */
    1.75 +	{
    1.76 +	TInt numentries=0;
    1.77 +	switch(aDispMode)
    1.78 +		{
    1.79 +	case EGray2:
    1.80 +		numentries=2;
    1.81 +		break;
    1.82 +	case EGray4:
    1.83 +		numentries=4;
    1.84 +		break;
    1.85 +	case EGray16:
    1.86 +	case EColor16:
    1.87 +		numentries=16;
    1.88 +		break;
    1.89 +	case EGray256:
    1.90 +	case EColor256:
    1.91 +		numentries=256;
    1.92 +		break;
    1.93 +	default:
    1.94 +		User::Leave(KErrNotSupported);
    1.95 +		};
    1.96 +	CPalette* thisptr=new(ELeave) CPalette;
    1.97 +	CleanupStack::PushL(thisptr);
    1.98 +	thisptr->ConstructL(numentries);
    1.99 +	TInt count=0;
   1.100 +	switch(aDispMode)
   1.101 +		{
   1.102 +	case EGray2:
   1.103 +		thisptr->iArray[0]=TRgb::_Gray2(0);
   1.104 +		thisptr->iArray[1]=TRgb::_Gray2(1);
   1.105 +		break;
   1.106 +	case EGray4:
   1.107 +		for(;count<numentries;count++)
   1.108 +			thisptr->iArray[count]=TRgb::_Gray4(count);
   1.109 +		break;
   1.110 +	case EGray16:
   1.111 +		for(;count<numentries;count++)
   1.112 +			thisptr->iArray[count]=TRgb::_Gray16(count);
   1.113 +		break;
   1.114 +	case EColor16:
   1.115 +		for(;count<numentries;count++)
   1.116 +			thisptr->iArray[count]=TRgb::Color16(count);
   1.117 +		break;
   1.118 +	case EGray256:
   1.119 +		for(;count<numentries;count++)
   1.120 +			thisptr->iArray[count]=TRgb::_Gray256(count);
   1.121 +		break;
   1.122 +	case EColor256:
   1.123 +		for(;count<numentries;count++)
   1.124 +			thisptr->iArray[count]=TRgb::Color256(count);
   1.125 +		break;
   1.126 +	default:
   1.127 +		User::Leave(KErrNotSupported);
   1.128 +		}
   1.129 +	CleanupStack::Pop();
   1.130 +	return(thisptr);
   1.131 +	}
   1.132 +
   1.133 +void CPalette::ConstructL(TInt aNumberOfEntries)
   1.134 +	{
   1.135 +	if(aNumberOfEntries<=0) User::Leave(KErrArgument);
   1.136 +	iArray=new(ELeave) TRgb[aNumberOfEntries];
   1.137 +	iNumEntries=aNumberOfEntries;
   1.138 +	}
   1.139 +
   1.140 +
   1.141 +EXPORT_C TRgb CPalette::GetEntry(TInt aIndex) const
   1.142 +/** Gets the RGB value of the palette entry at aPaletteIndex.
   1.143 +	
   1.144 +@param aPaletteIndex The index of the palette entry to get. 
   1.145 +@return The RGB value of the palette entry */
   1.146 +	{
   1.147 +	GDI_ASSERT_ALWAYS_GENERAL(aIndex<iNumEntries,User::Panic(KGdiCPalettePanicCategory,KErrTooBig));
   1.148 +	if(aIndex<iNumEntries)
   1.149 +		return(iArray[aIndex]);
   1.150 +	TRgb defaultcol(0,0,0);
   1.151 +	return(defaultcol);
   1.152 +	}
   1.153 +
   1.154 +
   1.155 +EXPORT_C TRgb CPalette::NearestEntry(const TRgb& aColor) const
   1.156 +/** Gets the colour in the palette which is closest to the specified colour.
   1.157 +
   1.158 +@param aColor The colour to find.
   1.159 +@return The colour in the palette which is closest to the specified colour. */
   1.160 +	{
   1.161 +	return(iArray[NearestIndex(aColor)]);
   1.162 +	}
   1.163 +
   1.164 +
   1.165 +EXPORT_C TInt CPalette::NearestIndex(const TRgb& aColor) const
   1.166 +/** Gets the index of the colour in the palette which is closest to the specified 
   1.167 +colour.
   1.168 +
   1.169 +@param aColor The colour to find.
   1.170 +@return The index of the colour in the palette which is closest to the specified 
   1.171 +colour. */
   1.172 +	{
   1.173 +	TRgb* entry = iArray;
   1.174 +	TRgb* entryLimit = entry+iNumEntries;
   1.175 +	TRgb* lowest = entry;
   1.176 +	TInt mindif = KMaxTInt;
   1.177 +
   1.178 +	while(entry<entryLimit)
   1.179 +		{
   1.180 +		TInt value = entry->Internal();
   1.181 +
   1.182 +		TInt difference = Abs((TInt)(aColor.Internal()&0xFF)-(TInt)(value&0xFF))+
   1.183 +						  (Abs((TInt)(aColor.Internal()&0xFF00)-(TInt)(value&0xFF00))>>8)+
   1.184 +						  (Abs((TInt)(aColor.Internal()&0xFF0000)-(TInt)(value&0xFF0000))>>16);
   1.185 +
   1.186 +		if(difference<mindif)
   1.187 +			{
   1.188 +			lowest=entry;
   1.189 +			mindif=difference;
   1.190 +			if(difference==0)
   1.191 +				break;
   1.192 +			}
   1.193 +		entry++;
   1.194 +		}
   1.195 +	return(lowest-iArray);
   1.196 +	}
   1.197 +
   1.198 +
   1.199 +EXPORT_C void CPalette::SetEntry(TInt aIndex,const TRgb& aColor)
   1.200 +/** Sets the palette entry at aPaletteIndex to the RGB value aPaletteEntry.
   1.201 +	
   1.202 +@param aPaletteIndex The index of the palette entry to be set.
   1.203 +@param aPaletteEntry The RGB value to set that entry to. */
   1.204 +	{
   1.205 +	GDI_ASSERT_ALWAYS_GENERAL(aIndex<iNumEntries,User::Panic(KGdiCPalettePanicCategory,KErrTooBig));
   1.206 +	iArray[aIndex]=aColor;
   1.207 +	}
   1.208 +
   1.209 +
   1.210 +EXPORT_C void CPalette::GetDataPtr(TInt aFirstColor,TInt aNumColors,TPtr8& aPtr)
   1.211 +/** Returns a descriptor over the palette entries for the specifed colors. It is 
   1.212 +designed so that the descriptor can be passed to another thread and that thread 
   1.213 +copy the relevant entries.
   1.214 +
   1.215 +@param aFirstColor The first colour.
   1.216 +@param aNumColors Number of colours.
   1.217 +@param aPtr Descriptor. */
   1.218 +	{
   1.219 +	TInt size=sizeof(TRgb)*aNumColors;
   1.220 +	aPtr.Set(REINTERPRET_CAST(TUint8*,&iArray[aFirstColor]),size,size);
   1.221 +	}