csgfx/rgbpixel.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 1998 by Jorrit Tyberghein 00003 Contributions made by Ivan Avramovic <ivan@avramovic.com> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public 00016 License along with this library; if not, write to the Free 00017 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00018 */ 00019 00025 //----------------------------------------------------------------------------- 00026 // Implementation Note: Eric Sunshine <sunshine@sunshineco.com> 1999/02/09 00027 // 00028 // Certain portions of the Crystal Space code have strict requirements about 00029 // the sizes of the structures csRGBcolor and csRGBpixel. In particular, some 00030 // pieces of code make these assumptions: 00031 // 00032 // sizeof(csRGBcolor) == 3 (byte:rgb) 00033 // sizeof(csRGBpixel) == 4 (byte:rgb + byte:alpha) 00034 // 00035 // Originally, csRGBpixel was implemented as a subclasse of csRGBcolor and 00036 // added a byte-sized "alpha" variable. Thus, the original implementation made 00037 // the assumption that the compiler would not pad out the csRGBcolor structure. 00038 // 00039 // Unfortunately in some environments (such as the NextStep compiler on M68K 00040 // hardware) the compiler does pad csRGBcolor thus breaking the original 00041 // assumptions about structure sizes. In such cases, csRGBcolor is padded out 00042 // to 4 bytes instead of 3 and csRGBpixel is padded out to 6 bytes instead of 00043 // 4. This padding results in problems in code which makes assumptions about 00044 // the sizes of each structure. In practice, problems were observed in code 00045 // which expected csRGBpixel to be 4 bytes. 00046 // 00047 // To work around this problem, csRGBpixel has been re-implemented so that it 00048 // is no longer derived from csRGBcolor. An unfortunate side-effect of this 00049 // re-implementation is that code is no longer inherited, and is thus 00050 // duplicated in each class. However, except for this minor point, the size of 00051 // each structure should now be more stable between various compilers. 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 ( 00086 (unsigned char)(c.red + red), 00087 (unsigned char)(c.green + green), 00088 (unsigned char)(c.blue + blue)); } 00089 } CS_STRUCT_ALIGN_4BYTE_END; 00090 00091 00097 CS_STRUCT_ALIGN_4BYTE_BEGIN 00098 struct csRGBpixel 00099 { 00101 unsigned char red, green, blue, alpha; 00103 csRGBpixel () : red(0), green(0), blue(0), alpha(255) {} 00105 csRGBpixel (const csRGBpixel& p) : 00106 red (p.red), green (p.green), blue (p.blue), alpha (p.alpha) {} 00108 csRGBpixel (const csRGBcolor& c) : 00109 red (c.red), green (c.green), blue (c.blue), alpha (255) {} 00111 csRGBpixel (int r, int g, int b) : 00112 red ((unsigned char)r), 00113 green ((unsigned char)g), 00114 blue ((unsigned char)b), 00115 alpha ((unsigned char)255) {} 00117 bool operator == (const csRGBcolor& c) const 00118 { return (c.red == red) && (c.green == green) && (c.blue == blue); } 00120 bool operator == (const csRGBpixel& p) const 00121 { return (p.red == red) && (p.green == green) && (p.blue == blue); } 00123 bool operator != (const csRGBcolor& c) const 00124 { return !operator == (c); } 00129 bool operator != (const csRGBpixel& p) const 00130 { return !operator == (p); } 00132 operator csRGBcolor () const 00133 { return csRGBcolor (red, green, blue); } 00135 bool eq (const csRGBpixel& p) const 00136 { return operator==(p); } 00138 int Intensity () 00139 { return (red + green + blue) / 3; } 00141 unsigned char Luminance () 00142 { 00143 return (unsigned char)(((int)red*30 + (int)green*59 + (int)blue*11) / 100); 00144 } 00146 void Set (const int r, const int g, const int b) 00147 { 00148 red = (unsigned char)r; 00149 green = (unsigned char)g; 00150 blue = (unsigned char)b; 00151 alpha = (unsigned char)255; 00152 } 00154 void Set (const int r, const int g, const int b, const int a) 00155 { 00156 red = (unsigned char)r; 00157 green = (unsigned char)g; 00158 blue = (unsigned char)b; 00159 alpha = (unsigned char)a; 00160 } 00162 void Set (const csRGBpixel& p) 00163 { red = p.red; green = p.green; blue = p.blue; alpha = p.alpha; } 00165 void operator += (const csRGBcolor& c) 00166 { 00167 red = (unsigned char)(red + c.red ); 00168 green = (unsigned char)(green + c.green); 00169 blue = (unsigned char)(blue + c.blue ); 00170 } 00175 void UnsafeAdd (const csRGBpixel& c) 00176 { 00177 red = (unsigned char)(red + c.red ); 00178 green = (unsigned char)(green + c.green); 00179 blue = (unsigned char)(blue + c.blue ); 00180 } 00185 void SafeAdd (const csRGBpixel& c) 00186 { 00187 int color = red + c.red; 00188 red = (unsigned char)(color > 255 ? 255 : color); 00189 color = green + c.green; 00190 green = (unsigned char)(color > 255 ? 255 : color); 00191 color = blue + c.blue; 00192 blue = (unsigned char)(color > 255 ? 255 : color); 00193 } 00194 } CS_STRUCT_ALIGN_4BYTE_END; 00195 00196 00203 00204 #define R_COEF 173 00205 00206 #define G_COEF 242 00207 00208 #define B_COEF 107 00209 00212 00213 #define R_COEF_SQ 299 00214 00215 #define G_COEF_SQ 587 00216 00217 #define B_COEF_SQ 114 00218 00222 #endif // __CS_RGBPIXEL_H__
Generated for Crystal Space by doxygen 1.2.18