]> 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 <set>
5
6 #undef ITK_MANUAL_INSTANTIATION
7 #include <itkImage.h>
8 #include <itkRGBPixel.h>
9 #include <itkImageFileReader.h>
10 #include <itkImageSeriesReader.h>
11
12 // -------------------------------------------------------------------------
13 cpPlugins::Plugins::ImageReader::
14 ImageReader( )
15   : Superclass( )
16 {
17   this->m_ClassName = "cpPlugins::ImageReader";
18   this->m_ClassCategory = "ImageReader";
19
20   this->SetNumberOfOutputs( 1 );
21   this->_MakeOutput< cpPlugins::Interface::Image >( 0 );
22
23   using namespace cpPlugins::Interface;
24   this->m_DefaultParameters.Configure( Parameters::StringList, "FileNames" );
25   this->m_Parameters = this->m_DefaultParameters;
26 }
27
28 // -------------------------------------------------------------------------
29 cpPlugins::Plugins::ImageReader::
30 ~ImageReader( )
31 {
32 }
33
34 // -------------------------------------------------------------------------
35 std::string cpPlugins::Plugins::ImageReader::
36 _GenerateData( )
37 {
38   // Get filenames
39   TStringList names;
40   this->m_Parameters.GetValueAsStringList( names, "FileNames" );
41
42   std::string r = "";
43   if( names.size( ) >= 1 )
44   {
45     // Guess image properties
46     itk::ImageIOBase::Pointer io =
47       itk::ImageIOFactory::CreateImageIO(
48         names[ 0 ].c_str( ),
49         itk::ImageIOFactory::ReadMode
50         );
51     if( io.IsNotNull( ) )
52     {
53       io->SetFileName( names[ 0 ] );
54       io->ReadImageInformation( );
55       if( names.size( ) == 1 )
56       {
57         switch( io->GetNumberOfDimensions( ) )
58         {
59         case 1: r = this->_GD0< 1 >( io, names ); break;
60         case 2: r = this->_GD0< 2 >( io, names ); break;
61         case 3: r = this->_GD0< 3 >( io, names ); break;
62         default:
63           r = "ImageReader: Image dimension not supported.";
64           break;
65         } // hctiws
66       }
67       else if( names.size( ) > 1 )
68       {
69         switch( io->GetNumberOfDimensions( ) )
70         {
71         case 1: r = this->_GD0< 2 >( io, names ); break;
72         case 2: r = this->_GD0< 3 >( io, names ); break;
73         default:
74           r = "ImageReader: Image dimension not supported.";
75           break;
76         } // hctiws
77
78       } // fi
79     }
80     else
81       r = "ImageReader: Could not CreateImageIO for \"" + names[ 0 ] + "\"";
82   }
83   else
84     r = "No image files given";
85   return( r );
86 }
87
88 // -------------------------------------------------------------------------
89 template< unsigned int D >
90 std::string cpPlugins::Plugins::ImageReader::
91 _GD0( itk::ImageIOBase* io, const TStringList& names )
92 {
93   std::string r = "";
94   switch( io->GetComponentType( ) )
95   {
96   case itk::ImageIOBase::UCHAR:
97     r = this->_GD1< unsigned char, D >( io, names );
98     break;
99   case itk::ImageIOBase::CHAR:
100     r = this->_GD1< char, D >( io, names );
101     break;
102   case itk::ImageIOBase::USHORT:
103     r = this->_GD1< unsigned short, D >( io, names );
104     break;
105   case itk::ImageIOBase::SHORT:
106     r = this->_GD1< short, D >( io, names );
107     break;
108   case itk::ImageIOBase::UINT:
109     r = this->_GD1< unsigned int, D >( io, names );
110     break;
111   case itk::ImageIOBase::INT:
112     r = this->_GD1< int, D >( io, names );
113     break;
114   case itk::ImageIOBase::ULONG:
115     r = this->_GD1< unsigned long, D >( io, names );
116     break;
117   case itk::ImageIOBase::LONG:
118     r = this->_GD1< long, D >( io, names );
119     break;
120   case itk::ImageIOBase::FLOAT:
121     r = this->_GD1< float, D >( io, names );
122     break;
123   case itk::ImageIOBase::DOUBLE:
124     r = this->_GD1< double, D >( io, names );
125     break;
126   default:
127     r = "ImageReader: Atomic pixel type not supported.";
128     break;
129   } // hctiws
130   return( r );
131 }
132
133 // -------------------------------------------------------------------------
134 template< class P, unsigned int D >
135 std::string cpPlugins::Plugins::ImageReader::
136 _GD1( itk::ImageIOBase* io, const TStringList& names )
137 {
138   std::string r = "";
139   switch( io->GetPixelType( ) )
140   {
141   case itk::ImageIOBase::SCALAR:
142     r = this->_GD2< P, D >( names );
143     break;
144   case itk::ImageIOBase::RGB:
145     r = this->_GD2< itk::RGBPixel< P >, D >( names );
146     break;
147   case itk::ImageIOBase::RGBA:
148     r = this->_GD2< itk::RGBAPixel< P >, D >( names );
149     break;
150     /* TODO
151        case itk::ImageIOBase::OFFSET:
152        r = this->_GD2< itk::Offset< P >, D >( names );
153        break;
154        case itk::ImageIOBase::VECTOR:
155        r = this->_GD2< P, D >( names );
156        break;
157        case itk::ImageIOBase::POINT:
158        r = this->_GD2< P, D >( names );
159        break;
160        case itk::ImageIOBase::COVARIANTVECTOR:
161        r = this->_GD2< P, D >( names );
162        break;
163        case itk::ImageIOBase::SYMMETRICSECONDRANKTENSOR:
164        r = this->_GD2< P, D >( names );
165        break;
166        case itk::ImageIOBase::DIFFUSIONTENSOR3D:
167        r = this->_GD2< P, D >( names );
168        break;
169     */
170     /* TODO
171        case itk::ImageIOBase::COMPLEX:
172        r = this->_GD2< std::complex< P >, D >( names );
173        break;
174        case itk::ImageIOBase::FIXEDARRAY:
175        r = this->_GD2< P, D >( names );
176        break;
177        case itk::ImageIOBase::MATRIX:
178        r = this->_GD2< P, D >( names );
179        break;
180     */
181   default:
182     r = "ImageReader: Pixel type not supported.";
183     break;
184   } // hctiws
185   return( r );
186 }
187
188 // -------------------------------------------------------------------------
189 template< class P, unsigned int D >
190 std::string cpPlugins::Plugins::ImageReader::
191 _GD2( const TStringList& names )
192 {
193   typedef itk::Image< P, D > _I;
194
195   std::string r = "";
196   if( names.size( ) == 1 )
197   {
198     // Read single image
199     typedef itk::ImageFileReader< _I > _SR;
200     _SR* reader =
201       dynamic_cast< _SR* >( this->m_RealProcessObject.GetPointer( ) );
202     if( reader == NULL )
203     {
204       this->m_RealProcessObject = _SR::New( );
205       reader =
206         dynamic_cast< _SR* >( this->m_RealProcessObject.GetPointer( ) );
207
208     } // fi
209     reader->SetFileName( names[ 0 ] );
210     try
211     {
212       reader->Update( );
213       this->m_Outputs[ 0 ]->SetITKDataObject( reader->GetOutput( ) );
214     }
215     catch( itk::ExceptionObject& err )
216     {
217       r = "ImageReader: " + std::string( err.GetDescription( ) );
218       this->m_Outputs[ 0 ]->SetITKDataObject( NULL );
219
220     } // yrt
221   }
222   else if( names.size( ) > 1 )
223   {
224     // Read image series
225     std::set< std::string > ordered_names;
226     for( unsigned int i = 0; i < names.size( ); ++i )
227       ordered_names.insert( names[ i ] );
228
229     typedef itk::ImageSeriesReader< _I > _MR;
230     _MR* reader =
231       dynamic_cast< _MR* >( this->m_RealProcessObject.GetPointer( ) );
232     if( reader == NULL )
233     {
234       this->m_RealProcessObject = _MR::New( );
235       reader =
236         dynamic_cast< _MR* >( this->m_RealProcessObject.GetPointer( ) );
237
238     } // fi
239     std::set< std::string >::const_iterator fnIt = ordered_names.begin( );
240     for( ; fnIt != ordered_names.end( ); ++fnIt )
241       reader->AddFileName( *fnIt );
242     try
243     {
244       reader->Update( );
245       this->m_Outputs[ 0 ]->SetITKDataObject( reader->GetOutput( ) );
246     }
247     catch( itk::ExceptionObject& err )
248     {
249       r = "ImageReader: " + std::string( err.GetDescription( ) );
250       this->m_Outputs[ 0 ]->SetITKDataObject( NULL );
251
252     } // yrt
253   }
254   else
255     r = "ImageReader: No image files given";
256   return( r );
257 }
258
259 // eof - $RCSfile$