1 #include <cpPlugins/Plugins/ImageReader.h>
2 #include <cpPlugins/Interface/Image.h>
6 #undef ITK_MANUAL_INSTANTIATION
8 #include <itkRGBPixel.h>
9 #include <itkImageFileReader.h>
10 #include <itkImageSeriesReader.h>
12 // -------------------------------------------------------------------------
13 cpPlugins::Plugins::ImageReader::
17 this->m_ClassName = "cpPlugins::ImageReader";
18 this->m_ClassCategory = "ImageReader";
20 this->SetNumberOfOutputs( 1 );
21 this->_MakeOutput< cpPlugins::Interface::Image >( 0 );
23 using namespace cpPlugins::Interface;
24 this->m_DefaultParameters.Configure( Parameters::StringList, "FileNames" );
25 this->m_Parameters = this->m_DefaultParameters;
28 // -------------------------------------------------------------------------
29 cpPlugins::Plugins::ImageReader::
34 // -------------------------------------------------------------------------
35 std::string cpPlugins::Plugins::ImageReader::
40 this->m_Parameters.GetValueAsStringList( names, "FileNames" );
43 if( names.size( ) >= 1 )
45 // Guess image properties
46 itk::ImageIOBase::Pointer io =
47 itk::ImageIOFactory::CreateImageIO(
49 itk::ImageIOFactory::ReadMode
53 io->SetFileName( names[ 0 ] );
54 io->ReadImageInformation( );
55 if( names.size( ) == 1 )
57 switch( io->GetNumberOfDimensions( ) )
59 case 1: r = this->_GD0< 1 >( io, names ); break;
60 case 2: r = this->_GD0< 2 >( io, names ); break;
61 case 3: r = this->_GD0< 3 >( io, names ); break;
63 r = "ImageReader: Image dimension not supported.";
67 else if( names.size( ) > 1 )
69 switch( io->GetNumberOfDimensions( ) )
71 case 1: r = this->_GD0< 2 >( io, names ); break;
72 case 2: r = this->_GD0< 3 >( io, names ); break;
74 r = "ImageReader: Image dimension not supported.";
81 r = "ImageReader: Could not CreateImageIO for \"" + names[ 0 ] + "\"";
84 r = "No image files given";
88 // -------------------------------------------------------------------------
89 template< unsigned int D >
90 std::string cpPlugins::Plugins::ImageReader::
91 _GD0( itk::ImageIOBase* io, const TStringList& names )
94 switch( io->GetComponentType( ) )
96 case itk::ImageIOBase::UCHAR:
97 r = this->_GD1< unsigned char, D >( io, names );
99 case itk::ImageIOBase::CHAR:
100 r = this->_GD1< char, D >( io, names );
102 case itk::ImageIOBase::USHORT:
103 r = this->_GD1< unsigned short, D >( io, names );
105 case itk::ImageIOBase::SHORT:
106 r = this->_GD1< short, D >( io, names );
108 case itk::ImageIOBase::UINT:
109 r = this->_GD1< unsigned int, D >( io, names );
111 case itk::ImageIOBase::INT:
112 r = this->_GD1< int, D >( io, names );
114 case itk::ImageIOBase::ULONG:
115 r = this->_GD1< unsigned long, D >( io, names );
117 case itk::ImageIOBase::LONG:
118 r = this->_GD1< long, D >( io, names );
120 case itk::ImageIOBase::FLOAT:
121 r = this->_GD1< float, D >( io, names );
123 case itk::ImageIOBase::DOUBLE:
124 r = this->_GD1< double, D >( io, names );
127 r = "ImageReader: Atomic pixel type not supported.";
133 // -------------------------------------------------------------------------
134 template< class P, unsigned int D >
135 std::string cpPlugins::Plugins::ImageReader::
136 _GD1( itk::ImageIOBase* io, const TStringList& names )
139 switch( io->GetPixelType( ) )
141 case itk::ImageIOBase::SCALAR:
142 r = this->_GD2< P, D >( names );
144 case itk::ImageIOBase::RGB:
145 r = this->_GD2< itk::RGBPixel< P >, D >( names );
147 case itk::ImageIOBase::RGBA:
148 r = this->_GD2< itk::RGBAPixel< P >, D >( names );
151 case itk::ImageIOBase::OFFSET:
152 r = this->_GD2< itk::Offset< P >, D >( names );
154 case itk::ImageIOBase::VECTOR:
155 r = this->_GD2< P, D >( names );
157 case itk::ImageIOBase::POINT:
158 r = this->_GD2< P, D >( names );
160 case itk::ImageIOBase::COVARIANTVECTOR:
161 r = this->_GD2< P, D >( names );
163 case itk::ImageIOBase::SYMMETRICSECONDRANKTENSOR:
164 r = this->_GD2< P, D >( names );
166 case itk::ImageIOBase::DIFFUSIONTENSOR3D:
167 r = this->_GD2< P, D >( names );
171 case itk::ImageIOBase::COMPLEX:
172 r = this->_GD2< std::complex< P >, D >( names );
174 case itk::ImageIOBase::FIXEDARRAY:
175 r = this->_GD2< P, D >( names );
177 case itk::ImageIOBase::MATRIX:
178 r = this->_GD2< P, D >( names );
182 r = "ImageReader: Pixel type not supported.";
188 // -------------------------------------------------------------------------
189 template< class P, unsigned int D >
190 std::string cpPlugins::Plugins::ImageReader::
191 _GD2( const TStringList& names )
193 typedef itk::Image< P, D > _I;
196 if( names.size( ) == 1 )
199 typedef itk::ImageFileReader< _I > _SR;
201 dynamic_cast< _SR* >( this->m_RealProcessObject.GetPointer( ) );
204 this->m_RealProcessObject = _SR::New( );
206 dynamic_cast< _SR* >( this->m_RealProcessObject.GetPointer( ) );
209 reader->SetFileName( names[ 0 ] );
213 this->m_Outputs[ 0 ]->SetITKDataObject( reader->GetOutput( ) );
215 catch( itk::ExceptionObject& err )
217 r = "ImageReader: " + std::string( err.GetDescription( ) );
218 this->m_Outputs[ 0 ]->SetITKDataObject( NULL );
222 else if( names.size( ) > 1 )
225 std::set< std::string > ordered_names;
226 for( unsigned int i = 0; i < names.size( ); ++i )
227 ordered_names.insert( names[ i ] );
229 typedef itk::ImageSeriesReader< _I > _MR;
231 dynamic_cast< _MR* >( this->m_RealProcessObject.GetPointer( ) );
234 this->m_RealProcessObject = _MR::New( );
236 dynamic_cast< _MR* >( this->m_RealProcessObject.GetPointer( ) );
239 std::set< std::string >::const_iterator fnIt = ordered_names.begin( );
240 for( ; fnIt != ordered_names.end( ); ++fnIt )
241 reader->AddFileName( *fnIt );
245 this->m_Outputs[ 0 ]->SetITKDataObject( reader->GetOutput( ) );
247 catch( itk::ExceptionObject& err )
249 r = "ImageReader: " + std::string( err.GetDescription( ) );
250 this->m_Outputs[ 0 ]->SetITKDataObject( NULL );
255 r = "ImageReader: No image files given";