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