render_png.c
changeset 13 ee426f453cf5
parent 11 082bfaf38cf0
child 15 e7f0697814dc
--- a/render_png.c	Fri Jun 06 23:37:45 2008 +0300
+++ b/render_png.c	Sat Jun 07 05:05:18 2008 +0300
@@ -11,31 +11,33 @@
     png_structp png_ptr;
     png_infop info_ptr;
 
-    // our render op, containing the I/O callbacks
-    struct render *render;
+    // some info that we need to keep from the struct render
+    render_ctx_write_cb io_write_fn;
+    render_ctx_flush_cb io_flush_fn;
+    void *cb_arg;
 };
 
-void _render_png_write(png_structp png_ptr, png_bytep data, png_size_t length) {
+static void _render_png_write(png_structp png_ptr, png_bytep data, png_size_t length) {
     struct render_png *ctx = png_get_io_ptr(png_ptr);
 
-    if (ctx->render->io_write_fn)
-        if (ctx->render->io_write_fn(data, length, ctx->render->cb_arg)) {
+    if (ctx->io_write_fn)
+        if (ctx->io_write_fn(data, length, ctx->cb_arg)) {
             // error, doesn't return
             png_error(png_ptr, "_render_png_write: io_write_fn");
         }
 }
 
-void _render_png_flush(png_structp png_ptr) {
+static void _render_png_flush(png_structp png_ptr) {
     struct render_png *ctx = png_get_io_ptr(png_ptr);
     
-    if (ctx->render->io_flush_fn)
-        if (ctx->render->io_flush_fn(ctx->render->cb_arg)) {
+    if (ctx->io_flush_fn)
+        if (ctx->io_flush_fn(ctx->cb_arg)) {
             // error, doesn't return
             png_error(png_ptr, "_render_png_flush: io_flush_fn");
         }
 }
 
-void _render_png_free (struct render_png *ctx) {
+static void _render_png_free (struct render_png *ctx) {
     if (ctx)
         png_destroy_write_struct(&ctx->png_ptr, &ctx->info_ptr);
 
@@ -51,8 +53,10 @@
     if (!(ctx = calloc(1, sizeof(struct render_png))))
         ERROR("calloc");
 
-    // store the struct render
-    ctx->render = render;
+    // store some info from the struct render
+    ctx->io_write_fn = render->io_write_fn;
+    ctx->io_flush_fn = render->io_flush_fn;
+    ctx->cb_arg = render->cb_arg;
 
     // libpng initialization
     if (!(ctx->png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)))
@@ -111,6 +115,9 @@
 
     // write end
     png_write_end(ctx->png_ptr, ctx->info_ptr);
+    
+    // free everything
+    _render_png_free(ctx);
 
     // success
     return 0;
@@ -124,7 +131,8 @@
     // libpng error handling
     if (setjmp(png_jmpbuf(ctx->png_ptr)))
         ERROR("libpng");
-
+    
+    // just free it
     _render_png_free(ctx);
 
     // success