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