]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/pPlotter/pHistogram.cxx
#3144 creaMaracasVisu Bug New Normal - changeWx28to30
[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         buildHistogram(imageData);
78 }
79
80 //----------------------------------------------------------------------------
81 pHistogram::~pHistogram()
82 {
83         if(points!=NULL)points->Delete();
84 }
85
86 //----------------------------------------------------------------------------
87 void pHistogram::setImagePath(std::string filePath)
88 {
89         path=filePath;
90 }
91
92 //----------------------------------------------------------------------------
93 void pHistogram::buildHistogram()
94 {
95         /*
96                 reader: is the reader of the image
97                 histogramVector:Is the image data of the original image
98                 imageData: vtkImageData of the image that we are reading
99                 range :it has the (min/max) range of the levels of grey
100         */
101         points= vtkImageData::New();
102         vtkMetaImageReader *reader = vtkMetaImageReader::New();
103         vtkImageData* imageData=NULL;
104         double range[2];
105         
106         //reading
107         reader->SetFileName(path.c_str());
108         reader->Update();
109
110         
111         // getting the data 
112         imageData=reader->GetOutput();
113         imageData->GetScalarRange(range);
114         initializePoints(size);
115         setPoints(imageData);
116 }
117
118 /*
119 Calculate the histogram and save it in the attribute points
120 it is used if the user had given the imageData
121 Pre: Size had to be setted if the user wants
122 another value that 100
123 */
124 void pHistogram::buildHistogram(vtkImageData* imageData)
125 {
126         initializePoints(size);
127         setPoints(imageData);   
128 }
129
130 /*
131   getting ready the points
132 */
133 void pHistogram::initializePoints(int xDimension)
134 {
135         //setting image data of the points
136         points->SetDimensions(xDimension,1,1);
137 //EED   points->SetScalarTypeToUnsignedShort();
138
139 //EED 2017-01-01 Migration VTK7
140 #if VTK_MAJOR_VERSION <= 5
141         points->SetScalarTypeToDouble();
142         points->AllocateScalars();
143         points->Update();
144 #else
145         points->AllocateScalars(VTK_DOUBLE,1);
146 #endif
147
148 }
149
150 /*
151          constructing the histogram
152 */
153 void pHistogram::setPoints(vtkImageData* imageData)
154 {
155
156 /*
157 printf("EED pHistogram::setPoints Test: vtkPComputeHistogram2DOutliers\n");
158 vtkComputeHistogram2DOutliers *vtkhist = vtkComputeHistogram2DOutliers::New();
159 vtkhist->SetPreferredNumberOfOutliers(10);
160 vtkhist->SetInput(imageData);
161 vtkTable *resulttable = vtkhist->GetOutputTable();
162
163 printf("EED pHistogram::setPoints rows:%d\n", (int)(resulttable->GetNumberOfRows()) );
164 printf("EED pHistogram::setPoints colums:%d\n", (int)(resulttable->GetNumberOfColumns()) );
165 */
166
167         /*
168                 Pointers
169         */
170
171         char*                           dataImagePointerC               = (char*)imageData->GetScalarPointer(0,0,0);
172         unsigned char*          dataImagePointerUC              = (unsigned char*)imageData->GetScalarPointer(0,0,0);
173         short*                          dataImagePointerS               = (short*)imageData->GetScalarPointer(0,0,0);
174         unsigned short*         dataImagePointerUS              = (unsigned short*)imageData->GetScalarPointer(0,0,0);
175         float*                          dataImagePointerF               = (float*)imageData->GetScalarPointer(0,0,0);
176         double*                         dataImagePointerD               = (double*)imageData->GetScalarPointer(0,0,0);
177         double*                         dataHistogramPointer    = (double*)points->GetScalarPointer(0,0,0);
178         
179         /*
180          Range of greys
181         */
182         double range[2];
183         if(imageData==NULL)
184         {
185                 range[1] = 1;
186         } else {
187                 imageData->GetScalarRange(range);
188         } // imageData
189
190         /*
191          Setting the  minimun and maximum levels of grey
192         */
193         maxLevelOfGrey = range[1];
194         minLevelOfGrey = range[0];
195         //std::cout<<"maxLevelOfGrey "<<maxLevelOfGrey<<" minLevelOfGrey "<<minLevelOfGrey<<std::endl;
196         /*
197          Image Size
198         */
199         int ext[6];
200         imageData->GetExtent(ext);
201         int sx,sy,sz;
202         sx = ext[1]-ext[0]+1;
203         sy = ext[3]-ext[2]+1;
204         sz = ext[5]-ext[4]+1;
205
206         sizeImage=sx*sy*sz;
207
208         int i;
209         /*
210           getting ready the histogram
211         */
212         for(i=0;i<size;i++)
213         {
214                 dataHistogramPointer[i]=0;
215         }
216         
217         long int j=0;
218
219
220         if (imageData->GetScalarType()==VTK_CHAR)
221         {
222                 for(i=0;i<sizeImage;i++)
223                 {
224                         j=getIndex(*dataImagePointerC);
225                         dataImagePointerC++;
226                         dataHistogramPointer[j]++;
227                 }  // for i
228         } //  CHAR
229
230         if (imageData->GetScalarType()==VTK_UNSIGNED_CHAR)
231         {
232                 for(i=0;i<sizeImage;i++)
233                 {
234                         j=getIndex(*dataImagePointerUC);
235                         dataImagePointerUC++;
236                         dataHistogramPointer[j]++;
237                 }  // for i
238         } // UNSIGNED CHAR
239
240
241         if (imageData->GetScalarType()==VTK_SHORT)
242         {
243                 for(i=0;i<sizeImage;i++)
244                 {
245                         j=getIndex(*dataImagePointerS);
246                         dataImagePointerS++;
247                         dataHistogramPointer[j]++;
248                 }  // for i
249         } // SHORT
250
251         if (imageData->GetScalarType()==VTK_UNSIGNED_SHORT)
252         {
253                 for(i=0;i<sizeImage;i++)
254                 {
255                         j=getIndex(*dataImagePointerUS);
256                         dataImagePointerUS++;
257                         dataHistogramPointer[j]++;
258                 }  // for i
259         } // UNSIGNED SHORT
260
261
262         if (imageData->GetScalarType()==VTK_FLOAT)
263         {
264                 for(i=0;i<sizeImage;i++)
265                 {
266                         j=getIndex(*dataImagePointerF);
267                         dataImagePointerF++;
268                         dataHistogramPointer[j]++;
269                 }  // for i
270         } // FLOAT
271
272
273         if (imageData->GetScalarType()==VTK_DOUBLE)
274         {
275                 for(i=0;i<sizeImage;i++)
276                 {
277                         j=getIndex(*dataImagePointerD);
278                         dataImagePointerD++;
279                         dataHistogramPointer[j]++;
280                 }  // for i
281         } // DOUBLE
282
283 }
284
285
286 /*
287 Returns the poins of the histograms
288 */
289 vtkImageData* pHistogram::getHistogram()
290 {
291         return points;
292 }
293
294 /*
295 hash por getting the index for the histogram vector of the original
296 image
297 @gValue: Level of grey for which wants the index in the histogrram
298 */
299 int pHistogram::getIndex(double gValue)
300 {
301         double p;
302         double diff=maxLevelOfGrey-minLevelOfGrey;
303         if (diff!=0)
304         {
305                 p=((double)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey);
306         } else {
307                 p=0;
308         }
309         double k=p*(size-1);
310         //std::cout<<"gValue "<<gValue<<" k "<<k<<std::endl;
311         return (int)k;
312 }
313 /*
314 Setting the size
315 */
316 void pHistogram::setSize(int nSize)
317 {
318         size=nSize;
319 }
320 /*
321         Get Image Size
322 */
323 int pHistogram::getImageSize()
324
325         return sizeImage;
326 }
327 /*
328         Get Size of the histogram
329 */
330 int pHistogram::getSize()
331 {
332         return size;
333 }
334 /*
335         Get the maximum value of grey of the histogram
336 */
337 int pHistogram::getMaximumLevelOfGrey()
338 {
339         return maxLevelOfGrey;
340 }
341 /*
342         Get the minimum value of grey of the histogram
343 */
344 int pHistogram::getMinimumLevelOfGrey()
345 {
346         return minLevelOfGrey;
347 }
348 /*
349                 get a point of the Histogram
350                 given the grey value
351 */
352 int pHistogram::getHistogramPoint(int gValue)
353 {
354         //double p=((float)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey); // JPRx
355         //double k=p*size;
356
357         double* dataHistogramPointer = dataHistogramPointer=(double*)points->GetScalarPointer(0,0,0);
358         return dataHistogramPointer[gValue];
359 }
360
361