1 #include <cpPlugins/Plugins/ImageReader.h>
2 #include <cpPlugins/Interface/Image.h>
4 #include <itkImageFileReader.h>
5 #include <itkImageSeriesReader.h>
7 #define ITK_MANUAL_INSTANTIATION
9 #include <itkRGBPixel.h>
11 // -------------------------------------------------------------------------
12 std::string cpPlugins::Plugins::ImageReader::
15 return( "cpPlugins::Plugins::ImageReader" );
18 // -------------------------------------------------------------------------
19 cpPlugins::Plugins::ImageReader::
23 this->SetNumberOfOutputs( 1 );
24 this->_MakeOutput< cpPlugins::Interface::Image >( 0 );
26 using namespace cpPlugins::Interface;
27 this->m_DefaultParameters.Configure( Parameters::StringList, "FileNames" );
28 this->m_DefaultParameters.Configure( Parameters::String, "PixelType" );
29 this->m_DefaultParameters.Configure( Parameters::Uint, "Dimension" );
30 this->m_DefaultParameters.Configure( Parameters::Uint, "IsColorImage" );
31 this->m_DefaultParameters.SetValueAsString( "PixelType", "uchar" );
32 this->m_DefaultParameters.SetValueAsUint( "Dimension", 3 );
33 this->m_DefaultParameters.SetValueAsUint( "IsColorImage", 0 );
34 this->m_Parameters = this->m_DefaultParameters;
37 // -------------------------------------------------------------------------
38 cpPlugins::Plugins::ImageReader::
43 // -------------------------------------------------------------------------
44 std::string cpPlugins::Plugins::ImageReader::
47 using namespace cpPlugins::Interface;
48 Parameters::TUint dim = this->m_Parameters.GetValueAsUint( "Dimension" );
49 std::string r = "cpPlugins::Plugins::ImageReader: itk::Image dimension not supported.";
50 if ( dim == 1 ) r = this->_GD0< 1 >( );
51 else if( dim == 2 ) r = this->_GD0< 2 >( );
52 else if( dim == 3 ) r = this->_GD0< 3 >( );
53 else if( dim == 4 ) r = this->_GD0< 4 >( );
58 // -------------------------------------------------------------------------
59 template< unsigned int D >
60 std::string cpPlugins::Plugins::ImageReader::
63 using namespace cpPlugins::Interface;
64 Parameters::TString pt = this->m_Parameters.GetValueAsString( "PixelType" );
65 Parameters::TUint ci = this->m_Parameters.GetValueAsUint( "IsColorImage" );
67 std::string r = "cpPlugins::Plugins::ImageReader: itk::Image pixel type not supported";
71 r = this->_GD1< char, D >( );
72 else if( pt == "short" )
73 r = this->_GD1< short, D >( );
74 else if( pt == "int" )
75 r = this->_GD1< int, D >( );
76 else if( pt == "long" )
77 r = this->_GD1< long, D >( );
78 else if( pt == "uchar" )
79 r = this->_GD1< unsigned char, D >( );
80 else if( pt == "ushort" )
81 r = this->_GD1< unsigned short, D >( );
82 else if( pt == "uint" )
83 r = this->_GD1< unsigned int, D >( );
84 else if( pt == "ulong" )
85 r = this->_GD1< unsigned long, D >( );
86 else if( pt == "float" )
87 r = this->_GD1< float, D >( );
88 else if( pt == "double" )
89 r = this->_GD1< double, D >( );
94 r = this->_GD1< itk::RGBPixel< char >, D >( );
95 else if( pt == "short" )
96 r = this->_GD1< itk::RGBPixel< short >, D >( );
97 else if( pt == "int" )
98 r = this->_GD1< itk::RGBPixel< int >, D >( );
99 else if( pt == "long" )
100 r = this->_GD1< itk::RGBPixel< long >, D >( );
101 else if( pt == "uchar" )
102 r = this->_GD1< itk::RGBPixel< unsigned char >, D >( );
103 else if( pt == "ushort" )
104 r = this->_GD1< itk::RGBPixel< unsigned short >, D >( );
105 else if( pt == "uint" )
106 r = this->_GD1< itk::RGBPixel< unsigned int >, D >( );
107 else if( pt == "ulong" )
108 r = this->_GD1< itk::RGBPixel< unsigned long >, D >( );
109 else if( pt == "float" )
110 r = this->_GD1< itk::RGBPixel< float >, D >( );
111 else if( pt == "double" )
112 r = this->_GD1< itk::RGBPixel< double >, D >( );
117 // -------------------------------------------------------------------------
118 template< class P, unsigned int D >
119 std::string cpPlugins::Plugins::ImageReader::
122 typedef itk::Image< P, D > _TImage;
125 using namespace cpPlugins::Interface;
126 std::vector< Parameters::TString > unordered_names;
127 this->m_Parameters.GetValueAsStringList( unordered_names, "FileNames" );
129 if( unordered_names.size( ) == 1 )
132 typedef itk::ImageFileReader< _TImage > _TSingleReader;
134 _TSingleReader* singleReader =
135 dynamic_cast< _TSingleReader* >(
136 this->m_RealProcessObject.GetPointer( )
138 if( singleReader == NULL )
140 this->m_RealProcessObject = _TSingleReader::New( );
142 dynamic_cast< _TSingleReader* >(
143 this->m_RealProcessObject.GetPointer( )
147 singleReader->SetFileName( unordered_names.front( ) );
150 singleReader->Update( );
152 catch( itk::ExceptionObject& err )
154 return( err.GetDescription( ) );
157 this->_SetOutput( 0, singleReader->GetOutput( ) );
160 else if( unordered_names.size( ) > 1 )
163 std::set< std::string > ordered_names;
164 for( unsigned int i = 0; i < unordered_names.size( ); ++i )
165 ordered_names.insert( unordered_names[ i ] );
167 typedef itk::ImageSeriesReader< _TImage > _TMultiReader;
168 _TMultiReader* multiReader =
169 dynamic_cast< _TMultiReader* >(
170 this->m_RealProcessObject.GetPointer( )
172 if( multiReader == NULL )
174 this->m_RealProcessObject = _TMultiReader::New( );
176 dynamic_cast< _TMultiReader* >(
177 this->m_RealProcessObject.GetPointer( )
181 std::set< std::string >::const_iterator fnIt = ordered_names.begin( );
182 for( ; fnIt != ordered_names.end( ); ++fnIt )
183 multiReader->AddFileName( *fnIt );
186 multiReader->Update( );
188 catch( itk::ExceptionObject& err )
190 return( err.GetDescription( ) );
193 this->_SetOutput( 0, multiReader->GetOutput( ) );
197 return( "No image filename(s) given." );