ITK  4.0.0
Insight Segmentation and Registration Toolkit
itkRGBPixel.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 __itkRGBPixel_h
00019 #define __itkRGBPixel_h
00020 
00021 // Undefine an eventual RGBPixel macro
00022 #ifdef RGBPixel
00023 #undef RGBPixel
00024 #endif
00025 
00026 #include "itkIndent.h"
00027 #include "itkFixedArray.h"
00028 #include "vnl/vnl_math.h"
00029 
00030 namespace itk
00031 {
00057 template< typename TComponent = unsigned short >
00058 class RGBPixel:public FixedArray< TComponent, 3 >
00059 {
00060 public:
00062   typedef RGBPixel                    Self;
00063   typedef FixedArray< TComponent, 3 > Superclass;
00064 
00066   typedef FixedArray< TComponent, 3 > BaseArray;
00067 
00069   itkStaticConstMacro(Dimension, unsigned int, 3);
00070 
00072   itkStaticConstMacro(Length, unsigned int, 3);
00073 
00075   typedef TComponent                     ComponentType;
00076   typedef typename Superclass::ValueType ValueType;
00077 
00079   RGBPixel() {}
00080 
00082   RGBPixel (const ComponentType & r) { this->Fill(r); }
00083 
00085   template< class TRGBPixelValueType >
00086   RGBPixel(const RGBPixel< TRGBPixelValueType > & r):BaseArray(r) {}
00087   RGBPixel(const ComponentType r[3]):BaseArray(r) {}
00089 
00091   template< class TRGBPixelValueType >
00092   Self & operator=(const RGBPixel< TRGBPixelValueType > & r)
00093   {
00094     BaseArray::operator=(r);
00095     return *this;
00096   }
00098 
00099   Self & operator=(const ComponentType r[3]);
00100 
00102   Self operator+(const Self & vec) const;
00103 
00104   Self operator-(const Self & vec) const;
00105 
00106   const Self & operator+=(const Self & vec);
00107 
00108   const Self & operator-=(const Self & vec);
00109 
00110   Self operator *(const ComponentType & f) const;
00111 
00112   bool operator<(const Self & vec) const;
00113 
00114   bool operator==(const Self & vec) const;
00115 
00117   static unsigned int GetNumberOfComponents(){ return 3; }
00118 
00120   ComponentType GetNthComponent(int c) const { return this->operator[](c); }
00121 
00123   ComponentType GetScalarValue() const
00124   {
00125     return static_cast< ComponentType >( vcl_sqrt(
00126                                            static_cast< double >( this->operator[](0) )
00127                                            * static_cast< double >( this->operator[](0) )
00128                                            + static_cast< double >( this->operator[](1) )
00129                                            * static_cast< double >( this->operator[](1) )
00130                                            + static_cast< double >( this->operator[](2) )
00131                                            * static_cast< double >( this->operator[](2) ) ) );
00132   }
00133 
00135   void SetNthComponent(int c, const ComponentType & v) {  this->operator[](c) = v; }
00136 
00138   void SetRed(ComponentType red) { this->operator[](0) = red; }
00139 
00141   void SetGreen(ComponentType green) { this->operator[](1) = green; }
00142 
00144   void SetBlue(ComponentType blue) { this->operator[](2) = blue; }
00145 
00147   void Set(ComponentType red, ComponentType green, ComponentType blue)
00148   {
00149     this->operator[](0) = red;
00150     this->operator[](1) = green;
00151     this->operator[](2) = blue;
00152   }
00154 
00156   const ComponentType & GetRed(void) const { return this->operator[](0); }
00157 
00159   const ComponentType & GetGreen(void) const { return this->operator[](1); }
00160 
00162   const ComponentType & GetBlue(void) const { return this->operator[](2); }
00163 
00165   ComponentType GetLuminance(void) const;
00166 };
00167 
00168 template< typename TComponent  >
00169 ITK_EXPORT std::ostream & operator<<(std::ostream & os,
00170                                      const RGBPixel< TComponent > & c);
00171 
00172 template< typename TComponent  >
00173 ITK_EXPORT std::istream & operator>>(std::istream & is,
00174                                      RGBPixel< TComponent > & c);
00175 } // end namespace itk
00176 
00177 // Define instantiation macro for this template.
00178 #define ITK_TEMPLATE_RGBPixel(_, EXPORT, TypeX, TypeY)        \
00179   namespace itk                                               \
00180   {                                                           \
00181   _( 1 ( class EXPORT RGBPixel< ITK_TEMPLATE_1 TypeX > ) )    \
00182   namespace Templates                                         \
00183   {                                                           \
00184   typedef RGBPixel< ITK_TEMPLATE_1 TypeX > RGBPixel##TypeY; \
00185   }                                                           \
00186   }
00187 
00188 #if ITK_TEMPLATE_EXPLICIT
00189 #include "Templates/itkRGBPixel+-.h"
00190 #endif
00191 
00192 //
00193 // Numeric traits must be included after (optionally) including the explicit
00194 // instantiations control of this class, in case the implicit instantiation
00195 // needs to be disabled.
00196 //
00197 // NumericTraits must be included before (optionally) including the .hxx file,
00198 // in case the .hxx requires to use NumericTraits.
00199 //
00200 #include "itkNumericTraitsRGBPixel.h"
00201 
00202 #if ITK_TEMPLATE_TXX
00203 #include "itkRGBPixel.hxx"
00204 #endif
00205 
00206 #endif
00207