ITK  4.3.0
Insight Segmentation and Registration Toolkit
itkRGBPixel.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkRGBPixel_h
19 #define __itkRGBPixel_h
20 
21 // Undefine an eventual RGBPixel macro
22 #ifdef RGBPixel
23 #undef RGBPixel
24 #endif
25 
26 #include "itkIndent.h"
27 #include "itkFixedArray.h"
28 #include "vnl/vnl_math.h"
29 
30 namespace itk
31 {
57 template< typename TComponent = unsigned short >
58 class RGBPixel:public FixedArray< TComponent, 3 >
59 {
60 public:
62  typedef RGBPixel Self;
64 
67 
69  itkStaticConstMacro(Dimension, unsigned int, 3);
70 
72  itkStaticConstMacro(Length, unsigned int, 3);
73 
75  typedef TComponent ComponentType;
77 
79  RGBPixel() {}
80 
82  RGBPixel (const ComponentType & r) { this->Fill(r); }
83 
85  template< class TRGBPixelValueType >
87  RGBPixel(const ComponentType r[3]):BaseArray(r) {}
89 
91  template< class TRGBPixelValueType >
93  {
95  return *this;
96  }
98 
99  Self & operator=(const ComponentType r[3]);
100 
102  Self operator+(const Self & vec) const;
103 
104  Self operator-(const Self & vec) const;
105 
106  const Self & operator+=(const Self & vec);
107 
108  const Self & operator-=(const Self & vec);
109 
110  Self operator *(const ComponentType & f) const;
111 
112  bool operator<(const Self & vec) const;
113 
114  bool operator==(const Self & vec) const;
115 
117  static unsigned int GetNumberOfComponents(){ return 3; }
118 
120  ComponentType GetNthComponent(int c) const { return this->operator[](c); }
121 
124  {
125  return static_cast< ComponentType >( vcl_sqrt(
126  static_cast< double >( this->operator[](0) )
127  * static_cast< double >( this->operator[](0) )
128  + static_cast< double >( this->operator[](1) )
129  * static_cast< double >( this->operator[](1) )
130  + static_cast< double >( this->operator[](2) )
131  * static_cast< double >( this->operator[](2) ) ) );
132  }
133 
135  void SetNthComponent(int c, const ComponentType & v) { this->operator[](c) = v; }
136 
138  void SetRed(ComponentType red) { this->operator[](0) = red; }
139 
141  void SetGreen(ComponentType green) { this->operator[](1) = green; }
142 
144  void SetBlue(ComponentType blue) { this->operator[](2) = blue; }
145 
148  {
149  this->operator[](0) = red;
150  this->operator[](1) = green;
151  this->operator[](2) = blue;
152  }
154 
156  const ComponentType & GetRed(void) const { return this->operator[](0); }
157 
159  const ComponentType & GetGreen(void) const { return this->operator[](1); }
160 
162  const ComponentType & GetBlue(void) const { return this->operator[](2); }
163 
165  LuminanceType GetLuminance(void) const;
166 };
167 
168 template< typename TComponent >
169 ITK_EXPORT std::ostream & operator<<(std::ostream & os,
170  const RGBPixel< TComponent > & c);
171 
172 template< typename TComponent >
173 ITK_EXPORT std::istream & operator>>(std::istream & is,
174  RGBPixel< TComponent > & c);
175 } // end namespace itk
176 
177 //
178 // Numeric traits must be included after (optionally) including the explicit
179 // instantiations control of this class, in case the implicit instantiation
180 // needs to be disabled.
181 //
182 // NumericTraits must be included before (optionally) including the .hxx file,
183 // in case the .hxx requires to use NumericTraits.
184 //
186 
187 
188 #ifndef ITK_MANUAL_INSTANTIATION
189 #include "itkRGBPixel.hxx"
190 #endif
191 
192 #endif
193