ITK  5.1.0
Insight Segmentation and Registration Toolkit
itkRGBAPixel.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 itkRGBAPixel_h
19 #define itkRGBAPixel_h
20 
21 // Undefine an eventual RGBAPixel macro
22 #ifdef RGBAPixel
23 #undef RGBAPixel
24 #endif
25 
26 #include "itkIndent.h"
27 #include "itkFixedArray.h"
28 #include "itkMath.h"
29 
30 namespace itk
31 {
58 template< typename TComponent = unsigned short >
59 class ITK_TEMPLATE_EXPORT RGBAPixel:public FixedArray< TComponent, 4 >
60 {
61 public:
63  using Self = RGBAPixel;
65 
67  static constexpr unsigned int Dimension = 4;
68 
70  static constexpr unsigned int Length = 4;
71 
74 
76  using ComponentType = TComponent;
78 
80  RGBAPixel() { this->Fill(0); }
81  RGBAPixel(const RGBAPixel &) = default;
82  RGBAPixel & operator=(const RGBAPixel &) = default;
83  RGBAPixel( RGBAPixel &&) = default;
84  RGBAPixel & operator=(RGBAPixel &&) = default;
85  ~RGBAPixel() = default;
87 
89  template< typename TRGBAPixelValueType >
91  RGBAPixel(const ComponentType r[4]):BaseArray(r) {}
92  RGBAPixel(const ComponentType & r) { this->Fill(r); }
94 
96  RGBAPixel & operator=(const ComponentType r[4]);
97 
99  Self operator+(const Self & vec) const;
100  Self operator-(const Self & vec) const;
101  Self operator *(const ComponentType & f) const;
102  Self operator /(const ComponentType & f) const;
104 
106  const Self & operator+=(const Self & vec);
107  const Self & operator-=(const Self & vec);
108  const Self & operator*=(const ComponentType & f);
109  const Self & operator/=(const ComponentType & f);
111 
113  bool operator<(const Self & vec) const;
114 
115  bool operator==(const Self & vec) const;
116 
118  static unsigned int GetNumberOfComponents() { return 4; }
119 
121  ComponentType GetNthComponent(int c) const { return this->operator[](c); }
122 
125  {
126  return static_cast< ComponentType >( std::sqrt(
127  static_cast< double >( this->operator[](0) )
128  * static_cast< double >( this->operator[](0) )
129  + static_cast< double >( this->operator[](1) )
130  * static_cast< double >( this->operator[](1) )
131  + static_cast< double >( this->operator[](2) )
132  * static_cast< double >( this->operator[](2) ) ) );
133  }
134 
136  void SetNthComponent(int c, const ComponentType & v) { this->operator[](c) = v; }
137 
139  void SetRed(ComponentType red) { this->operator[](0) = red; }
140 
142  void SetGreen(ComponentType green) { this->operator[](1) = green; }
143 
145  void SetBlue(ComponentType blue) { this->operator[](2) = blue; }
146 
148  void SetAlpha(ComponentType alpha) { this->operator[](3) = alpha; }
149 
152  {
153  this->operator[](0) = red;
154  this->operator[](1) = green;
155  this->operator[](2) = blue;
156  this->operator[](3) = alpha;
157  }
159 
161  const ComponentType & GetRed() const { return this->operator[](0); }
162 
164  const ComponentType & GetGreen() const { return this->operator[](1); }
165 
167  const ComponentType & GetBlue() const { return this->operator[](2); }
168 
170  const ComponentType & GetAlpha() const { return this->operator[](3); }
171 
173  LuminanceType GetLuminance() const;
174 };
175 
176 template< typename TComponent >
177 std::ostream & operator<<(std::ostream & os,
178  const RGBAPixel< TComponent > & c);
179 
180 template< typename TComponent >
181 std::istream & operator>>(std::istream & is,
182  RGBAPixel< TComponent > & c);
183 
184 template<typename T>
185 inline void swap( RGBAPixel<T> &a, RGBAPixel<T> &b )
186 {
187  a.swap(b);
188 }
189 
190 } // end namespace itk
191 
192 //
193 // Numeric traits must be included after (optionally) including the explicit
194 // instantiations control of this class, in case the implicit instantiation
195 // needs to be disabled.
196 //
197 // NumericTraits must be included before (optionally) including the .hxx file,
198 // in case the .hxx requires to use NumericTraits.
199 //
201 
202 #ifndef ITK_MANUAL_INSTANTIATION
203 #include "itkRGBAPixel.hxx"
204 #endif
205 
206 #endif
RGBAPixel(const ComponentType &r)
Definition: itkRGBAPixel.h:92
void swap(FixedArray &other)
void Set(ComponentType red, ComponentType green, ComponentType blue, ComponentType alpha)
Definition: itkRGBAPixel.h:151
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:483
RGBAPixel(const ComponentType r[4])
Definition: itkRGBAPixel.h:91
Represent Red, Green, Blue and Alpha components for color images.
Definition: itkRGBAPixel.h:59
std::istream & operator>>(std::istream &is, Point< T, NPointDimension > &v)
const ComponentType & GetAlpha() const
Definition: itkRGBAPixel.h:170
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:188
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
void SetRed(ComponentType red)
Definition: itkRGBAPixel.h:139
const ComponentType & GetRed() const
Definition: itkRGBAPixel.h:161
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:52
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage > ::OffsetType &ind)
const ComponentType & GetGreen() const
Definition: itkRGBAPixel.h:164
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:211
CovariantVector< T, NVectorDimension > operator *(const T &scalar, const CovariantVector< T, NVectorDimension > &v)
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
void SetGreen(ComponentType green)
Definition: itkRGBAPixel.h:142
void SetBlue(ComponentType blue)
Definition: itkRGBAPixel.h:145
typename NumericTraits< ComponentType >::RealType LuminanceType
Definition: itkRGBAPixel.h:77
void SetAlpha(ComponentType alpha)
Definition: itkRGBAPixel.h:148
bool operator<(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:497
static unsigned int GetNumberOfComponents()
Definition: itkRGBAPixel.h:118
RGBAPixel(const RGBAPixel< TRGBAPixelValueType > &r)
Definition: itkRGBAPixel.h:90
ComponentType GetScalarValue() const
Definition: itkRGBAPixel.h:124
void SetNthComponent(int c, const ComponentType &v)
Definition: itkRGBAPixel.h:136
const ComponentType & GetBlue() const
Definition: itkRGBAPixel.h:167
ComponentType GetNthComponent(int c) const
Definition: itkRGBAPixel.h:121