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;
43 colorBar_Bitmap = NULL;
46 InitCurrentPathColorsFuntion();
47 this->SetSizer(getBitmapCombo());
58 HistogramDialogComboBox::~HistogramDialogComboBox()
62 void HistogramDialogComboBox::SetColors(std::vector<double> greyvect, std::vector<double> redvect, std::vector<double> greenvect, std::vector<double> bluevect)
66 _greenvect = greenvect;
70 wxSizer* HistogramDialogComboBox::getBitmapCombo()
72 //_bitmapsizer = new wxBoxSizer(wxVERTICAL);
73 _bitmapsizer = new wxBoxSizer(wxVERTICAL);
75 wxBoxSizer* comboeditsizer = new wxBoxSizer(wxHORIZONTAL);
76 _bitmapcombo = getBitmapComboElements();
77 BitmapComboItemSelect(0);
79 wxBitmap bitmap1(Edit_xpm);
80 wxBitmapButton* edit = new wxBitmapButton(this, -1, bitmap1,wxDefaultPosition,wxSize(30,30));
81 Connect(edit->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)&HistogramDialogComboBox::OnEditBitmapCombo);
83 comboeditsizer->Add(_bitmapcombo,wxSizerFlags().Center().FixedMinSize());
84 comboeditsizer->Add(edit,wxSizerFlags().Center());
86 _bitmapsizer->Add(comboeditsizer,wxSizerFlags().FixedMinSize().Center());
87 _bitmapsizer->AddSpacer(5);
88 _bitmapsizer->Add(getSlidersWlCo(),wxSizerFlags().Expand().Center());
93 wxSizer* HistogramDialogComboBox::getSlidersWlCo()
95 wxBoxSizer* sizersliders = new wxBoxSizer(wxVERTICAL);
97 _slidercolor = new wxSlider(this, -1,1,0,1,wxDefaultPosition,wxDefaultSize,wxSL_LABELS);
98 _sliderwindowlevel = new wxSlider(this, -1,1,0,1,wxDefaultPosition,wxDefaultSize,wxSL_LABELS);
100 sizersliders->Add(_slidercolor,wxSizerFlags().Expand().Center());
101 sizersliders->Add(_sliderwindowlevel,wxSizerFlags().Expand().Center());
103 Connect(_slidercolor->GetId(), wxEVT_SCROLL_THUMBTRACK, (wxObjectEventFunction)&HistogramDialogComboBox::OnColorLevelChanged);
104 Connect(_sliderwindowlevel->GetId(), wxEVT_SCROLL_THUMBTRACK, (wxObjectEventFunction)&HistogramDialogComboBox::OnWindowLevelChanged);
109 double HistogramDialogComboBox::GetWindowLevel()
112 if(_sliderwindowlevel != NULL)
113 val= _sliderwindowlevel->GetValue();
117 double HistogramDialogComboBox::GetColorLevel()
120 if(_slidercolor != NULL)
121 val = _slidercolor->GetValue();
125 void HistogramDialogComboBox::OnColorLevelChanged(wxCommandEvent& event)
127 _slidercolor->GetValue();
128 wxCommandEvent newevent(wxEVT_SCROLL_THUMBTRACK,this->GetId());
129 ProcessEvent(newevent);
132 void HistogramDialogComboBox::OnWindowLevelChanged(wxCommandEvent& event)
134 _sliderwindowlevel->GetValue();
135 wxCommandEvent newevent(wxEVT_SCROLL_THUMBTRACK,this->GetId());
136 ProcessEvent(newevent);
139 void HistogramDialogComboBox::setImageData(vtkImageData* img)
141 _maxgreyvalue = img->GetScalarRange()[1];
146 void HistogramDialogComboBox::setSlidersValue()
149 if(_slidercolor!=NULL){
150 _slidercolor->SetRange(_img->GetScalarRange()[0],_img->GetScalarRange()[1]);
151 _slidercolor->SetValue((_img->GetScalarRange()[0]+_img->GetScalarRange()[1])/2);
153 if(_sliderwindowlevel!=NULL){
154 _sliderwindowlevel->SetRange(_img->GetScalarRange()[0],_img->GetScalarRange()[1]);
155 _sliderwindowlevel->SetValue((_img->GetScalarRange()[0]+_img->GetScalarRange()[1])/2);
160 void HistogramDialogComboBox::OnEditBitmapCombo(wxCommandEvent& event)
164 bool createaddandremovecontrols = true;
165 HistogramDialog* dialog = new HistogramDialog(this,_T("Color Configuration"),createaddandremovecontrols);
166 dialog->initializeHistogram(_img);
167 dialog->setInfoPanExtraControls(_bitmapsitems);
169 if(dialog->ShowModal()==wxID_OK)
171 dialog->UpdateCurrentComboElement();
172 _bitmapsitems = dialog->getComboBoxItems();
174 std::vector<double> redvect,greenvect,bluevect;
175 std::vector<double> greyvect;
177 saveCurrentConfiguration(_bitmapsitems, GetFileNameOfColorsFunction() );
178 wxBitmapComboBox* tempbitmapcombo = _bitmapcombo;
179 for(int i = 0; i < _bitmapsitems.size();i++)
181 delete _bitmapsitems[i];
183 _bitmapsitems.clear();
184 _bitmapcombo = getBitmapComboElements();
187 // if(_bitmapsizer->Replace(tempbitmapcombo,_bitmapcombo))
189 // tempbitmapcombo->Destroy();
191 // printf ("EED ERROR HistogramDialogComboBox::OnEditBitmapCombo. Can't replace bitmapcombo \n");
201 void HistogramDialogComboBox::saveCurrentConfiguration(std::vector<HistogramDialogComboBoxItem*>& itembitmaps,std::string filename)
204 file.open( (const char*) (filename.c_str()) );
205 double gv=-1,red=-1,gr=-1,bl=-1;
210 file << "<ComboBoxData>" << std::endl;
211 for(int i = 0; i < itembitmaps.size();i++)
213 HistogramDialogComboBoxItem* item = itembitmaps[i];
214 std::vector<double> vecttransfer = item->getGreyVectorTransfer();
215 std::vector<double> value = item->getValueVector();
217 file << " <ComboBoxItem>"<< std::endl;
218 for(int j = 0; j < vecttransfer.size();j++)
221 file << " <TransferenceFunction>"<< std::endl;
223 file << " <Transferencepoint>" << std::endl;
224 file << " <greyValue>" << vecttransfer[j] << "</greyValue>" <<std::endl;
225 file << " <intensity>" << value[j] << "</intensity> "<<std::endl;
226 file << " </Transferencepoint>" << std::endl;
227 if(j==vecttransfer.size()-1){
228 file << " </TransferenceFunction>"<< std::endl;
231 std::vector<double> greyv = item->getGreyVector();
232 std::vector<double> red = item->getRedVector();
233 std::vector<double> green = item->getGreenVector();
234 std::vector<double> blue = item->getBlueVector();
235 for(int j = 0; j < greyv.size();j++)
238 file << " <Colors>" << std::endl;
240 file << " <greyValueRGBpoint>" << std::endl;
241 file << " <RGBgreyValue>" << greyv[j] << "</RGBgreyValue>" <<std::endl;
242 file << " <red>" << red[j] << "</red>" <<std::endl;
243 file << " <green>" << green[j] << "</green>" <<std::endl;
244 file << " <blue>" << blue[j] << "</blue>" <<std::endl;
245 file << " </greyValueRGBpoint>" << std::endl;
246 if(j==greyv.size()-1){
247 file << " </Colors>" << std::endl;
250 file << " </ComboBoxItem>"<< std::endl;
252 file << "</ComboBoxData>" << std::endl;
257 void HistogramDialogComboBox::InitCurrentPathColorsFuntion()
259 _currentpathColorsFuntion = "";
261 _currentpathColorsFuntion = crea::System::GetDllAppPath("bbcreaMaracasVisu");
264 _currentpathColorsFuntion = crea::System::GetExecutablePath();
265 _currentpathColorsFuntion.append("/../share/creaMaracasVisu/data/");
267 crea::System::createDirectory(_currentpathColorsFuntion.c_str());
272 std::string HistogramDialogComboBox::GetFileNameOfColorsFunction()
274 std::string filename;
275 filename = _currentpathColorsFuntion;
276 filename.append(COLORSFUNCTIONFILENAME);
281 wxBitmapComboBox* HistogramDialogComboBox::getBitmapComboElements()
283 std::vector<HistogramDialogComboBoxItem*> bitmapsitems;
284 OnLoadComboBoxData(bitmapsitems, GetFileNameOfColorsFunction() );
285 _bitmapsitems = bitmapsitems;
287 choices = new wxString[bitmapsitems.size()];
288 for(int i = 0; i < bitmapsitems.size();i++)
290 choices[i] = _T("Color Function");
293 wxBitmapComboBox* bitmapcombo;
294 //EED wxBitmapComboBox* bitmapcombo = new wxBitmapComboBox(this, -1, _T(""), wxDefaultPosition, wxDefaultSize, bitmapsitems.size(),choices);
295 if (_bitmapcombo!=NULL)
297 _bitmapcombo->Clear();
298 bitmapcombo=_bitmapcombo;
300 bitmapcombo = new wxBitmapComboBox(this, -1, _T(""));
302 // wxBitmapComboBox* bitmapcombo = new wxBitmapComboBox(this, -1, _T(""));
303 bitmapcombo->SetSize(165,30);
305 Connect(bitmapcombo->GetId(), wxEVT_COMMAND_COMBOBOX_SELECTED, (wxObjectEventFunction)&HistogramDialogComboBox::OnBitmapComboItemSelected);
307 for(int i = 0; i < bitmapsitems.size(); i++)
309 bitmapcombo->Append(_T("ColorFunction"), bitmapsitems[i]->GetBitmap() );
310 // bitmapcombo->SetItemBitmap(i, bitmapsitems[i]->GetBitmap());
312 BitmapComboItemSelect(0);
317 void HistogramDialogComboBox::BitmapComboItemSelect(int value)
319 HistogramDialogComboBoxItem* currentconfig;
320 if(_bitmapcombo!=NULL)
322 _currentitem = value;
323 _bitmapcombo->Select(value);
324 if(_bitmapsitems.size()>_currentitem)
326 currentconfig = _bitmapsitems[_currentitem];
327 _greyvect = currentconfig->getGreyVector();
328 _redvect = currentconfig->getRedVector();
329 _greenvect = currentconfig->getGreenVector();
330 _bluevect = currentconfig->getBlueVector();
331 _greyvecttransfer = currentconfig->getGreyVectorTransfer();
332 _value = currentconfig->getValueVector();
333 wxCommandEvent newevent(wxEVT_COMMAND_COMBOBOX_SELECTED,this->GetId());
334 ProcessEvent(newevent);
340 void HistogramDialogComboBox::OnBitmapComboItemSelected(wxCommandEvent& event)
342 BitmapComboItemSelect( event.GetInt() );
345 void HistogramDialogComboBox::OnLoadComboBoxData(std::vector<HistogramDialogComboBoxItem*>& itembitmaps, std::string filename)
347 std::vector<double> redvect, greenvect, bluevect;
348 std::vector<double> greyvect, greyvecttransfunct, value;
354 greyvecttransfunct.clear();
358 file.open( (const char*) (filename.c_str()) );
359 double gv=-1,gvtransfer=-1,intensity=-1,red=-1,gr=-1,bl=-1;
362 //std::cout<<filename<<std::endl;
368 HistogramDialogComboBoxItem* item=NULL;
372 std::getline(file,line);
373 //std::cout<<line<<std::endl;
374 if( (int)(line.find("<ComboBoxItem>")!=-1))
376 item = new HistogramDialogComboBoxItem();
377 }else if((int)(line.find("<greyValue>"))!=-1) {
378 int pos1=line.find(">");
379 int pos2=line.find("<",pos1+1);
380 std::string x=line.substr(pos1+1,pos2-pos1-1);
381 gvtransfer=atof(x.c_str());
382 greyvecttransfunct.push_back(gvtransfer);
383 }else if((int)(line.find("<intensity>"))!=-1) {
384 int pos1=line.find(">");
385 int pos2=line.find("<",pos1+1);
386 std::string x=line.substr(pos1+1,pos2-pos1-1);
387 intensity=atof(x.c_str());
388 value.push_back(intensity);
390 else if( (int)(line.find("<RGBgreyValue>"))!=-1)
392 int pos1=line.find(">");
393 int pos2=line.find("<",pos1+1);
394 std::string x=line.substr(pos1+1,pos2-pos1-1);
396 greyvect.push_back(gv);
398 else if( (int)(line.find("<red>"))!=-1)
400 int pos1=line.find(">");
401 int pos2=line.find("<",pos1+1);
402 std::string x=line.substr(pos1+1,pos2-pos1-1);
404 redvect.push_back(red);
406 else if( (int)(line.find("<green>"))!=-1)
408 int pos1=line.find(">");
409 int pos2=line.find("<",pos1+1);
410 std::string x=line.substr(pos1+1,pos2-pos1-1);
412 greenvect.push_back(gr);
414 else if( (int)(line.find("<blue>"))!=-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 bluevect.push_back(bl);
422 }else if( (int)(line.find("</ComboBoxItem>"))!=-1 ) {
424 item->SetColors(greyvect,redvect,greenvect,bluevect);
425 item->SetTransferFunction(greyvecttransfunct,value);
426 /*for(int i = 0; i < greyvecttransfunct.size();i++){
427 std::cout<<"HistogramDialogComboBox::OnLoadComboBoxData("<<greyvecttransfunct[i]<<std::endl;
428 std::cout<<value[i]<<std::endl;
430 for(int i = 0; i < greyvect.size();i++){
431 std::cout<<"HistogramDialogComboBox::OnLoadComboBoxData("<<greyvect[i]<<std::endl;
432 std::cout<<redvect[i]<<std::endl;
433 std::cout<<greenvect[i]<<std::endl;
434 std::cout<<bluevect[i]<<std::endl;
437 itembitmaps.push_back(item);
439 greyvecttransfunct.clear();
449 //std::cout<<itembitmaps.size()<<std::endl;
454 ** Returns two vectors, the grey level of the point and its value, the value is between [0,1]
456 void HistogramDialogComboBox::GetValuesPointsFunction(std::vector<double>& greylevel,std::vector<double>& value)
458 for(int i = 0; i < _greyvecttransfer.size();i++) {
459 greylevel.push_back(_greyvecttransfer[i]*_maxgreyvalue);
460 value.push_back(_value[i]);
465 ** Returns two vectors, the grey level of the point and its value, the red, green
466 ** and blue value is between [0,1]
468 void HistogramDialogComboBox::GetValuesColorPointsFunction(std::vector<double>& greylevel,
469 std::vector<double>& red,
470 std::vector<double>& green,
471 std::vector<double>& blue)
473 for(int i = 0; i < _greyvect.size();i++) {
474 greylevel.push_back(_greyvect[i]*_maxgreyvalue);
475 red.push_back(_redvect[i]);
476 green.push_back(_greenvect[i]);
477 blue.push_back(_bluevect[i]);