]> Creatis software - creaImageIO.git/blob - src/creaImageIOWxSimpleDlg.cpp
#3208 creaImageIO Feature New Normal - branch mingw64
[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) CREATIS-LRMN 2008"),
144                    wxDefaultPosition,
145                    wxSize(810,750),
146                    GIMMICK_2D_IMAGE_SELECTION,
147                    GIMMICK_3D_IMAGE_SELECTION,
148                    _3D,
149                                    1);
150                  dlg.ShowModal();
151                  if (dlg.GetReturnCode() == wxID_OK)
152              {
153 //EED1                          dlg.stopReading();
154
155                                 std::vector<creaImageIO::OutStrGimmick> outStrGimmick;
156                                 dlg.getSelected(outStrGimmick, m_attrDicomTags,true,"");
157
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 printf("EED1 WxSimpleDlg::OnReadGimmick %d \n", ii);
166                                         m_results.push_back(outStrGimmick[ii].img);
167                                         m_resultsDicomAtr.push_back( outStrGimmick[ii].infos );
168                                 }
169 //                      } else {
170 //                              for (ii=0;ii<size;ii++)
171 //                              {
172 //                                      m_resultsInfo.push_back(outStrGimmick[ii]);
173 //                              }
174 //                      }
175
176                         infoimage               =       _T("EED Missing info <void> ???");
177                         infoimageSource =       _T("GIMMICK");
178
179                         dlg.OnExit();
180                  }
181                  SetReturnCode( dlg.GetReturnCode() );
182                  EndModal( dlg.GetReturnCode() );
183            }
184
185 //-----------------------------------------------------------------------------   
186         wxString WxSimpleDlg::getInfoImage()
187         {
188                 return infoimage;
189         }
190
191 //-----------------------------------------------------------------------------   
192         wxString WxSimpleDlg::getInfoImageSource()
193         {
194                 return infoimageSource;
195         }
196
197  //////////////////////////////////////////////////////////////////////
198 // Return the results vector                                                                            //
199 //////////////////////////////////////////////////////////////////////
200           std::vector<vtkImageData*> WxSimpleDlg::getImagesSelected()
201           {
202                   return m_results;
203           }
204
205 //-----------------------------------------------------------------------------   
206         std::vector< std::map<std::string, std::string> >  WxSimpleDlg::getDicomInfoImagesSelected()
207         {
208                 return m_resultsDicomAtr;
209         }
210
211 //-----------------------------------------------------------------------------   
212         void WxSimpleDlg::SetAttrDicomTags( std::vector<std::string> attr )
213         {
214                 m_attrDicomTags=attr;
215         }
216           
217
218 //-----------------------------------------------------------------------------   
219         vtkImageData*  WxSimpleDlg::getVolumeSelected()
220         {
221                 vtkImageData *result;
222
223 //        creaImageIO::WxSimpleDlg dlg(0,crea::std2wx(bbGetInputTitle()),"localdatabase_Descriptor.dscp","Local Database"); 
224 //              dlg.SetAttrDicomTags( bbGetInputDicomTags() );
225 //              dlg.ShowModal(); 
226 //              bbSetOutputDicomInfo( dlg.getDicomInfoImagesSelected() );
227
228                 if (getImagesSelected().size()!=0){ 
229 //                bbSetOutputFileName( std::string( dlg.getInfoImage().mb_str() ) );
230                   if(getImagesSelected().size() ==1)
231                   {
232                         result = getImagesSelected()[0] ;  
233                   } else {
234                                 // FCY: it will be a big problem if we have several kind of data in the same folder.
235                                 // creation of a huge vtkImageData!!!!
236                                 vtkImageData* first = getImagesSelected()[0];
237                                 int dim[3];
238                                 first->GetDimensions(dim);
239                                 if (dim[2]==1)
240                                 {
241                                         vtkImageData *out = vtkImageData::New();
242                                         int ext[6];
243 //EED 2017-01-01 Migration VTK7
244 #if VTK_MAJOR_VERSION <= 5
245                                         first->GetWholeExtent(ext); 
246 #else
247                                         first->GetExtent(ext); 
248 #endif
249                                         if(ext[5] == 0)
250                                         {
251                                            ext[5] = getImagesSelected().size()-1;
252                                          } else {
253                                                 ext[5] = ext[5] * getImagesSelected().size()-1; // to deal with multiframes 
254                                         } // ext
255                                         out->SetExtent(ext);
256                                         double spac[3];
257                                         first->GetDimensions(dim);
258                                         first->GetSpacing(spac);
259                                         if (spac[0]==spac[1])
260                                         {
261                                                 spac[2]=spac[0];  
262                                 }
263                                         out->SetSpacing(spac);
264                                         out->SetDimensions(dim[0], dim[1], getImagesSelected().size() );
265 //EED 2017-01-01 Migration VTK7
266 #if VTK_MAJOR_VERSION <= 5
267
268                                         out->SetScalarType(first->GetScalarType());
269                                         out->SetNumberOfScalarComponents(first->GetNumberOfScalarComponents());
270                                         out->AllocateScalars();
271                                         out->Update();
272 #else
273                                         out->AllocateScalars(first->GetScalarType(),first->GetNumberOfScalarComponents());
274 #endif
275
276
277                                         unsigned long imsize = dim[0] * dim[1];
278                                         imsize = imsize * dim[2];  // deal with multiframes here
279                                         // differents formats char , short, etc...
280                                         // differents components 1..3  ex. jpg ->RGB 3
281                                         imsize = imsize * first->GetScalarSize() * first->GetNumberOfScalarComponents();
282                                 int slice,sizeImageVector=getImagesSelected().size();
283                                         for (slice=0 ; slice<sizeImageVector ; slice++)
284                                         {
285                                                 vtkImageData *img = getImagesSelected()[slice];
286                                                 memcpy(out->GetScalarPointer(0,0,slice), img->GetScalarPointer(0,0,0), imsize);
287                                                 //img->Delete();
288                                         } // for slice  
289
290 //EED This is NOT working. The iteration over the pointer vtkImageData
291 //                              int slice=0;
292 //                              std::vector<vtkImageData*>::iterator it;
293 //                              for (it=dlg.getImagesSelected().begin(); it!=dlg.getImagesSelected().end(); ++it) 
294 //                                      {
295 //                                                memcpy(out->GetScalarPointer(0,0,slice), (*it)->GetScalarPointer(0,0,0), imsize);
296 //                                                slice++;
297 //                                      }       
298 //                              //for (it=dlg.getImagesSelected().begin(); it!=dlg.getImagesSelected().end(); ++it) 
299 //                                      //{
300 //                                      //              (*it)->Delete();
301 //                                      //}
302
303                                         result =out;
304
305                                 } else {
306                                         result = first;
307                                 } // dim
308                         } // if getImagesSelected size
309                 } else { 
310                 result = NULL;  
311                 } // getImagesSelected().size()!=0
312
313
314                 return result;
315
316                 }
317
318
319
320 } // namespace creaImageIO
321