also link statically
[libjh.git] / ppm.c
diff --git a/ppm.c b/ppm.c
index cdaebf5..a8bf94a 100644 (file)
--- a/ppm.c
+++ b/ppm.c
@@ -2,7 +2,6 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <assert.h>
 #include <string.h>
 #include <errno.h>
 
@@ -12,7 +11,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 +67,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;
+}