ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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
30namespace itk
31{
56
57template <typename TComponent = unsigned short>
58class ITK_TEMPLATE_EXPORT RGBPixel : public FixedArray<TComponent, 3>
59{
60public:
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)
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
107 template <typename TRGBPixelValueType>
112 : BaseArray(r)
113 {}
114
115
118 template <typename TRGBPixelValueType>
119 Self &
121 {
123 return *this;
124 }
125
126
127 Self &
129
132 Self
133 operator+(const Self & r) const;
134 Self
135 operator-(const Self & r) const;
136 Self
137 operator*(const ComponentType & r) const;
138 Self
139 operator/(const ComponentType & r) const;
141
144 const Self &
145 operator+=(const Self & r);
146 const Self &
147 operator-=(const Self & r);
148 const Self &
150 const Self &
153
155 bool
156 operator<(const Self & r) const;
157
158 bool
159 operator==(const Self & r) const;
160
162 static unsigned int
164 {
165 return 3;
166 }
167
169 ComponentType
170 GetNthComponent(int c) const
171 {
172 return this->operator[](c);
173 }
174
176 ComponentType
178 {
179 return static_cast<ComponentType>(
180 std::sqrt(static_cast<double>(this->operator[](0)) * static_cast<double>(this->operator[](0)) +
181 static_cast<double>(this->operator[](1)) * static_cast<double>(this->operator[](1)) +
182 static_cast<double>(this->operator[](2)) * static_cast<double>(this->operator[](2))));
183 }
184
186 void
188 {
189 this->operator[](c) = v;
190 }
191
193 void
195 {
196 this->operator[](0) = red;
197 }
198
200 void
202 {
203 this->operator[](1) = green;
204 }
205
207 void
209 {
210 this->operator[](2) = blue;
211 }
212
214 void
216 {
217 this->operator[](0) = red;
218 this->operator[](1) = green;
219 this->operator[](2) = blue;
220 }
221
223 const ComponentType &
224 GetRed() const
225 {
226 return this->operator[](0);
227 }
228
230 const ComponentType &
231 GetGreen() const
232 {
233 return this->operator[](1);
234 }
235
237 const ComponentType &
238 GetBlue() const
239 {
240 return this->operator[](2);
241 }
242
244 LuminanceType
246};
247
248template <typename TComponent>
249std::ostream &
250operator<<(std::ostream & os, const RGBPixel<TComponent> & c);
251
252template <typename TComponent>
253std::istream &
254operator>>(std::istream & is, RGBPixel<TComponent> & c);
255
256template <typename T>
257inline void
258swap(RGBPixel<T> & a, RGBPixel<T> & b) noexcept
259{
260 a.swap(b);
261}
262
263} // end namespace itk
264
265//
266// Numeric traits must be included after (optionally) including the explicit
267// instantiations control of this class, in case the implicit instantiation
268// needs to be disabled.
269//
270// NumericTraits must be included before (optionally) including the .hxx file,
271// in case the .hxx requires to use NumericTraits.
272//
274
275
276#ifndef ITK_MANUAL_INSTANTIATION
277# include "itkRGBPixel.hxx"
278#endif
279
280#endif
FixedArray & operator=(const FixedArray< TFixedArrayValueType, VLength > &r)
ITK_GCC_PRAGMA_PUSH ITK_GCC_SUPPRESS_Warray_bounds constexpr reference operator[](unsigned int index)
void Fill(const ValueType &)
Represent Red, Green and Blue components for color images.
Definition itkRGBPixel.h:59
ComponentType GetNthComponent(int c) const
static unsigned int GetNumberOfComponents()
void Set(ComponentType red, ComponentType green, ComponentType blue)
Self operator*(const ComponentType &r) const
Self & operator=(const RGBPixel< TRGBPixelValueType > &r)
RGBPixel(const RGBPixel< TRGBPixelValueType > &r)
const ComponentType & GetBlue() const
const Self & operator-=(const Self &r)
RGBPixel(const ComponentType &r)
Definition itkRGBPixel.h:94
LuminanceType GetLuminance() const
void SetBlue(ComponentType blue)
FixedArray< T, 3 > Superclass
Definition itkRGBPixel.h:63
void SetRed(ComponentType red)
typename NumericTraits< ComponentType >::RealType LuminanceType
Definition itkRGBPixel.h:76
RGBPixel(std::nullptr_t)=delete
Self operator-(const Self &r) const
Self operator+(const Self &r) const
RGBPixel(const ComponentType r[3])
bool operator<(const Self &r) const
const Self & operator*=(const ComponentType &r)
const Self & operator/=(const ComponentType &r)
Self & operator=(const ComponentType r[3])
const ComponentType & GetGreen() const
ComponentType GetScalarValue() const
static constexpr unsigned int Length
Definition itkRGBPixel.h:72
void SetGreen(ComponentType green)
const Self & operator+=(const Self &r)
void SetNthComponent(int c, const ComponentType &v)
Self operator/(const ComponentType &r) const
bool operator==(const Self &r) const
const ComponentType & GetRed() const
RGBPixel()=default
FixedArray< T, 3 > BaseArray
Definition itkRGBPixel.h:66
static constexpr unsigned int Dimension
Definition itkRGBPixel.h:69
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
void swap(Array< T > &a, Array< T > &b) noexcept
Definition itkArray.h:247
std::istream & operator>>(std::istream &is, Point< T, VPointDimension > &vct)
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)