-
Notifications
You must be signed in to change notification settings - Fork 0
/
ellipse.c
82 lines (66 loc) · 1.98 KB
/
ellipse.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
/*
Author: Himol Shah
Description: Fill in a ellipse
*/
// Header files
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <malloc.h>
#include <fcntl.h>
// Void Function
void main()
{
// Declaration of variables
int pix, npix, scan, nscan, *fp1,*fp2, x_centre, y_centre;
unsigned char *image;
char output_file[100], input_file[100];
float x,y,a,b;
// Accepting size and names of input and output files
printf("Enter npix nscan of the image: \n");
scanf("%d %d",&npix,&nscan);
image = (unsigned char *)calloc(npix,sizeof(unsigned char)); // assigning memory to the array
// Accepting names of input and output files
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 constants of ellipse
printf("Enter a: \n");
scanf("%f", &a);
printf("Enter b: \n");
scanf("%f", &b);
// computing centre coordinates
x_centre = (npix+1)/2;
y_centre = (nscan+1)/2;
fp1 = open(input_file,O_RDONLY); // opening the input file
if(fp1 < 0) // checking for errors while opening the file
{
printf("Error in opening the input file\n");
exit(1);
}
fp2 = creat(output_file,0666); // creating output file
if(fp2 < 0) // checking for errors while creating the file
{
printf("Error in creating the output file\n");
exit(1);
}
for(scan = 0; scan < nscan; scan++)
{
read(fp1, &image[0], npix*sizeof(unsigned char)); // reading a pixel row from the image
for(pix=0; pix < npix; pix++)
{
// computing distance form centre coordinates
x = pix - x_centre;
y = y_centre - scan;
if(((x*x/(a*a) + y*y/(b*b)) -1<=(float)(1))) //If the point lies inside the ellipse, assign black color to it
{
image[pix] = 0;
}
}
write(fp2,&image[0],npix*sizeof(unsigned char)); // writing into the output file
}
free(image); // freeing memory
close(fp1); // closing file
close(fp2);
}