graphicstools/gdi_tools/bmconv/RGB.CPP
changeset 0 5d03bc08d59c
equal deleted inserted replaced
-1:000000000000 0:5d03bc08d59c
       
     1 // Copyright (c) 1997-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 "BMCONV.H"
       
    17 #include "RGB.H"
       
    18 
       
    19 extern TRgb* color256Palette;
       
    20 extern char* color256InversePalette;
       
    21 
       
    22 TRgb* color256Palette = NULL;
       
    23 char* color256InversePalette = NULL;
       
    24 
       
    25 TRgb::TRgb()
       
    26 	: iRed(255),iGreen(255),iBlue(255),iSpare(0)
       
    27 /** Constructs default TRgb with all 3 colour components set to 255. */
       
    28 	{}
       
    29 
       
    30 TRgb::TRgb(long unsigned int val)
       
    31 	: iRed((unsigned char)(val&0xff)),iGreen((unsigned char)((val>>8)&0xff)),iBlue((unsigned char)((val>>16)&0xff)),iSpare(0)
       
    32 	{}
       
    33 
       
    34 TRgb::TRgb(int r,int g,int b)
       
    35 	: iRed((unsigned char)r),iGreen((unsigned char)g),iBlue((unsigned char)b),iSpare(0)
       
    36 /** Constructs a TRgb from its three component colours.
       
    37 	
       
    38 Each colour has a value between 0 and 255.
       
    39 	
       
    40 @param aRed Red component of the colour (0 - 255). 
       
    41 @param aGreen Green component of the colour (0 - 255). 
       
    42 @param aBlue Blue component of the colour (0 - 255). */
       
    43 	{}
       
    44 
       
    45 TRgb &TRgb::operator=(const TRgb &col)
       
    46 	{
       
    47 	iRed=col.iRed;
       
    48 	iGreen=col.iGreen;
       
    49 	iBlue=col.iBlue;
       
    50 	return(*this);
       
    51 	}
       
    52 
       
    53 int TRgb::operator==(const TRgb &col)
       
    54 	{
       
    55 	return(iRed==col.iRed && iGreen==col.iGreen && iBlue==col.iBlue);
       
    56 	}
       
    57 
       
    58 int TRgb::Difference(const TRgb& col) const
       
    59 	{
       
    60 	return abs(iRed-col.iRed) + abs(iGreen-col.iGreen) + abs(iBlue-col.iBlue);
       
    61 	}
       
    62 
       
    63 int TRgb::Gray2() const
       
    64 /** Gets a 2 level grayscale value from this TRgb. 
       
    65 
       
    66 @return Equivalent 2 level grayscale value. The greyscale value is 0 or 1, 
       
    67 and is calculated using c=(2*r+5*g+b)/1024. Note that the return value is 
       
    68 rounded down to the nearest integer. */
       
    69 	{
       
    70 	return Gray256() / 128;
       
    71 	}
       
    72 
       
    73 int TRgb::Gray4() const
       
    74 /** Gets a 4 level grayscale value from this TRgb.
       
    75 
       
    76 @return Equivalent 4 level grayscale value. The greyscale value is calculated 
       
    77 using c=(2*r+5*g+b)/512. Note that the return value is rounded down to the 
       
    78 nearest integer. */
       
    79 	{
       
    80 	return Gray256() / 64;
       
    81 	}
       
    82 
       
    83 int TRgb::Gray16() const
       
    84 /** Gets a 16 level grayscale value from this TRgb.
       
    85 
       
    86 @return The greyscale value is calculated using c=(2*r+5*g+b)/128. Note that 
       
    87 the return value is rounded down to the nearest integer. */
       
    88 	{
       
    89 	return Gray256() / 16;
       
    90 	}
       
    91 
       
    92 int TRgb::Gray256() const
       
    93 /** Gets a 256 level grayscale value from this TRgb.
       
    94 
       
    95 @return The greyscale value is calculated using c=(2*r+5*g+b)/8. Note that 
       
    96 the return value is rounded down to the nearest integer. */
       
    97 	{
       
    98 	return((2*iRed+5*iGreen+iBlue)/8);
       
    99 	}
       
   100 
       
   101 int TRgb::Color16() const
       
   102 /** Gets a 4 bit index into a colour palette from this TRgb.
       
   103 
       
   104 @return The EGA low colour constant closest to the TRgb. */
       
   105 	{
       
   106 	int index = (iRed >> 5) & 0x007;
       
   107 	index |= (iGreen  >> 2) & 0x038;
       
   108 	index |= (iBlue << 1) & 0x1c0;
       
   109 	return color16inverse[index];
       
   110 	}
       
   111 
       
   112 int TRgb::Color256() const
       
   113 /** Gets an 8 bit index into a colour palette from this TRgb.
       
   114 
       
   115 @return An 8 bit index. */
       
   116 	{
       
   117 	int index = (iRed >> 4) & 0x00f;
       
   118 	index |= iGreen & 0x0f0;
       
   119 	index |= (iBlue << 4) & 0xf00;
       
   120 
       
   121 	if (color256InversePalette)
       
   122 		return color256InversePalette[index];
       
   123 	else
       
   124 		return color256inverse[index];
       
   125 	}
       
   126 
       
   127 int TRgb::Color4K() const
       
   128 /** Gets a 12 bit index into a colour palette from this TRgb.
       
   129 
       
   130 @return A 12 bit index. */
       
   131 	{
       
   132 	return(((iRed&0xf0)<<4)|(iGreen&0xf0)|((iBlue&0xf0)>>4));
       
   133 	}
       
   134 
       
   135 int TRgb::Color64K() const
       
   136 /** Gets a 24 bit index into a colour palette from this TRgb.
       
   137 
       
   138 @return A 24 bit index. */
       
   139 	{
       
   140 	return(((iRed&0xf8)<<8)|((iGreen&0xfc)<<3)|((iBlue&0xf8)>>3));
       
   141 	}
       
   142 
       
   143 long int TRgb::Color16M() const
       
   144 /** Gets a 16 bit index into a colour palette from this TRgb.
       
   145 
       
   146 @return A 16 bit index. */
       
   147 	{
       
   148 	return((iRed<<16)|(iGreen<<8)|iBlue);
       
   149 	}
       
   150 
       
   151 TRgb TRgb::Gray2(int aGray2)
       
   152 /** Gets TRgb from 2 level grayscale. 
       
   153 
       
   154 The function takes a grayscale argument and return a TRgb whose red, green 
       
   155 and blue values are set to an appropriate level. 
       
   156 
       
   157 @param aGray2 Grayscale value to be converted. 
       
   158 @return Equivalent 24 bit colour. Gray2 has only 2 levels (black and white), -
       
   159 the function returns r=g=b=0 or r=g=b=255. */
       
   160 	{
       
   161 	aGray2 *= 255;
       
   162 	return TRgb(aGray2,aGray2,aGray2);
       
   163 	}
       
   164 
       
   165 TRgb TRgb::Gray4(int aGray4)
       
   166 /** Gets TRgb from 4 level grayscale. 
       
   167 
       
   168 The function takes a grayscale argument and return a TRgb whose red, green 
       
   169 and blue values are set to an appropriate level.
       
   170 
       
   171 @param aGray4 Grayscale value to be converted. 
       
   172 @return Equivalent 24 bit colour. Gray4 has 4 levels- the function returns 
       
   173 r=g=b=85*c, where c=0,1,2, or 3. */
       
   174 	{
       
   175 	aGray4 *= 85;
       
   176 	return TRgb(aGray4,aGray4,aGray4);
       
   177 	}
       
   178 
       
   179 TRgb TRgb::Gray16(int aGray16)
       
   180 /** Gets TRgb from 16 level grayscale. 
       
   181 
       
   182 The function takes a grayscale argument and return a TRgb whose red, green 
       
   183 and blue values are set to an appropriate level.
       
   184 
       
   185 @param aGray16 Grayscale value to be converted. 
       
   186 @return Equivalent 24 bit colour. Gray16 has 16 levels- the function returns 
       
   187 r=g=b=17*c, where c=0, 1, ... 15. */
       
   188 	{
       
   189 	aGray16 *= 17;
       
   190 	return TRgb(aGray16,aGray16,aGray16);
       
   191 	}
       
   192 
       
   193 TRgb TRgb::Gray256(int aGray256)
       
   194 /** Gets TRgb from 256 level grayscale. 
       
   195 
       
   196 The function takes a grayscale argument and return a TRgb whose red, green 
       
   197 and blue values are set to an appropriate level.
       
   198 
       
   199 @param aGray256 Grayscale value to be converted. 
       
   200 @return Equivalent 24 bit colour. Gray256 has 256 levels- the function 
       
   201 returns r=g=b=c, where c=0, 1, ... 255. */
       
   202 	{
       
   203 	return TRgb(aGray256,aGray256,aGray256);
       
   204 	}
       
   205 
       
   206 TRgb TRgb::Color16(int aColor16)
       
   207 /** Gets TRgb from 4 bit colour index.
       
   208 
       
   209 The function takes a 4 bit index into a colour palette and returns a TRgb 
       
   210 whose red, green and blue values are set to an appropriate level.
       
   211 
       
   212 @param aColor16 4 bit index into a colour palette 
       
   213 @return Equivalent 24 bit colour. */
       
   214 	{
       
   215 	return TRgb(color16array[aColor16&0xf]);
       
   216 	}
       
   217 
       
   218 TRgb TRgb::Color256(int aColor256)
       
   219 /** Gets TRgb from 8 bit colour index.
       
   220 
       
   221 The function takes an 8 bit index into a colour palette and returns a TRgb 
       
   222 whose red, green and blue values are set to an appropriate level.
       
   223 
       
   224 @param aColor256 8 bit index into a colour palette. 
       
   225 @return Equivalent 24 bit colour. */
       
   226 	{
       
   227 	if (color256Palette)
       
   228 		return color256Palette[aColor256&0xff];
       
   229 	else
       
   230 		return TRgb(color256array[aColor256&0xff]);
       
   231 	}
       
   232 
       
   233 TRgb TRgb::Color4K(int aColor4K)
       
   234 	{
       
   235 	return TRgb(((aColor4K>>8)&0xf)*17,((aColor4K>>4)&0xf)*17,(aColor4K&0xf)*17);
       
   236 	}
       
   237 
       
   238 TRgb TRgb::Color64K(int aColor64K)
       
   239 /** Gets TRgb from 64K colour index.
       
   240 
       
   241 The function takes a 16 bit index into a colour palette and returns a TRgb 
       
   242 whose red, green and blue values are set to an appropriate level.
       
   243 
       
   244 @param aColor64K 16 bit index into a colour palette 
       
   245 @return Equivalent 24 bit colour. */
       
   246 	{
       
   247 	return TRgb(((aColor64K>>11)&0x1f)*255/31,((aColor64K>>5)&0x3f)*255/63,(aColor64K&0x1f)*255/31);
       
   248 	}
       
   249 
       
   250 TRgb TRgb::Color16M(long int aColor16M)
       
   251 /** Gets TRgb from 16M colour index.
       
   252 
       
   253 The function takes a 24 bit index into a colour palette and returns the TRgb 
       
   254 whose red, green and blue values represent it exactly.
       
   255 
       
   256 @param aColor16M 24 bit index into a colour palette 
       
   257 @return The TRgb which represents the index exactly. */
       
   258 	{
       
   259 	return TRgb(((aColor16M>>16)&0xff),(aColor16M>>8)&0xff,aColor16M&0xff);
       
   260 	}
       
   261