1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/graphics/graphicsdeviceinterface/gdi/sgdi/RGB.CPP Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,529 @@
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 <graphics/lookuptable.h>
1.20 +#include <graphics/blendingalgorithms.h>
1.21 +#include <gdi.h>
1.22 +#include <palette.h>
1.23 +
1.24 +// Class TRgb Definition
1.25 +
1.26 +/** Sets the red component.
1.27 +
1.28 +@param aRed Red component (0 - 255). */
1.29 +EXPORT_C void TRgb::SetRed(TInt aRed)
1.30 + {
1.31 + iValue&=0xff00ffff;
1.32 + iValue|=(aRed&0xff)<<16;
1.33 + }
1.34 +
1.35 +/** Sets the green component.
1.36 +
1.37 +@param aGreen Green component (0 - 255). */
1.38 +EXPORT_C void TRgb::SetGreen(TInt aGreen)
1.39 + {
1.40 + iValue&=0xffff00ff;
1.41 + iValue|=(aGreen&0xff)<<8;
1.42 + }
1.43 +
1.44 +/** Sets the blue component.
1.45 +
1.46 +@param aBlue Blue component (0 - 255). */
1.47 +EXPORT_C void TRgb::SetBlue(TInt aBlue)
1.48 + {
1.49 + iValue&=0xffffff00;
1.50 + iValue|=(aBlue&0xff);
1.51 + }
1.52 +
1.53 +/** Gets TRgb from 2 level grayscale.
1.54 +
1.55 +The function takes a grayscale argument and return a TRgb whose red, green
1.56 +and blue values are set to an appropriate level.
1.57 +
1.58 +@param aGray2 Grayscale value to be converted.
1.59 +@return Equivalent 24 bit colour. Gray2 has only 2 levels (black and white),
1.60 +the function returns r=g=b=0 or r=g=b=255. */
1.61 +EXPORT_C TRgb TRgb::Gray2(TInt aGray2)
1.62 + {
1.63 + if(aGray2) return(TRgb(0xffffff, 0xff));
1.64 + return(TRgb(0, 0xff));
1.65 + }
1.66 +
1.67 +/** Gets TRgb from 4 level grayscale.
1.68 +
1.69 +The function takes a grayscale argument and return a TRgb whose red, green
1.70 +and blue values are set to an appropriate level.
1.71 +
1.72 +@param aGray4 Grayscale value to be converted.
1.73 +@return Equivalent 24 bit colour. Gray4 has 4 levels- the function returns
1.74 +r=g=b=85*c, where c=0,1,2, or 3. */
1.75 +EXPORT_C TRgb TRgb::Gray4(TInt aGray4)
1.76 + {
1.77 + aGray4&=3;
1.78 + aGray4|=aGray4<<2;
1.79 + aGray4|=aGray4<<4;
1.80 + return(TRgb(aGray4,aGray4,aGray4));
1.81 + }
1.82 +
1.83 +/** Gets TRgb from 16 level grayscale.
1.84 +
1.85 +The function takes a grayscale argument and return a TRgb whose red, green
1.86 +and blue values are set to an appropriate level.
1.87 +
1.88 +@param aGray16 Grayscale value to be converted.
1.89 +@return Equivalent 24 bit colour. Gray16 has 16 levels - the function returns
1.90 +r=g=b=17*c, where c=0, 1, ... 15. */
1.91 +EXPORT_C TRgb TRgb::Gray16(TInt aGray16)
1.92 + {
1.93 + aGray16&=0xf;
1.94 + aGray16|=aGray16<<4;
1.95 + return(TRgb(aGray16,aGray16,aGray16));
1.96 + }
1.97 +
1.98 +/** Gets TRgb from 256 level grayscale.
1.99 +
1.100 +The function takes a grayscale argument and return a TRgb whose red, green
1.101 +and blue values are set to an appropriate level.
1.102 +
1.103 +@param aGray256 Grayscale value to be converted.
1.104 +@return Equivalent 24 bit colour. Gray256 has 256 levels- the function
1.105 +returns r=g=b=c, where c=0, 1, ... 255. */
1.106 +EXPORT_C TRgb TRgb::Gray256(TInt aGray256)
1.107 + {
1.108 + aGray256&=0xff;
1.109 + return(TRgb(aGray256,aGray256,aGray256));
1.110 + }
1.111 +
1.112 +/** Gets TRgb from 4 bit colour index.
1.113 +
1.114 +The function takes a 4 bit index into a colour palette and returns a TRgb
1.115 +whose red, green and blue values are set to an appropriate level.
1.116 +
1.117 +@param aColor16 4 bit index into a colour palette
1.118 +@return Equivalent 24 bit colour. */
1.119 +EXPORT_C TRgb TRgb::Color16(TInt aColor16)
1.120 + {
1.121 + return(TRgb(DynamicPalette::Color16array()[aColor16&0xf]));
1.122 + }
1.123 +
1.124 +/** Gets TRgb from 8 bit colour index.
1.125 +
1.126 +The function takes an 8 bit index into a colour palette and returns a TRgb
1.127 +whose red, green and blue values are set to an appropriate level.
1.128 +
1.129 +@param aColor256 8 bit index into a colour palette.
1.130 +@return Equivalent 24 bit colour. */
1.131 +EXPORT_C TRgb TRgb::Color256(TInt aColor256)
1.132 + {
1.133 + return(TRgb(DynamicPalette::DefaultColor256Util()->iColorTable[aColor256&0xff]));
1.134 + }
1.135 +
1.136 +/** Gets TRgb from 4K colour index.
1.137 +
1.138 +The function takes a 12 bit index into a colour palette and returns a TRgb
1.139 +whose red, green and blue values are set to an appropriate level.
1.140 +
1.141 +@param aColor4K 12 bit index into a colour palette
1.142 +@return Equivalent 24 bit colour. */
1.143 +EXPORT_C TRgb TRgb::Color4K(TInt aColor4K)
1.144 + {
1.145 + return _Color4K(aColor4K);
1.146 + }
1.147 +
1.148 +/** Gets TRgb from 64K colour index.
1.149 +
1.150 +The function takes a 16 bit index into a colour palette and returns a TRgb
1.151 +whose red, green and blue values are set to an appropriate level.
1.152 +
1.153 +@param aColor64K 16 bit index into a colour palette
1.154 +@return Equivalent 24 bit colour. */
1.155 +EXPORT_C TRgb TRgb::Color64K(TInt aColor64K)
1.156 + {
1.157 + return _Color64K(aColor64K);
1.158 + }
1.159 +
1.160 +/** Gets TRgb from 16M colour index.
1.161 +
1.162 +The function takes a 24 bit index into a colour palette and returns the TRgb
1.163 +whose red, green and blue values represent it exactly.
1.164 +
1.165 +@param aColor16M 24 bit index into a colour palette
1.166 +@return The TRgb which represents the index exactly. */
1.167 +EXPORT_C TRgb TRgb::Color16M(TInt aColor16M)
1.168 + {
1.169 + return _Color16M(aColor16M);
1.170 + }
1.171 +
1.172 +/** Gets the index of the closest TRgb value to this,
1.173 +based on the matching display mode.
1.174 +
1.175 +@return The index (0 - 1) representing the nearest TRgb. */
1.176 +EXPORT_C TInt TRgb::Gray2() const
1.177 + {
1.178 + return _Gray2();
1.179 + }
1.180 +
1.181 +/**Gets the index of the closest TRgb value to this,
1.182 +based on the matching display mode.
1.183 +
1.184 +@return The index (0 - 3) representing the nearest TRgb. */
1.185 +EXPORT_C TInt TRgb::Gray4() const
1.186 + {
1.187 + return _Gray4();
1.188 + }
1.189 +
1.190 +/** Gets the index of the closest TRgb value to this,
1.191 +based on the matching display mode.
1.192 +
1.193 +@return The index (0 - 15) representing the nearest TRgb.*/
1.194 +EXPORT_C TInt TRgb::Gray16() const
1.195 + {
1.196 + return _Gray16();
1.197 + }
1.198 +
1.199 +/** Gets the index of the closest TRgb value to this,
1.200 +based on the matching display mode.
1.201 +
1.202 +@return The index (0 - 255) representing the nearest TRgb.*/
1.203 +EXPORT_C TInt TRgb::Gray256() const
1.204 + {
1.205 + return _Gray256();
1.206 + }
1.207 +
1.208 +/** Gets the index of the closest TRgb value to this,
1.209 +based on the matching display mode.
1.210 +
1.211 +@return The index (0 - 15) representing the nearest TRgb. */
1.212 +EXPORT_C TInt TRgb::Color16() const
1.213 + {
1.214 + TInt index = (iValue & 0x000000e0) << 1;
1.215 + index |= (iValue & 0x0000e000) >> 10;
1.216 + index |= (iValue & 0x00e00000) >> 21;
1.217 + return DynamicPalette::Color16inverse()[index];
1.218 + }
1.219 +
1.220 +/** Gets the index of the closest TRgb value to this,
1.221 +based on the matching display mode.
1.222 +
1.223 +@return The index (0 - 255) representing the nearest TRgb. */
1.224 +EXPORT_C TInt TRgb::Color256() const
1.225 + {
1.226 + TInt index = (iValue & 0x000000f0) << 4;
1.227 + index |= (iValue & 0x0000f000) >> 8;
1.228 + index |= (iValue & 0x00f00000) >> 20;
1.229 + return DynamicPalette::DefaultColor256Util()->iInverseColorTable[index];
1.230 + }
1.231 +
1.232 +/** Gets the index of the closest TRgb value to this,
1.233 +based on the matching display mode.
1.234 +
1.235 +@return The index (0 - 4095) representing the nearest TRgb. */
1.236 +EXPORT_C TInt TRgb::Color4K() const
1.237 + {
1.238 + return _Color4K();
1.239 + }
1.240 +
1.241 +/** Gets the index of the closest TRgb value to this,
1.242 +based on the matching display mode.
1.243 +
1.244 +@return The index (0 - 65535) representing the nearest TRgb.*/
1.245 +EXPORT_C TInt TRgb::Color64K() const
1.246 + {
1.247 + return _Color64K();
1.248 + }
1.249 +
1.250 +/** Gets the index of the closest TRgb value to this,
1.251 +based on the matching display mode.
1.252 +
1.253 +@return The index (0 - 16777215) representing the nearest TRgb.*/
1.254 +EXPORT_C TInt TRgb::Color16M() const
1.255 + {
1.256 + return _Color16M();
1.257 + }
1.258 +
1.259 +/** Gets the difference between two TRgbs.
1.260 +
1.261 +This difference is defined as the sum of the absolute values of the difference
1.262 +in the red, green and blue components.
1.263 +
1.264 +@param aColor The TRgb to be compared.
1.265 +@return The sum of the absolute value of the differences between the red, green
1.266 +and blue components. */
1.267 +EXPORT_C TInt TRgb::Difference(const TRgb& aColor) const
1.268 + {
1.269 + return(Abs((TInt)(aColor.Internal()&0xFF)-(TInt)(Internal()&0xFF))+
1.270 + (Abs((TInt)(aColor.Internal()&0xFF00)-(TInt)(Internal()&0xFF00))>>8)+
1.271 + (Abs((TInt)(aColor.Internal()&0xFF0000)-(TInt)(Internal()&0xFF0000))>>16));
1.272 + }
1.273 +
1.274 +/** Internalises a TRgb object from a stream.
1.275 +
1.276 +The presence of this function means that the standard templated stream operator>>()
1.277 +is available to internalise objects of this class.
1.278 +
1.279 +@param aStream Stream from which the object is internalised.
1.280 +@see operator>>() */
1.281 +EXPORT_C void TRgb::InternalizeL(RReadStream& aStream)
1.282 + {
1.283 + TInt red=aStream.ReadUint8L();
1.284 + TInt green=aStream.ReadUint8L();
1.285 + TInt blue=aStream.ReadUint8L();
1.286 + *this=TRgb(red,green,blue);
1.287 + }
1.288 +
1.289 +/** Externalises a TRgb object to a stream.
1.290 +
1.291 +The presence of this function means that the standard templated stream operator<<()
1.292 +is available to externalise objects of this class.
1.293 +
1.294 +@param aStream Stream to which the object is externalised. */
1.295 +EXPORT_C void TRgb::ExternalizeL(RWriteStream& aStream) const
1.296 + {
1.297 + aStream.WriteUint8L(Red());
1.298 + aStream.WriteUint8L(Green());
1.299 + aStream.WriteUint8L(Blue());
1.300 + }
1.301 +
1.302 +/** Gets TRgb from 16MU colour index.
1.303 +The function takes a 24 bit colour value with eight bits for each
1.304 +component, blue in the low byte, and returns the TRgb
1.305 +whose red, green and blue values represent it exactly.
1.306 +@param a0RGB The color - 0, R, G, B bytes. / BGR0 - little endian format /
1.307 +@return The TRgb which represents the index exactly. */
1.308 +EXPORT_C TRgb TRgb::Color16MU(TInt a0RGB)
1.309 + {
1.310 + // R G B
1.311 + return _Color16MU(a0RGB);
1.312 + }
1.313 +
1.314 +/** Gets the index of the closest TRgb value to this, based on the matching display mode.
1.315 +@return The index (0 - 16777215) representing the nearest TRgb. */
1.316 +EXPORT_C TInt TRgb::Color16MU() const
1.317 + {
1.318 + // R G B
1.319 + return _Color16MU();
1.320 + }
1.321 +
1.322 +
1.323 +/** Sets the alpha component.
1.324 +
1.325 +@param aAlpha Alpha component (0 - 255). */
1.326 +EXPORT_C void TRgb::SetAlpha(TInt aAlpha)
1.327 + {
1.328 + iValue&=0x00ffffff;
1.329 + iValue|=(aAlpha << 24);
1.330 + }
1.331 +
1.332 +
1.333 +/** Gets TRgb from 16MA colour index.
1.334 +The function takes a 32 bit colour value with eight bits for each
1.335 +component, blue in the low byte, and returns the TRgb
1.336 +whose red, green, blue and alpha values represent it exactly.
1.337 +@param aARGB The color - A, R, G, B bytes. / BGRA - little endian format /
1.338 +@return The TRgb which represents the index exactly. */
1.339 +EXPORT_C TRgb TRgb::Color16MA(TUint aARGB)
1.340 + {
1.341 + return _Color16MA(aARGB);
1.342 + }
1.343 +
1.344 +/** Gets the index of the closest TRgb value to this, based on the matching display mode.
1.345 +@return The index (0 - 16777215) representing the nearest TRgb. */
1.346 +EXPORT_C TUint TRgb::Color16MA() const
1.347 + {
1.348 + return _Color16MA();
1.349 + }
1.350 +
1.351 +/** Gets TRgb from 16MAP colour index.
1.352 +The function takes a 32 bit colour value with eight bits for each
1.353 +component, blue in the low byte, and returns the TRgb
1.354 +whose red, green, and blue vales are divided by the alpha value.
1.355 +@param aARGB The pre-multiplied (EColor16MAP) color value.
1.356 +@return The TRgb which represents the color channel and alpha information. */
1.357 +/* static */
1.358 +EXPORT_C TRgb TRgb::Color16MAP(TUint aARGB)
1.359 + {
1.360 + const TUint16* ArTable = PtrTo16BitNormalisationTable();
1.361 + TRgb retColor;
1.362 + retColor.SetInternal(PMA2NonPMAPixel(aARGB, ArTable));
1.363 + return retColor;
1.364 + }
1.365 +
1.366 +/** Gets the index of the closest TRgb value to this, based on the matching display mode.
1.367 +pre-multiplies the alpha channels with the color channel.
1.368 +@return The pre-multiplied color value */
1.369 +EXPORT_C TUint TRgb::Color16MAP() const
1.370 + {
1.371 + return NonPMA2PMAPixel(iValue);
1.372 + }
1.373 +
1.374 +
1.375 +/** Tests whether the display mode specified is colour or greyscale.
1.376 +
1.377 +@param aDispMode The display mode.
1.378 +@return ETrue if colour; EFalse if greyscale or monochrome. */
1.379 +EXPORT_C TBool TDisplayModeUtils::IsDisplayModeColor(TDisplayMode aDispMode)
1.380 + {
1.381 + return (aDispMode >= EColor16);
1.382 + }
1.383 +
1.384 +/** Tests whether the display mode specified is one of the valid values.
1.385 +
1.386 +@param aDispMode The display mode to be tested.
1.387 +@return ETrue if aDispMode is valid; EFalse if not valid. */
1.388 +EXPORT_C TBool TDisplayModeUtils::IsDisplayModeValid(TDisplayMode aDispMode)
1.389 + {
1.390 + return aDispMode >= ENone && aDispMode < EColorLast;
1.391 + }
1.392 +
1.393 +/** Gets the number of colours or shades of grey supported by the specified
1.394 +display mode.
1.395 +
1.396 +For instance, a display mode of EGray4 returns 4, EColor4K returns 4096.
1.397 +
1.398 +@param aDispMode The display mode.
1.399 +@return The number of colours/grey shades supported by the display mode. */
1.400 +EXPORT_C TInt TDisplayModeUtils::NumDisplayModeColors(TDisplayMode aDispMode)
1.401 + {
1.402 + switch (aDispMode)
1.403 + {
1.404 + case EGray2:
1.405 + return 2;
1.406 + case EGray4:
1.407 + return 4;
1.408 + case EGray16:
1.409 + case EColor16:
1.410 + return 16;
1.411 + case EGray256:
1.412 + case EColor256:
1.413 + return 256;
1.414 + case EColor4K:
1.415 + return 4096;
1.416 + case EColor64K:
1.417 + return 65536;
1.418 + case EColor16M:
1.419 + case EColor16MU:
1.420 + case EColor16MA:
1.421 + case EColor16MAP:
1.422 + return 16777216;
1.423 + default:
1.424 + return 0;
1.425 + };
1.426 + }
1.427 +
1.428 +/** Gets the number of bits required by each pixel when displayed in the
1.429 +specified display mode.
1.430 +
1.431 +@param aDispMode The display mode.
1.432 +@return The number of bits required by each pixel. */
1.433 +EXPORT_C TInt TDisplayModeUtils::NumDisplayModeBitsPerPixel(TDisplayMode aDispMode)
1.434 + {
1.435 + switch (aDispMode)
1.436 + {
1.437 + case EGray2:
1.438 + return 1;
1.439 + case EGray4:
1.440 + return 2;
1.441 + case EGray16:
1.442 + case EColor16:
1.443 + return 4;
1.444 + case EGray256:
1.445 + case EColor256:
1.446 + return 8;
1.447 + case EColor4K:
1.448 + return 12;
1.449 + case EColor64K:
1.450 + return 16;
1.451 + case EColor16M:
1.452 + return 24;
1.453 + case EColor16MU:
1.454 + case EColor16MA:
1.455 + case EColor16MAP:
1.456 + return 32;
1.457 + default:
1.458 + return 0;
1.459 + };
1.460 + }
1.461 +
1.462 +//
1.463 +// TColor256Util
1.464 +//
1.465 +/** Initialises the two lookup tables using the specified palette.
1.466 +
1.467 +@param aPalette The palette of colours used to initialise the colour lookup
1.468 +tables. */
1.469 +EXPORT_C void TColor256Util::Construct(const CPalette& aPalette)
1.470 + {
1.471 + TInt n = aPalette.Entries();
1.472 + if(n>256)
1.473 + n = 256;
1.474 +
1.475 + TInt i;
1.476 + for(i=0; i<n; i++)
1.477 + iColorTable[i] = aPalette.GetEntry(i).Value();
1.478 + for(; i<256; i++)
1.479 + iColorTable[i] = 0;
1.480 +
1.481 + i = 0;
1.482 + for(TInt b=0; b<0x100; b+=0x11)
1.483 + for(TInt g=0; g<0x100; g+=0x11)
1.484 + for(TInt r=0; r<0x100; r+=0x11)
1.485 + iInverseColorTable[i++] = (TUint8)aPalette.NearestIndex(TRgb(r,g,b));
1.486 + }
1.487 +
1.488 +/** Gets the entry from the inverse colour lookup table for the colour that most
1.489 +closely matches the specified TRgb value.
1.490 +
1.491 +Entries in the inverse colour lookup table are indices into the palette that
1.492 +the object was created with. Essentially, this function matches aRgb to the
1.493 +index of the nearest colour in the palette.
1.494 +
1.495 +@param aRgb The conversion colour.
1.496 +@return The index of the nearest colour to aRgb in the palette. */
1.497 +EXPORT_C TInt TColor256Util::Color256(TRgb aRgb) const
1.498 + {
1.499 + TInt index = (aRgb.Value() & 0x000000f0) >> 4;
1.500 + index |= (aRgb.Value() & 0x0000f000) >> 8;
1.501 + index |= (aRgb.Value() & 0x00f00000) >> 12;
1.502 + return iInverseColorTable[index];
1.503 + }
1.504 +
1.505 +/** Gets the entries from the inverse colour lookup table for the colours that
1.506 +most closely match the specified TRgb values.
1.507 +
1.508 +@param aDestination On return, a pointer to a buffer containing the entries
1.509 +from the inverse colour lookup table.
1.510 +@param aSource Pointer to the first TRgb value to match.
1.511 +@param aNumPixels The number of TRgb values to match. */
1.512 +EXPORT_C void TColor256Util::Color256(TUint8* aDestination,const TRgb* aSource,TInt aNumPixels) const
1.513 + {
1.514 + TUint8* limit = aDestination+aNumPixels;
1.515 + while(aDestination<limit)
1.516 + {
1.517 + TInt value = (*aSource++).Value();
1.518 + TInt index = (value & 0x000000f0) >> 4;
1.519 + index |= (value & 0x0000f000) >> 8;
1.520 + index |= (value & 0x00f00000) >> 12;
1.521 + *(aDestination++) = iInverseColorTable[index];
1.522 + }
1.523 + }
1.524 +
1.525 +/** Returns a pointer to the system default 256 colour palette.
1.526 +
1.527 +@return Pointer to the system default 256 colour palette. */
1.528 +EXPORT_C const TColor256Util* TColor256Util::Default()
1.529 + {
1.530 + return DynamicPalette::DefaultColor256Util();
1.531 + }
1.532 +