]> Creatis software - cpPlugins.git/blob - plugins/IO/ImageReader.cxx
...
[cpPlugins.git] / plugins / IO / ImageReader.cxx
1 #include <plugins/IO/ImageReader.h>
2 #include <plugins/IO/ImageReaderQDialog.h>
3 #include <cpPlugins/Image.h>
4 #include <cpPlugins_Instances_ImageReaders.h>
5 #include <itkImageIOFactory.h>
6
7 #ifdef cpPlugins_QT4
8
9 #include <QApplication>
10 #include <QFileInfo>
11
12 // -------------------------------------------------------------------------
13 cpPluginsIO::ImageReaderQDialog::
14 ImageReaderQDialog( QWidget* parent )
15   : QFileDialog( parent ),
16     m_ProcessObject( NULL )
17 {
18   this->connect(
19     this, SIGNAL( accepted( ) ), this, SLOT( _dlg_Accepted( ) )
20     );
21   this->setWindowTitle( "Open an(some) image(s)" );
22 }
23
24 // -------------------------------------------------------------------------
25 cpPluginsIO::ImageReaderQDialog::
26 ~ImageReaderQDialog( )
27 {
28 }
29
30 // -------------------------------------------------------------------------
31 void cpPluginsIO::ImageReaderQDialog::
32 setProcessObject( cpPlugins::ProcessObject* obj )
33 {
34   if( obj == NULL )
35     return;
36   this->m_ProcessObject = obj;
37   auto param = this->m_ProcessObject->GetParameters( );
38   auto extensions = param->GetAcceptedFileExtensions( "FileNames" );
39   auto files = param->GetOpenFileNameList( "FileNames" );
40
41   QStringList filters;
42   if( extensions != "" )
43     filters << extensions.c_str( );
44   filters << "Any file (*)";
45   this->setFileMode( QFileDialog::ExistingFiles );
46   this->setNameFilters( filters );
47   this->setAcceptMode( QFileDialog::AcceptOpen );
48   if( files.size( ) > 0 )
49   {
50     QFileInfo info( files[ 0 ].c_str( ) );
51     this->setDirectory( info.canonicalPath( ) );
52
53   } // fi
54 }
55
56 // -------------------------------------------------------------------------
57 void cpPluginsIO::ImageReaderQDialog::
58 _dlg_Accepted( )
59 {
60   if( this->m_ProcessObject != NULL )
61   {
62     auto param = this->m_ProcessObject->GetParameters( );
63     auto files = this->selectedFiles( );
64     param->ClearOpenFileNameList( "FileNames" );
65     for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt )
66       param->AddToOpenFileNameList( "FileNames", fIt->toStdString( ) );
67
68   } // fi
69 }
70
71 #endif // cpPlugins_QT4
72
73 // -------------------------------------------------------------------------
74 QDialog* cpPluginsIO::ImageReader::
75 CreateQDialog( )
76 {
77 #ifdef cpPlugins_QT4
78   ImageReaderQDialog* dlg = NULL;
79   if( QApplication::instance( ) != NULL )
80   {
81     dlg = new ImageReaderQDialog( );
82     dlg->setProcessObject( this );
83
84   } // fi
85   return( dlg );
86 #else // cpPlugins_QT4
87   return( NULL );
88 #endif // cpPlugins_QT4
89 }
90
91 // -------------------------------------------------------------------------
92 cpPluginsIO::ImageReader::
93 ImageReader( )
94   : Superclass( )
95 {
96   this->_AddOutput< cpPlugins::Image >( "Output" );
97   this->m_Parameters.Clear( );
98   this->m_Parameters.ConfigureAsOpenFileNameList( "FileNames" );
99   this->m_Parameters.SetAcceptedFileExtensions(
100     "FileNames",
101     "Image files (*.bmp *.png *.jpg *.jpeg *.dcm *.mhd *.nhdr *.nrrd *.tiff)"
102     );
103 }
104
105 // -------------------------------------------------------------------------
106 cpPluginsIO::ImageReader::
107 ~ImageReader( )
108 {
109 }
110
111 // -------------------------------------------------------------------------
112 void cpPluginsIO::ImageReader::
113 _GenerateData( )
114 {
115   // Get filenames
116   auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
117   if( fnames.size( ) >= 1 )
118   {
119     // Guess image properties
120     itk::ImageIOBase::Pointer io =
121       itk::ImageIOFactory::CreateImageIO(
122         fnames[ 0 ].c_str( ),
123         itk::ImageIOFactory::ReadMode
124         );
125     if( io.IsNotNull( ) )
126     {
127       io->SetFileName( fnames[ 0 ] );
128       io->ReadImageInformation( );
129       if( fnames.size( ) >= 1 )
130       {
131         switch( io->GetNumberOfDimensions( ) )
132         {
133         case 1: this->_GD0< 1 >( io ); break;
134         case 2: this->_GD0< 2 >( io ); break;
135         case 3: this->_GD0< 3 >( io ); break;
136         case 4: this->_GD0< 4 >( io ); break;
137         default:
138           this->_Error( "Image dimension not supported." );
139           break;
140         } // hctiws
141
142       } // fi
143     }
144     else
145       this->_Error(
146         std::string( "Could not create an ImageIO for \"" ) +
147         fnames[ 0 ] +
148         std::string( "\"" )
149         );
150   }
151   else
152     this->_Error( "No image(s) given" );
153 }
154
155 // -------------------------------------------------------------------------
156 template< unsigned int _Dim >
157 void cpPluginsIO::ImageReader::
158 _GD0( itk::ImageIOBase* io )
159 {
160   typedef unsigned char  uchar;
161   typedef unsigned short ushort;
162   typedef unsigned int   uint;
163   typedef unsigned long  ulong;
164
165   itk::ImageIOBase::IOComponentType ct = io->GetComponentType( );
166   itk::ImageIOBase::IOPixelType pt = io->GetPixelType( );
167
168   if( pt == itk::ImageIOBase::SCALAR )
169   {
170     switch( ct )
171     {
172     case itk::ImageIOBase::CHAR   : this->_GD1<   char, _Dim >( io ); break;
173     case itk::ImageIOBase::SHORT  : this->_GD1<  short, _Dim >( io ); break;
174     case itk::ImageIOBase::INT    : this->_GD1<    int, _Dim >( io ); break;
175     case itk::ImageIOBase::LONG   : this->_GD1<   long, _Dim >( io ); break;
176     case itk::ImageIOBase::FLOAT  : this->_GD1<  float, _Dim >( io ); break;
177     case itk::ImageIOBase::DOUBLE : this->_GD1< double, _Dim >( io ); break;
178     case itk::ImageIOBase::UCHAR  : this->_GD1<  uchar, _Dim >( io ); break;
179     case itk::ImageIOBase::USHORT : this->_GD1< ushort, _Dim >( io ); break;
180     case itk::ImageIOBase::UINT   : this->_GD1<   uint, _Dim >( io ); break;
181     case itk::ImageIOBase::ULONG  : this->_GD1<  ulong, _Dim >( io ); break;
182     default: this->_Error( "Scalar pixel type not supported." ); break;
183     } // hctiws
184   }
185   else if( pt == itk::ImageIOBase::RGB )
186   {
187     switch( ct )
188     {
189     case itk::ImageIOBase::CHAR   : this->_GD1< itk::RGBPixel<   char >, _Dim >( io ); break;
190     case itk::ImageIOBase::SHORT  : this->_GD1< itk::RGBPixel<  short >, _Dim >( io ); break;
191     case itk::ImageIOBase::INT    : this->_GD1< itk::RGBPixel<    int >, _Dim >( io ); break;
192     case itk::ImageIOBase::LONG   : this->_GD1< itk::RGBPixel<   long >, _Dim >( io ); break;
193     case itk::ImageIOBase::FLOAT  : this->_GD1< itk::RGBPixel<  float >, _Dim >( io ); break;
194     case itk::ImageIOBase::DOUBLE : this->_GD1< itk::RGBPixel< double >, _Dim >( io ); break;
195     case itk::ImageIOBase::UCHAR  : this->_GD1< itk::RGBPixel<  uchar >, _Dim >( io ); break;
196     case itk::ImageIOBase::USHORT : this->_GD1< itk::RGBPixel< ushort >, _Dim >( io ); break;
197     case itk::ImageIOBase::UINT   : this->_GD1< itk::RGBPixel<   uint >, _Dim >( io ); break;
198     case itk::ImageIOBase::ULONG  : this->_GD1< itk::RGBPixel<  ulong >, _Dim >( io ); break;
199     default: this->_Error( "RGB pixel type not supported." ); break;
200     } // hctiws
201   }
202   else if( pt == itk::ImageIOBase::RGBA )
203   {
204     switch( ct )
205     {
206     case itk::ImageIOBase::CHAR   : this->_GD1< itk::RGBAPixel<   char >, _Dim >( io ); break;
207     case itk::ImageIOBase::SHORT  : this->_GD1< itk::RGBAPixel<  short >, _Dim >( io ); break;
208     case itk::ImageIOBase::INT    : this->_GD1< itk::RGBAPixel<    int >, _Dim >( io ); break;
209     case itk::ImageIOBase::LONG   : this->_GD1< itk::RGBAPixel<   long >, _Dim >( io ); break;
210     case itk::ImageIOBase::FLOAT  : this->_GD1< itk::RGBAPixel<  float >, _Dim >( io ); break;
211     case itk::ImageIOBase::DOUBLE : this->_GD1< itk::RGBAPixel< double >, _Dim >( io ); break;
212     case itk::ImageIOBase::UCHAR  : this->_GD1< itk::RGBAPixel<  uchar >, _Dim >( io ); break;
213     case itk::ImageIOBase::USHORT : this->_GD1< itk::RGBAPixel< ushort >, _Dim >( io ); break;
214     case itk::ImageIOBase::UINT   : this->_GD1< itk::RGBAPixel<   uint >, _Dim >( io ); break;
215     case itk::ImageIOBase::ULONG  : this->_GD1< itk::RGBAPixel<  ulong >, _Dim >( io ); break;
216     default: this->_Error( "RGBA pixel type not supported." ); break;
217     } // hctiws
218   }
219   else if( pt == itk::ImageIOBase::COMPLEX )
220   {
221     switch( ct )
222     {
223     case itk::ImageIOBase::FLOAT  : this->_GD1< std::complex< float >, _Dim >( io ); break;
224     case itk::ImageIOBase::DOUBLE : this->_GD1< std::complex< double >, _Dim >( io ); break;
225     default: this->_Error( "Complex pixel type not supported." ); break;
226     } // hctiws
227   }
228   else if( pt == itk::ImageIOBase::COVARIANTVECTOR )
229   {
230     switch( ct )
231     {
232     case itk::ImageIOBase::FLOAT  : this->_GD1< itk::CovariantVector< float, _Dim >, _Dim >( io ); break;
233     case itk::ImageIOBase::DOUBLE : this->_GD1< itk::CovariantVector< double, _Dim >, _Dim >( io ); break;
234     default: this->_Error( "CovariantVector pixel type not supported." ); break;
235     } // hctiws
236   }
237   else if( pt == itk::ImageIOBase::POINT )
238   {
239     switch( ct )
240     {
241     case itk::ImageIOBase::FLOAT  : this->_GD1< itk::Point< float, _Dim >, _Dim >( io ); break;
242     case itk::ImageIOBase::DOUBLE : this->_GD1< itk::Point< double, _Dim >, _Dim >( io ); break;
243     default: this->_Error( "Point pixel type not supported." ); break;
244     } // hctiws
245   }
246   else if( pt == itk::ImageIOBase::VECTOR )
247   {
248     switch( ct )
249     {
250     case itk::ImageIOBase::FLOAT  : this->_GD1< itk::Vector< float, _Dim >, _Dim >( io ); break;
251     case itk::ImageIOBase::DOUBLE : this->_GD1< itk::Vector< double, _Dim >, _Dim >( io ); break;
252     default: this->_Error( "Vector pixel type not supported." ); break;
253     } // hctiws
254   }
255   else if( pt == itk::ImageIOBase::SYMMETRICSECONDRANKTENSOR )
256   {
257     switch( ct )
258     {
259     case itk::ImageIOBase::FLOAT  : this->_GD1< itk::SymmetricSecondRankTensor< float, _Dim >, _Dim >( io ); break;
260     case itk::ImageIOBase::DOUBLE : this->_GD1< itk::SymmetricSecondRankTensor< double, _Dim >, _Dim >( io ); break;
261     default: this->_Error( "SymmetricSecondRankTensor pixel type not supported." ); break;
262     } // hctiws
263   }
264   else if( pt == itk::ImageIOBase::DIFFUSIONTENSOR3D )
265   {
266     if( _Dim == 3 )
267     {
268       switch( ct )
269       {
270       case itk::ImageIOBase::FLOAT  : this->_GD1< itk::DiffusionTensor3D< float >, 3 >( io ); break;
271       case itk::ImageIOBase::DOUBLE : this->_GD1< itk::DiffusionTensor3D< double >, 3 >( io ); break;
272       default: this->_Error( "DiffusionTensor3D pixel type not supported." ); break;
273       } // hctiws
274     }
275     else
276       this->_Error( "DiffusionTensor3D dimension not supported." );
277   }
278   /* TODO
279      else if( pt == itk::ImageIOBase::OFFSET )
280      {
281      }
282      else if( pt == itk::ImageIOBase::FIXEDARRAY )
283      {
284      }
285      else if( pt == itk::ImageIOBase::MATRIX )
286      {
287      }
288   */
289   else
290     this->_Error( "Image pixel type not yet supported." );
291 }
292
293 // -------------------------------------------------------------------------
294 template< class _TPixel, unsigned int _Dim >
295 void cpPluginsIO::ImageReader::
296 _GD1( itk::ImageIOBase* io )
297 {
298   typedef itk::Image< _TPixel, _Dim > _TImage;
299
300   // Get filenames
301   auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
302   if( fnames.size( ) == 1 )
303   {
304     auto f = this->_CreateITK< itk::ImageFileReader< _TImage > >( );
305     f->SetFileName( fnames[ 0 ] );
306     f->SetImageIO( io );
307     try
308     {
309       f->Update( );
310       this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
311     }
312     catch( itk::ExceptionObject& err )
313     {
314       this->_Error( err.GetDescription( ) );
315     }
316   }
317   else // if( fnames.size( ) > 1 )
318   {
319     auto f = this->_CreateITK< itk::ImageSeriesReader< _TImage > >( );
320     for( auto i = fnames.begin( ); i != fnames.end( ); ++i )
321       f->AddFileName( *i );
322     f->SetImageIO( io );
323     try
324     {
325       f->Update( );
326       this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
327     }
328     catch( itk::ExceptionObject& err )
329     {
330       this->_Error( err.GetDescription( ) );
331     }
332
333   } // fi
334 }
335
336 // eof - $RCSfile$