]> Creatis software - creaRigidRegistration.git/blob - lib/Transparency.cxx
Black box that generates a grid over an image in order to visualize the deformation...
[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;  EED ???
134
135         if(_type == VTK_CHAR)
136         {
137                 //POINTERS: 
138                 char* dataImagePointer1 = NULL;
139                 char* dataImagePointer2 = NULL;
140                 char* dataImageResultPointer = NULL;
141         
142                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
143         }
144         else if(_type == VTK_SIGNED_CHAR)
145         {
146                 //POINTERS: 
147                 signed char* dataImagePointer1 = NULL;
148                 signed char* dataImagePointer2 = NULL;
149                 signed char* dataImageResultPointer = NULL;
150         
151                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
152         }
153         else if(_type == VTK_UNSIGNED_CHAR)
154         {
155                 //POINTERS: 
156                 unsigned char* dataImagePointer1 = NULL;
157                 unsigned char* dataImagePointer2 = NULL;
158                 unsigned char* dataImageResultPointer = NULL;
159         
160                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
161         }
162         else if(_type == VTK_SHORT)
163         {
164                 //POINTERS: 
165                 short* dataImagePointer1 = NULL;
166                 short* dataImagePointer2 = NULL;
167                 short* dataImageResultPointer = NULL;
168         
169                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
170         }
171         else if(_type == VTK_UNSIGNED_SHORT)
172         {
173                 //POINTERS: 
174                 unsigned short* dataImagePointer1 = NULL;
175                 unsigned short* dataImagePointer2 = NULL;
176                 unsigned short* dataImageResultPointer = NULL;
177         
178                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
179         }
180         else if(_type == VTK_INT)
181         {
182                 //POINTERS: 
183                 int* dataImagePointer1 = NULL;
184                 int* dataImagePointer2 = NULL;
185                 int* dataImageResultPointer = NULL;
186         
187                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
188         }
189         else if(_type == VTK_UNSIGNED_INT)
190         {
191                 //POINTERS: 
192                 unsigned int* dataImagePointer1 = NULL;
193                 unsigned int* dataImagePointer2 = NULL;
194                 unsigned int* dataImageResultPointer = NULL;
195         
196                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
197         }
198         else if(_type == VTK_LONG)
199         {
200                 //POINTERS: 
201                 long* dataImagePointer1 = NULL;
202                 long* dataImagePointer2 = NULL;
203                 long* dataImageResultPointer = NULL;
204         
205                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
206         }
207         else if(_type == VTK_UNSIGNED_LONG)
208         {
209                 //POINTERS: 
210                 unsigned long* dataImagePointer1 = NULL;
211                 unsigned long* dataImagePointer2 = NULL;
212                 unsigned long* dataImageResultPointer = NULL;
213         
214                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);              
215         }
216         else if(_type == VTK_FLOAT)
217         {
218                 //POINTERS: 
219                 float* dataImagePointer1 = NULL;
220                 float* dataImagePointer2 = NULL;
221                 float* dataImageResultPointer = NULL;
222         
223                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
224         }
225         else if(_type == VTK_DOUBLE)
226         {
227                 //POINTERS: 
228                 double* dataImagePointer1 = NULL;
229                 double* dataImagePointer2 = NULL;
230                 double* dataImageResultPointer = NULL;
231
232                 createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
233         }
234
235         _newImage->Update();
236 }
237
238 template <class T>
239 void Transparency::createImageByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
240 {
241         int i, j;
242         
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++)
247         {
248                 for(j = 0; j < sizeY; j++)
249                 {
250                         dataImagePointer1 = (T*)img1->GetScalarPointer(i,j,0);
251                         dataImagePointer2 = (T*)img2->GetScalarPointer(i,j,0);
252                         dataImageResultPointer = (T*)_newImage->GetScalarPointer(i,j,0);
253                         
254                         *dataImageResultPointer = (T)*dataImagePointer1*_percent + (T)*dataImagePointer2*(1-_percent);                          
255                 }
256         }
257 }
258
259 /*
260 * Get new image
261 */
262 //------------------------------------------------------------
263 vtkImageData* Transparency::getImage()
264 {
265         if(_processed){
266                 return _newImage;
267         }
268         else{
269                 return NULL;
270         }
271 }
272
273 //------------------------------------------------------------
274 void Transparency::setImage1(vtkImageData *image)
275 {
276         _image1=image;
277 }
278
279 //------------------------------------------------------------
280 void Transparency::setImage2(vtkImageData *image)
281 {
282         _image2=image;
283 }
284
285 //------------------------------------------------------------
286 void Transparency::setPercent(int percent)
287 {
288     _percent=(double)percent/100.0;     
289 }
290