]> Creatis software - creaRigidRegistration.git/blob - lib/Transparency.cxx
#3221 creaRigidRegistration Feature New Normal - vtk8itk4wx3-mingw64
[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 printf("EED  Transparency::calculateImage Start\n");
56         if(_image1!=NULL && _image2!=NULL)
57         {
58 printf("EED  Transparency::calculateImage 1\n");
59
60                 // IMAGE 1
61                         
62                 // Information from image1
63                 _image1->GetSpacing(spcImg1);
64                 _image1->GetExtent(extImg1);
65                 _type = _image1->GetScalarType();
66                 // The Z dimension is 1 for a 2D image
67                 dimImg1[0] = extImg1[1] - extImg1[0]+1;
68                 dimImg1[1] = extImg1[3] - extImg1[2]+1;
69                 dimImg1[2] = 1;
70                 //IMAGE 2
71                 // Information from image2
72                 _image2->GetSpacing(spcImg2);
73                 _image2->GetExtent(extImg2);
74                 // The Z dimension is 1 for a 2D image
75                 dimImg2[0] = extImg2[1] - extImg2[0]+1;
76                 dimImg2[1] = extImg2[3] - extImg2[2]+1;
77                 dimImg2[2] = 1;
78                 long numPixelsImg1 = dimImg1[0]*dimImg1[1];
79                 long numPixelsImg2 = dimImg2[0]*dimImg2[1];             
80                 double factorX = 1;
81                 double factorY = 1;
82                 vtkImageResample *resample = vtkImageResample::New();
83                 vtkImageData *result;
84 printf("EED  Transparency::calculateImage 2\n");
85                 if(numPixelsImg1<numPixelsImg2)
86                 {
87 //EED 2017-01-01 Migration VTK7
88 #if VTK_MAJOR_VERSION <= 5
89                         resample->SetInput(_image1);
90 #else
91                         resample->SetInputData(_image1);
92 #endif
93                         factorX = (double)extImg2[1]/extImg1[1];
94                         factorY = (double)extImg2[3]/extImg1[3];
95                         resample->SetAxisMagnificationFactor(0,factorX);
96                         resample->SetAxisMagnificationFactor(1,factorY);
97                         resample->SetInformationInput(_image2);
98                         initialize(dimImg2, spcImg2);
99 //EED 2017-01-01 Migration VTK7
100 #if VTK_MAJOR_VERSION <= 5
101                         //..
102 #else
103                         resample->Update();
104 #endif
105 printf("EED  Transparency::calculateImage 3\n");
106                         result = resample->GetOutput();
107 //EED 2017-01-01 Migration VTK7
108 #if VTK_MAJOR_VERSION <= 5
109                         result->Update();
110 #else
111                         //...
112 #endif
113                         createImage(result,_image2,dimImg2[0],dimImg2[1]);              
114                 } else if (numPixelsImg1>numPixelsImg2) {
115 printf("EED  Transparency::calculateImage 4\n");
116 //EED 2017-01-01 Migration VTK7
117 #if VTK_MAJOR_VERSION <= 5
118                         resample->SetInput(_image2);
119 #else
120                         resample->SetInputData(_image2);
121 #endif
122                         factorX = (double)extImg1[1]/extImg2[1];
123                         factorY = (double)extImg1[3]/extImg2[3];
124                         resample->SetAxisMagnificationFactor(0,factorX);
125                         resample->SetAxisMagnificationFactor(1,factorY);
126                         resample->SetInformationInput(_image1);
127                         initialize(dimImg1, spcImg1);
128 //EED 2017-01-01 Migration VTK7
129 #if VTK_MAJOR_VERSION <= 5
130                         // ...
131 #else
132                         resample->Update();
133 #endif
134                         result = resample->GetOutput();
135                         
136 //EED 2017-01-01 Migration VTK7
137 #if VTK_MAJOR_VERSION <= 5
138                         result->Update();
139 #else
140                         result->Modified();
141 #endif
142 printf("EED  Transparency::calculateImage 4.1\n");
143                         createImage(_image1,result,dimImg1[0],dimImg1[1]);
144 printf("EED  Transparency::calculateImage 4.2\n");
145                 } else {
146 printf("EED  Transparency::calculateImage 5\n");
147                         //If both images have the same number of pixels, the resultant image will have the 
148                         //properties of the first image.
149 //EED 2017-01-01 Migration VTK7
150 #if VTK_MAJOR_VERSION <= 5
151                         resample->SetInput(_image2);
152 #else
153                         resample->SetInputData(_image2);
154 #endif
155                         factorX = (double)extImg1[1]/extImg2[1];
156                         factorY = (double)extImg1[3]/extImg2[3];
157                         resample->SetAxisMagnificationFactor(0,factorX);
158                         resample->SetAxisMagnificationFactor(1,factorY);
159                         resample->SetInformationInput(_image1);
160                         initialize(dimImg1, spcImg1);
161 //EED 2017-01-01 Migration VTK7
162 #if VTK_MAJOR_VERSION <= 5
163                         // ...
164 #else
165                         resample->Update();
166 #endif
167                         result = resample->GetOutput();
168 //EED 2017-01-01 Migration VTK7
169 #if VTK_MAJOR_VERSION <= 5
170                         result->Update();
171 #else
172                         result->Modified();
173 #endif
174                         createImage(_image1,result,dimImg1[0],dimImg1[1]);
175 printf("EED  Transparency::calculateImage 6\n");
176                 } //else
177                 resample->Delete();             
178                 _processed=true;
179 printf("EED  Transparency::calculateImage 7\n");
180         }
181
182 printf("EED  Transparency::calculateImage End\n");
183
184 }
185
186 void Transparency::initialize(int dimensions[], double spacing[])
187 {
188         // Setting the new image
189         _newImage = vtkImageData::New();
190         _newImage->SetSpacing(spacing);
191         _newImage->SetDimensions(dimensions);
192 //EED 2017-01-01 Migration VTK7
193 #if VTK_MAJOR_VERSION <= 5
194         _newImage->SetScalarType(_type);
195         _newImage->AllocateScalars();
196         _newImage->Update();
197 #else
198         _newImage->AllocateScalars(_type,1);
199 #endif
200 }
201
202 void Transparency::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
203 {
204 printf("EED  Transparency::createImage Start\n");
205 //      int i, j;  EED ???
206         if(_type == VTK_CHAR)
207         {
208 printf("EED  Transparency::createImage 1\n");
209                 //POINTERS: 
210                 char* dataImagePointer1                 = NULL;
211                 char* dataImagePointer2                 = NULL;
212                 char* dataImageResultPointer    = NULL;
213                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
214         }
215         else if(_type == VTK_SIGNED_CHAR)
216         {
217 printf("EED  Transparency::createImage 2\n");
218                 //POINTERS: 
219                 signed char* dataImagePointer1          = NULL;
220                 signed char* dataImagePointer2          = NULL;
221                 signed char* dataImageResultPointer = NULL;
222         
223                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
224         }
225         else if(_type == VTK_UNSIGNED_CHAR)
226         {
227 printf("EED  Transparency::createImage 3\n");
228                 //POINTERS: 
229                 unsigned char* dataImagePointer1                = NULL;
230                 unsigned char* dataImagePointer2                = NULL;
231                 unsigned char* dataImageResultPointer   = NULL;
232         
233                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
234         }
235         else if(_type == VTK_SHORT)
236         {
237 printf("EED  Transparency::createImage 4\n");
238                 //POINTERS: 
239                 short* dataImagePointer1                = NULL;
240                 short* dataImagePointer2                = NULL;
241                 short* dataImageResultPointer   = NULL;
242         
243                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
244         }
245         else if(_type == VTK_UNSIGNED_SHORT)
246         {
247 printf("EED  Transparency::createImage 5\n");
248                 //POINTERS: 
249                 unsigned short* dataImagePointer1               = NULL;
250                 unsigned short* dataImagePointer2               = NULL;
251                 unsigned short* dataImageResultPointer  = NULL;
252         
253                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
254         }
255         else if(_type == VTK_INT)
256         {
257 printf("EED  Transparency::createImage 6\n");
258                 //POINTERS: 
259                 int* dataImagePointer1 = NULL;
260                 int* dataImagePointer2 = NULL;
261                 int* dataImageResultPointer = NULL;
262         
263                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
264         }
265         else if(_type == VTK_UNSIGNED_INT)
266         {
267 printf("EED  Transparency::createImage 7\n");
268                 //POINTERS: 
269                 unsigned int* dataImagePointer1 = NULL;
270                 unsigned int* dataImagePointer2 = NULL;
271                 unsigned int* dataImageResultPointer = NULL;
272         
273                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
274         }
275         else if(_type == VTK_LONG)
276         {
277 printf("EED  Transparency::createImage 8\n");
278                 //POINTERS: 
279                 long* dataImagePointer1 = NULL;
280                 long* dataImagePointer2 = NULL;
281                 long* dataImageResultPointer = NULL;
282         
283                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
284         }
285         else if(_type == VTK_UNSIGNED_LONG)
286         {
287 printf("EED  Transparency::createImage 9\n");
288                 //POINTERS: 
289                 unsigned long* dataImagePointer1                = NULL;
290                 unsigned long* dataImagePointer2                = NULL;
291                 unsigned long* dataImageResultPointer   = NULL;
292         
293                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);              
294         }
295         else if(_type == VTK_FLOAT)
296         {
297 printf("EED  Transparency::createImage 10\n");
298                 //POINTERS: 
299                 float* dataImagePointer1                = NULL;
300                 float* dataImagePointer2                = NULL;
301                 float* dataImageResultPointer   = NULL;
302         
303                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
304         }
305         else if(_type == VTK_DOUBLE)
306         {
307 printf("EED  Transparency::createImage 11\n");
308                 //POINTERS: 
309                 double* dataImagePointer1               = NULL;
310                 double* dataImagePointer2               = NULL;
311                 double* dataImageResultPointer  = NULL;
312
313                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
314         }
315
316 //EED 2017-01-01 Migration VTK7
317 #if VTK_MAJOR_VERSION <= 5
318         _newImage->Update();
319 #else
320         _newImage->Modified();
321 #endif
322 printf("EED  Transparency::createImage End\n");
323 }
324
325 template <class T>
326 void Transparency::createImageByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
327 {
328 printf("EED  Transparency::createImageByType Start %d %d\n", sizeX, sizeY);
329         int i, j;
330         dataImagePointer1 = (T*) img1->GetScalarPointer(0,0,0);
331         dataImagePointer2 = (T*) img2->GetScalarPointer(0,0,0);
332         dataImageResultPointer = (T*) _newImage->GetScalarPointer(0,0,0);
333 printf("EED  Transparency::createImageByType 0 %d %d\n", sizeX, sizeY);
334         for(i = 0; i < sizeX; i++)
335         {
336                 for(j = 0; j < sizeY; j++)
337                 {
338 printf("EED  Transparency::createImageByType 2 %d %d\n", sizeX, sizeY);
339                         dataImagePointer1 = (T*)img1->GetScalarPointer(i,j,0);
340 printf("EED  Transparency::createImageByType 3 %d %d\n", sizeX, sizeY);
341                         dataImagePointer2 = (T*)img2->GetScalarPointer(i,j,0);
342 printf("EED  Transparency::createImageByType 4 %d %d\n", sizeX, sizeY);
343                         dataImageResultPointer = (T*)_newImage->GetScalarPointer(i,j,0);
344 printf("EED  Transparency::createImageByType 5 %d %d\n", sizeX, sizeY);
345                         *dataImageResultPointer = (T)*dataImagePointer1*_percent + (T)*dataImagePointer2*(1-_percent);                          
346 printf("EED  Transparency::createImageByType 6 %d %d\n", sizeX, sizeY);
347                 } // for h
348         } // for i
349 printf("EED  Transparency::createImageByType End\n");
350 }
351         
352 /*
353 * Get new image
354 */
355 //------------------------------------------------------------
356 vtkImageData* Transparency::getImage()
357 {
358         if(_processed){
359                 return _newImage;
360         }
361         else{
362                 return NULL;
363         }
364 }
365
366 //------------------------------------------------------------
367 void Transparency::setImage1(vtkImageData *image)
368 {
369         _image1=image;
370 }
371
372 //------------------------------------------------------------
373 void Transparency::setImage2(vtkImageData *image)
374 {
375         _image2=image;
376 }
377
378 //------------------------------------------------------------
379 void Transparency::setPercent(int percent)
380 {
381     _percent=(double)percent/100.0;     
382 }
383