00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 #ifndef __CS_RGBPIXEL_H__
00055 #define __CS_RGBPIXEL_H__
00056
00057 #include <stdio.h>
00058 #include "cstypes.h"
00059
00064 CS_STRUCT_ALIGN_4BYTE_BEGIN
00065 struct csRGBcolor
00066 {
00068 unsigned char red, green, blue;
00070 csRGBcolor () : red(0), green(0), blue(0) {}
00072 csRGBcolor (unsigned char r, unsigned char g, unsigned char b) :
00073 red(r), green(g), blue(b) {}
00075 void Set (unsigned char r, unsigned char g, unsigned char b)
00076 { red = r; green = g; blue = b; }
00078 bool operator == (const csRGBcolor& c) const
00079 { return (c.red == red) && (c.green == green) && (c.blue == blue); }
00081 bool operator != (const csRGBcolor& c) const
00082 { return !operator == (c); }
00084 csRGBcolor operator + (const csRGBcolor& c) const
00085 { return csRGBcolor (c.red + red, c.green + green, c.blue + blue); }
00086 } CS_STRUCT_ALIGN_4BYTE_END;
00087
00088
00089 #ifdef CS_BIG_ENDIAN
00090 # define RGB_MASK 0xffffff00
00091 #else
00092 # define RGB_MASK 0x00ffffff
00093 #endif
00094
00100 CS_STRUCT_ALIGN_4BYTE_BEGIN
00101 struct csRGBpixel
00102 {
00104 unsigned char red, green, blue, alpha;
00106 csRGBpixel ()
00107 { *(uint32 *)this = (uint32)~RGB_MASK; }
00109 csRGBpixel (const csRGBpixel& p)
00110
00111 { *(uint32*)this = *(uint32*)&p; }
00113 csRGBpixel (const csRGBcolor& c) :
00114 red (c.red), green (c.green), blue (c.blue), alpha (255) {}
00116 csRGBpixel (int r, int g, int b) :
00117 red (r), green (g), blue (b), alpha (255) {}
00119 bool operator == (const csRGBcolor& c) const
00120 { return (c.red == red) && (c.green == green) && (c.blue == blue); }
00122 bool operator == (const csRGBpixel& p) const
00123
00124 { return *(uint32*)this == *(uint32*)&p; }
00126 bool operator != (const csRGBcolor& c) const
00127 { return !operator == (c); }
00132 bool operator != (const csRGBpixel& p) const
00133 { return !operator == (p); }
00135 operator csRGBcolor () const
00136 { return csRGBcolor (red, green, blue); }
00138 bool eq (const csRGBpixel& p) const
00139 { return ((*(uint32*)this) & RGB_MASK) == ((*(uint32*)&p) & RGB_MASK); }
00141 int Intensity ()
00142 { return (red + green + blue) / 3; }
00144 unsigned char Luminance ()
00145 { return (((int)red)*30 + ((int)green)*59 + ((int)blue)*11)/100; }
00147 void Set (const int r, const int g, const int b)
00148 { red = r; green = g; blue = b; alpha = 255; }
00150 void Set (const int r, const int g, const int b, const int a)
00151 { red = r; green = g; blue = b; alpha = a; }
00153 void Set (const csRGBpixel& p)
00154
00155 { *(uint32*)this = *(uint32*)&p; }
00157 void operator += (const csRGBcolor& c)
00158 {
00159 red += c.red;
00160 green += c.green;
00161 blue += c.blue;
00162 }
00167 void UnsafeAdd (const csRGBpixel&c)
00168 { *(uint32*)this += *(uint32*)&c; }
00173 void SafeAdd (const csRGBpixel&c)
00174 {
00175 int color = red+c.red;
00176 red = (color > 255) ? 255 : color;
00177 color = green+c.green;
00178 green = (color > 255) ? 255 : color;
00179 color = blue+c.blue;
00180 blue = (color > 255) ? 255 : color;
00181 }
00182 } CS_STRUCT_ALIGN_4BYTE_END;
00183
00184
00185 #undef RGB_MASK
00186
00193
00194 #define R_COEF 173
00195
00196 #define G_COEF 242
00197
00198 #define B_COEF 107
00199
00202
00203 #define R_COEF_SQ 299
00204
00205 #define G_COEF_SQ 587
00206
00207 #define B_COEF_SQ 114
00208
00212 #endif // __CS_RGBPIXEL_H__