]> Creatis software - cpPlugins.git/blob - plugins/IO/ImageReader.cxx
...
[cpPlugins.git] / plugins / IO / ImageReader.cxx
1 #include <IO/ImageReader.h>
2 #include <IO/ImageReaderQDialog.h>
3 #include <cpPlugins/DataObjects/Image.h>
4
5 #include <itkImageFileReader.h>
6 #include <itkImageSeriesReader.h>
7 #include <itkImageIOFactory.h>
8
9 #ifdef cpPlugins_QT4
10
11 #include <QApplication>
12 #include <QFileInfo>
13
14 // -------------------------------------------------------------------------
15 cpPluginsIO::ImageReaderQDialog::
16 ImageReaderQDialog( QWidget* parent )
17   : QFileDialog( parent ),
18     m_ProcessObject( NULL )
19 {
20   this->connect(
21     this, SIGNAL( accepted( ) ), this, SLOT( _dlg_Accepted( ) )
22     );
23   this->setWindowTitle( "Open an(some) image(s)" );
24 }
25
26 // -------------------------------------------------------------------------
27 cpPluginsIO::ImageReaderQDialog::
28 ~ImageReaderQDialog( )
29 {
30 }
31
32 // -------------------------------------------------------------------------
33 void cpPluginsIO::ImageReaderQDialog::
34 setProcessObject( cpPlugins::BaseObjects::ProcessObject* obj )
35 {
36   if( obj == NULL )
37     return;
38   this->m_ProcessObject = obj;
39   auto param = this->m_ProcessObject->GetParameters( );
40   auto extensions = param->GetAcceptedFileExtensions( "FileNames" );
41   auto files = param->GetOpenFileNameList( "FileNames" );
42
43   QStringList filters;
44   if( extensions != "" )
45     filters << extensions.c_str( );
46   filters << "Any file (*)";
47   this->setFileMode( QFileDialog::ExistingFiles );
48   this->setNameFilters( filters );
49   this->setAcceptMode( QFileDialog::AcceptOpen );
50   if( files.size( ) > 0 )
51   {
52     QFileInfo info( files[ 0 ].c_str( ) );
53     this->setDirectory( info.canonicalPath( ) );
54
55   } // fi
56 }
57
58 // -------------------------------------------------------------------------
59 void cpPluginsIO::ImageReaderQDialog::
60 _dlg_Accepted( )
61 {
62   if( this->m_ProcessObject != NULL )
63   {
64     auto param = this->m_ProcessObject->GetParameters( );
65     auto files = this->selectedFiles( );
66     param->ClearOpenFileNameList( "FileNames" );
67     for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt )
68       param->AddToOpenFileNameList( "FileNames", fIt->toStdString( ) );
69
70   } // fi
71 }
72
73 #endif // cpPlugins_QT4
74
75 // -------------------------------------------------------------------------
76 QDialog* cpPluginsIO::ImageReader::
77 CreateQDialog( )
78 {
79 #ifdef cpPlugins_QT4
80   ImageReaderQDialog* dlg = NULL;
81   if( QApplication::instance( ) != NULL )
82   {
83     dlg = new ImageReaderQDialog( );
84     dlg->setProcessObject( this );
85
86   } // fi
87   return( dlg );
88 #else // cpPlugins_QT4
89   return( NULL );
90 #endif // cpPlugins_QT4
91 }
92
93 // -------------------------------------------------------------------------
94 cpPluginsIO::ImageReader::
95 ImageReader( )
96   : Superclass( )
97 {
98   this->_ConfigureOutput< cpPlugins::DataObjects::Image >( "Output" );
99   this->m_Parameters.Clear( );
100   this->m_Parameters.ConfigureAsOpenFileNameList( "FileNames" );
101   this->m_Parameters.SetAcceptedFileExtensions(
102     "FileNames",
103     "Image files (*.bmp *.png *.jpg *.jpeg *.dcm *.mhd *.nhdr *.nrrd *.tiff)"
104     );
105 }
106
107 // -------------------------------------------------------------------------
108 cpPluginsIO::ImageReader::
109 ~ImageReader( )
110 {
111 }
112
113 // -------------------------------------------------------------------------
114 void cpPluginsIO::ImageReader::
115 _GenerateData( )
116 {
117   // Get filenames
118   auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
119   if( fnames.size( ) >= 1 )
120   {
121     // Guess image properties
122     itk::ImageIOBase::Pointer io =
123       itk::ImageIOFactory::CreateImageIO(
124         fnames[ 0 ].c_str( ),
125         itk::ImageIOFactory::ReadMode
126         );
127     if( io.IsNotNull( ) )
128     {
129       io->SetFileName( fnames[ 0 ] );
130       io->ReadImageInformation( );
131       if( fnames.size( ) >= 1 )
132       {
133         bool success = false;
134         unsigned int dim = io->GetNumberOfDimensions( );
135 #ifdef cpPlugins_CONFIG_PROCESS_DIMENSIONS_1
136         if( dim == 1 ) success = this->_GD0< 1 >( io );
137 #endif // cpPlugins_CONFIG_PROCESS_DIMENSIONS_1
138 #ifdef cpPlugins_CONFIG_PROCESS_DIMENSIONS_2
139         if( dim == 2 ) success = this->_GD0< 2 >( io );
140 #endif // cpPlugins_CONFIG_PROCESS_DIMENSIONS_2
141 #ifdef cpPlugins_CONFIG_PROCESS_DIMENSIONS_3
142         if( dim == 3 ) success = this->_GD0< 3 >( io );
143 #endif // cpPlugins_CONFIG_PROCESS_DIMENSIONS_3
144 #ifdef cpPlugins_CONFIG_PROCESS_DIMENSIONS_4
145         if( dim == 4 ) success = this->_GD0< 4 >( io );
146 #endif // cpPlugins_CONFIG_PROCESS_DIMENSIONS_4
147         if( !success )
148           this->_Error( "Image dimension not supported." );
149
150       } // fi
151     }
152     else
153       this->_Error(
154         std::string( "Could not create an ImageIO for \"" ) +
155         fnames[ 0 ] +
156         std::string( "\"" )
157         );
158   }
159   else
160     this->_Error( "No image(s) given" );
161 }
162
163 // -------------------------------------------------------------------------
164 template< unsigned int _Dim >
165 bool cpPluginsIO::ImageReader::
166 _GD0( itk::ImageIOBase* io )
167 {
168   typedef unsigned char  uchar;
169   typedef unsigned short ushort;
170   typedef unsigned int   uint;
171   typedef unsigned long  ulong;
172
173   itk::ImageIOBase::IOComponentType ct = io->GetComponentType( );
174   itk::ImageIOBase::IOPixelType pt = io->GetPixelType( );
175
176   bool success = false;
177   if( pt == itk::ImageIOBase::SCALAR )
178   {
179 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_char
180     if( ct == itk::ImageIOBase::CHAR ) success = this->_GD1< char, _Dim >( io );
181     if( ct == itk::ImageIOBase::UCHAR ) success = this->_GD1< uchar, _Dim >( io );
182 #endif // cpPlugins_CONFIG_INTEGER_TYPES_char
183 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_short
184     if( ct == itk::ImageIOBase::SHORT ) success = this->_GD1< short, _Dim >( io );
185     if( ct == itk::ImageIOBase::USHORT ) success = this->_GD1< ushort, _Dim >( io );
186 #endif // cpPlugins_CONFIG_INTEGER_TYPES_short
187 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_int
188     if( ct == itk::ImageIOBase::INT ) success = this->_GD1< int, _Dim >( io );
189     if( ct == itk::ImageIOBase::UINT ) success = this->_GD1< uint, _Dim >( io );
190 #endif // cpPlugins_CONFIG_INTEGER_TYPES_int
191 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_long
192     if( ct == itk::ImageIOBase::LONG ) success = this->_GD1< long, _Dim >( io );
193     if( ct == itk::ImageIOBase::ULONG ) success = this->_GD1< ulong, _Dim >( io );
194 #endif // cpPlugins_CONFIG_INTEGER_TYPES_long
195 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_float
196     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< float, _Dim >( io );
197 #endif // cpPlugins_CONFIG_INTEGER_TYPES_float
198 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_double
199     if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< double, _Dim >( io );
200 #endif // cpPlugins_CONFIG_INTEGER_TYPES_double
201   }
202   else if( pt == itk::ImageIOBase::RGB )
203   {
204 #ifdef cpPlugins_CONFIG_COLOR_PIXELS_RGBPixel
205 #  ifdef cpPlugins_CONFIG_INTEGER_TYPES_char
206     if( ct == itk::ImageIOBase::CHAR ) success = this->_GD1< itk::RGBPixel< char >, _Dim >( io );
207     if( ct == itk::ImageIOBase::UCHAR ) success = this->_GD1< itk::RGBPixel< uchar >, _Dim >( io );
208 #  endif // cpPlugins_CONFIG_INTEGER_TYPES_char
209 #  ifdef cpPlugins_CONFIG_INTEGER_TYPES_short
210     if( ct == itk::ImageIOBase::SHORT ) success = this->_GD1< itk::RGBPixel< short >, _Dim >( io );
211     if( ct == itk::ImageIOBase::USHORT ) success = this->_GD1< itk::RGBPixel< ushort >, _Dim >( io );
212 #  endif // cpPlugins_CONFIG_INTEGER_TYPES_short
213 #  ifdef cpPlugins_CONFIG_INTEGER_TYPES_int
214     if( ct == itk::ImageIOBase::INT ) success = this->_GD1< itk::RGBPixel< int >, _Dim >( io );
215     if( ct == itk::ImageIOBase::UINT ) success = this->_GD1< itk::RGBPixel< uint >, _Dim >( io );
216 #  endif // cpPlugins_CONFIG_INTEGER_TYPES_int
217 #  ifdef cpPlugins_CONFIG_INTEGER_TYPES_long
218     if( ct == itk::ImageIOBase::LONG ) success = this->_GD1< itk::RGBPixel< long >, _Dim >( io );
219     if( ct == itk::ImageIOBase::ULONG ) success = this->_GD1< itk::RGBPixel< ulong >, _Dim >( io );
220 #  endif // cpPlugins_CONFIG_INTEGER_TYPES_long
221 #  ifdef cpPlugins_CONFIG_INTEGER_TYPES_float
222     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::RGBPixel< float >, _Dim >( io );
223 #  endif // cpPlugins_CONFIG_INTEGER_TYPES_float
224 #  ifdef cpPlugins_CONFIG_INTEGER_TYPES_double
225     if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::RGBPixel< double >, _Dim >( io );
226 #  endif // cpPlugins_CONFIG_INTEGER_TYPES_double
227 #endif // cpPlugins_CONFIG_COLOR_PIXELS_RGBPixel
228   }
229   else if( pt == itk::ImageIOBase::RGBA )
230   {
231 #ifdef cpPlugins_CONFIG_COLOR_PIXELS_RGBAPixel
232 #  ifdef cpPlugins_CONFIG_INTEGER_TYPES_char
233     if( ct == itk::ImageIOBase::CHAR ) success = this->_GD1< itk::RGBAPixel< char >, _Dim >( io );
234     if( ct == itk::ImageIOBase::UCHAR ) success = this->_GD1< itk::RGBAPixel< uchar >, _Dim >( io );
235 #  endif // cpPlugins_CONFIG_INTEGER_TYPES_char
236 #  ifdef cpPlugins_CONFIG_INTEGER_TYPES_short
237     if( ct == itk::ImageIOBase::SHORT ) success = this->_GD1< itk::RGBAPixel< short >, _Dim >( io );
238     if( ct == itk::ImageIOBase::USHORT ) success = this->_GD1< itk::RGBAPixel< ushort >, _Dim >( io );
239 #  endif // cpPlugins_CONFIG_INTEGER_TYPES_short
240 #  ifdef cpPlugins_CONFIG_INTEGER_TYPES_int
241     if( ct == itk::ImageIOBase::INT ) success = this->_GD1< itk::RGBAPixel< int >, _Dim >( io );
242     if( ct == itk::ImageIOBase::UINT ) success = this->_GD1< itk::RGBAPixel< uint >, _Dim >( io );
243 #  endif // cpPlugins_CONFIG_INTEGER_TYPES_int
244 #  ifdef cpPlugins_CONFIG_INTEGER_TYPES_long
245     if( ct == itk::ImageIOBase::LONG ) success = this->_GD1< itk::RGBAPixel< long >, _Dim >( io );
246     if( ct == itk::ImageIOBase::ULONG ) success = this->_GD1< itk::RGBAPixel< ulong >, _Dim >( io );
247 #  endif // cpPlugins_CONFIG_INTEGER_TYPES_long
248 #  ifdef cpPlugins_CONFIG_INTEGER_TYPES_float
249     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::RGBAPixel< float >, _Dim >( io );
250 #  endif // cpPlugins_CONFIG_INTEGER_TYPES_float
251 #  ifdef cpPlugins_CONFIG_INTEGER_TYPES_double
252     if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::RGBAPixel< double >, _Dim >( io );
253 #  endif // cpPlugins_CONFIG_INTEGER_TYPES_double
254 #endif // cpPlugins_CONFIG_COLOR_PIXELS_RGBAPixel
255   }
256   else if( pt == itk::ImageIOBase::COMPLEX )
257   {
258 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_float
259     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< std::complex< float >, _Dim >( io );
260 #endif // cpPlugins_CONFIG_INTEGER_TYPES_float
261 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_double
262     if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< std::complex< double >, _Dim >( io );
263 #endif // cpPlugins_CONFIG_INTEGER_TYPES_double
264   }
265   else if( pt == itk::ImageIOBase::COVARIANTVECTOR )
266   {
267 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_float
268     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::CovariantVector< float, _Dim >, _Dim >( io );
269 #endif // cpPlugins_CONFIG_INTEGER_TYPES_float
270 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_double
271     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::CovariantVector< double, _Dim >, _Dim >( io );
272 #endif // cpPlugins_CONFIG_INTEGER_TYPES_double
273   }
274   else if( pt == itk::ImageIOBase::POINT )
275   {
276 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_float
277     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::Point< float, _Dim >, _Dim >( io );
278 #endif // cpPlugins_CONFIG_INTEGER_TYPES_float
279 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_double
280     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::Point< double, _Dim >, _Dim >( io );
281 #endif // cpPlugins_CONFIG_INTEGER_TYPES_double
282   }
283   else if( pt == itk::ImageIOBase::VECTOR )
284   {
285 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_float
286     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::Vector< float, _Dim >, _Dim >( io );
287 #endif // cpPlugins_CONFIG_INTEGER_TYPES_float
288 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_double
289     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::Vector< double, _Dim >, _Dim >( io );
290 #endif // cpPlugins_CONFIG_INTEGER_TYPES_double
291   }
292   else if( pt == itk::ImageIOBase::SYMMETRICSECONDRANKTENSOR )
293   {
294 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_float
295     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::SymmetricSecondRankTensor< float, _Dim >, _Dim >( io );
296 #endif // cpPlugins_CONFIG_INTEGER_TYPES_float
297 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_double
298     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::SymmetricSecondRankTensor< double, _Dim >, _Dim >( io );
299 #endif // cpPlugins_CONFIG_INTEGER_TYPES_double
300   }
301   else if( pt == itk::ImageIOBase::DIFFUSIONTENSOR3D )
302   {
303     if( _Dim == 3 )
304     {
305 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_float
306       if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::DiffusionTensor3D< float >, _Dim >( io );
307 #endif // cpPlugins_CONFIG_INTEGER_TYPES_float
308 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_double
309       if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::DiffusionTensor3D< double >, _Dim >( io );
310 #endif // cpPlugins_CONFIG_INTEGER_TYPES_double
311     }
312     else
313       this->_Error( "DiffusionTensor3D dimension not supported." );
314   }
315   else if( pt == itk::ImageIOBase::MATRIX )
316   {
317 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_float
318     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::Matrix< float, _Dim, _Dim >, _Dim >( io );
319 #endif // cpPlugins_CONFIG_INTEGER_TYPES_float
320 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_double
321     if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::Matrix< double, _Dim, _Dim >, _Dim >( io );
322 #endif // cpPlugins_CONFIG_INTEGER_TYPES_double
323   }
324   else if( pt == itk::ImageIOBase::OFFSET )
325   {
326     this->_GD1< itk::Offset< _Dim >, _Dim >( io );
327   }
328   else if( pt == itk::ImageIOBase::FIXEDARRAY )
329   {
330   } // fi
331   return( success );
332 }
333
334 // -------------------------------------------------------------------------
335 template< class _TPixel, unsigned int _Dim >
336 bool cpPluginsIO::ImageReader::
337 _GD1( itk::ImageIOBase* io )
338 {
339   typedef itk::Image< _TPixel, _Dim > _TImage;
340
341   // Get filenames
342   auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
343   if( fnames.size( ) == 1 )
344   {
345     auto f = this->_CreateITK< itk::ImageFileReader< _TImage > >( );
346     f->SetFileName( fnames[ 0 ] );
347     f->SetImageIO( io );
348     try
349     {
350       f->Update( );
351       this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
352       return( true );
353     }
354     catch( itk::ExceptionObject& err )
355     {
356       this->_Error( err.GetDescription( ) );
357       return( false );
358
359     } // yrt
360   }
361   else // if( fnames.size( ) > 1 )
362   {
363     auto f = this->_CreateITK< itk::ImageSeriesReader< _TImage > >( );
364     for( auto i = fnames.begin( ); i != fnames.end( ); ++i )
365       f->AddFileName( *i );
366     f->SetImageIO( io );
367     try
368     {
369       f->Update( );
370       this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
371       return( true );
372     }
373     catch( itk::ExceptionObject& err )
374     {
375       this->_Error( err.GetDescription( ) );
376       return( false );
377
378     } // yrt
379
380   } // fi
381 }
382
383 // eof - $RCSfile$