1 #include "Transparency.h"
7 //------------------------------------------------------------
8 Transparency::Transparency()
20 //------------------------------------------------------------
21 Transparency::~Transparency()
23 if (_newImage != NULL ) { _newImage->Delete(); }
26 //------------------------------------------------------------
27 void Transparency::calculateImage()
30 if(_image1!=NULL && _image2!=NULL)
35 // Information from image1
36 _image1->GetSpacing(spcImg1);
37 _image1->GetExtent(extImg1);
38 _type = _image1->GetScalarType();
40 // The Z dimension is 1 for a 2D image
41 dimImg1[0] = extImg1[1] - extImg1[0]+1;
42 dimImg1[1] = extImg1[3] - extImg1[2]+1;
47 // Information from image2
48 _image2->GetSpacing(spcImg2);
49 _image2->GetExtent(extImg2);
51 // The Z dimension is 1 for a 2D image
52 dimImg2[0] = extImg2[1] - extImg2[0]+1;
53 dimImg2[1] = extImg2[3] - extImg2[2]+1;
56 long numPixelsImg1 = dimImg1[0]*dimImg1[1];
57 long numPixelsImg2 = dimImg2[0]*dimImg2[1];
61 vtkImageResample *resample = vtkImageResample::New();
63 if(numPixelsImg1<numPixelsImg2)
65 resample->SetInput(_image1);
66 factorX = (double)extImg2[1]/extImg1[1];
67 factorY = (double)extImg2[3]/extImg1[3];
68 resample->SetAxisMagnificationFactor(0,factorX);
69 resample->SetAxisMagnificationFactor(1,factorY);
70 resample->SetInformationInput(_image2);
72 initialize(dimImg2, spcImg2);
73 result = resample->GetOutput();
75 createImage(result,_image2,dimImg2[0],dimImg2[1]);
78 else if (numPixelsImg1>numPixelsImg2)
80 resample->SetInput(_image2);
81 factorX = (double)extImg1[1]/extImg2[1];
82 factorY = (double)extImg1[3]/extImg2[3];
83 resample->SetAxisMagnificationFactor(0,factorX);
84 resample->SetAxisMagnificationFactor(1,factorY);
85 resample->SetInformationInput(_image1);
87 initialize(dimImg1, spcImg1);
88 result = resample->GetOutput();
92 createImage(_image1,result,dimImg1[0],dimImg1[1]);
96 //If both images have the same number of pixels, the resultant image will have the
97 //properties of the first image.
98 resample->SetInput(_image2);
99 factorX = (double)extImg1[1]/extImg2[1];
100 factorY = (double)extImg1[3]/extImg2[3];
101 resample->SetAxisMagnificationFactor(0,factorX);
102 resample->SetAxisMagnificationFactor(1,factorY);
103 resample->SetInformationInput(_image1);
105 initialize(dimImg1, spcImg1);
107 result = resample->GetOutput();
110 createImage(_image1,result,dimImg1[0],dimImg1[1]);
119 void Transparency::initialize(int dimensions[], double spacing[])
121 // Setting the new image
122 _newImage = vtkImageData::New();
123 _newImage->SetScalarType(_type);
124 _newImage->SetSpacing(spacing);
125 _newImage->SetDimensions(dimensions);
126 _newImage->AllocateScalars();
131 void Transparency::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
134 if(_type == VTK_CHAR)
137 char* dataImagePointer1 = NULL;
138 char* dataImagePointer2 = NULL;
139 char* dataImageResultPointer = NULL;
141 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
143 else if(_type == VTK_SIGNED_CHAR)
146 signed char* dataImagePointer1 = NULL;
147 signed char* dataImagePointer2 = NULL;
148 signed char* dataImageResultPointer = NULL;
150 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
152 else if(_type == VTK_UNSIGNED_CHAR)
155 unsigned char* dataImagePointer1 = NULL;
156 unsigned char* dataImagePointer2 = NULL;
157 unsigned char* dataImageResultPointer = NULL;
159 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
161 else if(_type == VTK_SHORT)
164 short* dataImagePointer1 = NULL;
165 short* dataImagePointer2 = NULL;
166 short* dataImageResultPointer = NULL;
168 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
170 else if(_type == VTK_UNSIGNED_SHORT)
173 unsigned short* dataImagePointer1 = NULL;
174 unsigned short* dataImagePointer2 = NULL;
175 unsigned short* dataImageResultPointer = NULL;
177 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
179 else if(_type == VTK_INT)
182 int* dataImagePointer1 = NULL;
183 int* dataImagePointer2 = NULL;
184 int* dataImageResultPointer = NULL;
186 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
188 else if(_type == VTK_UNSIGNED_INT)
191 unsigned int* dataImagePointer1 = NULL;
192 unsigned int* dataImagePointer2 = NULL;
193 unsigned int* dataImageResultPointer = NULL;
195 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
197 else if(_type == VTK_LONG)
200 long* dataImagePointer1 = NULL;
201 long* dataImagePointer2 = NULL;
202 long* dataImageResultPointer = NULL;
204 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
206 else if(_type == VTK_UNSIGNED_LONG)
209 unsigned long* dataImagePointer1 = NULL;
210 unsigned long* dataImagePointer2 = NULL;
211 unsigned long* dataImageResultPointer = NULL;
213 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
215 else if(_type == VTK_FLOAT)
218 float* dataImagePointer1 = NULL;
219 float* dataImagePointer2 = NULL;
220 float* dataImageResultPointer = NULL;
222 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
224 else if(_type == VTK_DOUBLE)
227 double* dataImagePointer1 = NULL;
228 double* dataImagePointer2 = NULL;
229 double* dataImageResultPointer = NULL;
231 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
238 void Transparency::createImageByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
242 dataImagePointer1 = (T*) img1->GetScalarPointer(0,0,0);
243 dataImagePointer2 = (T*) img2->GetScalarPointer(0,0,0);
244 dataImageResultPointer = (T*) _newImage->GetScalarPointer(0,0,0);
245 for(i = 0; i < sizeX; i++)
247 for(j = 0; j < sizeY; j++)
249 dataImagePointer1 = (T*)img1->GetScalarPointer(i,j,0);
250 dataImagePointer2 = (T*)img2->GetScalarPointer(i,j,0);
251 dataImageResultPointer = (T*)_newImage->GetScalarPointer(i,j,0);
253 *dataImageResultPointer = (T)*dataImagePointer1*_percent + (T)*dataImagePointer2*(1-_percent);
261 //------------------------------------------------------------
262 vtkImageData* Transparency::getImage()
272 //------------------------------------------------------------
273 void Transparency::setImage1(vtkImageData *image)
278 //------------------------------------------------------------
279 void Transparency::setImage2(vtkImageData *image)
284 //------------------------------------------------------------
285 void Transparency::setPercent(int percent)
287 _percent=(double)percent/100.0;