ITK  5.0.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 "itkMath.h"
29 
30 namespace itk
31 {
57 template< typename TComponent = unsigned short >
58 class ITK_TEMPLATE_EXPORT RGBPixel:public FixedArray< TComponent, 3 >
59 {
60 public:
62  using Self = RGBPixel;
64 
67 
69  static constexpr unsigned int Dimension = 3;
70 
72  static constexpr unsigned int Length = 3;
73 
75  using ComponentType = TComponent;
77 
79  RGBPixel() { this->Fill(0); }
80  RGBPixel(const RGBPixel&) = default;
81  RGBPixel(RGBPixel&&) = default;
82  RGBPixel & operator=(const RGBPixel &) = default;
83  RGBPixel & operator=(RGBPixel &&) = default;
84  ~RGBPixel() = default;
86 
88  RGBPixel (const ComponentType & r) { this->Fill(r); }
89 
91  template< typename TRGBPixelValueType >
93  RGBPixel(const ComponentType r[3]):BaseArray(r) {}
95 
97  template< typename TRGBPixelValueType >
99  {
100  BaseArray::operator=(r);
101  return *this;
102  }
104 
105  Self & operator=(const ComponentType r[3]);
106 
110  Self operator+(const Self & vec) const;
111  Self operator-(const Self & vec) const;
112  Self operator *(const ComponentType & f) const;
113  Self operator /(const ComponentType & f) const;
115 
117  const Self & operator+=(const Self & vec);
118  const Self & operator-=(const Self & vec);
119  const Self & operator*=(const ComponentType & f);
120  const Self & operator/=(const ComponentType & f);
122 
124  bool operator<(const Self & vec) const;
125 
126  bool operator==(const Self & vec) const;
127 
129  static unsigned int GetNumberOfComponents(){ return 3; }
130 
132  ComponentType GetNthComponent(int c) const { return this->operator[](c); }
133 
136  {
137  return static_cast< ComponentType >( std::sqrt(
138  static_cast< double >( this->operator[](0) )
139  * static_cast< double >( this->operator[](0) )
140  + static_cast< double >( this->operator[](1) )
141  * static_cast< double >( this->operator[](1) )
142  + static_cast< double >( this->operator[](2) )
143  * static_cast< double >( this->operator[](2) ) ) );
144  }
145 
147  void SetNthComponent(int c, const ComponentType & v) { this->operator[](c) = v; }
148 
150  void SetRed(ComponentType red) { this->operator[](0) = red; }
151 
153  void SetGreen(ComponentType green) { this->operator[](1) = green; }
154 
156  void SetBlue(ComponentType blue) { this->operator[](2) = blue; }
157 
160  {
161  this->operator[](0) = red;
162  this->operator[](1) = green;
163  this->operator[](2) = blue;
164  }
166 
168  const ComponentType & GetRed() const { return this->operator[](0); }
169 
171  const ComponentType & GetGreen() const { return this->operator[](1); }
172 
174  const ComponentType & GetBlue() const { return this->operator[](2); }
175 
177  LuminanceType GetLuminance() const;
178 };
179 
180 template< typename TComponent >
181 std::ostream & operator<<(std::ostream & os, const RGBPixel< TComponent > & c);
182 
183 template< typename TComponent >
184 std::istream & operator>>(std::istream & is, RGBPixel< TComponent > & c);
185 
186 template<typename T>
187 inline void swap( RGBPixel<T> &a, RGBPixel<T> &b )
188 {
189  a.swap(b);
190 }
191 
192 } // end namespace itk
193 
194 //
195 // Numeric traits must be included after (optionally) including the explicit
196 // instantiations control of this class, in case the implicit instantiation
197 // needs to be disabled.
198 //
199 // NumericTraits must be included before (optionally) including the .hxx file,
200 // in case the .hxx requires to use NumericTraits.
201 //
203 
204 
205 #ifndef ITK_MANUAL_INSTANTIATION
206 #include "itkRGBPixel.hxx"
207 #endif
208 
209 #endif
void swap(FixedArray &other)
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:485
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Define numeric traits for std::vector.
std::istream & operator>>(std::istream &is, Point< T, NPointDimension > &v)
RGBPixel(const ComponentType r[3])
Definition: itkRGBPixel.h:93
CovariantVector< T, NVectorDimension > operator*(const T &scalar, const CovariantVector< T, NVectorDimension > &v)
void Set(ComponentType red, ComponentType green, ComponentType blue)
Definition: itkRGBPixel.h:159
RGBPixel(const RGBPixel< TRGBPixelValueType > &r)
Definition: itkRGBPixel.h:92
void SetNthComponent(int c, const ComponentType &v)
Definition: itkRGBPixel.h:147
Simulate a standard C array with copy semnatics.
Definition: itkFixedArray.h:51
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:211
const ComponentType & GetBlue() const
Definition: itkRGBPixel.h:174
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
const ComponentType & GetGreen() const
Definition: itkRGBPixel.h:171
ComponentType GetScalarValue() const
Definition: itkRGBPixel.h:135
RGBPixel(const ComponentType &r)
Definition: itkRGBPixel.h:88
Represent Red, Green and Blue components for color images.
Definition: itkRGBPixel.h:58
Self & operator=(const RGBPixel< TRGBPixelValueType > &r)
Definition: itkRGBPixel.h:98
ComponentType GetNthComponent(int c) const
Definition: itkRGBPixel.h:132
bool operator<(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:499
void SetGreen(ComponentType green)
Definition: itkRGBPixel.h:153
const ComponentType & GetRed() const
Definition: itkRGBPixel.h:168
static unsigned int GetNumberOfComponents()
Definition: itkRGBPixel.h:129
typename NumericTraits< ComponentType >::RealType LuminanceType
Definition: itkRGBPixel.h:76
void SetRed(ComponentType red)
Definition: itkRGBPixel.h:150
void SetBlue(ComponentType blue)
Definition: itkRGBPixel.h:156