]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/ImageReader.cxx
...
[cpPlugins.git] / lib / cpPlugins / Plugins / ImageReader.cxx
1 #include <cpPlugins/Plugins/ImageReader.h>
2 #include <cpPlugins/Interface/Image.h>
3
4 #include <itkImageFileReader.h>
5 #include <itkImageSeriesReader.h>
6
7 #define ITK_MANUAL_INSTANTIATION
8 #include <itkImage.h>
9 #include <itkRGBPixel.h>
10
11 // -------------------------------------------------------------------------
12 std::string cpPlugins::Plugins::ImageReader::
13 GetClassName( ) const
14 {
15   return( "cpPlugins::Plugins::ImageReader" );
16 }
17
18 // -------------------------------------------------------------------------
19 cpPlugins::Plugins::ImageReader::
20 ImageReader( )
21   : Superclass( )
22 {
23   this->SetNumberOfOutputs( 1 );
24   this->_MakeOutput< cpPlugins::Interface::Image >( 0 );
25
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;
35 }
36
37 // -------------------------------------------------------------------------
38 cpPlugins::Plugins::ImageReader::
39 ~ImageReader( )
40 {
41 }
42
43 // -------------------------------------------------------------------------
44 std::string cpPlugins::Plugins::ImageReader::
45 _GenerateData( )
46 {
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 >( );
54
55   return( r );
56 }
57
58 // -------------------------------------------------------------------------
59 template< unsigned int D >
60 std::string cpPlugins::Plugins::ImageReader::
61 _GD0( )
62 {
63   using namespace cpPlugins::Interface;
64   Parameters::TString pt = this->m_Parameters.GetValueAsString( "PixelType" );
65   Parameters::TUint ci = this->m_Parameters.GetValueAsUint( "IsColorImage" );
66
67   std::string r = "cpPlugins::Plugins::ImageReader: itk::Image pixel type not supported";
68   if( ci == 0 )
69   {
70     if( pt == "char" )
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 >( );
90   }
91   else
92   {
93     if( pt == "char" )
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 >( );
113   } // fi
114   return( r );
115 }
116
117 // -------------------------------------------------------------------------
118 template< class P, unsigned int D >
119 std::string cpPlugins::Plugins::ImageReader::
120 _GD1( )
121 {
122   typedef itk::Image< P, D > _TImage;
123
124   // Get filenames
125   using namespace cpPlugins::Interface;
126   std::vector< Parameters::TString > unordered_names;
127   this->m_Parameters.GetValueAsStringList( unordered_names, "FileNames" );
128
129   if( unordered_names.size( ) == 1 )
130   {
131     // Read single image
132     typedef itk::ImageFileReader< _TImage > _TSingleReader;
133
134     _TSingleReader* singleReader =
135       dynamic_cast< _TSingleReader* >(
136         this->m_RealProcessObject.GetPointer( )
137         );
138     if( singleReader == NULL )
139     {
140       this->m_RealProcessObject = _TSingleReader::New( );
141       singleReader =
142         dynamic_cast< _TSingleReader* >(
143           this->m_RealProcessObject.GetPointer( )
144           );
145
146     } // fi
147     singleReader->SetFileName( unordered_names.front( ) );
148     try
149     {
150       singleReader->Update( );
151     }
152     catch( itk::ExceptionObject& err )
153     {
154       return( err.GetDescription( ) );
155
156     } // yrt
157     this->_SetOutput( 0, singleReader->GetOutput( ) );
158     return( "" );
159   }
160   else if( unordered_names.size( ) > 1 )
161   {
162     // Read image series
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 ] );
166
167     typedef itk::ImageSeriesReader< _TImage > _TMultiReader;
168     _TMultiReader* multiReader =
169       dynamic_cast< _TMultiReader* >(
170         this->m_RealProcessObject.GetPointer( )
171         );
172     if( multiReader == NULL )
173     {
174       this->m_RealProcessObject = _TMultiReader::New( );
175       multiReader =
176         dynamic_cast< _TMultiReader* >(
177           this->m_RealProcessObject.GetPointer( )
178           );
179
180     } // fi
181     std::set< std::string >::const_iterator fnIt = ordered_names.begin( );
182     for( ; fnIt != ordered_names.end( ); ++fnIt )
183       multiReader->AddFileName( *fnIt );
184     try
185     {
186       multiReader->Update( );
187     }
188     catch( itk::ExceptionObject& err )
189     {
190       return( err.GetDescription( ) );
191
192     } // yrt
193     this->_SetOutput( 0, multiReader->GetOutput( ) );
194     return( "" );
195   }
196   else
197     return( "No image filename(s) given." );
198 }
199
200 // eof - $RCSfile$