1 #include <cpPluginsIO/ImageReader.h>
2 #include <cpPlugins/Image.h>
4 #include <itkImageFileReader.h>
5 #include <itkImageSeriesReader.h>
6 #include <itkImageFileReader.hxx>
7 #include <itkImageSeriesReader.hxx>
8 #include <itkImageAlgorithm.hxx>
10 // -------------------------------------------------------------------------
11 cpPluginsIO::ImageReader::
15 this->_AddOutput< cpPlugins::Image >( "Output" );
16 this->m_Parameters.Clear( );
17 this->m_Parameters.ConfigureAsOpenFileNameList( "FileNames" );
18 this->m_Parameters.SetAcceptedFileExtensions(
20 "Image files (*.bmp *.png *.jpg *.jpeg *.dcm *.mhd *.nhdr *.nrrd *.tiff)"
24 // -------------------------------------------------------------------------
25 cpPluginsIO::ImageReader::
30 // -------------------------------------------------------------------------
31 std::string cpPluginsIO::ImageReader::
35 auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
37 if( fnames.size( ) >= 1 )
39 // Guess image properties
40 itk::ImageIOBase::Pointer io =
41 itk::ImageIOFactory::CreateImageIO(
43 itk::ImageIOFactory::ReadMode
47 io->SetFileName( fnames[ 0 ] );
48 io->ReadImageInformation( );
49 if( fnames.size( ) >= 1 )
51 switch( io->GetNumberOfDimensions( ) )
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;
58 r = "IO::ImageReader: Image dimension not supported.";
66 "IO::ImageReader: Could not CreateImageIO for \"" +
71 r = "IO::ImageReader: No image(s) given";
75 // -------------------------------------------------------------------------
76 template< unsigned int _Dim >
77 std::string cpPluginsIO::ImageReader::
78 _GD0( itk::ImageIOBase* io )
80 typedef unsigned char uchar;
81 typedef unsigned short ushort;
82 typedef unsigned int uint;
83 typedef unsigned long ulong;
85 itk::ImageIOBase::IOComponentType ct = io->GetComponentType( );
86 itk::ImageIOBase::IOPixelType pt = io->GetPixelType( );
89 if( pt == itk::ImageIOBase::SCALAR )
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;
104 r = "IO::ImageReader: Scalar pixel type not supported.";
108 else if( pt == itk::ImageIOBase::RGB )
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;
123 r = "IO::ImageReader: RGB pixel type not supported.";
127 else if( pt == itk::ImageIOBase::RGBA )
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;
142 r = "IO::ImageReader: RGBA pixel type not supported.";
146 else if( pt == itk::ImageIOBase::COMPLEX )
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;
153 r = "IO::ImageReader: complex pixel type not supported.";
158 else if( pt == itk::ImageIOBase::VECTOR )
161 else if( pt == itk::ImageIOBase::POINT )
164 else if( pt == itk::ImageIOBase::COVARIANTVECTOR )
167 else if( pt == itk::ImageIOBase::SYMMETRICSECONDRANKTENSOR )
170 else if( pt == itk::ImageIOBase::DIFFUSIONTENSOR3D )
173 else if( pt == itk::ImageIOBase::OFFSET )
176 else if( pt == itk::ImageIOBase::FIXEDARRAY )
179 else if( pt == itk::ImageIOBase::MATRIX )
184 r = "IO::ImageReader: Image pixel type not yet supported.";
188 // -------------------------------------------------------------------------
189 template< class _TPixel, unsigned int _Dim >
190 std::string cpPluginsIO::ImageReader::
191 _GD1( itk::ImageIOBase* io )
193 typedef itk::Image< _TPixel, _Dim > _TImage;
197 auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
198 if( fnames.size( ) == 1 )
200 auto f = this->_CreateITK< itk::ImageFileReader< _TImage > >( );
201 f->SetFileName( fnames[ 0 ] );
206 this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
208 catch( itk::ExceptionObject& err )
210 r = "IO::ImageReader: " + std::string( err.GetDescription( ) );
213 else // if( fnames.size( ) > 1 )
215 auto f = this->_CreateITK< itk::ImageSeriesReader< _TImage > >( );
216 for( auto i = fnames.begin( ); i != fnames.end( ); ++i )
217 f->AddFileName( *i );
222 this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
224 catch( itk::ExceptionObject& err )
226 r = "IO::ImageReader: " + std::string( err.GetDescription( ) );