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