1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------ */
26 #include "HistogramDialogComboBox.h"
31 #include "creaSystem.h"
32 #include "HistogramDialog.h"
33 //-------------------------
35 //-------------------------
36 HistogramDialogComboBox::HistogramDialogComboBox(wxWindow* parent)
42 _sliderwindowlevel = NULL;
44 colorBar_Bitmap = NULL;
46 InitCurrentPathColorsFuntion();
47 this->SetSizer(getBitmapCombo());
54 HistogramDialogComboBox::~HistogramDialogComboBox()
58 void HistogramDialogComboBox::SetColors(std::vector<double> greyvect, std::vector<double> redvect, std::vector<double> greenvect, std::vector<double> bluevect)
62 _greenvect = greenvect;
66 wxSizer* HistogramDialogComboBox::getBitmapCombo()
68 //_bitmapsizer = new wxBoxSizer(wxVERTICAL);
69 _bitmapsizer = new wxBoxSizer(wxVERTICAL);
71 wxBoxSizer* comboeditsizer = new wxBoxSizer(wxHORIZONTAL);
72 _bitmapcombo = getBitmapComboElements();
73 BitmapComboItemSelect(0);
74 wxBitmap bitmap1(Edit_xpm);
75 wxBitmapButton* edit = new wxBitmapButton(this, -1, bitmap1,wxDefaultPosition,wxSize(30,30));
76 Connect(edit->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)&HistogramDialogComboBox::OnEditBitmapCombo);
77 _ckboxActive= new wxCheckBox( this,
81 //wxSize(sizeX,sizeY),
83 wxCHK_2STATE | wxALIGN_RIGHT);
84 _ckboxActive->SetValue(false);
86 Connect( _ckboxActive->GetId(), wxEVT_COMMAND_CHECKBOX_CLICKED, (wxObjectEventFunction) (void (wxPanel::*)(wxScrollEvent&)) &HistogramDialogComboBox::OnChkBoxActive);
88 comboeditsizer->Add(_ckboxActive,wxSizerFlags().Center().FixedMinSize());
89 comboeditsizer->Add(_bitmapcombo,wxSizerFlags().Center().FixedMinSize());
90 comboeditsizer->Add(edit,wxSizerFlags().Center());
91 _bitmapsizer->Add(comboeditsizer,wxSizerFlags().FixedMinSize().Center());
92 _bitmapsizer->AddSpacer(5);
93 _bitmapsizer->Add(getSlidersWlCo(),wxSizerFlags().Expand().Center());
97 wxSizer* HistogramDialogComboBox::getSlidersWlCo()
99 wxBoxSizer* sizersliders = new wxBoxSizer(wxVERTICAL);
100 _slidercolor = new wxSlider(this, -1,1,0,1,wxDefaultPosition,wxDefaultSize,wxSL_LABELS);
101 _sliderwindowlevel = new wxSlider(this, -1,1,0,1,wxDefaultPosition,wxDefaultSize,wxSL_LABELS);
102 sizersliders->Add(_slidercolor,wxSizerFlags().Expand().Center());
103 sizersliders->Add(_sliderwindowlevel,wxSizerFlags().Expand().Center());
104 Connect(_slidercolor->GetId(), wxEVT_SCROLL_THUMBTRACK, (wxObjectEventFunction)&HistogramDialogComboBox::OnColorLevelChanged);
105 Connect(_sliderwindowlevel->GetId(), wxEVT_SCROLL_THUMBTRACK, (wxObjectEventFunction)&HistogramDialogComboBox::OnWindowLevelChanged);
109 double HistogramDialogComboBox::GetWindowLevel()
112 if(_sliderwindowlevel != NULL)
114 val= _sliderwindowlevel->GetValue();
119 double HistogramDialogComboBox::GetColorLevel()
122 if(_slidercolor != NULL)
124 val = _slidercolor->GetValue();
130 bool HistogramDialogComboBox::GetActive()
133 if (_ckboxActive!=NULL)
135 result = _ckboxActive->GetValue();
141 void HistogramDialogComboBox::OnChkBoxActive(wxCommandEvent& event)
143 wxCommandEvent newevent(wxEVT_COMMAND_COMBOBOX_SELECTED,this->GetId());
144 ProcessEvent(newevent);
148 void HistogramDialogComboBox::OnColorLevelChanged(wxCommandEvent& event)
150 _slidercolor->GetValue();
151 wxCommandEvent newevent(wxEVT_SCROLL_THUMBTRACK,this->GetId());
152 ProcessEvent(newevent);
155 void HistogramDialogComboBox::OnWindowLevelChanged(wxCommandEvent& event)
157 _sliderwindowlevel->GetValue();
158 wxCommandEvent newevent(wxEVT_SCROLL_THUMBTRACK,this->GetId());
159 ProcessEvent(newevent);
162 void HistogramDialogComboBox::setImageData(vtkImageData* img)
164 _maxgreyvalue = img->GetScalarRange()[1];
169 void HistogramDialogComboBox::setSlidersValue()
172 if(_slidercolor!=NULL){
173 _slidercolor->SetRange(_img->GetScalarRange()[0],_img->GetScalarRange()[1]);
174 _slidercolor->SetValue((_img->GetScalarRange()[0]+_img->GetScalarRange()[1])/2);
176 if(_sliderwindowlevel!=NULL){
177 _sliderwindowlevel->SetRange(_img->GetScalarRange()[0],_img->GetScalarRange()[1]);
178 _sliderwindowlevel->SetValue((_img->GetScalarRange()[0]+_img->GetScalarRange()[1])/2);
183 void HistogramDialogComboBox::OnEditBitmapCombo(wxCommandEvent& event)
187 bool createaddandremovecontrols = true;
188 HistogramDialog* dialog = new HistogramDialog(this,_T("Color Configuration"),createaddandremovecontrols);
189 dialog->initializeHistogram(_img);
190 dialog->setInfoPanExtraControls(_bitmapsitems);
192 if(dialog->ShowModal()==wxID_OK)
194 dialog->UpdateCurrentComboElement();
195 _bitmapsitems = dialog->getComboBoxItems();
197 std::vector<double> redvect,greenvect,bluevect;
198 std::vector<double> greyvect;
200 saveCurrentConfiguration(_bitmapsitems, GetFileNameOfColorsFunction() );
201 wxBitmapComboBox* tempbitmapcombo = _bitmapcombo;
202 for(int i = 0; i < _bitmapsitems.size();i++)
204 delete _bitmapsitems[i];
206 _bitmapsitems.clear();
207 _bitmapcombo = getBitmapComboElements();
210 // if(_bitmapsizer->Replace(tempbitmapcombo,_bitmapcombo))
212 // tempbitmapcombo->Destroy();
214 // printf ("EED ERROR HistogramDialogComboBox::OnEditBitmapCombo. Can't replace bitmapcombo \n");
224 void HistogramDialogComboBox::saveCurrentConfiguration(std::vector<HistogramDialogComboBoxItem*>& itembitmaps,std::string filename)
227 file.open( (const char*) (filename.c_str()) );
228 double gv=-1,red=-1,gr=-1,bl=-1;
233 file << "<ComboBoxData>" << std::endl;
234 for(int i = 0; i < itembitmaps.size();i++)
236 HistogramDialogComboBoxItem* item = itembitmaps[i];
237 std::vector<double> vecttransfer = item->getGreyVectorTransfer();
238 std::vector<double> value = item->getValueVector();
240 file << " <ComboBoxItem>"<< std::endl;
241 for(int j = 0; j < vecttransfer.size();j++)
244 file << " <TransferenceFunction>"<< std::endl;
246 file << " <Transferencepoint>" << std::endl;
247 file << " <greyValue>" << vecttransfer[j] << "</greyValue>" <<std::endl;
248 file << " <intensity>" << value[j] << "</intensity> "<<std::endl;
249 file << " </Transferencepoint>" << std::endl;
250 if(j==vecttransfer.size()-1){
251 file << " </TransferenceFunction>"<< std::endl;
254 std::vector<double> greyv = item->getGreyVector();
255 std::vector<double> red = item->getRedVector();
256 std::vector<double> green = item->getGreenVector();
257 std::vector<double> blue = item->getBlueVector();
258 for(int j = 0; j < greyv.size();j++)
261 file << " <Colors>" << std::endl;
263 file << " <greyValueRGBpoint>" << std::endl;
264 file << " <RGBgreyValue>" << greyv[j] << "</RGBgreyValue>" <<std::endl;
265 file << " <red>" << red[j] << "</red>" <<std::endl;
266 file << " <green>" << green[j] << "</green>" <<std::endl;
267 file << " <blue>" << blue[j] << "</blue>" <<std::endl;
268 file << " </greyValueRGBpoint>" << std::endl;
269 if(j==greyv.size()-1){
270 file << " </Colors>" << std::endl;
273 file << " </ComboBoxItem>"<< std::endl;
275 file << "</ComboBoxData>" << std::endl;
280 void HistogramDialogComboBox::InitCurrentPathColorsFuntion()
282 _currentpathColorsFuntion = "";
284 _currentpathColorsFuntion = crea::System::GetDllAppPath("bbcreaMaracasVisu");
285 _currentpathColorsFuntion.append("/data/");
288 _currentpathColorsFuntion = crea::System::GetExecutablePath();
289 _currentpathColorsFuntion.append("/../share/creaMaracasVisu/data/");
291 crea::System::createDirectory(_currentpathColorsFuntion.c_str());
296 std::string HistogramDialogComboBox::GetFileNameOfColorsFunction()
298 std::string filename;
299 filename = _currentpathColorsFuntion;
300 filename.append(COLORSFUNCTIONFILENAME);
305 wxBitmapComboBox* HistogramDialogComboBox::getBitmapComboElements()
307 std::vector<HistogramDialogComboBoxItem*> bitmapsitems;
308 OnLoadComboBoxData(bitmapsitems, GetFileNameOfColorsFunction() );
309 _bitmapsitems = bitmapsitems;
311 choices = new wxString[bitmapsitems.size()];
312 for(int i = 0; i < bitmapsitems.size();i++)
314 choices[i] = _T("Color Function");
317 wxBitmapComboBox* bitmapcombo;
318 //EED wxBitmapComboBox* bitmapcombo = new wxBitmapComboBox(this, -1, _T(""), wxDefaultPosition, wxDefaultSize, bitmapsitems.size(),choices);
319 if (_bitmapcombo!=NULL)
321 _bitmapcombo->Clear();
322 bitmapcombo=_bitmapcombo;
324 bitmapcombo = new wxBitmapComboBox(this, -1, _T(""));
326 // wxBitmapComboBox* bitmapcombo = new wxBitmapComboBox(this, -1, _T(""));
327 bitmapcombo->SetSize(165,30);
329 Connect(bitmapcombo->GetId(), wxEVT_COMMAND_COMBOBOX_SELECTED, (wxObjectEventFunction)&HistogramDialogComboBox::OnBitmapComboItemSelected);
331 for(int i = 0; i < bitmapsitems.size(); i++)
333 bitmapcombo->Append(_T("ColorFunction"), bitmapsitems[i]->GetBitmap() );
334 // bitmapcombo->SetItemBitmap(i, bitmapsitems[i]->GetBitmap());
336 BitmapComboItemSelect(0);
341 void HistogramDialogComboBox::BitmapComboItemSelect(int value)
343 HistogramDialogComboBoxItem* currentconfig;
344 if(_bitmapcombo!=NULL)
346 _currentitem = value;
347 _bitmapcombo->Select(value);
348 if(_bitmapsitems.size()>_currentitem)
350 currentconfig = _bitmapsitems[_currentitem];
351 _greyvect = currentconfig->getGreyVector();
352 _redvect = currentconfig->getRedVector();
353 _greenvect = currentconfig->getGreenVector();
354 _bluevect = currentconfig->getBlueVector();
355 _greyvecttransfer = currentconfig->getGreyVectorTransfer();
356 _value = currentconfig->getValueVector();
357 wxCommandEvent newevent(wxEVT_COMMAND_COMBOBOX_SELECTED,this->GetId());
358 ProcessEvent(newevent);
364 void HistogramDialogComboBox::OnBitmapComboItemSelected(wxCommandEvent& event)
366 BitmapComboItemSelect( event.GetInt() );
369 void HistogramDialogComboBox::OnLoadComboBoxData(std::vector<HistogramDialogComboBoxItem*>& itembitmaps, std::string filename)
371 std::vector<double> redvect, greenvect, bluevect;
372 std::vector<double> greyvect, greyvecttransfunct, value;
378 greyvecttransfunct.clear();
382 file.open( (const char*) (filename.c_str()) );
383 double gv=-1,gvtransfer=-1,intensity=-1,red=-1,gr=-1,bl=-1;
386 //std::cout<<filename<<std::endl;
392 HistogramDialogComboBoxItem* item=NULL;
396 std::getline(file,line);
397 //std::cout<<line<<std::endl;
398 if( (int)(line.find("<ComboBoxItem>")!=-1))
400 item = new HistogramDialogComboBoxItem();
401 }else if((int)(line.find("<greyValue>"))!=-1) {
402 int pos1=line.find(">");
403 int pos2=line.find("<",pos1+1);
404 std::string x=line.substr(pos1+1,pos2-pos1-1);
405 gvtransfer=atof(x.c_str());
406 greyvecttransfunct.push_back(gvtransfer);
407 }else if((int)(line.find("<intensity>"))!=-1) {
408 int pos1=line.find(">");
409 int pos2=line.find("<",pos1+1);
410 std::string x=line.substr(pos1+1,pos2-pos1-1);
411 intensity=atof(x.c_str());
412 value.push_back(intensity);
414 else if( (int)(line.find("<RGBgreyValue>"))!=-1)
416 int pos1=line.find(">");
417 int pos2=line.find("<",pos1+1);
418 std::string x=line.substr(pos1+1,pos2-pos1-1);
420 greyvect.push_back(gv);
422 else if( (int)(line.find("<red>"))!=-1)
424 int pos1=line.find(">");
425 int pos2=line.find("<",pos1+1);
426 std::string x=line.substr(pos1+1,pos2-pos1-1);
428 redvect.push_back(red);
430 else if( (int)(line.find("<green>"))!=-1)
432 int pos1=line.find(">");
433 int pos2=line.find("<",pos1+1);
434 std::string x=line.substr(pos1+1,pos2-pos1-1);
436 greenvect.push_back(gr);
438 else if( (int)(line.find("<blue>"))!=-1 )
440 int pos1=line.find(">");
441 int pos2=line.find("<",pos1+1);
442 std::string x=line.substr(pos1+1,pos2-pos1-1);
444 bluevect.push_back(bl);
446 }else if( (int)(line.find("</ComboBoxItem>"))!=-1 ) {
448 item->SetColors(greyvect,redvect,greenvect,bluevect);
449 item->SetTransferFunction(greyvecttransfunct,value);
450 /*for(int i = 0; i < greyvecttransfunct.size();i++){
451 std::cout<<"HistogramDialogComboBox::OnLoadComboBoxData("<<greyvecttransfunct[i]<<std::endl;
452 std::cout<<value[i]<<std::endl;
454 for(int i = 0; i < greyvect.size();i++){
455 std::cout<<"HistogramDialogComboBox::OnLoadComboBoxData("<<greyvect[i]<<std::endl;
456 std::cout<<redvect[i]<<std::endl;
457 std::cout<<greenvect[i]<<std::endl;
458 std::cout<<bluevect[i]<<std::endl;
461 itembitmaps.push_back(item);
463 greyvecttransfunct.clear();
473 //std::cout<<itembitmaps.size()<<std::endl;
478 ** Returns two vectors, the grey level of the point and its value, the value is between [0,1]
480 void HistogramDialogComboBox::GetValuesPointsFunction(std::vector<double>& greylevel,std::vector<double>& value)
482 for(int i = 0; i < _greyvecttransfer.size();i++)
484 greylevel.push_back(_greyvecttransfer[i]*_maxgreyvalue);
485 value.push_back(_value[i]);
490 ** Returns two vectors, the grey level of the point and its value, the red, green
491 ** and blue value is between [0,1]
493 void HistogramDialogComboBox::GetValuesColorPointsFunction(std::vector<double>& greylevel,
494 std::vector<double>& red,
495 std::vector<double>& green,
496 std::vector<double>& blue)
498 for(int i = 0; i < _greyvect.size();i++)
500 greylevel.push_back(_greyvect[i]*_maxgreyvalue);
501 red.push_back(_redvect[i]);
502 green.push_back(_greenvect[i]);
503 blue.push_back(_bluevect[i]);