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
30namespace itk
31{
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
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 &
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 &
146 const Self &
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
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
243};
244
245template <typename TComponent>
246std::ostream &
247operator<<(std::ostream & os, const RGBPixel<TComponent> & c);
248
249template <typename TComponent>
250std::istream &
251operator>>(std::istream & is, RGBPixel<TComponent> & c);
252
253template <typename T>
254inline void
255swap(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
Pixel-wise addition of two images.
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:54
Represent Red, Green and Blue components for color images.
Definition: itkRGBPixel.h:59
ComponentType GetNthComponent(int c) const
Definition: itkRGBPixel.h:166
static unsigned int GetNumberOfComponents()
Definition: itkRGBPixel.h:159
void Set(ComponentType red, ComponentType green, ComponentType blue)
Definition: itkRGBPixel.h:211
Self operator*(const ComponentType &r) const
Self & operator=(const RGBPixel< TRGBPixelValueType > &r)
Definition: itkRGBPixel.h:118
RGBPixel(const RGBPixel< TRGBPixelValueType > &r)
Definition: itkRGBPixel.h:107
const ComponentType & GetBlue() const
Definition: itkRGBPixel.h:235
TComponent ComponentType
Definition: itkRGBPixel.h:75
const Self & operator-=(const Self &r)
RGBPixel(const ComponentType &r)
Definition: itkRGBPixel.h:94
LuminanceType GetLuminance() const
void SetBlue(ComponentType blue)
Definition: itkRGBPixel.h:204
void SetRed(ComponentType red)
Definition: itkRGBPixel.h:190
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])
Definition: itkRGBPixel.h:110
const Self & operator*=(const ComponentType &r)
const Self & operator/=(const ComponentType &r)
Self & operator=(const ComponentType r[3])
const ComponentType & GetGreen() const
Definition: itkRGBPixel.h:228
ComponentType GetScalarValue() const
Definition: itkRGBPixel.h:173
void SetGreen(ComponentType green)
Definition: itkRGBPixel.h:197
const Self & operator+=(const Self &r)
void SetNthComponent(int c, const ComponentType &v)
Definition: itkRGBPixel.h:183
Self operator/(const ComponentType &r) const
bool operator==(const Self &r) const
const ComponentType & GetRed() const
Definition: itkRGBPixel.h:221
RGBPixel()=default
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
void swap(Array< T > &a, Array< T > &b) noexcept
Definition: itkArray.h:242
std::istream & operator>>(std::istream &is, Point< T, VPointDimension > &vct)
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
bool operator<(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:566