]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/ThresholdImageView/ColorLayerImageViewPanel.cxx
histogram BUG
[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
93         _spcBase[0] = _spcBase[1] = _spcBase[2] = 0;
94         _dimBase[0] = _dimBase[1] = _dimBase[2] = 0;
95
96         _colorLayerImageViewManager     = new ColorLayerImageViewManager();
97
98         _btn_ReadImage                  = new wxButton(this, wxID_ANY, _T("Read Image") );
99         _thresholdGo                    = true;
100         _cb_ShowHide                    = new wxCheckBox(this, wxID_ANY, _T("Show/Hide Layer") );
101         _cb_ShowHide->SetValue(_thresholdGo);
102
103         _cb_SliceFixDinamic = new wxCheckBox(this, wxID_ANY, _T("Fixed/Dynamic Slice") );
104         _cb_SliceFixDinamic->SetValue(true);
105
106         _sl_SliceImageX = new wxSlider(this, wxID_ANY, 0, 1000, 1000, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
107         _sl_SliceImageY = new wxSlider(this, wxID_ANY, 0, 1000, 1000, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
108         _sl_SliceImageZ = new wxSlider(this, wxID_ANY, 0, 1000, 1000, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
109         _sl_SliceImageX->Enable(false);
110         _sl_SliceImageY->Enable(false);
111         _sl_SliceImageZ->Enable(false);
112         _interpolationCheckBox = new wxCheckBox(this, -1, _T("Image Interpolation") );
113         _interpolationCheckBox->SetValue(true);
114         _opacity = new wxSlider(this, wxID_ANY, 6, 1, 10, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
115
116         _histogram=NULL;
117         _histogramMinMaxLevel=NULL;
118
119 //      _histogram                                      = new Histogram(  this , -1, wxPoint(0,0), wxSize(400,400), /*wxNO_BORDER*/ wxBORDER_DEFAULT ); 
120 //      _histogramMinMaxLevel   = new HistogramMinMaxLevel( (HistogramBase*)_histogram );
121
122         if (type==0)
123         {
124         }
125
126         if (type==1)
127         {
128         }
129
130
131         Connect( _btn_ReadImage->GetId(), wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &ColorLayerImageViewPanel::onReadImage );
132         Connect( _cb_ShowHide->GetId(), wxEVT_COMMAND_CHECKBOX_CLICKED  , (wxObjectEventFunction) &ColorLayerImageViewPanel::onThresholdShow );
133         Connect( _interpolationCheckBox->GetId(), wxEVT_COMMAND_CHECKBOX_CLICKED, (wxObjectEventFunction) &ColorLayerImageViewPanel::onThresholdInterpolation );
134         Connect( _opacity->GetId(), wxEVT_SCROLL_THUMBTRACK, (wxObjectEventFunction) &ColorLayerImageViewPanel::onChangeOpacity );
135         Connect( _cb_SliceFixDinamic->GetId(), wxEVT_COMMAND_CHECKBOX_CLICKED, (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceFixDinamic );
136
137         Connect( _sl_SliceImageX->GetId(), wxEVT_SCROLL_THUMBTRACK      , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
138         Connect( _sl_SliceImageX->GetId(), wxEVT_SCROLL_LINEUP          , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
139         Connect( _sl_SliceImageX->GetId(), wxEVT_SCROLL_LINEDOWN                , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
140         Connect( _sl_SliceImageX->GetId(), wxEVT_SCROLL_PAGEUP          , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
141         Connect( _sl_SliceImageX->GetId(), wxEVT_SCROLL_PAGEDOWN                , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
142
143         Connect( _sl_SliceImageY->GetId(), wxEVT_SCROLL_THUMBTRACK      , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
144         Connect( _sl_SliceImageY->GetId(), wxEVT_SCROLL_LINEUP          , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
145         Connect( _sl_SliceImageY->GetId(), wxEVT_SCROLL_LINEDOWN                , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
146         Connect( _sl_SliceImageY->GetId(), wxEVT_SCROLL_PAGEUP          , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
147         Connect( _sl_SliceImageY->GetId(), wxEVT_SCROLL_PAGEDOWN                , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
148
149         Connect( _sl_SliceImageZ->GetId(), wxEVT_SCROLL_THUMBTRACK      , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
150         Connect( _sl_SliceImageZ->GetId(), wxEVT_SCROLL_LINEUP          , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
151         Connect( _sl_SliceImageZ->GetId(), wxEVT_SCROLL_LINEDOWN                , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
152         Connect( _sl_SliceImageZ->GetId(), wxEVT_SCROLL_PAGEUP          , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
153         Connect( _sl_SliceImageZ->GetId(), wxEVT_SCROLL_PAGEDOWN                , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
154
155         wxFlexGridSizer * sizer         = new wxFlexGridSizer(1);
156         if (type==1)
157         {
158                 sizer -> Add( new wxStaticText(this,-1,_T("Color Image Layer"))         , 1, wxGROW );
159                 sizer -> Add( _btn_ReadImage                                                                                                    , 1, wxGROW );
160                 sizer -> Add( new wxStaticText(this,-1,_T(" "))                                                 , 1, wxGROW );
161         }
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         this->SetSizer( sizer );
178         this->SetAutoLayout( true );
179         this->Layout();
180      
181         //CM Sets the default fitting mode to Pixel mode.
182         _fitting_mode = 3;
183         // EO CM
184 }
185
186 //----------------------------------------------------------------------------
187 ColorLayerImageViewPanel::~ColorLayerImageViewPanel()
188 {
189 }
190
191
192 //----------------------------------------------------------------------------
193 ColorLayerImageViewManager* ColorLayerImageViewPanel::GetColorLayerImageViewManager()
194 {
195    return _colorLayerImageViewManager;
196 }
197
198
199 //----------------------------------------------------------------------------
200 // This is the new spacing of the background image  (1,1,1)
201 void ColorLayerImageViewPanel::SetBaseSpacing(double spc[3])
202 {
203         _spcBase[0] = spc[0];
204         _spcBase[1] = spc[1];
205         _spcBase[2] = spc[2];
206
207         _spcBase[0] = 1;
208         _spcBase[1] = 1;
209         _spcBase[2] = 1;
210 }
211
212 //----------------------------------------------------------------------------
213 // This is the Original spacing of the background image
214 void ColorLayerImageViewPanel::SetOriginalSpacing(double spc[3])
215 {
216         _spcOrg[0] = spc[0];
217         _spcOrg[1] = spc[1];
218         _spcOrg[2] = spc[2];
219 }
220
221 //----------------------------------------------------------------------------
222 // This is the size in pixels of the background image
223 void ColorLayerImageViewPanel::SetBaseDimension(int dim[3])
224 {
225         _dimBase[0] = dim[0];
226         _dimBase[1] = dim[1];
227         _dimBase[2] = dim[2];
228 }
229
230 //----------------------------------------------------------------------------
231 void ColorLayerImageViewPanel::SetImage(vtkImageData *img)
232 {
233     if (img!=NULL)
234     {
235                 if (_histogramMinMaxLevel!=NULL) 
236                 {
237                         _histogramMinMaxLevel->Configure( img );
238                 }
239         _colorLayerImageViewManager->SetImage( img );
240
241         double spc[3];
242         img->GetSpacing(spc);
243
244         int dim[3];
245         int ext[6];
246         img->GetWholeExtent(ext);
247         dim[0] = ext[1]-ext[0];
248         dim[1] = ext[3]-ext[2];
249         dim[2] = ext[5]-ext[4];
250         _sl_SliceImageX->SetRange( 0 , dim[0] );
251         _sl_SliceImageY->SetRange( 0 , dim[1] );
252         _sl_SliceImageZ->SetRange( 0 , dim[2] );
253
254         if ( (spc[0]!=_spcBase[0]) || (spc[1]!=_spcBase[1]) || (spc[2]!=_spcBase[2]) ||
255              (dim[0]!=_dimBase[0]) || (spc[1]!=_spcBase[1]) || (spc[2]!=_spcBase[2])
256             )
257         {
258           // CM
259           int typeOfTransformation;     
260           // In case of the option is set to create a dialog box.
261           if (_fitting_mode == -1)
262             {
263               bool transformOkDlg;
264               wxString msg = _T("The image resolution of both images are not compatible. Do you want to tranform it? ");
265               wxDlgTransformByDimensionBySpacingByPixel dlg;
266               dlg.GetTransformType(this,msg,typeOfTransformation,transformOkDlg);
267               // CM Reset the default value (Pixel i.e. 3) if the dialog box returned an inappropriate value.
268               if (transformOkDlg == false)
269                 typeOfTransformation = 3;
270             }
271           else
272             typeOfTransformation = _fitting_mode;
273           //EO CM
274             if (typeOfTransformation==1)  // make dimensions equals
275             {
276                 printf("EED ColorLayerImageViewPanel::onReadImage ...WARNNING.... dif size image spc*dim ofnew image ...???\n");
277                 spc[0]=_spcBase[0]*_dimBase[0]/dim[0];
278                 spc[1]=_spcBase[1]*_dimBase[1]/dim[1];
279                 spc[2] = 1;
280             }
281
282             if (typeOfTransformation==2)  // make spacing equals
283             {
284                 spc[0] = ( _spcBase[0]/spc[0] ) * _spcOrg[0];
285                 spc[1] = ( _spcBase[1]/spc[1] ) * _spcOrg[1];
286                 spc[2] = ( _spcBase[2]/spc[2] ) * _spcOrg[2];
287             }
288
289             if (typeOfTransformation==3)  // make spacing = 1
290             {
291                 spc[0] = 1;
292                 spc[1] = 1;
293                 spc[2] = 1;
294             }
295
296             img->SetSpacing(spc);
297         } // spc !_spcBase   dim!__dimBase
298     } // _firsttime_mode
299
300 printf("EED ColorLayerImageViewPanel::SetImage\n");
301         if (_active==true)
302         {
303 printf("EED ColorLayerImageViewPanel::SetImage _active=true \n");
304             _colorLayerImageViewManager->onThreshold();
305                 RefreshView();
306         } // _ative 
307
308 }
309
310
311 void ColorLayerImageViewPanel::RefreshView()
312 {
313 //EED01         _colorLayerImageView->Refresh();
314         _colorLayerImageViewManager->Refresh();
315 }
316
317 //----------------------------------------------------------------------------
318 void ColorLayerImageViewPanel::SetFittingMode(int fitting_mode)
319 {
320   // CM Sets the fitting mode if an appropriate value is provided.
321   if (fitting_mode == -1 || fitting_mode == 1 || fitting_mode == 2 || fitting_mode == 3)
322     {
323       _fitting_mode = fitting_mode;
324     }
325   //Otherwise, an exception should be thrown (value 0 is fine, it corresponds to an empty field i.e. the default value is kept).
326   else if (fitting_mode != 0)
327     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;
328   //EO CM
329 }
330
331 //----------------------------------------------------------------------------
332 int ColorLayerImageViewPanel::GetFittingMode()
333 {
334   return _fitting_mode;
335 }
336
337 //----------------------------------------------------------------------------
338 void ColorLayerImageViewPanel::onReadImage(wxCommandEvent& event)
339 {
340 // EED ???
341 //      creaImageIO::WxSimpleDlg w(0,_T("Select your image"),"creaContours_Descriptor.dscp","creatisContours DB");
342 //      w.ShowModal();
343         printf("EED ColorLayerImageViewPanel::onReadImage ....WARNING... Read dlg. all images creaImageIO ... ???\n");
344
345         creaMaracasVisuKernel::OpenImageDialog diag = creaMaracasVisuKernel::OpenImageDialog();
346         SetImage( diag.getImageData() );
347 }
348
349 //----------------------------------------------------------------------------
350 void ColorLayerImageViewPanel::onThresholdChange(wxCommandEvent& event)
351 {
352                 if (_thresholdGo)
353                 {
354                  _colorLayerImageViewManager->onThresholdChange();
355                  RefreshView();
356                 //std::cout<< "Valor Min: " << minVal << " & Valor Max: " << maxVal  << std::endl;
357           } // _thresholdGo
358 }
359
360 //----------------------------------------------------------------------------
361 void ColorLayerImageViewPanel::onThresholdShow(wxCommandEvent& event)
362 {
363         _thresholdGo = _cb_ShowHide->GetValue();
364                         _active = _thresholdGo;
365         if (_thresholdGo)
366         {
367                     _colorLayerImageViewManager->onThreshold();
368         }else{
369                     _colorLayerImageViewManager->onThresholdRemove( );
370         }
371              RefreshView();
372 }
373
374 //----------------------------------------------------------------------------
375 void ColorLayerImageViewPanel::onThresholdStop()
376 {
377                 if (_thresholdGo)
378                 {
379                     _colorLayerImageViewManager->onThresholdRemove( );
380                     _thresholdGo=false;
381                 }
382 }
383
384 //----------------------------------------------------------------------------
385 void ColorLayerImageViewPanel::onThresholdInterpolation(wxCommandEvent& event)
386 {
387            _colorLayerImageViewManager->onThresholdInterpolation(_interpolationCheckBox->GetValue());
388                 RefreshView();
389 }
390
391 //----------------------------------------------------------------------------
392 void ColorLayerImageViewPanel::onSliceFixDinamic(wxCommandEvent& event)
393 {
394         bool fixdin = _cb_SliceFixDinamic->GetValue();
395         _colorLayerImageViewManager->SetSliceFixDynamic( fixdin );
396         if (fixdin==false)
397         {
398             _colorLayerImageViewManager->SetX2( _sl_SliceImageX->GetValue() );
399             _colorLayerImageViewManager->SetY2( _sl_SliceImageY->GetValue() );
400             _colorLayerImageViewManager->SetZ2( _sl_SliceImageZ->GetValue() );
401         }
402         _sl_SliceImageX->Enable(!fixdin);
403         _sl_SliceImageY->Enable(!fixdin);
404         _sl_SliceImageZ->Enable(!fixdin);
405         _colorLayerImageViewManager->onThreshold();
406         RefreshView();
407 }
408
409 //----------------------------------------------------------------------------
410 void ColorLayerImageViewPanel::onSliceImage(wxScrollEvent& event)
411 {
412    _colorLayerImageViewManager->SetX2( _sl_SliceImageX->GetValue() );
413    _colorLayerImageViewManager->SetY2( _sl_SliceImageY->GetValue() );
414    _colorLayerImageViewManager->SetZ2( _sl_SliceImageZ->GetValue() );
415    _colorLayerImageViewManager->onThreshold();
416         RefreshView();
417 }
418
419 //----------------------------------------------------------------------------
420 void ColorLayerImageViewPanel::onChangeOpacity(wxScrollEvent& event)
421 {
422         int opacity = _opacity->GetValue();
423         _colorLayerImageViewManager->onThresholdChangeOpacity(opacity);
424         RefreshView();
425 }
426
427
428 //----------------------------------------------------------------------------
429 bool ColorLayerImageViewPanel::IsVisible()
430 {
431         return _thresholdGo;
432 }
433
434 //----------------------------------------------------------------------------
435 void ColorLayerImageViewPanel::SetActive(bool active)
436 {
437       _active=active;
438       _cb_ShowHide->SetValue( _active );
439 }
440
441
442 // EOF
443