title: Rotate image in SDL
body: <p>I've got working program that blur an image and I have to make similar function that is rotating image. I wrote algorithm that is inversing 2d array:</p>
  
  <pre><code>int old_data[height][width];
  int new_data[width][height];
  
  for(i=0; i&lt;width; i++) {
      for(j=0; j&lt;height; j++) {
          new_data[i][j] = old_data[height-1-j][i];
      }
  }
  </code></pre>
  
  <p>This is my bluring program:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  #include &lt;SDL/SDL.h&gt;
  #include &lt;SDL/SDL_image.h&gt;
  
  void Filter(unsigned char * buf, int width, int height, int size, char bpp, unsigned short pitch)
  {
    printf("%d %d %d\n", width, height, (int)bpp);
    unsigned char *tmp = (unsigned char*)malloc(pitch*height);
    memcpy(tmp, buf, pitch*height);
    int pix[9], sum, count;
    int x, y, k, i;
    for(y=0; y&lt;height; ++y)
      for(x=0; x&lt;width; ++x)
      {
  
        pix[0] = y*pitch + x*bpp; 
        pix[1] = pix[0] - pitch; 
        pix[2] = pix[0] + pitch; 
        pix[3] = pix[1] - bpp; 
        pix[4] = pix[1] + bpp; 
        pix[5] = pix[0] - bpp; 
        pix[6] = pix[0] + bpp; 
        pix[7] = pix[2] - bpp; 
        pix[8] = pix[2] + bpp; 
  
        for(k=0; k&lt;bpp; ++k)
        {
          sum = 0;
          count = 0;
          for(i=0; i&lt;9; ++i)
          {
            if(pix[i]&gt;=0 &amp;&amp; pix[i]&lt;pitch*height)
            {
              sum += (int) (tmp[ pix[i]+k ]);
              ++count;
            }
          }
          sum /= count;
          buf[ pix[0]+k ] = (unsigned char)sum;
        }
      }
  }
  
  
  SDL_Surface* Load_image(char *file_name)
  {
          /* Open the image file */
          SDL_Surface* tmp = IMG_Load(file_name);
          if ( tmp == NULL ) {
              fprintf(stderr, "Couldn't load %s: %s\n",
                      file_name, SDL_GetError());
                  exit(0);
          }
          return tmp; 
  }
  
  void Paint(SDL_Surface* image, SDL_Surface* screen)
  {
          SDL_BlitSurface(image, NULL, screen, NULL);
          SDL_UpdateRect(screen, 0, 0, 0, 0);
  };
  
  
  
  
  
  int main(int argc, char *argv[])
  {
      Uint32 flags;
      SDL_Surface *screen, *image;
      int depth, done;
      SDL_Event event;
  
      /* Check command line usage */
      if ( ! argv[1] ) {
          fprintf(stderr, "Usage: %s &lt;image_file&gt;, (int) size\n", argv[0]);
          return(1);
      }
  
      if ( ! argv[2] ) {
          fprintf(stderr, "Usage: %s &lt;image_file&gt;, (int) size\n", argv[0]);
          return(1);
      }
  
      /* Initialize the SDL library */
      if ( SDL_Init(SDL_INIT_VIDEO) &lt; 0 ) {
          fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
          return(255);
      }
  
      flags = SDL_SWSURFACE;
      image = Load_image( argv[1] );
      printf( "\n\nImage properts:\n" );
      printf( "BitsPerPixel = %i \n", image-&gt;format-&gt;BitsPerPixel );
      printf( "BytesPerPixel = %i \n", image-&gt;format-&gt;BytesPerPixel );
      printf( "width %d ,height %d \n\n", image-&gt;w, image-&gt;h );       
  
      SDL_WM_SetCaption(argv[1], "showimage");
  
      /* Create a display for the image */
      depth = SDL_VideoModeOK(image-&gt;w, image-&gt;h, 32, flags);
      /* Use the deepest native mode, except that we emulate 32bpp
         for viewing non-indexed images on 8bpp screens */
      if ( depth == 0 ) {
          if ( image-&gt;format-&gt;BytesPerPixel &gt; 1 ) {
              depth = 32;
          } else {
              depth = 8;
          }
      } else
      if ( (image-&gt;format-&gt;BytesPerPixel &gt; 1) &amp;&amp; (depth == 8) ) {
              depth = 32;
      }
      if(depth == 8)
          flags |= SDL_HWPALETTE;
      screen = SDL_SetVideoMode(image-&gt;w, image-&gt;h, depth, flags);
      if ( screen == NULL ) {
          fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",
              image-&gt;w, image-&gt;h, depth, SDL_GetError());
      }
  
      /* Set the palette, if one exists */
      if ( image-&gt;format-&gt;palette ) {
          SDL_SetColors(screen, image-&gt;format-&gt;palette-&gt;colors,
                    0, image-&gt;format-&gt;palette-&gt;ncolors);
      }
  
  
          printf("$$$$$$ %u \n", image-&gt;pitch);
      /* Display the image */
      Paint(image, screen);
  
      done = 0;
      int size =atoi( argv[2] );
      printf("Actual size is: %d\n", size);
      while ( ! done ) {
          if ( SDL_PollEvent(&amp;event) ) {
              switch (event.type) {
                  case SDL_KEYUP:
                  switch (event.key.keysym.sym) {
                      case SDLK_ESCAPE:
                      case SDLK_TAB:
                      case SDLK_q:
                      done = 1;
                      break;
                      case SDLK_SPACE:
                      case SDLK_f:
                      SDL_LockSurface(image);
  
                      printf("Start filtering...  ");
                      Filter(image-&gt;pixels,image-&gt;w,image-&gt;h, size, image-&gt;format-&gt;BytesPerPixel, image-&gt;pitch);
                      printf("Done.\n");
  
                      SDL_UnlockSurface(image);
  
                      printf("Repainting after filtered...  ");
                      Paint(image, screen);
                      printf("Done.\n");
  
                      break;
                      case SDLK_r:
                      printf("Reloading image...  ");
                      image = Load_image( argv[1] );
                      Paint(image,screen);
                      printf("Done.\n");
                      break;
                      case SDLK_PAGEDOWN:
                      case SDLK_DOWN:
                      case SDLK_KP_MINUS:
                      size--;
                      if (size==0) size--;
                      printf("Actual size is: %d\n", size);
                          break;
                      case SDLK_PAGEUP:
                      case SDLK_UP:
                      case SDLK_KP_PLUS:
                      size++;
                      if (size==0) size++;
                      printf("Actual size is: %d\n", size);
                      break;      
                     case SDLK_s:
                      printf("Saving surface at nowy.bmp ...");
                      SDL_SaveBMP(image, "nowy.bmp" ); 
                      printf("Done.\n");
                      default:
                      break;
                  }
                  break;
  //              case  SDL_MOUSEBUTTONDOWN:
  //              done = 1;
  //              break;
                              case SDL_QUIT:
                  done = 1;
                  break;
                  default:
                  break;
              }
          } else {
              SDL_Delay(10);
          }
      }
      SDL_FreeSurface(image);
      /* We're done! */
      SDL_Quit();
      return(0);
  }
  </code></pre>
  
  <p>Could anyone help me to change Filter function that will rotating an image?</p>