]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/ImageSeriesReader.cxx
...
[cpPlugins.git] / lib / cpPlugins / Plugins / ImageSeriesReader.cxx
1 #include <cpPlugins/Plugins/ImageSeriesReader.h>
2 #include <cpPlugins/Interface/Image.h>
3
4 #include <set>
5 #include <sstream>
6
7 #include <itkImageSeriesReader.h>
8
9 #define ITK_MANUAL_INSTANTIATION
10 #include <itkImage.h>
11 #include <itkRGBPixel.h>
12
13 // -------------------------------------------------------------------------
14 std::string cpPlugins::Plugins::ImageSeriesReader::
15 GetClassName( ) const
16 {
17   return( "cpPlugins::Plugins::ImageSeriesReader" );
18 }
19
20 // -------------------------------------------------------------------------
21 cpPlugins::Plugins::ImageSeriesReader::
22 ImageSeriesReader( )
23   : Superclass( )
24 {
25   this->SetNumberOfOutputs( 1 );
26   this->_MakeOutput< cpPlugins::Interface::Image >( 0 );
27
28   using namespace cpPlugins::Interface;
29   this->m_DefaultParameters.Configure( Parameters::StringList, "FileNames" );
30   this->m_DefaultParameters.Configure( Parameters::String, "PixelType" );
31   this->m_DefaultParameters.Configure( Parameters::Uint, "Dimension" );
32   this->m_DefaultParameters.Configure( Parameters::Uint, "IsColorImage" );
33   this->m_DefaultParameters.SetValueAsString( "PixelType", "uchar" );
34   this->m_DefaultParameters.SetValueAsUint( "Dimension", 3 );
35   this->m_DefaultParameters.SetValueAsUint( "IsColorImage", 0 );
36   this->m_Parameters = this->m_DefaultParameters;
37 }
38
39 // -------------------------------------------------------------------------
40 cpPlugins::Plugins::ImageSeriesReader::
41 ~ImageSeriesReader( )
42 {
43 }
44
45 // -------------------------------------------------------------------------
46 std::string cpPlugins::Plugins::ImageSeriesReader::
47 _GenerateData( )
48 {
49   using namespace cpPlugins::Interface;
50   Parameters::TUint dim = this->m_Parameters.GetValueAsUint( "Dimension" );
51
52   std::string r = "cpPlugins::Plugins::ImageSeriesReader: itk::Image dimension not supported.";
53   if     ( dim == 2 ) r = this->_GD0< 2 >( );
54   else if( dim == 3 ) r = this->_GD0< 3 >( );
55   else if( dim == 4 ) r = this->_GD0< 4 >( );
56
57   return( r );
58 }
59
60 // -------------------------------------------------------------------------
61 template< unsigned int D >
62 std::string cpPlugins::Plugins::ImageSeriesReader::
63 _GD0( )
64 {
65   using namespace cpPlugins::Interface;
66   Parameters::TString pt = this->m_Parameters.GetValueAsString( "PixelType" );
67   Parameters::TUint ci = this->m_Parameters.GetValueAsUint( "IsColorImage" );
68
69   std::string r = "cpPlugins::Plugins::ImageSeriesReader: itk::Image pixel type not supported";
70   if( ci == 0 )
71   {
72     if( pt == "char" )
73       r = this->_GD1< char, D >( );
74     else if( pt == "short" )
75       r = this->_GD1< short, D >( );
76     else if( pt == "int" )
77       r = this->_GD1< int, D >( );
78     else if( pt == "long" )
79       r = this->_GD1< long, D >( );
80     else if( pt == "uchar" )
81       r = this->_GD1< unsigned char, D >( );
82     else if( pt == "ushort" )
83       r = this->_GD1< unsigned short, D >( );
84     else if( pt == "uint" )
85       r = this->_GD1< unsigned int, D >( );
86     else if( pt == "ulong" )
87       r = this->_GD1< unsigned long, D >( );
88     else if( pt == "float" )
89       r = this->_GD1< float, D >( );
90     else if( pt == "double" )
91       r = this->_GD1< double, D >( );
92   }
93   else if( ci == 1 )
94   {
95     if( pt == "char" )
96       r = this->_GD1< itk::RGBPixel< char >, D >( );
97     else if( pt == "short" )
98       r = this->_GD1< itk::RGBPixel< short >, D >( );
99     else if( pt == "int" )
100       r = this->_GD1< itk::RGBPixel< int >, D >( );
101     else if( pt == "long" )
102       r = this->_GD1< itk::RGBPixel< long >, D >( );
103     else if( pt == "uchar" )
104       r = this->_GD1< itk::RGBPixel< unsigned char >, D >( );
105     else if( pt == "ushort" )
106       r = this->_GD1< itk::RGBPixel< unsigned short >, D >( );
107     else if( pt == "uint" )
108       r = this->_GD1< itk::RGBPixel< unsigned int >, D >( );
109     else if( pt == "ulong" )
110       r = this->_GD1< itk::RGBPixel< unsigned long >, D >( );
111     else if( pt == "float" )
112       r = this->_GD1< itk::RGBPixel< float >, D >( );
113     else if( pt == "double" )
114       r = this->_GD1< itk::RGBPixel< double >, D >( );
115   } // fi
116   return( r );
117 }
118
119 // -------------------------------------------------------------------------
120 template< class P, unsigned int D >
121 std::string cpPlugins::Plugins::ImageSeriesReader::
122 _GD1( )
123 {
124   using namespace cpPlugins::Interface;
125   std::vector< Parameters::TString > unordered_names;
126   this->m_Parameters.GetValueAsStringList( unordered_names, "FileNames" );
127   std::set< std::string > ordered_names;
128   for( unsigned int i = 0; i < unordered_names.size( ); ++i )
129     ordered_names.insert( unordered_names[ i ] );
130
131   // Reader
132   typedef itk::Image< P, D > _TImage;
133   typedef itk::ImageSeriesReader< _TImage > _TReader;
134
135   _TReader* reader =
136     dynamic_cast< _TReader* >( this->m_RealProcessObject.GetPointer( ) );
137   if( reader == NULL )
138   {
139     this->m_RealProcessObject = _TReader::New( );
140     reader =
141       dynamic_cast< _TReader* >( this->m_RealProcessObject.GetPointer( ) );
142
143   } // fi
144   std::set< std::string >::const_iterator fnIt = ordered_names.begin( );
145   for( ; fnIt != ordered_names.end( ); ++fnIt )
146     reader->AddFileName( *fnIt );
147   try
148   {
149     reader->Update( );
150   }
151   catch( itk::ExceptionObject& err )
152   {
153     return( err.GetDescription( ) );
154
155   } // yrt
156   this->_SetOutput( 0, reader->GetOutput( ) );
157   return( "" );
158 }
159
160 // eof - $RCSfile$