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