]> Creatis software - cpPlugins.git/blob - plugins/ITKIO/ImageReader.cxx
...
[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   if( fnames.size( ) > 1 )
59   {
60     std::stringstream fname_str;
61     fname_str << fnames[ 0 ] << cpPlugins_PATH_SEPARATOR << fnames[ 1 ];
62     std::string fname = fname_str.str( );
63
64     // Guess image properties
65     itk::ImageIOBase::Pointer io =
66       itk::ImageIOFactory::CreateImageIO(
67         fname.c_str( ), itk::ImageIOFactory::ReadMode
68         );
69     if( io.IsNotNull( ) )
70     {
71       io->SetFileName( fname );
72       io->ReadImageInformation( );
73       bool success = false;
74       unsigned int dim = io->GetNumberOfDimensions( );
75 #ifdef cpPlugins_CONFIG_PROCESS_DIMENSIONS_1
76       if( dim == 1 ) success = this->_GD0< 1 >( io );
77 #endif // cpPlugins_CONFIG_PROCESS_DIMENSIONS_1
78 #ifdef cpPlugins_CONFIG_PROCESS_DIMENSIONS_2
79       if( dim == 2 ) success = this->_GD0< 2 >( io );
80 #endif // cpPlugins_CONFIG_PROCESS_DIMENSIONS_2
81 #ifdef cpPlugins_CONFIG_PROCESS_DIMENSIONS_3
82       if( dim == 3 ) success = this->_GD0< 3 >( io );
83 #endif // cpPlugins_CONFIG_PROCESS_DIMENSIONS_3
84 #ifdef cpPlugins_CONFIG_PROCESS_DIMENSIONS_4
85       if( dim == 4 ) success = this->_GD0< 4 >( io );
86 #endif // cpPlugins_CONFIG_PROCESS_DIMENSIONS_4
87       if( !success )
88         this->_Error( "Image dimension not supported." );
89     }
90     else
91       this->_Error(
92         std::string( "Could not create an ImageIO for \"" ) +
93         fnames[ 0 ] +
94         std::string( "\"" )
95         );
96   }
97   else
98     this->_Error( "No image(s) given" );
99 }
100
101 // -------------------------------------------------------------------------
102 template< unsigned int _Dim >
103 bool cpPluginsITKIO::ImageReader::
104 _GD0( itk::ImageIOBase* io )
105 {
106   typedef unsigned char  uchar;
107   typedef unsigned short ushort;
108   typedef unsigned int   uint;
109   typedef unsigned long  ulong;
110
111   itk::ImageIOBase::IOComponentType ct = io->GetComponentType( );
112   itk::ImageIOBase::IOPixelType pt = io->GetPixelType( );
113
114   bool success = false;
115   if( pt == itk::ImageIOBase::SCALAR )
116   {
117 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_char
118     if( ct == itk::ImageIOBase::CHAR ) success = this->_GD1< char, _Dim >( io );
119     if( ct == itk::ImageIOBase::UCHAR ) success = this->_GD1< uchar, _Dim >( io );
120 #endif // cpPlugins_CONFIG_INTEGER_TYPES_char
121 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_short
122     if( ct == itk::ImageIOBase::SHORT ) success = this->_GD1< short, _Dim >( io );
123     if( ct == itk::ImageIOBase::USHORT ) success = this->_GD1< ushort, _Dim >( io );
124 #endif // cpPlugins_CONFIG_INTEGER_TYPES_short
125 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_int
126     if( ct == itk::ImageIOBase::INT ) success = this->_GD1< int, _Dim >( io );
127     if( ct == itk::ImageIOBase::UINT ) success = this->_GD1< uint, _Dim >( io );
128 #endif // cpPlugins_CONFIG_INTEGER_TYPES_int
129 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_long
130     if( ct == itk::ImageIOBase::LONG ) success = this->_GD1< long, _Dim >( io );
131     if( ct == itk::ImageIOBase::ULONG ) success = this->_GD1< ulong, _Dim >( io );
132 #endif // cpPlugins_CONFIG_INTEGER_TYPES_long
133 #ifdef cpPlugins_CONFIG_REAL_TYPES_float
134     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< float, _Dim >( io );
135 #endif // cpPlugins_CONFIG_REAL_TYPES_float
136 #ifdef cpPlugins_CONFIG_REAL_TYPES_double
137     if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< double, _Dim >( io );
138 #endif // cpPlugins_CONFIG_REAL_TYPES_double
139   }
140   else if( pt == itk::ImageIOBase::RGB )
141   {
142 #ifdef cpPlugins_CONFIG_COLOR_PIXELS_RGBPixel
143 #  ifdef cpPlugins_CONFIG_INTEGER_TYPES_char
144     if( ct == itk::ImageIOBase::CHAR ) success = this->_GD1< itk::RGBPixel< char >, _Dim >( io );
145     if( ct == itk::ImageIOBase::UCHAR ) success = this->_GD1< itk::RGBPixel< uchar >, _Dim >( io );
146 #  endif // cpPlugins_CONFIG_INTEGER_TYPES_char
147 #  ifdef cpPlugins_CONFIG_INTEGER_TYPES_short
148     if( ct == itk::ImageIOBase::SHORT ) success = this->_GD1< itk::RGBPixel< short >, _Dim >( io );
149     if( ct == itk::ImageIOBase::USHORT ) success = this->_GD1< itk::RGBPixel< ushort >, _Dim >( io );
150 #  endif // cpPlugins_CONFIG_INTEGER_TYPES_short
151 #  ifdef cpPlugins_CONFIG_INTEGER_TYPES_int
152     if( ct == itk::ImageIOBase::INT ) success = this->_GD1< itk::RGBPixel< int >, _Dim >( io );
153     if( ct == itk::ImageIOBase::UINT ) success = this->_GD1< itk::RGBPixel< uint >, _Dim >( io );
154 #  endif // cpPlugins_CONFIG_INTEGER_TYPES_int
155 #  ifdef cpPlugins_CONFIG_INTEGER_TYPES_long
156     if( ct == itk::ImageIOBase::LONG ) success = this->_GD1< itk::RGBPixel< long >, _Dim >( io );
157     if( ct == itk::ImageIOBase::ULONG ) success = this->_GD1< itk::RGBPixel< ulong >, _Dim >( io );
158 #  endif // cpPlugins_CONFIG_INTEGER_TYPES_long
159 #  ifdef cpPlugins_CONFIG_REAL_TYPES_float
160     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::RGBPixel< float >, _Dim >( io );
161 #  endif // cpPlugins_CONFIG_REAL_TYPES_float
162 #  ifdef cpPlugins_CONFIG_REAL_TYPES_double
163     if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::RGBPixel< double >, _Dim >( io );
164 #  endif // cpPlugins_CONFIG_REAL_TYPES_double
165 #endif // cpPlugins_CONFIG_COLOR_PIXELS_RGBPixel
166   }
167   else if( pt == itk::ImageIOBase::RGBA )
168   {
169 #ifdef cpPlugins_CONFIG_COLOR_PIXELS_RGBAPixel
170 #  ifdef cpPlugins_CONFIG_INTEGER_TYPES_char
171     if( ct == itk::ImageIOBase::CHAR ) success = this->_GD1< itk::RGBAPixel< char >, _Dim >( io );
172     if( ct == itk::ImageIOBase::UCHAR ) success = this->_GD1< itk::RGBAPixel< uchar >, _Dim >( io );
173 #  endif // cpPlugins_CONFIG_INTEGER_TYPES_char
174 #  ifdef cpPlugins_CONFIG_INTEGER_TYPES_short
175     if( ct == itk::ImageIOBase::SHORT ) success = this->_GD1< itk::RGBAPixel< short >, _Dim >( io );
176     if( ct == itk::ImageIOBase::USHORT ) success = this->_GD1< itk::RGBAPixel< ushort >, _Dim >( io );
177 #  endif // cpPlugins_CONFIG_INTEGER_TYPES_short
178 #  ifdef cpPlugins_CONFIG_INTEGER_TYPES_int
179     if( ct == itk::ImageIOBase::INT ) success = this->_GD1< itk::RGBAPixel< int >, _Dim >( io );
180     if( ct == itk::ImageIOBase::UINT ) success = this->_GD1< itk::RGBAPixel< uint >, _Dim >( io );
181 #  endif // cpPlugins_CONFIG_INTEGER_TYPES_int
182 #  ifdef cpPlugins_CONFIG_INTEGER_TYPES_long
183     if( ct == itk::ImageIOBase::LONG ) success = this->_GD1< itk::RGBAPixel< long >, _Dim >( io );
184     if( ct == itk::ImageIOBase::ULONG ) success = this->_GD1< itk::RGBAPixel< ulong >, _Dim >( io );
185 #  endif // cpPlugins_CONFIG_INTEGER_TYPES_long
186 #  ifdef cpPlugins_CONFIG_REAL_TYPES_float
187     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::RGBAPixel< float >, _Dim >( io );
188 #  endif // cpPlugins_CONFIG_REAL_TYPES_float
189 #  ifdef cpPlugins_CONFIG_REAL_TYPES_double
190     if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::RGBAPixel< double >, _Dim >( io );
191 #  endif // cpPlugins_CONFIG_REAL_TYPES_double
192 #endif // cpPlugins_CONFIG_COLOR_PIXELS_RGBAPixel
193   }
194   else if( pt == itk::ImageIOBase::COMPLEX )
195   {
196 #ifdef cpPlugins_CONFIG_REAL_TYPES_float
197     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< std::complex< float >, _Dim >( io );
198 #endif // cpPlugins_CONFIG_REAL_TYPES_float
199 #ifdef cpPlugins_CONFIG_REAL_TYPES_double
200     if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< std::complex< double >, _Dim >( io );
201 #endif // cpPlugins_CONFIG_REAL_TYPES_double
202   }
203   else if( pt == itk::ImageIOBase::COVARIANTVECTOR )
204   {
205 #ifdef cpPlugins_CONFIG_REAL_TYPES_float
206     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::CovariantVector< float, _Dim >, _Dim >( io );
207 #endif // cpPlugins_CONFIG_REAL_TYPES_float
208 #ifdef cpPlugins_CONFIG_REAL_TYPES_double
209     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::CovariantVector< double, _Dim >, _Dim >( io );
210 #endif // cpPlugins_CONFIG_REAL_TYPES_double
211   }
212   else if( pt == itk::ImageIOBase::POINT )
213   {
214 #ifdef cpPlugins_CONFIG_REAL_TYPES_float
215     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::Point< float, _Dim >, _Dim >( io );
216 #endif // cpPlugins_CONFIG_REAL_TYPES_float
217 #ifdef cpPlugins_CONFIG_REAL_TYPES_double
218     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::Point< double, _Dim >, _Dim >( io );
219 #endif // cpPlugins_CONFIG_REAL_TYPES_double
220   }
221   else if( pt == itk::ImageIOBase::VECTOR )
222   {
223 #ifdef cpPlugins_CONFIG_REAL_TYPES_float
224     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::Vector< float, _Dim >, _Dim >( io );
225 #endif // cpPlugins_CONFIG_REAL_TYPES_float
226 #ifdef cpPlugins_CONFIG_REAL_TYPES_double
227     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::Vector< double, _Dim >, _Dim >( io );
228 #endif // cpPlugins_CONFIG_REAL_TYPES_double
229   }
230   else if( pt == itk::ImageIOBase::SYMMETRICSECONDRANKTENSOR )
231   {
232 #ifdef cpPlugins_CONFIG_REAL_TYPES_float
233     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::SymmetricSecondRankTensor< float, _Dim >, _Dim >( io );
234 #endif // cpPlugins_CONFIG_REAL_TYPES_float
235 #ifdef cpPlugins_CONFIG_REAL_TYPES_double
236     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::SymmetricSecondRankTensor< double, _Dim >, _Dim >( io );
237 #endif // cpPlugins_CONFIG_REAL_TYPES_double
238   }
239   else if( pt == itk::ImageIOBase::DIFFUSIONTENSOR3D )
240   {
241     if( _Dim == 3 )
242     {
243 #ifdef cpPlugins_CONFIG_REAL_TYPES_float
244       if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::DiffusionTensor3D< float >, _Dim >( io );
245 #endif // cpPlugins_CONFIG_REAL_TYPES_float
246 #ifdef cpPlugins_CONFIG_REAL_TYPES_double
247       if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::DiffusionTensor3D< double >, _Dim >( io );
248 #endif // cpPlugins_CONFIG_REAL_TYPES_double
249     }
250     else
251       this->_Error( "DiffusionTensor3D dimension not supported." );
252   }
253   else if( pt == itk::ImageIOBase::MATRIX )
254   {
255 #ifdef cpPlugins_CONFIG_REAL_TYPES_float
256     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::Matrix< float, _Dim, _Dim >, _Dim >( io );
257 #endif // cpPlugins_CONFIG_REAL_TYPES_float
258 #ifdef cpPlugins_CONFIG_REAL_TYPES_double
259     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::Matrix< double, _Dim, _Dim >, _Dim >( io );
260 #endif // cpPlugins_CONFIG_REAL_TYPES_double
261   }
262   else if( pt == itk::ImageIOBase::OFFSET )
263   {
264     this->_GD1< itk::Offset< _Dim >, _Dim >( io );
265   }
266   else if( pt == itk::ImageIOBase::FIXEDARRAY )
267   {
268   } // fi
269   return( success );
270 }
271
272 // -------------------------------------------------------------------------
273 template< class _TPixel, unsigned int _Dim >
274 bool cpPluginsITKIO::ImageReader::
275 _GD1( itk::ImageIOBase* io )
276 {
277   typedef itk::Image< _TPixel, _Dim > _TImage;
278
279   // Get filenames
280   auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
281   if( fnames.size( ) == 2 )
282   {
283     std::stringstream fname_str;
284     fname_str << fnames[ 0 ] << cpPlugins_PATH_SEPARATOR << fnames[ 1 ];
285
286     auto f = this->_CreateITK< itk::ImageFileReader< _TImage > >( );
287     f->SetFileName( fname_str.str( ) );
288     f->SetImageIO( io );
289     try
290     {
291       f->Update( );
292       this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
293       return( true );
294     }
295     catch( itk::ExceptionObject& err )
296     {
297       this->_Error( err.GetDescription( ) );
298       return( false );
299
300     } // yrt
301   }
302   else // if( fnames.size( ) > 1 )
303   {
304     auto f = this->_CreateITK< itk::ImageSeriesReader< _TImage > >( );
305     auto i = fnames.begin( );
306     std::stringstream dir;
307     dir << *i << cpPlugins_PATH_SEPARATOR;
308     i++;
309     for( ; i != fnames.end( ); ++i )
310       f->AddFileName( dir.str( ) + *i );
311     f->SetImageIO( io );
312     try
313     {
314       f->Update( );
315       this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
316       return( true );
317     }
318     catch( itk::ExceptionObject& err )
319     {
320       this->_Error( err.GetDescription( ) );
321       return( false );
322
323     } // yrt
324
325   } // fi
326 }
327
328 // eof - $RCSfile$