render_raw.c
changeset 13 ee426f453cf5
child 17 8e8b56b0e0f5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/render_raw.c	Sat Jun 07 05:05:18 2008 +0300
@@ -0,0 +1,98 @@
+#include <stdlib.h>
+
+#include "common.h"
+#include "render_internal.h"
+#include "render_raw.h"
+
+struct render_raw {
+    // some info that we need to keep from the struct render
+    u_int32_t img_w;
+
+    // cb or stream?
+    FILE *io_stream;
+    render_ctx_write_cb io_write_fn;
+    render_ctx_flush_cb io_flush_fn;
+    void *cb_arg;
+};
+
+static void _render_raw_free (struct render_raw *ctx) {
+    free(ctx);
+}
+
+struct render_raw *render_raw_init (struct render *render) {
+    struct render_raw *ctx = NULL;
+
+    // calloc the render_png
+    if (!(ctx = calloc(1, sizeof(struct render_raw))))
+        ERROR("calloc");
+
+    // store some info from the struct render
+    ctx->img_w = render->img_w;
+    ctx->io_stream = render->io_stream;
+    ctx->io_write_fn = render->io_write_fn;
+    ctx->io_flush_fn = render->io_flush_fn;
+    ctx->cb_arg = render->cb_arg;
+
+    // success
+    return ctx;
+
+error:
+    _render_raw_free(ctx);
+    return NULL;
+}
+
+int render_raw_row (struct render_raw *ctx, unsigned char *rowbuf) {
+    // write it out
+    if (ctx->io_stream) {
+        if (fwrite(rowbuf, ctx->img_w, 1, ctx->io_stream) != 1)
+            PERROR("fwrite");
+
+    } else if (ctx->io_write_fn) {
+        if (ctx->io_write_fn(rowbuf, ctx->img_w, ctx->cb_arg))
+            ERROR("io_write_fn");
+
+    } else {
+        // ignore
+    }
+
+    // success
+    return 0;
+
+error:
+    // don't free it yet, raw_{done,abort} does that!
+    return -1;
+}
+
+int render_raw_done (struct render_raw *ctx) {
+    // flush
+    if (ctx->io_stream) {
+        if (fflush(ctx->io_stream))
+            PERROR("fflush");
+
+    } else if (ctx->io_flush_fn) {
+        if (ctx->io_flush_fn(ctx->cb_arg))
+            ERROR("io_flush_fn");
+
+    } else {
+        // ignore
+    }
+
+    // free everything
+    _render_raw_free(ctx);
+
+    // success
+    return 0;
+
+error:
+    _render_raw_free(ctx);
+    return -1;
+}
+
+int render_raw_abort (struct render_raw *ctx) {
+    // just free it
+    _render_raw_free(ctx);
+
+    // success
+    return 0;
+}
+