]> Creatis software - cpPlugins.git/blob - plugins/cpPluginsIO/ImageReader.cxx
e9f3e59d178dd9ff2436941ae4395ef8691afdd6
[cpPlugins.git] / plugins / cpPluginsIO / ImageReader.cxx
1 #include <cpPluginsIO/ImageReader.h>
2 #include <cpPlugins/Image.h>
3 #include <cpPlugins_Instances/ImagesIO.h>
4 #include <itkImageIOFactory.h>
5
6 // -------------------------------------------------------------------------
7 cpPluginsIO::ImageReader::
8 ImageReader( )
9   : Superclass( )
10 {
11   this->_AddOutput< cpPlugins::Image >( "Output" );
12   this->m_Parameters.Clear( );
13   this->m_Parameters.ConfigureAsOpenFileNameList( "FileNames" );
14   this->m_Parameters.SetAcceptedFileExtensions(
15     "FileNames",
16     "Image files (*.bmp *.png *.jpg *.jpeg *.dcm *.mhd *.nhdr *.nrrd *.tiff)"
17     );
18 }
19
20 // -------------------------------------------------------------------------
21 cpPluginsIO::ImageReader::
22 ~ImageReader( )
23 {
24 }
25
26 // -------------------------------------------------------------------------
27 std::string cpPluginsIO::ImageReader::
28 _GenerateData( )
29 {
30   // Get filenames
31   auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
32   std::string r = "";
33   if( fnames.size( ) >= 1 )
34   {
35     // Guess image properties
36     itk::ImageIOBase::Pointer io =
37       itk::ImageIOFactory::CreateImageIO(
38         fnames[ 0 ].c_str( ),
39         itk::ImageIOFactory::ReadMode
40         );
41     if( io.IsNotNull( ) )
42     {
43       io->SetFileName( fnames[ 0 ] );
44       io->ReadImageInformation( );
45       if( fnames.size( ) >= 1 )
46       {
47         switch( io->GetNumberOfDimensions( ) )
48         {
49           // TODO: case 1: r = this->_GD0< 1 >( io ); break;
50         case 2: r = this->_GD0< 2 >( io ); break;
51         case 3: r = this->_GD0< 3 >( io ); break;
52           // TODO: case 4: r = this->_GD0< 4 >( io ); break;
53         default:
54           r = "IO::ImageReader: Image dimension not supported.";
55           break;
56         } // hctiws
57
58       } // fi
59     }
60     else
61       r =
62         "IO::ImageReader: Could not CreateImageIO for \"" +
63         fnames[ 0 ] +
64         "\"";
65   }
66   else
67     r = "IO::ImageReader: No image(s) given";
68   return( r );
69 }
70
71 // -------------------------------------------------------------------------
72 template< unsigned int _Dim >
73 std::string cpPluginsIO::ImageReader::
74 _GD0( itk::ImageIOBase* io )
75 {
76   typedef unsigned char  uchar;
77   typedef unsigned short ushort;
78   typedef unsigned int   uint;
79   typedef unsigned long  ulong;
80
81   itk::ImageIOBase::IOComponentType ct = io->GetComponentType( );
82   itk::ImageIOBase::IOPixelType pt = io->GetPixelType( );
83
84   std::string r = "";
85   if( pt == itk::ImageIOBase::SCALAR )
86   {
87     switch( ct )
88     {
89     case itk::ImageIOBase::CHAR   : r = this->_GD1<   char, _Dim >( io ); break;
90     case itk::ImageIOBase::SHORT  : r = this->_GD1<  short, _Dim >( io ); break;
91     case itk::ImageIOBase::INT    : r = this->_GD1<    int, _Dim >( io ); break;
92     case itk::ImageIOBase::LONG   : r = this->_GD1<   long, _Dim >( io ); break;
93     case itk::ImageIOBase::FLOAT  : r = this->_GD1<  float, _Dim >( io ); break;
94     case itk::ImageIOBase::DOUBLE : r = this->_GD1< double, _Dim >( io ); break;
95     case itk::ImageIOBase::UCHAR  : r = this->_GD1<  uchar, _Dim >( io ); break;
96     case itk::ImageIOBase::USHORT : r = this->_GD1< ushort, _Dim >( io ); break;
97     case itk::ImageIOBase::UINT   : r = this->_GD1<   uint, _Dim >( io ); break;
98     case itk::ImageIOBase::ULONG  : r = this->_GD1<  ulong, _Dim >( io ); break;
99     default:
100       r = "IO::ImageReader: Scalar pixel type not supported.";
101       break;
102     } // hctiws
103   }
104   else if( pt == itk::ImageIOBase::RGB )
105   {
106     switch( ct )
107     {
108     case itk::ImageIOBase::CHAR   : r = this->_GD1< itk::RGBPixel<   char >, _Dim >( io ); break;
109     case itk::ImageIOBase::SHORT  : r = this->_GD1< itk::RGBPixel<  short >, _Dim >( io ); break;
110     case itk::ImageIOBase::INT    : r = this->_GD1< itk::RGBPixel<    int >, _Dim >( io ); break;
111     case itk::ImageIOBase::LONG   : r = this->_GD1< itk::RGBPixel<   long >, _Dim >( io ); break;
112     case itk::ImageIOBase::FLOAT  : r = this->_GD1< itk::RGBPixel<  float >, _Dim >( io ); break;
113     case itk::ImageIOBase::DOUBLE : r = this->_GD1< itk::RGBPixel< double >, _Dim >( io ); break;
114     case itk::ImageIOBase::UCHAR  : r = this->_GD1< itk::RGBPixel<  uchar >, _Dim >( io ); break;
115     case itk::ImageIOBase::USHORT : r = this->_GD1< itk::RGBPixel< ushort >, _Dim >( io ); break;
116     case itk::ImageIOBase::UINT   : r = this->_GD1< itk::RGBPixel<   uint >, _Dim >( io ); break;
117     case itk::ImageIOBase::ULONG  : r = this->_GD1< itk::RGBPixel<  ulong >, _Dim >( io ); break;
118     default:
119       r = "IO::ImageReader: RGB pixel type not supported.";
120       break;
121     } // hctiws
122   }
123   else if( pt == itk::ImageIOBase::RGBA )
124   {
125     switch( ct )
126     {
127     case itk::ImageIOBase::CHAR   : r = this->_GD1< itk::RGBAPixel<   char >, _Dim >( io ); break;
128     case itk::ImageIOBase::SHORT  : r = this->_GD1< itk::RGBAPixel<  short >, _Dim >( io ); break;
129     case itk::ImageIOBase::INT    : r = this->_GD1< itk::RGBAPixel<    int >, _Dim >( io ); break;
130     case itk::ImageIOBase::LONG   : r = this->_GD1< itk::RGBAPixel<   long >, _Dim >( io ); break;
131     case itk::ImageIOBase::FLOAT  : r = this->_GD1< itk::RGBAPixel<  float >, _Dim >( io ); break;
132     case itk::ImageIOBase::DOUBLE : r = this->_GD1< itk::RGBAPixel< double >, _Dim >( io ); break;
133     case itk::ImageIOBase::UCHAR  : r = this->_GD1< itk::RGBAPixel<  uchar >, _Dim >( io ); break;
134     case itk::ImageIOBase::USHORT : r = this->_GD1< itk::RGBAPixel< ushort >, _Dim >( io ); break;
135     case itk::ImageIOBase::UINT   : r = this->_GD1< itk::RGBAPixel<   uint >, _Dim >( io ); break;
136     case itk::ImageIOBase::ULONG  : r = this->_GD1< itk::RGBAPixel<  ulong >, _Dim >( io ); break;
137     default:
138       r = "IO::ImageReader: RGBA pixel type not supported.";
139       break;
140     } // hctiws
141   }
142   else if( pt == itk::ImageIOBase::COMPLEX )
143   {
144     switch( ct )
145     {
146     case itk::ImageIOBase::FLOAT  : r = this->_GD1< std::complex<  float >, _Dim >( io ); break;
147     case itk::ImageIOBase::DOUBLE : r = this->_GD1< std::complex< double >, _Dim >( io ); break;
148     default:
149       r = "IO::ImageReader: complex pixel type not supported.";
150       break;
151     } // hctiws
152   }
153   /* TODO
154      else if( pt == itk::ImageIOBase::VECTOR )
155      {
156      }
157      else if( pt == itk::ImageIOBase::POINT )
158      {
159      }
160      else if( pt == itk::ImageIOBase::COVARIANTVECTOR )
161      {
162      }
163      else if( pt == itk::ImageIOBase::SYMMETRICSECONDRANKTENSOR )
164      {
165      }
166      else if( pt == itk::ImageIOBase::DIFFUSIONTENSOR3D )
167      {
168      }
169      else if( pt == itk::ImageIOBase::OFFSET )
170      {
171      }
172      else if( pt == itk::ImageIOBase::FIXEDARRAY )
173      {
174      }
175      else if( pt == itk::ImageIOBase::MATRIX )
176      {
177      }
178   */
179   else
180     r = "IO::ImageReader: Image pixel type not yet supported.";
181   return( r );
182 }
183
184 // -------------------------------------------------------------------------
185 template< class _TPixel, unsigned int _Dim >
186 std::string cpPluginsIO::ImageReader::
187 _GD1( itk::ImageIOBase* io )
188 {
189   typedef itk::Image< _TPixel, _Dim > _TImage;
190
191   // Get filenames
192   std::string r = "";
193   auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
194   if( fnames.size( ) == 1 )
195   {
196     auto f = this->_CreateITK< itk::ImageFileReader< _TImage > >( );
197     f->SetFileName( fnames[ 0 ] );
198     f->SetImageIO( io );
199     try
200     {
201       f->Update( );
202       this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
203     }
204     catch( itk::ExceptionObject& err )
205     {
206       r = "IO::ImageReader: " + std::string( err.GetDescription( ) );
207     }
208   }
209   else // if( fnames.size( ) > 1 )
210   {
211     auto f = this->_CreateITK< itk::ImageSeriesReader< _TImage > >( );
212     for( auto i = fnames.begin( ); i != fnames.end( ); ++i )
213       f->AddFileName( *i );
214     f->SetImageIO( io );
215     try
216     {
217       f->Update( );
218       this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
219     }
220     catch( itk::ExceptionObject& err )
221     {
222       r = "IO::ImageReader: " + std::string( err.GetDescription( ) );
223     }
224
225   } // fi
226   return( r );
227 }
228
229 // eof - $RCSfile$