ppm: more methods, make existing one accessible
authorJann Horn <jann@thejh.net>
Tue, 5 Nov 2013 21:57:05 +0000 (22:57 +0100)
committerJann Horn <jann@thejh.net>
Tue, 5 Nov 2013 21:57:05 +0000 (22:57 +0100)
ppm.c

diff --git a/ppm.c b/ppm.c
index cdaebf5..87b18e6 100644 (file)
--- a/ppm.c
+++ b/ppm.c
@@ -12,7 +12,7 @@ HEADER   unsigned char *data;
 HEADER   int data_size;
 HEADER } pnm_image;
 
-pnm_image *read_image(char *path) {
+PUBLIC_FN pnm_image *read_image(char *path) {
   int saved_errno = 0;
   
   // prepare stuff
@@ -68,4 +68,22 @@ out_close:
   fclose(f);
   if (saved_errno) errno = saved_errno;
   return result;
-}
\ No newline at end of file
+}
+
+PUBLIC_FN int write_image(pnm_image *img, char *outfile) {
+  char outbuf[100+img->data_size];
+  int ob_used = snprintf(outbuf, sizeof(outbuf), "P6\n%d %d\n255\n", img->width, img->height);
+  if (ob_used >= 100) xperror("header size is bigger than anticipated", 0);
+  memcpy(outbuf+ob_used, img->data, img->data_size);
+  return write_file(outfile, outbuf, ob_used+img->data_size, 0);
+}
+
+PUBLIC_FN pnm_image *dup_image(pnm_image *img) {
+  pnm_image *r = malloc(sizeof(pnm_image));
+  if (!r) return NULL;
+  *r = *img;
+  r->data = malloc(r->data_size);
+  if (!r->data) { free(r); return NULL; }
+  memcpy(r->data, img->data, r->data_size);
+  return r;
+}