Thread: Rotating a .bmp image by a multiple of 90 in C

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    10

    Rotating a .bmp image by a multiple of 90 in C

    I am trying to rotate a bmp image by 90(-270), 180(-180), 270(-90) degrees. But I am not sure how to go about it. I know I have to manipulate the rows and columns for the bitmap. But I am unsure how to. I don't want the answer, just some guidance so I can get there ... Here is what I have:

    Code:
    int rotate(PIXEL* original, int rows, int cols, int rotation,
               PIXEL** new, int* newrows, int* newcols)
    {
    
    
      int c,r;
      *newrows = cols;
      *newcols = rows;
      PIXEL *o;
      PIXEL *n;
      //for 90 or -270 degrees
      if(rotation == 90 || rotation == -270 )
      {
    
    
            for(r = 0; r < rows; r++)
            {
                    for(c = 0; c < cols; c++ )
                    {
                         //not sure what to do 
    
    
    
    
                    }//end of for
            }//end of for
      }//end of if
    
    
    
    
      return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Well, think about it. When you rotate by 90 degrees, what happens to the columns and rows? The transformation is:
    Code:
    rotCol = col*cos(theta) - row*sin(theta)
    rotRow = col*sin(theta) + row*cos(theta)
    which for 90 degrees simplifies to:
    Code:
    rotCol = -row
    rotRow = col
    You can do similar derivations for the other three rotations too.
    Last edited by GReaper; 11-17-2017 at 11:37 PM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    10
    Makes sense, Okay Ill try it now. Thanks for the help by the way. People usually dont like to help much these days.

  4. #4
    Registered User
    Join Date
    Nov 2017
    Posts
    10
    I tried implementing your approach but it didnt quite work :-/. I really don't know why.

    Code:
    int rotate(PIXEL* original, int rows, int cols, int rotation,
               PIXEL** new, int* newrows, int* newcols)
    {
    
    
      int c,r;
      //for 90 or -270 degrees
      if(rotation == 90 || rotation == -270 )
      {
    
    
            for(r = 0; r < rows; r++)
            {
                    for(c = 0; c < cols; c++ )
                    {
                            *newcols = -r;
                            *newrows = c;
    
    
                    }//end of for
            }//end of for
      }//end of if
    
    
    
    
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Nov 2017
    Posts
    10
    I was able to get the rotation of 90 degrees and -90 degrees. Cant figure out the 180 yet...

    Code:
    int rotate(PIXEL* original, int rows, int cols, int rotation,
               PIXEL** new, int* newrows, int* newcols)
    {
    
    
      int c,r;//counters for rows and columns
      *newrows = cols;
      *newcols = rows;
      //allocating space for the new rotated image
      *new = (PIXEL*)malloc(rows*cols*sizeof(PIXEL));
      PIXEL *o;
      PIXEL *n;
      //rotating -90 or 270 degrees (they are the same)
      if(rotation == -90 || rotation == 270 )
      {
            for(r = 0; r < rows; r++)
            {
                    for(c = 0; c < cols; c++ )
                    {
                            n = (*new) + (c * rows) + (rows - r - 1 );
                            o = ((original) + (r * cols) + c);
                            *n = *o;
                    }//end of for
            }//end of for
      }else if(rotation == 90 || rotation == -270)//rotating 90 or -270
      {
            for(r = 0; r < rows; r++)
            {
                    for(c = 0; c < cols; c++ )
                    {
                            n = (*new) + (c * rows) + (rows + r + 1 );
                            o = ((original) + (r * cols) + c);
                            *n = *o;
                    }//end of for
            }//end of for
      }else if(rotation == 180 || rotation == -180)//rotating 180 or -180
      {
            for(r = 0; r < rows; r++)
            {
                    for(c = 0; c < cols; c++ )
                    {
                            n = (*new) + (c * rows) + (rows + r + 1);
                            o = ((original) + (r * cols) + c);
                            *n = *o;
                    }//end of for
            }//end of for
    
    
      }
    
    
    
    
      return 0;
    }

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    My bad, when I gave you that formula, I assumed that you count the center of the image as the origin... which almost nobody does. In that case, the formula should be altered like this:
    Code:
    rotCol = (col - width/2)*cos(theta) - (row - height/2)*sin(theta) + width/2
    rotRow = (col - width/2)*sin(theta) + (row - height/2)*cos(theta) + height/2
    Which for 90 degrees simplifies to:
    Code:
    rotCol = (height + width)/2 - row
    rotRow = (height - width)/2 + col
    for 180 degrees:
    Code:
    rotCol = width - col
    rotRow = height - row
    and for 270 degrees:
    Code:
    rotCol = (width - height)/2 + row
    rotRow = (width + height)/2 - col
    Last edited by GReaper; 11-19-2017 at 12:45 AM.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-07-2012, 08:28 PM
  2. merge multiple image file into one image
    By mr_empty in forum C++ Programming
    Replies: 7
    Last Post: 12-09-2009, 02:12 PM
  3. Pasting image to rotating billboard
    By funwey in forum Game Programming
    Replies: 1
    Last Post: 07-16-2009, 04:29 PM
  4. Burn large nero image to multiple cd's?
    By Waldo2k2 in forum Tech Board
    Replies: 3
    Last Post: 05-20-2004, 08:14 AM

Tags for this Thread