]> 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   }
259   else if( pt == itk::ImageIOBase::COVARIANTVECTOR )
260   {
261   }
262   else if( pt == itk::ImageIOBase::POINT )
263   {
264   }
265   else if( pt == itk::ImageIOBase::VECTOR )
266   {
267   }
268   else if( pt == itk::ImageIOBase::SYMMETRICSECONDRANKTENSOR )
269   {
270   }
271   else if( pt == itk::ImageIOBase::DIFFUSIONTENSOR3D )
272   {
273     if( _Dim == 3 )
274     {
275     }
276     else
277       this->_Error( "DiffusionTensor3D dimension not supported." );
278   }
279   else if( pt == itk::ImageIOBase::MATRIX )
280   {
281   }
282   else if( pt == itk::ImageIOBase::OFFSET )
283   {
284     this->_GD1< itk::Offset< _Dim >, _Dim >( io );
285   }
286   else if( pt == itk::ImageIOBase::FIXEDARRAY )
287   {
288   }
289   return( success );
290 }
291
292 // -------------------------------------------------------------------------
293 template< class _TPixel, unsigned int _Dim >
294 bool cpPluginsIO::ImageReader::
295 _GD1( itk::ImageIOBase* io )
296 {
297   typedef itk::Image< _TPixel, _Dim > _TImage;
298
299   // Get filenames
300   auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
301   if( fnames.size( ) == 1 )
302   {
303     auto f = this->_CreateITK< itk::ImageFileReader< _TImage > >( );
304     f->SetFileName( fnames[ 0 ] );
305     f->SetImageIO( io );
306     try
307     {
308       f->Update( );
309       this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
310       return( true );
311     }
312     catch( itk::ExceptionObject& err )
313     {
314       this->_Error( err.GetDescription( ) );
315       return( false );
316
317     } // yrt
318   }
319   else // if( fnames.size( ) > 1 )
320   {
321     auto f = this->_CreateITK< itk::ImageSeriesReader< _TImage > >( );
322     for( auto i = fnames.begin( ); i != fnames.end( ); ++i )
323       f->AddFileName( *i );
324     f->SetImageIO( io );
325     try
326     {
327       f->Update( );
328       this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
329       return( true );
330     }
331     catch( itk::ExceptionObject& err )
332     {
333       this->_Error( err.GetDescription( ) );
334       return( false );
335
336     } // yrt
337
338   } // fi
339 }
340
341 // eof - $RCSfile$