I have a bmp image and I am trying to rotate it by 180 degrees using c.
Here is the code that I got so far, but it does not rotate the image for some reasons(it does nothing with the image). Here h is the height of a picture and w is a width.
int rotate(int h, int w)
{
FILE *infile = fopen("in2.bmp", "rb");
FILE *outfile = fopen("copy3.bmp", "wb");
char header[HEADER_SIZE];
unsigned char pixels[h][w * 3];
fread( header, 1 , HEADER_SIZE , infile);
fread( pixels, 1 , h * w * 3 , infile);
int r,c;
for( r = 0; r < h; r++)
{
for ( c = 0; c < w * 3; c+=1)
{
char temp = pixels[r][c];
pixels[r][c] = pixels[h-r-1][(w*3-1)-c];
pixels[h-r-1][(w*3-1)-c] = temp;
}
}
fwrite( header, sizeof(char) , HEADER_SIZE , outfile);
fwrite( pixels, sizeof(char) , h * w * 3 , outfile);
fclose(infile);
fclose(outfile);
return 0;
}
Thank you!