]> Creatis software - creaImageIO.git/blob - src/creaImageIOWxISimpleDlg.hpp
036b1eed61eb23f76e3bf1c9f37bd9a9ea1346bf
[creaImageIO.git] / src / creaImageIOWxISimpleDlg.hpp
1 //#include "creaImageIOWxISimpleDlg.h"
2
3 #include "creaImageIOWxGimmickReaderDialog.h"
4 #include <itkAnalyzeImageIO.h>
5 #include <itkImageFileReader.h>
6 #include <itkImageSeriesReader.h>
7 #include <itkImage.h>
8 #include <itkImageSeriesWriter.h>
9 #include <itkGDCMImageIO.h>
10 #include <itkDICOMSeriesFileNames.h>
11 #include <itkNumericSeriesFileNames.h>
12 #include <itkVectorImage.h>
13 #include <itkMetaImageIO.h>
14 #include <itkOrientedImage.h>
15 #include <vtkImageReader2.h>
16 #include <vtkMetaImageReader.h>
17 #include <boost/filesystem/path.hpp>
18 #include <boost/filesystem.hpp>
19 #include <boost/utility.hpp>
20 #include <creaVtkBasicSlicer.h>
21
22 namespace creaImageIO
23 {
24   template <typename TImage2> 
25   typename TImage2::Pointer ReadImage( const std::string &fileName) 
26   { 
27     typename TImage2::Pointer image; 
28
29     typedef itk::ImageFileReader<TImage2> ReaderType; 
30     typename ReaderType::Pointer reader = ReaderType::New(); 
31     reader->SetFileName( fileName.c_str() ); 
32     try 
33       { 
34       reader->Update(); 
35       } 
36     catch( itk::ExceptionObject & err ) 
37       { 
38       std::cout << "Caught an exception reading" << fileName << ": " << std::endl; 
39       std::cout << err << " " << __FILE__ << " " << __LINE__ << std::endl; 
40       throw err; 
41       } 
42     catch(...) 
43       { 
44       std::cout << "Error while reading image " << fileName << std::endl; 
45       throw; 
46       } 
47      image = reader->GetOutput(); 
48     
49     return image; 
50   } 
51
52
53   ///Ctor
54   template <typename TImage> 
55    WxISimpleDlg<TImage>::WxISimpleDlg(wxWindow *parent, 
56                                       wxString i_title,  
57                                       const std::string i_namedescp , 
58                                       const std::string i_namedb)
59     : wxDialog(parent, -1,_T("SELECT IMAGE(S)"), wxDefaultPosition, wxSize(230,150))
60    {
61            namedescp = i_namedescp; 
62            namedb    = i_namedb;
63
64        if(!i_title.empty())
65        {
66            this->SetTitle(i_title);
67        }
68        // Button to select file(s)
69        wxButton *fileBut = new wxButton(this, -1,_T("Select a file to display"), wxPoint(10,7) );
70        Connect( fileBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxISimpleDlg::OnReadFile ); 
71
72        // Button to select directory
73        wxButton *directoryBut = new wxButton(this, -1,_T("Select a directory to display"), wxPoint(10,40) );
74        Connect( directoryBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxISimpleDlg::OnReadDirectory ); 
75
76        // button to select creaImageIO
77        wxButton *gimmickBut = new wxButton(this, -1,_T("Select Gimmick"), wxPoint(10,70) );
78        Connect( gimmickBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxISimpleDlg::OnReadGimmick ); 
79
80        /// \TODO  Button to select Bruker directory
81        m_dicom = true;
82        Layout(); 
83     
84     }
85 //////////////////////////////////////////////////////////////////////
86 //                                                                  //
87 //////////////////////////////////////////////////////////////////////
88      template <typename TImage> 
89       void WxISimpleDlg<TImage>::OnReadFile(wxCommandEvent& event)
90       {
91           int resultShowModal;
92           wxFileDialog* fileDlg = new wxFileDialog( 0,  _T("Select file"), _T(""), _T(""), crea::std2wx("*"), wxOPEN |wxFD_MULTIPLE, wxDefaultPosition);
93     
94           resultShowModal = fileDlg->ShowModal();
95           if ( resultShowModal==wxID_OK )
96             {
97                 wxArrayString wxArray;
98                 fileDlg->GetPaths(wxArray);
99                 if(wxArray.size() >0)
100                 {
101                     for( int i = 0; i < wxArray.GetCount(); i++)
102                     {
103                         readImg( crea::wx2std(wxArray[i]));
104                     }
105                 } 
106                 else {
107                     /// \TODO WARNING MESSAGES
108                 }
109             }
110          SetReturnCode( resultShowModal );
111 //       Close();
112          EndModal( resultShowModal );
113       }
114
115
116         template <typename TImage> 
117         void WxISimpleDlg<TImage>::setExts(std::vector<std::string> i_exts)
118         {
119             m_exts = i_exts;
120         }
121 //////////////////////////////////////////////////////////////////////
122 //                                                                  //
123 //////////////////////////////////////////////////////////////////////
124    template <typename TImage> 
125       void WxISimpleDlg<TImage>::OnReadDirectory(wxCommandEvent &event)
126       {
127          int resultShowModal;
128          bool bvalid = false;
129          long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
130          wxDirDialog* dirDlg = new wxDirDialog( 0, _T("Select the directory to display"), _T(""), style);
131          
132          resultShowModal = dirDlg->ShowModal();
133          if ( resultShowModal==wxID_OK )
134          {  
135             std::string path = crea::wx2std(dirDlg->GetPath());
136             typedef boost::filesystem::directory_iterator dir_it;
137             dir_it itr(path);
138             dir_it end_itr;
139             /*if (boost::filesystem::exists(path))
140             {*/
141                 for(;itr != end_itr; ++itr)
142                 {
143                        bvalid = m_exts.size() == 0? true : false;
144                        std::vector<std::string>::iterator it = m_exts.begin();
145                        std::string ext = itr->filename().substr(itr->filename().find_last_of("."));
146                        for(; it != m_exts.end(); it++)
147                        {
148                            if(ext == (*it) )
149                            {
150                                bvalid = true;
151                                break;
152                            }
153                        }
154                     if (!boost::filesystem::is_directory(itr->status()) && bvalid)
155                     {
156                         readImg(itr->string().c_str());
157                     }
158                 }
159          }
160          SetReturnCode( resultShowModal );
161 //         Close();
162          EndModal( resultShowModal );
163       }
164
165 //////////////////////////////////////////////////////////////////////
166 //                                                                  //
167 //////////////////////////////////////////////////////////////////////
168             template <typename TImage> 
169       void WxISimpleDlg<TImage>::OnReadGimmick(wxCommandEvent &event)
170       {
171           // Run Gimmick
172            WxGimmickReaderDialog dlg(0,-1, 
173                    namedescp,
174                    namedb, 
175                    _T("Select image(s)        - Gimmick! (c) CREATIS-LRMN 2008"),
176                    wxDefaultPosition,
177                    wxSize(810,750),
178                    GIMMICK_2D_IMAGE_SELECTION,
179                    GIMMICK_3D_IMAGE_SELECTION,
180                    _3D,
181                    1);
182          dlg.ShowModal();
183          if (dlg.GetReturnCode() == wxID_OK)
184          {
185             std::vector<std::string> out;
186             dlg.stopReading();
187             dlg.GetSelectedFiles(out);
188             if(m_dicom)
189             {
190                 readDicomImg( out);
191             }
192             else
193             {
194                 std::vector<std::string>::iterator ii = out.begin();
195                 for (;ii != out.end();ii++)
196                 {
197                     readImg( (*ii).c_str() );
198                 }
199             }
200             dlg.OnExit();
201          }
202          SetReturnCode( dlg.GetReturnCode() );
203 //         Close();
204          EndModal( dlg.GetReturnCode() );
205        }
206
207
208      template <typename TImage> 
209      wxString WxISimpleDlg<TImage>::getInfoImage()
210     {
211         return infoimage;
212     }
213
214
215     //////////////////////////////////////////////////////////////////////
216     // Return the results vector                                        //
217     //////////////////////////////////////////////////////////////////////
218      template <typename TImage>  
219      std::vector<typename TImage::Pointer> WxISimpleDlg<TImage>::getImagesSelected()
220       {
221           return m_Iresults;
222       }
223
224       template <typename TImage> 
225       void WxISimpleDlg<TImage>::readImg(const std::string &i_name)
226       {
227             // FCY : just to check but not needed, we hardly suppose that we load only same type and dim of images
228             // uncomment to test
229               /*typedef itk::ImageIOBase::IOComponentType ScalarPixelType;
230             itk::ImageIOBase::Pointer imageIO =  itk::ImageIOFactory::CreateImageIO(i_name.c_str(), itk::ImageIOFactory::ReadMode);
231             imageIO->SetFileName(i_name.c_str());
232             imageIO->ReadImageInformation();
233             ScalarPixelType pixelType = imageIO->GetComponentType();
234             const size_t dims =  imageIO->GetNumberOfDimensions();*/
235
236             typedef itk::ImageFileReader<TImage> ReaderType;
237             typename ReaderType::Pointer reader = ReaderType::New();
238             reader->SetFileName( i_name );
239             try
240             { 
241                 reader->Update();
242             } 
243             catch( itk::ExceptionObject & err ) 
244             { 
245                 std::cout << "Caught an exception reading" << i_name << ": " << std::endl; 
246                 std::cout << err << " " << __FILE__ << " " << __LINE__ << std::endl; 
247                 throw err; 
248             } 
249             catch(...)
250             { 
251                 std::cout << "Error while reading image " << i_name << std::endl; 
252                 throw; 
253             } 
254             m_Iresults.push_back(reader->GetOutput());
255       }
256
257
258
259       template <typename TImage> 
260       void WxISimpleDlg<TImage>::readDicomImg(const std::vector<std::string> &i_names)
261       {
262             typedef itk::GDCMImageIO GDCMType;
263             typedef itk::DICOMSeriesFileNames dicnames;
264             GDCMType::Pointer gdcmIO = GDCMType::New(); 
265             dicnames::Pointer generator = dicnames::New();
266 //          generator->SetInput(i_names);
267             typedef itk::ImageSeriesReader<TImage> ReaderType; 
268             typename ReaderType::Pointer reader = ReaderType::New(); 
269             reader->SetImageIO(gdcmIO);
270             reader->SetFileNames(i_names);
271         //    reader->SetFileName( i_name ); 
272             try 
273             { 
274                 reader->Update(); 
275             } 
276             catch( itk::ExceptionObject & err ) 
277             { 
278             //    std::cout << "Caught an exception reading" << i_name << ": " << std::endl; 
279                 std::cout << err << " " << __FILE__ << " " << __LINE__ << std::endl; 
280                 throw err; 
281             } 
282             catch(...) 
283             { 
284                 //std::cout << "Error while reading image " << i_name << std::endl; 
285                 throw; 
286             } 
287             m_Iresults.push_back(reader->GetOutput());
288       }
289
290
291
292
293       template <typename TImage> 
294       void WxISimpleDlg<TImage>::split3Din3Dvtk()
295       {
296           if ((m_Iresults).size() != 0)
297           {
298               typedef itk::ImageToVTKImageFilter<  TImage > ConnectorType;
299               ConnectorType::Pointer       connector = ConnectorType::New();
300                for(unsigned int i = 0 ;i < m_Iresults.size(); i++)
301               {
302                     connector->SetInput(m_Iresults[i]);
303                     connector->GetImporter()->SetDataScalarTypeToUnsignedChar();
304                     connector->Update();
305                     vtkImageData *im = vtkImageData::New();
306                     im->ShallowCopy(connector->GetOutput());
307                     im->Update();
308                     m_Vresults.push_back(im);
309              }
310           }
311       }
312
313
314
315       template <typename TImage> 
316       void WxISimpleDlg<TImage>::split4Din3Dvtk(const std::string &i_dir)
317       {
318             if ((m_Iresults).size() != 0)
319             {
320                 typename TImage::Pointer image = m_Iresults[0];
321             
322                  if(image->GetImageDimension() == 4)
323                  {
324                      // size of fourth dimension 
325                      int dsize = image->GetLargestPossibleRegion().GetSize(3);
326                      // Output Type
327                      typedef itk::Image<unsigned char,3> ImageOutputType;  
328                      typedef itk::ImageSeriesWriter<TImage, ImageOutputType >  SeriesWriterType;
329                      typedef itk::MetaImageIO MetaImageType;
330                      MetaImageType::Pointer metaIO;
331                      SeriesWriterType::Pointer seriesWriter = SeriesWriterType::New();
332             
333                      // from JPR file to generate output files
334                      typedef itk::NumericSeriesFileNames NamesGeneratorType;
335                      NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New();
336                      namesGenerator->SetStartIndex(0);
337                      namesGenerator->SetEndIndex(dsize-1); 
338                      namesGenerator->SetIncrementIndex(1);
339                      std::string format = i_dir;
340                      format += "/image%03d.mhd";
341                      namesGenerator->SetSeriesFormat( format.c_str() );
342                     
343                      const std::vector<std::string> names = namesGenerator->GetFileNames();
344                      seriesWriter->SetFileNames( names );
345                      seriesWriter->SetInput(image);
346                      seriesWriter->SetImageIO(metaIO);
347                      try
348                      {
349                         seriesWriter->Update();
350                         vtkMetaImageReader *vReader = vtkMetaImageReader::New();
351                         std::vector<std::string>::const_iterator it = names.begin();
352                         for( ;it !=  names.end(); ++it)
353                         {
354                             vReader->SetFileName((*it).c_str());
355                             vReader->Update();
356                             m_Vresults.push_back(vReader->GetOutput());
357                         }
358                     }
359                     catch( itk::ExceptionObject & excp )
360                     {
361                         std::cerr << "Exception thrown while writing the series " << std::endl;
362                         std::cerr << excp << std::endl;
363                         //return EXIT_FAILURE;
364                     }
365                  }
366               }
367       }
368
369  }// namespace end
370