1 // Copyright (2013) Jann Horn <jann@thejh.net>
9 HEADER typedef struct {
10 HEADER int width, height;
11 HEADER unsigned char *data;
15 pnm_image *read_image(char *path) {
19 FILE *f = fopen(path, "r");
21 pnm_image *result = malloc(sizeof(pnm_image));
22 if (!result) { saved_errno = errno; goto out_close; }
27 char *buf = malloc(bufsize);
29 assert(fgets(buf, bufsize, f) == buf);
30 if (buf[0] == '#') continue;
33 if (strcmp(buf, "P6\n")) {
34 saved_errno = ENOEXEC;
41 sscanf(buf, "%i %i", &result->width, &result->height);
42 result->data_size = 3 * result->width * result->height;
43 result->data = malloc(result->data_size);
48 if (strcmp(buf, "255\n")) {
62 assert(fread(result->data, result->data_size, 1, f) == 1);
69 if (saved_errno) errno = saved_errno;