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