From 7c0413065745f2cd119bbb1f352770d61be6b21c Mon Sep 17 00:00:00 2001 From: trillos Date: Tue, 13 Oct 2009 08:48:50 +0000 Subject: [PATCH] Added Transparency and Checkboard Libraries --- lib/CheckBoard.cpp | 790 +++++++++++++++++++++++++++++++++++++++++++ lib/CheckBoard.h | 75 ++++ lib/Transparency.cxx | 410 ++++++++++++++++++++++ lib/Transparency.h | 63 ++++ 4 files changed, 1338 insertions(+) create mode 100644 lib/CheckBoard.cpp create mode 100644 lib/CheckBoard.h create mode 100644 lib/Transparency.cxx create mode 100644 lib/Transparency.h diff --git a/lib/CheckBoard.cpp b/lib/CheckBoard.cpp new file mode 100644 index 0000000..30c4f17 --- /dev/null +++ b/lib/CheckBoard.cpp @@ -0,0 +1,790 @@ +#include "CheckBoard.h" + + +/* +* Constructor +*/ +//------------------------------------------------------------ +CheckBoard::CheckBoard() +{ + image1=NULL; + image2=NULL; + squaresX=0; + squaresY=0; + newImage = NULL; + processed=false; +} + +/* +* Destructor +*/ +//------------------------------------------------------------ +CheckBoard::~CheckBoard() +{ + //if (image1 != NULL ) { image1->Delete(); } + //if (image2 != NULL ) { image2->Delete(); } + if (newImage != NULL ) { newImage->Delete(); } +} + +//------------------------------------------------------------ +void CheckBoard::calculateImage() +{ + + if(image1!=NULL && image2!=NULL) + { + + // IMAGE 1 + + // Information from image1 + image1->GetSpacing(spcImg1); + image1->GetExtent(extImg1); + + type = image1->GetScalarType(); + + // The Z dimension is 1 for a 2D image + dimImg1[0] = extImg1[1] - extImg1[0]+1; + dimImg1[1] = extImg1[3] - extImg1[2]+1; + dimImg1[2] = 1; + + //IMAGE 2 + + // Information from image2 + image2->GetSpacing(spcImg2); + image2->GetExtent(extImg2); + + // The Z dimension is 1 for a 2D image + dimImg2[0] = extImg2[1] - extImg2[0]+1; + dimImg2[1] = extImg2[3] - extImg2[2]+1; + dimImg2[2] = 1; + + long numPixelsImg1 = dimImg1[0]*dimImg1[1]; + long numPixelsImg2 = dimImg2[0]*dimImg2[1]; + + double factorX = 1; + double factorY = 1; + vtkImageResample *resample = vtkImageResample::New(); + vtkImageData *result; + if(numPixelsImg1SetInput(image1); + factorX = (double)extImg2[1]/extImg1[1]; + factorY = (double)extImg2[3]/extImg1[3]; + resample->SetAxisMagnificationFactor(0,factorX); + resample->SetAxisMagnificationFactor(1,factorY); + resample->SetInformationInput(image2); + + initialize(dimImg2, spcImg2); + result = resample->GetOutput(); + result->Update(); + createImage(result,image2,dimImg2[0],dimImg2[1]); + + } //if + else if (numPixelsImg1>numPixelsImg2) + { + resample->SetInput(image2); + factorX = (double)extImg1[1]/extImg2[1]; + factorY = (double)extImg1[3]/extImg2[3]; + resample->SetAxisMagnificationFactor(0,factorX); + resample->SetAxisMagnificationFactor(1,factorY); + resample->SetInformationInput(image1); + + initialize(dimImg1, spcImg1); + result = resample->GetOutput(); + + result->Update(); + + createImage(image1,result,dimImg1[0],dimImg1[1]); + } // else if + else + { + //If both images have the same number of pixels, the resultant image will have the + //properties of the first image. + resample->SetInput(image2); + factorX = (double)extImg1[1]/extImg2[1]; + factorY = (double)extImg1[3]/extImg2[3]; + resample->SetAxisMagnificationFactor(0,factorX); + resample->SetAxisMagnificationFactor(1,factorY); + resample->SetInformationInput(image1); + + initialize(dimImg1, spcImg1); + + result = resample->GetOutput(); + result->Update(); + + createImage(image1,result,dimImg1[0],dimImg1[1]); + + } //else + + resample->Delete(); + processed=true; + } +} + +//------------------------------------------------------------ +void CheckBoard::initialize(int dimensions[], double spacing[]) +{ + // Setting the new image + newImage = vtkImageData::New(); + newImage->SetScalarType(type); + newImage->SetSpacing(spacing); + newImage->SetDimensions(dimensions); + newImage->AllocateScalars(); + newImage->Update(); + +} + +//------------------------------------------------------------ +void CheckBoard::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY) +{ + int imageType=img1->GetScalarType(); + + if(imageType == VTK_CHAR) + { + //POINTERS: + char* dataImagePointer1 = NULL; + char* dataImagePointer2 = NULL; + char* dataImageResultPointer = NULL; + + dataImagePointer1 = (char*) img1->GetScalarPointer(0,0,0); + dataImagePointer2 = (char*) img2->GetScalarPointer(0,0,0); + dataImageResultPointer = (char*) newImage->GetScalarPointer(0,0,0); + + if(squaresX == 0) + { + squaresX = 1; + } + if(squaresY == 0) + { + squaresY = 1; + } + + int divX = sizeX/squaresX; + int divY = sizeY/squaresY; + int i, j, counterX, counterY; + for(i = 0; i < sizeX; i++) + { + counterY = 0; + for(j = 0; j < sizeY; j++) + { + dataImagePointer1 = (char*)img1->GetScalarPointer(i,j,0); + dataImagePointer2 = (char*)img2->GetScalarPointer(i,j,0); + dataImageResultPointer = (char*)newImage->GetScalarPointer(i,j,0); + + if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) ) + { + *dataImageResultPointer = (char) *dataImagePointer1; + } + else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) ) + { + *dataImageResultPointer = (char) *dataImagePointer2; + } + + if(counterX >= 2*divX) + { + counterX = 0; + } + else if(counterY >= 2*divY) + { + counterY = 0; + } + + counterY++; + } + counterX++; + } + + } + else if(imageType == VTK_SIGNED_CHAR) + { + //POINTERS: + signed char* dataImagePointer1 = NULL; + signed char* dataImagePointer2 = NULL; + signed char* dataImageResultPointer = NULL; + + dataImagePointer1 = (signed char*) img1->GetScalarPointer(0,0,0); + dataImagePointer2 = (signed char*) img2->GetScalarPointer(0,0,0); + dataImageResultPointer = (signed char*) newImage->GetScalarPointer(0,0,0); + + if(squaresX == 0) + { + squaresX = 1; + } + if(squaresY == 0) + { + squaresY = 1; + } + + int divX = sizeX/squaresX; + int divY = sizeY/squaresY; + int i, j, counterX, counterY; + for(i = 0; i < sizeX; i++) + { + counterY = 0; + for(j = 0; j < sizeY; j++) + { + dataImagePointer1 = (signed char*)img1->GetScalarPointer(i,j,0); + dataImagePointer2 = (signed char*)img2->GetScalarPointer(i,j,0); + dataImageResultPointer = (signed char*)newImage->GetScalarPointer(i,j,0); + + if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) ) + { + *dataImageResultPointer = (signed char) *dataImagePointer1; + } + else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) ) + { + *dataImageResultPointer = (signed char) *dataImagePointer2; + } + + if(counterX >= 2*divX) + { + counterX = 0; + } + else if(counterY >= 2*divY) + { + counterY = 0; + } + + counterY++; + } + counterX++; + } + } + else if(imageType == VTK_UNSIGNED_CHAR) + { + //POINTERS: + unsigned char* dataImagePointer1 = NULL; + unsigned char* dataImagePointer2 = NULL; + unsigned char* dataImageResultPointer = NULL; + + dataImagePointer1 = (unsigned char*) img1->GetScalarPointer(0,0,0); + dataImagePointer2 = (unsigned char*) img2->GetScalarPointer(0,0,0); + dataImageResultPointer = (unsigned char*) newImage->GetScalarPointer(0,0,0); + + if(squaresX == 0) + { + squaresX = 1; + } + if(squaresY == 0) + { + squaresY = 1; + } + + int divX = sizeX/squaresX; + int divY = sizeY/squaresY; + int i, j, counterX, counterY; + for(i = 0; i < sizeX; i++) + { + counterY = 0; + for(j = 0; j < sizeY; j++) + { + dataImagePointer1 = (unsigned char*)img1->GetScalarPointer(i,j,0); + dataImagePointer2 = (unsigned char*)img2->GetScalarPointer(i,j,0); + dataImageResultPointer = (unsigned char*)newImage->GetScalarPointer(i,j,0); + + if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) ) + { + *dataImageResultPointer = (unsigned char) *dataImagePointer1; + } + else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) ) + { + *dataImageResultPointer = (unsigned char) *dataImagePointer2; + } + + if(counterX >= 2*divX) + { + counterX = 0; + } + else if(counterY >= 2*divY) + { + counterY = 0; + } + + counterY++; + } + counterX++; + } + } + else if(imageType == VTK_SHORT) + { + //POINTERS: + short* dataImagePointer1 = NULL; + short* dataImagePointer2 = NULL; + short* dataImageResultPointer = NULL; + + dataImagePointer1 = (short*) img1->GetScalarPointer(0,0,0); + dataImagePointer2 = (short*) img2->GetScalarPointer(0,0,0); + dataImageResultPointer = (short*) newImage->GetScalarPointer(0,0,0); + + if(squaresX == 0) + { + squaresX = 1; + } + if(squaresY == 0) + { + squaresY = 1; + } + + int divX = sizeX/squaresX; + int divY = sizeY/squaresY; + int i, j, counterX, counterY; + for(i = 0; i < sizeX; i++) + { + counterY = 0; + for(j = 0; j < sizeY; j++) + { + dataImagePointer1 = (short*)img1->GetScalarPointer(i,j,0); + dataImagePointer2 = (short*)img2->GetScalarPointer(i,j,0); + dataImageResultPointer = (short*)newImage->GetScalarPointer(i,j,0); + + if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) ) + { + *dataImageResultPointer = (short) *dataImagePointer1; + } + else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) ) + { + *dataImageResultPointer = (short) *dataImagePointer2; + } + + if(counterX >= 2*divX) + { + counterX = 0; + } + else if(counterY >= 2*divY) + { + counterY = 0; + } + + counterY++; + } + counterX++; + } + } + else if(imageType == VTK_UNSIGNED_SHORT) + { + //POINTERS: + unsigned short* dataImagePointer1 = NULL; + unsigned short* dataImagePointer2 = NULL; + unsigned short* dataImageResultPointer = NULL; + + dataImagePointer1 = (unsigned short*) img1->GetScalarPointer(0,0,0); + dataImagePointer2 = (unsigned short*) img2->GetScalarPointer(0,0,0); + dataImageResultPointer = (unsigned short*) newImage->GetScalarPointer(0,0,0); + + if(squaresX == 0) + { + squaresX = 1; + } + if(squaresY == 0) + { + squaresY = 1; + } + + int divX = sizeX/squaresX; + int divY = sizeY/squaresY; + int i, j, counterX, counterY; + for(i = 0; i < sizeX; i++) + { + counterY = 0; + for(j = 0; j < sizeY; j++) + { + dataImagePointer1 = (unsigned short*)img1->GetScalarPointer(i,j,0); + dataImagePointer2 = (unsigned short*)img2->GetScalarPointer(i,j,0); + dataImageResultPointer = (unsigned short*)newImage->GetScalarPointer(i,j,0); + + if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) ) + { + *dataImageResultPointer = (unsigned short) *dataImagePointer1; + } + else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) ) + { + *dataImageResultPointer = (unsigned short) *dataImagePointer2; + } + + if(counterX >= 2*divX) + { + counterX = 0; + } + else if(counterY >= 2*divY) + { + counterY = 0; + } + + counterY++; + } + counterX++; + } + } + + else if(imageType == VTK_INT) + { + //POINTERS: + int* dataImagePointer1 = NULL; + int* dataImagePointer2 = NULL; + int* dataImageResultPointer = NULL; + + dataImagePointer1 = (int*) img1->GetScalarPointer(0,0,0); + dataImagePointer2 = (int*) img2->GetScalarPointer(0,0,0); + dataImageResultPointer = (int*) newImage->GetScalarPointer(0,0,0); + + if(squaresX == 0) + { + squaresX = 1; + } + if(squaresY == 0) + { + squaresY = 1; + } + + int divX = sizeX/squaresX; + int divY = sizeY/squaresY; + int i, j, counterX, counterY; + for(i = 0; i < sizeX; i++) + { + counterY = 0; + for(j = 0; j < sizeY; j++) + { + dataImagePointer1 = (int*)img1->GetScalarPointer(i,j,0); + dataImagePointer2 = (int*)img2->GetScalarPointer(i,j,0); + dataImageResultPointer = (int*)newImage->GetScalarPointer(i,j,0); + + if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) ) + { + *dataImageResultPointer = (int) *dataImagePointer1; + } + else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) ) + { + *dataImageResultPointer = (int) *dataImagePointer2; + } + + if(counterX >= 2*divX) + { + counterX = 0; + } + else if(counterY >= 2*divY) + { + counterY = 0; + } + + counterY++; + } + counterX++; + } + } + else if(imageType == VTK_UNSIGNED_INT) + { + //POINTERS: + unsigned int* dataImagePointer1 = NULL; + unsigned int* dataImagePointer2 = NULL; + unsigned int* dataImageResultPointer = NULL; + + dataImagePointer1 = (unsigned int*) img1->GetScalarPointer(0,0,0); + dataImagePointer2 = (unsigned int*) img2->GetScalarPointer(0,0,0); + dataImageResultPointer = (unsigned int*) newImage->GetScalarPointer(0,0,0); + + if(squaresX == 0) + { + squaresX = 1; + } + if(squaresY == 0) + { + squaresY = 1; + } + + int divX = sizeX/squaresX; + int divY = sizeY/squaresY; + int i, j, counterX, counterY; + for(i = 0; i < sizeX; i++) + { + counterY = 0; + for(j = 0; j < sizeY; j++) + { + dataImagePointer1 = (unsigned int*)img1->GetScalarPointer(i,j,0); + dataImagePointer2 = (unsigned int*)img2->GetScalarPointer(i,j,0); + dataImageResultPointer = (unsigned int*)newImage->GetScalarPointer(i,j,0); + + if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) ) + { + *dataImageResultPointer = (unsigned int) *dataImagePointer1; + } + else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) ) + { + *dataImageResultPointer = (unsigned int) *dataImagePointer2; + } + + if(counterX >= 2*divX) + { + counterX = 0; + } + else if(counterY >= 2*divY) + { + counterY = 0; + } + + counterY++; + } + counterX++; + } + } + else if(imageType == VTK_LONG) + { + //POINTERS: + long* dataImagePointer1 = NULL; + long* dataImagePointer2 = NULL; + long* dataImageResultPointer = NULL; + + dataImagePointer1 = (long*) img1->GetScalarPointer(0,0,0); + dataImagePointer2 = (long*) img2->GetScalarPointer(0,0,0); + dataImageResultPointer = (long*) newImage->GetScalarPointer(0,0,0); + + if(squaresX == 0) + { + squaresX = 1; + } + if(squaresY == 0) + { + squaresY = 1; + } + + int divX = sizeX/squaresX; + int divY = sizeY/squaresY; + int i, j, counterX, counterY; + for(i = 0; i < sizeX; i++) + { + counterY = 0; + for(j = 0; j < sizeY; j++) + { + dataImagePointer1 = (long*)img1->GetScalarPointer(i,j,0); + dataImagePointer2 = (long*)img2->GetScalarPointer(i,j,0); + dataImageResultPointer = (long*)newImage->GetScalarPointer(i,j,0); + + if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) ) + { + *dataImageResultPointer = (long) *dataImagePointer1; + } + else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) ) + { + *dataImageResultPointer = (long) *dataImagePointer2; + } + + if(counterX >= 2*divX) + { + counterX = 0; + } + else if(counterY >= 2*divY) + { + counterY = 0; + } + + counterY++; + } + counterX++; + } + } + else if(imageType == VTK_UNSIGNED_LONG) + { + //POINTERS: + unsigned long* dataImagePointer1 = NULL; + unsigned long* dataImagePointer2 = NULL; + unsigned long* dataImageResultPointer = NULL; + + dataImagePointer1 = (unsigned long*) img1->GetScalarPointer(0,0,0); + dataImagePointer2 = (unsigned long*) img2->GetScalarPointer(0,0,0); + dataImageResultPointer = (unsigned long*) newImage->GetScalarPointer(0,0,0); + + if(squaresX == 0) + { + squaresX = 1; + } + if(squaresY == 0) + { + squaresY = 1; + } + + int divX = sizeX/squaresX; + int divY = sizeY/squaresY; + int i, j, counterX, counterY; + for(i = 0; i < sizeX; i++) + { + counterY = 0; + for(j = 0; j < sizeY; j++) + { + dataImagePointer1 = (unsigned long*)img1->GetScalarPointer(i,j,0); + dataImagePointer2 = (unsigned long*)img2->GetScalarPointer(i,j,0); + dataImageResultPointer = (unsigned long*)newImage->GetScalarPointer(i,j,0); + + if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) ) + { + *dataImageResultPointer = (unsigned long) *dataImagePointer1; + } + else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) ) + { + *dataImageResultPointer = (unsigned long) *dataImagePointer2; + } + + if(counterX >= 2*divX) + { + counterX = 0; + } + else if(counterY >= 2*divY) + { + counterY = 0; + } + counterY++; + } + counterX++; + } + } + else if(imageType == VTK_FLOAT) + { + //POINTERS: + float* dataImagePointer1 = NULL; + float* dataImagePointer2 = NULL; + float* dataImageResultPointer = NULL; + + dataImagePointer1 = (float*) img1->GetScalarPointer(0,0,0); + dataImagePointer2 = (float*) img2->GetScalarPointer(0,0,0); + dataImageResultPointer = (float*) newImage->GetScalarPointer(0,0,0); + + if(squaresX == 0) + { + squaresX = 1; + } + if(squaresY == 0) + { + squaresY = 1; + } + + int divX = sizeX/squaresX; + int divY = sizeY/squaresY; + int i, j, counterX, counterY; + for(i = 0; i < sizeX; i++) + { + counterY = 0; + for(j = 0; j < sizeY; j++) + { + dataImagePointer1 = (float*)img1->GetScalarPointer(i,j,0); + dataImagePointer2 = (float*)img2->GetScalarPointer(i,j,0); + dataImageResultPointer = (float*)newImage->GetScalarPointer(i,j,0); + + if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) ) + { + *dataImageResultPointer = (float) *dataImagePointer1; + } + else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) ) + { + *dataImageResultPointer = (float) *dataImagePointer2; + } + + if(counterX >= 2*divX) + { + counterX = 0; + } + else if(counterY >= 2*divY) + { + counterY = 0; + } + + counterY++; + } + counterX++; + } + } + else if(imageType == VTK_DOUBLE) + { + //POINTERS: + double* dataImagePointer1 = NULL; + double* dataImagePointer2 = NULL; + double* dataImageResultPointer = NULL; + + dataImagePointer1 = (double*) img1->GetScalarPointer(0,0,0); + dataImagePointer2 = (double*) img2->GetScalarPointer(0,0,0); + dataImageResultPointer = (double*) newImage->GetScalarPointer(0,0,0); + + if(squaresX == 0) + { + squaresX = 1; + } + if(squaresY == 0) + { + squaresY = 1; + } + + int divX = sizeX/squaresX; + int divY = sizeY/squaresY; + int i, j, counterX, counterY; + for(i = 0; i < sizeX; i++) + { + counterY = 0; + for(j = 0; j < sizeY; j++) + { + dataImagePointer1 = (double*)img1->GetScalarPointer(i,j,0); + dataImagePointer2 = (double*)img2->GetScalarPointer(i,j,0); + dataImageResultPointer = (double*)newImage->GetScalarPointer(i,j,0); + + if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) ) + { + *dataImageResultPointer = (double) *dataImagePointer1; + } + else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) ) + { + *dataImageResultPointer = (double) *dataImagePointer2; + } + + if(counterX >= 2*divX) + { + counterX = 0; + } + else if(counterY >= 2*divY) + { + counterY = 0; + } + counterY++; + } + counterX++; + } + } + + + //std::cout << "The image has been checkboardized!" << std::endl; + newImage->Update(); +// newImage->Modified(); +} + +/* +* Get new image +*/ +//------------------------------------------------------------ +vtkImageData* CheckBoard::getFilteredImage() +{ + if(processed){ + return newImage; + } + else{ + return NULL; + } +} + + +//------------------------------------------------------------ +void CheckBoard::setInputImage1(vtkImageData *_image) +{ + image1=_image; +} + +//------------------------------------------------------------ +void CheckBoard::setInputImage2(vtkImageData *_image) +{ + image2=_image; +} + +//------------------------------------------------------------ +void CheckBoard::setCols(int cols) +{ + squaresX=cols; +} + +//------------------------------------------------------------ +void CheckBoard::setRows(int rows) +{ + squaresY=rows; +} \ No newline at end of file diff --git a/lib/CheckBoard.h b/lib/CheckBoard.h new file mode 100644 index 0000000..cc1ef87 --- /dev/null +++ b/lib/CheckBoard.h @@ -0,0 +1,75 @@ +#include "vtkImageData.h" +#include "vtkImageResample.h" +#include +#include +#include +#include + +class CheckBoard +{ + public: + CheckBoard(); + ~CheckBoard(); + + //Initialize newImage points + void initialize(int dimensions[], double spacing[]); + + //Calculate the new image and save it in the attribute image + void calculateImage(); + + //Gets the result + vtkImageData* getFilteredImage(); + + //Gets the image Size + int getImageSize(); + + //Constructs new image from image 2 + void createImage(vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY); + + void setInputImage1(vtkImageData *_image); + + void setInputImage2(vtkImageData *_image); + + void setCols(int cols); + + void setRows(int rows); + + // --- Atributes --- // + private: + + bool processed; + + int type; + + vtkImageData *image1; + + vtkImageData *image2; + + //Resulting image + vtkImageData *newImage; + + //Total number of divisions for the X axis (colums) + int squaresX; + + //Total number of divisions for the Y axis (rows) + int squaresY; + + // Image 1 extent (iniX, finX, iniY, finY, iniZ, finZ) + int extImg1[6]; + // Dimensionality image 1 + int dimImg1[3]; + // Image 1 spacing + double spcImg1[3]; + + // Image 2 extent (iniX, finX, iniY, finY, iniZ, finZ) + int extImg2[6]; + // Dimensionality image 2 + int dimImg2[3]; + // Image 2 spacing + double spcImg2[3]; + +}; + +//#endif + + diff --git a/lib/Transparency.cxx b/lib/Transparency.cxx new file mode 100644 index 0000000..0cf822d --- /dev/null +++ b/lib/Transparency.cxx @@ -0,0 +1,410 @@ +#include "Transparency.h" + + +/* +* Constructor +*/ +//------------------------------------------------------------ +Transparency::Transparency() +{ + _image1=NULL; + _image2=NULL; + _percent=0; + _newImage = NULL; + _processed=false; +} + +/* +* Destructor +*/ +//------------------------------------------------------------ +Transparency::~Transparency() +{ + if (_newImage != NULL ) { _newImage->Delete(); } +} + +//------------------------------------------------------------ +void Transparency::calculateImage() +{ + + if(_image1!=NULL && _image2!=NULL) + { + + // IMAGE 1 + + // Information from image1 + _image1->GetSpacing(spcImg1); + _image1->GetExtent(extImg1); + _type = _image1->GetScalarType(); + + // The Z dimension is 1 for a 2D image + dimImg1[0] = extImg1[1] - extImg1[0]+1; + dimImg1[1] = extImg1[3] - extImg1[2]+1; + dimImg1[2] = 1; + + //IMAGE 2 + + // Information from image2 + _image2->GetSpacing(spcImg2); + _image2->GetExtent(extImg2); + + // The Z dimension is 1 for a 2D image + dimImg2[0] = extImg2[1] - extImg2[0]+1; + dimImg2[1] = extImg2[3] - extImg2[2]+1; + dimImg2[2] = 1; + + long numPixelsImg1 = dimImg1[0]*dimImg1[1]; + long numPixelsImg2 = dimImg2[0]*dimImg2[1]; + + double factorX = 1; + double factorY = 1; + vtkImageResample *resample = vtkImageResample::New(); + vtkImageData *result; + if(numPixelsImg1SetInput(_image1); + factorX = (double)extImg2[1]/extImg1[1]; + factorY = (double)extImg2[3]/extImg1[3]; + resample->SetAxisMagnificationFactor(0,factorX); + resample->SetAxisMagnificationFactor(1,factorY); + resample->SetInformationInput(_image2); + + initialize(dimImg2, spcImg2); + result = resample->GetOutput(); + result->Update(); + createImage(result,_image2,dimImg2[0],dimImg2[1]); + + } //if + else if (numPixelsImg1>numPixelsImg2) + { + resample->SetInput(_image2); + factorX = (double)extImg1[1]/extImg2[1]; + factorY = (double)extImg1[3]/extImg2[3]; + resample->SetAxisMagnificationFactor(0,factorX); + resample->SetAxisMagnificationFactor(1,factorY); + resample->SetInformationInput(_image1); + + initialize(dimImg1, spcImg1); + result = resample->GetOutput(); + + result->Update(); + + createImage(_image1,result,dimImg1[0],dimImg1[1]); + } // else if + else + { + //If both images have the same number of pixels, the resultant image will have the + //properties of the first image. + resample->SetInput(_image2); + factorX = (double)extImg1[1]/extImg2[1]; + factorY = (double)extImg1[3]/extImg2[3]; + resample->SetAxisMagnificationFactor(0,factorX); + resample->SetAxisMagnificationFactor(1,factorY); + resample->SetInformationInput(_image1); + + initialize(dimImg1, spcImg1); + + result = resample->GetOutput(); + result->Update(); + + createImage(_image1,result,dimImg1[0],dimImg1[1]); + + } //else + + resample->Delete(); + _processed=true; + } +} + +void Transparency::initialize(int dimensions[], double spacing[]) +{ + // Setting the new image + _newImage = vtkImageData::New(); + _newImage->SetScalarType(_type); + _newImage->SetSpacing(spacing); + _newImage->SetDimensions(dimensions); + _newImage->AllocateScalars(); + _newImage->Update(); + +} + +void Transparency::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY) +{ + int i, j; + if(_type == VTK_CHAR) + { + //POINTERS: + char* dataImagePointer1 = NULL; + char* dataImagePointer2 = NULL; + char* dataImageResultPointer = NULL; + + dataImagePointer1 = (char*) img1->GetScalarPointer(0,0,0); + dataImagePointer2 = (char*) img2->GetScalarPointer(0,0,0); + dataImageResultPointer = (char*) _newImage->GetScalarPointer(0,0,0); + for(i = 0; i < sizeX; i++) + { + for(j = 0; j < sizeY; j++) + { + dataImagePointer1 = (char*)img1->GetScalarPointer(i,j,0); + dataImagePointer2 = (char*)img2->GetScalarPointer(i,j,0); + dataImageResultPointer = (char*)_newImage->GetScalarPointer(i,j,0); + + *dataImageResultPointer = (char)*dataImagePointer1*_percent + (char)*dataImagePointer2*(1-_percent); + } + } + } + else if(_type == VTK_SIGNED_CHAR) + { + //POINTERS: + signed char* dataImagePointer1 = NULL; + signed char* dataImagePointer2 = NULL; + signed char* dataImageResultPointer = NULL; + + dataImagePointer1 = (signed char*) img1->GetScalarPointer(0,0,0); + dataImagePointer2 = (signed char*) img2->GetScalarPointer(0,0,0); + dataImageResultPointer = (signed char*) _newImage->GetScalarPointer(0,0,0); + for(i = 0; i < sizeX; i++) + { + for(j = 0; j < sizeY; j++) + { + dataImagePointer1 = (signed char*)img1->GetScalarPointer(i,j,0); + dataImagePointer2 = (signed char*)img2->GetScalarPointer(i,j,0); + dataImageResultPointer = (signed char*)_newImage->GetScalarPointer(i,j,0); + + *dataImageResultPointer = (signed char)*dataImagePointer1*_percent + (signed char)*dataImagePointer2*(1-_percent); + } + } + } + else if(_type == VTK_UNSIGNED_CHAR) + { + //POINTERS: + unsigned char* dataImagePointer1 = NULL; + unsigned char* dataImagePointer2 = NULL; + unsigned char* dataImageResultPointer = NULL; + + dataImagePointer1 = (unsigned char*) img1->GetScalarPointer(0,0,0); + dataImagePointer2 = (unsigned char*) img2->GetScalarPointer(0,0,0); + dataImageResultPointer = (unsigned char*) _newImage->GetScalarPointer(0,0,0); + for(i = 0; i < sizeX; i++) + { + for(j = 0; j < sizeY; j++) + { + dataImagePointer1 = (unsigned char*)img1->GetScalarPointer(i,j,0); + dataImagePointer2 = (unsigned char*)img2->GetScalarPointer(i,j,0); + dataImageResultPointer = (unsigned char*)_newImage->GetScalarPointer(i,j,0); + + *dataImageResultPointer = (unsigned char)*dataImagePointer1*_percent + (unsigned char)*dataImagePointer2*(1-_percent); + } + } + } + else if(_type == VTK_SHORT) + { + //POINTERS: + short* dataImagePointer1 = NULL; + short* dataImagePointer2 = NULL; + short* dataImageResultPointer = NULL; + + dataImagePointer1 = (short*) img1->GetScalarPointer(0,0,0); + dataImagePointer2 = (short*) img2->GetScalarPointer(0,0,0); + dataImageResultPointer = (short*) _newImage->GetScalarPointer(0,0,0); + for(i = 0; i < sizeX; i++) + { + for(j = 0; j < sizeY; j++) + { + dataImagePointer1 = (short*)img1->GetScalarPointer(i,j,0); + dataImagePointer2 = (short*)img2->GetScalarPointer(i,j,0); + dataImageResultPointer = (short*)_newImage->GetScalarPointer(i,j,0); + + *dataImageResultPointer = (short)*dataImagePointer1*_percent + (short)*dataImagePointer2*(1-_percent); + } + } + } + else if(_type == VTK_UNSIGNED_SHORT) + { + //POINTERS: + unsigned short* dataImagePointer1 = NULL; + unsigned short* dataImagePointer2 = NULL; + unsigned short* dataImageResultPointer = NULL; + + dataImagePointer1 = (unsigned short*) img1->GetScalarPointer(0,0,0); + dataImagePointer2 = (unsigned short*) img2->GetScalarPointer(0,0,0); + dataImageResultPointer = (unsigned short*) _newImage->GetScalarPointer(0,0,0); + for(i = 0; i < sizeX; i++) + { + for(j = 0; j < sizeY; j++) + { + dataImagePointer1 = (unsigned short*)img1->GetScalarPointer(i,j,0); + dataImagePointer2 = (unsigned short*)img2->GetScalarPointer(i,j,0); + dataImageResultPointer = (unsigned short*)_newImage->GetScalarPointer(i,j,0); + + *dataImageResultPointer = (unsigned short)*dataImagePointer1*_percent + (unsigned short)*dataImagePointer2*(1-_percent); + } + } + } + else if(_type == VTK_INT) + { + //POINTERS: + int* dataImagePointer1 = NULL; + int* dataImagePointer2 = NULL; + int* dataImageResultPointer = NULL; + + dataImagePointer1 = (int*) img1->GetScalarPointer(0,0,0); + dataImagePointer2 = (int*) img2->GetScalarPointer(0,0,0); + dataImageResultPointer = (int*) _newImage->GetScalarPointer(0,0,0); + for(i = 0; i < sizeX; i++) + { + for(j = 0; j < sizeY; j++) + { + dataImagePointer1 = (int*)img1->GetScalarPointer(i,j,0); + dataImagePointer2 = (int*)img2->GetScalarPointer(i,j,0); + dataImageResultPointer = (int*)_newImage->GetScalarPointer(i,j,0); + + *dataImageResultPointer = (int)*dataImagePointer1*_percent + (int)*dataImagePointer2*(1-_percent); + } + } + } + else if(_type == VTK_UNSIGNED_INT) + { + //POINTERS: + unsigned int* dataImagePointer1 = NULL; + unsigned int* dataImagePointer2 = NULL; + unsigned int* dataImageResultPointer = NULL; + + dataImagePointer1 = (unsigned int*) img1->GetScalarPointer(0,0,0); + dataImagePointer2 = (unsigned int*) img2->GetScalarPointer(0,0,0); + dataImageResultPointer = (unsigned int*) _newImage->GetScalarPointer(0,0,0); + for(i = 0; i < sizeX; i++) + { + for(j = 0; j < sizeY; j++) + { + dataImagePointer1 = (unsigned int*)img1->GetScalarPointer(i,j,0); + dataImagePointer2 = (unsigned int*)img2->GetScalarPointer(i,j,0); + dataImageResultPointer = (unsigned int*)_newImage->GetScalarPointer(i,j,0); + + *dataImageResultPointer = (unsigned int)*dataImagePointer1*_percent + (unsigned int)*dataImagePointer2*(1-_percent); + } + } + } + else if(_type == VTK_LONG) + { + //POINTERS: + long* dataImagePointer1 = NULL; + long* dataImagePointer2 = NULL; + long* dataImageResultPointer = NULL; + + dataImagePointer1 = (long*) img1->GetScalarPointer(0,0,0); + dataImagePointer2 = (long*) img2->GetScalarPointer(0,0,0); + dataImageResultPointer = (long*) _newImage->GetScalarPointer(0,0,0); + for(i = 0; i < sizeX; i++) + { + for(j = 0; j < sizeY; j++) + { + dataImagePointer1 = (long*)img1->GetScalarPointer(i,j,0); + dataImagePointer2 = (long*)img2->GetScalarPointer(i,j,0); + dataImageResultPointer = (long*)_newImage->GetScalarPointer(i,j,0); + + *dataImageResultPointer = (long)*dataImagePointer1*_percent + (long)*dataImagePointer2*(1-_percent); + } + } + } + else if(_type == VTK_UNSIGNED_LONG) + { + //POINTERS: + unsigned long* dataImagePointer1 = NULL; + unsigned long* dataImagePointer2 = NULL; + unsigned long* dataImageResultPointer = NULL; + + dataImagePointer1 = (unsigned long*) img1->GetScalarPointer(0,0,0); + dataImagePointer2 = (unsigned long*) img2->GetScalarPointer(0,0,0); + dataImageResultPointer = (unsigned long*) _newImage->GetScalarPointer(0,0,0); + for(i = 0; i < sizeX; i++) + { + for(j = 0; j < sizeY; j++) + { + dataImagePointer1 = (unsigned long*)img1->GetScalarPointer(i,j,0); + dataImagePointer2 = (unsigned long*)img2->GetScalarPointer(i,j,0); + dataImageResultPointer = (unsigned long*)_newImage->GetScalarPointer(i,j,0); + + *dataImageResultPointer = (unsigned long)*dataImagePointer1*_percent + (unsigned long)*dataImagePointer2*(1-_percent); + } + } + } + else if(_type == VTK_FLOAT) + { + //POINTERS: + float* dataImagePointer1 = NULL; + float* dataImagePointer2 = NULL; + float* dataImageResultPointer = NULL; + + dataImagePointer1 = (float*) img1->GetScalarPointer(0,0,0); + dataImagePointer2 = (float*) img2->GetScalarPointer(0,0,0); + dataImageResultPointer = (float*) _newImage->GetScalarPointer(0,0,0); + for(i = 0; i < sizeX; i++) + { + for(j = 0; j < sizeY; j++) + { + dataImagePointer1 = (float*)img1->GetScalarPointer(i,j,0); + dataImagePointer2 = (float*)img2->GetScalarPointer(i,j,0); + dataImageResultPointer = (float*)_newImage->GetScalarPointer(i,j,0); + + *dataImageResultPointer = (float)*dataImagePointer1*_percent + (float)*dataImagePointer2*(1-_percent); + } + } + } + else if(_type == VTK_DOUBLE) + { + //POINTERS: + double* dataImagePointer1 = NULL; + double* dataImagePointer2 = NULL; + double* dataImageResultPointer = NULL; + + dataImagePointer1 = (double*) img1->GetScalarPointer(0,0,0); + dataImagePointer2 = (double*) img2->GetScalarPointer(0,0,0); + dataImageResultPointer = (double*) _newImage->GetScalarPointer(0,0,0); + for(i = 0; i < sizeX; i++) + { + for(j = 0; j < sizeY; j++) + { + dataImagePointer1 = (double*)img1->GetScalarPointer(i,j,0); + dataImagePointer2 = (double*)img2->GetScalarPointer(i,j,0); + dataImageResultPointer = (double*)_newImage->GetScalarPointer(i,j,0); + + *dataImageResultPointer = (double)*dataImagePointer1*_percent + (double)*dataImagePointer2*(1-_percent); + } + } + } + + _newImage->Update(); +} + +/* +* Get new image +*/ +//------------------------------------------------------------ +vtkImageData* Transparency::getImage() +{ + if(_processed){ + return _newImage; + } + else{ + return NULL; + } +} + +//------------------------------------------------------------ +void Transparency::setImage1(vtkImageData *image) +{ + _image1=image; +} + +//------------------------------------------------------------ +void Transparency::setImage2(vtkImageData *image) +{ + _image2=image; +} + +//------------------------------------------------------------ +void Transparency::setPercent(int percent) +{ + _percent=(double)percent/100.0; +} \ No newline at end of file diff --git a/lib/Transparency.h b/lib/Transparency.h new file mode 100644 index 0000000..c087a95 --- /dev/null +++ b/lib/Transparency.h @@ -0,0 +1,63 @@ +#include "vtkImageData.h" +#include "vtkImageResample.h" +#include +#include +#include +#include + +class Transparency +{ + public: + Transparency(); + ~Transparency(); + + //Calculate the new image and save it in the attribute image + void calculateImage(); + + //Gets the result + vtkImageData* getImage(); + + void setImage1(vtkImageData *image); + + void setImage2(vtkImageData *image); + + void setPercent(int percent); + + //Initialize newImage points + void initialize(int dimensions[], double spacing[]); + + //Constructs new transparent image + void createImage(vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY); + + // --- Atributes --- // + private: + + bool _processed; + + int _type; + + vtkImageData *_image1; + + vtkImageData *_image2; + + //Resulting image + vtkImageData *_newImage; + + //Percent of transparency for the second image + double _percent; + + // Image 1 extent (iniX, finX, iniY, finY, iniZ, finZ) + int extImg1[6]; + // Dimensionality image 1 + int dimImg1[3]; + // Image 1 spacing + double spcImg1[3]; + + // Image 2 extent (iniX, finX, iniY, finY, iniZ, finZ) + int extImg2[6]; + // Dimensionality image 2 + int dimImg2[3]; + // Image 2 spacing + double spcImg2[3]; + +}; \ No newline at end of file -- 2.47.1