/*******************************************************
1.6
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?
Note: At first line the parameter N is read.
After the first line
N * N lines are read to store into the image structure,
each line containing 4 bytes pixel data.
This code will perform a anticlockwise rotation of 90 degrees.
Complexity: O(n^2 * k)
Mod Time: 4.22, 2012
Copyright: Ben
*******************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define BYTE_PER_PIX 4
typedef struct
{
short byte[4];
} PIXEL;
int main(void)
{
//ImageN is the one size length of image. i.e. the whole image is n * n.
//Pixel is number of pixel, i.e. n * n
int i, j, k;
int ImageN, Pixel;
PIXEL *ImageMap;
freopen("Image.txt", "r", stdin);
scanf("%d", &ImageN);
Pixel = ImageN * ImageN;
ImageMap = (PIXEL *)malloc(sizeof(PIXEL) * Pixel);
for(i = 0; i < ImageN; i++)
{
for(j = 0; j < ImageN; j++)
{
for(k = 0; k < BYTE_PER_PIX; k++)
{ //scanf should take the address as parameter.
scanf("%hd", &((ImageMap + ImageN * (ImageN - 1 - j) + i)->byte[k])); //ImageMap[N - 1 - j][i]
}
}
}
for(i = 0; i < ImageN; i++)
{
for(j = 0; j < ImageN; j++)
{
for(k = 0; k < BYTE_PER_PIX; k++)
{
printf("%hd\t", (ImageMap + (ImageN * i + j))->byte[k]); //ImageMap[i][j]
}
printf("\n");
}
}
fclose(stdin);
return 0;
}
1.6
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?
Note: At first line the parameter N is read.
After the first line
N * N lines are read to store into the image structure,
each line containing 4 bytes pixel data.
This code will perform a anticlockwise rotation of 90 degrees.
Complexity: O(n^2 * k)
Mod Time: 4.22, 2012
Copyright: Ben
*******************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define BYTE_PER_PIX 4
typedef struct
{
short byte[4];
} PIXEL;
int main(void)
{
//ImageN is the one size length of image. i.e. the whole image is n * n.
//Pixel is number of pixel, i.e. n * n
int i, j, k;
int ImageN, Pixel;
PIXEL *ImageMap;
freopen("Image.txt", "r", stdin);
scanf("%d", &ImageN);
Pixel = ImageN * ImageN;
ImageMap = (PIXEL *)malloc(sizeof(PIXEL) * Pixel);
for(i = 0; i < ImageN; i++)
{
for(j = 0; j < ImageN; j++)
{
for(k = 0; k < BYTE_PER_PIX; k++)
{ //scanf should take the address as parameter.
scanf("%hd", &((ImageMap + ImageN * (ImageN - 1 - j) + i)->byte[k])); //ImageMap[N - 1 - j][i]
}
}
}
for(i = 0; i < ImageN; i++)
{
for(j = 0; j < ImageN; j++)
{
for(k = 0; k < BYTE_PER_PIX; k++)
{
printf("%hd\t", (ImageMap + (ImageN * i + j))->byte[k]); //ImageMap[i][j]
}
printf("\n");
}
}
fclose(stdin);
return 0;
}