]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/ImageReader.cxx
0aa4bfc233f7ae56f0f57c259ec86ed8cd9cd711
[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   this->m_DefaultParameters[ "FileName" ] =
26     TParameter( "string", "no_file_name" );
27   this->m_DefaultParameters[ "PixelType" ] = TParameter( "type", "uchar" );
28   this->m_DefaultParameters[ "ImageDimension" ] = TParameter( "int", "2" );
29   this->m_DefaultParameters[ "IsColorImage" ] = TParameter( "bool", "0" );
30 }
31
32 // -------------------------------------------------------------------------
33 cpPlugins::Plugins::ImageReader::
34 ~ImageReader( )
35 {
36 }
37
38 // -------------------------------------------------------------------------
39 std::string cpPlugins::Plugins::ImageReader::
40 _GenerateData( )
41 {
42   TParameters::const_iterator dIt;
43
44   // Get image dimension
45   dIt = this->m_Parameters.find( "ImageDimension" );
46   if( dIt == this->m_Parameters.end( ) )
47     dIt = this->m_DefaultParameters.find( "ImageDimension" );
48
49   std::string r = "cpPlugins::Plugins::ImageReader: itk::Image dimension not supported.";
50   if     ( dIt->second.second == "1" ) r = this->_GD0< 1 >( );
51   else if( dIt->second.second == "2" ) r = this->_GD0< 2 >( );
52   else if( dIt->second.second == "3" ) r = this->_GD0< 3 >( );
53   else if( dIt->second.second == "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   TParameters::const_iterator tIt, cIt;
64
65   // Get image pixel type
66   tIt = this->m_Parameters.find( "PixelType" );
67   if( tIt == this->m_Parameters.end( ) )
68     tIt = this->m_DefaultParameters.find( "PixelType" );
69   cIt = this->m_Parameters.find( "IsColorImage" );
70   if( cIt == this->m_Parameters.end( ) )
71     cIt = this->m_DefaultParameters.find( "IsColorImage" );
72
73   std::string r = "cpPlugins::Plugins::ImageReader: itk::Image pixel type not supported";
74   if( cIt->second.second == "0" )
75   {
76     if( tIt->second.second == "char" )
77       r = this->_GD1< char, D >( );
78     else if( tIt->second.second == "short" )
79       r = this->_GD1< short, D >( );
80     else if( tIt->second.second == "int" )
81       r = this->_GD1< int, D >( );
82     else if( tIt->second.second == "long" )
83       r = this->_GD1< long, D >( );
84     else if( tIt->second.second == "uchar" )
85       r = this->_GD1< unsigned char, D >( );
86     else if( tIt->second.second == "ushort" )
87       r = this->_GD1< unsigned short, D >( );
88     else if( tIt->second.second == "uint" )
89       r = this->_GD1< unsigned int, D >( );
90     else if( tIt->second.second == "ulong" )
91       r = this->_GD1< unsigned long, D >( );
92     else if( tIt->second.second == "float" )
93       r = this->_GD1< float, D >( );
94     else if( tIt->second.second == "double" )
95       r = this->_GD1< double, D >( );
96   }
97   else if( cIt->second.second == "1" )
98   {
99     if( tIt->second.second == "char" )
100       r = this->_GD1< itk::RGBPixel< char >, D >( );
101     else if( tIt->second.second == "short" )
102       r = this->_GD1< itk::RGBPixel< short >, D >( );
103     else if( tIt->second.second == "int" )
104       r = this->_GD1< itk::RGBPixel< int >, D >( );
105     else if( tIt->second.second == "long" )
106       r = this->_GD1< itk::RGBPixel< long >, D >( );
107     else if( tIt->second.second == "uchar" )
108       r = this->_GD1< itk::RGBPixel< unsigned char >, D >( );
109     else if( tIt->second.second == "ushort" )
110       r = this->_GD1< itk::RGBPixel< unsigned short >, D >( );
111     else if( tIt->second.second == "uint" )
112       r = this->_GD1< itk::RGBPixel< unsigned int >, D >( );
113     else if( tIt->second.second == "ulong" )
114       r = this->_GD1< itk::RGBPixel< unsigned long >, D >( );
115     else if( tIt->second.second == "float" )
116       r = this->_GD1< itk::RGBPixel< float >, D >( );
117     else if( tIt->second.second == "double" )
118       r = this->_GD1< itk::RGBPixel< double >, D >( );
119   } // fi
120   return( r );
121 }
122
123 // -------------------------------------------------------------------------
124 template< class P, unsigned int D >
125 std::string cpPlugins::Plugins::ImageReader::
126 _GD1( )
127 {
128   TParameters::const_iterator fIt;
129
130   // Get filename
131   fIt = this->m_Parameters.find( "FileName" );
132   if( fIt == this->m_Parameters.end( ) )
133     fIt = this->m_DefaultParameters.find( "FileName" );
134
135   typedef itk::Image< P, D > _TImage;
136   typedef itk::ImageFileReader< _TImage > _TReader;
137
138   _TReader* reader =
139     dynamic_cast< _TReader* >( this->m_RealProcessObject.GetPointer( ) );
140   if( reader == NULL )
141   {
142     this->m_RealProcessObject = _TReader::New( );
143     reader =
144       dynamic_cast< _TReader* >( this->m_RealProcessObject.GetPointer( ) );
145
146   } // fi
147   reader->SetFileName( fIt->second.second );
148   try
149   {
150     reader->Update( );
151   }
152   catch( itk::ExceptionObject& err )
153   {
154     return( err.GetDescription( ) );
155
156   } // yrt
157   this->_SetOutput( 0, reader->GetOutput( ) );
158   return( "" );
159 }
160
161 // eof - $RCSfile$