]> Creatis software - creaRigidRegistration.git/blob - lib/Transparency.cxx
*** empty log message ***
[creaRigidRegistration.git] / lib / Transparency.cxx
1 #include "Transparency.h"
2
3
4 /*
5 * Constructor
6 */
7 //------------------------------------------------------------
8 Transparency::Transparency()
9 {
10         _image1=NULL;
11         _image2=NULL;
12         _percent=0;
13         _newImage = NULL;
14         _processed=false;
15 }
16
17 /*
18 * Destructor
19 */
20 //------------------------------------------------------------
21 Transparency::~Transparency()
22 {
23         if (_newImage != NULL ) { _newImage->Delete(); }
24 }
25
26 //------------------------------------------------------------
27 void Transparency::calculateImage()
28 {
29
30         if(_image1!=NULL && _image2!=NULL)
31         {
32
33                 // IMAGE 1
34                         
35                 // Information from image1
36                 _image1->GetSpacing(spcImg1);
37                 _image1->GetExtent(extImg1);
38                 _type = _image1->GetScalarType();
39                 
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;
43                 dimImg1[2] = 1;
44
45                 //IMAGE 2
46
47                 // Information from image2
48                 _image2->GetSpacing(spcImg2);
49                 _image2->GetExtent(extImg2);
50                 
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;
54                 dimImg2[2] = 1;
55
56                 long numPixelsImg1 = dimImg1[0]*dimImg1[1];
57                 long numPixelsImg2 = dimImg2[0]*dimImg2[1];
58                 
59                 double factorX = 1;
60                 double factorY = 1;
61                 vtkImageResample *resample = vtkImageResample::New();
62                 vtkImageData *result;
63                 if(numPixelsImg1<numPixelsImg2)
64                 {
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);
71
72                         initialize(dimImg2, spcImg2);
73                         result = resample->GetOutput();
74                         result->Update();
75                         createImage(result,_image2,dimImg2[0],dimImg2[1]);              
76                         
77                 } //if
78                 else if (numPixelsImg1>numPixelsImg2)
79                 {
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);
86
87                         initialize(dimImg1, spcImg1);
88                         result = resample->GetOutput();
89                         
90                         result->Update();
91
92                         createImage(_image1,result,dimImg1[0],dimImg1[1]);
93                 } // else if
94                 else
95                 {
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);
104
105                         initialize(dimImg1, spcImg1);
106
107                         result = resample->GetOutput();
108                         result->Update();
109                         
110                         createImage(_image1,result,dimImg1[0],dimImg1[1]);
111
112                 } //else
113                 
114                 resample->Delete();             
115                 _processed=true;
116         }
117 }
118
119 void Transparency::initialize(int dimensions[], double spacing[])
120 {
121         // Setting the new image
122         _newImage = vtkImageData::New();
123         _newImage->SetScalarType(_type);
124         _newImage->SetSpacing(spacing);
125         _newImage->SetDimensions(dimensions);
126         _newImage->AllocateScalars();
127         _newImage->Update();
128
129 }
130
131 void Transparency::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
132 {
133         int i, j;
134         if(_type == VTK_CHAR)
135         {
136                 //POINTERS: 
137                 char* dataImagePointer1 = NULL;
138                 char* dataImagePointer2 = NULL;
139                 char* dataImageResultPointer = NULL;
140         
141                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
142         }
143         else if(_type == VTK_SIGNED_CHAR)
144         {
145                 //POINTERS: 
146                 signed char* dataImagePointer1 = NULL;
147                 signed char* dataImagePointer2 = NULL;
148                 signed char* dataImageResultPointer = NULL;
149         
150                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
151         }
152         else if(_type == VTK_UNSIGNED_CHAR)
153         {
154                 //POINTERS: 
155                 unsigned char* dataImagePointer1 = NULL;
156                 unsigned char* dataImagePointer2 = NULL;
157                 unsigned char* dataImageResultPointer = NULL;
158         
159                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
160         }
161         else if(_type == VTK_SHORT)
162         {
163                 //POINTERS: 
164                 short* dataImagePointer1 = NULL;
165                 short* dataImagePointer2 = NULL;
166                 short* dataImageResultPointer = NULL;
167         
168                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
169         }
170         else if(_type == VTK_UNSIGNED_SHORT)
171         {
172                 //POINTERS: 
173                 unsigned short* dataImagePointer1 = NULL;
174                 unsigned short* dataImagePointer2 = NULL;
175                 unsigned short* dataImageResultPointer = NULL;
176         
177                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
178         }
179         else if(_type == VTK_INT)
180         {
181                 //POINTERS: 
182                 int* dataImagePointer1 = NULL;
183                 int* dataImagePointer2 = NULL;
184                 int* dataImageResultPointer = NULL;
185         
186                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
187         }
188         else if(_type == VTK_UNSIGNED_INT)
189         {
190                 //POINTERS: 
191                 unsigned int* dataImagePointer1 = NULL;
192                 unsigned int* dataImagePointer2 = NULL;
193                 unsigned int* dataImageResultPointer = NULL;
194         
195                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
196         }
197         else if(_type == VTK_LONG)
198         {
199                 //POINTERS: 
200                 long* dataImagePointer1 = NULL;
201                 long* dataImagePointer2 = NULL;
202                 long* dataImageResultPointer = NULL;
203         
204                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
205         }
206         else if(_type == VTK_UNSIGNED_LONG)
207         {
208                 //POINTERS: 
209                 unsigned long* dataImagePointer1 = NULL;
210                 unsigned long* dataImagePointer2 = NULL;
211                 unsigned long* dataImageResultPointer = NULL;
212         
213                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);              
214         }
215         else if(_type == VTK_FLOAT)
216         {
217                 //POINTERS: 
218                 float* dataImagePointer1 = NULL;
219                 float* dataImagePointer2 = NULL;
220                 float* dataImageResultPointer = NULL;
221         
222                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
223         }
224         else if(_type == VTK_DOUBLE)
225         {
226                 //POINTERS: 
227                 double* dataImagePointer1 = NULL;
228                 double* dataImagePointer2 = NULL;
229                 double* dataImageResultPointer = NULL;
230
231                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
232         }
233
234         _newImage->Update();
235 }
236
237 template <class T>
238 void Transparency::createImageByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
239 {
240         int i, j;
241         
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++)
246         {
247                 for(j = 0; j < sizeY; j++)
248                 {
249                         dataImagePointer1 = (T*)img1->GetScalarPointer(i,j,0);
250                         dataImagePointer2 = (T*)img2->GetScalarPointer(i,j,0);
251                         dataImageResultPointer = (T*)_newImage->GetScalarPointer(i,j,0);
252                         
253                         *dataImageResultPointer = (T)*dataImagePointer1*_percent + (T)*dataImagePointer2*(1-_percent);                          
254                 }
255         }
256 }
257
258 /*
259 * Get new image
260 */
261 //------------------------------------------------------------
262 vtkImageData* Transparency::getImage()
263 {
264         if(_processed){
265                 return _newImage;
266         }
267         else{
268                 return NULL;
269         }
270 }
271
272 //------------------------------------------------------------
273 void Transparency::setImage1(vtkImageData *image)
274 {
275         _image1=image;
276 }
277
278 //------------------------------------------------------------
279 void Transparency::setImage2(vtkImageData *image)
280 {
281         _image2=image;
282 }
283
284 //------------------------------------------------------------
285 void Transparency::setPercent(int percent)
286 {
287     _percent=(double)percent/100.0;     
288 }
289