I am doing an image rotation method. It takes two matricies and a degree of rotation. It rotates original matrix by amount of degrees and stores it into rotated matrix. I have the following "normal" code (for CPU - taken from this site - http://sinepost.wordpress.com/2012/07/24/image-rotation/) and it is working as it should;
static void RotateImage(unsigned char original[RAW_HEIGHT][RAW_WIDTH] , unsigned char rotated[RAW_HEIGHT][RAW_WIDTH] , int degrees)
{
double centerX = RAW_WIDTH/2;
double centerY = RAW_HEIGHT/2;
for(int x = 0; x< RAW_HEIGHT;x++)
{
for (int y = 0; y < RAW_WIDTH; y++)
{
double dir = calculateDirection(x-centerX,y-centerY);
double mag = calculateMagnitude(x-centerX,y-centerY);
dir-=degrees;
int origX = (int)(centerX + calculateX(dir,mag));
int origY = (int)(centerY + calculateY(dir,mag));
if (origX >= 0 && origX < RAW_HEIGHT && origY >= 0 && origY < RAW_WIDTH)
{
rotated[x][y] = original[origX][origY];
}
}
}
}
I would like to transfer this code to CUDA code. Here is my version:
#define RAW_WIDTH 1600*3
#define RAW_HEIGHT 1200
unsigned char *dev_original_image;
unsigned char *dev_rotated_image;
__global__ void rotatePicture(unsigned char *original, unsigned char *rotated, int degrees)
{
int x = threadIdx.x + blockDim.x * blockIdx.x;
int y = threadIdx.y + blockDim.y * blockIdx.y;
int offset_rotated = x + y * blockDim.x * gridDim.x;
double centerX = 2400.0;
double centerY = 600.0;
double dir = (atan2(y-centerY,x-centerX))*180/3.14159265;
double mag = sqrt((x-centerX)*(x-centerX) + (y-centerY)*(y-centerY));
dir = dir - degrees;
int origX = (int)(centerX + cos((dir*3.14159265/180)) * mag);
int origY = (int)(centerY + sin((dir*3.14159265/180)) * mag);
int offset_original = origX + origY * blockDim.x * gridDim.x;
if(offset_original > 0 && offset_original < RAW_HEIGHT*RAW_WIDTH)
*(rotated + offset_rotated) = *(original + offset_original);
}
but it doesn't give me same result as CPU part. I think the problem is in passing arguments of CUDA kerenl. I am passing them as 2D arrays, is this ok? Can someone explain this to me? Here is my kerenl configuration and call:
dim3 BlockPerGrid(450,400,1);
dim3 ThreadsPerGrid(8,4,1);
cudaMalloc((void**)&dev_original_image,sizeof(unsigned char)*RAW_HEIGHT*RAW_WIDTH);
cudaMalloc((void**)&dev_rotated_image,sizeof(unsigned char)*RAW_HEIGHT*RAW_WIDTH);
cudaMemcpy(dev_original_image, raw_image2D, sizeof(unsigned char)*RAW_HEIGHT*RAW_WIDTH,cudaMemcpyHostToDevice);
cudaMemcpy(dev_rotated_image, raw_image2D_rotated, sizeof(unsigned char)*RAW_HEIGHT*RAW_WIDTH, cudaMemcpyHostToDevice);
rotatePicture<<<BlockPerGrid,ThreadsPerGrid>>>(dev_original_image,dev_rotated_image, deg);
Thank you for your advices!
NOTE: I modified my code and is working better but still not correct.