graphicsdeviceinterface/gdi/sgdi/PALETTE.CPP
changeset 0 5d03bc08d59c
equal deleted inserted replaced
-1:000000000000 0:5d03bc08d59c
       
     1 // Copyright (c) 1998-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 #include <gdi.h>
       
    17 #include "GDIPANIC.h"
       
    18 
       
    19 //
       
    20 // CPalette
       
    21 //
       
    22 
       
    23 _LIT(KGdiCPalettePanicCategory,"CPalette");
       
    24 
       
    25 EXPORT_C CPalette::CPalette():
       
    26 	CBase(),
       
    27 	iArray(NULL),
       
    28 	iNumEntries(0)
       
    29 	{}
       
    30 
       
    31 
       
    32 EXPORT_C CPalette::~CPalette()
       
    33 /** Destructor. */
       
    34 	{
       
    35 	delete [] iArray;
       
    36 	}
       
    37 
       
    38 
       
    39 EXPORT_C void CPalette::Clear()
       
    40 /** Clears all the entries in the palette to TRgb(0). */
       
    41 	{
       
    42 	TRgb blank(0);
       
    43 	for(TInt count=0;count<iNumEntries;count++)
       
    44 		iArray[count]=blank;
       
    45 	}
       
    46 
       
    47 
       
    48 EXPORT_C CPalette* CPalette::NewL(TInt aNumberOfEntries)
       
    49 /** Creates a new palette with the specified number of entries. 
       
    50 
       
    51 @param aNumberOfEntries The number of entries in the palette being created.
       
    52 @return The newly created palette. */
       
    53 	{
       
    54 	CPalette* thisptr=new(ELeave) CPalette;
       
    55 	CleanupStack::PushL(thisptr);
       
    56 	thisptr->ConstructL(aNumberOfEntries);
       
    57 	CleanupStack::Pop();
       
    58 	return(thisptr);
       
    59 	}
       
    60 
       
    61 
       
    62 EXPORT_C CPalette* CPalette::NewDefaultL(TDisplayMode aDispMode)
       
    63 /** Creates a new default palette for the specified display mode. The default palette 
       
    64 for a particular display mode has one entry for each possible colour in that 
       
    65 display mode (2 entries for EGray2, 16 entries for EColor16 etc.); the colour 
       
    66 of each index p in the default palette is set to its default value according 
       
    67 to its display mode (e.g. if the mode is EColor16 then palette[p]==TRgb::Color16(p); 
       
    68 if the mode is EGray4 then palette[p]==TRgb::_Gray4(p)). 
       
    69 
       
    70 @param aDispMode The display mode for which the palette is to be created. 
       
    71 @return The newly created palette */
       
    72 	{
       
    73 	TInt numentries=0;
       
    74 	switch(aDispMode)
       
    75 		{
       
    76 	case EGray2:
       
    77 		numentries=2;
       
    78 		break;
       
    79 	case EGray4:
       
    80 		numentries=4;
       
    81 		break;
       
    82 	case EGray16:
       
    83 	case EColor16:
       
    84 		numentries=16;
       
    85 		break;
       
    86 	case EGray256:
       
    87 	case EColor256:
       
    88 		numentries=256;
       
    89 		break;
       
    90 	default:
       
    91 		User::Leave(KErrNotSupported);
       
    92 		};
       
    93 	CPalette* thisptr=new(ELeave) CPalette;
       
    94 	CleanupStack::PushL(thisptr);
       
    95 	thisptr->ConstructL(numentries);
       
    96 	TInt count=0;
       
    97 	switch(aDispMode)
       
    98 		{
       
    99 	case EGray2:
       
   100 		thisptr->iArray[0]=TRgb::_Gray2(0);
       
   101 		thisptr->iArray[1]=TRgb::_Gray2(1);
       
   102 		break;
       
   103 	case EGray4:
       
   104 		for(;count<numentries;count++)
       
   105 			thisptr->iArray[count]=TRgb::_Gray4(count);
       
   106 		break;
       
   107 	case EGray16:
       
   108 		for(;count<numentries;count++)
       
   109 			thisptr->iArray[count]=TRgb::_Gray16(count);
       
   110 		break;
       
   111 	case EColor16:
       
   112 		for(;count<numentries;count++)
       
   113 			thisptr->iArray[count]=TRgb::Color16(count);
       
   114 		break;
       
   115 	case EGray256:
       
   116 		for(;count<numentries;count++)
       
   117 			thisptr->iArray[count]=TRgb::_Gray256(count);
       
   118 		break;
       
   119 	case EColor256:
       
   120 		for(;count<numentries;count++)
       
   121 			thisptr->iArray[count]=TRgb::Color256(count);
       
   122 		break;
       
   123 	default:
       
   124 		User::Leave(KErrNotSupported);
       
   125 		}
       
   126 	CleanupStack::Pop();
       
   127 	return(thisptr);
       
   128 	}
       
   129 
       
   130 void CPalette::ConstructL(TInt aNumberOfEntries)
       
   131 	{
       
   132 	if(aNumberOfEntries<=0) User::Leave(KErrArgument);
       
   133 	iArray=new(ELeave) TRgb[aNumberOfEntries];
       
   134 	iNumEntries=aNumberOfEntries;
       
   135 	}
       
   136 
       
   137 
       
   138 EXPORT_C TRgb CPalette::GetEntry(TInt aIndex) const
       
   139 /** Gets the RGB value of the palette entry at aPaletteIndex.
       
   140 	
       
   141 @param aPaletteIndex The index of the palette entry to get. 
       
   142 @return The RGB value of the palette entry */
       
   143 	{
       
   144 	GDI_ASSERT_ALWAYS_GENERAL(aIndex<iNumEntries,User::Panic(KGdiCPalettePanicCategory,KErrTooBig));
       
   145 	if(aIndex<iNumEntries)
       
   146 		return(iArray[aIndex]);
       
   147 	TRgb defaultcol(0,0,0);
       
   148 	return(defaultcol);
       
   149 	}
       
   150 
       
   151 
       
   152 EXPORT_C TRgb CPalette::NearestEntry(const TRgb& aColor) const
       
   153 /** Gets the colour in the palette which is closest to the specified colour.
       
   154 
       
   155 @param aColor The colour to find.
       
   156 @return The colour in the palette which is closest to the specified colour. */
       
   157 	{
       
   158 	return(iArray[NearestIndex(aColor)]);
       
   159 	}
       
   160 
       
   161 
       
   162 EXPORT_C TInt CPalette::NearestIndex(const TRgb& aColor) const
       
   163 /** Gets the index of the colour in the palette which is closest to the specified 
       
   164 colour.
       
   165 
       
   166 @param aColor The colour to find.
       
   167 @return The index of the colour in the palette which is closest to the specified 
       
   168 colour. */
       
   169 	{
       
   170 	TRgb* entry = iArray;
       
   171 	TRgb* entryLimit = entry+iNumEntries;
       
   172 	TRgb* lowest = entry;
       
   173 	TInt mindif = KMaxTInt;
       
   174 
       
   175 	while(entry<entryLimit)
       
   176 		{
       
   177 		TInt value = entry->Internal();
       
   178 
       
   179 		TInt difference = Abs((TInt)(aColor.Internal()&0xFF)-(TInt)(value&0xFF))+
       
   180 						  (Abs((TInt)(aColor.Internal()&0xFF00)-(TInt)(value&0xFF00))>>8)+
       
   181 						  (Abs((TInt)(aColor.Internal()&0xFF0000)-(TInt)(value&0xFF0000))>>16);
       
   182 
       
   183 		if(difference<mindif)
       
   184 			{
       
   185 			lowest=entry;
       
   186 			mindif=difference;
       
   187 			if(difference==0)
       
   188 				break;
       
   189 			}
       
   190 		entry++;
       
   191 		}
       
   192 	return(lowest-iArray);
       
   193 	}
       
   194 
       
   195 
       
   196 EXPORT_C void CPalette::SetEntry(TInt aIndex,const TRgb& aColor)
       
   197 /** Sets the palette entry at aPaletteIndex to the RGB value aPaletteEntry.
       
   198 	
       
   199 @param aPaletteIndex The index of the palette entry to be set.
       
   200 @param aPaletteEntry The RGB value to set that entry to. */
       
   201 	{
       
   202 	GDI_ASSERT_ALWAYS_GENERAL(aIndex<iNumEntries,User::Panic(KGdiCPalettePanicCategory,KErrTooBig));
       
   203 	iArray[aIndex]=aColor;
       
   204 	}
       
   205 
       
   206 
       
   207 EXPORT_C void CPalette::GetDataPtr(TInt aFirstColor,TInt aNumColors,TPtr8& aPtr)
       
   208 /** Returns a descriptor over the palette entries for the specifed colors. It is 
       
   209 designed so that the descriptor can be passed to another thread and that thread 
       
   210 copy the relevant entries.
       
   211 
       
   212 @param aFirstColor The first colour.
       
   213 @param aNumColors Number of colours.
       
   214 @param aPtr Descriptor. */
       
   215 	{
       
   216 	TInt size=sizeof(TRgb)*aNumColors;
       
   217 	aPtr.Set(REINTERPRET_CAST(TUint8*,&iArray[aFirstColor]),size,size);
       
   218 	}