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)
135 if(_type == VTK_CHAR)
138 char* dataImagePointer1 = NULL;
139 char* dataImagePointer2 = NULL;
140 char* dataImageResultPointer = NULL;
142 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
144 else if(_type == VTK_SIGNED_CHAR)
147 signed char* dataImagePointer1 = NULL;
148 signed char* dataImagePointer2 = NULL;
149 signed char* dataImageResultPointer = NULL;
151 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
153 else if(_type == VTK_UNSIGNED_CHAR)
156 unsigned char* dataImagePointer1 = NULL;
157 unsigned char* dataImagePointer2 = NULL;
158 unsigned char* dataImageResultPointer = NULL;
160 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
162 else if(_type == VTK_SHORT)
165 short* dataImagePointer1 = NULL;
166 short* dataImagePointer2 = NULL;
167 short* dataImageResultPointer = NULL;
169 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
171 else if(_type == VTK_UNSIGNED_SHORT)
174 unsigned short* dataImagePointer1 = NULL;
175 unsigned short* dataImagePointer2 = NULL;
176 unsigned short* dataImageResultPointer = NULL;
178 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
180 else if(_type == VTK_INT)
183 int* dataImagePointer1 = NULL;
184 int* dataImagePointer2 = NULL;
185 int* dataImageResultPointer = NULL;
187 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
189 else if(_type == VTK_UNSIGNED_INT)
192 unsigned int* dataImagePointer1 = NULL;
193 unsigned int* dataImagePointer2 = NULL;
194 unsigned int* dataImageResultPointer = NULL;
196 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
198 else if(_type == VTK_LONG)
201 long* dataImagePointer1 = NULL;
202 long* dataImagePointer2 = NULL;
203 long* dataImageResultPointer = NULL;
205 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
207 else if(_type == VTK_UNSIGNED_LONG)
210 unsigned long* dataImagePointer1 = NULL;
211 unsigned long* dataImagePointer2 = NULL;
212 unsigned long* dataImageResultPointer = NULL;
214 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
216 else if(_type == VTK_FLOAT)
219 float* dataImagePointer1 = NULL;
220 float* dataImagePointer2 = NULL;
221 float* dataImageResultPointer = NULL;
223 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
225 else if(_type == VTK_DOUBLE)
228 double* dataImagePointer1 = NULL;
229 double* dataImagePointer2 = NULL;
230 double* dataImageResultPointer = NULL;
232 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
239 void Transparency::createImageByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
243 dataImagePointer1 = (T*) img1->GetScalarPointer(0,0,0);
244 dataImagePointer2 = (T*) img2->GetScalarPointer(0,0,0);
245 dataImageResultPointer = (T*) _newImage->GetScalarPointer(0,0,0);
246 for(i = 0; i < sizeX; i++)
248 for(j = 0; j < sizeY; j++)
250 dataImagePointer1 = (T*)img1->GetScalarPointer(i,j,0);
251 dataImagePointer2 = (T*)img2->GetScalarPointer(i,j,0);
252 dataImageResultPointer = (T*)_newImage->GetScalarPointer(i,j,0);
254 *dataImageResultPointer = (T)*dataImagePointer1*_percent + (T)*dataImagePointer2*(1-_percent);
262 //------------------------------------------------------------
263 vtkImageData* Transparency::getImage()
273 //------------------------------------------------------------
274 void Transparency::setImage1(vtkImageData *image)
279 //------------------------------------------------------------
280 void Transparency::setImage2(vtkImageData *image)
285 //------------------------------------------------------------
286 void Transparency::setPercent(int percent)
288 _percent=(double)percent/100.0;