forked from abhijitnathwani/image-processing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_rotate.c
97 lines (80 loc) · 2.14 KB
/
image_rotate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* @file image_rotate.c
* @brief C program to rotate the image by 180-degree. ( Flipping)
* @author Priya Shah
* @version v1
* @date 2018-01-10
*/
#include <stdio.h>
int main()
{
FILE *fIn = fopen("lena512.bmp","r"); //Input File name
FILE *fOut = fopen("lena_rotate.bmp","w+"); //Output File name
int i,j,choice;
unsigned char byte[54],colorTable[1024];
if(fIn==NULL) // check if the input file has not been opened succesfully.
{
printf("File does not exist.\n");
}
for(i=0;i<54;i++) //read the 54 byte header from fIn
{
byte[i] = getc(fIn);
}
fwrite(byte,sizeof(unsigned char),54,fOut); //write the header back
// extract image height, width and bitDepth from imageHeader
int height = *(int*)&byte[18];
int width = *(int*)&byte[22];
int bitDepth = *(int*)&byte[28];
printf("width: %d\n",width);
printf("height: %d\n",height );
int size = height*width; //calculate image size
if(bitDepth <= 8) //if ColorTable present, extract it.
{
fread(colorTable,sizeof(unsigned char),1024,fIn);
fwrite(colorTable,sizeof(unsigned char),1024,fOut);
}
unsigned char buffer[width][height]; //to store the image data
unsigned char out_buffer[width][height];
fread(buffer,sizeof(unsigned char),size,fIn); //read the image data
printf("Enter your choice :\n");
printf("1. Rotate right\n");
printf("2. Rotate left\n");
printf("3. Rotate 180\n");
scanf("%d",&choice);
switch(choice) //to rotate image in 3 direction
{
case 1:
for(i=0;i<width;i++) //to rotate right
{
for(j=0;j<height;j++)
{
out_buffer[j][height-1-i]=buffer[i][j];
}
}
break;
case 2:
for(i=0;i<width;i++) //to rotate left
{
for(j=0;j<height;j++)
{
out_buffer[j][i]=buffer[i][j];
}
}
break;
case 3:
for(i=0;i<width;i++) //to rotate 180 degree
{
for(j=0;j<height;j++)
{
out_buffer[width-i][j]=buffer[i][j];
}
}
break;
default:
break;
}
fwrite(out_buffer,sizeof(unsigned char),size,fOut); //write back to the output image
fclose(fIn);
fclose(fOut);
return 0;
}