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