]> Creatis software - cpPlugins.git/blob - plugins/cpPluginsIO/ImageReader.cxx
...
[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 void cpPluginsIO::ImageReader::
28 _GenerateData( )
29 {
30   // Get filenames
31   auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
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: this->_GD0< 2 >( io ); break;
50         case 3: this->_GD0< 3 >( io ); break;
51           // TODO: case 4: r = this->_GD0< 4 >( io ); break;
52         default:
53           this->_Error( "Image dimension not supported." );
54           break;
55         } // hctiws
56
57       } // fi
58     }
59     else
60       this->_Error(
61         std::string( "Could not create an ImageIO for \"" ) +
62         fnames[ 0 ] +
63         std::string( "\"" )
64         );
65   }
66   else
67     this->_Error( "No image(s) given" );
68 }
69
70 // -------------------------------------------------------------------------
71 template< unsigned int _Dim >
72 void 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   if( pt == itk::ImageIOBase::SCALAR )
84   {
85     switch( ct )
86     {
87     case itk::ImageIOBase::CHAR   : this->_GD1<   char, _Dim >( io ); break;
88     case itk::ImageIOBase::SHORT  : this->_GD1<  short, _Dim >( io ); break;
89     case itk::ImageIOBase::INT    : this->_GD1<    int, _Dim >( io ); break;
90     case itk::ImageIOBase::LONG   : this->_GD1<   long, _Dim >( io ); break;
91     case itk::ImageIOBase::FLOAT  : this->_GD1<  float, _Dim >( io ); break;
92     case itk::ImageIOBase::DOUBLE : this->_GD1< double, _Dim >( io ); break;
93     case itk::ImageIOBase::UCHAR  : this->_GD1<  uchar, _Dim >( io ); break;
94     case itk::ImageIOBase::USHORT : this->_GD1< ushort, _Dim >( io ); break;
95     case itk::ImageIOBase::UINT   : this->_GD1<   uint, _Dim >( io ); break;
96     case itk::ImageIOBase::ULONG  : this->_GD1<  ulong, _Dim >( io ); break;
97     default: this->_Error( "Scalar pixel type not supported." ); break;
98     } // hctiws
99   }
100   else if( pt == itk::ImageIOBase::RGB )
101   {
102     switch( ct )
103     {
104     case itk::ImageIOBase::CHAR   : this->_GD1< itk::RGBPixel<   char >, _Dim >( io ); break;
105     case itk::ImageIOBase::SHORT  : this->_GD1< itk::RGBPixel<  short >, _Dim >( io ); break;
106     case itk::ImageIOBase::INT    : this->_GD1< itk::RGBPixel<    int >, _Dim >( io ); break;
107     case itk::ImageIOBase::LONG   : this->_GD1< itk::RGBPixel<   long >, _Dim >( io ); break;
108     case itk::ImageIOBase::FLOAT  : this->_GD1< itk::RGBPixel<  float >, _Dim >( io ); break;
109     case itk::ImageIOBase::DOUBLE : this->_GD1< itk::RGBPixel< double >, _Dim >( io ); break;
110     case itk::ImageIOBase::UCHAR  : this->_GD1< itk::RGBPixel<  uchar >, _Dim >( io ); break;
111     case itk::ImageIOBase::USHORT : this->_GD1< itk::RGBPixel< ushort >, _Dim >( io ); break;
112     case itk::ImageIOBase::UINT   : this->_GD1< itk::RGBPixel<   uint >, _Dim >( io ); break;
113     case itk::ImageIOBase::ULONG  : this->_GD1< itk::RGBPixel<  ulong >, _Dim >( io ); break;
114     default: this->_Error( "RGB pixel type not supported." ); break;
115     } // hctiws
116   }
117   else if( pt == itk::ImageIOBase::RGBA )
118   {
119     switch( ct )
120     {
121     case itk::ImageIOBase::CHAR   : this->_GD1< itk::RGBAPixel<   char >, _Dim >( io ); break;
122     case itk::ImageIOBase::SHORT  : this->_GD1< itk::RGBAPixel<  short >, _Dim >( io ); break;
123     case itk::ImageIOBase::INT    : this->_GD1< itk::RGBAPixel<    int >, _Dim >( io ); break;
124     case itk::ImageIOBase::LONG   : this->_GD1< itk::RGBAPixel<   long >, _Dim >( io ); break;
125     case itk::ImageIOBase::FLOAT  : this->_GD1< itk::RGBAPixel<  float >, _Dim >( io ); break;
126     case itk::ImageIOBase::DOUBLE : this->_GD1< itk::RGBAPixel< double >, _Dim >( io ); break;
127     case itk::ImageIOBase::UCHAR  : this->_GD1< itk::RGBAPixel<  uchar >, _Dim >( io ); break;
128     case itk::ImageIOBase::USHORT : this->_GD1< itk::RGBAPixel< ushort >, _Dim >( io ); break;
129     case itk::ImageIOBase::UINT   : this->_GD1< itk::RGBAPixel<   uint >, _Dim >( io ); break;
130     case itk::ImageIOBase::ULONG  : this->_GD1< itk::RGBAPixel<  ulong >, _Dim >( io ); break;
131     default: this->_Error( "RGBA pixel type not supported." ); break;
132     } // hctiws
133   }
134   else if( pt == itk::ImageIOBase::COMPLEX )
135   {
136     switch( ct )
137     {
138     case itk::ImageIOBase::FLOAT  : this->_GD1< std::complex< float >, _Dim >( io ); break;
139     case itk::ImageIOBase::DOUBLE : this->_GD1< std::complex< double >, _Dim >( io ); break;
140     default: this->_Error( "Complex pixel type not supported." ); break;
141     } // hctiws
142   }
143   /* TODO
144      else if( pt == itk::ImageIOBase::VECTOR )
145      {
146      }
147      else if( pt == itk::ImageIOBase::POINT )
148      {
149      }
150      else if( pt == itk::ImageIOBase::COVARIANTVECTOR )
151      {
152      }
153      else if( pt == itk::ImageIOBase::SYMMETRICSECONDRANKTENSOR )
154      {
155      }
156      else if( pt == itk::ImageIOBase::DIFFUSIONTENSOR3D )
157      {
158      }
159      else if( pt == itk::ImageIOBase::OFFSET )
160      {
161      }
162      else if( pt == itk::ImageIOBase::FIXEDARRAY )
163      {
164      }
165      else if( pt == itk::ImageIOBase::MATRIX )
166      {
167      }
168   */
169   else
170     this->_Error( "Image pixel type not yet supported." );
171 }
172
173 // -------------------------------------------------------------------------
174 template< class _TPixel, unsigned int _Dim >
175 void cpPluginsIO::ImageReader::
176 _GD1( itk::ImageIOBase* io )
177 {
178   typedef itk::Image< _TPixel, _Dim > _TImage;
179
180   // Get filenames
181   auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
182   if( fnames.size( ) == 1 )
183   {
184     auto f = this->_CreateITK< itk::ImageFileReader< _TImage > >( );
185     f->SetFileName( fnames[ 0 ] );
186     f->SetImageIO( io );
187     try
188     {
189       f->Update( );
190       this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
191     }
192     catch( itk::ExceptionObject& err )
193     {
194       this->_Error( err.GetDescription( ) );
195     }
196   }
197   else // if( fnames.size( ) > 1 )
198   {
199     auto f = this->_CreateITK< itk::ImageSeriesReader< _TImage > >( );
200     for( auto i = fnames.begin( ); i != fnames.end( ); ++i )
201       f->AddFileName( *i );
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       this->_Error( err.GetDescription( ) );
211     }
212
213   } // fi
214 }
215
216 // eof - $RCSfile$