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