]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/pPlotter/pHistogram.cxx
*** empty log message ***
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / pPlotter / pHistogram.cxx
1
2 //----------------------------------------------------------------------------
3 // Class definition include
4 //----------------------------------------------------------------------------
5
6 #include "pHistogram.h"
7 //#include "vtkJPEGReader.h"
8 #include "vtkImageData.h"
9 #include  "vtkImageCast.h"
10 //-----------------
11 // C++
12 //-----------------
13 #include <iostream>
14 #include <fstream>
15 #include <string>
16 #include <vector>
17
18 // ----------------------------------------------------------------------------
19 // WX headers inclusion.
20 // For compilers that support precompilation, includes <wx/wx.h>.
21 // ----------------------------------------------------------------------------
22
23 /*
24 #ifndef WX_PRECOMP
25 #include <wx/wx.h>
26 #endif
27 */
28 //----------------------------------------------------------------------------
29 // Class implementation
30 //----------------------------------------------------------------------------
31
32 //IMPLEMENT_CLASS(pHistogram, wxObject)
33
34 //----------------------------------------------------------------------------
35 // Constructors
36 //----------------------------------------------------------------------------
37 pHistogram::pHistogram(std::string filePath)
38 {
39         path=filePath;
40         points= vtkImageData::New();
41         size=100;
42         sizeImage=0;
43         buildHistogram();
44 }
45
46 pHistogram::pHistogram(vtkImageData* imageData)
47 {
48         points= vtkImageData::New();
49         size=100;
50         sizeImage=0;
51         //cast
52         /*
53         vtkImageCast* cast= vtkImageCast::New();
54         cast->SetInput(imageData);
55         cast->SetOutputScalarTypeToInt();
56         cast->Update();
57         */
58         //build the histogram
59         buildHistogram(imageData);
60 }
61
62 pHistogram::~pHistogram()
63 {
64         if(points!=NULL)points->Delete();
65 }
66 //----------------------------------------------------------------------------
67 // Methods
68 //----------------------------------------------------------------------------
69
70 void pHistogram::setImagePath(std::string filePath)
71 {
72         path=filePath;
73 }
74
75 void pHistogram::buildHistogram()
76 {
77         /*
78                 reader: is the reader of the image
79                 histogramVector:Is the image data of the original image
80                 imageData: vtkImageData of the image that we are reading
81                 range :it has the (min/max) range of the levels of grey
82         */
83         points= vtkImageData::New();
84         vtkMetaImageReader *reader = vtkMetaImageReader::New();
85         vtkImageData* imageData=NULL;
86         double range[2];
87         
88         //reading
89         reader->SetFileName(path.c_str());
90         reader->Update();
91
92         
93         // getting the data 
94         imageData=reader->GetOutput();
95         imageData->GetScalarRange(range);
96         initializePoints(size);
97         setPoints(imageData);
98         
99 }
100
101 /*
102 Calculate the histogram and save it in the attribute points
103 it is used if the user had given the imageData
104 Pre: Size had to be setted if the user wants
105 another value that 100
106 */
107 void pHistogram::buildHistogram(vtkImageData* imageData)
108 {
109         initializePoints(size);
110         setPoints(imageData);   
111 }
112
113 /*
114   getting ready the points
115 */
116 void pHistogram::initializePoints(int xDimension)
117 {
118         //setting image data of the points
119         points->SetDimensions(xDimension,1,1);
120         points->SetScalarTypeToUnsignedShort();
121         points->AllocateScalars();
122         points->Update();
123 }
124
125 /*
126          constructing the histogram
127 */
128 void pHistogram::setPoints(vtkImageData* imageData)
129 {
130         /*
131                 Pointers
132         */
133         unsigned short* dataImagePointer=NULL;
134         unsigned short* dataHistogramPointer=NULL;
135
136         dataImagePointer=(unsigned short*)imageData->GetScalarPointer(0,0,0);
137         dataHistogramPointer=(unsigned short*)points->GetScalarPointer(0,0,0);
138         
139         /*
140          Range of greys
141         */
142         double range[2];
143         if(imageData==NULL)
144                 range[1]=1;
145         else
146                 imageData->GetScalarRange(range);
147         /*
148          Setting the  minimun and maximum levels of grey
149         */
150         maxLevelOfGrey=(int)range[1];
151         minLevelOfGrey=(int)range[0];
152         //std::cout<<"maxLevelOfGrey "<<maxLevelOfGrey<<" minLevelOfGrey "<<minLevelOfGrey<<std::endl;
153         /*
154          Image Size
155         */
156         int ext[6];
157         imageData->GetExtent(ext);
158         int sx,sy,sz;
159         sx=ext[1]+1;
160         sy=ext[3]+1;
161         sz=ext[5]+1;
162
163         sizeImage=sx*sy*sz;
164
165         int i;
166         /*
167           getting ready the histogram
168         */
169         for(i=0;i<size;i++)
170         {
171                 dataHistogramPointer[i]=0;
172         }
173         
174         /*
175         Constructing the Histogram
176         */
177         //int k=size/(maxLevelOfGrey-minLevelOfGrey);
178         int j=0;
179         for(i=0;i<sizeImage;i++)
180         {
181                 /*
182                  hashing the histogram
183                 */
184                 //double p=((float)*dataImagePointer-minLevelOfGrey);
185                 //j=p*k;
186                 
187                 j=getIndex(*dataImagePointer);
188                 //std::cout<<j<<std::endl;
189                 dataHistogramPointer[j]++;
190                 dataImagePointer++;
191         }
192         /*
193         BORRAME
194         */
195         /*
196         k=0;
197         for(i=0;i<size;i++)
198         {
199                 k=dataHistogramPointer[i];
200         }
201         */
202 }
203 /*
204 Returns the poins of the histograms
205 */
206 vtkImageData* pHistogram::getHistogram()
207 {
208         return points;
209 }
210
211 /*
212 hash por getting the index for the histogram vector of the original
213 image
214 @gValue: Level of grey for which wants the index in the histogrram
215 */
216 int pHistogram::getIndex(int gValue)
217 {
218
219         double p=((double)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey);
220         double k=p*(size-1);
221         //std::cout<<"gValue "<<gValue<<" k "<<k<<std::endl;
222         return (int)k;
223 }
224 /*
225 Setting the size
226 */
227 void pHistogram::setSize(int nSize)
228 {
229         size=nSize;
230 }
231 /*
232         Get Image Size
233 */
234 int pHistogram::getImageSize()
235
236         return sizeImage;
237 }
238 /*
239         Get Size of the histogram
240 */
241 int pHistogram::getSize()
242 {
243         return size;
244 }
245 /*
246         Get the maximum value of grey of the histogram
247 */
248 int pHistogram::getMaximumLevelOfGrey()
249 {
250         return maxLevelOfGrey;
251 }
252 /*
253         Get the minimum value of grey of the histogram
254 */
255 int pHistogram::getMinimumLevelOfGrey()
256 {
257         return minLevelOfGrey;
258 }
259 /*
260                 get a point of the Histogram
261                 given the grey value
262 */
263 int pHistogram::getHistogramPoint(int gValue)
264 {
265         //double p=((float)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey); // JPRx
266         //double k=p*size;
267
268         unsigned short* dataHistogramPointer=NULL;
269         dataHistogramPointer=(unsigned short*)points->GetScalarPointer(0,0,0);
270
271         return dataHistogramPointer[gValue];
272 }
273
274