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 D >
77 std::string cpPluginsIO::ImageReader::
78 _GD0( itk::ImageIOBase* io )
80 itk::ImageIOBase::IOComponentType ct = io->GetComponentType( );
81 itk::ImageIOBase::IOPixelType pt = io->GetPixelType( );
84 if( pt == itk::ImageIOBase::SCALAR )
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;
99 r = "IO::ImageReader: Scalar pixel type not supported.";
103 else if( pt == itk::ImageIOBase::RGB )
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;
118 r = "IO::ImageReader: RGB pixel type not supported.";
122 else if( pt == itk::ImageIOBase::RGBA )
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;
137 r = "IO::ImageReader: RGBA pixel type not supported.";
142 else if( pt == itk::ImageIOBase::VECTOR )
145 else if( pt == itk::ImageIOBase::POINT )
148 else if( pt == itk::ImageIOBase::COVARIANTVECTOR )
151 else if( pt == itk::ImageIOBase::SYMMETRICSECONDRANKTENSOR )
154 else if( pt == itk::ImageIOBase::DIFFUSIONTENSOR3D )
157 else if( pt == itk::ImageIOBase::COMPLEX )
160 else if( pt == itk::ImageIOBase::OFFSET )
163 else if( pt == itk::ImageIOBase::FIXEDARRAY )
166 else if( pt == itk::ImageIOBase::MATRIX )
171 r = "IO::ImageReader: Image pixel type not yet supported.";
175 // -------------------------------------------------------------------------
176 template< class P, unsigned int D >
177 std::string cpPluginsIO::ImageReader::
178 _GD1( itk::ImageIOBase* io )
180 typedef itk::Image< P, D > _I;
184 auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
185 if( fnames.size( ) == 1 )
187 auto f = this->_CreateITK< itk::ImageFileReader< _I > >( );
188 f->SetFileName( fnames[ 0 ] );
193 this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
195 catch( itk::ExceptionObject& err )
197 r = "IO::ImageReader: " + std::string( err.GetDescription( ) );
200 else // if( fnames.size( ) > 1 )
202 auto f = this->_CreateITK< itk::ImageSeriesReader< _I > >( );
203 for( auto i = fnames.begin( ); i != fnames.end( ); ++i )
204 f->AddFileName( *i );
209 this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
211 catch( itk::ExceptionObject& err )
213 r = "IO::ImageReader: " + std::string( err.GetDescription( ) );