ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkRGBAPixel.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkRGBAPixel_h
00019 #define __itkRGBAPixel_h
00020 
00021 // Undefine an eventual RGBAPixel macro
00022 #ifdef RGBAPixel
00023 #undef RGBAPixel
00024 #endif
00025 
00026 #include "itkIndent.h"
00027 #include "itkFixedArray.h"
00028 #include "vnl/vnl_math.h"
00029 
00030 namespace itk
00031 {
00058 template< typename TComponent = unsigned short >
00059 class RGBAPixel:public FixedArray< TComponent, 4 >
00060 {
00061 public:
00063   typedef RGBAPixel                   Self;
00064   typedef FixedArray< TComponent, 4 > Superclass;
00065 
00067   itkStaticConstMacro(Dimension, unsigned int, 4);
00068 
00070   itkStaticConstMacro(Length, unsigned int, 4);
00071 
00073   typedef FixedArray< TComponent, 4 > BaseArray;
00074 
00076   typedef TComponent ComponentType;
00077 
00079   RGBAPixel() { this->Fill(0); }
00080   RGBAPixel (const ComponentType & r) { this->Fill(r); }
00082 
00084   template< class TRGBAPixelValueType >
00085   RGBAPixel(const RGBAPixel< TRGBAPixelValueType > & r):BaseArray(r) {}
00086   RGBAPixel(const ComponentType r[4]):BaseArray(r) {}
00088 
00090   RGBAPixel & operator=(const Self & r);
00091 
00092   RGBAPixel & operator=(const ComponentType r[4]);
00093 
00095   Self operator+(const Self & vec) const;
00096 
00097   Self operator-(const Self & vec) const;
00098 
00099   const Self & operator+=(const Self & vec);
00100 
00101   const Self & operator-=(const Self & vec);
00102 
00103   Self operator *(const ComponentType & f) const;
00104 
00105   bool operator==(const Self & vec) const;
00106 
00108   static unsigned int GetNumberOfComponents() { return 4; }
00109 
00111   ComponentType GetNthComponent(int c) const { return this->operator[](c); }
00112 
00114   ComponentType GetScalarValue() const
00115   {
00116     return static_cast< ComponentType >( vcl_sqrt(
00117                                            static_cast< double >( this->operator[](0) )
00118                                            * static_cast< double >( this->operator[](0) )
00119                                            + static_cast< double >( this->operator[](1) )
00120                                            * static_cast< double >( this->operator[](1) )
00121                                            + static_cast< double >( this->operator[](2) )
00122                                            * static_cast< double >( this->operator[](2) ) ) );
00123   }
00124 
00126   void SetNthComponent(int c, const ComponentType & v) {  this->operator[](c) = v; }
00127 
00129   void SetRed(ComponentType red) { this->operator[](0) = red; }
00130 
00132   void SetGreen(ComponentType green) { this->operator[](1) = green; }
00133 
00135   void SetBlue(ComponentType blue) { this->operator[](2) = blue; }
00136 
00138   void SetAlpha(ComponentType alpha) { this->operator[](3) = alpha; }
00139 
00141   void Set(ComponentType red, ComponentType green, ComponentType blue, ComponentType alpha)
00142   {
00143     this->operator[](0) = red;
00144     this->operator[](1) = green;
00145     this->operator[](2) = blue;
00146     this->operator[](3) = alpha;
00147   }
00149 
00151   const ComponentType & GetRed(void) const { return this->operator[](0); }
00152 
00154   const ComponentType & GetGreen(void) const { return this->operator[](1); }
00155 
00157   const ComponentType & GetBlue(void) const { return this->operator[](2); }
00158 
00160   const ComponentType & GetAlpha(void) const { return this->operator[](3); }
00161 
00163   ComponentType GetLuminance(void) const;
00164 };
00165 
00166 template< typename TComponent  >
00167 ITK_EXPORT std::ostream & operator<<(std::ostream & os,
00168                                      const RGBAPixel< TComponent > & c);
00169 
00170 template< typename TComponent  >
00171 ITK_EXPORT std::istream & operator>>(std::istream & is,
00172                                      RGBAPixel< TComponent > & c);
00173 } // end namespace itk
00174 
00175 // Define instantiation macro for this template.
00176 #define ITK_TEMPLATE_RGBAPixel(_, EXPORT, TypeX, TypeY)         \
00177   namespace itk                                                 \
00178   {                                                             \
00179   _( 1 ( class EXPORT RGBAPixel< ITK_TEMPLATE_1 TypeX > ) )     \
00180   namespace Templates                                           \
00181   {                                                             \
00182   typedef RGBAPixel< ITK_TEMPLATE_1 TypeX > RGBAPixel##TypeY; \
00183   }                                                             \
00184   }
00185 
00186 #if ITK_TEMPLATE_EXPLICIT
00187 #include "Templates/itkRGBAPixel+-.h"
00188 #endif
00189 
00190 //
00191 // Numeric traits must be included after (optionally) including the explicit
00192 // instantiations control of this class, in case the implicit instantiation
00193 // needs to be disabled.
00194 //
00195 // NumericTraits must be included before (optionally) including the .hxx file,
00196 // in case the .hxx requires to use NumericTraits.
00197 //
00198 #include "itkNumericTraitsRGBAPixel.h"
00199 
00200 #if ITK_TEMPLATE_TXX
00201 #include "itkRGBAPixel.hxx"
00202 #endif
00203 
00204 #endif
00205