0

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!

Marishka33
  • 21
  • 4
  • It appears you are trying to modify an RGB BMP file, but you are not indexing pixels or RGB values correctly. Give some more careful thought to your indexing scheme. Handle pixel indices first, then use another loop to handle RGB values. Beware of scan line padding! (I also recommend you get the actual dimensions from the file header directly!) – Dúthomhas Oct 18 '17 at 05:28
  • Read [this](https://stackoverflow.com/questions/1968561/getting-the-pixel-value-of-bmp-file) for more info, hope this will help – Eliad Cohen Oct 18 '17 at 06:43
  • You are replacing the first pixel with last pixel, going through the whole loop, then replacing the last pixel with first pixel. So nothing changes. There are many more errors. – Barmak Shemirani Oct 18 '17 at 08:43

0 Answers0