I have an exercise about optimization. I need to optimize a program which rotates and image by 45 degrees. I know accessing arrays using pointers is more efficient, so I tried the changes below- the original code:
RGB* nrgb = (RGB *)malloc(imgSizeXY*3);//3=sizeof(RGB)
//...
for (i=imgSizeY-1; i>=0; --i)
{
for (j=imgSizeX-1; j>=0; --j)
{
//...
int y=(i*imgSizeX+j);
nrgb[y].r = *imgInd; //*imgInd computed earlier
The changes:
RGB* nrgb = (RGB *)malloc(imgSizeXY*3);//3=sizeof(RGB)
RGB* rgbInd = nrgb+imgSizeXY-1;
for (i=imgSizeY-1; i>=0; --i)
{
for (j=imgSizeX-1; j>=0; --j)
{
rgbInd->r=*imgInd;
--rgbInd;
but when using pointers, the program produces an erroneous output. I have been staring at it for hours, and still have no idea why. Any ideas? Thank you very much!