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 # ------------------------------------------------------------------------ */
27 This class plots image's histograms in a plotter
30 //---------------------
32 //----------------------
33 #include "HistogramWidget.h"
34 #include "vtkImageCast.h"
37 // ----------------------------------------------------------------------------
38 // WX headers inclusion.
39 // For compilers that support precompilation, includes <wx/wx.h>.
40 // ----------------------------------------------------------------------------
46 #include <wx/bitmap.h>
47 //----------------------------------------------------------------------------
48 // Class implementation
49 //----------------------------------------------------------------------------
51 IMPLEMENT_CLASS(HistogramWidget, wxWindow)
53 //----------------------------------------------------------------------------
55 //----------------------------------------------------------------------------
57 BEGIN_EVENT_TABLE(HistogramWidget, wxWindow)
58 EVT_SIZE (HistogramWidget::OnSize)
60 //---------------------
62 //----------------------
63 //wxScrolledWindow(wxWindow* parent, wxWindowID id = -1, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxHSCROLL | wxVSCROLL, const wxString& name = "scrolledWindow")
64 //wxWindow(wxWindow* parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = 0, const wxString& name = wxPanelNameStr)
65 //pPlotterWindow( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, int flag )
67 HistogramWidget::HistogramWidget( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, int flag):wxWindow(parent,id,pos,size,flag)
71 HistogramWidget::HistogramWidget( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, int flag,vtkImageData* imageData,int type):
72 wxPanel(parent,id,wxDefaultPosition,wxDefaultSize)
74 SetBackgroundColour(wxColour(255,255,255));
76 histogram= new pHistogram(imageData);
80 plotter=new pPlotter(this, 400,350);
82 //is a plotter of histograms
85 plotter->setPopUpMenu(true,true,true,true,true,true,false,false,false,false,false,false,false);
87 idTransferenceFunction = -1;
89 transferenceFunctionHasColor = true;
90 transferenceFunctionHasPoints = true;
93 this->SetAutoLayout(true);
98 drawTransferenceFunction();
101 HistogramWidget::HistogramWidget( wxWindow *parent, wxWindowID id)
102 : wxPanel(parent,id){
104 SetBackgroundColour(wxColour(255,255,255));
108 plotter=new pPlotter(this, 400,350);
110 //is a plotter of histograms
112 //setting the popMenu
113 plotter->setPopUpMenu(true,true,true,true,true,true,false,false,false,false,false,false,false);
115 idTransferenceFunction = -1;
117 transferenceFunctionHasColor = true;
118 transferenceFunctionHasPoints = true;
120 this->SetAutoLayout(true);
126 void HistogramWidget::initializeHistogram(vtkImageData* img){
129 histogram= new pHistogram(img);
133 drawTransferenceFunction();
136 HistogramWidget::~HistogramWidget()
143 Draw the histogram in the plotter
145 void HistogramWidget::drawHistogram()
147 //int xValues[MAX],yValues[MAX],extent[6];
151 vtkImageData* histogramImageData=histogram->getHistogram();
154 histogramSize=histogram->getSize();
157 xValues=(double*)malloc(NUM_POINTS*sizeof(double));
158 yValues=(double*)malloc(NUM_POINTS*sizeof(double));
160 double* histogramPointer=(double*)histogramImageData->GetScalarPointer(0,0,0);
162 for(i=0; i< histogramSize; i++)
165 if (histogramPointer[i]==0)
169 yValues[i]=log(histogramPointer[i])*10;
170 } // histogramPointer
174 pGraphicalFunction* histogramFunction=plotter->getFunctionForVectors(xValues,histogramSize,yValues,histogramSize);
176 if (histogramFunction)
178 histogramFunction->setEditable(false);
180 histogramFunction->setType(2);
182 histogramFunction->setmType(2);
183 idHistogram=plotter->addFunction(histogramFunction);
184 //for setting range the vision
185 plotter->addFunctionToMove(histogramFunction);
186 wxPen mypen1(*wxBLUE, 1, wxSOLID );
188 histogramFunction->SetPen( mypen1 );
196 Draw the transference function in the plotter
199 void HistogramWidget::drawTransferenceFunction()
201 double xValues[5],yValues[5];
203 int maxValueGrey=histogram->getMaximumLevelOfGrey();
205 xValues[1] = maxValueGrey/16;
206 xValues[2] = maxValueGrey/8;
207 xValues[3] = maxValueGrey/16+(maxValueGrey-maxValueGrey/2)/2;
208 xValues[4] = maxValueGrey;
217 pGraphicalFunction * tf = plotter ->getFunctionForVectors( xValues, 5, yValues, 5 );
218 printf("EED %p HistogramWidget::drawTransferenceFunction %p\n", this , tf);
219 // Including and drawing the created function in the plotter
223 //is the actual Function
226 tf->SetShowPoints(true);
227 idTransferenceFunction=plotter->addFunction( tf );
230 plotter->addFunctionToMove(tf);
232 wxPen mypen(*wxBLACK,0.5, wxSOLID );
239 if the user resize the window
241 void HistogramWidget::OnSize(wxSizeEvent &WXUNUSED(event))
244 GetClientSize(&scrX,&scrY);
245 plotter->SetSize(scrX,scrY);
246 pGraphicalFunction* actual=plotter->getFunction(idTransferenceFunction);
249 actual->setScreens(scrX,scrY);
255 get number of points of the transference function
257 int HistogramWidget::getSizeTransferenceFunction()
259 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
260 return tf->getSizePoints();
264 get a point of the transference function
266 void HistogramWidget::getTransferenceFunctionPoint(int index,int& x,int& y)
268 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
271 wxNode* pnode = tf->GetPointAt(index);
272 pFunctionPoint* point = (pFunctionPoint*)pnode->GetData();
273 x = point->getRealX();
274 y = point->getRealY();
278 get a point of the Histogram
281 int HistogramWidget::getHistogramPoint(int gValue)
283 return histogram->getHistogramPoint(gValue);
286 get number of points of the barColor
288 int HistogramWidget::getSizeBarColor()
290 return plotter->getColorPointsSize();
293 get a color int the bqr color
295 void HistogramWidget::getDataBarColorPoint(int index,int&x, int& red,int& green,int& blue)
298 plotter->getBarColorDataAt(index,tmp,red,green,blue);
302 Returns the maximum value ot the histogram that is show to the user
304 float HistogramWidget::getMaxShowedPorcentage()
306 float porcentageMaxX = plotter->getMaxShowedPorcentage();
307 pGraphicalFunction* histogramFunction = plotter->getFunction(idHistogram);
308 int min = histogramFunction->getMinX();
309 float x = porcentageMaxX*(histogramFunction->getMaxX()-min);
313 Returns the minimum value ot the histogram that is show to the user
315 float HistogramWidget::getMinShowedPorcentage()
317 float porcentageMinX = plotter->getMinShowedPorcentage();
318 pGraphicalFunction* histogramFunction = plotter->getFunction(idHistogram);
319 int min = histogramFunction->getMinX();
320 float x = porcentageMinX*(histogramFunction->getMaxX()-min);
324 Returns the minimum value ot the histogram that is show to the user
326 float HistogramWidget::getActualShowedPorcentage()
328 float porcentageActualX = plotter->getMinShowedPorcentage();
329 pGraphicalFunction* histogramFunction = plotter->getFunction(idHistogram);
330 int min = histogramFunction->getMinX();
331 float x = porcentageActualX*(histogramFunction->getMaxX()-min);
334 //---------------------------------------
335 // setting data in transferences function
337 //----------------------------------------
339 Adds a point to the transference function
341 bool HistogramWidget::addPointToTransferenceFunction(double x, double y)
344 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
345 //printf("EED %p HistogramWidget::addPointToTransferenceFunction tp%p x%f y%f %d\n",this, tf, x ,y, idTransferenceFunction);
348 result=tf->AddPoint(x,y);
354 add a color point to the histogram
355 @param x the level of grey to which the color is assigned
356 @param red the level of red for the color
357 @param green the level of red for the color
358 @param blue the level of red for the color
360 bool HistogramWidget::addColorPoint(double x,int red,int green, int blue)
362 return plotter->addColorPoint(x,red,green,blue);
365 //------------------------
367 //------------------------
369 Erase all the points that are in the transference function
372 void HistogramWidget::erasePointsTransferenceFunction(/*bool allPoints*/)
374 if(transferenceFunctionHasPoints)
376 // we have to erase the points
377 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
379 int numOfPoints=tf->getSizePoints();
380 int i=numOfPoints-1;//-2;
383 tf->deletePointAt(i);
387 } // if transferenceFunctionHasPoints
389 //we set for actual the histogram
390 //plotter->setActual()
393 Erase the color points in the plotter
395 void HistogramWidget::eraseColorPoints()
397 plotter->eraseColorPoints();
402 void HistogramWidget::updatePlotter()
407 //-------------------
408 // Getter and setters
409 //-------------------
411 void HistogramWidget::setTransferenceFunctionHasPoints(bool hasPoints)
413 transferenceFunctionHasPoints = hasPoints;
416 void HistogramWidget::setTransferenceFunctionHasColor(bool hasColorPoints)
418 transferenceFunctionHasPoints = hasColorPoints;
421 int HistogramWidget::getHistogramSize()
423 return histogramSize;
426 void HistogramWidget::setType(int type)
432 ** Returns two vectors, the grey level of the point and its value, the value is between [0,1]
434 void HistogramWidget::GetValuesPointsFunction(std::vector<double>& greylevel,std::vector<double>& value)
436 plotter->GetValuesPointsFunction(greylevel,value,histogramSize);
440 ** Returns two vectors, the grey level of the point and its value, the red, green
441 ** and blue value is between [0,1]
443 void HistogramWidget::GetValuesColorPointsFunction(std::vector<double>& greylevel,
444 std::vector<double>& red,
445 std::vector<double>& green,
446 std::vector<double>& blue)
448 plotter->GetValuesColorPointsFunction(greylevel,red,green,blue);