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