creaImageIO_lib
creaImageIOWxAnySimpleDlg.cpp
Go to the documentation of this file.
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 # pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
16 #
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
21 # liability.
22 #
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
26 */
27 
28 
30 
32 #include <itkAnalyzeImageIO.h>
33 #include <itkImageFileReader.h>
34 #include <itkImageSeriesReader.h>
35 #include <itkImage.h>
36 #include <itkImageSeriesWriter.h>
37 #include <itkGDCMImageIO.h>
38 #include <itkDICOMSeriesFileNames.h>
39 #include <itkNumericSeriesFileNames.h>
40 #include <itkVectorImage.h>
41 #include <itkMetaImageIO.h>
42 #include <vtkImageReader2.h>
43 #include <vtkMetaImageReader.h>
44 #include <boost/filesystem/path.hpp>
45 #include <boost/filesystem.hpp>
46 #include <boost/utility.hpp>
47 #include <creaVtkBasicSlicer.h>
48 
49 
50 namespace creaImageIO
51 {
52 
54  WxAnySimpleDlg::WxAnySimpleDlg(wxWindow *parent,
55  wxString i_title,
56  const std::string i_namedescp ,
57  const std::string i_namedb)
58  : wxDialog(parent, -1,_T("DISPLAY IMAGES"), wxDefaultPosition, wxSize(230,150))
59  {
60  namedescp = i_namedescp;
61  namedb = i_namedb;
62 
63  if(!i_title.empty())
64  {
65  this->SetTitle(i_title);
66  }
67  // Button to select file(s)
68  wxButton *fileBut = new wxButton(this, -1,_T("Select a file to display"), wxPoint(10,7) );
69  Connect( fileBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxAnySimpleDlg::OnReadFile );
70 
71  // Button to select directory
72  wxButton *directoryBut = new wxButton(this, -1,_T("Select a directory to display"), wxPoint(10,40) );
73  Connect( directoryBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxAnySimpleDlg::OnReadDirectory );
74 
75  // button to select creaImageIO
76  wxButton *gimmickBut = new wxButton(this, -1,_T("Select Gimmick"), wxPoint(10,70) );
77  Connect( gimmickBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxAnySimpleDlg::OnReadGimmick );
78 
80  m_dicom = false;
81  Layout();
82  }
83 
85 // //
87 
88  void WxAnySimpleDlg::OnReadFile(wxCommandEvent& event)
89  {
90  int resultShowModal;
91  wxFileDialog* fileDlg = new wxFileDialog( 0, _T("Select file"), _T(""), _T(""), crea::std2wx("*"), wxOPEN |wxFD_MULTIPLE, wxDefaultPosition);
92 
93  resultShowModal = fileDlg->ShowModal();
94  if ( resultShowModal==wxID_OK )
95  {
96  wxArrayString wxArray;
97  fileDlg->GetPaths(wxArray);
98  if(wxArray.size() >0)
99  {
100  for( int i = 0; i < wxArray.GetCount(); i++)
101  {
102  std::string name = crea::wx2std(wxArray[i]);
103  // FOR THE MOMENT ONLY short 3D et short 4D
104  readImg(name);
105  }
106  }
107  else {
108  // TO DO WARNING MESSAGES
109  }
110  }
111  SetReturnCode( resultShowModal );
112 // Close();
113  EndModal( resultShowModal );
114  }
115 
116  void WxAnySimpleDlg::setExts(std::vector<std::string> i_exts)
117  {
118  m_exts = i_exts;
119  }
121 // //
123 
124  void WxAnySimpleDlg::OnReadDirectory(wxCommandEvent &event)
125  {
126  int resultShowModal;
127  bool bvalid = false;
128  long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
129  wxDirDialog* dirDlg = new wxDirDialog( 0, _T("Select the directory to display"), _T(""), style);
130 
131  resultShowModal = dirDlg->ShowModal();
132  if ( resultShowModal==wxID_OK )
133  {
134  std::string path = crea::wx2std(dirDlg->GetPath());
135  typedef boost::filesystem::directory_iterator dir_it;
136  dir_it itr(path);
137  dir_it end_itr;
138  /*if (boost::filesystem::exists(path))
139  {*/
140  for(;itr != end_itr; ++itr)
141  {
142  bvalid = m_exts.size() == 0? true : false;
143  std::vector<std::string>::iterator it = m_exts.begin();
144  std::string ext = itr->path().filename().string().substr(itr->path().filename().string().find_last_of("."));
145  for(; it != m_exts.end(); it++)
146  {
147  if(ext == (*it) )
148  {
149  bvalid = true;
150  break;
151  }
152  }
153  if (!boost::filesystem::is_directory(itr->status()) && bvalid)
154  {
155  readImg(itr->path().string().c_str());
156  }
157  }
158  }
159  SetReturnCode( resultShowModal );
160 // Close();
161  EndModal( resultShowModal );
162  }
163 
165 // //
167 
168  void WxAnySimpleDlg::OnReadGimmick(wxCommandEvent &event)
169  {
170  // Run Gimmick
171  WxGimmickReaderDialog dlg(0,-1,
172  namedescp,
173  namedb,
174  _T("Select image(s) - Gimmick! (c) CREATIS-LRMN 2008"),
175  wxDefaultPosition,
176  wxSize(810,750),
179  _3D,
180  1);
181  dlg.ShowModal();
182  if (dlg.GetReturnCode() == wxID_OK)
183  {
184  std::vector<std::string> out;
185  dlg.stopReading();
186  dlg.GetSelectedFiles(out);
187  if(m_dicom)
188  {
189  readDicomImg( out);
190  }
191  else
192  {
193  std::vector<std::string>::iterator ii = out.begin();
194  for (;ii != out.end();ii++)
195  {
196  readImg( (*ii).c_str() );
197  }
198  }
199  dlg.OnExit();
200  }
201  SetReturnCode( dlg.GetReturnCode() );
202 // Close();
203  EndModal( dlg.GetReturnCode() );
204  }
205 
206 
207 
209  {
210  return infoimage;
211  }
212 
213 
214  // FCY : just to check but not needed, we hardly suppose that we load only same type and dim of images
215  const std::type_info & WxAnySimpleDlg::getType(const std::string &i_name)
216  {
217  typedef itk::ImageIOBase::IOComponentType ScalarPixelType;
218  itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(i_name.c_str(), itk::ImageIOFactory::ReadMode);
219  imageIO->SetFileName(i_name.c_str());
220  imageIO->ReadImageInformation();
221  return imageIO->GetComponentTypeInfo();//AsString( imageIO->GetComponentType());
222  }
223 
224  const size_t WxAnySimpleDlg::getNumberOfDimensions(const std::string &i_name)
225  {
226  typedef itk::ImageIOBase::IOComponentType ScalarPixelType;
227  itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(i_name.c_str(), itk::ImageIOFactory::ReadMode);
228  imageIO->SetFileName(i_name.c_str());
229  imageIO->ReadImageInformation();
230  return imageIO->GetNumberOfDimensions();
231  }
232 
233  void WxAnySimpleDlg::readImg(const std::string &i_name)
234  {
235  size_t dims = getNumberOfDimensions(i_name);
236  //const std::type_info *type= &getType(i_name);
237  switch(dims)
238  {
239  case 3:
240  if (getType(i_name) == typeid(unsigned char))
241  {
242  typedef itk::Image<unsigned char, 3> TImage;
243  typedef itk::ImageFileReader<TImage> ReaderType;
244  ReaderType::Pointer reader = ReaderType::New();
245  reader->SetFileName( i_name );
246  reader->Update();
247  reader->GetOutput()->Register();
248  m_AnyImages.push_back(reader->GetOutput());
249  }
250  else if (getType(i_name) == typeid(signed char))
251  {
252  typedef itk::Image<signed char, 3> TImage;
253  typedef itk::ImageFileReader<TImage> ReaderType;
254  ReaderType::Pointer reader = ReaderType::New();
255  reader->SetFileName( i_name );
256  reader->Update();
257  reader->GetOutput()->Register();
258  m_AnyImages.push_back(reader->GetOutput());
259  }
260  else if (getType(i_name) == typeid(short))
261  {
262  typedef itk::Image<short, 3> TImage;
263  typedef itk::ImageFileReader<TImage> ReaderType;
264  ReaderType::Pointer reader = ReaderType::New();
265  reader->SetFileName( i_name );
266  reader->Update();
267  reader->GetOutput()->Register();
268  m_AnyImages.push_back(reader->GetOutput());
269  }
270  else if (getType(i_name) == typeid(unsigned short))
271  {
272  typedef itk::Image<unsigned short, 3> TImage;
273  typedef itk::ImageFileReader<TImage> ReaderType;
274  ReaderType::Pointer reader = ReaderType::New();
275  reader->SetFileName( i_name );
276  reader->Update();
277  reader->GetOutput()->Register();
278  m_AnyImages.push_back(reader->GetOutput());
279  }
280  else if (getType(i_name) == typeid(unsigned int))
281  {
282  typedef itk::Image<unsigned int, 3> TImage;
283  typedef itk::ImageFileReader<TImage> ReaderType;
284  ReaderType::Pointer reader = ReaderType::New();
285  reader->SetFileName( i_name );
286  reader->Update();
287  reader->GetOutput()->Register();
288  m_AnyImages.push_back(reader->GetOutput());
289  }
290  else if (getType(i_name) == typeid(signed int))
291  {
292  typedef itk::Image<signed int, 3> TImage;
293  typedef itk::ImageFileReader<TImage> ReaderType;
294  ReaderType::Pointer reader = ReaderType::New();
295  reader->SetFileName( i_name );
296  reader->Update();
297  reader->GetOutput()->Register();
298  m_AnyImages.push_back(reader->GetOutput());
299  }
300  else if (getType(i_name) == typeid(unsigned long))
301  {
302  typedef itk::Image<unsigned long, 3> TImage;
303  typedef itk::ImageFileReader<TImage> ReaderType;
304  ReaderType::Pointer reader = ReaderType::New();
305  reader->SetFileName( i_name );
306  reader->Update();
307  reader->GetOutput()->Register();
308  m_AnyImages.push_back(reader->GetOutput());
309  }
310  else if (getType(i_name) == typeid(signed long))
311  {
312  typedef itk::Image<signed long, 3> TImage;
313  typedef itk::ImageFileReader<TImage> ReaderType;
314  ReaderType::Pointer reader = ReaderType::New();
315  reader->SetFileName( i_name );
316  reader->Update();
317  reader->GetOutput()->Register();
318  m_AnyImages.push_back(reader->GetOutput());
319  }
320  else if (getType(i_name) == typeid(float))
321  {
322  typedef itk::Image<float, 3> TImage;
323  typedef itk::ImageFileReader<TImage> ReaderType;
324  ReaderType::Pointer reader = ReaderType::New();
325  reader->SetFileName( i_name );
326  reader->Update();
327  reader->GetOutput()->Register();
328  m_AnyImages.push_back(reader->GetOutput());
329  }
330  else if (getType(i_name) == typeid(double))
331  {
332  typedef itk::Image<double, 3> TImage;
333  typedef itk::ImageFileReader<TImage> ReaderType;
334  ReaderType::Pointer reader = ReaderType::New();
335  reader->SetFileName( i_name );
336  reader->Update();
337  reader->GetOutput()->Register();
338  m_AnyImages.push_back(reader->GetOutput());
339  }
340  else
341  {
342  //????FCY, so what the type????
343  }
344  break;
345  //assume that we have only one 4D file
346  case 4:
347  if (getType(i_name) == typeid(unsigned char))
348  {
349  typedef itk::Image<unsigned char, 4> TImage;
350  typedef itk::ImageFileReader<TImage> ReaderType;
351  ReaderType::Pointer reader = ReaderType::New();
352  reader->SetFileName( i_name );
353  reader->Update();
354  reader->GetOutput()->Register();
355  m_AnyImages.push_back(reader->GetOutput());
356  split4Din3Dvtk<TImage>(reader->GetOutput());
357  }
358  else if (getType(i_name) == typeid(signed char))
359  {
360  typedef itk::Image<signed char, 4> TImage;
361  typedef itk::ImageFileReader<TImage> ReaderType;
362  ReaderType::Pointer reader = ReaderType::New();
363  reader->SetFileName( i_name );
364  reader->Update();
365  reader->GetOutput()->Register();
366  m_AnyImages.push_back(reader->GetOutput());
367  split4Din3Dvtk<TImage>(reader->GetOutput());
368  }
369  else if (getType(i_name) == typeid(unsigned short))
370  {
371  typedef itk::Image<unsigned short, 4> TImage;
372  typedef itk::ImageFileReader<TImage> ReaderType;
373  ReaderType::Pointer reader = ReaderType::New();
374  reader->SetFileName( i_name );
375  reader->Update();
376  reader->GetOutput()->Register();
377  m_AnyImages.push_back(reader->GetOutput());
378  split4Din3Dvtk<TImage>(reader->GetOutput());
379  }
380  else if(getType(i_name) == typeid(short))
381  {
382  typedef itk::Image<short, 4> TImage;
383  typedef itk::ImageFileReader<TImage> ReaderType;
384  ReaderType::Pointer reader = ReaderType::New();
385  reader->SetFileName( i_name );
386  try
387  {
388  reader->Update();
389  }
390  catch( itk::ExceptionObject & err )
391  {
392  std::cout << "Caught an exception reading" << i_name << ": " << std::endl;
393  std::cout << err << " " << __FILE__ << " " << __LINE__ << std::endl;
394  throw err;
395  }
396  catch(...)
397  {
398  std::cout << "Error while reading image " << i_name << std::endl;
399  throw;
400  }
401  reader->GetOutput()->Register();
402  m_AnyImages.push_back(reader->GetOutput());
403  split4Din3Dvtk<TImage>(reader->GetOutput());
404  }
405  else if (getType(i_name) == typeid(unsigned short))
406  {
407  typedef itk::Image<unsigned short, 4> TImage;
408  typedef itk::ImageFileReader<TImage> ReaderType;
409  ReaderType::Pointer reader = ReaderType::New();
410  reader->SetFileName( i_name );
411  reader->Update();
412  reader->GetOutput()->Register();
413  m_AnyImages.push_back(reader->GetOutput());
414  split4Din3Dvtk<TImage>(reader->GetOutput());
415  }
416  else if (getType(i_name) == typeid(unsigned int))
417  {
418  typedef itk::Image<unsigned int, 4> TImage;
419  typedef itk::ImageFileReader<TImage> ReaderType;
420  ReaderType::Pointer reader = ReaderType::New();
421  reader->SetFileName( i_name );
422  reader->Update();
423  reader->GetOutput()->Register();
424  m_AnyImages.push_back(reader->GetOutput());
425  split4Din3Dvtk<TImage>(reader->GetOutput());
426  }
427  else if (getType(i_name) == typeid(signed int))
428  {
429  typedef itk::Image<signed int, 4> TImage;
430  typedef itk::ImageFileReader<TImage> ReaderType;
431  ReaderType::Pointer reader = ReaderType::New();
432  reader->SetFileName( i_name );
433  reader->Update();
434  reader->GetOutput()->Register();
435  m_AnyImages.push_back(reader->GetOutput());
436  split4Din3Dvtk<TImage>(reader->GetOutput());
437  }
438  else if (getType(i_name) == typeid(unsigned long))
439  {
440  typedef itk::Image<unsigned long, 4> TImage;
441  typedef itk::ImageFileReader<TImage> ReaderType;
442  ReaderType::Pointer reader = ReaderType::New();
443  reader->SetFileName( i_name );
444  reader->Update();
445  reader->GetOutput()->Register();
446  m_AnyImages.push_back(reader->GetOutput());
447  split4Din3Dvtk<TImage>(reader->GetOutput());
448  }
449  else if (getType(i_name) == typeid(signed long))
450  {
451  typedef itk::Image<signed long, 4> TImage;
452  typedef itk::ImageFileReader<TImage> ReaderType;
453  ReaderType::Pointer reader = ReaderType::New();
454  reader->SetFileName( i_name );
455  reader->Update();
456  reader->GetOutput()->Register();
457  m_AnyImages.push_back(reader->GetOutput());
458  split4Din3Dvtk<TImage>(reader->GetOutput());
459  }
460  else if (getType(i_name) == typeid(float))
461  {
462  typedef itk::Image<float, 4> TImage;
463  typedef itk::ImageFileReader<TImage> ReaderType;
464  ReaderType::Pointer reader = ReaderType::New();
465  reader->SetFileName( i_name );
466  reader->Update();
467  reader->GetOutput()->Register();
468  m_AnyImages.push_back(reader->GetOutput());
469  split4Din3Dvtk<TImage>(reader->GetOutput());
470  }
471  else if (getType(i_name) == typeid(double))
472  {
473  typedef itk::Image<double, 4> TImage;
474  typedef itk::ImageFileReader<TImage> ReaderType;
475  ReaderType::Pointer reader = ReaderType::New();
476  reader->SetFileName( i_name );
477  reader->Update();
478  reader->GetOutput()->Register();
479  m_AnyImages.push_back(reader->GetOutput());
480  split4Din3Dvtk<TImage>(reader->GetOutput());
481  }
482  else
483  {
484  //????FCY, so what the type????
485  }
486  break;
487  }
488 
489  // Test to know if we have to add new type
490  bool btest = false;
491  if( m_AnyDims.size() != 0)
492  {
493  btest = true;
494  }
495  else
496  {
497  m_AnyDims.push_back(dims);
498  m_AnyType.push_back((std::type_info*)&getType(i_name)); //JPR cast
499  }
500  if (btest)
501  {
502  if(m_AnyDims.front() != dims || m_AnyType.front()->before(getType(i_name)))
503  {
504  m_AnyDims.push_back(dims);
505  m_AnyType.push_back((std::type_info*)&getType(i_name)); //JPR cast
506  }
507  else{}
508  }
509  else {}
510  }
511 
512  void WxAnySimpleDlg::readDicomImg(const std::vector<std::string> &i_names)
513  {
514  typedef itk::Image<short,3> TImage;
515  typedef itk::GDCMImageIO GDCMType;
516  typedef itk::DICOMSeriesFileNames dicnames;
517  GDCMType::Pointer gdcmIO = GDCMType::New();
518  dicnames::Pointer generator = dicnames::New();
519 
520 
521  typedef itk::ImageSeriesReader<TImage> ReaderType;
522  ReaderType::Pointer reader = ReaderType::New();
523  reader->SetImageIO(gdcmIO);
524  reader->SetFileNames(i_names);
525  // reader->SetFileName( i_name );
526  try
527  {
528  reader->Update();
529  }
530  catch( itk::ExceptionObject & err )
531  {
532  // std::cout << "Caught an exception reading" << i_name << ": " << std::endl;
533  std::cout << err << " " << __FILE__ << " " << __LINE__ << std::endl;
534  throw err;
535  }
536  catch(...)
537  {
538  //std::cout << "Error while reading image " << i_name << std::endl;
539  throw;
540  }
541  //m_Iresults.push_back(reader->GetOutput());
542 
543  }
544 
545  /* template <typename TImage>
546  void WxAnySimpleDlg::split3Din3Dvtk(TImage* i_Img)
547  {
548  typedef itk::ImageToVTKImageFilter< TImage > ConnectorType;
549  typename ConnectorType::Pointer connector = ConnectorType::New();
550  connector->SetInput(i_Img);
551  connector->GetImporter()->SetDataScalarTypeToUnsignedChar();
552  connector->Update();
553  vtkImageData *im = vtkImageData::New();
554  im->ShallowCopy(connector->GetOutput());
555  im->Update();
556  m_Vresults.push_back(im);
557  }*/
558 
559 
560  template <typename TImage>
561  void WxAnySimpleDlg::split4Din3Dvtk(TImage* i_Img)
562  {
563  if(i_Img->GetImageDimension() == 4)
564  {
565  // size of fourth dimension
566  int dsize = i_Img->GetLargestPossibleRegion().GetSize(3);
567  // Output Type
568  typedef itk::Image<unsigned char,3> ImageOutputType;
569  typedef itk::ImageSeriesWriter<TImage, ImageOutputType > SeriesWriterType;
570  typedef itk::MetaImageIO MetaImageType;
571  MetaImageType::Pointer metaIO;
572  typename SeriesWriterType::Pointer seriesWriter = SeriesWriterType::New();
573 
574  // from JPR file to generate output files
575  typedef itk::NumericSeriesFileNames NamesGeneratorType;
576  NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New();
577  namesGenerator->SetStartIndex(0);
578  namesGenerator->SetEndIndex(dsize-1);
579  namesGenerator->SetIncrementIndex(1);
580  std::string format = m_dir;
581  format += "/image%03d.mhd";
582  namesGenerator->SetSeriesFormat( format.c_str() );
583 
584  const std::vector<std::string> names = namesGenerator->GetFileNames();
585  seriesWriter->SetFileNames( names );
586  seriesWriter->SetInput(i_Img);
587  seriesWriter->SetImageIO(metaIO);
588  try
589  {
590  seriesWriter->Update();
591  vtkMetaImageReader *vReader = vtkMetaImageReader::New();
592  std::vector<std::string>::const_iterator it = names.begin();
593  for( ;it != names.end(); ++it)
594  {
595  vReader->SetFileName((*it).c_str());
596  vReader->Update();
597  m_Vresults.push_back(vReader->GetOutput());
598 
599  }
600  }
601  catch( itk::ExceptionObject & excp )
602  {
603  std::cerr << "Exception thrown while writing the series " << std::endl;
604  std::cerr << excp << std::endl;
605  //return EXIT_FAILURE;
606  }
607  }
608  }
609 
611  {
612  if(m_AnyType.size() == 1)
613  {
614  return true;
615  }
616  else
617  {
618  return false;
619  }
620 
621  }
622 
623  } // namespace end
624