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 "itkFixedArray.h"
27#include "itkMath.h"
28
29namespace itk
30{
55
56template <typename TComponent = unsigned short>
57class ITK_TEMPLATE_EXPORT RGBPixel : public FixedArray<TComponent, 3>
58{
59public:
61 using Self = RGBPixel;
63
66
68 static constexpr unsigned int Dimension = 3;
69
71 static constexpr unsigned int Length = 3;
72
74 using ComponentType = TComponent;
76
79#ifdef ITK_FUTURE_LEGACY_REMOVE
80 RGBPixel() = default;
81#else
82 constexpr RGBPixel()
84 {
85 // `: Superclass(Superclass())` is a workaround for an old compiler bug. A simple `: Superclass()` triggered
86 // warnings from GCC 9.4.0 saying: "warning: '<anonymous>' may be used uninitialized in this function
87 // [-Wmaybe-uninitialized]".
88 }
89#endif
90
91#if defined(ITK_LEGACY_REMOVE)
93 explicit RGBPixel(const ComponentType & r) { this->Fill(r); }
94
96 RGBPixel(std::nullptr_t) = delete;
97#else
98
101 RGBPixel(const ComponentType & r) { this->Fill(r); }
102#endif
103
106 template <typename TRGBPixelValueType>
111 : BaseArray(r)
112 {}
113
114
117 template <typename TRGBPixelValueType>
118 Self &
120 {
122 return *this;
123 }
124
125
126 Self &
128
131 Self
132 operator+(const Self & r) const;
133 Self
134 operator-(const Self & r) const;
135 Self
136 operator*(const ComponentType & r) const;
137 Self
138 operator/(const ComponentType & r) const;
140
143 const Self &
144 operator+=(const Self & r);
145 const Self &
146 operator-=(const Self & r);
147 const Self &
149 const Self &
152
154 bool
155 operator<(const Self & r) const;
156
157 bool
158 operator==(const Self & r) const;
159
161 static unsigned int
163 {
164 return 3;
165 }
166
168 [[nodiscard]] ComponentType
169 GetNthComponent(int c) const
170 {
171 return this->operator[](c);
172 }
173
175 [[nodiscard]] ComponentType
177 {
178 return static_cast<ComponentType>(
179 std::sqrt(static_cast<double>(this->operator[](0)) * static_cast<double>(this->operator[](0)) +
180 static_cast<double>(this->operator[](1)) * static_cast<double>(this->operator[](1)) +
181 static_cast<double>(this->operator[](2)) * static_cast<double>(this->operator[](2))));
182 }
183
185 void
187 {
188 this->operator[](c) = v;
189 }
190
192 void
194 {
195 this->operator[](0) = red;
196 }
197
199 void
201 {
202 this->operator[](1) = green;
203 }
204
206 void
208 {
209 this->operator[](2) = blue;
210 }
211
213 void
215 {
216 this->operator[](0) = red;
217 this->operator[](1) = green;
218 this->operator[](2) = blue;
219 }
220
222 [[nodiscard]] const ComponentType &
223 GetRed() const
224 {
225 return this->operator[](0);
226 }
227
229 [[nodiscard]] const ComponentType &
230 GetGreen() const
231 {
232 return this->operator[](1);
233 }
234
236 [[nodiscard]] const ComponentType &
237 GetBlue() const
238 {
239 return this->operator[](2);
240 }
241
243 [[nodiscard]] LuminanceType
245};
246
247template <typename TComponent>
248std::ostream &
249operator<<(std::ostream & os, const RGBPixel<TComponent> & c);
250
251template <typename TComponent>
252std::istream &
253operator>>(std::istream & is, RGBPixel<TComponent> & c);
254
255template <typename T>
256inline void
257swap(RGBPixel<T> & a, RGBPixel<T> & b) noexcept
258{
259 a.swap(b);
260}
261
262} // end namespace itk
263
264//
265// Numeric traits must be included after (optionally) including the explicit
266// instantiations control of this class, in case the implicit instantiation
267// needs to be disabled.
268//
269// NumericTraits must be included before (optionally) including the .hxx file,
270// in case the .hxx requires to use NumericTraits.
271//
273
274
275#ifndef ITK_MANUAL_INSTANTIATION
276# include "itkRGBPixel.hxx"
277#endif
278
279#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:58
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:93
LuminanceType GetLuminance() const
void SetBlue(ComponentType blue)
FixedArray< T, 3 > Superclass
Definition itkRGBPixel.h:62
void SetRed(ComponentType red)
typename NumericTraits< ComponentType >::RealType LuminanceType
Definition itkRGBPixel.h:75
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:71
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:65
static constexpr unsigned int Dimension
Definition itkRGBPixel.h:68
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, AnatomicalOrientation::CoordinateEnum value)