Google

Main Page   Class Hierarchy   Compound List   File List   Compound Members  

rgbpixel.h

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 
00020 //-----------------------------------------------------------------------------
00021 // Implementation Note: Eric Sunshine <sunshine@sunshineco.com>      1999/02/09
00022 //
00023 // Certain portions of the Crystal Space code have strict requirements about
00024 // the sizes of the structures csRGBcolor and csRGBpixel.  In particular, some
00025 // pieces of code make these assumptions:
00026 //
00027 //    sizeof(csRGBcolor) == 3  (byte:rgb)
00028 //    sizeof(csRGBpixel) == 4  (byte:rgb + byte:alpha)
00029 //
00030 // Originally, csRGBpixel was implemented as a subclasse of csRGBcolor and
00031 // added a byte-sized "alpha" variable.  Thus, the original implementation made
00032 // the assumption that the compiler would not pad out the csRGBcolor structure.
00033 //
00034 // Unfortunately in some environments (such as the NextStep compiler on M68K
00035 // hardware) the compiler does pad csRGBcolor thus breaking the original
00036 // assumptions about structure sizes.  In such cases, csRGBcolor is padded out
00037 // to 4 bytes instead of 3 and csRGBpixel is padded out to 6 bytes instead of
00038 // 4.  This padding results in problems in code which makes assumptions about
00039 // the sizes of each structure.  In practice, problems were observed in code
00040 // which expected csRGBpixel to be 4 bytes.
00041 //
00042 // To work around this problem, csRGBpixel has been re-implemented so that it
00043 // is no longer derived from csRGBcolor.  An unfortunate side-effect of this
00044 // re-implementation is that code is no longer inherited, and is thus
00045 // duplicated in each class.  However, except for this minor point, the size of
00046 // each structure should now be more stable between various compilers.
00047 //-----------------------------------------------------------------------------
00048 
00049 #ifndef __CS_RGBPIXEL_H__
00050 #define __CS_RGBPIXEL_H__
00051 
00052 #include <stdio.h>
00053 #include "cstypes.h"
00054 
00059 struct csRGBcolor
00060 {
00061   unsigned char red, green, blue;
00062   csRGBcolor () : red(0), green(0), blue(0) {}
00063   csRGBcolor (unsigned char r, unsigned char g, unsigned char b) :
00064     red(r), green(g), blue(b) {}
00065   void Set (unsigned char r, unsigned char g, unsigned char b)
00066   { red = r; green = g; blue = b; }
00067   bool operator == (const csRGBcolor& c) const
00068   { return (c.red == red) && (c.green == green) && (c.blue == blue); }
00069   bool operator != (const csRGBcolor& c) const
00070   { return !operator == (c); }
00071   csRGBcolor operator + (const csRGBcolor& c) const
00072   { return csRGBcolor (c.red + red, c.green + green, c.blue + blue); }
00073 };
00074 
00075 // As an optimization, we sometimes handle R/G/B values simultaneously.
00076 #ifdef CS_BIG_ENDIAN
00077 #  define RGB_MASK 0xffffff00
00078 #else
00079 #  define RGB_MASK 0x00ffffff
00080 #endif
00081 
00087 struct csRGBpixel
00088 {
00090   unsigned char red, green, blue, alpha;
00092   csRGBpixel () /* : red(0), green(0), blue(0), alpha(255) {} */
00093   { *(uint32 *)this = (uint32)~RGB_MASK; }
00095   csRGBpixel (const csRGBpixel& p)
00096   /* : red (p.red), green (p.green), blue (p.blue), alpha (p.alpha) {} */
00097   { *(uint32*)this = *(uint32*)&p; }
00099   csRGBpixel (const csRGBcolor& c) :
00100     red (c.red), green (c.green), blue (c.blue), alpha (255) {}
00102   csRGBpixel (int r, int g, int b) :
00103     red (r), green (g), blue (b), alpha (255) {}
00105   bool operator == (const csRGBcolor& c) const
00106   { return (c.red == red) && (c.green == green) && (c.blue == blue); }
00108   bool operator == (const csRGBpixel& p) const
00109   /* { return (p.red == red) && (p.green == green) && (p.blue == blue); } */
00110   { return *(uint32*)this == *(uint32*)&p; }
00112   bool operator != (const csRGBcolor& c) const
00113   { return !operator == (c); }
00118   bool operator != (const csRGBpixel& p) const
00119   { return !operator == (p); }
00121   operator csRGBcolor () const
00122   { return csRGBcolor (red, green, blue); }
00124   bool eq (const csRGBpixel& p) const
00125   { return ((*(uint32*)this) & RGB_MASK) == ((*(uint32*)&p) & RGB_MASK); }
00127   int Intensity ()
00128   { return (red + green + blue) / 3; }
00129   unsigned char Luminance ()
00130   { return (((int)red)*30 + ((int)green)*59 + ((int)blue)*11)/100; }
00132   void Set (const int r, const int g, const int b)
00133   { red = r; green = g; blue = b; alpha = 255; }
00135   void Set (const int r, const int g, const int b, const int a)
00136   { red = r; green = g; blue = b; alpha = a; }
00137   void Set (const csRGBpixel& p)
00138   /* : red (p.red), green (p.green), blue (p.blue), alpha (p.alpha) {} */
00139   { *(uint32*)this = *(uint32*)&p; }
00140 };
00141 
00142 // We don't need RGB_MASK anymore
00143 #undef RGB_MASK
00144 
00151 
00152 #define R_COEF          173
00153 
00154 #define G_COEF          242
00155 
00156 #define B_COEF          107
00159 #define R_COEF_SQ       299
00160 
00161 #define G_COEF_SQ       587
00162 
00163 #define B_COEF_SQ       114
00164 
00165 #endif // __CS_RGBPIXEL_H__

Generated for Crystal Space by doxygen 1.2.5 written by Dimitri van Heesch, ©1997-2000