CrystalSpace

Public API Reference

Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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 "csextern.h"
00058 
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, int a = 255) :
00112     red ((unsigned char)r),
00113     green ((unsigned char)g),
00114     blue ((unsigned char)b),
00115     alpha ((unsigned char)a) {}
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 () const
00139   { return (red + green + blue) / 3; }
00141   unsigned char Luminance () const
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, const int a = 255)
00147   {
00148     red = (unsigned char)r;
00149     green = (unsigned char)g;
00150     blue = (unsigned char)b;
00151     alpha = (unsigned char)a;
00152   }
00154   void Set (const csRGBpixel& p)
00155   { red = p.red; green = p.green; blue = p.blue; alpha = p.alpha; }
00157   void operator += (const csRGBcolor& c)
00158   {
00159       red   = (unsigned char)(red   + c.red  );
00160       green = (unsigned char)(green + c.green);
00161       blue  = (unsigned char)(blue  + c.blue );
00162   }
00167   void UnsafeAdd (const csRGBpixel& c)
00168   {
00169     red   = (unsigned char)(red   + c.red  );
00170     green = (unsigned char)(green + c.green);
00171     blue  = (unsigned char)(blue  + c.blue );
00172   }
00177   void SafeAdd (const csRGBpixel& c)
00178   {
00179     int color = red + c.red;
00180     red   = (unsigned char)(color > 255 ? 255 : color);
00181     color = green + c.green;
00182     green = (unsigned char)(color > 255 ? 255 : color);
00183     color = blue + c.blue;
00184     blue  = (unsigned char)(color > 255 ? 255 : color);
00185   }
00186 } CS_STRUCT_ALIGN_4BYTE_END;
00187 
00188 
00195 
00196 #define R_COEF          173
00197 
00198 #define G_COEF          242
00199 
00200 #define B_COEF          107
00201 
00204 
00205 #define R_COEF_SQ       299
00206 
00207 #define G_COEF_SQ       587
00208 
00209 #define B_COEF_SQ       114
00210 
00214 #endif // __CS_RGBPIXEL_H__

Generated for Crystal Space by doxygen 1.4.4