1 #include <ITKIO/ImageReader.h>
2 #include <cpInstances/Image.h>
3 #include <cpPlugins/QT/OpenFileDialog.h>
5 #define ITKIOImageBase_HIDDEN
6 #include <itkImageFileReader.h>
7 #include <itkImageSeriesReader.h>
8 #include <itkImageIOFactory.h>
11 # include <QApplication>
12 #endif // cpPlugins_QT4
14 // -------------------------------------------------------------------------
15 QDialog* cpPluginsITKIO::ImageReader::
19 cpPlugins::QT::OpenFileDialog* dlg = NULL;
20 if( QApplication::instance( ) != NULL )
22 dlg = new cpPlugins::QT::OpenFileDialog( );
23 dlg->SetParameters( &( this->m_Parameters ), "FileNames" );
27 #else // cpPlugins_QT4
29 #endif // cpPlugins_QT4
32 // -------------------------------------------------------------------------
33 cpPluginsITKIO::ImageReader::
37 this->_ConfigureOutput< cpInstances::Image >( "Output" );
38 this->m_Parameters.Clear( );
39 this->m_Parameters.ConfigureAsOpenFileNameList( "FileNames" );
40 this->m_Parameters.SetAcceptedFileExtensions(
42 "Image files (*.bmp *.png *.jpg *.jpeg *.dcm *.mhd *.nhdr *.nrrd *.tiff)"
46 // -------------------------------------------------------------------------
47 cpPluginsITKIO::ImageReader::
52 // -------------------------------------------------------------------------
53 void cpPluginsITKIO::ImageReader::
57 auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
58 if( fnames.size( ) > 1 )
60 std::stringstream fname_str;
61 fname_str << fnames[ 0 ] << cpPlugins_PATH_SEPARATOR << fnames[ 1 ];
62 std::string fname = fname_str.str( );
64 // Guess image properties
65 itk::ImageIOBase::Pointer io =
66 itk::ImageIOFactory::CreateImageIO(
67 fname.c_str( ), itk::ImageIOFactory::ReadMode
71 io->SetFileName( fname );
72 io->ReadImageInformation( );
74 unsigned int dim = io->GetNumberOfDimensions( );
75 #ifdef cpPlugins_CONFIG_PROCESS_DIMENSIONS_1
76 if( dim == 1 ) success = this->_GD0< 1 >( io );
77 #endif // cpPlugins_CONFIG_PROCESS_DIMENSIONS_1
78 #ifdef cpPlugins_CONFIG_PROCESS_DIMENSIONS_2
79 if( dim == 2 ) success = this->_GD0< 2 >( io );
80 #endif // cpPlugins_CONFIG_PROCESS_DIMENSIONS_2
81 #ifdef cpPlugins_CONFIG_PROCESS_DIMENSIONS_3
82 if( dim == 3 ) success = this->_GD0< 3 >( io );
83 #endif // cpPlugins_CONFIG_PROCESS_DIMENSIONS_3
84 #ifdef cpPlugins_CONFIG_PROCESS_DIMENSIONS_4
85 if( dim == 4 ) success = this->_GD0< 4 >( io );
86 #endif // cpPlugins_CONFIG_PROCESS_DIMENSIONS_4
88 this->_Error( "Image dimension not supported." );
92 std::string( "Could not create an ImageIO for \"" ) +
98 this->_Error( "No image(s) given" );
101 // -------------------------------------------------------------------------
102 template< unsigned int _Dim >
103 bool cpPluginsITKIO::ImageReader::
104 _GD0( itk::ImageIOBase* io )
106 typedef unsigned char uchar;
107 typedef unsigned short ushort;
108 typedef unsigned int uint;
109 typedef unsigned long ulong;
111 itk::ImageIOBase::IOComponentType ct = io->GetComponentType( );
112 itk::ImageIOBase::IOPixelType pt = io->GetPixelType( );
114 bool success = false;
115 if( pt == itk::ImageIOBase::SCALAR )
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 if( ct == itk::ImageIOBase::SHORT ) success = this->_GD1< short, _Dim >( io );
120 if( ct == itk::ImageIOBase::USHORT ) success = this->_GD1< ushort, _Dim >( io );
121 if( ct == itk::ImageIOBase::INT ) success = this->_GD1< int, _Dim >( io );
122 if( ct == itk::ImageIOBase::UINT ) success = this->_GD1< uint, _Dim >( io );
123 if( ct == itk::ImageIOBase::LONG ) success = this->_GD1< long, _Dim >( io );
124 if( ct == itk::ImageIOBase::ULONG ) success = this->_GD1< ulong, _Dim >( io );
125 if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< float, _Dim >( io );
126 if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< double, _Dim >( io );
128 else if( pt == itk::ImageIOBase::RGB )
130 if( ct == itk::ImageIOBase::CHAR ) success = this->_GD1< itk::RGBPixel< char >, _Dim >( io );
131 if( ct == itk::ImageIOBase::UCHAR ) success = this->_GD1< itk::RGBPixel< uchar >, _Dim >( io );
132 if( ct == itk::ImageIOBase::SHORT ) success = this->_GD1< itk::RGBPixel< short >, _Dim >( io );
133 if( ct == itk::ImageIOBase::USHORT ) success = this->_GD1< itk::RGBPixel< ushort >, _Dim >( io );
134 if( ct == itk::ImageIOBase::INT ) success = this->_GD1< itk::RGBPixel< int >, _Dim >( io );
135 if( ct == itk::ImageIOBase::UINT ) success = this->_GD1< itk::RGBPixel< uint >, _Dim >( io );
136 if( ct == itk::ImageIOBase::LONG ) success = this->_GD1< itk::RGBPixel< long >, _Dim >( io );
137 if( ct == itk::ImageIOBase::ULONG ) success = this->_GD1< itk::RGBPixel< ulong >, _Dim >( io );
138 if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::RGBPixel< float >, _Dim >( io );
139 if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::RGBPixel< double >, _Dim >( io );
141 else if( pt == itk::ImageIOBase::RGBA )
143 if( ct == itk::ImageIOBase::CHAR ) success = this->_GD1< itk::RGBAPixel< char >, _Dim >( io );
144 if( ct == itk::ImageIOBase::UCHAR ) success = this->_GD1< itk::RGBAPixel< uchar >, _Dim >( io );
145 if( ct == itk::ImageIOBase::SHORT ) success = this->_GD1< itk::RGBAPixel< short >, _Dim >( io );
146 if( ct == itk::ImageIOBase::USHORT ) success = this->_GD1< itk::RGBAPixel< ushort >, _Dim >( io );
147 if( ct == itk::ImageIOBase::INT ) success = this->_GD1< itk::RGBAPixel< int >, _Dim >( io );
148 if( ct == itk::ImageIOBase::UINT ) success = this->_GD1< itk::RGBAPixel< uint >, _Dim >( io );
149 if( ct == itk::ImageIOBase::LONG ) success = this->_GD1< itk::RGBAPixel< long >, _Dim >( io );
150 if( ct == itk::ImageIOBase::ULONG ) success = this->_GD1< itk::RGBAPixel< ulong >, _Dim >( io );
151 if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::RGBAPixel< float >, _Dim >( io );
152 if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::RGBAPixel< double >, _Dim >( io );
154 else if( pt == itk::ImageIOBase::COMPLEX )
156 if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< std::complex< float >, _Dim >( io );
157 if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< std::complex< double >, _Dim >( io );
159 else if( pt == itk::ImageIOBase::COVARIANTVECTOR )
161 if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::CovariantVector< float, _Dim >, _Dim >( io );
162 if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::CovariantVector< double, _Dim >, _Dim >( io );
164 else if( pt == itk::ImageIOBase::POINT )
166 if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::Point< float, _Dim >, _Dim >( io );
167 if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::Point< double, _Dim >, _Dim >( io );
169 else if( pt == itk::ImageIOBase::VECTOR )
171 if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::Vector< float, _Dim >, _Dim >( io );
172 if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::Vector< double, _Dim >, _Dim >( io );
174 else if( pt == itk::ImageIOBase::SYMMETRICSECONDRANKTENSOR )
176 if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::SymmetricSecondRankTensor< float, _Dim >, _Dim >( io );
177 if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::SymmetricSecondRankTensor< double, _Dim >, _Dim >( io );
179 else if( pt == itk::ImageIOBase::DIFFUSIONTENSOR3D )
183 if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::DiffusionTensor3D< float >, _Dim >( io );
184 if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::DiffusionTensor3D< double >, _Dim >( io );
187 this->_Error( "DiffusionTensor3D dimension not supported." );
189 else if( pt == itk::ImageIOBase::MATRIX )
191 if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::Matrix< float, _Dim, _Dim >, _Dim >( io );
192 if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::Matrix< double, _Dim, _Dim >, _Dim >( io );
194 else if( pt == itk::ImageIOBase::OFFSET )
196 this->_GD1< itk::Offset< _Dim >, _Dim >( io );
198 else if( pt == itk::ImageIOBase::FIXEDARRAY )
204 // -------------------------------------------------------------------------
205 template< class _TPixel, unsigned int _Dim >
206 bool cpPluginsITKIO::ImageReader::
207 _GD1( itk::ImageIOBase* io )
209 typedef itk::Image< _TPixel, _Dim > _TImage;
212 auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
213 if( fnames.size( ) == 2 )
215 std::stringstream fname_str;
216 fname_str << fnames[ 0 ] << cpPlugins_PATH_SEPARATOR << fnames[ 1 ];
218 auto f = this->_CreateITK< itk::ImageFileReader< _TImage > >( );
219 f->SetFileName( fname_str.str( ) );
224 this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
227 catch( itk::ExceptionObject& err )
229 this->_Error( err.GetDescription( ) );
234 else // if( fnames.size( ) > 1 )
236 auto f = this->_CreateITK< itk::ImageSeriesReader< _TImage > >( );
237 auto i = fnames.begin( );
238 std::stringstream dir;
239 dir << *i << cpPlugins_PATH_SEPARATOR;
241 for( ; i != fnames.end( ); ++i )
242 f->AddFileName( dir.str( ) + *i );
247 this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
250 catch( itk::ExceptionObject& err )
252 this->_Error( err.GetDescription( ) );