-
Notifications
You must be signed in to change notification settings - Fork 0
/
gray_scale_gradients.c
62 lines (48 loc) · 1.34 KB
/
gray_scale_gradients.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
/*
Author: Himol Shah
Description: Create gradient of gray scale intensities
*/
// Header Files
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<fcntl.h>
// Main Function
void main()
{
// declaration of variables
int nscan, unit, npix, pix, scan, fp, i;
unsigned char *pat;
char output_file[100];
// accepting desires size and name of the image
printf("Enter the size of image: \n");
scanf("%d", &nscan);
printf("Enter name of the output file: ");
scanf("%s", output_file);
// creating a new file
fp = creat(output_file, 0666);
if(fp < 0) // checking for errors while creating the file
{
printf("Error in opening output file");
exit(1);
}
npix = nscan; // creating a square sized image
if(nscan < 255) // checking a special case
unit = 1;
else
unit = nscan/256; // defining a unit as one unit of pixel rows will have same intensity
pat = (unsigned char *)calloc(npix, sizeof(unsigned char)); // assigning memory to the array used for writing in the file
for(scan = 0 ; scan < nscan ; scan++)
{
i = 0;
for(pix = 0; pix < npix ; pix++)
pat[pix] = scan; // assigning pixel values as current scan
while(i < unit) // iterating till 1 unit
{
write(fp, &pat[0], npix*sizeof(unsigned char)); // writing into the file
i = i + 1;
}
}
free(pat); // freeing memory
close(fp); // closing file
}