-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzoom.c
101 lines (79 loc) · 2.55 KB
/
zoom.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
/*
Author: Himol Shah
Description: Zoom an image
*/
// 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 zoom;
double rads;
// 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 the zooming factor
printf("Enter the zooming factor:\n");
scanf("%f", &zoom);
// computing new dimesions of the image
npix_out = zoom*npix;
nscan_out = zoom*nscan;
// 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 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 image
}
close(fp1); // closing the input image
fp2 = creat(output_file,0666); // creating new file
if(fp2 < 0) // checking for errors while creating the 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 = (int)scan/zoom;
y = (int)pix/zoom;
image_out[scan][pix] = image[x][y]; // 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 file
}
close(fp2); // closing the file
}