1 #include <IO/ImageReader.h>
2 #include <cpPlugins/DataObjects/Image.h>
3 #include <cpPlugins/QT/OpenFileDialog.h>
5 #include <itkImageFileReader.h>
6 #include <itkImageSeriesReader.h>
7 #include <itkImageIOFactory.h>
10 # include <QApplication>
11 #endif // cpPlugins_QT4
13 // -------------------------------------------------------------------------
14 QDialog* cpPluginsIO::ImageReader::
18 cpPlugins::QT::OpenFileDialog* dlg = NULL;
19 if( QApplication::instance( ) != NULL )
21 dlg = new cpPlugins::QT::OpenFileDialog( );
22 dlg->SetParameters( &( this->m_Parameters ), "FileNames" );
26 #else // cpPlugins_QT4
28 #endif // cpPlugins_QT4
31 // -------------------------------------------------------------------------
32 cpPluginsIO::ImageReader::
36 this->_ConfigureOutput< cpPlugins::DataObjects::Image >( "Output" );
37 this->m_Parameters.Clear( );
38 this->m_Parameters.ConfigureAsOpenFileNameList( "FileNames" );
39 this->m_Parameters.SetAcceptedFileExtensions(
41 "Image files (*.bmp *.png *.jpg *.jpeg *.dcm *.mhd *.nhdr *.nrrd *.tiff)"
45 // -------------------------------------------------------------------------
46 cpPluginsIO::ImageReader::
51 // -------------------------------------------------------------------------
52 void cpPluginsIO::ImageReader::
56 auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
57 if( fnames.size( ) > 1 )
59 std::stringstream fname_str;
60 fname_str << fnames[ 0 ] << cpPlugins_PATH_SEPARATOR << fnames[ 1 ];
61 std::string fname = fname_str.str( );
63 // Guess image properties
64 itk::ImageIOBase::Pointer io =
65 itk::ImageIOFactory::CreateImageIO(
66 fname.c_str( ), itk::ImageIOFactory::ReadMode
70 io->SetFileName( fname );
71 io->ReadImageInformation( );
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
87 this->_Error( "Image dimension not supported." );
91 std::string( "Could not create an ImageIO for \"" ) +
97 this->_Error( "No image(s) given" );
100 // -------------------------------------------------------------------------
101 template< unsigned int _Dim >
102 bool cpPluginsIO::ImageReader::
103 _GD0( itk::ImageIOBase* io )
105 typedef unsigned char uchar;
106 typedef unsigned short ushort;
107 typedef unsigned int uint;
108 typedef unsigned long ulong;
110 itk::ImageIOBase::IOComponentType ct = io->GetComponentType( );
111 itk::ImageIOBase::IOPixelType pt = io->GetPixelType( );
113 bool success = false;
114 if( pt == itk::ImageIOBase::SCALAR )
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
139 else if( pt == itk::ImageIOBase::RGB )
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
166 else if( pt == itk::ImageIOBase::RGBA )
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
193 else if( pt == itk::ImageIOBase::COMPLEX )
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
202 else if( pt == itk::ImageIOBase::COVARIANTVECTOR )
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
211 else if( pt == itk::ImageIOBase::POINT )
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
220 else if( pt == itk::ImageIOBase::VECTOR )
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
229 else if( pt == itk::ImageIOBase::SYMMETRICSECONDRANKTENSOR )
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
238 else if( pt == itk::ImageIOBase::DIFFUSIONTENSOR3D )
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
250 this->_Error( "DiffusionTensor3D dimension not supported." );
252 else if( pt == itk::ImageIOBase::MATRIX )
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
261 else if( pt == itk::ImageIOBase::OFFSET )
263 this->_GD1< itk::Offset< _Dim >, _Dim >( io );
265 else if( pt == itk::ImageIOBase::FIXEDARRAY )
271 // -------------------------------------------------------------------------
272 template< class _TPixel, unsigned int _Dim >
273 bool cpPluginsIO::ImageReader::
274 _GD1( itk::ImageIOBase* io )
276 typedef itk::Image< _TPixel, _Dim > _TImage;
279 auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
280 if( fnames.size( ) == 2 )
282 std::stringstream fname_str;
283 fname_str << fnames[ 0 ] << cpPlugins_PATH_SEPARATOR << fnames[ 1 ];
285 auto f = this->_CreateITK< itk::ImageFileReader< _TImage > >( );
286 f->SetFileName( fname_str.str( ) );
291 this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
294 catch( itk::ExceptionObject& err )
296 this->_Error( err.GetDescription( ) );
301 else // if( fnames.size( ) > 1 )
303 auto f = this->_CreateITK< itk::ImageSeriesReader< _TImage > >( );
304 auto i = fnames.begin( );
305 std::stringstream dir;
306 dir << *i << cpPlugins_PATH_SEPARATOR;
308 for( ; i != fnames.end( ); ++i )
309 f->AddFileName( dir.str( ) + *i );
314 this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
317 catch( itk::ExceptionObject& err )
319 this->_Error( err.GetDescription( ) );