-
Notifications
You must be signed in to change notification settings - Fork 1
/
pgm.h
187 lines (145 loc) · 4.66 KB
/
pgm.h
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#ifndef _PGM_H_
#define _PGM_H_
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PGM_MAGIC "P5"
#ifdef _WIN32
#define STRTOK_R(ptr, del, saveptr) strtok_s(ptr, del, saveptr)
#else
#define STRTOK_R(ptr, del, saveptr) strtok_r(ptr, del, saveptr)
#endif
typedef struct _pgm_t {
int width;
int height;
unsigned char *buf;
} pgm_t;
int readPGM(pgm_t *pgm, const char *filename) {
char *token, *pc, *saveptr;
char *buf;
size_t bufsize;
char del[] = " \t\n";
unsigned char *dot;
long begin, end;
int filesize;
int i, w, h, luma, pixs;
FILE *fp;
if ((fp = fopen(filename, "rb")) == NULL) {
fprintf(stderr, "Failed to open file\n");
return -1;
}
fseek(fp, 0, SEEK_SET);
begin = ftell(fp);
fseek(fp, 0, SEEK_END);
end = ftell(fp);
filesize = (int)(end - begin);
buf = (char *)malloc(filesize * sizeof(char));
fseek(fp, 0, SEEK_SET);
bufsize = fread(buf, filesize * sizeof(char), 1, fp);
fclose(fp);
token = (char *)STRTOK_R(buf, del, &saveptr);
if (strncmp(token, PGM_MAGIC, 2) != 0) {
return -1;
}
token = (char *)STRTOK_R(NULL, del, &saveptr);
if (token[0] == '#') {
token = (char *)STRTOK_R(NULL, "\n", &saveptr);
token = (char *)STRTOK_R(NULL, del, &saveptr);
}
w = strtoul(token, &pc, 10);
token = (char *)STRTOK_R(NULL, del, &saveptr);
h = strtoul(token, &pc, 10);
token = (char *)STRTOK_R(NULL, del, &saveptr);
luma = strtoul(token, &pc, 10);
token = pc + 1;
pixs = w * h;
pgm->buf = (unsigned char *)malloc(pixs * sizeof(unsigned char));
dot = pgm->buf;
for (i = 0; i < pixs; i++, dot++) {
*dot = *token++;
}
pgm->width = w;
pgm->height = h;
return 0;
}
int writePGM(pgm_t *pgm, const char *filename) {
int i, w, h, pixs;
FILE *fp;
unsigned char *dot;
w = pgm->width;
h = pgm->height;
pixs = w * h;
if ((fp = fopen(filename, "wb+")) == NULL) {
fprintf(stderr, "Failed to open file\n");
return -1;
}
fprintf(fp, "%s\n%d %d\n255\n", PGM_MAGIC, w, h);
dot = pgm->buf;
for (i = 0; i < pixs; i++, dot++) {
putc((unsigned char)*dot, fp);
}
fclose(fp);
return 0;
}
int normalizeD2PGM(pgm_t *pgm, double *x) {
int i, j, w, h;
w = pgm->width;
h = pgm->height;
pgm->buf = (unsigned char *)malloc(w * h * sizeof(unsigned char));
double min = 0;
double max = 0;
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
if (max < x[i * w + j])
max = x[i * w + j];
if (min > x[i * w + j])
min = x[i * w + j];
}
}
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
if ((max - min) != 0)
pgm->buf[i * w + j] =
(unsigned char)(255 * (x[i * w + j] - min) /
(max - min));
else
pgm->buf[i * w + j] = 0;
}
}
return 0;
}
int normalizeF2PGM(pgm_t *pgm, float *x) {
int i, j, w, h;
w = pgm->width;
h = pgm->height;
pgm->buf = (unsigned char *)malloc(w * h * sizeof(unsigned char));
float min = 0;
float max = 0;
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
if (max < x[i * w + j])
max = x[i * w + j];
if (min > x[i * w + j])
min = x[i * w + j];
}
}
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
if ((max - min) != 0)
pgm->buf[i * w + j] =
(unsigned char)(255 * (x[i * w + j] - min) /
(max - min));
else
pgm->buf[i * w + j] = 0;
}
}
return 0;
}
int destroyPGM(pgm_t *pgm) {
if (pgm->buf) {
free(pgm->buf);
}
return 0;
}
#endif /* _PGM_H_ */