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
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;
+}