]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Extensions/DataStructures/ITKAndVTKImage.h
8a2953657bad6c3d6f3d988da006cbe92af11354
[cpPlugins.git] / lib / cpPlugins / Extensions / DataStructures / ITKAndVTKImage.h
1 #ifndef __CPPLUGINS__EXTENSIONS__DATASTRUCTURES__ITKANDVTKIMAGE__H__
2 #define __CPPLUGINS__EXTENSIONS__DATASTRUCTURES__ITKANDVTKIMAGE__H__
3
4 #include <itkImage.h>
5 #include <itkImageFileReader.h>
6 #include <itkImageFileWriter.h>
7 #include <itkImageToVTKImageFilter.h>
8
9 namespace cpPlugins
10 {
11   namespace Extensions
12   {
13     namespace DataStructures
14     {
15       /**
16        */
17       template< typename P, unsigned int D >
18       struct ITKAndVTKImage
19       {
20         typedef itk::Image< P, D >                   TImage;
21         typedef itk::ImageToVTKImageFilter< TImage > TItk2Vtk;
22
23         typename TImage::Pointer   ITK;
24         typename TItk2Vtk::Pointer VTK;
25
26         void CopyInformation( const itk::ImageBase< D >* i )
27           {
28             this->ITK = TImage::New( );
29             this->ITK->SetLargestPossibleRegion( i->GetLargestPossibleRegion( ) );
30             this->ITK->SetRequestedRegion( i->GetRequestedRegion( ) );
31             this->ITK->SetBufferedRegion( i->GetBufferedRegion( ) );
32             this->ITK->SetSpacing( i->GetSpacing( ) );
33             this->ITK->SetOrigin( i->GetOrigin( ) );
34             this->ITK->SetDirection( i->GetDirection( ) );
35             this->ITK->Allocate( );
36           }
37
38         void SetItkImage( TImage* image )
39           {
40             this->ITK = image;
41             this->_Synch( );
42           }
43
44         std::string ReadFromFile( const std::string& filename )
45           {
46             typename itk::ImageFileReader< TImage >::Pointer r =
47               itk::ImageFileReader< TImage >::New( );
48             r->SetFileName( filename );
49             try
50             {
51               r->Update( );
52             }
53             catch( itk::ExceptionObject& err )
54             {
55               this->ITK = NULL;
56               return( err.GetDescription( ) );
57
58             } // fi
59             this->ITK = r->GetOutput( );
60             this->ITK->DisconnectPipeline( );
61             this->_Synch( );
62             return( "" );
63           }
64
65         std::string WriteToFile( const std::string& filename ) const
66           {
67             typename itk::ImageFileWriter< TImage >::Pointer w =
68               itk::ImageFileWriter< TImage >::New( );
69             w->SetFileName( filename );
70             w->SetInput( this->ITK );
71             try
72             {
73               w->Update( );
74             }
75             catch( itk::ExceptionObject& err )
76             {
77               return( err.GetDescription( ) );
78
79             } // fi
80             return( "" );
81           }
82
83         TImage* GetItkImage( )
84           { return( this->ITK ); }
85         vtkImageData* GetVtkImage( )
86           { return( this->VTK->GetOutput( ) ); }
87         const TImage* GetItkImage( ) const
88           { return( this->ITK ); }
89         const vtkImageData* GetVtkImage( ) const
90           { return( this->VTK->GetOutput( ) ); }
91         void DisconnectPipeline( )
92           { this->ITK->DisconnectPipeline( ); }
93
94         void _Synch( )
95           {
96             this->VTK = TItk2Vtk::New( );
97             this->VTK->SetInput( this->ITK );
98             this->VTK->Update( );
99           }
100
101       };
102
103     } // ecapseman
104
105   } // ecapseman
106
107 } // ecapseman
108
109 #endif // __CPPLUGINS__EXTENSIONS__DATASTRUCTURES__ITKANDVTKIMAGE__H__
110
111 // eof - $RCSfile$