ITK  4.6.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< typename TRGBPixelValueType >
87  RGBPixel(const ComponentType r[3]):BaseArray(r) {}
89 
91  template< typename 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 >( std::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 std::ostream & operator<<(std::ostream & os,
170  const RGBPixel< TComponent > & c);
171 
172 template< typename TComponent >
173 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
TComponent ComponentType
Definition: itkRGBPixel.h:75
const ComponentType & GetBlue(void) const
Definition: itkRGBPixel.h:162
FixedArray< TComponent, 3 > BaseArray
Definition: itkRGBPixel.h:66
reference operator[](short index)
std::istream & operator>>(std::istream &is, Point< T, NPointDimension > &v)
const Self & operator+=(const Self &vec)
RGBPixel(const ComponentType r[3])
Definition: itkRGBPixel.h:87
const ComponentType & GetGreen(void) const
Definition: itkRGBPixel.h:159
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:184
RGBPixel Self
Definition: itkRGBPixel.h:62
NumericTraits< ComponentType >::RealType LuminanceType
Definition: itkRGBPixel.h:76
void Set(ComponentType red, ComponentType green, ComponentType blue)
Definition: itkRGBPixel.h:147
RGBPixel(const RGBPixel< TRGBPixelValueType > &r)
Definition: itkRGBPixel.h:86
void Fill(const ValueType &)
void SetNthComponent(int c, const ComponentType &v)
Definition: itkRGBPixel.h:135
Self operator-(const Self &vec) const
Simulate a standard C array with copy semnatics.
Definition: itkFixedArray.h:50
LuminanceType GetLuminance(void) const
const Self & operator-=(const Self &vec)
bool operator==(const Self &vec) const
static const unsigned int Dimension
Definition: itkRGBPixel.h:69
FixedArray< TComponent, 3 > Superclass
Definition: itkRGBPixel.h:63
ComponentType GetScalarValue() const
Definition: itkRGBPixel.h:123
static const unsigned int Length
Definition: itkRGBPixel.h:72
Self operator*(const ComponentType &f) const
FixedArray & operator=(const FixedArray< TFixedArrayValueType, VLength > &r)
Self operator+(const Self &vec) const
RGBPixel(const ComponentType &r)
Definition: itkRGBPixel.h:82
Represent Red, Green and Blue components for color images.
Definition: itkRGBPixel.h:58
Self & operator=(const RGBPixel< TRGBPixelValueType > &r)
Definition: itkRGBPixel.h:92
ComponentType GetNthComponent(int c) const
Definition: itkRGBPixel.h:120
const ComponentType & GetRed(void) const
Definition: itkRGBPixel.h:156
bool operator<(const Self &vec) const
void SetGreen(ComponentType green)
Definition: itkRGBPixel.h:141
static unsigned int GetNumberOfComponents()
Definition: itkRGBPixel.h:117
void SetRed(ComponentType red)
Definition: itkRGBPixel.h:138
void SetBlue(ComponentType blue)
Definition: itkRGBPixel.h:144