1 #include <IO/ImageReader.h>
2 #include <IO/ImageReaderQDialog.h>
3 #include <cpPlugins/DataObjects/Image.h>
5 #include <itkImageFileReader.h>
6 #include <itkImageSeriesReader.h>
7 #include <itkImageIOFactory.h>
11 #include <QApplication>
14 // -------------------------------------------------------------------------
15 cpPluginsIO::ImageReaderQDialog::
16 ImageReaderQDialog( QWidget* parent )
17 : QFileDialog( parent ),
18 m_ProcessObject( NULL )
21 this, SIGNAL( accepted( ) ), this, SLOT( _dlg_Accepted( ) )
23 this->setWindowTitle( "Open an(some) image(s)" );
26 // -------------------------------------------------------------------------
27 cpPluginsIO::ImageReaderQDialog::
28 ~ImageReaderQDialog( )
32 // -------------------------------------------------------------------------
33 void cpPluginsIO::ImageReaderQDialog::
34 setProcessObject( cpPlugins::BaseObjects::ProcessObject* obj )
38 this->m_ProcessObject = obj;
39 auto param = this->m_ProcessObject->GetParameters( );
40 auto extensions = param->GetAcceptedFileExtensions( "FileNames" );
41 auto files = param->GetOpenFileNameList( "FileNames" );
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 )
52 QFileInfo info( files[ 0 ].c_str( ) );
53 this->setDirectory( info.canonicalPath( ) );
58 // -------------------------------------------------------------------------
59 void cpPluginsIO::ImageReaderQDialog::
62 if( this->m_ProcessObject != NULL )
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( ) );
73 #endif // cpPlugins_QT4
75 // -------------------------------------------------------------------------
76 QDialog* cpPluginsIO::ImageReader::
80 ImageReaderQDialog* dlg = NULL;
81 if( QApplication::instance( ) != NULL )
83 dlg = new ImageReaderQDialog( );
84 dlg->setProcessObject( this );
88 #else // cpPlugins_QT4
90 #endif // cpPlugins_QT4
93 // -------------------------------------------------------------------------
94 cpPluginsIO::ImageReader::
98 this->_ConfigureOutput< cpPlugins::DataObjects::Image >( "Output" );
99 this->m_Parameters.Clear( );
100 this->m_Parameters.ConfigureAsOpenFileNameList( "FileNames" );
101 this->m_Parameters.SetAcceptedFileExtensions(
103 "Image files (*.bmp *.png *.jpg *.jpeg *.dcm *.mhd *.nhdr *.nrrd *.tiff)"
107 // -------------------------------------------------------------------------
108 cpPluginsIO::ImageReader::
113 // -------------------------------------------------------------------------
114 void cpPluginsIO::ImageReader::
118 auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
119 if( fnames.size( ) >= 1 )
121 // Guess image properties
122 itk::ImageIOBase::Pointer io =
123 itk::ImageIOFactory::CreateImageIO(
124 fnames[ 0 ].c_str( ),
125 itk::ImageIOFactory::ReadMode
127 if( io.IsNotNull( ) )
129 io->SetFileName( fnames[ 0 ] );
130 io->ReadImageInformation( );
131 if( fnames.size( ) >= 1 )
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
148 this->_Error( "Image dimension not supported." );
154 std::string( "Could not create an ImageIO for \"" ) +
160 this->_Error( "No image(s) given" );
163 // -------------------------------------------------------------------------
164 template< unsigned int _Dim >
165 bool cpPluginsIO::ImageReader::
166 _GD0( itk::ImageIOBase* io )
168 typedef unsigned char uchar;
169 typedef unsigned short ushort;
170 typedef unsigned int uint;
171 typedef unsigned long ulong;
173 itk::ImageIOBase::IOComponentType ct = io->GetComponentType( );
174 itk::ImageIOBase::IOPixelType pt = io->GetPixelType( );
176 bool success = false;
177 if( pt == itk::ImageIOBase::SCALAR )
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
202 else if( pt == itk::ImageIOBase::RGB )
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
229 else if( pt == itk::ImageIOBase::RGBA )
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
256 else if( pt == itk::ImageIOBase::COMPLEX )
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
265 else if( pt == itk::ImageIOBase::COVARIANTVECTOR )
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
274 else if( pt == itk::ImageIOBase::POINT )
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
283 else if( pt == itk::ImageIOBase::VECTOR )
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
292 else if( pt == itk::ImageIOBase::SYMMETRICSECONDRANKTENSOR )
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
301 else if( pt == itk::ImageIOBase::DIFFUSIONTENSOR3D )
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
313 this->_Error( "DiffusionTensor3D dimension not supported." );
315 else if( pt == itk::ImageIOBase::MATRIX )
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
324 else if( pt == itk::ImageIOBase::OFFSET )
326 this->_GD1< itk::Offset< _Dim >, _Dim >( io );
328 else if( pt == itk::ImageIOBase::FIXEDARRAY )
334 // -------------------------------------------------------------------------
335 template< class _TPixel, unsigned int _Dim >
336 bool cpPluginsIO::ImageReader::
337 _GD1( itk::ImageIOBase* io )
339 typedef itk::Image< _TPixel, _Dim > _TImage;
342 auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
343 if( fnames.size( ) == 1 )
345 auto f = this->_CreateITK< itk::ImageFileReader< _TImage > >( );
346 f->SetFileName( fnames[ 0 ] );
351 this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
354 catch( itk::ExceptionObject& err )
356 this->_Error( err.GetDescription( ) );
361 else // if( fnames.size( ) > 1 )
363 auto f = this->_CreateITK< itk::ImageSeriesReader< _TImage > >( );
364 for( auto i = fnames.begin( ); i != fnames.end( ); ++i )
365 f->AddFileName( *i );
370 this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
373 catch( itk::ExceptionObject& err )
375 this->_Error( err.GetDescription( ) );