-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotate.c
114 lines (93 loc) · 3.2 KB
/
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
Author: Himol Shah
Description: Rotate an image
Details: Please link the mathematical library while compilation
use: "gcc rotate.c -lm"
*/
// Header Files
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <malloc.h>
#include <fcntl.h>
// Main Function
void main()
{
// Declaration of variables
int pix,npix,scan,nscan, npix_out,nscan_out,*fp1,*fp2,cnt,temp,x,y,a,b,i,x0,y0,xx,yy;
char input_file[100],output_file[100];
float theta;
double radians;
// Accepting size and names of input and output files
printf("Enter npix nscan of the image: \n");
scanf("%d %d",&npix,&nscan);
printf("Enter name of the input file: \n");
scanf("%s", input_file);
printf("Enter name of the output file: \n");
scanf("%s",output_file);
// accepting angle of rotation
printf("Enter angle of rotation:\n");
scanf("%f",&theta);
radians = theta*22/(7*180); // computing radians from degrees
// computing npix and nscan for new image
npix_out = ceil(abs(npix*cos(radians))+abs(nscan*sin(radians))) + 1;
nscan_out = ceil(abs(npix*sin(radians))+abs(nscan*cos(radians))) + 1;
// defining x0 and y0
x0 = nscan/2;
y0 = npix/2;
// declaring and allocating memory for entire image
unsigned char **image = (unsigned char **)malloc(nscan * sizeof(unsigned char *));
for (scan=0; scan<nscan; scan++)
image[scan] = (unsigned char *)malloc(npix * sizeof(unsigned char));
unsigned char **image_out = (unsigned char **)malloc(nscan_out * sizeof(unsigned char *));
for (scan=0; scan<nscan_out; scan++)
image_out[scan] = (unsigned char *)malloc(npix_out * sizeof(unsigned char));
// initializing image_out for output image
for(scan = 0; scan < nscan_out; scan++)
{
for(pix = 0; pix < npix_out; pix++)
{
image_out[scan][pix] = 255;
}
}
fp1 = open(input_file,O_RDONLY); // opening the input file
if(fp1<0) // checking for errors while opening the inout file
{
printf("Error in opening the input file. \n");
exit(1);
}
for(scan = 0; scan < nscan; scan++)
{
read(fp1,&image[scan][0],npix*sizeof(unsigned char)); // reading the input file
}
close(fp1); // closing the input image file
fp2 = creat(output_file,0666); // creating a new file
if(fp2 < 0) // checking for errors while creating new file
{
printf("Error in creating the output file. \n");
exit(1);
}
for(scan = 0; scan < nscan_out; scan++)
{
for(pix = 0; pix < npix_out; pix++)
{
// performing change of coordinates operation
x = nscan_out/2-scan;
y = pix - npix_out/2;
// using trigonometric identities for changing the coordinates
xx = (int)(x*cos(-radians) - y*sin(-radians));
yy = (int)(x*sin(-radians) + y*cos(-radians));
xx = nscan/2-xx;
yy = yy+npix/2;
if(xx < 0 || xx >= nscan || yy < 0 || yy >= npix)
image_out[scan][pix] = 255; // blacking out the unwanted part
else
image_out[scan][pix] = image[xx][yy]; // mapping the exact coordinates of input image to the output image
}
}
for(scan = 0; scan < nscan_out; scan++)
{
write(fp2, &image_out[scan][0], npix_out*sizeof(unsigned char)); // writing into the output file
}
close(fp2); // closing the file
}