The following code should rotate an image 90 degrees, but it isn’t working. Instead, all i get on my screen after running is grey pixels and one line of the original pixels at the bottom. Any ideas about what I have done wrong in this code?
void rotate90(Image& image)
{
Pixel * tempPixel = new Pixel[(image.infoHeader.biWidth * image.infoHeader.biHeight)];
int tempWidth = image.infoHeader.biWidth;
for(int r = 0; r < image.infoHeader.biHeight; r ++)
{
for(int c = 0; c < image.infoHeader.biWidth; c++)
{
int f = c+(r*image.infoHeader.biWidth);
int t = (image.infoHeader.biHeight - r) + (image.infoHeader.biWidth-1);
tempPixel[t] = image.pixels[f];
}
}
delete[] image.pixels;
image.pixels=tempPixel ;
}
My updated code as of by comment below
void rotate90(Image& image)
{
Pixel * tempPixel = new Pixel[(image.infoHeader.biWidth * image.infoHeader.biHeight)];
int tempWidth = image.infoHeader.biWidth;
image.infoHeader.biWidth = image.infoHeader.biHeight;
image.infoHeader.biHeight = tempWidth;
for(int r = 0; r < image.infoHeader.biHeight; r ++)
{
for(int c = 0; c < image.infoHeader.biWidth; c++)
{
int f = c+(r*image.infoHeader.biWidth);
int t = (image.infoHeader.biHeight-r-1) +(image.infoHeader.biWidth * ((4 * image.infoHeader.biWidth * image.infoHeader.biHeight) % 4));
tempPixel[t] = image.pixels[f];
}
}
delete[] image.pixels;
image.pixels=tempPixel ;
}
You are not using
cin the calculation of the new positiont. It should be multiplying the width I guess