2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------ */
27 #include "Transparency.h"
33 //------------------------------------------------------------
34 Transparency::Transparency()
46 //------------------------------------------------------------
47 Transparency::~Transparency()
49 if (_newImage != NULL ) { _newImage->Delete(); }
52 //------------------------------------------------------------
53 void Transparency::calculateImage()
55 printf("EED Transparency::calculateImage Start\n");
56 if(_image1!=NULL && _image2!=NULL)
58 printf("EED Transparency::calculateImage 1\n");
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;
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;
78 long numPixelsImg1 = dimImg1[0]*dimImg1[1];
79 long numPixelsImg2 = dimImg2[0]*dimImg2[1];
82 vtkImageResample *resample = vtkImageResample::New();
84 printf("EED Transparency::calculateImage 2\n");
85 if(numPixelsImg1<numPixelsImg2)
87 //EED 2017-01-01 Migration VTK7
88 #if VTK_MAJOR_VERSION <= 5
89 resample->SetInput(_image1);
91 resample->SetInputData(_image1);
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
105 printf("EED Transparency::calculateImage 3\n");
106 result = resample->GetOutput();
107 //EED 2017-01-01 Migration VTK7
108 #if VTK_MAJOR_VERSION <= 5
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);
120 resample->SetInputData(_image2);
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
134 result = resample->GetOutput();
136 //EED 2017-01-01 Migration VTK7
137 #if VTK_MAJOR_VERSION <= 5
142 printf("EED Transparency::calculateImage 4.1\n");
143 createImage(_image1,result,dimImg1[0],dimImg1[1]);
144 printf("EED Transparency::calculateImage 4.2\n");
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);
153 resample->SetInputData(_image2);
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
167 result = resample->GetOutput();
168 //EED 2017-01-01 Migration VTK7
169 #if VTK_MAJOR_VERSION <= 5
174 createImage(_image1,result,dimImg1[0],dimImg1[1]);
175 printf("EED Transparency::calculateImage 6\n");
179 printf("EED Transparency::calculateImage 7\n");
182 printf("EED Transparency::calculateImage End\n");
186 void Transparency::initialize(int dimensions[], double spacing[])
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();
198 _newImage->AllocateScalars(_type,1);
202 void Transparency::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
204 printf("EED Transparency::createImage Start\n");
206 if(_type == VTK_CHAR)
208 printf("EED Transparency::createImage 1\n");
210 char* dataImagePointer1 = NULL;
211 char* dataImagePointer2 = NULL;
212 char* dataImageResultPointer = NULL;
213 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
215 else if(_type == VTK_SIGNED_CHAR)
217 printf("EED Transparency::createImage 2\n");
219 signed char* dataImagePointer1 = NULL;
220 signed char* dataImagePointer2 = NULL;
221 signed char* dataImageResultPointer = NULL;
223 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
225 else if(_type == VTK_UNSIGNED_CHAR)
227 printf("EED Transparency::createImage 3\n");
229 unsigned char* dataImagePointer1 = NULL;
230 unsigned char* dataImagePointer2 = NULL;
231 unsigned char* dataImageResultPointer = NULL;
233 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
235 else if(_type == VTK_SHORT)
237 printf("EED Transparency::createImage 4\n");
239 short* dataImagePointer1 = NULL;
240 short* dataImagePointer2 = NULL;
241 short* dataImageResultPointer = NULL;
243 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
245 else if(_type == VTK_UNSIGNED_SHORT)
247 printf("EED Transparency::createImage 5\n");
249 unsigned short* dataImagePointer1 = NULL;
250 unsigned short* dataImagePointer2 = NULL;
251 unsigned short* dataImageResultPointer = NULL;
253 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
255 else if(_type == VTK_INT)
257 printf("EED Transparency::createImage 6\n");
259 int* dataImagePointer1 = NULL;
260 int* dataImagePointer2 = NULL;
261 int* dataImageResultPointer = NULL;
263 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
265 else if(_type == VTK_UNSIGNED_INT)
267 printf("EED Transparency::createImage 7\n");
269 unsigned int* dataImagePointer1 = NULL;
270 unsigned int* dataImagePointer2 = NULL;
271 unsigned int* dataImageResultPointer = NULL;
273 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
275 else if(_type == VTK_LONG)
277 printf("EED Transparency::createImage 8\n");
279 long* dataImagePointer1 = NULL;
280 long* dataImagePointer2 = NULL;
281 long* dataImageResultPointer = NULL;
283 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
285 else if(_type == VTK_UNSIGNED_LONG)
287 printf("EED Transparency::createImage 9\n");
289 unsigned long* dataImagePointer1 = NULL;
290 unsigned long* dataImagePointer2 = NULL;
291 unsigned long* dataImageResultPointer = NULL;
293 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
295 else if(_type == VTK_FLOAT)
297 printf("EED Transparency::createImage 10\n");
299 float* dataImagePointer1 = NULL;
300 float* dataImagePointer2 = NULL;
301 float* dataImageResultPointer = NULL;
303 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
305 else if(_type == VTK_DOUBLE)
307 printf("EED Transparency::createImage 11\n");
309 double* dataImagePointer1 = NULL;
310 double* dataImagePointer2 = NULL;
311 double* dataImageResultPointer = NULL;
313 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
316 //EED 2017-01-01 Migration VTK7
317 #if VTK_MAJOR_VERSION <= 5
320 _newImage->Modified();
322 printf("EED Transparency::createImage End\n");
326 void Transparency::createImageByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
328 printf("EED Transparency::createImageByType Start %d %d\n", sizeX, sizeY);
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++)
336 for(j = 0; j < sizeY; j++)
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);
349 printf("EED Transparency::createImageByType End\n");
355 //------------------------------------------------------------
356 vtkImageData* Transparency::getImage()
366 //------------------------------------------------------------
367 void Transparency::setImage1(vtkImageData *image)
372 //------------------------------------------------------------
373 void Transparency::setImage2(vtkImageData *image)
378 //------------------------------------------------------------
379 void Transparency::setPercent(int percent)
381 _percent=(double)percent/100.0;