Razali

Image Rotation

Nov 13th, 2014
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1. /*
  2. * <summary> Flips the image by performing 2 rotations clockwise by 90 degrees </summmary>
  3. * <params>
  4. *       "img" = 2 dimensional array containing the image bits
  5. *       "size" = Max size of img's row and column dimensions
  6. * </params>
  7. * <precond> "size" > 0 </precond>
  8. */
  9. void flip(int img[][MAX_SIZE], int size)
  10. {
  11.         /* Flip is equivalent to 2 rotations */
  12.         rotate(img,size);
  13.         rotate(img,size);
  14. }
  15.  
  16. /*
  17. * <summary> Rotates the image clockwise by 90 degrees </summmary>
  18. * <params>
  19. *       "img" = 2 dimensional array containing the image bits
  20. *       "size" = Max size of img's row and column dimensions
  21. * </params>
  22. * <precond> "size" > 0 </precond>
  23. */
  24. void rotate(int img[][MAX_SIZE], int size)
  25. {
  26.         int temp[MAX_SIZE][MAX_SIZE];
  27.         int row, col, newRow,newCol;
  28.        
  29.         /*
  30.         *       Copy each element from img row-wise (from row 0)
  31.         *       to temp array column-wise(from col size-1)
  32.         */
  33.         for(row = 0, newCol = size - 1; row < size; row++, newCol--)
  34.                 for(col = 0, newRow = 0; col < size; col++, newRow++)
  35.                         temp[newRow][newCol] = img[row][col];
  36.  
  37.         /* Copy whole of temp to img array */
  38.         for(row = 0; row< size; row++)
  39.                 for(col = 0; col < size; col++)
  40.                         img[row][col] = temp[row][col];
  41. }
Advertisement
Add Comment
Please, Sign In to add comment