]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/DataObjects/Image.cxx
5453dce056604746f2d5bfd4cde3c78c99e072a0
[cpPlugins.git] / lib / cpPlugins / DataObjects / Image.cxx
1 #include <cpPlugins/DataObjects/Image.h>
2 #include <cpPlugins/DataObjects/Image_Demanglers.h>
3
4 #include <itkImageToVTKImageFilter.h>
5 #include <itkVTKImageToImageFilter.h>
6
7 #include <itkImageToVTKImageFilter.hxx>
8 #include <itkVTKImageToImageFilter.hxx>
9 #include <itkVTKImageExport.hxx>
10 #include <itkVTKImageImport.hxx>
11
12 // -------------------------------------------------------------------------
13 void cpPlugins::DataObjects::Image::
14 SetITK( itk::LightObject* o )
15 {
16   this->Superclass::SetITK( o );
17   cpPlugins_Demangle_Image_VisualDims_1( o, _ITK_2_VTK_0 )
18     cpPlugins_Demangle_Image_DiffTensors3D_1( o, _ITK_2_VTK_1 )
19   {
20     this->m_VTK = NULL;
21     this->m_ITKvVTK = NULL;
22
23   } // fi
24   this->Modified( );
25 }
26
27 // -------------------------------------------------------------------------
28 void cpPlugins::DataObjects::Image::
29 SetVTK( vtkObjectBase* o )
30 {
31   vtkImageData* img = dynamic_cast< vtkImageData* >( o );
32   this->Superclass::SetVTK( img );
33   if( img == NULL )
34   {
35     this->m_ITK = NULL;
36     this->m_ITKvVTK = NULL;
37     this->Modified( );
38     return;
39
40   } // fi
41
42   bool success = false;
43   auto stype = img->GetScalarType( );
44 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_char
45   if( stype == VTK_CHAR ) success = this->_VTK_2_ITK_0< char >( img );
46   if( stype == VTK_UNSIGNED_CHAR ) success = this->_VTK_2_ITK_0< unsigned char >( img );
47 #endif // cpPlugins_CONFIG_INTEGER_TYPES_char
48 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_short
49   if( stype == VTK_CHAR ) success = this->_VTK_2_ITK_0< short >( img );
50   if( stype == VTK_UNSIGNED_CHAR ) success = this->_VTK_2_ITK_0< unsigned short >( img );
51 #endif // cpPlugins_CONFIG_INTEGER_TYPES_short
52 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_int
53   if( stype == VTK_CHAR ) success = this->_VTK_2_ITK_0< int >( img );
54   if( stype == VTK_UNSIGNED_CHAR ) success = this->_VTK_2_ITK_0< unsigned int >( img );
55 #endif // cpPlugins_CONFIG_INTEGER_TYPES_int
56 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_long
57   if( stype == VTK_CHAR ) success = this->_VTK_2_ITK_0< long >( img );
58   if( stype == VTK_UNSIGNED_CHAR ) success = this->_VTK_2_ITK_0< unsigned long >( img );
59 #endif // cpPlugins_CONFIG_INTEGER_TYPES_long
60 #ifdef cpPlugins_CONFIG_REAL_TYPES_float
61   if( stype == VTK_CHAR ) success = this->_VTK_2_ITK_0< float >( img );
62 #endif // cpPlugins_CONFIG_REAL_TYPES_float
63 #ifdef cpPlugins_CONFIG_REAL_TYPES_double
64   if( stype == VTK_CHAR ) success = this->_VTK_2_ITK_0< double >( img );
65 #endif // cpPlugins_CONFIG_REAL_TYPES_double
66   /* TODO
67      #define cpPlugins_CONFIG_COLOR_PIXELS_RGBPixel
68      #define cpPlugins_CONFIG_COLOR_PIXELS_RGBAPixel
69      #define cpPlugins_CONFIG_VECTORS_CovariantVector
70      #define cpPlugins_CONFIG_VECTORS_Point
71      #define cpPlugins_CONFIG_VECTORS_SymmetricSecondRankTensor
72      #define cpPlugins_CONFIG_VECTORS_Vector
73      #define cpPlugins_CONFIG_DIFFUSIONTENSORS_DiffusionTensor3D
74      #define cpPlugins_CONFIG_MATRICES_Matrix
75   */
76   if( !success )
77   {
78     this->m_ITK = NULL;
79     this->m_ITKvVTK = NULL;
80
81   } // fi
82   this->Modified( );
83 }
84
85 // -------------------------------------------------------------------------
86 cpPlugins::DataObjects::Image::
87 Image( )
88   : Superclass( )
89 {
90 }
91
92 // -------------------------------------------------------------------------
93 cpPlugins::DataObjects::Image::
94 ~Image( )
95 {
96 }
97
98 // -------------------------------------------------------------------------
99 template< class _TImage >
100 void cpPlugins::DataObjects::Image::
101 _ITK_2_VTK_0( _TImage* image )
102 {
103   cpPlugins_Demangle_Image_ScalarPixels_1( image, _ITK_2_VTK_1, _TImage::ImageDimension )
104     cpPlugins_Demangle_Image_ColorPixels_1( image, _ITK_2_VTK_1, _TImage::ImageDimension )
105     cpPlugins_Demangle_Image_VectorPixels_1( image, _ITK_2_VTK_1, _TImage::ImageDimension )
106   {
107     this->m_VTK = NULL;
108     this->m_ITKvVTK = NULL;
109
110   } // fi
111 }
112
113 // -------------------------------------------------------------------------
114 template< class _TImage >
115 void cpPlugins::DataObjects::Image::
116 _ITK_2_VTK_1( _TImage* image )
117 {
118   typedef itk::ImageToVTKImageFilter< _TImage > _TFilter;
119   _TFilter* f = dynamic_cast< _TFilter* >( this->m_ITKvVTK.GetPointer( ) );
120   if( f == NULL )
121   {
122     typename _TFilter::Pointer nf = _TFilter::New( );
123     this->m_ITKvVTK = nf;
124     f = nf.GetPointer( );
125
126   } // fi
127   f->SetInput( image );
128   f->Update( );
129
130   // Keep object track
131   this->m_ITK = image;
132   this->m_VTK = f->GetOutput( );
133 }
134
135 // -------------------------------------------------------------------------
136 template< class _TPixel >
137 bool cpPlugins::DataObjects::Image::
138 _VTK_2_ITK_0( vtkImageData* image )
139 {
140   bool success = false;
141   unsigned int dim = image->GetDataDimension( );
142 #ifdef cpPlugins_CONFIG_VISUAL_DIMENSIONS_2
143   if( dim == 2 ) success = this->_VTK_2_ITK_1< _TPixel, 2 >( image );
144 #endif // cpPlugins_CONFIG_VISUAL_DIMENSIONS_2
145 #ifdef cpPlugins_CONFIG_VISUAL_DIMENSIONS_3
146   if( dim == 3 ) success = this->_VTK_2_ITK_1< _TPixel, 3 >( image );
147 #endif // cpPlugins_CONFIG_VISUAL_DIMENSIONS_2
148   return( success );
149 }
150
151 // -------------------------------------------------------------------------
152 template< class _TPixel, unsigned int _VDim >
153 bool cpPlugins::DataObjects::Image::
154 _VTK_2_ITK_1( vtkImageData* image )
155 {
156   typedef itk::Image< _TPixel, _VDim > _TImage;
157   typedef itk::VTKImageToImageFilter< _TImage > _TFilter;
158   _TFilter* f = dynamic_cast< _TFilter* >( this->m_ITKvVTK.GetPointer( ) );
159   if( f == NULL )
160   {
161     typename _TFilter::Pointer nf = _TFilter::New( );
162     this->m_ITKvVTK = nf;
163     f = nf.GetPointer( );
164
165   } // fi
166   f->SetInput( image );
167   f->Update( );
168
169   // Keep object track
170   this->m_VTK = image;
171   this->m_ITK = f->GetOutput( );
172   return( true );
173 }
174
175 // eof - $RCSfile$