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()
56 if(_image1!=NULL && _image2!=NULL)
61 // Information from image1
62 _image1->GetSpacing(spcImg1);
63 _image1->GetExtent(extImg1);
64 _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;
73 // Information from image2
74 _image2->GetSpacing(spcImg2);
75 _image2->GetExtent(extImg2);
77 // The Z dimension is 1 for a 2D image
78 dimImg2[0] = extImg2[1] - extImg2[0]+1;
79 dimImg2[1] = extImg2[3] - extImg2[2]+1;
82 long numPixelsImg1 = dimImg1[0]*dimImg1[1];
83 long numPixelsImg2 = dimImg2[0]*dimImg2[1];
87 vtkImageResample *resample = vtkImageResample::New();
89 if(numPixelsImg1<numPixelsImg2)
91 //EED 2017-01-01 Migration VTK7
92 #if VTK_MAJOR_VERSION <= 5
93 resample->SetInput(_image1);
95 resample->SetInputData(_image1);
97 factorX = (double)extImg2[1]/extImg1[1];
98 factorY = (double)extImg2[3]/extImg1[3];
99 resample->SetAxisMagnificationFactor(0,factorX);
100 resample->SetAxisMagnificationFactor(1,factorY);
101 resample->SetInformationInput(_image2);
103 initialize(dimImg2, spcImg2);
104 //EED 2017-01-01 Migration VTK7
105 #if VTK_MAJOR_VERSION <= 5
111 result = resample->GetOutput();
112 //EED 2017-01-01 Migration VTK7
113 #if VTK_MAJOR_VERSION <= 5
118 createImage(result,_image2,dimImg2[0],dimImg2[1]);
121 else if (numPixelsImg1>numPixelsImg2)
123 //EED 2017-01-01 Migration VTK7
124 #if VTK_MAJOR_VERSION <= 5
125 resample->SetInput(_image2);
127 resample->SetInputData(_image2);
129 factorX = (double)extImg1[1]/extImg2[1];
130 factorY = (double)extImg1[3]/extImg2[3];
131 resample->SetAxisMagnificationFactor(0,factorX);
132 resample->SetAxisMagnificationFactor(1,factorY);
133 resample->SetInformationInput(_image1);
135 initialize(dimImg1, spcImg1);
137 //EED 2017-01-01 Migration VTK7
138 #if VTK_MAJOR_VERSION <= 5
143 result = resample->GetOutput();
145 //EED 2017-01-01 Migration VTK7
146 #if VTK_MAJOR_VERSION <= 5
152 createImage(_image1,result,dimImg1[0],dimImg1[1]);
156 //If both images have the same number of pixels, the resultant image will have the
157 //properties of the first image.
158 //EED 2017-01-01 Migration VTK7
159 #if VTK_MAJOR_VERSION <= 5
160 resample->SetInput(_image2);
162 resample->SetInputData(_image2);
164 factorX = (double)extImg1[1]/extImg2[1];
165 factorY = (double)extImg1[3]/extImg2[3];
166 resample->SetAxisMagnificationFactor(0,factorX);
167 resample->SetAxisMagnificationFactor(1,factorY);
168 resample->SetInformationInput(_image1);
169 initialize(dimImg1, spcImg1);
170 //EED 2017-01-01 Migration VTK7
171 #if VTK_MAJOR_VERSION <= 5
177 result = resample->GetOutput();
179 //EED 2017-01-01 Migration VTK7
180 #if VTK_MAJOR_VERSION <= 5
186 createImage(_image1,result,dimImg1[0],dimImg1[1]);
195 void Transparency::initialize(int dimensions[], double spacing[])
197 // Setting the new image
198 _newImage = vtkImageData::New();
199 _newImage->SetSpacing(spacing);
200 _newImage->SetDimensions(dimensions);
201 //EED 2017-01-01 Migration VTK7
202 #if VTK_MAJOR_VERSION <= 5
203 _newImage->SetScalarType(_type);
204 _newImage->AllocateScalars();
207 _newImage->AllocateScalars(_type,1);
213 void Transparency::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
217 if(_type == VTK_CHAR)
220 char* dataImagePointer1 = NULL;
221 char* dataImagePointer2 = NULL;
222 char* dataImageResultPointer = NULL;
224 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
226 else if(_type == VTK_SIGNED_CHAR)
229 signed char* dataImagePointer1 = NULL;
230 signed char* dataImagePointer2 = NULL;
231 signed char* dataImageResultPointer = NULL;
233 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
235 else if(_type == VTK_UNSIGNED_CHAR)
238 unsigned char* dataImagePointer1 = NULL;
239 unsigned char* dataImagePointer2 = NULL;
240 unsigned char* dataImageResultPointer = NULL;
242 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
244 else if(_type == VTK_SHORT)
247 short* dataImagePointer1 = NULL;
248 short* dataImagePointer2 = NULL;
249 short* dataImageResultPointer = NULL;
251 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
253 else if(_type == VTK_UNSIGNED_SHORT)
256 unsigned short* dataImagePointer1 = NULL;
257 unsigned short* dataImagePointer2 = NULL;
258 unsigned short* dataImageResultPointer = NULL;
260 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
262 else if(_type == VTK_INT)
265 int* dataImagePointer1 = NULL;
266 int* dataImagePointer2 = NULL;
267 int* dataImageResultPointer = NULL;
269 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
271 else if(_type == VTK_UNSIGNED_INT)
274 unsigned int* dataImagePointer1 = NULL;
275 unsigned int* dataImagePointer2 = NULL;
276 unsigned int* dataImageResultPointer = NULL;
278 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
280 else if(_type == VTK_LONG)
283 long* dataImagePointer1 = NULL;
284 long* dataImagePointer2 = NULL;
285 long* dataImageResultPointer = NULL;
287 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
289 else if(_type == VTK_UNSIGNED_LONG)
292 unsigned long* dataImagePointer1 = NULL;
293 unsigned long* dataImagePointer2 = NULL;
294 unsigned long* dataImageResultPointer = NULL;
296 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
298 else if(_type == VTK_FLOAT)
301 float* dataImagePointer1 = NULL;
302 float* dataImagePointer2 = NULL;
303 float* dataImageResultPointer = NULL;
305 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
307 else if(_type == VTK_DOUBLE)
310 double* dataImagePointer1 = NULL;
311 double* dataImagePointer2 = NULL;
312 double* dataImageResultPointer = NULL;
314 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
317 //EED 2017-01-01 Migration VTK7
318 #if VTK_MAJOR_VERSION <= 5
321 _newImage->Modified();
328 void Transparency::createImageByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
332 dataImagePointer1 = (T*) img1->GetScalarPointer(0,0,0);
333 dataImagePointer2 = (T*) img2->GetScalarPointer(0,0,0);
334 dataImageResultPointer = (T*) _newImage->GetScalarPointer(0,0,0);
335 for(i = 0; i < sizeX; i++)
337 for(j = 0; j < sizeY; j++)
339 dataImagePointer1 = (T*)img1->GetScalarPointer(i,j,0);
340 dataImagePointer2 = (T*)img2->GetScalarPointer(i,j,0);
341 dataImageResultPointer = (T*)_newImage->GetScalarPointer(i,j,0);
343 *dataImageResultPointer = (T)*dataImagePointer1*_percent + (T)*dataImagePointer2*(1-_percent);
351 //------------------------------------------------------------
352 vtkImageData* Transparency::getImage()
362 //------------------------------------------------------------
363 void Transparency::setImage1(vtkImageData *image)
368 //------------------------------------------------------------
369 void Transparency::setImage2(vtkImageData *image)
374 //------------------------------------------------------------
375 void Transparency::setPercent(int percent)
377 _percent=(double)percent/100.0;