]> Creatis software - creaImageIO.git/blob - src/creaImageIOWxSimpleDlg.cpp
9ae6791c48192056357ea80f928e56a41d7ecb0c
[creaImageIO.git] / src / creaImageIOWxSimpleDlg.cpp
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 #include "creaImageIOWxSimpleDlg.h"
29
30
31 namespace creaImageIO
32 {
33
34
35         ///Ctor
36    WxSimpleDlg::WxSimpleDlg(wxWindow *parent, 
37                             wxString i_title,  
38                             const std::string i_namedescp , 
39                             const std::string i_namedb)
40     : wxDialog(parent, -1,_T("SELECT IMAGE(S)"), wxDefaultPosition, wxSize(230,150))
41    {
42                 namedescp       = i_namedescp; 
43                 namedb          = i_namedb;
44
45            if(!i_title.empty())
46            {
47                         this->SetTitle(i_title);  
48            }
49            // Button to select file(s)
50            wxButton *fileBut = new wxButton(this, -1,_T("Select a single file"), wxPoint(10,7) );
51            Connect( fileBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxSimpleDlg::OnReadFile ); 
52
53            // Button to select directory
54            wxButton *directoryBut = new wxButton(this, -1,_T("Select all the files in a directory"), wxPoint(10,40) );
55            Connect( directoryBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxSimpleDlg::OnReadDirectory ); 
56
57            // button to select creaImageIO
58            wxButton *gimmickBut = new wxButton(this, -1,_T("Select thru Dicom browser"), wxPoint(10,70) );
59            Connect( gimmickBut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxSimpleDlg::OnReadGimmick ); 
60
61            /// \TODO  Button to select Bruker directory
62            bInfo = false;
63         Layout(); 
64         
65         }
66 //////////////////////////////////////////////////////////////////////
67 //                                                                  //
68 //////////////////////////////////////////////////////////////////////
69           void WxSimpleDlg::OnReadFile(wxCommandEvent& event)
70           {
71                   int resultShowModal;
72 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
73 #if wxMAJOR_VERSION <= 2
74                   wxFileDialog* fileDlg = new wxFileDialog( 0,  _T("Select file"), _T(""), _T(""), crea::std2wx("*"), wxOPEN |wxFD_MULTIPLE, wxDefaultPosition);
75 #else
76                   wxFileDialog* fileDlg = new wxFileDialog( 0,  _T("Select file"), _T(""), _T(""), crea::std2wx("*"), wxFD_OPEN |wxFD_MULTIPLE, wxDefaultPosition);
77 #endif
78     
79                     resultShowModal = fileDlg->ShowModal();
80                         if ( resultShowModal==wxID_OK )
81                         {
82                                 wxArrayString wxArray;
83                                 fileDlg->GetPaths(wxArray);
84                                 if(wxArray.size() >0)
85                                 {
86                                         m_results.clear();
87                                         infoimage               =       wxArray[0];
88                                         infoimageSource =       _T("FILE(s)");
89                                         std::vector<std::string> files;
90                                         for( int i = 0; i < wxArray.GetCount(); i++)
91                                         {
92                                                 files.push_back( crea::wx2std(wxArray[i]));
93                                         }
94                                         if( !m_view.readFile(files , m_attrDicomTags , m_resultsDicomAtr , m_results) )
95                                         {
96                                                 //TO DO WARNING MESSAGE;
97                                         }
98                                 } else {
99                                         // TO DO WARNING MESSAGES
100                                 }
101                         }
102                   SetReturnCode( resultShowModal );
103 //               Close();
104                  EndModal( resultShowModal );
105           }
106
107  //////////////////////////////////////////////////////////////////////
108 //                                                                                                                                      //
109 //////////////////////////////////////////////////////////////////////
110
111           void WxSimpleDlg::OnReadDirectory(wxCommandEvent &event)
112           {
113                   int resultShowModal;
114              long style = wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST;
115                  wxDirDialog* dirDlg = new wxDirDialog( 0, _T("Select the directory to display"), _T(""), style);
116                  
117                  resultShowModal = dirDlg->ShowModal();
118                  if ( resultShowModal==wxID_OK )
119                  {  
120                          m_results.clear();
121                         infoimage               =       dirDlg->GetPath();
122                         infoimageSource =       _T("DIRECTORY");
123                         if(! m_view.readDirectory(crea::wx2std(dirDlg->GetPath()) , m_attrDicomTags , m_resultsDicomAtr , m_results))
124                         {
125                                 //TO DO WARNING MESSAGE;
126                         }
127                  }
128                  SetReturnCode( resultShowModal );
129 //               Close();
130                  EndModal( resultShowModal );
131           }
132
133           //////////////////////////////////////////////////////////////////////
134 //                                                                                                                                      //
135 //////////////////////////////////////////////////////////////////////
136
137           void WxSimpleDlg::OnReadGimmick(wxCommandEvent &event)
138           {
139                   // Run Gimmick
140                    WxGimmickReaderDialog dlg(0,-1, 
141                                    namedescp,
142                                    namedb, 
143                                    _T("Select image(s)        - Gimmick! (c)"),
144                    wxDefaultPosition,
145                    wxSize(810,750),
146                    GIMMICK_2D_IMAGE_SELECTION,
147                    GIMMICK_3D_IMAGE_SELECTION,
148                    _3D,
149                                    0);
150                  dlg.ShowModal();
151                  if (dlg.GetReturnCode() == wxID_OK)
152              {
153                         dlg.stopReading();
154                         std::vector<creaImageIO::OutStrGimmick> outStrGimmick;
155 printf("EED WxSimpleDlg::OnReadGimmick 1 \n");
156                         dlg.getSelected(outStrGimmick, m_attrDicomTags,true,"");
157 printf("EED WxSimpleDlg::OnReadGimmick 2 \n");
158                         m_results.clear();
159                         int size=(int)outStrGimmick.size();
160                         int ii;
161 //                      if(!bInfo)
162 //                      {
163                                 for (ii=0;ii<size;ii++)
164                                 {
165                                         m_results.push_back(outStrGimmick[ii].img);
166                                         m_resultsDicomAtr.push_back( outStrGimmick[ii].infos );
167                                 }
168 //                      } else {
169 //                              for (ii=0;ii<size;ii++)
170 //                              {
171 //                                      m_resultsInfo.push_back(outStrGimmick[ii]);
172 //                              }
173 //                      }
174
175                         infoimage               =       _T("EED Missing info <void> ???");
176                         infoimageSource =       _T("GIMMICK");
177                         dlg.OnExit();
178                  }
179                  SetReturnCode( dlg.GetReturnCode() );
180                  EndModal( dlg.GetReturnCode() );
181            }
182
183 //-----------------------------------------------------------------------------   
184         wxString WxSimpleDlg::getInfoImage()
185         {
186                 return infoimage;
187         }
188
189 //-----------------------------------------------------------------------------   
190         wxString WxSimpleDlg::getInfoImageSource()
191         {
192                 return infoimageSource;
193         }
194
195  //////////////////////////////////////////////////////////////////////
196 // Return the results vector                                                                            //
197 //////////////////////////////////////////////////////////////////////
198           std::vector<vtkImageData*> WxSimpleDlg::getImagesSelected()
199           {
200                   return m_results;
201           }
202
203 //-----------------------------------------------------------------------------   
204         std::vector< std::map<std::string, std::string> >  WxSimpleDlg::getDicomInfoImagesSelected()
205         {
206                 return m_resultsDicomAtr;
207         }
208
209 //-----------------------------------------------------------------------------   
210         void WxSimpleDlg::SetAttrDicomTags( std::vector<std::string> attr )
211         {
212                 m_attrDicomTags=attr;
213         }
214           
215
216 //-----------------------------------------------------------------------------   
217         vtkImageData*  WxSimpleDlg::getVolumeSelected()
218         {
219                 vtkImageData *result;
220
221 //        creaImageIO::WxSimpleDlg dlg(0,crea::std2wx(bbGetInputTitle()),"localdatabase_Descriptor.dscp","Local Database"); 
222 //              dlg.SetAttrDicomTags( bbGetInputDicomTags() );
223 //              dlg.ShowModal(); 
224 //              bbSetOutputDicomInfo( dlg.getDicomInfoImagesSelected() );
225
226                 if (getImagesSelected().size()!=0){ 
227 //                bbSetOutputFileName( std::string( dlg.getInfoImage().mb_str() ) );
228                   if(getImagesSelected().size() ==1)
229                   {
230                         result = getImagesSelected()[0] ;  
231                   } else {
232                                 // FCY: it will be a big problem if we have several kind of data in the same folder.
233                                 // creation of a huge vtkImageData!!!!
234                                 vtkImageData* first = getImagesSelected()[0];
235                                 int dim[3];
236                                 first->GetDimensions(dim);
237                                 if (dim[2]==1)
238                                 {
239                                         vtkImageData *out = vtkImageData::New();
240                                         int ext[6];
241 //EED 2017-01-01 Migration VTK7
242 #if VTK_MAJOR_VERSION <= 5
243                                         first->GetWholeExtent(ext); 
244 #else
245                                         first->GetExtent(ext); 
246 #endif
247                                         if(ext[5] == 0)
248                                         {
249                                            ext[5] = getImagesSelected().size()-1;
250                                          } else {
251                                                 ext[5] = ext[5] * getImagesSelected().size()-1; // to deal with multiframes 
252                                         } // ext
253                                         out->SetExtent(ext);
254                                         double spac[3];
255                                         first->GetDimensions(dim);
256                                         first->GetSpacing(spac);
257                                         if (spac[0]==spac[1])
258                                         {
259                                                 spac[2]=spac[0];  
260                                 }
261                                         out->SetSpacing(spac);
262                                         out->SetDimensions(dim[0], dim[1], getImagesSelected().size() );
263 //EED 2017-01-01 Migration VTK7
264 #if VTK_MAJOR_VERSION <= 5
265
266                                         out->SetScalarType(first->GetScalarType());
267                                         out->SetNumberOfScalarComponents(first->GetNumberOfScalarComponents());
268                                         out->AllocateScalars();
269                                         out->Update();
270 #else
271                                         out->AllocateScalars(first->GetScalarType(),first->GetNumberOfScalarComponents());
272 #endif
273
274
275                                         unsigned long imsize = dim[0] * dim[1];
276                                         imsize = imsize * dim[2];  // deal with multiframes here
277                                         // differents formats char , short, etc...
278                                         // differents components 1..3  ex. jpg ->RGB 3
279                                         imsize = imsize * first->GetScalarSize() * first->GetNumberOfScalarComponents();
280                                 int slice,sizeImageVector=getImagesSelected().size();
281                                         for (slice=0 ; slice<sizeImageVector ; slice++)
282                                         {
283                                                 vtkImageData *img = getImagesSelected()[slice];
284                                                 memcpy(out->GetScalarPointer(0,0,slice), img->GetScalarPointer(0,0,0), imsize);
285                                                 //img->Delete();
286                                         } // for slice  
287                                         out->Modified();
288 //EED This is NOT working. The iteration over the pointer vtkImageData
289 //                              int slice=0;
290 //                              std::vector<vtkImageData*>::iterator it;
291 //                              for (it=dlg.getImagesSelected().begin(); it!=dlg.getImagesSelected().end(); ++it) 
292 //                                      {
293 //                                                memcpy(out->GetScalarPointer(0,0,slice), (*it)->GetScalarPointer(0,0,0), imsize);
294 //                                                slice++;
295 //                                      }       
296 //                              //for (it=dlg.getImagesSelected().begin(); it!=dlg.getImagesSelected().end(); ++it) 
297 //                                      //{
298 //                                      //              (*it)->Delete();
299 //                                      //}
300
301                                         result =out;
302
303                                 } else {
304                                         result = first;
305                                 } // dim
306                         } // if getImagesSelected size
307                 } else { 
308                 result = NULL;  
309                 } // getImagesSelected().size()!=0
310
311                         result->Modified();
312                 return result;
313
314                 }
315
316
317
318 } // namespace creaImageIO
319