os/graphics/graphicsdeviceinterface/gdi/sgdi/RGB.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.
     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 <graphics/lookuptable.h>
    17 #include <graphics/blendingalgorithms.h>
    18 #include <gdi.h>
    19 #include <palette.h>
    20 
    21 // Class TRgb Definition
    22 
    23 /** Sets the red component.
    24 
    25 @param aRed Red component (0 - 255). */
    26 EXPORT_C void TRgb::SetRed(TInt aRed)
    27 	{
    28 	iValue&=0xff00ffff;
    29 	iValue|=(aRed&0xff)<<16;
    30 	}
    31 
    32 /** Sets the green component.
    33 
    34 @param aGreen Green component (0 - 255). */
    35 EXPORT_C void TRgb::SetGreen(TInt aGreen)
    36 	{
    37 	iValue&=0xffff00ff;
    38 	iValue|=(aGreen&0xff)<<8;
    39 	}
    40 
    41 /** Sets the blue component.
    42 
    43 @param aBlue Blue component (0 - 255). */
    44 EXPORT_C void TRgb::SetBlue(TInt aBlue)
    45 	{
    46 	iValue&=0xffffff00;
    47 	iValue|=(aBlue&0xff);
    48 	}
    49 
    50 /** Gets TRgb from 2 level grayscale.
    51 
    52 The function takes a grayscale argument and return a TRgb whose red, green
    53 and blue values are set to an appropriate level.
    54 
    55 @param aGray2 Grayscale value to be converted.
    56 @return Equivalent 24 bit colour. Gray2 has only 2 levels (black and white),
    57 the function returns r=g=b=0 or r=g=b=255. */
    58 EXPORT_C TRgb TRgb::Gray2(TInt aGray2)
    59 	{
    60     if(aGray2) return(TRgb(0xffffff, 0xff));
    61     return(TRgb(0, 0xff));
    62 	}
    63 
    64 /** Gets TRgb from 4 level grayscale.
    65 
    66 The function takes a grayscale argument and return a TRgb whose red, green
    67 and blue values are set to an appropriate level.
    68 
    69 @param aGray4 Grayscale value to be converted.
    70 @return Equivalent 24 bit colour. Gray4 has 4 levels-  the function returns
    71 r=g=b=85*c, where c=0,1,2, or 3. */
    72 EXPORT_C TRgb TRgb::Gray4(TInt aGray4)
    73 	{
    74     aGray4&=3;
    75     aGray4|=aGray4<<2;
    76     aGray4|=aGray4<<4;
    77     return(TRgb(aGray4,aGray4,aGray4));
    78 	}
    79 
    80 /** Gets TRgb from 16 level grayscale.
    81 
    82 The function takes a grayscale argument and return a TRgb whose red, green
    83 and blue values are set to an appropriate level.
    84 
    85 @param aGray16 Grayscale value to be converted.
    86 @return Equivalent 24 bit colour. Gray16 has 16 levels - the function returns
    87 r=g=b=17*c, where c=0, 1, ... 15. */
    88 EXPORT_C TRgb TRgb::Gray16(TInt aGray16)
    89 	{
    90     aGray16&=0xf;
    91     aGray16|=aGray16<<4;
    92     return(TRgb(aGray16,aGray16,aGray16));
    93 	}
    94 
    95 /** Gets TRgb from 256 level grayscale.
    96 
    97 The function takes a grayscale argument and return a TRgb whose red, green
    98 and blue values are set to an appropriate level.
    99 
   100 @param aGray256 Grayscale value to be converted.
   101 @return Equivalent 24 bit colour. Gray256 has 256 levels- the function
   102 returns r=g=b=c, where c=0, 1, ... 255. */
   103 EXPORT_C TRgb TRgb::Gray256(TInt aGray256)
   104 	{
   105     aGray256&=0xff;
   106     return(TRgb(aGray256,aGray256,aGray256));
   107 	}
   108 
   109 /** Gets TRgb from 4 bit colour index.
   110 
   111 The function takes a 4 bit index into a colour palette and returns a TRgb
   112 whose red, green and blue values are set to an appropriate level.
   113 
   114 @param aColor16 4 bit index into a colour palette
   115 @return Equivalent 24 bit colour. */
   116 EXPORT_C TRgb TRgb::Color16(TInt aColor16)
   117 	{
   118 	return(TRgb(DynamicPalette::Color16array()[aColor16&0xf]));
   119 	}
   120 
   121 /** Gets TRgb from 8 bit colour index.
   122 
   123 The function takes an 8 bit index into a colour palette and returns a TRgb
   124 whose red, green and blue values are set to an appropriate level.
   125 
   126 @param aColor256 8 bit index into a colour palette.
   127 @return Equivalent 24 bit colour. */
   128 EXPORT_C TRgb TRgb::Color256(TInt aColor256)
   129 	{
   130 	return(TRgb(DynamicPalette::DefaultColor256Util()->iColorTable[aColor256&0xff]));
   131 	}
   132 
   133 /** Gets TRgb from 4K colour index.
   134 
   135 The function takes a 12 bit index into a colour palette and returns a TRgb
   136 whose red, green and blue values are set to an appropriate level.
   137 
   138 @param aColor4K 12 bit index into a colour palette
   139 @return Equivalent 24 bit colour. */
   140 EXPORT_C TRgb TRgb::Color4K(TInt aColor4K)
   141 	{
   142 	return _Color4K(aColor4K);
   143 	}
   144 
   145 /** Gets TRgb from 64K colour index.
   146 
   147 The function takes a 16 bit index into a colour palette and returns a TRgb
   148 whose red, green and blue values are set to an appropriate level.
   149 
   150 @param aColor64K 16 bit index into a colour palette
   151 @return Equivalent 24 bit colour. */
   152 EXPORT_C TRgb TRgb::Color64K(TInt aColor64K)
   153 	{
   154 	return _Color64K(aColor64K);
   155 	}
   156 
   157 /** Gets TRgb from 16M colour index.
   158 
   159 The function takes a 24 bit index into a colour palette and returns the TRgb
   160 whose red, green and blue values represent it exactly.
   161 
   162 @param aColor16M 24 bit index into a colour palette
   163 @return The TRgb which represents the index exactly. */
   164 EXPORT_C TRgb TRgb::Color16M(TInt aColor16M)
   165 	{
   166 	return _Color16M(aColor16M);
   167 	}
   168 
   169 /** Gets the index of the closest TRgb value to this,
   170 based on the matching display mode.
   171 
   172 @return    The index (0 - 1) representing the nearest TRgb. */
   173 EXPORT_C TInt TRgb::Gray2() const
   174 	{
   175 	return _Gray2();
   176 	}
   177 
   178 /**Gets the index of the closest TRgb value to this,
   179 based on the matching display mode.
   180 
   181 @return     The index (0 - 3) representing the nearest TRgb. */
   182 EXPORT_C TInt TRgb::Gray4() const
   183 	{
   184 	return _Gray4();
   185 	}
   186 
   187 /** Gets the index of the closest TRgb value to this,
   188 based on the matching display mode.
   189 
   190 @return     The index (0 - 15) representing the nearest TRgb.*/
   191 EXPORT_C TInt TRgb::Gray16() const
   192 	{
   193 	return _Gray16();
   194 	}
   195 
   196 /** Gets the index of the closest TRgb value to this,
   197 based on the matching display mode.
   198 
   199 @return     The index (0 - 255) representing the nearest TRgb.*/
   200 EXPORT_C TInt TRgb::Gray256() const
   201 	{
   202 	return _Gray256();
   203 	}
   204 
   205 /** Gets the index of the closest TRgb value to this,
   206 based on the matching display mode.
   207 
   208 @return  The index (0 - 15) representing the nearest TRgb. */
   209 EXPORT_C TInt TRgb::Color16() const
   210 	{
   211 	TInt index = (iValue & 0x000000e0) << 1;
   212 	index |= (iValue & 0x0000e000) >> 10;
   213 	index |= (iValue & 0x00e00000) >> 21;
   214 	return DynamicPalette::Color16inverse()[index];
   215 	}
   216 
   217 /** Gets the index of the closest TRgb value to this,
   218 based on the matching display mode.
   219 
   220 @return    The index (0 - 255) representing the nearest TRgb. */
   221 EXPORT_C TInt TRgb::Color256() const
   222 	{
   223 	TInt index = (iValue & 0x000000f0) << 4;
   224 	index |= (iValue & 0x0000f000) >> 8;
   225 	index |= (iValue & 0x00f00000) >> 20;
   226 	return DynamicPalette::DefaultColor256Util()->iInverseColorTable[index];
   227 	}
   228 
   229 /** Gets the index of the closest TRgb value to this,
   230 based on the matching display mode.
   231 
   232 @return The index (0 - 4095) representing the nearest TRgb. */
   233 EXPORT_C TInt TRgb::Color4K() const
   234 	{
   235 	return _Color4K();
   236 	}
   237 
   238 /**  Gets the index of the closest TRgb value to this,
   239 based on the matching display mode.
   240 
   241 @return The index (0 - 65535) representing the nearest TRgb.*/
   242 EXPORT_C TInt TRgb::Color64K() const
   243 	{
   244 	return _Color64K();
   245 	}
   246 
   247 /** Gets the index of the closest TRgb value to this,
   248 based on the matching display mode.
   249 
   250 @return The index (0 - 16777215) representing the nearest TRgb.*/
   251 EXPORT_C TInt TRgb::Color16M() const
   252 	{
   253 	return _Color16M();
   254 	}
   255 
   256 /** Gets the difference between two TRgbs.
   257 
   258 This difference is defined as the sum of the absolute values of the difference
   259 in the red, green and blue components.
   260 
   261 @param aColor The TRgb to be compared.
   262 @return The sum of the absolute value of the differences between the red, green
   263 and blue components. */
   264 EXPORT_C TInt TRgb::Difference(const TRgb& aColor) const
   265 	{
   266 	return(Abs((TInt)(aColor.Internal()&0xFF)-(TInt)(Internal()&0xFF))+
   267 		(Abs((TInt)(aColor.Internal()&0xFF00)-(TInt)(Internal()&0xFF00))>>8)+
   268 		(Abs((TInt)(aColor.Internal()&0xFF0000)-(TInt)(Internal()&0xFF0000))>>16));
   269 	}
   270 
   271 /** Internalises a TRgb object from a stream.
   272 
   273 The presence of this function means that the standard templated stream operator>>()
   274 is available to internalise objects of this class.
   275 
   276 @param aStream Stream from which the object is internalised.
   277 @see operator>>() */
   278 EXPORT_C void TRgb::InternalizeL(RReadStream& aStream)
   279 	{
   280 	TInt red=aStream.ReadUint8L();
   281 	TInt green=aStream.ReadUint8L();
   282 	TInt blue=aStream.ReadUint8L();
   283 	*this=TRgb(red,green,blue);
   284 	}
   285 
   286 /** Externalises a TRgb object to a stream.
   287 
   288 The presence of this function means that the standard templated stream operator<<()
   289 is available to externalise objects of this class.
   290 
   291 @param aStream Stream to which the object is externalised. */
   292 EXPORT_C void TRgb::ExternalizeL(RWriteStream& aStream) const
   293 	{
   294 	aStream.WriteUint8L(Red());
   295 	aStream.WriteUint8L(Green());
   296 	aStream.WriteUint8L(Blue());
   297 	}
   298 
   299 /** Gets TRgb from 16MU colour index.
   300 The function takes a 24 bit colour value with eight bits for each
   301 component, blue in the low byte, and returns the TRgb
   302 whose red, green and blue values represent it exactly.
   303 @param     a0RGB The color - 0, R, G, B bytes. / BGR0 - little endian format /
   304 @return    The TRgb which represents the index exactly. */
   305 EXPORT_C TRgb TRgb::Color16MU(TInt a0RGB)
   306 	{
   307 	//                  R                           G                     B
   308 	return _Color16MU(a0RGB);
   309 	}
   310 
   311 /** Gets the index of the closest TRgb value to this, based on the matching display mode.
   312 @return   The index (0 - 16777215) representing the nearest TRgb. */
   313 EXPORT_C TInt TRgb::Color16MU() const
   314 	{
   315 	//              R                            G                      B
   316 	return _Color16MU();
   317 	}
   318 
   319 
   320 /** Sets the alpha component.
   321 
   322 @param aAlpha Alpha component (0 - 255). */
   323 EXPORT_C void TRgb::SetAlpha(TInt aAlpha)
   324 	{
   325 	iValue&=0x00ffffff;
   326 	iValue|=(aAlpha << 24);
   327 	}
   328 
   329 
   330 /** Gets TRgb from 16MA colour index.
   331 The function takes a 32 bit colour value with eight bits for each
   332 component, blue in the low byte, and returns the TRgb
   333 whose red, green, blue and alpha values represent it exactly.
   334 @param     aARGB The color - A, R, G, B bytes. / BGRA - little endian format /
   335 @return    The TRgb which represents the index exactly. */
   336 EXPORT_C TRgb TRgb::Color16MA(TUint aARGB)
   337 	{
   338 	return _Color16MA(aARGB);
   339 	}
   340 
   341 /** Gets the index of the closest TRgb value to this, based on the matching display mode.
   342 @return   The index (0 - 16777215) representing the nearest TRgb. */
   343 EXPORT_C TUint TRgb::Color16MA() const
   344 	{
   345 	return _Color16MA();
   346 	}
   347 
   348 /** Gets TRgb from 16MAP colour index.
   349 The function takes a 32 bit colour value with eight bits for each
   350 component, blue in the low byte, and returns the TRgb
   351 whose red, green, and blue vales are divided by the alpha value.
   352 @param     aARGB The pre-multiplied (EColor16MAP) color value.
   353 @return    The TRgb which represents the color channel and alpha information. */
   354 /* static */
   355 EXPORT_C TRgb TRgb::Color16MAP(TUint aARGB)
   356 	{
   357 	const TUint16* ArTable = PtrTo16BitNormalisationTable();
   358 	TRgb retColor;
   359 	retColor.SetInternal(PMA2NonPMAPixel(aARGB, ArTable));
   360 	return retColor;
   361 	}
   362 
   363 /** Gets the index of the closest TRgb value to this, based on the matching display mode.
   364 pre-multiplies the alpha channels with the color channel.
   365 @return   The pre-multiplied color value */	
   366 EXPORT_C TUint TRgb::Color16MAP() const
   367 	{
   368 	return NonPMA2PMAPixel(iValue);
   369 	}
   370 	
   371 
   372 /** Tests whether the display mode specified is colour or greyscale.
   373 
   374 @param aDispMode The display mode.
   375 @return ETrue if colour; EFalse if greyscale or monochrome. */
   376 EXPORT_C TBool TDisplayModeUtils::IsDisplayModeColor(TDisplayMode aDispMode)
   377 	{
   378 	return (aDispMode >= EColor16);
   379 	}
   380 	
   381 /** Tests whether the display mode specified is one of the valid values.
   382 
   383 @param aDispMode The display mode to be tested.
   384 @return ETrue if aDispMode is valid; EFalse if not valid. */
   385 EXPORT_C TBool TDisplayModeUtils::IsDisplayModeValid(TDisplayMode aDispMode)
   386 	{
   387 	return aDispMode >= ENone && aDispMode < EColorLast;
   388 	}
   389 
   390 /** Gets the number of colours or shades of grey supported by the specified
   391 display mode.
   392 
   393 For instance, a display mode of EGray4 returns 4, EColor4K returns 4096.
   394 
   395 @param aDispMode The display mode.
   396 @return The number of colours/grey shades supported by the display mode. */
   397 EXPORT_C TInt TDisplayModeUtils::NumDisplayModeColors(TDisplayMode aDispMode)
   398 	{
   399 	switch (aDispMode)
   400 		{
   401 	case EGray2:
   402 		return 2;
   403 	case EGray4:
   404 		return 4;
   405 	case EGray16:
   406 	case EColor16:
   407 		return 16;
   408 	case EGray256:
   409 	case EColor256:
   410 		return 256;
   411 	case EColor4K:
   412 		return 4096;
   413 	case EColor64K:
   414 		return 65536;
   415 	case EColor16M:
   416 	case EColor16MU:
   417 	case EColor16MA:
   418 	case EColor16MAP:
   419 		return 16777216;
   420 	default:
   421 		return 0;
   422 		};
   423 	}
   424 
   425 /** Gets the number of bits required by each pixel when displayed in the
   426 specified display mode.
   427 
   428 @param aDispMode The display mode.
   429 @return The number of bits required by each pixel. */
   430 EXPORT_C TInt TDisplayModeUtils::NumDisplayModeBitsPerPixel(TDisplayMode aDispMode)
   431 	{
   432 	switch (aDispMode)
   433 		{
   434 	case EGray2:
   435 		return 1;
   436 	case EGray4:
   437 		return 2;
   438 	case EGray16:
   439 	case EColor16:
   440 		return 4;
   441 	case EGray256:
   442 	case EColor256:
   443 		return 8;
   444 	case EColor4K:
   445 		return 12;
   446 	case EColor64K:
   447 		return 16;
   448 	case EColor16M:
   449 		return 24;
   450 	case EColor16MU:
   451 	case EColor16MA:
   452 	case EColor16MAP:
   453 		return 32;
   454 	default:
   455 		return 0;
   456 		};
   457 	}
   458 
   459 //
   460 // TColor256Util
   461 //
   462 /** Initialises the two lookup tables using the specified palette.
   463 
   464 @param aPalette The palette of colours used to initialise the colour lookup
   465 tables. */
   466 EXPORT_C void TColor256Util::Construct(const CPalette& aPalette)
   467 	{
   468 	TInt n = aPalette.Entries();
   469 	if(n>256)
   470 		n = 256;
   471 
   472 	TInt i;
   473 	for(i=0; i<n; i++)
   474 		iColorTable[i] = aPalette.GetEntry(i).Value();
   475 	for(; i<256; i++)
   476 		iColorTable[i] = 0;
   477 
   478 	i = 0;
   479 	for(TInt b=0; b<0x100; b+=0x11)
   480 		for(TInt g=0; g<0x100; g+=0x11)
   481 			for(TInt r=0; r<0x100; r+=0x11)
   482 				iInverseColorTable[i++] = (TUint8)aPalette.NearestIndex(TRgb(r,g,b));
   483 	}
   484 
   485 /** Gets the entry from the inverse colour lookup table for the colour that most
   486 closely matches the specified TRgb value.
   487 
   488 Entries in the inverse colour lookup table are indices into the palette that
   489 the object was created with. Essentially, this function matches aRgb to the
   490 index of the nearest colour in the palette.
   491 
   492 @param aRgb The conversion colour.
   493 @return The index of the nearest colour to aRgb in the palette. */
   494 EXPORT_C TInt TColor256Util::Color256(TRgb aRgb) const
   495 	{
   496 	TInt index = (aRgb.Value() & 0x000000f0) >> 4;
   497 	index |= (aRgb.Value() & 0x0000f000) >> 8;
   498 	index |= (aRgb.Value() & 0x00f00000) >> 12;
   499 	return iInverseColorTable[index];
   500 	}
   501 
   502 /** Gets the entries from the inverse colour lookup table for the colours that
   503 most closely match the specified TRgb values.
   504 
   505 @param aDestination On return, a pointer to a buffer containing the entries
   506 from the inverse colour lookup table.
   507 @param aSource Pointer to the first TRgb value to match.
   508 @param aNumPixels The number of TRgb values to match. */
   509 EXPORT_C void TColor256Util::Color256(TUint8* aDestination,const TRgb* aSource,TInt aNumPixels) const
   510 	{
   511 	TUint8* limit = aDestination+aNumPixels;
   512 	while(aDestination<limit)
   513 		{
   514 		TInt value = (*aSource++).Value();
   515 		TInt index = (value & 0x000000f0) >> 4;
   516 		index |= (value & 0x0000f000) >> 8;
   517 		index |= (value & 0x00f00000) >> 12;
   518 		*(aDestination++) = iInverseColorTable[index];
   519 		}
   520 	}
   521 
   522 /** Returns a pointer to the system default 256 colour palette.
   523 
   524 @return Pointer to the system default 256 colour palette. */
   525 EXPORT_C const TColor256Util* TColor256Util::Default()
   526 	{
   527 	return DynamicPalette::DefaultColor256Util();
   528 	}
   529