1 #include "CheckBoard.h"
7 //------------------------------------------------------------
8 CheckBoard::CheckBoard()
24 //------------------------------------------------------------
25 CheckBoard::~CheckBoard()
27 //if (image1 != NULL ) { image1->Delete(); }
28 //if (image2 != NULL ) { image2->Delete(); }
29 if (newImage != NULL ) { newImage->Delete(); }
30 if (resample != NULL ) { resample->Delete(); }
31 if (result != NULL ) { result->Delete(); }
34 //------------------------------------------------------------
35 void CheckBoard::calculateImage()
38 if(image1!=NULL && image2!=NULL)
43 // Information from image1
44 image1->GetSpacing(spcImg1);
45 image1->GetExtent(extImg1);
47 type = image1->GetScalarType();
49 // The Z dimension is 1 for a 2D image
50 dimImg1[0] = extImg1[1] - extImg1[0]+1;
51 dimImg1[1] = extImg1[3] - extImg1[2]+1;
56 // Information from image2
57 image2->GetSpacing(spcImg2);
58 image2->GetExtent(extImg2);
60 // The Z dimension is 1 for a 2D image
61 dimImg2[0] = extImg2[1] - extImg2[0]+1;
62 dimImg2[1] = extImg2[3] - extImg2[2]+1;
65 long numPixelsImg1 = dimImg1[0]*dimImg1[1];
66 long numPixelsImg2 = dimImg2[0]*dimImg2[1];
70 resample = vtkImageResample::New();
71 result = vtkImageData::New();
72 if(numPixelsImg1<numPixelsImg2)
74 resample->SetInput(image1);
75 factorX = (double)extImg2[1]/extImg1[1];
76 factorY = (double)extImg2[3]/extImg1[3];
77 resample->SetAxisMagnificationFactor(0,factorX);
78 resample->SetAxisMagnificationFactor(1,factorY);
79 resample->SetInformationInput(image2);
81 initialize(dimImg2, spcImg2);
82 result = resample->GetOutput();
84 createImage(result,image2,dimImg2[0],dimImg2[1]);
87 else if (numPixelsImg1>numPixelsImg2)
89 resample->SetInput(image2);
90 factorX = (double)extImg1[1]/extImg2[1];
91 factorY = (double)extImg1[3]/extImg2[3];
92 resample->SetAxisMagnificationFactor(0,factorX);
93 resample->SetAxisMagnificationFactor(1,factorY);
94 resample->SetInformationInput(image1);
96 initialize(dimImg1, spcImg1);
97 result = resample->GetOutput();
101 createImage(image1,result,dimImg1[0],dimImg1[1]);
105 //If both images have the same number of pixels, the resultant image will have the
106 //properties of the first image.
107 resample->SetInput(image2);
108 factorX = (double)extImg1[1]/extImg2[1];
109 factorY = (double)extImg1[3]/extImg2[3];
110 resample->SetAxisMagnificationFactor(0,factorX);
111 resample->SetAxisMagnificationFactor(1,factorY);
112 resample->SetInformationInput(image1);
114 initialize(dimImg1, spcImg1);
116 result = resample->GetOutput();
119 createImage(image1,result,dimImg1[0],dimImg1[1]);
128 //------------------------------------------------------------
129 void CheckBoard::initialize(int dimensions[], double spacing[])
131 // Setting the new image
132 if(newImage != NULL){
135 newImage = vtkImageData::New();
136 newImage->SetScalarType(type);
137 newImage->SetSpacing(spacing);
138 newImage->SetDimensions(dimensions);
139 newImage->AllocateScalars();
145 //------------------------------------------------------------
146 void CheckBoard::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
148 int imageType=img1->GetScalarType();
150 if(imageType == VTK_CHAR)
153 char* dataImagePointer1 = NULL;
154 char* dataImagePointer2 = NULL;
155 char* dataImageResultPointer = NULL;
157 createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
159 else if(imageType == VTK_SIGNED_CHAR)
162 signed char* dataImagePointer1 = NULL;
163 signed char* dataImagePointer2 = NULL;
164 signed char* dataImageResultPointer = NULL;
166 createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
168 else if(imageType == VTK_UNSIGNED_CHAR)
171 unsigned char* dataImagePointer1 = NULL;
172 unsigned char* dataImagePointer2 = NULL;
173 unsigned char* dataImageResultPointer = NULL;
175 createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
177 else if(imageType == VTK_SHORT)
180 short* dataImagePointer1 = NULL;
181 short* dataImagePointer2 = NULL;
182 short* dataImageResultPointer = NULL;
184 createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
186 else if(imageType == VTK_UNSIGNED_SHORT)
189 unsigned short* dataImagePointer1 = NULL;
190 unsigned short* dataImagePointer2 = NULL;
191 unsigned short* dataImageResultPointer = NULL;
193 createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
196 else if(imageType == VTK_INT)
199 int* dataImagePointer1 = NULL;
200 int* dataImagePointer2 = NULL;
201 int* dataImageResultPointer = NULL;
203 createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
205 else if(imageType == VTK_UNSIGNED_INT)
208 unsigned int* dataImagePointer1 = NULL;
209 unsigned int* dataImagePointer2 = NULL;
210 unsigned int* dataImageResultPointer = NULL;
212 createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
214 else if(imageType == VTK_LONG)
217 long* dataImagePointer1 = NULL;
218 long* dataImagePointer2 = NULL;
219 long* dataImageResultPointer = NULL;
221 createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
223 else if(imageType == VTK_UNSIGNED_LONG)
226 unsigned long* dataImagePointer1 = NULL;
227 unsigned long* dataImagePointer2 = NULL;
228 unsigned long* dataImageResultPointer = NULL;
230 createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
232 else if(imageType == VTK_FLOAT)
235 float* dataImagePointer1 = NULL;
236 float* dataImagePointer2 = NULL;
237 float* dataImageResultPointer = NULL;
239 createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
241 else if(imageType == VTK_DOUBLE)
244 double* dataImagePointer1 = NULL;
245 double* dataImagePointer2 = NULL;
246 double* dataImageResultPointer = NULL;
248 createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
252 //std::cout << "The image has been checkboardized!" << std::endl;
254 // newImage->Modified();
256 /**Template used for the different image types
259 void CheckBoard::createImageByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
261 dataImagePointer1 = (T*) img1->GetScalarPointer(0,0,0);
262 dataImagePointer2 = (T*) img2->GetScalarPointer(0,0,0);
263 dataImageResultPointer = (T*) newImage->GetScalarPointer(0,0,0);
274 int divX = sizeX/squaresX;
275 int divY = sizeY/squaresY;
276 int i, j, counterX, counterY;
277 for(i = 0; i < sizeX; i++)
280 for(j = 0; j < sizeY; j++)
282 dataImagePointer1 = (T*)img1->GetScalarPointer(i,j,0);
283 dataImagePointer2 = (T*)img2->GetScalarPointer(i,j,0);
284 dataImageResultPointer = (T*)newImage->GetScalarPointer(i,j,0);
285 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
287 *dataImageResultPointer = (T) *dataImagePointer1;
289 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
291 *dataImageResultPointer = (T) *dataImagePointer2;
294 if(counterX >= 2*divX)
298 else if(counterY >= 2*divY)
311 //------------------------------------------------------------
312 vtkImageData* CheckBoard::getFilteredImage()
322 //------------------------------------------------------------
323 void CheckBoard::setInputImage1(vtkImageData *_image)
328 //------------------------------------------------------------
329 void CheckBoard::setInputImage2(vtkImageData *_image)
334 //------------------------------------------------------------
335 void CheckBoard::setCols(int cols)
340 //------------------------------------------------------------
341 void CheckBoard::setRows(int rows)