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