forked from uneupane/guided-filter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
inout.c
102 lines (77 loc) · 2.01 KB
/
inout.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void ReadPGM(FILE* fp, unsigned char **image, int *rows, int *cols)
{
int c;
int i,j;
int xdim, ydim;
int val;
int maxraw;
unsigned char *line;
char buf[1024];
unsigned char *temp;
while ((c=fgetc(fp)) == '#')
fgets(buf, 1024, fp);
ungetc(c, fp);
if (fscanf(fp, "P%d\n", &c) != 1) {
printf ("read error ....");
exit(0);
}
if (c != 5 && c != 2) {
printf ("read error ....");
exit(0);
}
if (c==5) {
while ((c=fgetc(fp)) == '#')
fgets(buf, 1024, fp);
ungetc(c, fp);
if (fscanf(fp, "%d%d%d",&xdim, &ydim, &maxraw) != 3) {
printf("failed to read width/height/max\n");
exit(0);
}
printf("Width=%d, Height=%d \nMaximum=%d\n",xdim,ydim,maxraw);
temp = (unsigned char*)malloc(sizeof(unsigned char)*xdim*ydim);
getc(fp);
line = (unsigned char *)malloc(sizeof(unsigned char)*xdim);
for (j=0; j<ydim; j++) {
fread(line, 1, xdim, fp);
for (i=0; i<xdim; i++) {
temp[j*xdim+i] = line[i];
}
}
free(line);
}
else if (c==2) {
while ((c=fgetc(fp)) == '#')
fgets(buf, 1024, fp);
ungetc(c, fp);
if (fscanf(fp, "%d%d%d", &xdim, &ydim, &maxraw) != 3) {
printf("failed to read width/height/max\n");
exit(0);
}
printf("Width=%d, Height=%d \nMaximum=%d,\n",xdim,ydim,maxraw);
temp = (unsigned char*)malloc(sizeof(unsigned char)*xdim*ydim);
getc(fp);
for (j=0; j<ydim; j++)
for (i=0; i<xdim; i++) {
fscanf(fp, "%d", &val);
temp[j*xdim+i] = val;
}
}
*cols = xdim;
*rows = ydim;
*image = temp;
fclose(fp);
}
void WritePGM(int rows, int cols, unsigned char *image,
unsigned char *result, FILE* fp)
{
int i, j;
fprintf(fp, "P5\n%d %d\n%d\n", cols, rows, 255);
for (j=0; j<rows; j++)
for (i=0; i<cols; i++)
fputc(result[j*cols+i], fp);
// printf("%u",result);
fclose(fp);
}