]> Creatis software - cpPlugins.git/blob - plugins/cpPluginsIO/ImageReader.cxx
First dump for version 0.1.0
[cpPlugins.git] / plugins / 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 D >
77 std::string cpPluginsIO::ImageReader::
78 _GD0( itk::ImageIOBase* io )
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, D >( io ); break;
89     case itk::ImageIOBase::SHORT  : r = this->_GD1<          short, D >( io ); break;
90     case itk::ImageIOBase::INT    : r = this->_GD1<            int, D >( io ); break;
91     case itk::ImageIOBase::LONG   : r = this->_GD1<           long, D >( io ); break;
92     case itk::ImageIOBase::FLOAT  : r = this->_GD1<          float, D >( io ); break;
93     case itk::ImageIOBase::DOUBLE : r = this->_GD1<         double, D >( io ); break;
94     case itk::ImageIOBase::UCHAR  : r = this->_GD1<  unsigned char, D >( io ); break;
95     case itk::ImageIOBase::USHORT : r = this->_GD1< unsigned short, D >( io ); break;
96     case itk::ImageIOBase::UINT   : r = this->_GD1<   unsigned int, D >( io ); break;
97     case itk::ImageIOBase::ULONG  : r = this->_GD1<  unsigned long, D >( 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 >, D >( io ); break;
108     case itk::ImageIOBase::SHORT  : r = this->_GD1< itk::RGBPixel<          short >, D >( io ); break;
109     case itk::ImageIOBase::INT    : r = this->_GD1< itk::RGBPixel<            int >, D >( io ); break;
110     case itk::ImageIOBase::LONG   : r = this->_GD1< itk::RGBPixel<           long >, D >( io ); break;
111     case itk::ImageIOBase::FLOAT  : r = this->_GD1< itk::RGBPixel<          float >, D >( io ); break;
112     case itk::ImageIOBase::DOUBLE : r = this->_GD1< itk::RGBPixel<         double >, D >( io ); break;
113     case itk::ImageIOBase::UCHAR  : r = this->_GD1< itk::RGBPixel<  unsigned char >, D >( io ); break;
114     case itk::ImageIOBase::USHORT : r = this->_GD1< itk::RGBPixel< unsigned short >, D >( io ); break;
115     case itk::ImageIOBase::UINT   : r = this->_GD1< itk::RGBPixel<   unsigned int >, D >( io ); break;
116     case itk::ImageIOBase::ULONG  : r = this->_GD1< itk::RGBPixel<  unsigned long >, D >( 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 >, D >( io ); break;
127     case itk::ImageIOBase::SHORT  : r = this->_GD1< itk::RGBAPixel<          short >, D >( io ); break;
128     case itk::ImageIOBase::INT    : r = this->_GD1< itk::RGBAPixel<            int >, D >( io ); break;
129     case itk::ImageIOBase::LONG   : r = this->_GD1< itk::RGBAPixel<           long >, D >( io ); break;
130     case itk::ImageIOBase::FLOAT  : r = this->_GD1< itk::RGBAPixel<          float >, D >( io ); break;
131     case itk::ImageIOBase::DOUBLE : r = this->_GD1< itk::RGBAPixel<         double >, D >( io ); break;
132     case itk::ImageIOBase::UCHAR  : r = this->_GD1< itk::RGBAPixel<  unsigned char >, D >( io ); break;
133     case itk::ImageIOBase::USHORT : r = this->_GD1< itk::RGBAPixel< unsigned short >, D >( io ); break;
134     case itk::ImageIOBase::UINT   : r = this->_GD1< itk::RGBAPixel<   unsigned int >, D >( io ); break;
135     case itk::ImageIOBase::ULONG  : r = this->_GD1< itk::RGBAPixel<  unsigned long >, D >( io ); break;
136     default:
137       r = "IO::ImageReader: RGBA pixel type not supported.";
138       break;
139     } // hctiws
140   }
141   /* TODO
142      else if( pt == itk::ImageIOBase::VECTOR )
143      {
144      }
145      else if( pt == itk::ImageIOBase::POINT )
146      {
147      }
148      else if( pt == itk::ImageIOBase::COVARIANTVECTOR )
149      {
150      }
151      else if( pt == itk::ImageIOBase::SYMMETRICSECONDRANKTENSOR )
152      {
153      }
154      else if( pt == itk::ImageIOBase::DIFFUSIONTENSOR3D )
155      {
156      }
157      else if( pt == itk::ImageIOBase::COMPLEX )
158      {
159      }
160      else if( pt == itk::ImageIOBase::OFFSET )
161      {
162      }
163      else if( pt == itk::ImageIOBase::FIXEDARRAY )
164      {
165      }
166      else if( pt == itk::ImageIOBase::MATRIX )
167      {
168      }
169   */
170   else
171     r = "IO::ImageReader: Image pixel type not yet supported.";
172   return( r );
173 }
174
175 // -------------------------------------------------------------------------
176 template< class P, unsigned int D >
177 std::string cpPluginsIO::ImageReader::
178 _GD1( itk::ImageIOBase* io )
179 {
180   typedef itk::Image< P, D > _I;
181
182   // Get filenames
183   std::string r = "";
184   auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
185   if( fnames.size( ) == 1 )
186   {
187     auto f = this->_CreateITK< itk::ImageFileReader< _I > >( );
188     f->SetFileName( fnames[ 0 ] );
189     f->SetImageIO( io );
190     try
191     {
192       f->Update( );
193       this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
194     }
195     catch( itk::ExceptionObject& err )
196     {
197       r = "IO::ImageReader: " + std::string( err.GetDescription( ) );
198     }
199   }
200   else // if( fnames.size( ) > 1 )
201   {
202     auto f = this->_CreateITK< itk::ImageSeriesReader< _I > >( );
203     for( auto i = fnames.begin( ); i != fnames.end( ); ++i )
204       f->AddFileName( *i );
205     f->SetImageIO( io );
206     try
207     {
208       f->Update( );
209       this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
210     }
211     catch( itk::ExceptionObject& err )
212     {
213       r = "IO::ImageReader: " + std::string( err.GetDescription( ) );
214     }
215
216   } // fi
217   return( r );
218 }
219
220 // eof - $RCSfile$