]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/pPlotter/pHistogram.cxx
creaMaracasVisu Library
[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         /*
153          Image Size
154         */
155         int ext[6];
156         imageData->GetExtent(ext);
157         int sx,sy,sz;
158         sx=ext[1]+1;
159         sy=ext[3]+1;
160         sz=ext[5]+1;
161
162         sizeImage=sx*sy*sz;
163
164         int i;
165         /*
166           getting ready the histogram
167         */
168         for(i=0;i<size;i++)
169         {
170                 dataHistogramPointer[i]=0;
171         }
172         
173         /*
174         Constructing the Histogram
175         */
176         //int k=size/(maxLevelOfGrey-minLevelOfGrey);
177         int j=0;
178         for(i=0;i<sizeImage;i++)
179         {
180                 /*
181                  hashing the histogram
182                 */
183                 //double p=((float)*dataImagePointer-minLevelOfGrey);
184                 //j=p*k;
185                 
186                 j=getIndex(*dataImagePointer);
187                 dataHistogramPointer[j]++;
188                 dataImagePointer++;
189         }
190         /*
191         BORRAME
192         */
193         /*
194         k=0;
195         for(i=0;i<size;i++)
196         {
197                 k=dataHistogramPointer[i];
198         }
199         */
200 }
201 /*
202 Returns the poins of the histograms
203 */
204 vtkImageData* pHistogram::getHistogram()
205 {
206         return points;
207 }
208
209 /*
210 hash por getting the index for the histogram vector of the original
211 image
212 @gValue: Level of grey for which wants the index in the histogrram
213 */
214 int pHistogram::getIndex(int gValue)
215 {
216         double p=((float)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey);
217         double k=p*(size-1);
218         return (int)k;
219 }
220 /*
221 Setting the size
222 */
223 void pHistogram::setSize(int nSize)
224 {
225         size=nSize;
226 }
227 /*
228         Get Image Size
229 */
230 int pHistogram::getImageSize()
231
232         return sizeImage;
233 }
234 /*
235         Get Size of the histogram
236 */
237 int pHistogram::getSize()
238 {
239         return size;
240 }
241 /*
242         Get the maximum value of grey of the histogram
243 */
244 int pHistogram::getMaximumLevelOfGrey()
245 {
246         return maxLevelOfGrey;
247 }
248 /*
249         Get the minimum value of grey of the histogram
250 */
251 int pHistogram::getMinimumLevelOfGrey()
252 {
253         return minLevelOfGrey;
254 }
255 /*
256                 get a point of the Histogram
257                 given the grey value
258 */
259 int pHistogram::getHistogramPoint(int gValue)
260 {
261         double p=((float)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey);
262         double k=p*size;
263
264         unsigned short* dataHistogramPointer=NULL;
265         dataHistogramPointer=(unsigned short*)points->GetScalarPointer(0,0,0);
266
267         return dataHistogramPointer[gValue];
268 }
269
270