-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_io.cc
More file actions
255 lines (200 loc) · 6.14 KB
/
data_io.cc
File metadata and controls
255 lines (200 loc) · 6.14 KB
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
#include <cstdio>
#include <cstring>
#include <cassert>
#include <vector>
#include "data_io.h"
// See "data_io.h" for details on the file format.
bool isValidDataFile(const char *filename) {
bool success = false;
FILE *inf = fopen(filename, "rb");
if (!inf) return false;
// try reading the text format
int width = -1, height = -1;
if (2 == fscanf(inf, "%d cols %d rows", &width, &height) &&
width >= 1 && height >= 1) {
success = true;
goto done;
}
// try reading the binary format
char buf[9];
if (8 != fread(buf, 1, 8, inf)) goto done;
buf[8] = 0;
if (!strcmp(buf, "binary \n")) success = true;
done:
fclose(inf);
return success;
}
// check if the given file is in binary format
static bool isBinary(const char *filename) {
FILE *inf = fopen(filename, "rb");
if (!inf) return false;
char buf[9];
if (8 != fread(buf, 1, 8, inf)) return false;
fclose(inf);
buf[8] = 0;
return !strncmp(buf, "binary", 6);
}
// read a file in binary format
static bool readBinaryDataFile(const char *filename, float **data,
int *width, int *height) {
FILE *inf = fopen(filename, "rb");
if (!inf) {
fprintf(stderr, "Failed to open \"%s\" for reading.\n", filename);
return false;
}
// skip the header
fseek(inf, 8, SEEK_SET);
if (1 != fread(width, sizeof(int), 1, inf)) return false;
if (1 != fread(height, sizeof(int), 1, inf)) return false;
// if only the size was needed, return
if (data == NULL) {
fclose(inf);
return true;
}
// check for invalid image sizes
if (*width < 1 || *height < 1) {
fprintf(stderr, "Invalid image size: %d x %d\n", *width, *height);
fclose(inf);
return false;
}
long long unsigned cells = *height * *width;
if (cells > 0xffffffff) {
fprintf(stderr, "Image size too large: %d x %d\n", *width, *height);
fclose(inf);
return false;
}
*data = new float[(int)cells];
if (!*data) {
fprintf(stderr, "Out of memory allocating %d x %d image\n",
*width, *height);
fclose(inf);
return false;
}
if (cells != fread(*data, 4, (int)cells, inf)) return false;
fclose(inf);
return true;
}
static bool readTextDataFile(const char *filename, float **data,
int *width, int *height) {
FILE *inf = fopen(filename, "rt");
if (!inf) {
fprintf(stderr, "Failed to open \"%s\" for reading.\n", filename);
return false;
}
// read the first line
if (2 != fscanf(inf, "%d cols %d rows", width, height)
|| *width < 1 || *height < 1) {
fprintf(stderr, "Bad input file format (width=%d, height=%d)\n",
*width, *height);
fclose(inf);
return false;
}
// if only the size was needed, return
if (data == NULL) {
fclose(inf);
return true;
}
int entryCount = (*width) * (*height);
*data = new float[entryCount];
for (int i=0; i < entryCount; i++) {
float tmp;
if (1 != fscanf(inf, "%f", &tmp)) {
fprintf(stderr, "Bad data: expected %d values, got %d\n", entryCount, i);
fclose(inf);
return false;
}
(*data)[i] = tmp;
}
fclose(inf);
return true;
}
// Read a file of data, automatically detecting whether it's in
// text or binary format.
// On error, print it to stderr and return false.
// Caller is responsible for calling 'delete[]' on *data.
bool readDataFile(const char *filename, float **data, int *width, int *height) {
bool success;
if (isBinary(filename)) {
success = readBinaryDataFile(filename, data, width, height);
} else {
success = readTextDataFile(filename, data, width, height);
}
return success;
}
// Read data as doubles.
bool readDataFile(const char *filename, double **data,
int *width, int *height) {
bool success;
float *floatData;
if (isBinary(filename)) {
success = readBinaryDataFile(filename, &floatData, width, height);
} else {
success = readTextDataFile(filename, &floatData, width, height);
}
if (!success) return false;
int size = *width * *height;
*data = new double[size];
for (int i=0; i < size; i++)
(*data)[i] = floatData[i];
delete[] floatData;
return true;
}
// Write a data file in binary format
// Note: this code assumes the current machine is little endian.
// If this is ported to a big endian machine it will need to be tweaked.
static bool writeBinaryDataFile(const char *filename, float *data,
int width, int height) {
#ifndef NDEBUG
static unsigned endianCheck = 0x11223344;
#endif
FILE *outf = fopen(filename, "wb");
if (!outf) {
fprintf(stderr, "Failed to open \"%s\" for writing.\n", filename);
return false;
}
assert(sizeof(int) == 4);
assert(sizeof(float) == 4);
assert(*((char*)&endianCheck) == 0x44);
// 8 byte header
fwrite("binary \n", 1, 8, outf);
// width and height
fwrite(&width, sizeof(int), 1, outf);
fwrite(&height, sizeof(int), 1, outf);
size_t bytesWritten = fwrite(data, sizeof(float), width*height, outf);
if (bytesWritten != (size_t)(width*height)) {
fprintf(stderr, "Failed to write to \"%s\".\n", filename);
fclose(outf);
return false;
}
fclose(outf);
return true;
}
bool writeDataFile(const char *filename, double *dataDoubles,
int width, int height, bool isBinary) {
float *data = new float[width*height];
for (int i=0; i < width*height; i++)
data[i] = (float)dataDoubles[i];
bool success = writeDataFile(filename, data, width, height, isBinary);
delete[] data;
return success;
}
// Write a data file in text format
bool writeDataFile(const char *filename, float *data, int width, int height,
bool isBinary) {
if (isBinary) return writeBinaryDataFile(filename, data, width, height);
FILE *outf = fopen(filename, "wt");
if (!outf) {
fprintf(stderr, "Failed to open \"%s\" for writing.\n", filename);
return false;
}
fprintf(outf, "%d cols %d rows\n", width, height);
for (int r=0; r < height; r++) {
for (int c=0; c < width; c++) {
if (c > 0) fputc(' ', outf);
fprintf(outf, "%7.5f", *data++);
}
fputc('\n', outf);
}
fclose(outf);
return true;
}