]> Creatis software - creaRigidRegistration.git/blob - lib/CheckBoard.cpp
afa33d2c9e98bea1551be40099aa496eac14a3dc
[creaRigidRegistration.git] / lib / CheckBoard.cpp
1 #include "CheckBoard.h"
2
3
4 /*
5 * Constructor
6 */
7 //------------------------------------------------------------
8 CheckBoard::CheckBoard()
9 {
10         image1=NULL;
11         image2=NULL;
12         squaresX=0;
13         squaresY=0;
14         newImage = NULL;
15         processed=false;
16         newImage = NULL;
17 }
18
19 /*
20 * Destructor
21 */
22 //------------------------------------------------------------
23 CheckBoard::~CheckBoard()
24 {
25         //if (image1 != NULL ) { image1->Delete(); }
26         //if (image2 != NULL ) { image2->Delete(); }
27         if (newImage != NULL ) { newImage->Delete(); }
28 }
29
30 //------------------------------------------------------------
31 void CheckBoard::calculateImage()
32 {
33
34         if(image1!=NULL && image2!=NULL)
35         {
36
37                 // IMAGE 1
38                         
39                 // Information from image1
40                 image1->GetSpacing(spcImg1);
41                 image1->GetExtent(extImg1);
42
43                 type = image1->GetScalarType();
44                 
45                 // The Z dimension is 1 for a 2D image
46                 dimImg1[0] = extImg1[1] - extImg1[0]+1;
47                 dimImg1[1] = extImg1[3] - extImg1[2]+1;
48                 dimImg1[2] = 1;
49
50                 //IMAGE 2
51
52                 // Information from image2
53                 image2->GetSpacing(spcImg2);
54                 image2->GetExtent(extImg2);
55                 
56                 // The Z dimension is 1 for a 2D image
57                 dimImg2[0] = extImg2[1] - extImg2[0]+1;
58                 dimImg2[1] = extImg2[3] - extImg2[2]+1;
59                 dimImg2[2] = 1;
60
61                 long numPixelsImg1 = dimImg1[0]*dimImg1[1];
62                 long numPixelsImg2 = dimImg2[0]*dimImg2[1];
63                 
64                 double factorX = 1;
65                 double factorY = 1;
66                 vtkImageResample *resample = vtkImageResample::New();
67                 vtkImageData *result;
68                 if(numPixelsImg1<numPixelsImg2)
69                 {
70                         resample->SetInput(image1);
71                         factorX = (double)extImg2[1]/extImg1[1];
72                         factorY = (double)extImg2[3]/extImg1[3];
73                         resample->SetAxisMagnificationFactor(0,factorX);
74                         resample->SetAxisMagnificationFactor(1,factorY);
75                         resample->SetInformationInput(image2);
76
77                         initialize(dimImg2, spcImg2);
78                         result = resample->GetOutput();
79                         result->Update();
80                         createImage(result,image2,dimImg2[0],dimImg2[1]);               
81                         
82                 } //if
83                 else if (numPixelsImg1>numPixelsImg2)
84                 {
85                         resample->SetInput(image2);
86                         factorX = (double)extImg1[1]/extImg2[1];
87                         factorY = (double)extImg1[3]/extImg2[3];
88                         resample->SetAxisMagnificationFactor(0,factorX);
89                         resample->SetAxisMagnificationFactor(1,factorY);
90                         resample->SetInformationInput(image1);
91
92                         initialize(dimImg1, spcImg1);
93                         result = resample->GetOutput();
94                         
95                         result->Update();
96
97                         createImage(image1,result,dimImg1[0],dimImg1[1]);
98                 } // else if
99                 else
100                 {
101                         //If both images have the same number of pixels, the resultant image will have the 
102                         //properties of the first image.
103                         resample->SetInput(image2);
104                         factorX = (double)extImg1[1]/extImg2[1];
105                         factorY = (double)extImg1[3]/extImg2[3];
106                         resample->SetAxisMagnificationFactor(0,factorX);
107                         resample->SetAxisMagnificationFactor(1,factorY);
108                         resample->SetInformationInput(image1);
109
110                         initialize(dimImg1, spcImg1);
111
112                         result = resample->GetOutput();
113                         result->Update();
114                         
115                         createImage(image1,result,dimImg1[0],dimImg1[1]);
116
117                 } //else
118                 
119                 resample->Delete();             
120                 processed=true;
121         }
122 }
123
124 //------------------------------------------------------------
125 void CheckBoard::initialize(int dimensions[], double spacing[])
126 {
127         // Setting the new image
128         if(newImage != NULL){
129                         newImage->Delete();
130         }
131         newImage = vtkImageData::New();
132         newImage->SetScalarType(type);
133         newImage->SetSpacing(spacing);
134         newImage->SetDimensions(dimensions);
135         newImage->AllocateScalars();
136         newImage->Update();
137         
138
139 }
140
141 //------------------------------------------------------------
142 void CheckBoard::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
143 {
144         int imageType=img1->GetScalarType();
145
146         if(imageType == VTK_CHAR)
147         {
148                 //POINTERS: 
149                 char* dataImagePointer1 = NULL;
150                 char* dataImagePointer2 = NULL;
151                 char* dataImageResultPointer = NULL;
152         
153                 dataImagePointer1 = (char*) img1->GetScalarPointer(0,0,0);
154                 dataImagePointer2 = (char*) img2->GetScalarPointer(0,0,0);
155                 dataImageResultPointer = (char*) newImage->GetScalarPointer(0,0,0);
156         
157                 if(squaresX == 0)
158                 {
159                         squaresX = 1;
160                 }
161                 if(squaresY == 0)
162                 {
163                         squaresY = 1;
164                 }    
165
166                 int divX = sizeX/squaresX;
167                 int divY = sizeY/squaresY;
168                 int i, j, counterX, counterY;
169                 for(i = 0; i < sizeX; i++)
170                 {
171                         counterY = 0; 
172                         for(j = 0; j < sizeY; j++)
173                         {
174                                 dataImagePointer1 = (char*)img1->GetScalarPointer(i,j,0);
175                                 dataImagePointer2 = (char*)img2->GetScalarPointer(i,j,0);
176                                 dataImageResultPointer = (char*)newImage->GetScalarPointer(i,j,0);
177
178                                 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
179                                 {
180                                         *dataImageResultPointer = (char) *dataImagePointer1;
181                                 }
182                                 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
183                                 {
184                                         *dataImageResultPointer = (char) *dataImagePointer2;
185                                 }
186
187                                 if(counterX >= 2*divX)
188                                 {
189                                         counterX = 0;
190                                 }
191                                 else if(counterY >= 2*divY)
192                                 {
193                                         counterY = 0;
194                                 }
195                         
196                                 counterY++;
197                         }
198                         counterX++;
199                 }
200
201         }
202         else if(imageType == VTK_SIGNED_CHAR)
203         {
204                 //POINTERS: 
205                 signed char* dataImagePointer1 = NULL;
206                 signed char* dataImagePointer2 = NULL;
207                 signed char* dataImageResultPointer = NULL;
208         
209                 dataImagePointer1 = (signed char*) img1->GetScalarPointer(0,0,0);
210                 dataImagePointer2 = (signed char*) img2->GetScalarPointer(0,0,0);
211                 dataImageResultPointer = (signed char*) newImage->GetScalarPointer(0,0,0);
212         
213                 if(squaresX == 0)
214                 {
215                         squaresX = 1;
216                 }
217                 if(squaresY == 0)
218                 {
219                         squaresY = 1;
220                 }    
221
222                 int     divX = sizeX/squaresX;
223                 int divY = sizeY/squaresY;
224                 int i, j, counterX, counterY;
225                 for(i = 0; i < sizeX; i++)
226                 {
227                         counterY = 0; 
228                         for(j = 0; j < sizeY; j++)
229                         {
230                                 dataImagePointer1 = (signed char*)img1->GetScalarPointer(i,j,0);
231                                 dataImagePointer2 = (signed char*)img2->GetScalarPointer(i,j,0);
232                                 dataImageResultPointer = (signed char*)newImage->GetScalarPointer(i,j,0);
233
234                                 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
235                                 {
236                                         *dataImageResultPointer = (signed char) *dataImagePointer1;
237                                 }
238                                 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
239                                 {
240                                         *dataImageResultPointer = (signed char) *dataImagePointer2;
241                                 }
242
243                                 if(counterX >= 2*divX)
244                                 {
245                                         counterX = 0;
246                                 }
247                                 else if(counterY >= 2*divY)
248                                 {
249                                         counterY = 0;
250                                 }
251                         
252                                 counterY++;
253                         }
254                         counterX++;
255                 }
256         }
257         else if(imageType == VTK_UNSIGNED_CHAR)
258         {
259                 //POINTERS: 
260                 unsigned char* dataImagePointer1 = NULL;
261                 unsigned char* dataImagePointer2 = NULL;
262                 unsigned char* dataImageResultPointer = NULL;
263         
264                 dataImagePointer1 = (unsigned char*) img1->GetScalarPointer(0,0,0);
265                 dataImagePointer2 = (unsigned char*) img2->GetScalarPointer(0,0,0);
266                 dataImageResultPointer = (unsigned char*) newImage->GetScalarPointer(0,0,0);
267         
268                 if(squaresX == 0)
269                 {
270                         squaresX = 1;
271                 }
272                 if(squaresY == 0)
273                 {
274                         squaresY = 1;
275                 }    
276
277                 int divX = sizeX/squaresX;
278                 int divY = sizeY/squaresY;
279                 int i, j, counterX, counterY;
280                 for(i = 0; i < sizeX; i++)
281                 {
282                         counterY = 0; 
283                         for(j = 0; j < sizeY; j++)
284                         {
285                                 dataImagePointer1 = (unsigned char*)img1->GetScalarPointer(i,j,0);
286                                 dataImagePointer2 = (unsigned char*)img2->GetScalarPointer(i,j,0);
287                                 dataImageResultPointer = (unsigned char*)newImage->GetScalarPointer(i,j,0);
288
289                                 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
290                                 {
291                                         *dataImageResultPointer = (unsigned char) *dataImagePointer1;
292                                 }
293                                 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
294                                 {
295                                         *dataImageResultPointer = (unsigned char) *dataImagePointer2;
296                                 }
297
298                                 if(counterX >= 2*divX)
299                                 {
300                                         counterX = 0;
301                                 }
302                                 else if(counterY >= 2*divY)
303                                 {
304                                         counterY = 0;
305                                 }
306                         
307                                 counterY++;
308                         }
309                         counterX++;
310                 }
311         }
312         else if(imageType == VTK_SHORT)
313         {
314         //POINTERS: 
315                 short* dataImagePointer1 = NULL;
316                 short* dataImagePointer2 = NULL;
317                 short* dataImageResultPointer = NULL;
318         
319                 dataImagePointer1 = (short*) img1->GetScalarPointer(0,0,0);
320                 dataImagePointer2 = (short*) img2->GetScalarPointer(0,0,0);
321                 dataImageResultPointer = (short*) newImage->GetScalarPointer(0,0,0);
322         
323                 if(squaresX == 0)
324                 {
325                         squaresX = 1;
326                 }
327                 if(squaresY == 0)
328                 {
329                         squaresY = 1;
330                 }    
331
332                 int divX = sizeX/squaresX;
333                 int divY = sizeY/squaresY;
334                 int i, j, counterX, counterY;
335                 for(i = 0; i < sizeX; i++)
336                 {
337                         counterY = 0; 
338                         for(j = 0; j < sizeY; j++)
339                         {
340                                 dataImagePointer1 = (short*)img1->GetScalarPointer(i,j,0);
341                                 dataImagePointer2 = (short*)img2->GetScalarPointer(i,j,0);
342                                 dataImageResultPointer = (short*)newImage->GetScalarPointer(i,j,0);
343
344                                 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
345                                 {
346                                         *dataImageResultPointer = (short) *dataImagePointer1;
347                                 }
348                                 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
349                                 {
350                                         *dataImageResultPointer = (short) *dataImagePointer2;
351                                 }
352
353                                 if(counterX >= 2*divX)
354                                 {
355                                         counterX = 0;
356                                 }
357                                 else if(counterY >= 2*divY)
358                                 {
359                                         counterY = 0;
360                                 }
361                         
362                                 counterY++;
363                         }
364                         counterX++;
365                 }
366         }
367         else if(imageType == VTK_UNSIGNED_SHORT)
368         {
369                 //POINTERS: 
370                 unsigned short* dataImagePointer1 = NULL;
371                 unsigned short* dataImagePointer2 = NULL;
372                 unsigned short* dataImageResultPointer = NULL;
373         
374                 dataImagePointer1 = (unsigned short*) img1->GetScalarPointer(0,0,0);
375                 dataImagePointer2 = (unsigned short*) img2->GetScalarPointer(0,0,0);
376                 dataImageResultPointer = (unsigned short*) newImage->GetScalarPointer(0,0,0);
377         
378                 if(squaresX == 0)
379                 {
380                         squaresX = 1;
381                 }
382                 if(squaresY == 0)
383                 {
384                         squaresY = 1;
385                 }    
386
387                 int divX = sizeX/squaresX;
388                 int divY = sizeY/squaresY;
389                 int i, j, counterX, counterY;
390                 for(i = 0; i < sizeX; i++)
391                 {
392                         counterY = 0; 
393                         for(j = 0; j < sizeY; j++)
394                         {
395                                 dataImagePointer1 = (unsigned short*)img1->GetScalarPointer(i,j,0);
396                                 dataImagePointer2 = (unsigned short*)img2->GetScalarPointer(i,j,0);
397                                 dataImageResultPointer = (unsigned short*)newImage->GetScalarPointer(i,j,0);
398
399                                 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
400                                 {
401                                         *dataImageResultPointer = (unsigned short) *dataImagePointer1;
402                                 }
403                                 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
404                                 {
405                                         *dataImageResultPointer = (unsigned short) *dataImagePointer2;
406                                 }
407
408                                 if(counterX >= 2*divX)
409                                 {
410                                         counterX = 0;
411                                 }
412                                 else if(counterY >= 2*divY)
413                                 {
414                                         counterY = 0;
415                                 }
416                         
417                                 counterY++;
418                         }
419                         counterX++;
420                 }
421         }
422
423         else if(imageType == VTK_INT)
424         {
425                 //POINTERS: 
426                 int* dataImagePointer1 = NULL;
427                 int* dataImagePointer2 = NULL;
428                 int* dataImageResultPointer = NULL;
429         
430                 dataImagePointer1 = (int*) img1->GetScalarPointer(0,0,0);
431                 dataImagePointer2 = (int*) img2->GetScalarPointer(0,0,0);
432                 dataImageResultPointer = (int*) newImage->GetScalarPointer(0,0,0);
433         
434                 if(squaresX == 0)
435                 {
436                         squaresX = 1;
437                 }
438                 if(squaresY == 0)
439                 {
440                         squaresY = 1;
441                 }    
442
443                 int divX = sizeX/squaresX;
444                 int divY = sizeY/squaresY;
445                 int i, j, counterX, counterY;
446                 for(i = 0; i < sizeX; i++)
447                 {
448                         counterY = 0; 
449                         for(j = 0; j < sizeY; j++)
450                         {
451                                 dataImagePointer1 = (int*)img1->GetScalarPointer(i,j,0);
452                                 dataImagePointer2 = (int*)img2->GetScalarPointer(i,j,0);
453                                 dataImageResultPointer = (int*)newImage->GetScalarPointer(i,j,0);
454
455                                 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
456                                 {
457                                         *dataImageResultPointer = (int) *dataImagePointer1;
458                                 }
459                                 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
460                                 {
461                                         *dataImageResultPointer = (int) *dataImagePointer2;
462                                 }
463
464                                 if(counterX >= 2*divX)
465                                 {
466                                         counterX = 0;
467                                 }
468                                 else if(counterY >= 2*divY)
469                                 {
470                                         counterY = 0;
471                                 }
472                         
473                                 counterY++;
474                         }
475                         counterX++;
476                 }
477         }
478         else if(imageType == VTK_UNSIGNED_INT)
479         {
480                 //POINTERS: 
481                 unsigned int* dataImagePointer1 = NULL;
482                 unsigned int* dataImagePointer2 = NULL;
483                 unsigned int* dataImageResultPointer = NULL;
484         
485                 dataImagePointer1 = (unsigned int*) img1->GetScalarPointer(0,0,0);
486                 dataImagePointer2 = (unsigned int*) img2->GetScalarPointer(0,0,0);
487                 dataImageResultPointer = (unsigned int*) newImage->GetScalarPointer(0,0,0);
488         
489                 if(squaresX == 0)
490                 {
491                         squaresX = 1;
492                 }
493                 if(squaresY == 0)
494                 {
495                         squaresY = 1;
496                 }    
497
498                 int divX = sizeX/squaresX;
499                 int divY = sizeY/squaresY;
500                 int i, j, counterX, counterY;
501                 for(i = 0; i < sizeX; i++)
502                 {
503                         counterY = 0; 
504                         for(j = 0; j < sizeY; j++)
505                         {
506                                 dataImagePointer1 = (unsigned int*)img1->GetScalarPointer(i,j,0);
507                                 dataImagePointer2 = (unsigned int*)img2->GetScalarPointer(i,j,0);
508                                 dataImageResultPointer = (unsigned int*)newImage->GetScalarPointer(i,j,0);
509
510                                 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
511                                 {
512                                         *dataImageResultPointer = (unsigned int) *dataImagePointer1;
513                                 }
514                                 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
515                                 {
516                                         *dataImageResultPointer = (unsigned int) *dataImagePointer2;
517                                 }
518
519                                 if(counterX >= 2*divX)
520                                 {
521                                         counterX = 0;
522                                 }
523                                 else if(counterY >= 2*divY)
524                                 {
525                                         counterY = 0;
526                                 }
527                         
528                                 counterY++;
529                         }
530                         counterX++;
531                 }
532         }
533         else if(imageType == VTK_LONG)
534         {
535                 //POINTERS: 
536                 long* dataImagePointer1 = NULL;
537                 long* dataImagePointer2 = NULL;
538                 long* dataImageResultPointer = NULL;
539         
540                 dataImagePointer1 = (long*) img1->GetScalarPointer(0,0,0);
541                 dataImagePointer2 = (long*) img2->GetScalarPointer(0,0,0);
542                 dataImageResultPointer = (long*) newImage->GetScalarPointer(0,0,0);
543         
544                 if(squaresX == 0)
545                 {
546                         squaresX = 1;
547                 }
548                 if(squaresY == 0)
549                 {
550                         squaresY = 1;
551                 }    
552
553                 int divX = sizeX/squaresX;
554                 int divY = sizeY/squaresY;
555                 int i, j, counterX, counterY;
556                 for(i = 0; i < sizeX; i++)
557                 {
558                         counterY = 0; 
559                         for(j = 0; j < sizeY; j++)
560                         {
561                                 dataImagePointer1 = (long*)img1->GetScalarPointer(i,j,0);
562                                 dataImagePointer2 = (long*)img2->GetScalarPointer(i,j,0);
563                                 dataImageResultPointer = (long*)newImage->GetScalarPointer(i,j,0);
564
565                                 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
566                                 {
567                                         *dataImageResultPointer = (long) *dataImagePointer1;
568                                 }
569                                 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
570                                 {
571                                         *dataImageResultPointer = (long) *dataImagePointer2;
572                                 }
573
574                                 if(counterX >= 2*divX)
575                                 {
576                                         counterX = 0;
577                                 }
578                                 else if(counterY >= 2*divY)
579                                 {
580                                         counterY = 0;
581                                 }
582                         
583                                 counterY++;
584                         }
585                         counterX++;
586                 }
587         }
588         else if(imageType == VTK_UNSIGNED_LONG)
589         {
590                 //POINTERS: 
591                 unsigned long* dataImagePointer1 = NULL;
592                 unsigned long* dataImagePointer2 = NULL;
593                 unsigned long* dataImageResultPointer = NULL;
594         
595                 dataImagePointer1 = (unsigned long*) img1->GetScalarPointer(0,0,0);
596                 dataImagePointer2 = (unsigned long*) img2->GetScalarPointer(0,0,0);
597                 dataImageResultPointer = (unsigned long*) newImage->GetScalarPointer(0,0,0);
598         
599                 if(squaresX == 0)
600                 {
601                         squaresX = 1;
602                 }
603                 if(squaresY == 0)
604                 {
605                         squaresY = 1;
606                 }    
607
608                 int divX = sizeX/squaresX;
609                 int divY = sizeY/squaresY;
610                 int i, j, counterX, counterY;
611                 for(i = 0; i < sizeX; i++)
612                 {
613                         counterY = 0; 
614                         for(j = 0; j < sizeY; j++)
615                         {
616                                 dataImagePointer1 = (unsigned long*)img1->GetScalarPointer(i,j,0);
617                                 dataImagePointer2 = (unsigned long*)img2->GetScalarPointer(i,j,0);
618                                 dataImageResultPointer = (unsigned long*)newImage->GetScalarPointer(i,j,0);
619
620                                 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
621                                 {
622                                         *dataImageResultPointer = (unsigned long) *dataImagePointer1;
623                                 }
624                                 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
625                                 {
626                                         *dataImageResultPointer = (unsigned long) *dataImagePointer2;
627                                 }
628
629                                 if(counterX >= 2*divX)
630                                 {
631                                         counterX = 0;
632                                 }
633                                 else if(counterY >= 2*divY)
634                                 {
635                                         counterY = 0;
636                                 }                       
637                                 counterY++;
638                         }
639                         counterX++;
640                 }
641         }
642         else if(imageType == VTK_FLOAT)
643         {
644                 //POINTERS: 
645                 float* dataImagePointer1 = NULL;
646                 float* dataImagePointer2 = NULL;
647                 float* dataImageResultPointer = NULL;
648         
649                 dataImagePointer1 = (float*) img1->GetScalarPointer(0,0,0);
650                 dataImagePointer2 = (float*) img2->GetScalarPointer(0,0,0);
651                 dataImageResultPointer = (float*) newImage->GetScalarPointer(0,0,0);
652         
653                 if(squaresX == 0)
654                 {
655                         squaresX = 1;
656                 }
657                 if(squaresY == 0)
658                 {
659                         squaresY = 1;
660                 }    
661
662                 int divX = sizeX/squaresX;
663                 int divY = sizeY/squaresY;
664                 int i, j, counterX, counterY;
665                 for(i = 0; i < sizeX; i++)
666                 {
667                         counterY = 0; 
668                         for(j = 0; j < sizeY; j++)
669                         {
670                                 dataImagePointer1 = (float*)img1->GetScalarPointer(i,j,0);
671                                 dataImagePointer2 = (float*)img2->GetScalarPointer(i,j,0);
672                                 dataImageResultPointer = (float*)newImage->GetScalarPointer(i,j,0);
673
674                                 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
675                                 {
676                                         *dataImageResultPointer = (float) *dataImagePointer1;
677                                 }
678                                 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
679                                 {
680                                         *dataImageResultPointer = (float) *dataImagePointer2;
681                                 }
682
683                                 if(counterX >= 2*divX)
684                                 {
685                                         counterX = 0;
686                                 }
687                                 else if(counterY >= 2*divY)
688                                 {
689                                         counterY = 0;
690                                 }
691                         
692                                 counterY++;
693                         }
694                         counterX++;
695                 }
696         }
697         else if(imageType == VTK_DOUBLE)
698         {
699                 //POINTERS: 
700                 double* dataImagePointer1 = NULL;
701                 double* dataImagePointer2 = NULL;
702                 double* dataImageResultPointer = NULL;
703         
704                 dataImagePointer1 = (double*) img1->GetScalarPointer(0,0,0);
705                 dataImagePointer2 = (double*) img2->GetScalarPointer(0,0,0);
706                 dataImageResultPointer = (double*) newImage->GetScalarPointer(0,0,0);
707         
708                 if(squaresX == 0)
709                 {
710                         squaresX = 1;
711                 }
712                 if(squaresY == 0)
713                 {
714                         squaresY = 1;
715                 }    
716
717                 int divX = sizeX/squaresX;
718                 int divY = sizeY/squaresY;
719                 int i, j, counterX, counterY;
720                 for(i = 0; i < sizeX; i++)
721                 {
722                         counterY = 0; 
723                         for(j = 0; j < sizeY; j++)
724                         {
725                                 dataImagePointer1 = (double*)img1->GetScalarPointer(i,j,0);
726                                 dataImagePointer2 = (double*)img2->GetScalarPointer(i,j,0);
727                                 dataImageResultPointer = (double*)newImage->GetScalarPointer(i,j,0);
728
729                                 if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
730                                 {
731                                         *dataImageResultPointer = (double) *dataImagePointer1;
732                                 }
733                                 else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
734                                 {
735                                         *dataImageResultPointer = (double) *dataImagePointer2;
736                                 }
737
738                                 if(counterX >= 2*divX)
739                                 {
740                                         counterX = 0;
741                                 }
742                                 else if(counterY >= 2*divY)
743                                 {
744                                         counterY = 0;
745                                 }                       
746                                 counterY++;
747                         }
748                         counterX++;
749                 }
750         }
751
752                 
753         //std::cout << "The image has been checkboardized!" << std::endl;
754    newImage->Update();
755 //   newImage->Modified();
756 }
757
758 /*
759 * Get new image
760 */
761 //------------------------------------------------------------
762 vtkImageData* CheckBoard::getFilteredImage()
763 {
764         if(processed){
765                 return newImage;
766         }
767         else{
768                 return NULL;
769         }
770 }
771
772 //------------------------------------------------------------
773 void CheckBoard::setInputImage1(vtkImageData *_image)
774 {
775         image1=_image;
776 }
777
778 //------------------------------------------------------------
779 void CheckBoard::setInputImage2(vtkImageData *_image)
780 {
781         image2=_image;
782 }
783
784 //------------------------------------------------------------
785 void CheckBoard::setCols(int cols)
786 {
787         squaresX=cols;
788 }
789
790 //------------------------------------------------------------
791 void CheckBoard::setRows(int rows)
792 {
793         squaresY=rows;
794 }
795