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