title: C++: BMP rotate image
body: <p>Ok guys, it's the third time I'm posting the same question (previous are <a href="https://stackoverflow.com/questions/40668599/c-rotate-image-bmp">here</a> and <a href="https://stackoverflow.com/questions/40621200/c-c-rotate-an-bmp-image">here</a>).</p>
  
  <p>Now at this time I will try to explain what's my problem:</p>
  
  <ol>
  <li>So first them all, I need to rotate a <strong>.bmp</strong> image and it's not rotate correctly. But I don't need to rotate a random image with extension <strong>.bmp</strong>, I need to rotate <a href="https://www.dropbox.com/s/aggfmos2umttac2/Input-1.bmp?dl=0" rel="nofollow noreferrer">this one</a>. I've tried with many other images and all of them was rotated correctly, except mine.</li>
  <li>In this moment my code it works just for <strong>180-degree</strong>, how could make it to works on any degree which is multiple of 90-degree (I need to rotate my image just with <strong>90, 180 or 270 degrees</strong>, not more).</li>
  <li>I don't need any kind of external library for this code like <strong>CImage</strong>, <strong>OpenCV</strong>, <strong>ImageMagik</strong> and so on... I need to make this code to work.</li>
  </ol>
  
  <p>So yeh, that's it. And here you can find my actual <a href="https://imgur.com/Vsai1BF" rel="nofollow noreferrer">result</a>. </p>
  
  <p><strong>CODE:</strong></p>
  
  <pre><code>#include &lt;array&gt;
  
  using namespace std;
  
  struct BMP {
      int width;
      int height;
      unsigned char header[54];
      unsigned char *pixels;
      int row_padded;
      int size_padded;
  };
  
  void writeBMP(string filename, BMP image) {
      string fileName = "Output Files\\" + filename;
      FILE *out = fopen(fileName.c_str(), "wb");
      fwrite(image.header, sizeof(unsigned char), 54, out);
  
      unsigned char tmp;
      for (int i = 0; i &lt; image.height; i++) {
          for (int j = 0; j &lt; image.width * 3; j += 3) {
              //Convert(B, G, R) to(R, G, B)
              tmp = image.pixels[j];
              image.pixels[j] = image.pixels[j + 2];
              image.pixels[j + 2] = tmp;
          }
      }
      fwrite(image.pixels, sizeof(unsigned char), image.size_padded, out);
      fclose(out);
  }
  
  BMP readBMP(string filename) {
      BMP image;
      string fileName = "Input Files\\" + filename;
      FILE *in = fopen(fileName.c_str(), "rb");
  
      fread(image.header, sizeof(unsigned char), 54, in); // read the 54-byte header
  
      // extract image height and width from header
      image.width = *(int *) &amp;image.header[18];
      image.height = *(int *) &amp;image.header[22];
  
      image.row_padded = (image.width * 3 + 3) &amp; (~3);     // ok size of a single row rounded up to multiple of 4
      image.size_padded = image.row_padded * image.height;  // padded full size
      image.pixels = new unsigned char[image.size_padded];  // yeah !
  
      if (fread(image.pixels, sizeof(unsigned char), image.size_padded, in) == image.size_padded) {
          unsigned char tmp;
          for (int i = 0; i &lt; image.height; i++) {
              for (int j = 0; j &lt; image.width * 3; j += 3) {
                  //Convert (B, G, R) to (R, G, B)
                  tmp = image.pixels[j];
                  image.pixels[j] = image.pixels[j + 2];
                  image.pixels[j + 2] = tmp;
              }
          }
      }
      fclose(in);
      return image;
  }
  
  BMP rotate(BMP image, double degree) {
      BMP newImage = image;
          unsigned char *pixels = new unsigned char[image.size_padded];
  
          int height = image.height;
          int width = image.width;
          for (int x = 0; x &lt; height; x++) {
              for (int y = 0; y &lt; width; y++) {
                  pixels[(x * width + y) * 3 + 0] = image.pixels[((height - 1 - x) * width + (width - 1 - y)) * 3 + 0];
                  pixels[(x * width + y) * 3 + 1] = image.pixels[((height - 1 - x) * width + (width - 1 - y)) * 3 + 1];
                  pixels[(x * width + y) * 3 + 2] = image.pixels[((height - 1 - x) * width + (width - 1 - y)) * 3 + 2];
              }
          }
          newImage.pixels = pixels;
      return newImage;
  }
  
  int main() {
  
      BMP image = readBMP("Input-1.bmp");
      image = rotate(image, 180);
      writeBMP("Output.bmp", image);
  
      return 0;
  }
  </code></pre>