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