vil_rgb.h
Go to the documentation of this file.
1 // This is core/vil/vil_rgb.h
2 #ifndef vil_rgb_h_
3 #define vil_rgb_h_
4 //:
5 // \file
6 // \brief Pixel type for 24 bit images
7 //
8 // Currently also includes the following `utilities':
9 // - conversion to ubyte (luminance of vil_rgb: weights (0.299,0.587,0.114)).
10 // - min and max of vil_rgbcell values, useful for morphological operations.
11 // - arithmetic operations
12 //
13 // \author Peter Vanroose, K.U.Leuven, ESAT/VISICS
14 // \date 15 nov. 1997
15 //
16 //\verbatim
17 // Modifications:
18 // 250198 AWF Templated.
19 // 250198 AWF Modified to make POD struct until gcc inlines when debugging.
20 // 160298 PCP Removed underscore from public members.
21 // 290798 AWF Member templates for fancy compilers
22 // 220598 PVr moved instantiations files to Templates subdirectory.
23 // 050598 PVr added several operators ( + += - -= (T) ).
24 // 140898 David Capel added clamping functions to ensure 0-255 range on bytes and vil_rgb<byte>
25 // 090600 David Capel made clamping functions inline and removed all that partial specialization nonsense from the .hxx file.
26 // Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line
27 //\endverbatim
28 
29 #include <iostream>
30 #ifdef _MSC_VER
31 # include <vcl_msvc_warnings.h>
32 #endif
33 
34 //: This is the appropriate pixel type for 24-bit colour images.
35 //
36 // Currently also includes the following `utilities':
37 // - conversion to ubyte (luminance of vil_rgb: weights (0.299,0.587,0.114)).
38 // - min and max of vil_rgbcell values, useful for morphological operations.
39 // - arithmetic operations
40 template <class T>
41 struct vil_rgb
42 {
43  typedef T value_type;
44 
45  inline vil_rgb() = default;
46 
47  //:Create grey (v,v,v) vil_rgb cell from value v.
48  // This provides a conversion from T to vil_rgb<T>
49 
50  inline vil_rgb(T v):
51  r(v), g(v), b(v) {}
52 
53  //: Construct a vil_rgb value.
54  inline vil_rgb(T red, T green, T blue):
55  r(red), g(green), b(blue) {}
56 
57  // The rgb values
58  T r, g, b;
59  inline T R() const { return r; }
60  inline T G() const { return g; }
61  inline T B() const { return b; }
62 
63  //:Convert vil_rgb to gray using standard (.299, .587, .114) weighting.
64  inline T grey() const { return T(r*0.299+0.587*g+0.114*b); }
65 
66 #if 0 // deprecated -- use .grey() instead
67  inline operator T() const { return T(0.5+r*0.299+0.587*g+0.114*b); }
68 #endif
69 
70  //: equality
71  inline bool operator== (vil_rgb<T> const& o) const { return r==o.r && g==o.g && b==o.b; }
72 
73  // operators
74  inline vil_rgb<T> operator+ (vil_rgb<T> const& A) const { return vil_rgb<T>(r+A.r,g+A.g,b+A.b); }
75  inline vil_rgb<T> operator- (vil_rgb<T> const& A) const { return vil_rgb<T>(r-A.r,g-A.g,b-A.b); }
76  inline vil_rgb<T> operator/ (vil_rgb<T> const& A) const { return vil_rgb<T>(r/A.r,g/A.g,b/A.b);}
77  inline vil_rgb<T>& operator+= (vil_rgb<T> const& A) { r+=A.r,g+=A.g,b+=A.b; return *this; }
78  inline vil_rgb<T>& operator-= (vil_rgb<T> const& A) { r-=A.r,g-=A.g,b-=A.b; return *this; }
79  inline vil_rgb<T> operator* (T A) const { return vil_rgb<T>(r*A,g*A,b*A); }
80  inline vil_rgb<T> operator/ (T A) const { return vil_rgb<T>(r/A,g/A,b/A); }
81  inline vil_rgb<T>& operator*= (T A) { r*=A,g*=A,b*=A; return *this; }
82  inline vil_rgb<T>& operator/= (T A) { r/=A,g/=A,b/=A; return *this; }
83 
84  template <class S> inline
85  vil_rgb(vil_rgb<S> const& that):
86  r(T(that.r)),
87  g(T(that.g)),
88  b(T(that.b)) { }
89  template <class S> inline
91  r=T(that.r);
92  g=T(that.g);
93  b=T(that.b);
94  return *this;
95  }
96 };
97 
98 template <class T>
99 inline
100 std::ostream& operator<<(std::ostream& s, vil_rgb<T> const& rgb)
101 {
102  return s << '[' << rgb.r << ' ' << rgb.g << ' ' << rgb.b << ']';
103 }
104 
105 template <>
106 std::ostream& operator<<(std::ostream& s, vil_rgb<unsigned char> const& rgb);
107 
108 
109 // ** Arithmetic operators
110 
111 template <class T>
112 inline
113 bool operator!= (vil_rgb<T> const& a, vil_rgb<T> const& b)
114 {
115  return !(a==b);
116 }
117 
118 template <class T>
119 inline
121 {
122  return vil_rgb<T>((a.r + b.r)/2, (a.g + b.g)/2, (a.b + b.b)/2);
123 }
124 
125 template <class T>
126 inline
128 {
129  return vil_rgb<T>(a.r + b.r, a.g + b.g, a.b + b.b);
130 }
131 
132 template <class T>
133 inline
135 {
136  return vil_rgb<T>(a.r * b.r, a.g * b.g, a.b * b.b);
137 }
138 
139 template <class T>
140 inline
142 {
143  return vil_rgb<double>(a.r * b, a.g * b, a.b * b);
144 }
145 
146 template <class T>
147 inline
149 {
150  return vil_rgb<double>(a.r * b, a.g * b, a.b * b);
151 }
152 
153 template <class T>
154 inline
156 {
157  return vil_rgb<double>(a.r / b, a.g / b, a.b / b);
158 }
159 
160 #define VIL_RGB_INSTANTIATE(T) \
161 extern "you must include vil/vil_rgb.hxx first."
162 #define VIL_RGB_INSTANTIATE_LS(T) \
163 extern "you must include vil/vil_rgb.hxx first."
164 
165 #endif // vil_rgb_h_
vil_rgb< T > & operator+=(vil_rgb< T > const &A)
Definition: vil_rgb.h:77
vil_rgb< double > operator/(vil_rgb< T > const &a, double b)
Definition: vil_rgb.h:155
vil_rgb(T v)
Create grey (v,v,v) vil_rgb cell from value v.
Definition: vil_rgb.h:50
vil_rgb< T > & operator-=(vil_rgb< T > const &A)
Definition: vil_rgb.h:78
vil_rgb< T > operator-(vil_rgb< T > const &A) const
Definition: vil_rgb.h:75
vil_rgb< T > & operator/=(T A)
Definition: vil_rgb.h:82
vil_rgb< T > average(vil_rgb< T > const &a, vil_rgb< T > const &b)
Definition: vil_rgb.h:120
vil_rgb()=default
T grey() const
Convert vil_rgb to gray using standard (.299, .587, .114) weighting.
Definition: vil_rgb.h:64
vil_rgb< T > operator *(T A) const
Definition: vil_rgb.h:79
vil_rgb< T > operator *(vil_rgb< T > const &a, vil_rgb< T > const &b)
Definition: vil_rgb.h:134
vil_rgb< T > operator/(vil_rgb< T > const &A) const
Definition: vil_rgb.h:76
vil_rgb(vil_rgb< S > const &that)
Definition: vil_rgb.h:85
vil_rgb(T red, T green, T blue)
Construct a vil_rgb value.
Definition: vil_rgb.h:54
T value_type
Definition: vil_rgb.h:43
bool operator==(vil_rgb< T > const &o) const
equality.
Definition: vil_rgb.h:71
T g
Definition: vil_rgb.h:58
#define v
T B() const
Definition: vil_rgb.h:61
T b
Definition: vil_rgb.h:58
T R() const
Definition: vil_rgb.h:59
T r
Definition: vil_rgb.h:58
vil_rgb< T > operator+(vil_rgb< T > const &a, vil_rgb< T > const &b)
Definition: vil_rgb.h:127
bool operator!=(vil_rgb< T > const &a, vil_rgb< T > const &b)
Definition: vil_rgb.h:113
T G() const
Definition: vil_rgb.h:60
vil_rgb< T > & operator *=(T A)
Definition: vil_rgb.h:81
This is the appropriate pixel type for 24-bit colour images.
Definition: vil_fwd.h:14
std::ostream & operator<<(std::ostream &s, vil_rgb< T > const &rgb)
Definition: vil_rgb.h:100
vil_rgb< T > operator+(vil_rgb< T > const &A) const
Definition: vil_rgb.h:74
vil_rgb< T > & operator=(vil_rgb< S > const &that)
Definition: vil_rgb.h:90