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