]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/ImageReader.cxx
feca19b52bb063cb79c3f5a23164b41d40e39385
[cpPlugins.git] / lib / cpPlugins / Plugins / ImageReader.cxx
1 #include <cpPlugins/Plugins/ImageReader.h>
2
3 // TODO: interesting... #define ITK_MANUAL_INSTANTIATION
4 #include <itkImage.h>
5 #include <itkImageFileReader.h>
6 #include <itkRGBPixel.h>
7
8 // -------------------------------------------------------------------------
9 cpPlugins::Plugins::ImageReader::
10 ImageReader( )
11   : Superclass( )
12 {
13   this->SetNumberOfOutputs( 1 );
14
15   this->m_DefaultParameters[ "FileName" ] =
16     TParameter( "string", "no_file_name" );
17   this->m_DefaultParameters[ "PixelType" ] = TParameter( "type", "uchar" );
18   this->m_DefaultParameters[ "ImageDimension" ] = TParameter( "int", "2" );
19   this->m_DefaultParameters[ "IsColorImage" ] = TParameter( "bool", "0" );
20 }
21
22 // -------------------------------------------------------------------------
23 cpPlugins::Plugins::ImageReader::
24 ~ImageReader( )
25 {
26 }
27
28 // -------------------------------------------------------------------------
29 std::string cpPlugins::Plugins::ImageReader::
30 GetClassName( ) const
31 {
32   return( "cpPlugins::Plugins::ImageReader" );
33 }
34
35 // -------------------------------------------------------------------------
36 std::string cpPlugins::Plugins::ImageReader::
37 _GenerateData( )
38 {
39   TParameters::const_iterator dIt;
40
41   // Get image dimension
42   dIt = this->m_Parameters.find( "ImageDimension" );
43   if( dIt == this->m_Parameters.end( ) )
44     dIt = this->m_DefaultParameters.find( "ImageDimension" );
45
46   std::string ret = "itk::Image dimension not supported.";
47   if     ( dIt->second.second == "1" ) ret = this->_GenerateData0< 1 >( );
48   else if( dIt->second.second == "2" ) ret = this->_GenerateData0< 2 >( );
49   else if( dIt->second.second == "3" ) ret = this->_GenerateData0< 3 >( );
50   else if( dIt->second.second == "4" ) ret = this->_GenerateData0< 4 >( );
51
52   return( ret );
53 }
54
55 // -------------------------------------------------------------------------
56 template< unsigned int D >
57 std::string cpPlugins::Plugins::ImageReader::
58 _GenerateData0( )
59 {
60   TParameters::const_iterator tIt, cIt;
61
62   // Get image pixelType
63   tIt = this->m_Parameters.find( "PixelType" );
64   if( tIt == this->m_Parameters.end( ) )
65     tIt = this->m_DefaultParameters.find( "PixelType" );
66   cIt = this->m_Parameters.find( "IsColorImage" );
67   if( cIt == this->m_Parameters.end( ) )
68     cIt = this->m_DefaultParameters.find( "IsColorImage" );
69
70   std::string ret = "itk::Image pixel type not supported";
71   if( cIt->second.second == "0" )
72   {
73     if( tIt->second.second == "char" )
74       ret = this->_GenerateData1< char, D >( );
75     else if( tIt->second.second == "short" )
76       ret = this->_GenerateData1< short, D >( );
77     else if( tIt->second.second == "int" )
78       ret = this->_GenerateData1< int, D >( );
79     else if( tIt->second.second == "long" )
80       ret = this->_GenerateData1< long, D >( );
81     else if( tIt->second.second == "uchar" )
82       ret = this->_GenerateData1< unsigned char, D >( );
83     else if( tIt->second.second == "ushort" )
84       ret = this->_GenerateData1< unsigned short, D >( );
85     else if( tIt->second.second == "uint" )
86       ret = this->_GenerateData1< unsigned int, D >( );
87     else if( tIt->second.second == "ulong" )
88       ret = this->_GenerateData1< unsigned long, D >( );
89     else if( tIt->second.second == "float" )
90       ret = this->_GenerateData1< float, D >( );
91     else if( tIt->second.second == "double" )
92       ret = this->_GenerateData1< double, D >( );
93   }
94   else if( cIt->second.second == "1" )
95   {
96     if( tIt->second.second == "char" )
97       ret = this->_GenerateData1< itk::RGBPixel< char >, D >( );
98     else if( tIt->second.second == "short" )
99       ret = this->_GenerateData1< itk::RGBPixel< short >, D >( );
100     else if( tIt->second.second == "int" )
101       ret = this->_GenerateData1< itk::RGBPixel< int >, D >( );
102     else if( tIt->second.second == "long" )
103       ret = this->_GenerateData1< itk::RGBPixel< long >, D >( );
104     else if( tIt->second.second == "uchar" )
105       ret = this->_GenerateData1< itk::RGBPixel< unsigned char >, D >( );
106     else if( tIt->second.second == "ushort" )
107       ret = this->_GenerateData1< itk::RGBPixel< unsigned short >, D >( );
108     else if( tIt->second.second == "uint" )
109       ret = this->_GenerateData1< itk::RGBPixel< unsigned int >, D >( );
110     else if( tIt->second.second == "ulong" )
111       ret = this->_GenerateData1< itk::RGBPixel< unsigned long >, D >( );
112     else if( tIt->second.second == "float" )
113       ret = this->_GenerateData1< itk::RGBPixel< float >, D >( );
114     else if( tIt->second.second == "double" )
115       ret = this->_GenerateData1< itk::RGBPixel< double >, D >( );
116   } // fi
117   return( ret );
118 }
119
120 // -------------------------------------------------------------------------
121 template< class P, unsigned int D >
122 std::string cpPlugins::Plugins::ImageReader::
123 _GenerateData1( )
124 {
125   TParameters::const_iterator fIt;
126
127   // Get image pixelType
128   fIt = this->m_Parameters.find( "FileName" );
129   if( fIt == this->m_Parameters.end( ) )
130     fIt = this->m_DefaultParameters.find( "FileName" );
131
132   typedef itk::Image< P, D > _TImage;
133   typedef itk::ImageFileReader< _TImage > _TImageReader;
134
135   _TImageReader* reader =
136     dynamic_cast< _TImageReader* >( this->m_Reader.GetPointer( ) );
137   if( reader == NULL )
138   {
139     this->m_Reader = _TImageReader::New( );
140     reader = dynamic_cast< _TImageReader* >( this->m_Reader.GetPointer( ) );
141
142   } // fi
143   reader->SetFileName( fIt->second.second );
144   try
145   {
146     reader->Update( );
147   }
148   catch( itk::ExceptionObject& err )
149   {
150     return( err.GetDescription( ) );
151
152   } // yrt
153   this->_SetOutput( 0, reader->GetOutput( ) );
154   return( "" );
155 }
156
157 // eof - $RCSfile$