diff options
-rw-r--r-- | src/util.c | 41 | ||||
-rw-r--r-- | src/util.h | 3 |
2 files changed, 44 insertions, 0 deletions
@@ -1,9 +1,50 @@ +#define _XOPEN_SOURCE 700 #include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> #include "video_decode.h" #include "util.h" int +pgm_read(char *filename, unsigned char **buf, int *xsize, int *ysize) +{ + FILE *f = NULL; + unsigned char *b = NULL; + int width, height; + ssize_t n; + + f = fopen(filename, "r"); + if (f == NULL) + return -1; + + if (fscanf(f, "P5\n%d %d\n255\n", &width, &height) == 0) + return -1; + + if (width * height <= 0) + goto fail; + + if ((b = malloc(width * height)) == NULL) + goto fail; + if ((n = fread(b, 1, width * height, f)) == 0 || n != width * height) + goto fail; + + fclose(f); + + *buf = b; + *xsize = width; + *ysize = height; + + return 0; + +fail: + if (f) fclose(f); + if (b) free(b); + return -1; +} + +int pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, char *filename) { FILE *f; @@ -4,6 +4,9 @@ #include <stdint.h> int +pgm_read(char *filename, unsigned char **buf, int *xsize, int *ysize); + +int pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, char *filename); /* Store half of frame_a and half of frame_b in frame_a */ |