]> Creatis software - creaRigidRegistration.git/blob - lib/Transparency.cxx
#3463 creaRigid Registration Bug New Normal - Clean comments transparency
[creaRigidRegistration.git] / lib / Transparency.cxx
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and 
11 #  abiding by the rules of distribution of free software. You can  use, 
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
13 #  license as circulated by CEA, CNRS and INRIA at the following URL 
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability. 
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------      */                                                                    
26
27 #include "Transparency.h"
28
29
30 /*
31 * Constructor
32 */
33 //------------------------------------------------------------
34 Transparency::Transparency()
35 {
36         _image1         = NULL;
37         _image2         = NULL;
38         _percent        = 0;
39         _newImage       = NULL;
40         _processed      = false;
41 }
42
43 /*
44 * Destructor
45 */
46 //------------------------------------------------------------
47 Transparency::~Transparency()
48 {
49         if (_newImage != NULL ) { _newImage->Delete(); }
50 }
51
52 //------------------------------------------------------------
53 void Transparency::calculateImage()
54 {
55         if(_image1!=NULL && _image2!=NULL)
56         {
57                 // IMAGE 1
58                         
59                 // Information from image1
60                 _image1->GetSpacing(spcImg1);
61                 _image1->GetExtent(extImg1);
62                 _type = _image1->GetScalarType();
63                 // The Z dimension is 1 for a 2D image
64                 dimImg1[0] = extImg1[1] - extImg1[0]+1;
65                 dimImg1[1] = extImg1[3] - extImg1[2]+1;
66                 dimImg1[2] = 1;
67                 //IMAGE 2
68                 // Information from image2
69                 _image2->GetSpacing(spcImg2);
70                 _image2->GetExtent(extImg2);
71                 // The Z dimension is 1 for a 2D image
72                 dimImg2[0] = extImg2[1] - extImg2[0]+1;
73                 dimImg2[1] = extImg2[3] - extImg2[2]+1;
74                 dimImg2[2] = 1;
75                 long numPixelsImg1 = dimImg1[0]*dimImg1[1];
76                 long numPixelsImg2 = dimImg2[0]*dimImg2[1];             
77                 double factorX = 1;
78                 double factorY = 1;
79                 vtkImageResample *resample = vtkImageResample::New();
80                 vtkImageData *result;
81                 if(numPixelsImg1<numPixelsImg2)
82                 {
83 //EED 2017-01-01 Migration VTK7
84 #if VTK_MAJOR_VERSION <= 5
85                         resample->SetInput(_image1);
86 #else
87                         resample->SetInputData(_image1);
88 #endif
89                         factorX = (double)extImg2[1]/extImg1[1];
90                         factorY = (double)extImg2[3]/extImg1[3];
91                         resample->SetAxisMagnificationFactor(0,factorX);
92                         resample->SetAxisMagnificationFactor(1,factorY);
93                         resample->SetInformationInput(_image2);
94                         initialize(dimImg2, spcImg2);
95 //EED 2017-01-01 Migration VTK7
96 #if VTK_MAJOR_VERSION <= 5
97                         //..
98 #else
99                         resample->Update();
100 #endif
101                         result = resample->GetOutput();
102 //EED 2017-01-01 Migration VTK7
103 #if VTK_MAJOR_VERSION <= 5
104                         result->Update();
105 #else
106                         //...
107 #endif
108                         createImage(result,_image2,dimImg2[0],dimImg2[1]);              
109                 } else if (numPixelsImg1>numPixelsImg2) {
110 //EED 2017-01-01 Migration VTK7
111 #if VTK_MAJOR_VERSION <= 5
112                         resample->SetInput(_image2);
113 #else
114                         resample->SetInputData(_image2);
115 #endif
116                         factorX = (double)extImg1[1]/extImg2[1];
117                         factorY = (double)extImg1[3]/extImg2[3];
118                         resample->SetAxisMagnificationFactor(0,factorX);
119                         resample->SetAxisMagnificationFactor(1,factorY);
120                         resample->SetInformationInput(_image1);
121                         initialize(dimImg1, spcImg1);
122 //EED 2017-01-01 Migration VTK7
123 #if VTK_MAJOR_VERSION <= 5
124                         // ...
125 #else
126                         resample->Update();
127 #endif
128                         result = resample->GetOutput();
129                         
130 //EED 2017-01-01 Migration VTK7
131 #if VTK_MAJOR_VERSION <= 5
132                         result->Update();
133 #else
134                         result->Modified();
135 #endif
136                         createImage(_image1,result,dimImg1[0],dimImg1[1]);
137                 } else {
138                         //If both images have the same number of pixels, the resultant image will have the 
139                         //properties of the first image.
140 //EED 2017-01-01 Migration VTK7
141 #if VTK_MAJOR_VERSION <= 5
142                         resample->SetInput(_image2);
143 #else
144                         resample->SetInputData(_image2);
145 #endif
146                         factorX = (double)extImg1[1]/extImg2[1];
147                         factorY = (double)extImg1[3]/extImg2[3];
148                         resample->SetAxisMagnificationFactor(0,factorX);
149                         resample->SetAxisMagnificationFactor(1,factorY);
150                         resample->SetInformationInput(_image1);
151                         initialize(dimImg1, spcImg1);
152 //EED 2017-01-01 Migration VTK7
153 #if VTK_MAJOR_VERSION <= 5
154                         // ...
155 #else
156                         resample->Update();
157 #endif
158                         result = resample->GetOutput();
159 //EED 2017-01-01 Migration VTK7
160 #if VTK_MAJOR_VERSION <= 5
161                         result->Update();
162 #else
163                         result->Modified();
164 #endif
165                         createImage(_image1,result,dimImg1[0],dimImg1[1]);
166                 } //else
167                 resample->Delete();             
168                 _processed=true;
169                 }
170 }
171
172 void Transparency::initialize(int dimensions[], double spacing[])
173 {
174         // Setting the new image
175         _newImage = vtkImageData::New();
176         _newImage->SetSpacing(spacing);
177         _newImage->SetDimensions(dimensions);
178 //EED 2017-01-01 Migration VTK7
179 #if VTK_MAJOR_VERSION <= 5
180         _newImage->SetScalarType(_type);
181         _newImage->AllocateScalars();
182         _newImage->Update();
183 #else
184         _newImage->AllocateScalars(_type,1);
185 #endif
186 }
187
188 void Transparency::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
189 {
190         if(_type == VTK_CHAR)
191         {
192                 //POINTERS: 
193                 char* dataImagePointer1                 = NULL;
194                 char* dataImagePointer2                 = NULL;
195                 char* dataImageResultPointer    = NULL;
196                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
197         }
198         else if(_type == VTK_SIGNED_CHAR)
199         {
200                 //POINTERS: 
201                 signed char* dataImagePointer1          = NULL;
202                 signed char* dataImagePointer2          = NULL;
203                 signed char* dataImageResultPointer = NULL;
204         
205                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
206         }
207         else if(_type == VTK_UNSIGNED_CHAR)
208         {
209                 //POINTERS: 
210                 unsigned char* dataImagePointer1                = NULL;
211                 unsigned char* dataImagePointer2                = NULL;
212                 unsigned char* dataImageResultPointer   = NULL;
213         
214                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
215         }
216         else if(_type == VTK_SHORT)
217         {
218                 //POINTERS: 
219                 short* dataImagePointer1                = NULL;
220                 short* dataImagePointer2                = NULL;
221                 short* dataImageResultPointer   = NULL;
222         
223                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
224         }
225         else if(_type == VTK_UNSIGNED_SHORT)
226         {
227                 //POINTERS: 
228                 unsigned short* dataImagePointer1               = NULL;
229                 unsigned short* dataImagePointer2               = NULL;
230                 unsigned short* dataImageResultPointer  = NULL;
231         
232                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
233         }
234         else if(_type == VTK_INT)
235         {
236                 //POINTERS: 
237                 int* dataImagePointer1 = NULL;
238                 int* dataImagePointer2 = NULL;
239                 int* dataImageResultPointer = NULL;
240         
241                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
242         }
243         else if(_type == VTK_UNSIGNED_INT)
244         {
245                 //POINTERS: 
246                 unsigned int* dataImagePointer1 = NULL;
247                 unsigned int* dataImagePointer2 = NULL;
248                 unsigned int* dataImageResultPointer = NULL;
249         
250                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
251         }
252         else if(_type == VTK_LONG)
253         {
254                 //POINTERS: 
255                 long* dataImagePointer1 = NULL;
256                 long* dataImagePointer2 = NULL;
257                 long* dataImageResultPointer = NULL;
258         
259                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
260         }
261         else if(_type == VTK_UNSIGNED_LONG)
262         {
263                 //POINTERS: 
264                 unsigned long* dataImagePointer1                = NULL;
265                 unsigned long* dataImagePointer2                = NULL;
266                 unsigned long* dataImageResultPointer   = NULL;
267         
268                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);              
269         }
270         else if(_type == VTK_FLOAT)
271         {
272                 //POINTERS: 
273                 float* dataImagePointer1                = NULL;
274                 float* dataImagePointer2                = NULL;
275                 float* dataImageResultPointer   = NULL;
276         
277                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
278         }
279         else if(_type == VTK_DOUBLE)
280         {
281                 //POINTERS: 
282                 double* dataImagePointer1               = NULL;
283                 double* dataImagePointer2               = NULL;
284                 double* dataImageResultPointer  = NULL;
285
286                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
287         }
288
289 //EED 2017-01-01 Migration VTK7
290 #if VTK_MAJOR_VERSION <= 5
291         _newImage->Update();
292 #else
293         _newImage->Modified();
294 #endif
295 }
296
297 template <class T>
298 void Transparency::createImageByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
299 {
300         int i, j;
301         dataImagePointer1 = (T*) img1->GetScalarPointer(0,0,0);
302         dataImagePointer2 = (T*) img2->GetScalarPointer(0,0,0);
303         dataImageResultPointer = (T*) _newImage->GetScalarPointer(0,0,0);
304         for(i = 0; i < sizeX; i++)
305         {
306                 for(j = 0; j < sizeY; j++)
307                 {
308                         dataImagePointer1 = (T*)img1->GetScalarPointer(i,j,0);
309                         dataImagePointer2 = (T*)img2->GetScalarPointer(i,j,0);
310                         dataImageResultPointer = (T*)_newImage->GetScalarPointer(i,j,0);
311                         *dataImageResultPointer = (T)*dataImagePointer1*_percent + (T)*dataImagePointer2*(1-_percent);                          
312                 } // for h
313         } // for i
314 }
315         
316 /*
317 * Get new image
318 */
319 //------------------------------------------------------------
320 vtkImageData* Transparency::getImage()
321 {
322         if(_processed){
323                 return _newImage;
324         }
325         else{
326                 return NULL;
327         }
328 }
329
330 //------------------------------------------------------------
331 void Transparency::setImage1(vtkImageData *image)
332 {
333         _image1=image;
334 }
335
336 //------------------------------------------------------------
337 void Transparency::setImage2(vtkImageData *image)
338 {
339         _image2=image;
340 }
341
342 //------------------------------------------------------------
343 void Transparency::setPercent(int percent)
344 {
345     _percent=(double)percent/100.0;     
346 }
347