]> Creatis software - creaImageIO.git/blobdiff - src/creaImageIOWxISimpleDlg.hpp
#3185 creaImageIO Feature New Normal - Clean code
[creaImageIO.git] / src / creaImageIOWxISimpleDlg.hpp
index e310a0736c7fe581568e3582964c7c5135bba59c..3b70726dab880476230d7ce88bfccb9b1f6c9ee9 100644 (file)
@@ -1,25 +1,61 @@
+/*
+# ---------------------------------------------------------------------
+#
+# Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
+#                        pour la Santé)
+# Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
+# Previous Authors : Laurent Guigues, Jean-Pierre Roux
+# CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
+#
+#  This software is governed by the CeCILL-B license under French law and 
+#  abiding by the rules of distribution of free software. You can  use, 
+#  modify and/ or redistribute the software under the terms of the CeCILL-B 
+#  license as circulated by CEA, CNRS and INRIA at the following URL 
+#  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
+#  or in the file LICENSE.txt.
+#
+#  As a counterpart to the access to the source code and  rights to copy,
+#  modify and redistribute granted by the license, users are provided only
+#  with a limited warranty  and the software's author,  the holder of the
+#  economic rights,  and the successive licensors  have only  limited
+#  liability. 
+#
+#  The fact that you are presently reading this means that you have had
+#  knowledge of the CeCILL-B license and that you accept its terms.
+# ------------------------------------------------------------------------
+*/
+
+
+#ifndef __creaImageIOWxSimpleDlg_hpp_INCLUDED__
+#define __creaImageIOWxSimpleDlg_hpp_INCLUDED__
+
+
 //#include "creaImageIOWxISimpleDlg.h"
 
 #include "creaImageIOWxGimmickReaderDialog.h"
 #include <itkAnalyzeImageIO.h>
 #include <itkImageFileReader.h>
+#include <itkImageSeriesReader.h>
 #include <itkImage.h>
 #include <itkImageSeriesWriter.h>
+#include <itkGDCMImageIO.h>
+#include <itkDICOMSeriesFileNames.h>
 #include <itkNumericSeriesFileNames.h>
-#include "itkImageToVTKImageFilter.h"
+#include <itkVectorImage.h>
+#include <itkMetaImageIO.h>
+#include <itkOrientedImage.h>
 #include <vtkImageReader2.h>
+#include <vtkMetaImageReader.h>
 #include <boost/filesystem/path.hpp>
 #include <boost/filesystem.hpp>
 #include <boost/utility.hpp>
+#include <creaVtkBasicSlicer.h>
 
 namespace creaImageIO
 {
-               template <typename TImage2> 
+  template <typename TImage2> 
   typename TImage2::Pointer ReadImage( const std::string &fileName) 
   { 
-
-
-
     typename TImage2::Pointer image; 
 
     typedef itk::ImageFileReader<TImage2> ReaderType; 
@@ -46,173 +82,170 @@ namespace creaImageIO
   } 
 
 
-       ///Ctor
+  ///Ctor
   template <typename TImage> 
    WxISimpleDlg<TImage>::WxISimpleDlg(wxWindow *parent, 
-                                                       wxString i_title,  
-                                                       const std::string i_namedescp , 
-                                                       const std::string i_namedb)
-    : wxDialog(parent, -1,_T("DISPLAY IMAGES"), wxDefaultPosition, wxSize(230,150))
+                                      wxString i_title,  
+                                      const std::string i_namedescp , 
+                                      const std::string i_namedb)
+    : wxDialog(parent, -1,_T("SELECT IMAGE(S)"), wxDefaultPosition, wxSize(230,150))
    {
-               namedescp       = i_namedescp; 
-               namedb          = i_namedb;
-
-          if(!i_title.empty())
-          {
-                       this->SetTitle(i_title);  
-          }
-          // Button to select file(s)
-          wxButton *fileBut = new wxButton(this, -1,_T("Select a file to display"), wxPoint(10,7) );
-          Connect( fileBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxISimpleDlg::OnReadFile ); 
-
-          // Button to select directory
-          wxButton *directoryBut = new wxButton(this, -1,_T("Select a directory to display"), wxPoint(10,40) );
-          Connect( directoryBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxISimpleDlg::OnReadDirectory ); 
-
-          // button to select creaImageIO
-          wxButton *gimmickBut = new wxButton(this, -1,_T("Select Gimmick"), wxPoint(10,70) );
-          Connect( gimmickBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxISimpleDlg::OnReadGimmick ); 
-
-          /// \TODO  Button to select Bruker directory
-       
-       Layout(); 
-       
-       }
+           namedescp = i_namedescp; 
+           namedb    = i_namedb;
+
+       if(!i_title.empty())
+       {
+           this->SetTitle(i_title);
+       }
+       // Button to select file(s)
+       wxButton *fileBut = new wxButton(this, -1,_T("Select a file to display"), wxPoint(10,7) );
+       Connect( fileBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxISimpleDlg::OnReadFile ); 
+
+       // Button to select directory
+       wxButton *directoryBut = new wxButton(this, -1,_T("Select a directory to display"), wxPoint(10,40) );
+       Connect( directoryBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxISimpleDlg::OnReadDirectory ); 
+
+       // button to select creaImageIO
+       wxButton *gimmickBut = new wxButton(this, -1,_T("Select Gimmick"), wxPoint(10,70) );
+       Connect( gimmickBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxISimpleDlg::OnReadGimmick ); 
+
+       /// \TODO  Button to select Bruker directory
+       m_dicom = true;
+       Layout(); 
+    
+    }
 //////////////////////////////////////////////////////////////////////
 //                                                                  //
 //////////////////////////////////////////////////////////////////////
      template <typename TImage> 
-         void WxISimpleDlg<TImage>::OnReadFile(wxCommandEvent& event)
-         {
-                 int resultShowModal;
-                 wxFileDialog* fileDlg = new wxFileDialog( 0,  _T("Select file"), _T(""), _T(""), crea::std2wx("*"), wxOPEN |wxFD_MULTIPLE, wxDefaultPosition);
+      void WxISimpleDlg<TImage>::OnReadFile(wxCommandEvent& event)
+      {
+          int resultShowModal;
+          wxFileDialog* fileDlg = new wxFileDialog( 0,  _T("Select file"), _T(""), _T(""), crea::std2wx("*"), wxOPEN |wxFD_MULTIPLE, wxDefaultPosition);
     
-                   resultShowModal = fileDlg->ShowModal();
-                       if ( resultShowModal==wxID_OK )
-                       {
-                               wxArrayString wxArray;
-                               fileDlg->GetPaths(wxArray);
-                               if(wxArray.size() >0)
-                               {
-                                       for( int i = 0; i < wxArray.GetCount(); i++)
-                                       {
-                                               readImg( crea::wx2std(wxArray[i]));
-                                       }
-                               } 
-                               else {
-                                       // TO DO WARNING MESSAGES
-                               }
-                       }
-                 SetReturnCode( resultShowModal );
-//              Close();
-                EndModal( resultShowModal );
-         }
-
-
-           template <typename TImage> 
-               void WxISimpleDlg<TImage>::setExts(std::vector<std::string> i_exts)
-               {
-                       m_exts = i_exts;
-               }
- //////////////////////////////////////////////////////////////////////
-//                                                                                                                                     //
+          resultShowModal = fileDlg->ShowModal();
+          if ( resultShowModal==wxID_OK )
+            {
+                wxArrayString wxArray;
+                fileDlg->GetPaths(wxArray);
+                if(wxArray.size() >0)
+                {
+                    for( int i = 0; i < wxArray.GetCount(); i++)
+                    {
+                        readImg( crea::wx2std(wxArray[i]));
+                    }
+                } 
+                else {
+                    /// \TODO WARNING MESSAGES
+                }
+            }
+         SetReturnCode( resultShowModal );
+//       Close();
+         EndModal( resultShowModal );
+      }
+
+
+        template <typename TImage> 
+        void WxISimpleDlg<TImage>::setExts(std::vector<std::string> i_exts)
+        {
+            m_exts = i_exts;
+        }
 //////////////////////////////////////////////////////////////////////
-           template <typename TImage> 
-         void WxISimpleDlg<TImage>::OnReadDirectory(wxCommandEvent &event)
-         {
-                 int resultShowModal;
-                 bool bvalid = false;
-            long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
-                wxDirDialog* dirDlg = new wxDirDialog( 0, _T("Select the directory to display"), _T(""), style);
-                
-                resultShowModal = dirDlg->ShowModal();
-                if ( resultShowModal==wxID_OK )
-                {  
-                       std::string path = crea::wx2std(dirDlg->GetPath());
-                       typedef boost::filesystem::directory_iterator dir_it;
-                       dir_it itr(path);
-                       dir_it end_itr;
-                       /*if (boost::filesystem::exists(path))
-                       {*/
-                               for(;itr != end_itr; ++itr)
-                               {
-                                          bvalid = m_exts.size() == 0? true : false;
-                                          std::vector<std::string>::iterator it = m_exts.begin();
-                                          std::string ext = itr->filename().substr(itr->filename().find_last_of("."));
-                                          for(; it != m_exts.end(); it++)
-                                          {
-                                                  if(ext == (*it) )
-                                                  {
-                                                          bvalid = true;
-                                                          break;
-                                                  }
-                                          }
-                                       if (!boost::filesystem::is_directory(itr->status()) && bvalid)
-                                       {
-                                               readImg(itr->string().c_str());
-                                       }
-                               }
-                }
-                SetReturnCode( resultShowModal );
-//              Close();
-                EndModal( resultShowModal );
-         }
+//                                                                  //
+//////////////////////////////////////////////////////////////////////
+   template <typename TImage> 
+      void WxISimpleDlg<TImage>::OnReadDirectory(wxCommandEvent &event)
+      {
+         int resultShowModal;
+         bool bvalid = false;
+         long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
+         wxDirDialog* dirDlg = new wxDirDialog( 0, _T("Select the directory to display"), _T(""), style);
+         
+         resultShowModal = dirDlg->ShowModal();
+         if ( resultShowModal==wxID_OK )
+         {  
+            std::string path = crea::wx2std(dirDlg->GetPath());
+            typedef boost::filesystem::directory_iterator dir_it;
+            dir_it itr(path);
+            dir_it end_itr;
+            /*if (boost::filesystem::exists(path))
+            {*/
+                for(;itr != end_itr; ++itr)
+                {
+                       bvalid = m_exts.size() == 0? true : false;
+                       std::vector<std::string>::iterator it = m_exts.begin();
+                       std::string ext = itr->filename().substr(itr->filename().find_last_of("."));
+                       for(; it != m_exts.end(); it++)
+                       {
+                           if(ext == (*it) )
+                           {
+                               bvalid = true;
+                               break;
+                           }
+                       }
+                    if (!boost::filesystem::is_directory(itr->status()) && bvalid)
+                    {
+                        readImg(itr->string().c_str());
+                    }
+                }
+         }
+         SetReturnCode( resultShowModal );
+//         Close();
+         EndModal( resultShowModal );
+      }
 
-         //////////////////////////////////////////////////////////////////////
-//                                                                                                                                     //
 //////////////////////////////////////////////////////////////////////
-                   template <typename TImage> 
-         void WxISimpleDlg<TImage>::OnReadGimmick(wxCommandEvent &event)
-         {
-                 // Run Gimmick
-                  WxGimmickReaderDialog dlg(0,-1, 
-                                  namedescp,
-                                  namedb, 
-                                  _T("Select image(s)        - Gimmick! (c) CREATIS-LRMN 2008"),
+//                                                                  //
+//////////////////////////////////////////////////////////////////////
+            template <typename TImage> 
+      void WxISimpleDlg<TImage>::OnReadGimmick(wxCommandEvent &event)
+      {
+          // Run Gimmick
+           WxGimmickReaderDialog dlg(0,-1, 
+                   namedescp,
+                   namedb, 
+                   _T("Select image(s)        - Gimmick! (c) CREATIS-LRMN 2008"),
                    wxDefaultPosition,
                    wxSize(810,750),
                    GIMMICK_2D_IMAGE_SELECTION,
                    GIMMICK_3D_IMAGE_SELECTION,
                    _3D,
-                                  1);
-                dlg.ShowModal();
-                if (dlg.GetReturnCode() == wxID_OK)
-            {
-       //              infoimage=_T("DICOM: ???_EED_???");
-//EED 07JUIN2010                       dlg.GetSelectedImages(m_results,3);
-                       std::vector<creaImageIO::OutStrGimmick> out;
-                       std::vector<std::string> attr;
-//                             attr.push_back("D0028_0010");
-//                             attr.push_back("D0008_0023");
-//                             attr.push_back("D0008_1070");
-                               dlg.stopReading();
-                               dlg.getSelected(out, attr,true,"");
-//                             crea::VtkBasicSlicer(out.front().img);
-                       //m_results.clear();
-                       int size=out.size();
-                       int ii;
-                       //for (ii=0;ii<size;ii++)
-                       //{
-                               //m_results.push_back(out[ii].img);
-                       //}
-
-                       dlg.OnExit();
-                }
-                SetReturnCode( dlg.GetReturnCode() );
-//              Close();
-                EndModal( dlg.GetReturnCode() );
-          }
+                   1);
+         dlg.ShowModal();
+         if (dlg.GetReturnCode() == wxID_OK)
+         {
+            std::vector<std::string> out;
+            dlg.stopReading();
+            dlg.GetSelectedFiles(out);
+            if(m_dicom)
+            {
+                readDicomImg( out);
+            }
+            else
+            {
+                std::vector<std::string>::iterator ii = out.begin();
+                for (;ii != out.end();ii++)
+                {
+                    readImg( (*ii).c_str() );
+                }
+            }
+            dlg.OnExit();
+         }
+         SetReturnCode( dlg.GetReturnCode() );
+//         Close();
+         EndModal( dlg.GetReturnCode() );
+       }
 
 
      template <typename TImage> 
-        wxString WxISimpleDlg<TImage>::getInfoImage()
-       {
-               return infoimage;
-       }
+     wxString WxISimpleDlg<TImage>::getInfoImage()
+    {
+        return infoimage;
+    }
 
 
     //////////////////////////////////////////////////////////////////////
-    // Return the results vector                                                                               //
+    // Return the results vector                                        //
     //////////////////////////////////////////////////////////////////////
         template <typename TImage>  
         std::vector<typename TImage::Pointer> WxISimpleDlg<TImage>::getImagesSelected()
@@ -256,26 +289,58 @@ namespace creaImageIO
 
 
 
+         template <typename TImage> 
+         void WxISimpleDlg<TImage>::readDicomImg(const std::vector<std::string> &i_names)
+         {
+                 typedef itk::GDCMImageIO GDCMType;
+                 typedef itk::DICOMSeriesFileNames dicnames;
+                 GDCMType::Pointer gdcmIO = GDCMType::New(); 
+                 dicnames::Pointer generator = dicnames::New();
+//               generator->SetInput(i_names);
+                       typedef itk::ImageSeriesReader<TImage> ReaderType; 
+                   typename ReaderType::Pointer reader = ReaderType::New(); 
+                       reader->SetImageIO(gdcmIO);
+                       reader->SetFileNames(i_names);
+               //      reader->SetFileName( i_name ); 
+                       try 
+                       { 
+                               reader->Update(); 
+                       } 
+                       catch( itk::ExceptionObject & err ) 
+                       { 
+                       //      std::cout << "Caught an exception reading" << i_name << ": " << std::endl; 
+                               std::cout << err << " " << __FILE__ << " " << __LINE__ << std::endl; 
+                               throw err; 
+                       } 
+                       catch(...) 
+                       { 
+                               //std::cout << "Error while reading image " << i_name << std::endl; 
+                               throw; 
+                       } 
+                       m_Iresults.push_back(reader->GetOutput()); 
+    
+         }
+
+
+
+
          template <typename TImage> 
          void WxISimpleDlg<TImage>::split3Din3Dvtk()
          {
-                
-                int ii;
                  if ((m_Iresults).size() != 0)
                  {
-
-                         typename TImage::Pointer image = m_Iresults[0];
-                         image->GetImageDimension();
-                         typedef itk::ImageToVTKImageFilter< TImage > ConnectorType;
-                         ConnectorType::Pointer connector = ConnectorType::New();
-                         for(unsigned int i = 0 ;i < m_Iresults.size(); i++)
-                                  {
-                                               connector->SetInput(m_Iresults[i]);
-                                               connector->Update();
-                                               vtkImageData *im = vtkImageData::New();
-                                               im->ShallowCopy(connector->GetOutput());
-                                               m_Vresults.push_back(im);
-                                  }
+                         typedef itk::ImageToVTKImageFilter<  TImage > ConnectorType;
+                         ConnectorType::Pointer          connector = ConnectorType::New();
+             for(unsigned int i = 0 ;i < m_Iresults.size(); i++)
+                     {
+                                       connector->SetInput(m_Iresults[i]);
+                                       connector->GetImporter()->SetDataScalarTypeToUnsignedChar();
+                                       connector->Update();
+                                       vtkImageData *im = vtkImageData::New();
+                                       im->ShallowCopy(connector->GetOutput());
+                                       im->Update();
+                                       m_Vresults.push_back(im);
+                    }
                  }
          }
 
@@ -288,52 +353,58 @@ namespace creaImageIO
                        {
                            typename TImage::Pointer image = m_Iresults[0];
                        
-                                // Size and dims for Input Image
-                               typedef itk::Size<4> iSize;
-                               typedef const itk::Size<4>::SizeValueType iSizeVal;
-                               iSize size = image->GetLargestPossibleRegion().GetSize();
-                               iSizeVal *dims_size  = size.GetSize();
-
-                                // Output Type
-                                typedef itk::Image<short,3> ImageOutputType;  
-                            typedef itk::ImageSeriesWriter<TImage, ImageOutputType >  SeriesWriterType;
-                 
-                                SeriesWriterType::Pointer seriesWriter = SeriesWriterType::New();
-                                seriesWriter->SetInput( image);
-                       
-                                // from JPR file to generate output files
-                                typedef itk::NumericSeriesFileNames NamesGeneratorType;
-                                NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New();
-                                namesGenerator->SetStartIndex(0);
-                                namesGenerator->SetEndIndex(dims_size[3]-1);
-                                namesGenerator->SetIncrementIndex(1);
-                                std::string format = i_dir;
-                            format += "/image%03d.vtk";
-                                namesGenerator->SetSeriesFormat( format.c_str() );
-      
-                                seriesWriter->SetFileNames( namesGenerator->GetFileNames() );
+                                if(image->GetImageDimension() == 4)
+                                {
+                                                       boost::any a1(image);
+                                                       m_AnyImages.push_back(a1);
+                                        // size of fourth dimension 
+                                        int dsize = image->GetLargestPossibleRegion().GetSize(3);
+                                        // Output Type
+                                        typedef itk::Image<unsigned char,3> ImageOutputType;  
+                                        typedef itk::ImageSeriesWriter<TImage, ImageOutputType >  SeriesWriterType;
+                                        typedef itk::MetaImageIO MetaImageType;
+                                        MetaImageType::Pointer metaIO;
+                                        SeriesWriterType::Pointer seriesWriter = SeriesWriterType::New();
                        
-                                try
-                               {
-                                       seriesWriter->Update();
-                                       vtkImageReader2 *vReader =vtkImageReader2::New();
-                                       for(unsigned int i = 0 ;i < dims_size[3]; i++)
+                                        // from JPR file to generate output files
+                                        typedef itk::NumericSeriesFileNames NamesGeneratorType;
+                                        NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New();
+                                        namesGenerator->SetStartIndex(0);
+                                        namesGenerator->SetEndIndex(dsize-1); 
+                                        namesGenerator->SetIncrementIndex(1);
+                                        std::string format = i_dir;
+                                        format += "/image%03d.mhd";
+                                        namesGenerator->SetSeriesFormat( format.c_str() );
+                                       
+                                        const std::vector<std::string> names = namesGenerator->GetFileNames();
+                                        seriesWriter->SetFileNames( names );
+                                        seriesWriter->SetInput(image);
+                                        seriesWriter->SetImageIO(metaIO);
+                                        try
                                        {
-                                               vReader->SetFileName(namesGenerator->GetFileNames()[i].c_str());
-                                               vReader->Update();
-                                               m_Vresults.push_back(vReader->GetOutput());
+                                               seriesWriter->Update();
+                                               vtkMetaImageReader *vReader = vtkMetaImageReader::New();
+                                               std::vector<std::string>::const_iterator it = names.begin();
+                                               for( ;it !=  names.end(); ++it)
+                                               {
+                                                       vReader->SetFileName((*it).c_str());
+                                                       vReader->Update();
+                                                       m_Vresults.push_back(vReader->GetOutput());
+                                                       
+                                               }
                                        }
-                               }
-                               catch( itk::ExceptionObject & excp )
-                               {
-                                       std::cerr << "Exception thrown while writing the series " << std::endl;
-                                       std::cerr << excp << std::endl;
-                                       //return EXIT_FAILURE;
-                               }
-                               
-                 }
+                                       catch( itk::ExceptionObject & excp )
+                                       {
+                                               std::cerr << "Exception thrown while writing the series " << std::endl;
+                                               std::cerr << excp << std::endl;
+                                               //return EXIT_FAILURE;
+                                       }
+                                }
+                         }
 
          }
 
  }// namespace end
 
+#endif //__creaImageIOWxSimpleDlg_hpp_INCLUDED__
+