ITK  6.0.0
Insight Toolkit
itkRGBPixel.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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  * https://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 
80 #ifdef ITK_FUTURE_LEGACY_REMOVE
81  RGBPixel() = default;
82 #else
83  constexpr RGBPixel()
85  {
86  // `: Superclass(Superclass())` is a workaround for an old compiler bug. A simple `: Superclass()` triggered
87  // warnings from GCC 9.4.0 saying: "warning: '<anonymous>' may be used uninitialized in this function
88  // [-Wmaybe-uninitialized]".
89  }
90 #endif
91 
92 #if defined(ITK_LEGACY_REMOVE)
93 
94  explicit RGBPixel(const ComponentType & r) { this->Fill(r); }
95 
97  RGBPixel(std::nullptr_t) = delete;
98 #else
99 
102  RGBPixel(const ComponentType & r) { this->Fill(r); }
103 #endif
104 
106  template <typename TRGBPixelValueType>
108  : BaseArray(r)
109  {}
111  : BaseArray(r)
112  {}
116  template <typename TRGBPixelValueType>
117  Self &
119  {
120  BaseArray::operator=(r);
121  return *this;
122  }
125  Self &
126  operator=(const ComponentType r[3]);
127 
129  Self
130  operator+(const Self & r) const;
131  Self
132  operator-(const Self & r) const;
133  Self
134  operator*(const ComponentType & r) const;
135  Self
136  operator/(const ComponentType & r) const;
140  const Self &
141  operator+=(const Self & r);
142  const Self &
143  operator-=(const Self & r);
144  const Self &
145  operator*=(const ComponentType & r);
146  const Self &
147  operator/=(const ComponentType & r);
151  bool
152  operator<(const Self & r) const;
153 
154  bool
155  operator==(const Self & r) const;
156 
158  static unsigned int
160  {
161  return 3;
162  }
163 
165  ComponentType
166  GetNthComponent(int c) const
167  {
168  return this->operator[](c);
169  }
170 
172  ComponentType
174  {
175  return static_cast<ComponentType>(
176  std::sqrt(static_cast<double>(this->operator[](0)) * static_cast<double>(this->operator[](0)) +
177  static_cast<double>(this->operator[](1)) * static_cast<double>(this->operator[](1)) +
178  static_cast<double>(this->operator[](2)) * static_cast<double>(this->operator[](2))));
179  }
180 
182  void
183  SetNthComponent(int c, const ComponentType & v)
184  {
185  this->operator[](c) = v;
186  }
187 
189  void
191  {
192  this->operator[](0) = red;
193  }
194 
196  void
198  {
199  this->operator[](1) = green;
200  }
201 
203  void
205  {
206  this->operator[](2) = blue;
207  }
208 
210  void
212  {
213  this->operator[](0) = red;
214  this->operator[](1) = green;
215  this->operator[](2) = blue;
216  }
220  const ComponentType &
221  GetRed() const
222  {
223  return this->operator[](0);
224  }
225 
227  const ComponentType &
228  GetGreen() const
229  {
230  return this->operator[](1);
231  }
232 
234  const ComponentType &
235  GetBlue() const
236  {
237  return this->operator[](2);
238  }
239 
241  LuminanceType
242  GetLuminance() const;
243 };
244 
245 template <typename TComponent>
246 std::ostream &
247 operator<<(std::ostream & os, const RGBPixel<TComponent> & c);
248 
249 template <typename TComponent>
250 std::istream &
251 operator>>(std::istream & is, RGBPixel<TComponent> & c);
252 
253 template <typename T>
254 inline void
255 swap(RGBPixel<T> & a, RGBPixel<T> & b) noexcept
256 {
257  a.swap(b);
258 }
259 
260 } // end namespace itk
261 
262 //
263 // Numeric traits must be included after (optionally) including the explicit
264 // instantiations control of this class, in case the implicit instantiation
265 // needs to be disabled.
266 //
267 // NumericTraits must be included before (optionally) including the .hxx file,
268 // in case the .hxx requires to use NumericTraits.
269 //
271 
272 
273 #ifndef ITK_MANUAL_INSTANTIATION
274 # include "itkRGBPixel.hxx"
275 #endif
276 
277 #endif
itk::RGBPixel::GetNumberOfComponents
static unsigned int GetNumberOfComponents()
Definition: itkRGBPixel.h:159
itk::RGBPixel
Represent Red, Green and Blue components for color images.
Definition: itkRGBPixel.h:58
itk::operator<
bool operator<(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:566
itk::RGBPixel::GetRed
const ComponentType & GetRed() const
Definition: itkRGBPixel.h:221
itk::operator*
CovariantVector< T, VVectorDimension > operator*(const T &scalar, const CovariantVector< T, VVectorDimension > &v)
Definition: itkCovariantVector.h:272
itk::RGBPixel::Set
void Set(ComponentType red, ComponentType green, ComponentType blue)
Definition: itkRGBPixel.h:211
itk::RGBPixel::LuminanceType
typename NumericTraits< ComponentType >::RealType LuminanceType
Definition: itkRGBPixel.h:76
itk::RGBPixel::operator=
Self & operator=(const RGBPixel< TRGBPixelValueType > &r)
Definition: itkRGBPixel.h:118
itk::operator-
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:672
itk::operator<<
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
itk::RGBPixel::RGBPixel
RGBPixel(const ComponentType &r)
Definition: itkRGBPixel.h:94
itk::RGBPixel::GetGreen
const ComponentType & GetGreen() const
Definition: itkRGBPixel.h:228
itk::RGBPixel::GetScalarValue
ComponentType GetScalarValue() const
Definition: itkRGBPixel.h:173
itk::RGBPixel::GetBlue
const ComponentType & GetBlue() const
Definition: itkRGBPixel.h:235
itkNumericTraitsRGBPixel.h
itkIndent.h
itkFixedArray.h
itk::RGBPixel::RGBPixel
RGBPixel(const ComponentType r[3])
Definition: itkRGBPixel.h:110
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:552
itk::FixedArray
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:53
itk::RGBPixel::SetGreen
void SetGreen(ComponentType green)
Definition: itkRGBPixel.h:197
itk::RGBPixel::RGBPixel
RGBPixel(const RGBPixel< TRGBPixelValueType > &r)
Definition: itkRGBPixel.h:107
itk::RGBPixel::SetNthComponent
void SetNthComponent(int c, const ComponentType &v)
Definition: itkRGBPixel.h:183
itk::RGBPixel::GetNthComponent
ComponentType GetNthComponent(int c) const
Definition: itkRGBPixel.h:166
itk::swap
void swap(Array< T > &a, Array< T > &b) noexcept
Definition: itkArray.h:242
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::RGBPixel::ComponentType
TComponent ComponentType
Definition: itkRGBPixel.h:75
itk::operator+
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
Definition: itkConstNeighborhoodIterator.h:654
itk::NumericTraits::RealType
double RealType
Definition: itkNumericTraits.h:86
itk::RGBPixel::SetRed
void SetRed(ComponentType red)
Definition: itkRGBPixel.h:190
AddImageFilter
Definition: itkAddImageFilter.h:81
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44
itk::RGBPixel::SetBlue
void SetBlue(ComponentType blue)
Definition: itkRGBPixel.h:204
itkMath.h
itk::operator>>
std::istream & operator>>(std::istream &is, Point< T, VPointDimension > &vct)