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