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