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 if(_image1!=NULL && _image2!=NULL)
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;
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;
75 long numPixelsImg1 = dimImg1[0]*dimImg1[1];
76 long numPixelsImg2 = dimImg2[0]*dimImg2[1];
79 vtkImageResample *resample = vtkImageResample::New();
81 if(numPixelsImg1<numPixelsImg2)
83 //EED 2017-01-01 Migration VTK7
84 #if VTK_MAJOR_VERSION <= 5
85 resample->SetInput(_image1);
87 resample->SetInputData(_image1);
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
101 result = resample->GetOutput();
102 //EED 2017-01-01 Migration VTK7
103 #if VTK_MAJOR_VERSION <= 5
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);
114 resample->SetInputData(_image2);
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
128 result = resample->GetOutput();
130 //EED 2017-01-01 Migration VTK7
131 #if VTK_MAJOR_VERSION <= 5
136 createImage(_image1,result,dimImg1[0],dimImg1[1]);
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);
144 resample->SetInputData(_image2);
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
158 result = resample->GetOutput();
159 //EED 2017-01-01 Migration VTK7
160 #if VTK_MAJOR_VERSION <= 5
165 createImage(_image1,result,dimImg1[0],dimImg1[1]);
172 void Transparency::initialize(int dimensions[], double spacing[])
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();
184 _newImage->AllocateScalars(_type,1);
188 void Transparency::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
190 if(_type == VTK_CHAR)
193 char* dataImagePointer1 = NULL;
194 char* dataImagePointer2 = NULL;
195 char* dataImageResultPointer = NULL;
196 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
198 else if(_type == VTK_SIGNED_CHAR)
201 signed char* dataImagePointer1 = NULL;
202 signed char* dataImagePointer2 = NULL;
203 signed char* dataImageResultPointer = NULL;
205 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
207 else if(_type == VTK_UNSIGNED_CHAR)
210 unsigned char* dataImagePointer1 = NULL;
211 unsigned char* dataImagePointer2 = NULL;
212 unsigned char* dataImageResultPointer = NULL;
214 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
216 else if(_type == VTK_SHORT)
219 short* dataImagePointer1 = NULL;
220 short* dataImagePointer2 = NULL;
221 short* dataImageResultPointer = NULL;
223 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
225 else if(_type == VTK_UNSIGNED_SHORT)
228 unsigned short* dataImagePointer1 = NULL;
229 unsigned short* dataImagePointer2 = NULL;
230 unsigned short* dataImageResultPointer = NULL;
232 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
234 else if(_type == VTK_INT)
237 int* dataImagePointer1 = NULL;
238 int* dataImagePointer2 = NULL;
239 int* dataImageResultPointer = NULL;
241 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
243 else if(_type == VTK_UNSIGNED_INT)
246 unsigned int* dataImagePointer1 = NULL;
247 unsigned int* dataImagePointer2 = NULL;
248 unsigned int* dataImageResultPointer = NULL;
250 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
252 else if(_type == VTK_LONG)
255 long* dataImagePointer1 = NULL;
256 long* dataImagePointer2 = NULL;
257 long* dataImageResultPointer = NULL;
259 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
261 else if(_type == VTK_UNSIGNED_LONG)
264 unsigned long* dataImagePointer1 = NULL;
265 unsigned long* dataImagePointer2 = NULL;
266 unsigned long* dataImageResultPointer = NULL;
268 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
270 else if(_type == VTK_FLOAT)
273 float* dataImagePointer1 = NULL;
274 float* dataImagePointer2 = NULL;
275 float* dataImageResultPointer = NULL;
277 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
279 else if(_type == VTK_DOUBLE)
282 double* dataImagePointer1 = NULL;
283 double* dataImagePointer2 = NULL;
284 double* dataImageResultPointer = NULL;
286 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
289 //EED 2017-01-01 Migration VTK7
290 #if VTK_MAJOR_VERSION <= 5
293 _newImage->Modified();
298 void Transparency::createImageByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
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++)
306 for(j = 0; j < sizeY; j++)
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);
319 //------------------------------------------------------------
320 vtkImageData* Transparency::getImage()
330 //------------------------------------------------------------
331 void Transparency::setImage1(vtkImageData *image)
336 //------------------------------------------------------------
337 void Transparency::setImage2(vtkImageData *image)
342 //------------------------------------------------------------
343 void Transparency::setPercent(int percent)
345 _percent=(double)percent/100.0;