core/vil/vil_rgba.h
Go to the documentation of this file.
00001 // This is core/vil/vil_rgba.h
00002 #ifndef vil_rgba_h_
00003 #define vil_rgba_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief Templated four-value colour cell
00010 // \author Philip C. Pritchett, Robotics Research Group, University of Oxford
00011 // \date 12 Nov 99
00012 
00013 //: This is the appropriate pixel type for RGBA colour images.
00014 // The purpose of rgba<T> is to provide an object which consists of four Ts arranged
00015 // in order and which can be referred to as 'R', 'G', 'B' and 'A'. Thus, if win32
00016 // does something funny when blitting an rgba bitmap to screen, that's up to the
00017 // renderer to sort out.
00018 //
00019 //    Currently also includes the following `utilities':
00020 //    -#  conversion to ubyte (luminance of vil_rgba: weights (0.299, 0.587, 0.114, 0)).
00021 //    -#  min and max of vil_rgba<byte>  values, useful for morphological operations.
00022 //    -#  arithmetic operations
00023 
00024 #include <vcl_config_compiler.h>
00025 
00026 template <class T>
00027 struct vil_rgba
00028 {
00029  public:
00030   typedef T value_type;
00031 
00032   // The values.
00033   T  r; T g; T b; T a;
00034   inline T R() const { return r; }
00035   inline T G() const { return g; }
00036   inline T B() const { return b; }
00037   inline T A() const { return a; }
00038 
00039   vil_rgba() {}
00040 
00041   //: Create grey (v,v,v,1) vil_rgba cell from value v.
00042   // This provides a conversion from T to vil_rgba<T>, needed by e.g. two constructors in IUE_filter.h.
00043   vil_rgba(T v):
00044     r(v), g(v), b(v), a(1) {}
00045 
00046   //: Construct from four values.
00047   vil_rgba(T red, T green, T blue, T alpha = 1):
00048     r(red), g(green), b(blue), a(alpha) {}
00049 
00050   //: equality
00051   inline bool operator==(vil_rgba<T> const& o) const
00052   {
00053     return r==o.r && g==o.g && b==o.b && a==o.a;
00054   }
00055 
00056   // VC50 bombs with INTERNAL COMPILER ERROR on template member functions
00057 #if VCL_HAS_MEMBER_TEMPLATES
00058   template <class S>
00059   vil_rgba(const vil_rgba<S>& that) {
00060     r=((T)that.r);
00061     g=((T)that.g);
00062     b=((T)that.b);
00063     a=((T)that.a);
00064   }
00065 
00066   template <class S>
00067   vil_rgba<T>& operator=(const vil_rgba<S>& that) {
00068     r=((T)that.r);
00069     g=((T)that.g);
00070     b=((T)that.b);
00071     a=((T)that.a);
00072     return *this;
00073   }
00074 #else
00075   // Special-case for dumb compilers.
00076   inline vil_rgba(const vil_rgba<double>& that)
00077   {
00078     r=((T)that.r);
00079     g=((T)that.g);
00080     b=((T)that.b);
00081     a=((T)that.a);
00082   }
00083   inline vil_rgba(const vil_rgba<unsigned char>& that)
00084   {
00085     r=((T)that.r);
00086     g=((T)that.g);
00087     b=((T)that.b);
00088     a=((T)that.a);
00089   }
00090 
00091   inline vil_rgba<T>& operator=(const vil_rgba<double>& that)
00092   {
00093     r=((T)that.r);
00094     g=((T)that.g);
00095     b=((T)that.b);
00096     a=((T)that.a);
00097     return *this;
00098   }
00099 
00100   inline vil_rgba<T>& operator=(const vil_rgba<float>& that)
00101   {
00102     r=((T)that.r);
00103     g=((T)that.g);
00104     b=((T)that.b);
00105     a=((T)that.a);
00106     return *this;
00107   }
00108 
00109   inline vil_rgba<T>& operator=(const vil_rgba<unsigned char>& that)
00110   {
00111     r=((T)that.r);
00112     g=((T)that.g);
00113     b=((T)that.b);
00114     a=((T)that.a);
00115     return *this;
00116   }
00117 #endif
00118 
00119   //: Convert vil_rgba to gray using standard (.299, .587, .114) RGB weighting.
00120   T grey() const { return T(0.5+r*0.299+0.587*g+0.114*b); }
00121 
00122 #if 0 // deprecated -- use .grey() instead
00123   operator T() const { return T(0.5+r*0.299+0.587*g+0.114*b); }
00124 #endif
00125 };
00126 
00127 #define VIL_RGBA_INSTANTIATE \
00128 extern "please include vil/vil_rgba.txx instead"
00129 
00130 #endif // vil_rgba_h_