1 #include "CheckBoard.h"
7 //------------------------------------------------------------
8 CheckBoard::CheckBoard()
21 //------------------------------------------------------------
22 CheckBoard::~CheckBoard()
24 //if (image1 != NULL ) { image1->Delete(); }
25 //if (image2 != NULL ) { image2->Delete(); }
26 if (newImage != NULL ) { newImage->Delete(); }
29 //------------------------------------------------------------
30 void CheckBoard::calculateImage()
33 if(image1!=NULL && image2!=NULL)
38 // Information from image1
39 image1->GetSpacing(spcImg1);
40 image1->GetExtent(extImg1);
42 type = image1->GetScalarType();
44 // The Z dimension is 1 for a 2D image
45 dimImg1[0] = extImg1[1] - extImg1[0]+1;
46 dimImg1[1] = extImg1[3] - extImg1[2]+1;
51 // Information from image2
52 image2->GetSpacing(spcImg2);
53 image2->GetExtent(extImg2);
55 // The Z dimension is 1 for a 2D image
56 dimImg2[0] = extImg2[1] - extImg2[0]+1;
57 dimImg2[1] = extImg2[3] - extImg2[2]+1;
60 long numPixelsImg1 = dimImg1[0]*dimImg1[1];
61 long numPixelsImg2 = dimImg2[0]*dimImg2[1];
65 vtkImageResample *resample = vtkImageResample::New();
67 if(numPixelsImg1<numPixelsImg2)
69 resample->SetInput(image1);
70 factorX = (double)extImg2[1]/extImg1[1];
71 factorY = (double)extImg2[3]/extImg1[3];
72 resample->SetAxisMagnificationFactor(0,factorX);
73 resample->SetAxisMagnificationFactor(1,factorY);
74 resample->SetInformationInput(image2);
76 initialize(dimImg2, spcImg2);
77 result = resample->GetOutput();
79 createImage(result,image2,dimImg2[0],dimImg2[1]);
82 else if (numPixelsImg1>numPixelsImg2)
84 resample->SetInput(image2);
85 factorX = (double)extImg1[1]/extImg2[1];
86 factorY = (double)extImg1[3]/extImg2[3];
87 resample->SetAxisMagnificationFactor(0,factorX);
88 resample->SetAxisMagnificationFactor(1,factorY);
89 resample->SetInformationInput(image1);
91 initialize(dimImg1, spcImg1);
92 result = resample->GetOutput();
96 createImage(image1,result,dimImg1[0],dimImg1[1]);
100 //If both images have the same number of pixels, the resultant image will have the
101 //properties of the first image.
102 resample->SetInput(image2);
103 factorX = (double)extImg1[1]/extImg2[1];
104 factorY = (double)extImg1[3]/extImg2[3];
105 resample->SetAxisMagnificationFactor(0,factorX);
106 resample->SetAxisMagnificationFactor(1,factorY);
107 resample->SetInformationInput(image1);
109 initialize(dimImg1, spcImg1);
111 result = resample->GetOutput();
114 createImage(image1,result,dimImg1[0],dimImg1[1]);
123 //------------------------------------------------------------
124 void CheckBoard::initialize(int dimensions[], double spacing[])
126 // Setting the new image
127 newImage = vtkImageData::New();
128 newImage->SetScalarType(type);
129 newImage->SetSpacing(spacing);
130 newImage->SetDimensions(dimensions);
131 newImage->AllocateScalars();
136 //------------------------------------------------------------
137 void CheckBoard::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
139 int imageType=img1->GetScalarType();
141 if(imageType == VTK_CHAR)
144 char* dataImagePointer1 = NULL;
145 char* dataImagePointer2 = NULL;
146 char* dataImageResultPointer = NULL;
148 dataImagePointer1 = (char*) img1->GetScalarPointer(0,0,0);
149 dataImagePointer2 = (char*) img2->GetScalarPointer(0,0,0);
150 dataImageResultPointer = (char*) newImage->GetScalarPointer(0,0,0);
161 int divX = sizeX/squaresX;
162 int divY = sizeY/squaresY;
163 int i, j, counterX, counterY;
164 for(i = 0; i < sizeX; i++)
167 for(j = 0; j < sizeY; j++)
169 dataImagePointer1 = (char*)img1->GetScalarPointer(i,j,0);
170 dataImagePointer2 = (char*)img2->GetScalarPointer(i,j,0);
171 dataImageResultPointer = (char*)newImage->GetScalarPointer(i,j,0);
173 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
175 *dataImageResultPointer = (char) *dataImagePointer1;
177 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
179 *dataImageResultPointer = (char) *dataImagePointer2;
182 if(counterX >= 2*divX)
186 else if(counterY >= 2*divY)
197 else if(imageType == VTK_SIGNED_CHAR)
200 signed char* dataImagePointer1 = NULL;
201 signed char* dataImagePointer2 = NULL;
202 signed char* dataImageResultPointer = NULL;
204 dataImagePointer1 = (signed char*) img1->GetScalarPointer(0,0,0);
205 dataImagePointer2 = (signed char*) img2->GetScalarPointer(0,0,0);
206 dataImageResultPointer = (signed char*) newImage->GetScalarPointer(0,0,0);
217 int divX = sizeX/squaresX;
218 int divY = sizeY/squaresY;
219 int i, j, counterX, counterY;
220 for(i = 0; i < sizeX; i++)
223 for(j = 0; j < sizeY; j++)
225 dataImagePointer1 = (signed char*)img1->GetScalarPointer(i,j,0);
226 dataImagePointer2 = (signed char*)img2->GetScalarPointer(i,j,0);
227 dataImageResultPointer = (signed char*)newImage->GetScalarPointer(i,j,0);
229 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
231 *dataImageResultPointer = (signed char) *dataImagePointer1;
233 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
235 *dataImageResultPointer = (signed char) *dataImagePointer2;
238 if(counterX >= 2*divX)
242 else if(counterY >= 2*divY)
252 else if(imageType == VTK_UNSIGNED_CHAR)
255 unsigned char* dataImagePointer1 = NULL;
256 unsigned char* dataImagePointer2 = NULL;
257 unsigned char* dataImageResultPointer = NULL;
259 dataImagePointer1 = (unsigned char*) img1->GetScalarPointer(0,0,0);
260 dataImagePointer2 = (unsigned char*) img2->GetScalarPointer(0,0,0);
261 dataImageResultPointer = (unsigned char*) newImage->GetScalarPointer(0,0,0);
272 int divX = sizeX/squaresX;
273 int divY = sizeY/squaresY;
274 int i, j, counterX, counterY;
275 for(i = 0; i < sizeX; i++)
278 for(j = 0; j < sizeY; j++)
280 dataImagePointer1 = (unsigned char*)img1->GetScalarPointer(i,j,0);
281 dataImagePointer2 = (unsigned char*)img2->GetScalarPointer(i,j,0);
282 dataImageResultPointer = (unsigned char*)newImage->GetScalarPointer(i,j,0);
284 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
286 *dataImageResultPointer = (unsigned char) *dataImagePointer1;
288 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
290 *dataImageResultPointer = (unsigned char) *dataImagePointer2;
293 if(counterX >= 2*divX)
297 else if(counterY >= 2*divY)
307 else if(imageType == VTK_SHORT)
310 short* dataImagePointer1 = NULL;
311 short* dataImagePointer2 = NULL;
312 short* dataImageResultPointer = NULL;
314 dataImagePointer1 = (short*) img1->GetScalarPointer(0,0,0);
315 dataImagePointer2 = (short*) img2->GetScalarPointer(0,0,0);
316 dataImageResultPointer = (short*) newImage->GetScalarPointer(0,0,0);
327 int divX = sizeX/squaresX;
328 int divY = sizeY/squaresY;
329 int i, j, counterX, counterY;
330 for(i = 0; i < sizeX; i++)
333 for(j = 0; j < sizeY; j++)
335 dataImagePointer1 = (short*)img1->GetScalarPointer(i,j,0);
336 dataImagePointer2 = (short*)img2->GetScalarPointer(i,j,0);
337 dataImageResultPointer = (short*)newImage->GetScalarPointer(i,j,0);
339 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
341 *dataImageResultPointer = (short) *dataImagePointer1;
343 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
345 *dataImageResultPointer = (short) *dataImagePointer2;
348 if(counterX >= 2*divX)
352 else if(counterY >= 2*divY)
362 else if(imageType == VTK_UNSIGNED_SHORT)
365 unsigned short* dataImagePointer1 = NULL;
366 unsigned short* dataImagePointer2 = NULL;
367 unsigned short* dataImageResultPointer = NULL;
369 dataImagePointer1 = (unsigned short*) img1->GetScalarPointer(0,0,0);
370 dataImagePointer2 = (unsigned short*) img2->GetScalarPointer(0,0,0);
371 dataImageResultPointer = (unsigned short*) newImage->GetScalarPointer(0,0,0);
382 int divX = sizeX/squaresX;
383 int divY = sizeY/squaresY;
384 int i, j, counterX, counterY;
385 for(i = 0; i < sizeX; i++)
388 for(j = 0; j < sizeY; j++)
390 dataImagePointer1 = (unsigned short*)img1->GetScalarPointer(i,j,0);
391 dataImagePointer2 = (unsigned short*)img2->GetScalarPointer(i,j,0);
392 dataImageResultPointer = (unsigned short*)newImage->GetScalarPointer(i,j,0);
394 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
396 *dataImageResultPointer = (unsigned short) *dataImagePointer1;
398 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
400 *dataImageResultPointer = (unsigned short) *dataImagePointer2;
403 if(counterX >= 2*divX)
407 else if(counterY >= 2*divY)
418 else if(imageType == VTK_INT)
421 int* dataImagePointer1 = NULL;
422 int* dataImagePointer2 = NULL;
423 int* dataImageResultPointer = NULL;
425 dataImagePointer1 = (int*) img1->GetScalarPointer(0,0,0);
426 dataImagePointer2 = (int*) img2->GetScalarPointer(0,0,0);
427 dataImageResultPointer = (int*) newImage->GetScalarPointer(0,0,0);
438 int divX = sizeX/squaresX;
439 int divY = sizeY/squaresY;
440 int i, j, counterX, counterY;
441 for(i = 0; i < sizeX; i++)
444 for(j = 0; j < sizeY; j++)
446 dataImagePointer1 = (int*)img1->GetScalarPointer(i,j,0);
447 dataImagePointer2 = (int*)img2->GetScalarPointer(i,j,0);
448 dataImageResultPointer = (int*)newImage->GetScalarPointer(i,j,0);
450 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
452 *dataImageResultPointer = (int) *dataImagePointer1;
454 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
456 *dataImageResultPointer = (int) *dataImagePointer2;
459 if(counterX >= 2*divX)
463 else if(counterY >= 2*divY)
473 else if(imageType == VTK_UNSIGNED_INT)
476 unsigned int* dataImagePointer1 = NULL;
477 unsigned int* dataImagePointer2 = NULL;
478 unsigned int* dataImageResultPointer = NULL;
480 dataImagePointer1 = (unsigned int*) img1->GetScalarPointer(0,0,0);
481 dataImagePointer2 = (unsigned int*) img2->GetScalarPointer(0,0,0);
482 dataImageResultPointer = (unsigned int*) newImage->GetScalarPointer(0,0,0);
493 int divX = sizeX/squaresX;
494 int divY = sizeY/squaresY;
495 int i, j, counterX, counterY;
496 for(i = 0; i < sizeX; i++)
499 for(j = 0; j < sizeY; j++)
501 dataImagePointer1 = (unsigned int*)img1->GetScalarPointer(i,j,0);
502 dataImagePointer2 = (unsigned int*)img2->GetScalarPointer(i,j,0);
503 dataImageResultPointer = (unsigned int*)newImage->GetScalarPointer(i,j,0);
505 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
507 *dataImageResultPointer = (unsigned int) *dataImagePointer1;
509 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
511 *dataImageResultPointer = (unsigned int) *dataImagePointer2;
514 if(counterX >= 2*divX)
518 else if(counterY >= 2*divY)
528 else if(imageType == VTK_LONG)
531 long* dataImagePointer1 = NULL;
532 long* dataImagePointer2 = NULL;
533 long* dataImageResultPointer = NULL;
535 dataImagePointer1 = (long*) img1->GetScalarPointer(0,0,0);
536 dataImagePointer2 = (long*) img2->GetScalarPointer(0,0,0);
537 dataImageResultPointer = (long*) newImage->GetScalarPointer(0,0,0);
548 int divX = sizeX/squaresX;
549 int divY = sizeY/squaresY;
550 int i, j, counterX, counterY;
551 for(i = 0; i < sizeX; i++)
554 for(j = 0; j < sizeY; j++)
556 dataImagePointer1 = (long*)img1->GetScalarPointer(i,j,0);
557 dataImagePointer2 = (long*)img2->GetScalarPointer(i,j,0);
558 dataImageResultPointer = (long*)newImage->GetScalarPointer(i,j,0);
560 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
562 *dataImageResultPointer = (long) *dataImagePointer1;
564 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
566 *dataImageResultPointer = (long) *dataImagePointer2;
569 if(counterX >= 2*divX)
573 else if(counterY >= 2*divY)
583 else if(imageType == VTK_UNSIGNED_LONG)
586 unsigned long* dataImagePointer1 = NULL;
587 unsigned long* dataImagePointer2 = NULL;
588 unsigned long* dataImageResultPointer = NULL;
590 dataImagePointer1 = (unsigned long*) img1->GetScalarPointer(0,0,0);
591 dataImagePointer2 = (unsigned long*) img2->GetScalarPointer(0,0,0);
592 dataImageResultPointer = (unsigned long*) newImage->GetScalarPointer(0,0,0);
603 int divX = sizeX/squaresX;
604 int divY = sizeY/squaresY;
605 int i, j, counterX, counterY;
606 for(i = 0; i < sizeX; i++)
609 for(j = 0; j < sizeY; j++)
611 dataImagePointer1 = (unsigned long*)img1->GetScalarPointer(i,j,0);
612 dataImagePointer2 = (unsigned long*)img2->GetScalarPointer(i,j,0);
613 dataImageResultPointer = (unsigned long*)newImage->GetScalarPointer(i,j,0);
615 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
617 *dataImageResultPointer = (unsigned long) *dataImagePointer1;
619 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
621 *dataImageResultPointer = (unsigned long) *dataImagePointer2;
624 if(counterX >= 2*divX)
628 else if(counterY >= 2*divY)
637 else if(imageType == VTK_FLOAT)
640 float* dataImagePointer1 = NULL;
641 float* dataImagePointer2 = NULL;
642 float* dataImageResultPointer = NULL;
644 dataImagePointer1 = (float*) img1->GetScalarPointer(0,0,0);
645 dataImagePointer2 = (float*) img2->GetScalarPointer(0,0,0);
646 dataImageResultPointer = (float*) newImage->GetScalarPointer(0,0,0);
657 int divX = sizeX/squaresX;
658 int divY = sizeY/squaresY;
659 int i, j, counterX, counterY;
660 for(i = 0; i < sizeX; i++)
663 for(j = 0; j < sizeY; j++)
665 dataImagePointer1 = (float*)img1->GetScalarPointer(i,j,0);
666 dataImagePointer2 = (float*)img2->GetScalarPointer(i,j,0);
667 dataImageResultPointer = (float*)newImage->GetScalarPointer(i,j,0);
669 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
671 *dataImageResultPointer = (float) *dataImagePointer1;
673 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
675 *dataImageResultPointer = (float) *dataImagePointer2;
678 if(counterX >= 2*divX)
682 else if(counterY >= 2*divY)
692 else if(imageType == VTK_DOUBLE)
695 double* dataImagePointer1 = NULL;
696 double* dataImagePointer2 = NULL;
697 double* dataImageResultPointer = NULL;
699 dataImagePointer1 = (double*) img1->GetScalarPointer(0,0,0);
700 dataImagePointer2 = (double*) img2->GetScalarPointer(0,0,0);
701 dataImageResultPointer = (double*) newImage->GetScalarPointer(0,0,0);
712 int divX = sizeX/squaresX;
713 int divY = sizeY/squaresY;
714 int i, j, counterX, counterY;
715 for(i = 0; i < sizeX; i++)
718 for(j = 0; j < sizeY; j++)
720 dataImagePointer1 = (double*)img1->GetScalarPointer(i,j,0);
721 dataImagePointer2 = (double*)img2->GetScalarPointer(i,j,0);
722 dataImageResultPointer = (double*)newImage->GetScalarPointer(i,j,0);
724 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
726 *dataImageResultPointer = (double) *dataImagePointer1;
728 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
730 *dataImageResultPointer = (double) *dataImagePointer2;
733 if(counterX >= 2*divX)
737 else if(counterY >= 2*divY)
748 //std::cout << "The image has been checkboardized!" << std::endl;
750 // newImage->Modified();
756 //------------------------------------------------------------
757 vtkImageData* CheckBoard::getFilteredImage()
767 //------------------------------------------------------------
768 void CheckBoard::setInputImage1(vtkImageData *_image)
773 //------------------------------------------------------------
774 void CheckBoard::setInputImage2(vtkImageData *_image)
779 //------------------------------------------------------------
780 void CheckBoard::setCols(int cols)
785 //------------------------------------------------------------
786 void CheckBoard::setRows(int rows)