CrystalSpace

Public API Reference

Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

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 "csextern.h"
00058 
00059 #include <stdio.h>
00060 #include "cstypes.h"
00061 
00066 CS_STRUCT_ALIGN_4BYTE_BEGIN
00067 struct csRGBcolor
00068 {
00070   unsigned char red, green, blue;
00072   csRGBcolor () : red(0), green(0), blue(0) {}
00074   csRGBcolor (unsigned char r, unsigned char g, unsigned char b) :
00075     red(r), green(g), blue(b) {}
00077   void Set (unsigned char r, unsigned char g, unsigned char b)
00078   { red = r; green = g; blue = b; }
00080   bool operator == (const csRGBcolor& c) const
00081   { return (c.red == red) && (c.green == green) && (c.blue == blue); }
00083   bool operator != (const csRGBcolor& c) const
00084   { return !operator == (c); }
00086   csRGBcolor operator + (const csRGBcolor& c) const
00087   { return csRGBcolor (
00088     (unsigned char)(c.red + red),
00089     (unsigned char)(c.green + green),
00090     (unsigned char)(c.blue + blue)); }
00091 } CS_STRUCT_ALIGN_4BYTE_END;
00092 
00093 
00099 CS_STRUCT_ALIGN_4BYTE_BEGIN
00100 struct csRGBpixel
00101 {
00103   unsigned char red, green, blue, alpha;
00105   csRGBpixel () : red(0), green(0), blue(0), alpha(255) {}
00107   csRGBpixel (const csRGBpixel& p) :
00108     red (p.red), green (p.green), blue (p.blue), alpha (p.alpha) {}
00110   csRGBpixel (const csRGBcolor& c) :
00111     red (c.red), green (c.green), blue (c.blue), alpha (255) {}
00113   csRGBpixel (int r, int g, int b, int a = 255) :
00114     red ((unsigned char)r),
00115     green ((unsigned char)g),
00116     blue ((unsigned char)b),
00117     alpha ((unsigned char)a) {}
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   { return (p.red == red) && (p.green == green) && (p.blue == blue); }
00125   bool operator != (const csRGBcolor& c) const
00126   { return !operator == (c); }
00131   bool operator != (const csRGBpixel& p) const
00132   { return !operator == (p); }
00134   operator csRGBcolor () const
00135   { return csRGBcolor (red, green, blue); }
00137   bool eq (const csRGBpixel& p) const
00138   { return operator==(p); }
00140   int Intensity ()
00141   { return (red + green + blue) / 3; }
00143   unsigned char Luminance ()
00144   {
00145     return (unsigned char)(((int)red*30 + (int)green*59 + (int)blue*11) / 100);
00146   }
00148   void Set (const int r, const int g, const int b, const int a = 255)
00149   {
00150     red = (unsigned char)r;
00151     green = (unsigned char)g;
00152     blue = (unsigned char)b;
00153     alpha = (unsigned char)a;
00154   }
00156   void Set (const csRGBpixel& p)
00157   { red = p.red; green = p.green; blue = p.blue; alpha = p.alpha; }
00159   void operator += (const csRGBcolor& c)
00160   {
00161       red   = (unsigned char)(red   + c.red  );
00162       green = (unsigned char)(green + c.green);
00163       blue  = (unsigned char)(blue  + c.blue );
00164   }
00169   void UnsafeAdd (const csRGBpixel& c)
00170   {
00171     red   = (unsigned char)(red   + c.red  );
00172     green = (unsigned char)(green + c.green);
00173     blue  = (unsigned char)(blue  + c.blue );
00174   }
00179   void SafeAdd (const csRGBpixel& c)
00180   {
00181     int color = red + c.red;
00182     red   = (unsigned char)(color > 255 ? 255 : color);
00183     color = green + c.green;
00184     green = (unsigned char)(color > 255 ? 255 : color);
00185     color = blue + c.blue;
00186     blue  = (unsigned char)(color > 255 ? 255 : color);
00187   }
00188 } CS_STRUCT_ALIGN_4BYTE_END;
00189 
00190 
00197 
00198 #define R_COEF          173
00199 
00200 #define G_COEF          242
00201 
00202 #define B_COEF          107
00203 
00206 
00207 #define R_COEF_SQ       299
00208 
00209 #define G_COEF_SQ       587
00210 
00211 #define B_COEF_SQ       114
00212 
00216 #endif // __CS_RGBPIXEL_H__

Generated for Crystal Space by doxygen 1.2.18