Showing posts with label image. Show all posts
Showing posts with label image. Show all posts

Friday 6 July 2012

Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?




INPUT  >   OUTPUT        INPUT    >  >    OUTPUT  
4                                        4                                         
1 2 3 4        1 1 1 1           11 12 13 14      41 31 21 11
1 2 3 4        2 2 2 2           21 22 23 24      42 32 22 12
1 2 3 4        3 3 3 3           31 32 33 34      43 33 23 13
1 2 3 4        4 4 4 4           41 42 43 44      44 34 24 14



#include<stdio.h>
void rotate(int I[10][10],int O[10][10],int j,int i)
{
    int k,l,temp;
    for(k=0;k<j;k++)
    {
        temp=I[k+i][i];              // copy of Left in temp
        O[k+i][i]=I[i+j-1][i+k];     // copy bottom to left
        O[i+j-1][i+k]=I[i+j-k-1][i+j-1];   // copy right to bottom
        O[i+j-k-1][i+j-1]=I[i][i+j-k-1];   // copy top to right
        O[i][i+j-k-1]=temp;            // copy temp(left) to top
    }
}// end of rotate

int main()
{
    int I[10][10],O[10][10],i,j,N;
    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
            scanf("%d",&I[i][j]);
    }
    i=0;
    j=N;
    while(j>0)
    {
        rotate(I,O,j,i);  // i is base,  j is size
        j=j-2;
        i++;
    }
    printf("\n\n");
    for(i=0;i<N;i++)
    {
        printf("\n");
        for(j=0;j<N;j++)
            printf("%d ",O[i][j]);
    }
return 0;
}