1 #include <plugins/IO/ImageReader.h>
2 #include <plugins/IO/ImageReaderQDialog.h>
3 #include <cpPlugins/DataObjects/Image.h>
5 #include <itkImageFileReader.h>
6 #include <itkImageSeriesReader.h>
8 #include <itkImageFileReader.hxx>
9 #include <itkImageSeriesReader.hxx>
10 #include <itkConvertPixelBuffer.hxx>
11 #include <itkImageAlgorithm.hxx>
12 #include <itkSimpleDataObjectDecorator.hxx>
16 #include <QApplication>
19 // -------------------------------------------------------------------------
20 cpPluginsIO::ImageReaderQDialog::
21 ImageReaderQDialog( QWidget* parent )
22 : QFileDialog( parent ),
23 m_ProcessObject( NULL )
26 this, SIGNAL( accepted( ) ), this, SLOT( _dlg_Accepted( ) )
28 this->setWindowTitle( "Open an(some) image(s)" );
31 // -------------------------------------------------------------------------
32 cpPluginsIO::ImageReaderQDialog::
33 ~ImageReaderQDialog( )
37 // -------------------------------------------------------------------------
38 void cpPluginsIO::ImageReaderQDialog::
39 setProcessObject( cpPlugins::BaseObjects::ProcessObject* obj )
43 this->m_ProcessObject = obj;
44 auto param = this->m_ProcessObject->GetParameters( );
45 auto extensions = param->GetAcceptedFileExtensions( "FileNames" );
46 auto files = param->GetOpenFileNameList( "FileNames" );
49 if( extensions != "" )
50 filters << extensions.c_str( );
51 filters << "Any file (*)";
52 this->setFileMode( QFileDialog::ExistingFiles );
53 this->setNameFilters( filters );
54 this->setAcceptMode( QFileDialog::AcceptOpen );
55 if( files.size( ) > 0 )
57 QFileInfo info( files[ 0 ].c_str( ) );
58 this->setDirectory( info.canonicalPath( ) );
63 // -------------------------------------------------------------------------
64 void cpPluginsIO::ImageReaderQDialog::
67 if( this->m_ProcessObject != NULL )
69 auto param = this->m_ProcessObject->GetParameters( );
70 auto files = this->selectedFiles( );
71 param->ClearOpenFileNameList( "FileNames" );
72 for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt )
73 param->AddToOpenFileNameList( "FileNames", fIt->toStdString( ) );
78 #endif // cpPlugins_QT4
80 // -------------------------------------------------------------------------
81 QDialog* cpPluginsIO::ImageReader::
85 ImageReaderQDialog* dlg = NULL;
86 if( QApplication::instance( ) != NULL )
88 dlg = new ImageReaderQDialog( );
89 dlg->setProcessObject( this );
93 #else // cpPlugins_QT4
95 #endif // cpPlugins_QT4
98 // -------------------------------------------------------------------------
99 cpPluginsIO::ImageReader::
103 this->_ConfigureOutput< cpPlugins::DataObjects::Image >( "Output" );
104 this->m_Parameters.Clear( );
105 this->m_Parameters.ConfigureAsOpenFileNameList( "FileNames" );
106 this->m_Parameters.SetAcceptedFileExtensions(
108 "Image files (*.bmp *.png *.jpg *.jpeg *.dcm *.mhd *.nhdr *.nrrd *.tiff)"
112 // -------------------------------------------------------------------------
113 cpPluginsIO::ImageReader::
118 // -------------------------------------------------------------------------
119 void cpPluginsIO::ImageReader::
123 auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
124 if( fnames.size( ) >= 1 )
126 // Guess image properties
127 itk::ImageIOBase::Pointer io =
128 itk::ImageIOFactory::CreateImageIO(
129 fnames[ 0 ].c_str( ),
130 itk::ImageIOFactory::ReadMode
132 if( io.IsNotNull( ) )
134 io->SetFileName( fnames[ 0 ] );
135 io->ReadImageInformation( );
136 if( fnames.size( ) >= 1 )
138 switch( io->GetNumberOfDimensions( ) )
140 case 1: this->_GD0< 1 >( io ); break;
141 case 2: this->_GD0< 2 >( io ); break;
142 case 3: this->_GD0< 3 >( io ); break;
143 case 4: this->_GD0< 4 >( io ); break;
145 this->_Error( "Image dimension not supported." );
153 std::string( "Could not create an ImageIO for \"" ) +
159 this->_Error( "No image(s) given" );
162 // -------------------------------------------------------------------------
163 template< unsigned int _Dim >
164 void cpPluginsIO::ImageReader::
165 _GD0( itk::ImageIOBase* io )
167 typedef unsigned char uchar;
168 typedef unsigned short ushort;
169 typedef unsigned int uint;
170 typedef unsigned long ulong;
172 itk::ImageIOBase::IOComponentType ct = io->GetComponentType( );
173 itk::ImageIOBase::IOPixelType pt = io->GetPixelType( );
175 if( pt == itk::ImageIOBase::SCALAR )
179 case itk::ImageIOBase::CHAR : this->_GD1< char, _Dim >( io ); break;
180 case itk::ImageIOBase::SHORT : this->_GD1< short, _Dim >( io ); break;
181 case itk::ImageIOBase::INT : this->_GD1< int, _Dim >( io ); break;
182 case itk::ImageIOBase::LONG : this->_GD1< long, _Dim >( io ); break;
183 case itk::ImageIOBase::FLOAT : this->_GD1< float, _Dim >( io ); break;
184 case itk::ImageIOBase::DOUBLE : this->_GD1< double, _Dim >( io ); break;
185 case itk::ImageIOBase::UCHAR : this->_GD1< uchar, _Dim >( io ); break;
186 case itk::ImageIOBase::USHORT : this->_GD1< ushort, _Dim >( io ); break;
187 case itk::ImageIOBase::UINT : this->_GD1< uint, _Dim >( io ); break;
188 case itk::ImageIOBase::ULONG : this->_GD1< ulong, _Dim >( io ); break;
189 default: this->_Error( "Scalar pixel type not supported." ); break;
192 else if( pt == itk::ImageIOBase::RGB )
196 case itk::ImageIOBase::CHAR : this->_GD1< itk::RGBPixel< char >, _Dim >( io ); break;
197 case itk::ImageIOBase::SHORT : this->_GD1< itk::RGBPixel< short >, _Dim >( io ); break;
198 case itk::ImageIOBase::INT : this->_GD1< itk::RGBPixel< int >, _Dim >( io ); break;
199 case itk::ImageIOBase::LONG : this->_GD1< itk::RGBPixel< long >, _Dim >( io ); break;
200 case itk::ImageIOBase::FLOAT : this->_GD1< itk::RGBPixel< float >, _Dim >( io ); break;
201 case itk::ImageIOBase::DOUBLE : this->_GD1< itk::RGBPixel< double >, _Dim >( io ); break;
202 case itk::ImageIOBase::UCHAR : this->_GD1< itk::RGBPixel< uchar >, _Dim >( io ); break;
203 case itk::ImageIOBase::USHORT : this->_GD1< itk::RGBPixel< ushort >, _Dim >( io ); break;
204 case itk::ImageIOBase::UINT : this->_GD1< itk::RGBPixel< uint >, _Dim >( io ); break;
205 case itk::ImageIOBase::ULONG : this->_GD1< itk::RGBPixel< ulong >, _Dim >( io ); break;
206 default: this->_Error( "RGB pixel type not supported." ); break;
209 else if( pt == itk::ImageIOBase::RGBA )
213 case itk::ImageIOBase::CHAR : this->_GD1< itk::RGBAPixel< char >, _Dim >( io ); break;
214 case itk::ImageIOBase::SHORT : this->_GD1< itk::RGBAPixel< short >, _Dim >( io ); break;
215 case itk::ImageIOBase::INT : this->_GD1< itk::RGBAPixel< int >, _Dim >( io ); break;
216 case itk::ImageIOBase::LONG : this->_GD1< itk::RGBAPixel< long >, _Dim >( io ); break;
217 case itk::ImageIOBase::FLOAT : this->_GD1< itk::RGBAPixel< float >, _Dim >( io ); break;
218 case itk::ImageIOBase::DOUBLE : this->_GD1< itk::RGBAPixel< double >, _Dim >( io ); break;
219 case itk::ImageIOBase::UCHAR : this->_GD1< itk::RGBAPixel< uchar >, _Dim >( io ); break;
220 case itk::ImageIOBase::USHORT : this->_GD1< itk::RGBAPixel< ushort >, _Dim >( io ); break;
221 case itk::ImageIOBase::UINT : this->_GD1< itk::RGBAPixel< uint >, _Dim >( io ); break;
222 case itk::ImageIOBase::ULONG : this->_GD1< itk::RGBAPixel< ulong >, _Dim >( io ); break;
223 default: this->_Error( "RGBA pixel type not supported." ); break;
226 else if( pt == itk::ImageIOBase::COMPLEX )
230 case itk::ImageIOBase::FLOAT : this->_GD1< std::complex< float >, _Dim >( io ); break;
231 case itk::ImageIOBase::DOUBLE : this->_GD1< std::complex< double >, _Dim >( io ); break;
232 default: this->_Error( "Complex pixel type not supported." ); break;
235 else if( pt == itk::ImageIOBase::COVARIANTVECTOR )
239 case itk::ImageIOBase::FLOAT : this->_GD1< itk::CovariantVector< float, _Dim >, _Dim >( io ); break;
240 case itk::ImageIOBase::DOUBLE : this->_GD1< itk::CovariantVector< double, _Dim >, _Dim >( io ); break;
241 default: this->_Error( "CovariantVector pixel type not supported." ); break;
244 else if( pt == itk::ImageIOBase::POINT )
248 case itk::ImageIOBase::FLOAT : this->_GD1< itk::Point< float, _Dim >, _Dim >( io ); break;
249 case itk::ImageIOBase::DOUBLE : this->_GD1< itk::Point< double, _Dim >, _Dim >( io ); break;
250 default: this->_Error( "Point pixel type not supported." ); break;
253 else if( pt == itk::ImageIOBase::VECTOR )
257 case itk::ImageIOBase::FLOAT : this->_GD1< itk::Vector< float, _Dim >, _Dim >( io ); break;
258 case itk::ImageIOBase::DOUBLE : this->_GD1< itk::Vector< double, _Dim >, _Dim >( io ); break;
259 default: this->_Error( "Vector pixel type not supported." ); break;
262 else if( pt == itk::ImageIOBase::SYMMETRICSECONDRANKTENSOR )
266 case itk::ImageIOBase::FLOAT : this->_GD1< itk::SymmetricSecondRankTensor< float, _Dim >, _Dim >( io ); break;
267 case itk::ImageIOBase::DOUBLE : this->_GD1< itk::SymmetricSecondRankTensor< double, _Dim >, _Dim >( io ); break;
268 default: this->_Error( "SymmetricSecondRankTensor pixel type not supported." ); break;
271 else if( pt == itk::ImageIOBase::DIFFUSIONTENSOR3D )
277 case itk::ImageIOBase::FLOAT : this->_GD1< itk::DiffusionTensor3D< float >, 3 >( io ); break;
278 case itk::ImageIOBase::DOUBLE : this->_GD1< itk::DiffusionTensor3D< double >, 3 >( io ); break;
279 default: this->_Error( "DiffusionTensor3D pixel type not supported." ); break;
283 this->_Error( "DiffusionTensor3D dimension not supported." );
285 else if( pt == itk::ImageIOBase::MATRIX )
289 case itk::ImageIOBase::FLOAT : this->_GD1< itk::Matrix< float, _Dim, _Dim >, _Dim >( io ); break;
290 case itk::ImageIOBase::DOUBLE : this->_GD1< itk::Matrix< double, _Dim, _Dim >, _Dim >( io ); break;
291 default: this->_Error( "Matrix pixel type not supported." ); break;
294 else if( pt == itk::ImageIOBase::OFFSET )
296 this->_GD1< itk::Offset< _Dim >, _Dim >( io );
298 else if( pt == itk::ImageIOBase::FIXEDARRAY )
302 case itk::ImageIOBase::CHAR : this->_GD1< itk::FixedArray< char, _Dim >, _Dim >( io ); break;
303 case itk::ImageIOBase::SHORT : this->_GD1< itk::FixedArray< short, _Dim >, _Dim >( io ); break;
304 case itk::ImageIOBase::INT : this->_GD1< itk::FixedArray< int, _Dim >, _Dim >( io ); break;
305 case itk::ImageIOBase::LONG : this->_GD1< itk::FixedArray< long, _Dim >, _Dim >( io ); break;
306 case itk::ImageIOBase::FLOAT : this->_GD1< itk::FixedArray< float, _Dim >, _Dim >( io ); break;
307 case itk::ImageIOBase::DOUBLE : this->_GD1< itk::FixedArray< double, _Dim >, _Dim >( io ); break;
308 case itk::ImageIOBase::UCHAR : this->_GD1< itk::FixedArray< uchar, _Dim >, _Dim >( io ); break;
309 case itk::ImageIOBase::USHORT : this->_GD1< itk::FixedArray< ushort, _Dim >, _Dim >( io ); break;
310 case itk::ImageIOBase::UINT : this->_GD1< itk::FixedArray< uint, _Dim >, _Dim >( io ); break;
311 case itk::ImageIOBase::ULONG : this->_GD1< itk::FixedArray< ulong, _Dim >, _Dim >( io ); break;
312 default: this->_Error( "Scalar pixel type not supported." ); break;
316 this->_Error( "Image pixel type not yet supported." );
319 // -------------------------------------------------------------------------
320 template< class _TPixel, unsigned int _Dim >
321 void cpPluginsIO::ImageReader::
322 _GD1( itk::ImageIOBase* io )
324 typedef itk::Image< _TPixel, _Dim > _TImage;
327 auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
328 if( fnames.size( ) == 1 )
330 auto f = this->_CreateITK< itk::ImageFileReader< _TImage > >( );
331 f->SetFileName( fnames[ 0 ] );
336 this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
338 catch( itk::ExceptionObject& err )
340 this->_Error( err.GetDescription( ) );
344 else // if( fnames.size( ) > 1 )
346 auto f = this->_CreateITK< itk::ImageSeriesReader< _TImage > >( );
347 for( auto i = fnames.begin( ); i != fnames.end( ); ++i )
348 f->AddFileName( *i );
353 this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
355 catch( itk::ExceptionObject& err )
357 this->_Error( err.GetDescription( ) );