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
|
#include <daguerre/ppm.hpp>
namespace daguerre {
//TODO: make this more robust
static unsigned read_text_int(FILE *input) {
unsigned n = 0;
char ch;
fread(&ch, 1, 1, input);
if (ch == '#') {
do
fread(&ch, 1, 1, input);
while (ch != '\n');
fread(&ch, 1, 1, input);
}
do {
n = n * 10 + ch - '0';
fread(&ch, 1, 1, input);
} while (ch >= '0' && ch <= '9');
return n;
}
//TODO: this only supports p6 format, and assumes max < 256
std::optional<image<rgb24>> try_load_ppm(FILE *input) {
char header[3];
if (fread(header, 1, 3, input) != 3)
return {};
if (header[0] != 'P' || header[1] != '6' || header[2] != '\n')
return {};
unsigned width = read_text_int(input);
unsigned height = read_text_int(input);
unsigned max = read_text_int(input);
image<rgb24> im(width, height);
for (unsigned y = 0; y < height; ++y)
for (unsigned x = 0; x < width; ++x) {
if (fread(&im.buffer[y * width + x].r, 1, 1, input) != 1)
return {};
if (fread(&im.buffer[y * width + x].g, 1, 1, input) != 1)
return {};
if (fread(&im.buffer[y * width + x].b, 1, 1, input) != 1)
return {};
}
if (max != 255)
for (unsigned v = 0; v < width * height; ++v) {
im.buffer[v].r = ((uint16_t)im.buffer[v].r * 255) / max;
im.buffer[v].g = ((uint16_t)im.buffer[v].g * 255) / max;
im.buffer[v].b = ((uint16_t)im.buffer[v].b * 255) / max;
}
return im;
}
}
|