diff options
author | Benjamin Franzke <benjaminfranzke@googlemail.com> | 2013-01-31 22:27:36 +0100 |
---|---|---|
committer | Benjamin Franzke <benjaminfranzke@googlemail.com> | 2013-01-31 23:25:13 +0100 |
commit | b1d4e30974dea432cae98a6ed149988d1063e1e6 (patch) | |
tree | a7982c4179f15cd42a97734665ebe06fd62d3800 /src | |
parent | d8ab74a573970ede1fc8674a045eb2429d7e7881 (diff) | |
download | advtime-b1d4e30974dea432cae98a6ed149988d1063e1e6.tar.gz advtime-b1d4e30974dea432cae98a6ed149988d1063e1e6.tar.bz2 advtime-b1d4e30974dea432cae98a6ed149988d1063e1e6.zip |
util: Add pgm_read function
Diffstat (limited to 'src')
-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 */ |