ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkDefaultConvertPixelTraits.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 __itkDefaultConvertPixelTraits_h
00019 #define __itkDefaultConvertPixelTraits_h
00020 
00021 #include "itkOffset.h"
00022 #include "itkVector.h"
00023 #include "itkMatrix.h"
00024 #include "itkVariableLengthVector.h"
00025 
00026 namespace itk
00027 {
00039 template< typename PixelType >
00040 class DefaultConvertPixelTraits
00041 {
00042 public:
00044   typedef typename PixelType::ComponentType ComponentType;
00045 
00047   static unsigned int GetNumberOfComponents()
00048   { return PixelType::GetNumberOfComponents(); }
00049 
00050   static unsigned int GetNumberOfComponents( const PixelType itkNotUsed(pixel) )
00051   { return PixelType::GetNumberOfComponents( ); }
00052 
00054   static ComponentType GetNthComponent(int c, const PixelType & pixel)
00055   { return pixel.GetNthComponent(c); }
00056 
00058   static void SetNthComponent(int c, PixelType & pixel, const ComponentType & v)
00059   { pixel.SetNthComponent(c, v); }
00060 
00062   static ComponentType GetScalarValue(const PixelType & pixel)
00063   { return pixel.GetScalarValue(); }
00064 };
00066 
00067 #define ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(type)                      \
00068   template< >                                                              \
00069   class DefaultConvertPixelTraits< type >                                  \
00070   {                                                                        \
00071 public:                                                                    \
00072     typedef type ComponentType;                                            \
00073     static unsigned int GetNumberOfComponents()                            \
00074       {                                                                    \
00075       return 1;                                                            \
00076       }                                                                    \
00077     static unsigned int GetNumberOfComponents(const type)                  \
00078       {                                                                    \
00079       return 1;                                                            \
00080       }                                                                    \
00081     static void SetNthComponent(int, type & pixel, const ComponentType &v) \
00082       {                                                                    \
00083       pixel = v;                                                           \
00084       }                                                                    \
00085     static type GetNthComponent(int, const type pixel)                     \
00086       {                                                                    \
00087       return pixel;                                                        \
00088       }                                                                    \
00089     static type GetScalarValue(const type &pixel)                          \
00090       {                                                                    \
00091       return pixel;                                                        \
00092       }                                                                    \
00093   };
00094 
00095 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(float)
00096 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(double)
00097 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(int)
00098 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(char)
00099 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(short)
00100 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(unsigned int)
00101 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(signed char)
00102 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(unsigned char)
00103 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(unsigned short)
00104 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(long)
00105 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(unsigned long)
00106 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(bool)
00107 
00108 #undef ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL
00109 
00110 //
00111 //  Default traits for the Offset<> pixel type
00112 //
00113 
00114 template<unsigned int VDimension>
00115 class DefaultConvertPixelTraits< Offset< VDimension > >
00116 {
00117 public:
00118   typedef Offset< VDimension >                 TargetType;
00119   typedef typename TargetType::OffsetValueType ComponentType;
00120   static unsigned int GetNumberOfComponents()
00121     {
00122       return VDimension;
00123     }
00124   static void SetNthComponent(int i, TargetType & pixel, const ComponentType &v)
00125     {
00126       pixel[i] = v;
00127     }
00128   static ComponentType GetScalarValue(const TargetType &pixel)
00129     {
00130       return pixel[0];
00131     }
00132 };
00133 
00134 //
00135 //  Default traits for the pixel types deriving from FixedArray<>
00136 //
00137 
00138 #define ITK_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(type)                  \
00139   template<typename TComponentType, unsigned VDimension >               \
00140   class DefaultConvertPixelTraits< type< TComponentType, VDimension > > \
00141   {                                                                     \
00142   public:                                                               \
00143     typedef type< TComponentType, VDimension > TargetType;              \
00144     typedef TComponentType                    ComponentType;            \
00145     static unsigned int GetNumberOfComponents()                         \
00146     {                                                                   \
00147       return VDimension;                                                \
00148     }                                                                   \
00149     static unsigned int GetNumberOfComponents( const TargetType )       \
00150     {                                                                   \
00151       return VDimension;                                                \
00152     }                                                                   \
00153     static void SetNthComponent(int i, TargetType & pixel,              \
00154                                 const ComponentType &v)                 \
00155     {                                                                   \
00156       pixel[i] = v;                                                     \
00157     }                                                                   \
00158     static ComponentType GetNthComponent(int i, const TargetType pixel) \
00159     {                                                                   \
00160       return pixel[i];                                                  \
00161     }                                                                   \
00162     static ComponentType GetScalarValue(const TargetType &pixel)        \
00163     {                                                                   \
00164       return pixel[0];                                                  \
00165     }                                                                   \
00166   }                                                                     \
00167 
00168 ITK_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(Vector);
00169 ITK_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(CovariantVector);
00170 ITK_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(Point);
00171 ITK_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(FixedArray);
00172 
00173 //
00174 //  End of Traits for the classes deriving from FixedArray.
00175 //
00176 //
00177 
00178 //
00179 //  Default traits for pixel types deriving from VariableLengthVector<>
00180 //
00181 template<typename VComponent>
00182 class DefaultConvertPixelTraits< VariableLengthVector< VComponent > >
00183 {
00184 public:
00185   typedef VariableLengthVector< VComponent > TargetType;
00186   typedef VComponent                         ComponentType;
00187   static unsigned int GetNumberOfComponents()
00188   {
00189     return 0;
00190   }
00191   static unsigned int GetNumberOfComponents( const TargetType pixel )
00192   {
00193     return pixel.Size();
00194   }
00195   static void SetNthComponent(int i, TargetType & pixel,
00196                               const ComponentType &v)
00197   {
00198     pixel[i] = v;
00199   }
00200   static ComponentType GetNthComponent(int i, const TargetType & pixel)
00201   {
00202     return pixel[i];
00203   }
00204   static ComponentType GetScalarValue(const TargetType &pixel)
00205   {
00206     return pixel.GetNorm();
00207   }
00208 };
00209 
00210 
00211 //
00212 //  End of Traits for the classes deriving from FixedArray.
00213 //
00214 //
00215 
00216 //
00217 //  Default traits for the pixel types deriving from Matrix<>
00218 //
00219 
00220 template<typename VComponent, unsigned VRows, unsigned VCols >
00221 class DefaultConvertPixelTraits< Matrix< VComponent, VRows, VCols > >
00222 {
00223 public:
00224   typedef Matrix< VComponent, VRows, VCols > TargetType;
00225   typedef VComponent                         ComponentType;
00226   static unsigned int GetNumberOfComponents()
00227     {
00228       return VRows * VCols;
00229     }
00230   static void SetNthComponent(int i, TargetType & pixel,
00231                               const ComponentType &v)
00232     {
00233       const unsigned int row = i / VCols;
00234       const unsigned int col = i % VCols;
00235       pixel[row][col] = v;
00236     }
00237   static ComponentType GetScalarValue(const TargetType &pixel)
00238     {
00239       return pixel[0][0];
00240     }
00241 };
00242 
00243 //
00244 //  Default traits for the pixel types deriving from std::complex<>
00245 //
00246 
00247 template<typename TComponent >
00248 class DefaultConvertPixelTraits< ::std::complex< TComponent > >
00249 {
00250 public:
00251   typedef::std::complex< TComponent > TargetType;
00252   typedef TComponent                  ComponentType;
00253   static unsigned int GetNumberOfComponents()
00254     {
00255       return 2;
00256     }
00257   static void SetNthComponent(int i, TargetType & pixel, const ComponentType &v)
00258     {
00259       if ( i == 0 )
00260         {
00261         pixel = TargetType( v, pixel.imag() );
00262         }
00263       else
00264         {
00265         pixel = TargetType(pixel.real(), v);
00266         }
00267     }
00268   static ComponentType GetScalarValue(const TargetType &pixel)
00269     {
00270       return std::norm(pixel);
00271     }
00272 };
00273 
00274 
00275 //
00276 //  End of Traits for the classes deriving from std::complex.
00277 //
00278 //
00279 } // end namespace itk
00280 #endif
00281