title: C/C++ rotate an BMP image
body: <p>I'm trying to rotate an BMP image using C/C++ but it's not working.</p>
  
  <p>I've made some functions for read, write and rotation. The read and write functions are working perfectly fine but not rotate from some reason.</p>
  
  <p>EDIT(sin, cos and rotate function)</p>
  
  <p><strong>BMP struct:</strong></p>
  
  <pre><code>struct BMP {
      int width;
      int height;
      unsigned char header[54];
      unsigned char *pixels;
      int size;
  };
  </code></pre>
  
  <p><strong>WRITE:</strong></p>
  
  <pre><code>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);
      int i;
      unsigned char tmp;
      for (i = 0; i &lt; image.size; i += 3) {
          tmp = image.pixels[i];
          image.pixels[i] = image.pixels[i + 2];
          image.pixels[i + 2] = tmp;
      }
      fwrite(image.pixels, sizeof(unsigned char), image.size, out); // read the rest of the data at once
      fclose(out);
  }
  </code></pre>
  
  <p><strong>READ:</strong></p>
  
  <pre><code>BMP readBMP(string filename) {
      BMP image;
      int i;
      string fileName = "Input Files/" + filename;
      FILE *f = fopen(fileName.c_str(), "rb");
      fread(image.header, sizeof(unsigned char), 54, f); // 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.size = 3 * image.width * image.height;
      image.pixels = new unsigned char[image.size]; // allocate 3 bytes per pixel
      fread(image.pixels, sizeof(unsigned char), image.size, f); // read the rest of the data at once
      fclose(f);
  
      for (i = 0; i &lt; image.size; i += 3) {
          unsigned char tmp = image.pixels[i];
          image.pixels[i] = image.pixels[i + 2];
          image.pixels[i + 2] = tmp;
      }
      return image;
  }
  </code></pre>
  
  <p><strong>ROTATE:</strong></p>
  
  <pre><code>BMP rotate(BMP image, double degree) {
      BMP newImage = image;
      unsigned char *pixels = new unsigned char[image.size];
  
      double radians = (degree * M_PI) / 180;
      int sinf = (int) sin(radians);
      int cosf = (int) cos(radians);
  
      double x0 = 0.5 * (image.width - 1);     // point to rotate about
      double y0 = 0.5 * (image.height - 1);     // center of image
  
      // rotation
      for (int x = 0; x &lt; image.width; x++) {
          for (int y = 0; y &lt; image.height; y++) {
              long double a = x - x0;
              long double b = y - y0;
              int xx = (int) (+a * cosf - b * sinf + x0);
              int yy = (int) (+a * sinf + b * cosf + y0);
  
              if (xx &gt;= 0 &amp;&amp; xx &lt; image.width &amp;&amp; yy &gt;= 0 &amp;&amp; yy &lt; image.height) {
                  pixels[(y * image.height + x) * 3 + 0] = image.pixels[(yy * image.height + xx) * 3 + 0];
                  pixels[(y * image.height + x) * 3 + 1] = image.pixels[(yy * image.height + xx) * 3 + 1];
                  pixels[(y * image.height + x) * 3 + 2] = image.pixels[(yy * image.height + xx) * 3 + 2];
              }
          }
      }
      newImage.pixels = pixels;
      return newImage;
  }
  </code></pre>
  
  <p><strong>MAIN:</strong></p>
  
  <pre><code>int main() {
      BMP image = readBMP("InImage_2.bmp");
      image = rotate(image,180);
      writeBMP("Output-11.bmp", image);
      return 0;
  }
  </code></pre>
  
  <p>That <code>sin=0.8939966636</code>(in radians) and <code>cos=-0.44807361612</code>(in radians) means this image should be rotated by 90 degree.</p>
  
  <p>Here it's my original image:</p>
  
  <p><a href="https://i.stack.imgur.com/jxaoz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jxaoz.png" alt="original"></a></p>
  
  <p>And here it's moment my result in:</p>
  
  <p><a href="https://i.stack.imgur.com/ZPfoj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZPfoj.png" alt=" result"></a></p>
  
  <p>Could someone help me understand what I'm doing wrong here? I really need this function to work.</p>
  
  <p>I cannot use any third party libraries for this code.</p>