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