]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/pPlotter/pHistogram.cxx
#2482 creaMaracasVisu Bug New High - ColorLayer refresh missing. The MPR is not...
[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 //----------------------------------------------------------------------------
89 pHistogram::~pHistogram()
90 {
91         if(points!=NULL)points->Delete();
92 }
93
94 //----------------------------------------------------------------------------
95 void pHistogram::setImagePath(std::string filePath)
96 {
97         path=filePath;
98 }
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 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->SetScalarTypeToDouble();
147         points->AllocateScalars();
148         points->Update();
149 }
150
151 /*
152          constructing the histogram
153 */
154 void pHistogram::setPoints(vtkImageData* imageData)
155 {
156
157 printf("EED pHistogram::setPoints Trinng new histogram ..\n");
158 /*
159 printf("EED pHistogram::setPoints Test: vtkPComputeHistogram2DOutliers\n");
160 vtkComputeHistogram2DOutliers *vtkhist = vtkComputeHistogram2DOutliers::New();
161 vtkhist->SetPreferredNumberOfOutliers(10);
162 vtkhist->SetInput(imageData);
163 vtkTable *resulttable = vtkhist->GetOutputTable();
164
165 printf("EED pHistogram::setPoints rows:%d\n", (int)(resulttable->GetNumberOfRows()) );
166 printf("EED pHistogram::setPoints colums:%d\n", (int)(resulttable->GetNumberOfColumns()) );
167 */
168
169         /*
170                 Pointers
171         */
172
173         char*                           dataImagePointerC               = (char*)imageData->GetScalarPointer(0,0,0);
174         unsigned char*  dataImagePointerUC      = (unsigned char*)imageData->GetScalarPointer(0,0,0);
175         short*                          dataImagePointerS               = (short*)imageData->GetScalarPointer(0,0,0);
176         unsigned short*         dataImagePointerUS      = (unsigned short*)imageData->GetScalarPointer(0,0,0);
177         float*                          dataImagePointerF               = (float*)imageData->GetScalarPointer(0,0,0);
178         double*                                 dataImagePointerD               = (double*)imageData->GetScalarPointer(0,0,0);
179
180         double* dataHistogramPointer                            = (double*)points->GetScalarPointer(0,0,0);
181         
182         /*
183          Range of greys
184         */
185         double range[2];
186         if(imageData==NULL)
187         {
188                 range[1] = 1;
189         } else {
190                 imageData->GetScalarRange(range);
191         } // imageData
192
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]-ext[0]+1;
206         sy=ext[3]-ext[2]+1;
207         sz=ext[5]-ext[4]+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         int j=0;
221
222
223         if (imageData->GetScalarType()==VTK_CHAR)
224         {
225                 for(i=0;i<sizeImage;i++)
226                 {
227                         j=getIndex(*dataImagePointerC);
228                         dataImagePointerC++;
229                         dataHistogramPointer[j]++;
230                 }  // for i
231         } //  CHAR
232
233         if (imageData->GetScalarType()==VTK_UNSIGNED_CHAR)
234         {
235                 for(i=0;i<sizeImage;i++)
236                 {
237                         j=getIndex(*dataImagePointerUC);
238                         dataImagePointerUC++;
239                         dataHistogramPointer[j]++;
240                 }  // for i
241         } // UNSIGNED CHAR
242
243
244         if (imageData->GetScalarType()==VTK_SHORT)
245         {
246                 for(i=0;i<sizeImage;i++)
247                 {
248                         j=getIndex(*dataImagePointerS);
249                         dataImagePointerS++;
250                         dataHistogramPointer[j]++;
251                 }  // for i
252         } // SHORT
253
254         if (imageData->GetScalarType()==VTK_UNSIGNED_SHORT)
255         {
256                 for(i=0;i<sizeImage;i++)
257                 {
258                         j=getIndex(*dataImagePointerUS);
259                         dataImagePointerUS++;
260                         dataHistogramPointer[j]++;
261                 }  // for i
262         } // UNSIGNED SHORT
263
264
265         if (imageData->GetScalarType()==VTK_FLOAT)
266         {
267                 for(i=0;i<sizeImage;i++)
268                 {
269                         j=getIndex(*dataImagePointerF);
270                         dataImagePointerF++;
271                         dataHistogramPointer[j]++;
272                 }  // for i
273         } // FLOAT
274
275
276         if (imageData->GetScalarType()==VTK_DOUBLE)
277         {
278                 for(i=0;i<sizeImage;i++)
279                 {
280                         j=getIndex(*dataImagePointerD);
281                         dataImagePointerD++;
282                         dataHistogramPointer[j]++;
283                 }  // for i
284         } // DOUBLE
285
286 }
287
288
289 /*
290 Returns the poins of the histograms
291 */
292 vtkImageData* pHistogram::getHistogram()
293 {
294         return points;
295 }
296
297 /*
298 hash por getting the index for the histogram vector of the original
299 image
300 @gValue: Level of grey for which wants the index in the histogrram
301 */
302 int pHistogram::getIndex(double gValue)
303 {
304
305         double p=((double)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey);
306         double k=p*(size-1);
307         //std::cout<<"gValue "<<gValue<<" k "<<k<<std::endl;
308         return (int)k;
309 }
310 /*
311 Setting the size
312 */
313 void pHistogram::setSize(int nSize)
314 {
315         size=nSize;
316 }
317 /*
318         Get Image Size
319 */
320 int pHistogram::getImageSize()
321
322         return sizeImage;
323 }
324 /*
325         Get Size of the histogram
326 */
327 int pHistogram::getSize()
328 {
329         return size;
330 }
331 /*
332         Get the maximum value of grey of the histogram
333 */
334 int pHistogram::getMaximumLevelOfGrey()
335 {
336         return maxLevelOfGrey;
337 }
338 /*
339         Get the minimum value of grey of the histogram
340 */
341 int pHistogram::getMinimumLevelOfGrey()
342 {
343         return minLevelOfGrey;
344 }
345 /*
346                 get a point of the Histogram
347                 given the grey value
348 */
349 int pHistogram::getHistogramPoint(int gValue)
350 {
351         //double p=((float)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey); // JPRx
352         //double k=p*size;
353
354         double* dataHistogramPointer = dataHistogramPointer=(double*)points->GetScalarPointer(0,0,0);
355         return dataHistogramPointer[gValue];
356 }
357
358