]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/pPlotter/pHistogram.cxx
2382 creaMaracasVisu Bug New High Box HistogramView not accept all formats
[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 //EED   points->SetScalarTypeToUnsignedShort();
146         points->SetScalarTypeToShort();
147         points->AllocateScalars();
148         points->Update();
149 }
150
151 /*
152          constructing the histogram
153 */
154 void pHistogram::setPoints(vtkImageData* imageData)
155 {
156         /*
157                 Pointers
158         */
159         unsigned short* dataImagePointerUS      = NULL;
160         short* dataImagePointerS                = NULL;
161         double* dataImagePointerD               = NULL;
162
163         short* dataHistogramPointer             = NULL;
164
165         dataImagePointerUS      = (unsigned short*)imageData->GetScalarPointer(0,0,0);
166         dataImagePointerS       = (short*)imageData->GetScalarPointer(0,0,0);
167         dataImagePointerD       = (double*)imageData->GetScalarPointer(0,0,0);
168
169         dataHistogramPointer    = (short*)points->GetScalarPointer(0,0,0);
170         
171         /*
172          Range of greys
173         */
174         double range[2];
175         if(imageData==NULL)
176                 range[1]=1;
177         else
178                 imageData->GetScalarRange(range);
179         /*
180          Setting the  minimun and maximum levels of grey
181         */
182         maxLevelOfGrey=range[1];
183         minLevelOfGrey=range[0];
184         //std::cout<<"maxLevelOfGrey "<<maxLevelOfGrey<<" minLevelOfGrey "<<minLevelOfGrey<<std::endl;
185         /*
186          Image Size
187         */
188         int ext[6];
189         imageData->GetExtent(ext);
190         int sx,sy,sz;
191         sx=ext[1]+1;
192         sy=ext[3]+1;
193         sz=ext[5]+1;
194
195         sizeImage=sx*sy*sz;
196
197         int i;
198         /*
199           getting ready the histogram
200         */
201         for(i=0;i<size;i++)
202         {
203                 dataHistogramPointer[i]=0;
204         }
205         
206         /*
207         Constructing the Histogram
208         */
209         //int k=size/(maxLevelOfGrey-minLevelOfGrey);
210         int j=0;
211         for(i=0;i<sizeImage;i++)
212         {
213                 /*
214                  hashing the histogram
215                 */
216                 //double p=((float)*dataImagePointer-minLevelOfGrey);
217                 //j=p*k;
218                 if (imageData->GetScalarType()==VTK_UNSIGNED_SHORT)
219                 { 
220                         j=getIndex(*dataImagePointerUS);
221                         dataImagePointerUS++;
222                 }
223                 if (imageData->GetScalarType()==VTK_SHORT) 
224                 {
225                         j=getIndex(*dataImagePointerS);
226                         dataImagePointerS++;
227                 }
228                 if (imageData->GetScalarType()==VTK_DOUBLE) 
229                 {
230                         j=getIndex(*dataImagePointerD);
231                         dataImagePointerD++;
232                 }
233 //EED           j=getIndex(*dataImagePointer);
234
235                 //std::cout<<j<<std::endl;
236                 dataHistogramPointer[j]++;
237         }
238         /*
239         BORRAME
240         */
241         /*
242         k=0;
243         for(i=0;i<size;i++)
244         {
245                 k=dataHistogramPointer[i];
246         }
247         */
248 }
249 /*
250 Returns the poins of the histograms
251 */
252 vtkImageData* pHistogram::getHistogram()
253 {
254         return points;
255 }
256
257 /*
258 hash por getting the index for the histogram vector of the original
259 image
260 @gValue: Level of grey for which wants the index in the histogrram
261 */
262 int pHistogram::getIndex(double gValue)
263 {
264
265         double p=((double)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey);
266         double k=p*(size-1);
267         //std::cout<<"gValue "<<gValue<<" k "<<k<<std::endl;
268         return (int)k;
269 }
270 /*
271 Setting the size
272 */
273 void pHistogram::setSize(int nSize)
274 {
275         size=nSize;
276 }
277 /*
278         Get Image Size
279 */
280 int pHistogram::getImageSize()
281
282         return sizeImage;
283 }
284 /*
285         Get Size of the histogram
286 */
287 int pHistogram::getSize()
288 {
289         return size;
290 }
291 /*
292         Get the maximum value of grey of the histogram
293 */
294 int pHistogram::getMaximumLevelOfGrey()
295 {
296         return maxLevelOfGrey;
297 }
298 /*
299         Get the minimum value of grey of the histogram
300 */
301 int pHistogram::getMinimumLevelOfGrey()
302 {
303         return minLevelOfGrey;
304 }
305 /*
306                 get a point of the Histogram
307                 given the grey value
308 */
309 int pHistogram::getHistogramPoint(int gValue)
310 {
311         //double p=((float)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey); // JPRx
312         //double k=p*size;
313
314         unsigned short* dataHistogramPointer=NULL;
315         dataHistogramPointer=(unsigned short*)points->GetScalarPointer(0,0,0);
316
317         return dataHistogramPointer[gValue];
318 }
319
320