]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/ThresholdImageView/ColorLayerImageViewPanel.cxx
cad61bd9c6b7382f286c5db306b61b9f288bcddb
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / ThresholdImageView / ColorLayerImageViewPanel.cxx
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
8 #
9 #  This software is governed by the CeCILL-B license under French law and
10 #  abiding by the rules of distribution of free software. You can  use,
11 #  modify and/ or redistribute the software under the terms of the CeCILL-B
12 #  license as circulated by CEA, CNRS and INRIA at the following URL
13 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 #  or in the file LICENSE.txt.
15 #
16 #  As a counterpart to the access to the source code and  rights to copy,
17 #  modify and redistribute granted by the license, users are provided only
18 #  with a limited warranty  and the software's author,  the holder of the
19 #  economic rights,  and the successive licensors  have only  limited
20 #  liability.
21 #
22 #  The fact that you are presently reading this means that you have had
23 #  knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
25
26 /**
27  *  \file
28  *  \brief ClassThresholdImageViewPanel .
29  */
30
31
32 #include "ColorLayerImageViewPanel.h"
33 #include "OpenImageDialog.h"
34
35
36 //---------------------------------------------------------
37 //---------------------------------------------------------
38 //---------------------------------------------------------
39
40 wxDlgTransformByDimensionBySpacingByPixel::wxDlgTransformByDimensionBySpacingByPixel()
41 {
42 }
43
44 wxDlgTransformByDimensionBySpacingByPixel::~wxDlgTransformByDimensionBySpacingByPixel()
45 {
46 }
47
48
49 void wxDlgTransformByDimensionBySpacingByPixel::GetTransformType(wxWindow *parent , wxString message,int &typeOfTransformation,bool &dlgWxOK)
50 {
51         wxDialog* dial                          = new wxDialog (parent,-1,_T("Tools"),wxDefaultPosition, wxSize(590,190));
52         wxSizer* buttonsSizer   = dial->CreateSeparatedButtonSizer(wxOK|wxCANCEL);
53         wxBoxSizer *dialSizer   = new wxBoxSizer(wxVERTICAL);
54
55         wxString lstOptOperation[3];
56         lstOptOperation[0]=_T("By Dimension");
57         lstOptOperation[1]=_T("By Spacing");
58         lstOptOperation[2]=_T("By Pixel");
59         wxRadioBox * radioOpts = new wxRadioBox(dial, -1, _T("Type of Transformation"), wxDefaultPosition, wxSize(270,45), 3 , lstOptOperation,  3, wxRA_SPECIFY_COLS);
60
61         dialSizer->Add( new wxStaticText(dial,-1,message)  , 0, wxGROW );
62         dialSizer->Add( new wxStaticText(dial,-1,_T(" "))  , 0, wxGROW );
63         dialSizer->Add(radioOpts,0,wxGROW);
64         dialSizer->Add( new wxStaticText(dial,-1,_T(" "))  , 0, wxGROW );
65         dialSizer->Add(buttonsSizer,0,wxGROW);
66         dial->SetSizer(dialSizer, true);
67         dial->Layout();
68         dial->ShowModal();
69
70         typeOfTransformation    = -1;
71         dlgWxOK                 = false;
72
73         if (dial->GetReturnCode() == wxID_OK)
74         {
75                 typeOfTransformation    = radioOpts->GetSelection() + 1;
76                 dlgWxOK                 = true;
77         }
78 }
79
80
81 //---------------------------------------------------------
82 //---------------------------------------------------------
83 //---------------------------------------------------------
84
85
86 /**
87  ** Begin of the threshold panel
88  **/
89 ColorLayerImageViewPanel::ColorLayerImageViewPanel(wxWindow* parent, int min, int max, int type)
90 //: wxPanel(parent, -1, wxDefaultPosition, wxSize(600,100), wxBORDER_SUNKEN)
91
92 : wxPanel(parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL)
93
94
95 {
96 printf("EED ColorLayerImageViewPanel::ColorLayerImageViewPanel Start\n");
97
98         _spcBase[0] = _spcBase[1] = _spcBase[2] = 0;
99         _dimBase[0] = _dimBase[1] = _dimBase[2] = 0;
100
101         _cb_ShowHide                    = NULL;
102         _sl_SliceImageX                 = NULL;
103         _sl_SliceImageY                 = NULL;
104         _sl_SliceImageZ                 = NULL;
105         _histogram                              = NULL;
106         _histogramMinMaxLevel   = NULL;
107
108         wxFlexGridSizer * sizer         = new wxFlexGridSizer(1);
109         _colorLayerImageViewManager     = new ColorLayerImageViewManager();
110
111         if (type==1)
112         {
113                 _btn_ReadImage                                  = new wxButton(this, wxID_ANY, _T("Read Image") );
114                 _thresholdGo                                    = true;
115                 _cb_ShowHide                                    = new wxCheckBox(this, wxID_ANY, _T("Show/Hide Layer") );
116                 _cb_ShowHide->SetValue(_thresholdGo);
117                 _cb_SliceFixDinamic                     = new wxCheckBox(this, wxID_ANY, _T("Fixed/Dynamic Slice") );
118                 _cb_SliceFixDinamic->SetValue(true);
119                 _sl_SliceImageX = new wxSlider(this, wxID_ANY, 0, 1000, 1000, 
120                                                                                         wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
121                 _sl_SliceImageY = new wxSlider(this, wxID_ANY, 0, 1000, 1000, 
122                                                                                         wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
123                 _sl_SliceImageZ = new wxSlider(this, wxID_ANY, 0, 1000, 1000, 
124                                                                                         wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
125                 _sl_SliceImageX->Enable(false);
126                 _sl_SliceImageY->Enable(false);
127                 _sl_SliceImageZ->Enable(false);
128                 _interpolationCheckBox = new wxCheckBox(this, -1, _T("Image Interpolation") );
129                 _interpolationCheckBox->SetValue(true);
130                 _opacity = new wxSlider(this, wxID_ANY, 6, 1, 10, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
131
132         //      _histogram                                      = new Histogram(  this , -1, wxPoint(0,0), wxSize(400,400), /*wxNO_BORDER*/ wxBORDER_DEFAULT ); 
133         //      _histogramMinMaxLevel   = new HistogramMinMaxLevel( (HistogramBase*)_histogram );
134
135                 Connect( _btn_ReadImage->GetId(), wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &ColorLayerImageViewPanel::onReadImage );
136                 Connect( _cb_ShowHide->GetId(), wxEVT_COMMAND_CHECKBOX_CLICKED  , (wxObjectEventFunction) &ColorLayerImageViewPanel::onThresholdShow );
137                 Connect( _interpolationCheckBox->GetId(), wxEVT_COMMAND_CHECKBOX_CLICKED, (wxObjectEventFunction) &ColorLayerImageViewPanel::onThresholdInterpolation );
138                 Connect( _opacity->GetId(), wxEVT_SCROLL_THUMBTRACK, (wxObjectEventFunction) &ColorLayerImageViewPanel::onChangeOpacity );
139                 Connect( _cb_SliceFixDinamic->GetId(), wxEVT_COMMAND_CHECKBOX_CLICKED, (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceFixDinamic );
140
141                 Connect( _sl_SliceImageX->GetId(), wxEVT_SCROLL_THUMBTRACK      , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
142                 Connect( _sl_SliceImageX->GetId(), wxEVT_SCROLL_LINEUP          , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
143                 Connect( _sl_SliceImageX->GetId(), wxEVT_SCROLL_LINEDOWN        , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
144                 Connect( _sl_SliceImageX->GetId(), wxEVT_SCROLL_PAGEUP          , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
145                 Connect( _sl_SliceImageX->GetId(), wxEVT_SCROLL_PAGEDOWN        , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
146
147                 Connect( _sl_SliceImageY->GetId(), wxEVT_SCROLL_THUMBTRACK      , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
148                 Connect( _sl_SliceImageY->GetId(), wxEVT_SCROLL_LINEUP          , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
149                 Connect( _sl_SliceImageY->GetId(), wxEVT_SCROLL_LINEDOWN        , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
150                 Connect( _sl_SliceImageY->GetId(), wxEVT_SCROLL_PAGEUP          , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
151                 Connect( _sl_SliceImageY->GetId(), wxEVT_SCROLL_PAGEDOWN        , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
152
153                 Connect( _sl_SliceImageZ->GetId(), wxEVT_SCROLL_THUMBTRACK      , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
154                 Connect( _sl_SliceImageZ->GetId(), wxEVT_SCROLL_LINEUP          , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
155                 Connect( _sl_SliceImageZ->GetId(), wxEVT_SCROLL_LINEDOWN        , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
156                 Connect( _sl_SliceImageZ->GetId(), wxEVT_SCROLL_PAGEUP          , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
157                 Connect( _sl_SliceImageZ->GetId(), wxEVT_SCROLL_PAGEDOWN        , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
158
159                 sizer -> Add( new wxStaticText(this,-1,_T("Color Image Layer"))                 , 1, wxGROW );
160                 sizer -> Add( _btn_ReadImage                                                                                    , 1, wxGROW );
161                 sizer -> Add( new wxStaticText(this,-1,_T(" "))                                                 , 1, wxGROW );
162                 sizer -> Add( new wxStaticText(this,-1,_T("Opacity Level"))                     , 1, wxGROW );
163                 sizer -> Add( _opacity                                                                                                  , 1, wxGROW );
164                 sizer -> Add( new wxStaticText(this,-1,_T(" "))                                                 , 1, wxGROW );
165                 sizer -> Add( _cb_ShowHide                                                                                              , 1, wxGROW );
166                 sizer -> Add( new wxStaticText(this,-1,_T(" "))                                                 , 1, wxGROW );
167                 sizer -> Add( _interpolationCheckBox                                                                    , 1, wxGROW );
168
169                 sizer -> Add( new wxStaticText(this,-1,_T(" "))                                                 , 1, wxGROW );
170                 sizer -> Add( new wxStaticText(this,-1,_T("Slice "))                                    , 1, wxGROW );
171                 sizer -> Add( _sl_SliceImageX                                                                                   , 1, wxGROW );
172                 sizer -> Add( _sl_SliceImageY                                                                                   , 1, wxGROW );
173                 sizer -> Add( _sl_SliceImageZ                                                                                   , 1, wxGROW );
174                 sizer -> Add( _cb_SliceFixDinamic                                                                               , 1, wxGROW );
175         //      sizer -> Add( _histogramMinMaxLevel->GetWindow()                                                , 1, wxGROW );
176
177         } // type==1
178
179
180         if (type==2)
181         {
182                 _opacity = new wxSlider(this, wxID_ANY, 6, 1, 10, wxDefaultPosition, wxSize(2,40), wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
183                 Connect( _opacity->GetId(), wxEVT_SCROLL_THUMBTRACK, (wxObjectEventFunction) &ColorLayerImageViewPanel::onChangeOpacity );
184 //              sizer -> Add( new wxStaticText(this,-1,_T("Opacity Level"))                     , 1, wxEXPAND );
185 //              sizer -> Add( _opacity, 1, wxGROW );
186 //              sizer -> Add( new wxStaticText(this,-1,_T("Opacity Level"))  );
187                 sizer -> Add( _opacity ,1,wxGROW  );
188                 sizer->AddGrowableCol(0);
189
190         } // type==2
191
192
193         this->SetSizer( sizer );
194 //      this->SetAutoLayout( true );
195 //      this->Layout();
196      
197         //CM Sets the default fitting mode to Pixel mode.
198         _fitting_mode = 3;
199         // EO CM  EED
200 }
201
202 //----------------------------------------------------------------------------
203 ColorLayerImageViewPanel::~ColorLayerImageViewPanel()
204 {
205 }
206
207
208 //----------------------------------------------------------------------------
209 ColorLayerImageViewManager* ColorLayerImageViewPanel::GetColorLayerImageViewManager()
210 {
211    return _colorLayerImageViewManager;
212 }
213
214
215 //----------------------------------------------------------------------------
216 // This is the new spacing of the background image  (1,1,1)
217 void ColorLayerImageViewPanel::SetBaseSpacing(double spc[3])
218 {
219         _spcBase[0] = spc[0];
220         _spcBase[1] = spc[1];
221         _spcBase[2] = spc[2];
222
223         _spcBase[0] = 1;
224         _spcBase[1] = 1;
225         _spcBase[2] = 1;
226 }
227
228 //----------------------------------------------------------------------------
229 // This is the Original spacing of the background image
230 void ColorLayerImageViewPanel::SetOriginalSpacing(double spc[3])
231 {
232         _spcOrg[0] = spc[0];
233         _spcOrg[1] = spc[1];
234         _spcOrg[2] = spc[2];
235 }
236
237 //----------------------------------------------------------------------------
238 // This is the size in pixels of the background image
239 void ColorLayerImageViewPanel::SetBaseDimension(int dim[3])
240 {
241         _dimBase[0] = dim[0];
242         _dimBase[1] = dim[1];
243         _dimBase[2] = dim[2];
244 }
245
246 //----------------------------------------------------------------------------
247 void ColorLayerImageViewPanel::SetImage(vtkImageData *img)
248 {
249     if (img!=NULL)
250     {
251                 if (_histogramMinMaxLevel!=NULL) 
252                 {
253                         _histogramMinMaxLevel->Configure( img );
254                 }
255         _colorLayerImageViewManager->SetImage( img );
256
257         double spc[3];
258         img->GetSpacing(spc);
259
260         int dim[3];
261         int ext[6];
262         img->GetWholeExtent(ext);
263         dim[0] = ext[1]-ext[0];
264         dim[1] = ext[3]-ext[2];
265         dim[2] = ext[5]-ext[4];
266
267
268                 if (_sl_SliceImageX!=NULL)
269                 {
270                     _sl_SliceImageX->SetRange( 0 , dim[0] );
271                     _sl_SliceImageY->SetRange( 0 , dim[1] );
272                     _sl_SliceImageZ->SetRange( 0 , dim[2] );
273                 }
274
275         if ( (spc[0]!=_spcBase[0]) || (spc[1]!=_spcBase[1]) || (spc[2]!=_spcBase[2]) ||
276              (dim[0]!=_dimBase[0]) || (spc[1]!=_spcBase[1]) || (spc[2]!=_spcBase[2])
277             )
278         {
279           // CM
280           int typeOfTransformation;     
281           // In case of the option is set to create a dialog box.
282           if (_fitting_mode == -1)
283             {
284               bool transformOkDlg;
285               wxString msg = _T("The image resolution of both images are not compatible. Do you want to tranform it? ");
286               wxDlgTransformByDimensionBySpacingByPixel dlg;
287               dlg.GetTransformType(this,msg,typeOfTransformation,transformOkDlg);
288               // CM Reset the default value (Pixel i.e. 3) if the dialog box returned an inappropriate value.
289               if (transformOkDlg == false)
290                 typeOfTransformation = 3;
291             }
292           else
293             typeOfTransformation = _fitting_mode;
294           //EO CM
295             if (typeOfTransformation==1)  // make dimensions equals
296             {
297                 printf("EED ColorLayerImageViewPanel::onReadImage ...WARNNING.... dif size image spc*dim ofnew image ...???\n");
298                 spc[0]=_spcBase[0]*_dimBase[0]/dim[0];
299                 spc[1]=_spcBase[1]*_dimBase[1]/dim[1];
300                 spc[2] = 1;
301             }
302
303             if (typeOfTransformation==2)  // make spacing equals
304             {
305                 spc[0] = ( _spcBase[0]/spc[0] ) * _spcOrg[0];
306                 spc[1] = ( _spcBase[1]/spc[1] ) * _spcOrg[1];
307                 spc[2] = ( _spcBase[2]/spc[2] ) * _spcOrg[2];
308             }
309
310             if (typeOfTransformation==3)  // make spacing = 1
311             {
312                 spc[0] = 1;
313                 spc[1] = 1;
314                 spc[2] = 1;
315             }
316
317             img->SetSpacing(spc);
318         } // spc !_spcBase   dim!__dimBase
319     } // _firsttime_mode
320
321         if (_active==true)
322         {
323             _colorLayerImageViewManager->onThreshold();
324                 RefreshView();
325         } // _ative 
326
327 }
328
329
330 void ColorLayerImageViewPanel::RefreshView()
331 {
332 //EED01         _colorLayerImageView->Refresh();
333         _colorLayerImageViewManager->Refresh();
334 }
335
336 //----------------------------------------------------------------------------
337 void ColorLayerImageViewPanel::SetFittingMode(int fitting_mode)
338 {
339   // CM Sets the fitting mode if an appropriate value is provided.
340   if (fitting_mode == -1 || fitting_mode == 1 || fitting_mode == 2 || fitting_mode == 3)
341     {
342       _fitting_mode = fitting_mode;
343     }
344   //Otherwise, an exception should be thrown (value 0 is fine, it corresponds to an empty field i.e. the default value is kept).
345   else if (fitting_mode != 0)
346     std::cout << "CM ERROR!!! The fitting mode provided has an inappropriate value. It should be an int between -1 ; 1 ; 2 ; 3, but its value is :" << fitting_mode << ". The default value will be set instead, i.e. Pixel transformation (3)." <<  std::endl;
347   //EO CM
348 }
349
350 //----------------------------------------------------------------------------
351 int ColorLayerImageViewPanel::GetFittingMode()
352 {
353   return _fitting_mode;
354 }
355
356 //----------------------------------------------------------------------------
357 void ColorLayerImageViewPanel::onReadImage(wxCommandEvent& event)
358 {
359 // EED ???
360 //      creaImageIO::WxSimpleDlg w(0,_T("Select your image"),"creaContours_Descriptor.dscp","creatisContours DB");
361 //      w.ShowModal();
362         printf("EED ColorLayerImageViewPanel::onReadImage ....WARNING... Read dlg. all images creaImageIO ... ???\n");
363
364         creaMaracasVisuKernel::OpenImageDialog diag = creaMaracasVisuKernel::OpenImageDialog();
365         SetImage( diag.getImageData() );
366 }
367
368 //----------------------------------------------------------------------------
369 void ColorLayerImageViewPanel::onThresholdChange(wxCommandEvent& event)
370 {
371                 if (_thresholdGo)
372                 {
373                  _colorLayerImageViewManager->onThresholdChange();
374                  RefreshView();
375                 //std::cout<< "Valor Min: " << minVal << " & Valor Max: " << maxVal  << std::endl;
376           } // _thresholdGo
377 }
378
379 //----------------------------------------------------------------------------
380 void ColorLayerImageViewPanel::onThresholdShow(wxCommandEvent& event)
381 {
382         _thresholdGo = _cb_ShowHide->GetValue();
383                         _active = _thresholdGo;
384         if (_thresholdGo)
385         {
386                     _colorLayerImageViewManager->onThreshold();
387         }else{
388                     _colorLayerImageViewManager->onThresholdRemove( );
389         }
390              RefreshView();
391 }
392
393 //----------------------------------------------------------------------------
394 void ColorLayerImageViewPanel::onThresholdStop()
395 {
396                 if (_thresholdGo)
397                 {
398                     _colorLayerImageViewManager->onThresholdRemove( );
399                     _thresholdGo=false;
400                 }
401 }
402
403 //----------------------------------------------------------------------------
404 void ColorLayerImageViewPanel::onThresholdInterpolation(wxCommandEvent& event)
405 {
406            _colorLayerImageViewManager->onThresholdInterpolation(_interpolationCheckBox->GetValue());
407                 RefreshView();
408 }
409
410 //----------------------------------------------------------------------------
411 void ColorLayerImageViewPanel::onSliceFixDinamic(wxCommandEvent& event)
412 {
413         bool fixdin = _cb_SliceFixDinamic->GetValue();
414         _colorLayerImageViewManager->SetSliceFixDynamic( fixdin );
415         if (fixdin==false)
416         {
417             _colorLayerImageViewManager->SetX2( _sl_SliceImageX->GetValue() );
418             _colorLayerImageViewManager->SetY2( _sl_SliceImageY->GetValue() );
419             _colorLayerImageViewManager->SetZ2( _sl_SliceImageZ->GetValue() );
420         }
421         _sl_SliceImageX->Enable(!fixdin);
422         _sl_SliceImageY->Enable(!fixdin);
423         _sl_SliceImageZ->Enable(!fixdin);
424         _colorLayerImageViewManager->onThreshold();
425         RefreshView();
426 }
427
428 //----------------------------------------------------------------------------
429 void ColorLayerImageViewPanel::onSliceImage(wxScrollEvent& event)
430 {
431    _colorLayerImageViewManager->SetX2( _sl_SliceImageX->GetValue() );
432    _colorLayerImageViewManager->SetY2( _sl_SliceImageY->GetValue() );
433    _colorLayerImageViewManager->SetZ2( _sl_SliceImageZ->GetValue() );
434    _colorLayerImageViewManager->onThreshold();
435         RefreshView();
436 }
437
438 //----------------------------------------------------------------------------
439 void ColorLayerImageViewPanel::onChangeOpacity(wxScrollEvent& event)
440 {
441         int opacity = _opacity->GetValue();
442         _colorLayerImageViewManager->onThresholdChangeOpacity(opacity);
443         RefreshView();
444 }
445
446
447 //----------------------------------------------------------------------------
448 bool ColorLayerImageViewPanel::IsVisible()
449 {
450         return _thresholdGo;
451 }
452
453 //----------------------------------------------------------------------------
454 void ColorLayerImageViewPanel::SetActive(bool active)
455 {
456         _active=active;
457         if (_cb_ShowHide!=NULL)
458         {
459                 _cb_ShowHide->SetValue( _active );
460         }
461 }
462
463
464 // EOF
465