]> Creatis software - cpPlugins.git/blob - plugins/ITKIO/ImageReader.cxx
b336078ddb12d82010a298ba923a5bb085d1ed25
[cpPlugins.git] / plugins / ITKIO / ImageReader.cxx
1 #include <ITKIO/ImageReader.h>
2 #include <cpInstances/Image.h>
3 #include <cpPlugins/QT/OpenFileDialog.h>
4
5 #define ITKIOImageBase_HIDDEN
6 #include <itkImageFileReader.h>
7 #include <itkImageSeriesReader.h>
8 #include <itkImageIOFactory.h>
9
10 #ifdef cpPlugins_QT4
11 #  include <QApplication>
12 #endif // cpPlugins_QT4
13
14 // -------------------------------------------------------------------------
15 QDialog* cpPluginsITKIO::ImageReader::
16 CreateQDialog( )
17 {
18 #ifdef cpPlugins_QT4
19   cpPlugins::QT::OpenFileDialog* dlg = NULL;
20   if( QApplication::instance( ) != NULL )
21   {
22     dlg = new cpPlugins::QT::OpenFileDialog( );
23     dlg->SetParameters( &( this->m_Parameters ), "FileNames" );
24
25   } // fi
26   return( dlg );
27 #else // cpPlugins_QT4
28   return( NULL );
29 #endif // cpPlugins_QT4
30 }
31
32 // -------------------------------------------------------------------------
33 cpPluginsITKIO::ImageReader::
34 ImageReader( )
35   : Superclass( )
36 {
37   this->_ConfigureOutput< cpInstances::Image >( "Output" );
38   this->m_Parameters.Clear( );
39   this->m_Parameters.ConfigureAsOpenFileNameList( "FileNames" );
40   this->m_Parameters.SetAcceptedFileExtensions(
41     "FileNames",
42     "Image files (*.bmp *.png *.jpg *.jpeg *.dcm *.mhd *.nhdr *.nrrd *.tiff)"
43     );
44 }
45
46 // -------------------------------------------------------------------------
47 cpPluginsITKIO::ImageReader::
48 ~ImageReader( )
49 {
50 }
51
52 // -------------------------------------------------------------------------
53 void cpPluginsITKIO::ImageReader::
54 _GenerateData( )
55 {
56   // Get filenames
57   auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
58   std::string fname = "";
59   if( fnames.size( ) > 1 )
60   {
61     std::stringstream fname_str;
62     fname_str << fnames[ 0 ] << cpPlugins_PATH_SEPARATOR << fnames[ 1 ];
63     fname = fname_str.str( );
64   }
65   else if( fnames.size( ) == 1 )
66     fname = fnames[ 0 ];
67
68   if( fname != "" )
69   {
70     // Guess image properties
71     itk::ImageIOBase::Pointer io =
72       itk::ImageIOFactory::CreateImageIO(
73         fname.c_str( ), itk::ImageIOFactory::ReadMode
74         );
75     if( io.IsNotNull( ) )
76     {
77       io->SetFileName( fname );
78       io->ReadImageInformation( );
79       bool success = false;
80       unsigned int dim = io->GetNumberOfDimensions( );
81 #ifdef cpPlugins_CONFIG_PROCESS_DIMENSIONS_1
82       if( dim == 1 ) success = this->_GD0< 1 >( io );
83 #endif // cpPlugins_CONFIG_PROCESS_DIMENSIONS_1
84 #ifdef cpPlugins_CONFIG_PROCESS_DIMENSIONS_2
85       if( dim == 2 ) success = this->_GD0< 2 >( io );
86 #endif // cpPlugins_CONFIG_PROCESS_DIMENSIONS_2
87 #ifdef cpPlugins_CONFIG_PROCESS_DIMENSIONS_3
88       if( dim == 3 ) success = this->_GD0< 3 >( io );
89 #endif // cpPlugins_CONFIG_PROCESS_DIMENSIONS_3
90 #ifdef cpPlugins_CONFIG_PROCESS_DIMENSIONS_4
91       if( dim == 4 ) success = this->_GD0< 4 >( io );
92 #endif // cpPlugins_CONFIG_PROCESS_DIMENSIONS_4
93       if( !success )
94         this->_Error( "Image dimension not supported." );
95     }
96     else
97       this->_Error(
98         std::string( "Could not create an ImageIO for \"" ) +
99         fnames[ 0 ] +
100         std::string( "\"" )
101         );
102   }
103   else
104     this->_Error( "No image(s) given" );
105 }
106
107 // -------------------------------------------------------------------------
108 template< unsigned int _Dim >
109 bool cpPluginsITKIO::ImageReader::
110 _GD0( itk::ImageIOBase* io )
111 {
112   typedef unsigned char  uchar;
113   typedef unsigned short ushort;
114   typedef unsigned int   uint;
115   typedef unsigned long  ulong;
116
117   itk::ImageIOBase::IOComponentType ct = io->GetComponentType( );
118   itk::ImageIOBase::IOPixelType pt = io->GetPixelType( );
119
120   bool success = false;
121   if( pt == itk::ImageIOBase::SCALAR )
122   {
123     if( ct == itk::ImageIOBase::CHAR ) success = this->_GD1< char, _Dim >( io );
124     if( ct == itk::ImageIOBase::UCHAR ) success = this->_GD1< uchar, _Dim >( io );
125     if( ct == itk::ImageIOBase::SHORT ) success = this->_GD1< short, _Dim >( io );
126     if( ct == itk::ImageIOBase::USHORT ) success = this->_GD1< ushort, _Dim >( io );
127     if( ct == itk::ImageIOBase::INT ) success = this->_GD1< int, _Dim >( io );
128     if( ct == itk::ImageIOBase::UINT ) success = this->_GD1< uint, _Dim >( io );
129     if( ct == itk::ImageIOBase::LONG ) success = this->_GD1< long, _Dim >( io );
130     if( ct == itk::ImageIOBase::ULONG ) success = this->_GD1< ulong, _Dim >( io );
131     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< float, _Dim >( io );
132     if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< double, _Dim >( io );
133   }
134   else if( pt == itk::ImageIOBase::RGB )
135   {
136     if( ct == itk::ImageIOBase::CHAR ) success = this->_GD1< itk::RGBPixel< char >, _Dim >( io );
137     if( ct == itk::ImageIOBase::UCHAR ) success = this->_GD1< itk::RGBPixel< uchar >, _Dim >( io );
138     if( ct == itk::ImageIOBase::SHORT ) success = this->_GD1< itk::RGBPixel< short >, _Dim >( io );
139     if( ct == itk::ImageIOBase::USHORT ) success = this->_GD1< itk::RGBPixel< ushort >, _Dim >( io );
140     if( ct == itk::ImageIOBase::INT ) success = this->_GD1< itk::RGBPixel< int >, _Dim >( io );
141     if( ct == itk::ImageIOBase::UINT ) success = this->_GD1< itk::RGBPixel< uint >, _Dim >( io );
142     if( ct == itk::ImageIOBase::LONG ) success = this->_GD1< itk::RGBPixel< long >, _Dim >( io );
143     if( ct == itk::ImageIOBase::ULONG ) success = this->_GD1< itk::RGBPixel< ulong >, _Dim >( io );
144     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::RGBPixel< float >, _Dim >( io );
145     if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::RGBPixel< double >, _Dim >( io );
146   }
147   else if( pt == itk::ImageIOBase::RGBA )
148   {
149     if( ct == itk::ImageIOBase::CHAR ) success = this->_GD1< itk::RGBAPixel< char >, _Dim >( io );
150     if( ct == itk::ImageIOBase::UCHAR ) success = this->_GD1< itk::RGBAPixel< uchar >, _Dim >( io );
151     if( ct == itk::ImageIOBase::SHORT ) success = this->_GD1< itk::RGBAPixel< short >, _Dim >( io );
152     if( ct == itk::ImageIOBase::USHORT ) success = this->_GD1< itk::RGBAPixel< ushort >, _Dim >( io );
153     if( ct == itk::ImageIOBase::INT ) success = this->_GD1< itk::RGBAPixel< int >, _Dim >( io );
154     if( ct == itk::ImageIOBase::UINT ) success = this->_GD1< itk::RGBAPixel< uint >, _Dim >( io );
155     if( ct == itk::ImageIOBase::LONG ) success = this->_GD1< itk::RGBAPixel< long >, _Dim >( io );
156     if( ct == itk::ImageIOBase::ULONG ) success = this->_GD1< itk::RGBAPixel< ulong >, _Dim >( io );
157     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::RGBAPixel< float >, _Dim >( io );
158     if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::RGBAPixel< double >, _Dim >( io );
159   }
160   else if( pt == itk::ImageIOBase::COMPLEX )
161   {
162     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< std::complex< float >, _Dim >( io );
163     if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< std::complex< double >, _Dim >( io );
164   }
165   else if( pt == itk::ImageIOBase::COVARIANTVECTOR )
166   {
167     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::CovariantVector< float, _Dim >, _Dim >( io );
168     if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::CovariantVector< double, _Dim >, _Dim >( io );
169   }
170   else if( pt == itk::ImageIOBase::POINT )
171   {
172     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::Point< float, _Dim >, _Dim >( io );
173     if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::Point< double, _Dim >, _Dim >( io );
174   }
175   else if( pt == itk::ImageIOBase::VECTOR )
176   {
177     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::Vector< float, _Dim >, _Dim >( io );
178     if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::Vector< double, _Dim >, _Dim >( io );
179   }
180   else if( pt == itk::ImageIOBase::SYMMETRICSECONDRANKTENSOR )
181   {
182     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::SymmetricSecondRankTensor< float, _Dim >, _Dim >( io );
183     if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::SymmetricSecondRankTensor< double, _Dim >, _Dim >( io );
184   }
185   else if( pt == itk::ImageIOBase::DIFFUSIONTENSOR3D )
186   {
187     if( _Dim == 3 )
188     {
189       if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::DiffusionTensor3D< float >, _Dim >( io );
190       if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::DiffusionTensor3D< double >, _Dim >( io );
191     }
192     else
193       this->_Error( "DiffusionTensor3D dimension not supported." );
194   }
195   else if( pt == itk::ImageIOBase::MATRIX )
196   {
197     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::Matrix< float, _Dim, _Dim >, _Dim >( io );
198     if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::Matrix< double, _Dim, _Dim >, _Dim >( io );
199   }
200   else if( pt == itk::ImageIOBase::OFFSET )
201   {
202     this->_GD1< itk::Offset< _Dim >, _Dim >( io );
203   }
204   else if( pt == itk::ImageIOBase::FIXEDARRAY )
205   {
206   } // fi
207   return( success );
208 }
209
210 // -------------------------------------------------------------------------
211 template< class _TPixel, unsigned int _Dim >
212 bool cpPluginsITKIO::ImageReader::
213 _GD1( itk::ImageIOBase* io )
214 {
215   typedef itk::Image< _TPixel, _Dim > _TImage;
216
217   // Get filenames
218   auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
219   if( fnames.size( ) == 1 || fnames.size( ) == 2 )
220   {
221     std::stringstream fname_str;
222     if( fnames.size( ) == 1 )
223       fname_str << fnames[ 0 ];
224     else
225       fname_str << fnames[ 0 ] << cpPlugins_PATH_SEPARATOR << fnames[ 1 ];
226
227     auto f = this->_CreateITK< itk::ImageFileReader< _TImage > >( );
228     f->SetFileName( fname_str.str( ) );
229     f->SetImageIO( io );
230     try
231     {
232       f->Update( );
233       this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
234       return( true );
235     }
236     catch( itk::ExceptionObject& err )
237     {
238       this->_Error( err.GetDescription( ) );
239       return( false );
240
241     } // yrt
242   }
243   else // if( fnames.size( ) > 1 )
244   {
245     auto f = this->_CreateITK< itk::ImageSeriesReader< _TImage > >( );
246     auto i = fnames.begin( );
247     std::stringstream dir;
248     dir << *i << cpPlugins_PATH_SEPARATOR;
249     i++;
250     for( ; i != fnames.end( ); ++i )
251       f->AddFileName( dir.str( ) + *i );
252     f->SetImageIO( io );
253     try
254     {
255       f->Update( );
256       this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
257       return( true );
258     }
259     catch( itk::ExceptionObject& err )
260     {
261       this->_Error( err.GetDescription( ) );
262       return( false );
263
264     } // yrt
265
266   } // fi
267 }
268
269 // eof - $RCSfile$