CrystalSpace

Public API Reference

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

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 (c.red + red, c.green + green, c.blue + blue); }
00086 } CS_STRUCT_ALIGN_4BYTE_END;
00087 
00088 // As an optimization, we sometimes handle R/G/B values simultaneously.
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 () /* : red(0), green(0), blue(0), alpha(255) {} */
00107   { *(uint32 *)this = (uint32)~RGB_MASK; }
00109   csRGBpixel (const csRGBpixel& p)
00110   /* : red (p.red), green (p.green), blue (p.blue), alpha (p.alpha) {} */
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   /* { return (p.red == red) && (p.green == green) && (p.blue == blue); } */
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   /* : red (p.red), green (p.green), blue (p.blue), alpha (p.alpha) {} */
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 // We don't need RGB_MASK anymore
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__

Generated for Crystal Space by doxygen 1.2.14