summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Franzke <benjaminfranzke@googlemail.com>2013-01-31 22:27:36 +0100
committerBenjamin Franzke <benjaminfranzke@googlemail.com>2013-01-31 23:25:13 +0100
commitb1d4e30974dea432cae98a6ed149988d1063e1e6 (patch)
treea7982c4179f15cd42a97734665ebe06fd62d3800
parentd8ab74a573970ede1fc8674a045eb2429d7e7881 (diff)
downloadadvtime-b1d4e30974dea432cae98a6ed149988d1063e1e6.tar.gz
advtime-b1d4e30974dea432cae98a6ed149988d1063e1e6.tar.bz2
advtime-b1d4e30974dea432cae98a6ed149988d1063e1e6.zip
util: Add pgm_read function
-rw-r--r--src/util.c41
-rw-r--r--src/util.h3
2 files changed, 44 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
index 8b73466..7b7077d 100644
--- a/src/util.c
+++ b/src/util.c
@@ -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;
diff --git a/src/util.h b/src/util.h
index 649e9ee..5921a86 100644
--- a/src/util.h
+++ b/src/util.h
@@ -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 */