-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgm_dump.h
303 lines (273 loc) · 6.96 KB
/
pgm_dump.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* Copyright 2014 Douglas Bagnall <[email protected]> LGPL */
#ifndef HAVE_PGM_DUMP
#define HAVE_PGM_DUMP
#define IMAGE_DIR "images/"
#include <string.h>
#include <math.h>
#include "recur-common.h"
/*pgm for greyscale */
static inline void
pgm_dump(const u8 *data, u32 width, u32 height, const char *name)
{
FILE *fh = fopen(name, "w");
if (fh == NULL){
DEBUG("could not open '%s' for writing", name);
return;
}
size_t size = width * height;
fprintf(fh, "P5\n%u %u\n255\n", width, height);
size_t wrote = fwrite(data, 1, size, fh);
if (wrote != size){
fprintf(stderr, "wanted to write %zu bytes; fwrite said %zu\n", size, wrote);
}
fflush(fh);
fclose(fh);
}
static inline void
pgm_dump_auto_name(const u8 *data, u32 width, u32 height,
int stream_no, s64 stream_time, const char *id)
{
static const char PGM_NAME_TEMPLATE[] = IMAGE_DIR "%s-%02d-%013lld-%ux%u.pgm";
if (id == NULL)
id = "img";
char id_truncated[10];
snprintf(id_truncated, sizeof(id_truncated), "%s", id);
char pgm_name[sizeof(PGM_NAME_TEMPLATE) + 60];
snprintf(pgm_name, sizeof(pgm_name), PGM_NAME_TEMPLATE,
id_truncated,
stream_no,
(long long) stream_time,
width, height);
pgm_dump(data,
width,
height,
pgm_name);
}
static inline void
pgm_dump_normalised_float(const float *data, u32 width, u32 height, const char *name)
{
u8 bytes [width * height];
for (u32 i = 0; i < width * height; i++){
int b = data[i] * 255.99f;
if (b < 0)
b = 0;
else if (b > 255)
b = 255;
bytes[i] = (u8)b;
}
pgm_dump(bytes, width, height, name);
}
static inline void
pgm_dump_unnormalised_float(const float *weights, int width, int height, const char *name)
{
float biggest = 1e-35f;
for (int i = 0; i < width * height; i++){
float f = fabsf(weights[i]);
if (f > biggest)
biggest = f;
}
float scale = 255.99f / biggest;
u8 *im = malloc_aligned_or_die(width * height);
for (int i = 0; i < width * height; i++){
im[i] = (u8)(fabs(weights[i]) * scale);
}
pgm_dump(im, width, height, name);
MAYBE_DEBUG("%s biggest is %.2g", name, biggest);
free(im);
}
/*pbm for bitmap */
static inline void
pbm_dump(const u64 *data64,
const int width,
const int height,
const char *name)
{
u8 *data = (u8*)data64;
FILE *fh = fopen(name, "w");
if (fh == NULL){
DEBUG("could not open '%s' for writing", name);
return;
}
fprintf(fh, "P4\n%u %u\n", width, height);
int rx = width;
u8 byte = 0;
int offset = 0;
for (int i = 0; i < width * height;){
byte = data[i / 8];
byte <<= offset;
byte |= data[i / 8 + 1] >> (8 - offset);
putc(~byte, fh);
if (rx < 8){
offset = (offset + rx) & 7;
i += rx;
rx = width;
}
else {
i += 8;
rx -= 8;
}
}
fflush(fh);
fclose(fh);
}
static inline void
putc_colourcoded_float(float f, FILE *fh){
u8 b = fabsf(f);
if (f < 0.0){
putc(b, fh);
putc(0, fh);
putc(0, fh);
}
else if (f > 0.0){
putc(0, fh);
putc(b, fh);
putc(0, fh);
}
else {/* zero is blue */
putc(0, fh);
putc(0, fh);
putc(180, fh);
}
}
static inline void
ppm_dump_signed_unnormalised_float(const float *weights, int width, int height, const char *name)
{
float biggest = 1e-35f;
for (int i = 0; i < width * height; i++){
float f = fabsf(weights[i]);
if (f > biggest)
biggest = f;
}
float scale = 255.99f / biggest;
MAYBE_DEBUG("%s biggest is %.2g", name, biggest);
FILE *fh = fopen(name, "w");
if (fh == NULL){
DEBUG("could not open '%s' for writing", name);
return;
}
fprintf(fh, "P6\n%u %u\n255\n", width, height);
for (int i = 0; i < width * height; i++){
putc_colourcoded_float(weights[i] * scale, fh);
}
fflush(fh);
fclose(fh);
}
static inline void
ppm_dump_signed_unnormalised_colweighted_float(const float *weights, int width, int height, const char *name)
{
float scales[width];
memset(scales, 0, sizeof(scales));
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
float f = fabsf(weights[y * width + x]);
if (f > scales[x])
scales[x] = f;
}
}
for (int i = 0; i < width; i++){
scales[i] = 255.99f / scales[i];
}
FILE *fh = fopen(name, "w");
if (fh == NULL){
DEBUG("could not open '%s' for writing", name);
return;
}
fprintf(fh, "P6\n%u %u\n255\n", width, height);
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
float f = weights[y * width + x] * scales[x];
putc_colourcoded_float(f, fh);
}
}
fflush(fh);
fclose(fh);
}
static inline void
dump_weights_autoname(const float *weights, int width, int height,
const char *desc, int id){
char name[100];
snprintf(name, sizeof(name), IMAGE_DIR "%s-%08d-%dx%d.pgm", desc, id, width, height);
pgm_dump_unnormalised_float(weights, width, height, name);
}
static inline void
dump_colour_weights_autoname(const float *weights, int width, int height,
const char *desc, int id){
char name[100];
snprintf(name, sizeof(name), IMAGE_DIR "%s-%08d-%dx%d.ppm", desc, id, width, height);
ppm_dump_signed_unnormalised_float(weights, width, height, name);
}
enum {
PGM_DUMP_GREY,
PGM_DUMP_COLOUR,
};
typedef struct _TemporalPPM {
float *im;
int width;
int height;
int y;
int id;
char *basename;
int counter;
int mode;
float **source;
} TemporalPPM;
static inline TemporalPPM *
temporal_ppm_alloc(int width, int height, const char *basename, int id,
int mode, float **source){
TemporalPPM *p = malloc(sizeof(TemporalPPM));
p->im = malloc_aligned_or_die(width * height * sizeof(float));
p->width = width;
p->height = height;
p->y = 0;
p->id = id;
p->counter = 0;
p->basename = strdup(basename);
p->mode = mode;
p->source = source;
return p;
}
static inline void
temporal_ppm_free(TemporalPPM *ppm){
free(ppm->im);
free(ppm->basename);
free(ppm);
}
static inline void
temporal_ppm_write(TemporalPPM *ppm){
char name[200];
snprintf(name, sizeof(name), IMAGE_DIR "%s-%d-%08d-%dx%d.ppm",
ppm->basename, ppm->id, ppm->counter, ppm->width, ppm->height);
if (ppm->mode == PGM_DUMP_GREY){
pgm_dump_unnormalised_float(ppm->im, ppm->width, ppm->height, name);
}
else {
ppm_dump_signed_unnormalised_float(ppm->im, ppm->width, ppm->height, name);
}
ppm->y = 0;
ppm->counter += ppm->height;
}
static inline void
temporal_ppm_add_row(TemporalPPM *ppm, const float *row){
if (ppm == NULL){
DEBUG("temporal PPM not initialised!");
return;
}
memcpy(ppm->im + ppm->y * ppm->width, row, ppm->width * sizeof(float));
ppm->y++;
if (ppm->y == ppm->height){
temporal_ppm_write(ppm);
}
}
static inline void
temporal_ppm_row_from_source(TemporalPPM *ppm){
if (ppm == NULL){
DEBUG("temporal PPM not initialised!");
return;
}
memcpy(ppm->im + ppm->y * ppm->width, *ppm->source, ppm->width * sizeof(float));
ppm->y++;
if (ppm->y == ppm->height){
temporal_ppm_write(ppm);
}
}
#endif