title: Custom rotation function in C with SDL 1.2 not really working
body: <p>I have to do an OCR for scholar purposes.
  I don't know if I can use embedded rotation functions in SDL so I've written a basic rotation function.
  It seems to work but I have some problems: It works only with some image and when it works, the new image is kinda messy. Example:</p>
  
  <p>Image before rotation:</p>
  
  <p><img src="https://i.stack.imgur.com/JGpUT.png" alt="enter image description here"></p>
  
  <p>Image after 60 degree rotation:</p>
  
  <p><img src="https://i.stack.imgur.com/KXspO.png" alt="enter image description here"></p>
  
  <p>Here's my code:</p>
  
  <pre><code>#include &lt;SDL/SDL.h&gt;
  #include &lt;SDL/SDL_image.h&gt;
  #include &lt;math.h&gt;
  #include "image.h"
  
  double deg_to_rad(int angle)
  {
      return (angle*0.017453292519943);
  }
  
  SDL_Surface *rotate(SDL_Surface *image, int angle)
  {
      /* Trigo */
      double cosa = cos(deg_to_rad(angle));
      double sina = sin(deg_to_rad(angle));
  
      int wo = abs((image-&gt;w)*cosa + (image-&gt;h)*sina);
      int ho = abs((image-&gt;h)*cosa + (image-&gt;w)*sina);
  
  
      /* Création d'une nouvelle image */
      SDL_Surface *new_img;
      Uint32 rmask, gmask, bmask, amask;
  
      #if SDL_BYTEORDER == SDL_BIG_ENDIAN
              rmask = 0xff000000;
              gmask = 0x00ff0000;
              bmask = 0x0000ff00;
              amask = 0x000000ff;
      #else
              rmask = 0x000000ff;
              gmask = 0x0000ff00;
              bmask = 0x00ff0000;
              amask = 0xff000000;
      #endif
  
      new_img = SDL_CreateRGBSurface(0,wo,ho,32, rmask, gmask, bmask, amask);
      int center_x = image-&gt;w / 2;
      int center_y = image-&gt;h / 2;
      int new_center_x = wo/2;
      int new_center_y = ho/2;
      for (int x = 0; x &lt; wo; x++)
      {
              for (int y = 0; y &lt; ho; y++)
              {
                      int xo = abs(cosa*(x - new_center_x) + sina*(y - new_center_y) + center_x);
                      int yo = abs((-1)*sina*(x - new_center_x) + cosa*(y - new_center_y) + center_y);
  
                      lockSurface(image);
                      lockSurface(new_img);
  
                      Uint8 r,g,b;
                      Uint32 pixel;
                      Uint32 color;
                      if (xo &gt;= 0 &amp;&amp; yo &gt;= 0 &amp;&amp; xo &lt; image-&gt;w &amp;&amp; yo &lt; image-&gt;h
                          &amp;&amp; x &gt;= 0 &amp;&amp; x &lt; wo &amp;&amp; y &gt;=0 &amp;&amp; y &lt; ho)
                      {
                              pixel = getPixel(image, xo, yo);
                              SDL_GetRGB(pixel, image-&gt;format, &amp;r, &amp;g, &amp;b);
                              color = SDL_MapRGB(image-&gt;format, r, g, b);
                              setPixel(new_img, x, y, color);
                      }
  
                      unlockSurface(image);
                      unlockSurface(new_img);
              }
      }
      return new_img;
  }
  </code></pre>
  
  <p>What am I doing wrong?</p>