ITK  4.2.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 "vnl/vnl_math.h"
29 
30 namespace itk
31 {
58 template< typename TComponent = unsigned short >
59 class RGBAPixel:public FixedArray< TComponent, 4 >
60 {
61 public:
63  typedef RGBAPixel Self;
65 
67  itkStaticConstMacro(Dimension, unsigned int, 4);
68 
70  itkStaticConstMacro(Length, unsigned int, 4);
71 
74 
76  typedef TComponent ComponentType;
78 
80  RGBAPixel() { this->Fill(0); }
81  RGBAPixel (const ComponentType & r) { this->Fill(r); }
83 
85  template< class TRGBAPixelValueType >
87  RGBAPixel(const ComponentType r[4]):BaseArray(r) {}
89 
91  RGBAPixel & operator=(const Self & r);
92 
93  RGBAPixel & operator=(const ComponentType r[4]);
94 
96  Self operator+(const Self & vec) const;
97 
98  Self operator-(const Self & vec) const;
99 
100  const Self & operator+=(const Self & vec);
101 
102  const Self & operator-=(const Self & vec);
103 
104  Self operator *(const ComponentType & f) const;
105 
106  bool operator==(const Self & vec) const;
107 
109  static unsigned int GetNumberOfComponents() { return 4; }
110 
112  ComponentType GetNthComponent(int c) const { return this->operator[](c); }
113 
116  {
117  return static_cast< ComponentType >( vcl_sqrt(
118  static_cast< double >( this->operator[](0) )
119  * static_cast< double >( this->operator[](0) )
120  + static_cast< double >( this->operator[](1) )
121  * static_cast< double >( this->operator[](1) )
122  + static_cast< double >( this->operator[](2) )
123  * static_cast< double >( this->operator[](2) ) ) );
124  }
125 
127  void SetNthComponent(int c, const ComponentType & v) { this->operator[](c) = v; }
128 
130  void SetRed(ComponentType red) { this->operator[](0) = red; }
131 
133  void SetGreen(ComponentType green) { this->operator[](1) = green; }
134 
136  void SetBlue(ComponentType blue) { this->operator[](2) = blue; }
137 
139  void SetAlpha(ComponentType alpha) { this->operator[](3) = alpha; }
140 
143  {
144  this->operator[](0) = red;
145  this->operator[](1) = green;
146  this->operator[](2) = blue;
147  this->operator[](3) = alpha;
148  }
150 
152  const ComponentType & GetRed(void) const { return this->operator[](0); }
153 
155  const ComponentType & GetGreen(void) const { return this->operator[](1); }
156 
158  const ComponentType & GetBlue(void) const { return this->operator[](2); }
159 
161  const ComponentType & GetAlpha(void) const { return this->operator[](3); }
162 
164  LuminanceType GetLuminance(void) const;
165 };
166 
167 template< typename TComponent >
168 ITK_EXPORT std::ostream & operator<<(std::ostream & os,
169  const RGBAPixel< TComponent > & c);
170 
171 template< typename TComponent >
172 ITK_EXPORT std::istream & operator>>(std::istream & is,
173  RGBAPixel< TComponent > & c);
174 } // end namespace itk
175 
176 // Define instantiation macro for this template.
177 #define ITK_TEMPLATE_RGBAPixel(_, EXPORT, TypeX, TypeY) \
178  namespace itk \
179  { \
180  _( 1 ( class EXPORT RGBAPixel< ITK_TEMPLATE_1 TypeX > ) ) \
181  namespace Templates \
182  { \
183  typedef RGBAPixel< ITK_TEMPLATE_1 TypeX > RGBAPixel##TypeY; \
184  } \
185  }
186 
187 #if ITK_TEMPLATE_EXPLICIT
188 #include "Templates/itkRGBAPixel+-.h"
189 #endif
190 
191 //
192 // Numeric traits must be included after (optionally) including the explicit
193 // instantiations control of this class, in case the implicit instantiation
194 // needs to be disabled.
195 //
196 // NumericTraits must be included before (optionally) including the .hxx file,
197 // in case the .hxx requires to use NumericTraits.
198 //
200 
201 #if ITK_TEMPLATE_TXX
202 #include "itkRGBAPixel.hxx"
203 #endif
204 
205 #endif
206