ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkMetaArrayReader.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 __itkMetaArrayReader_h
00019 #define __itkMetaArrayReader_h
00020 
00021 #include "itkLightProcessObject.h"
00022 #include "itkArray.h"
00023 #include "itkCovariantVector.h"
00024 #include "itkVariableLengthVector.h"
00025 #include "metaArray.h"
00026 
00027 namespace itk
00028 {
00029 class ITK_EXPORT MetaArrayReader:public LightProcessObject
00030 {
00031 public:
00032 
00034   typedef MetaArrayReader            Self;
00035   typedef LightProcessObject         Superclass;
00036   typedef SmartPointer< Self >       Pointer;
00037   typedef SmartPointer< const Self > ConstPointer;
00038 
00040   itkNewMacro(Self);
00041 
00043   itkTypeMacro(MetaArrayReader, LightProcessObject);
00044 
00046   itkSetStringMacro(FileName);
00047 
00049   itkGetStringMacro(FileName);
00050 
00053   MetaArray * GetMetaArrayPointer(void);
00054 
00056   void SetBuffer(void *_buffer);
00057 
00059   void Update();
00060 
00062   MET_ValueEnumType GetDataType(void) const
00063   {
00064     return m_MetaArray.ElementType();
00065   }
00066 
00068   int Size(void) const
00069   {
00070     return m_MetaArray.Length();
00071   }
00072 
00074   int GetNumberOfElements(void) const
00075   {
00076     return m_MetaArray.Length();
00077   }
00078 
00080   int GetVectorDimension(void) const
00081   {
00082     return m_MetaArray.Length();
00083   }
00084 
00086   int GetNumberOfComponents(void) const
00087   {
00088     return m_MetaArray.Length();
00089   }
00090 
00092   int GetCovariantVectorDimension(void) const
00093   {
00094     return m_MetaArray.Length();
00095   }
00096 
00098   template< typename TValueType >
00099   inline void GetElement(TValueType & value, unsigned int i,
00100                          unsigned int channel = 0) const
00101   {
00102     value = static_cast< TValueType >( m_MetaArray.ElementData(
00103                                          i * m_MetaArray.ElementNumberOfChannels() + channel) );
00104   }
00105 
00114   template< typename TValueType >
00115   void GetOutput(MET_ValueEnumType _metaElementType,
00116                  Array< TValueType > *_array,
00117                  bool _letArrayManageData = true)
00118   {
00119     if ( m_MetaArray.ElementType() != _metaElementType )
00120       {
00121       m_MetaArray.ConvertElementDataTo(_metaElementType);
00122       }
00123     _array->SetData( (TValueType *)( m_MetaArray.ElementData() ),
00124                      m_MetaArray.Length(),
00125                      _letArrayManageData );
00126     if ( _letArrayManageData )
00127       {
00128       m_MetaArray.AutoFreeElementData(false);
00129       }
00130   }
00132 
00134   template< typename TValueType, unsigned int VLength >
00135   bool GetOutput(MET_ValueEnumType itkNotUsed(_metaElementType),
00136                  FixedArray< TValueType, VLength > *_array)
00137   {
00138     if ( static_cast< int >( VLength ) <= m_MetaArray.Length() )
00139       {
00140       unsigned int i;
00141       for ( i = 0; i < VLength; i++ )
00142         {
00143         this->GetElement( ( *_array )[i], i );
00144         }
00145       return true;
00146       }
00147     return false;
00148   }
00150 
00154   template< typename TValueType, unsigned int VLength >
00155   bool GetOutput(MET_ValueEnumType itkNotUsed(_metaElementType),
00156                  Vector< TValueType, VLength > *_vector)
00157   {
00158     if ( static_cast< int >( VLength ) <= m_MetaArray.Length() )
00159       {
00160       unsigned int i;
00161       for ( i = 0; i < VLength; i++ )
00162         {
00163         this->GetElement( ( *_vector )[i], i );
00164         }
00165       return true;
00166       }
00167     return false;
00168   }
00170 
00175   template< typename TValueType, unsigned int VLength >
00176   bool GetOutput(MET_ValueEnumType itkNotUsed(_metaElementType),
00177                  CovariantVector< TValueType, VLength > *_vector)
00178   {
00179     if ( static_cast< int >( VLength ) <= m_MetaArray.Length() )
00180       {
00181       unsigned int i;
00182       for ( i = 0; i < VLength; i++ )
00183         {
00184         this->GetElement( ( *_vector )[i], i );
00185         }
00186       return true;
00187       }
00188     return false;
00189   }
00191 
00201   template< typename TValueType >
00202   void GetOutput(MET_ValueEnumType _metaElementType,
00203                  VariableLengthVector< TValueType > *_vector,
00204                  bool _letVectorManageData = true)
00205   {
00206     if ( m_MetaArray.ElementType() != _metaElementType )
00207       {
00208       m_MetaArray.ConvertElementDataTo(_metaElementType);
00209       }
00210     _vector->SetData( (TValueType *)( m_MetaArray.ElementData() ),
00211                       m_MetaArray.Length(),
00212                       _letVectorManageData );
00213     if ( _letVectorManageData )
00214       {
00215       m_MetaArray.AutoFreeElementData(false);
00216       }
00217   }
00219 
00225   template< typename TValueType >
00226   void GetMultiChannelOutput(MET_ValueEnumType _metaElementType,
00227                              Array< TValueType > *_array)
00228   {
00229     if ( m_MetaArray.ElementType() != _metaElementType )
00230       {
00231       m_MetaArray.ConvertElementDataTo(_metaElementType);
00232       }
00233     int rows = m_MetaArray.Length();
00234     int cols = m_MetaArray.ElementNumberOfChannels();
00235     _array->SetSize(rows);
00236     for ( int i = 0; i < rows; i++ )
00237       {
00238       ( *_array )[i].SetSize(cols);
00239       for ( int j = 0; j < cols; j++ )
00240         {
00241         ( *_array )[i][j] = static_cast< typename TValueType::ValueType >
00242                             ( m_MetaArray.ElementData(i * cols + j) );
00243         }
00244       }
00245   }
00247 
00248 protected:
00249 
00250   MetaArrayReader();
00251   virtual ~MetaArrayReader();
00252   void PrintSelf(std::ostream & os, Indent indent) const;
00253 
00254 private:
00255 
00256   MetaArray m_MetaArray;
00257 
00258   std::string m_FileName;
00259 
00260   void *m_Buffer;
00261 };
00262 } // namespace itk
00263 
00264 #endif // __itkTransformFileReader_h
00265