mandelbrot.h
changeset 0 5b010627d7ed
child 1 6aa1a0d1f88d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mandelbrot.h	Fri May 30 14:24:23 2008 +0300
@@ -0,0 +1,117 @@
+/*
+ * code to render a mandelbrot
+ */
+
+#include <sys/types.h>
+
+typedef int (*render_ctx_write_cb)(const unsigned char *data, size_t length, void *arg);
+typedef int (*render_ctx_flush_cb)(void *arg);
+
+#define RENDER_CB_OK 0
+#define RENDER_CB_ERR 1
+
+struct render_ctx {
+    /*
+     * If we just use normal FILE* stream operations
+     */
+    FILE *stream;
+
+    /*
+     * These callback functions are used to handle the PNG data as it's rendered.
+     *
+     * If they return RENDER_CB_OK, all is fine and rendering will continue normally.
+     * If they return RENDER_CB_ERR, rendering will abort.
+     */
+
+    // called to handle the output data
+    render_ctx_write_cb write_fn;
+
+    // called when the output data should be flushed - can be safely ignored if not needed
+    render_ctx_flush_cb flush_fn;
+
+    // the callback argument
+    void *cb_arg;
+
+    // error status
+    int _error;
+};
+
+/*
+ * initialize the given struct render_ctx
+ */
+void render_ctx_set (struct render_ctx *ctx, render_ctx_write_cb write_fn, render_ctx_flush_cb flush_fn, void *arg);
+void render_ctx_stream (struct render_ctx *ctx, FILE *fh);
+
+/*
+ * return codes for mandelbrot_render
+ */
+#define MANDELBROT_OK 0
+#define MANDELBROT_ERR_MALLOC 1
+#define MANDELBROT_ERR_PNG_INIT 2
+#define MANDELBROT_ERR_CB 3
+#define MANDELBROT_ERR_INVALID 4
+
+/*
+ * render an <img_w>x<img_h> PNG image of the mandelbrot region bounded by (x1, y1) -> (x2, y2)
+ */
+int mandelbrot_render_region (
+        struct render_ctx *ctx, 
+        u_int32_t img_w, u_int32_t img_h, 
+        double x1, double y1, double x2, double y2
+);
+
+int mandelbrot_render_region_timed (
+        struct render_ctx *ctx, 
+        u_int32_t img_w, u_int32_t img_h, 
+        double x1, double y1, double x2, double y2,
+        double *duration
+);
+
+/*
+ * remote rendering
+ */
+
+#pragma pack(push)
+#pragma pack(1)
+
+struct render_cmd {
+    u_int32_t   img_w;
+    u_int32_t   img_h;
+
+    double      x1;
+    double      y1;
+    double      x2;
+    double      y2;
+};
+
+#pragma pack(pop)
+
+#define RENDER_PORT 6159
+#define RENDER_CMD_SIZE sizeof(struct render_cmd)
+
+/*
+ * build the command to send for remote rendering
+ */
+int render_cmd_set (
+        struct render_cmd *cmd,
+        u_int32_t img_w, u_int32_t img_h, 
+        double x1, double y1, double x2, double y2
+);
+
+/* 
+ * The "full" mandelbrot region
+ */
+#define REGION_X1 -2.0
+#define REGION_Y1 -1.5
+#define REGION_X2 1.0
+#define REGION_Y2 1.5
+
+#define mandelbrot_render_full(ctx, img_w, img_h) \
+    mandelbrot_render_region(ctx, img_w, img_h, REGION_X1, REGION_Y1, REGION_X2, REGION_Y2)
+
+#define mandelbrot_render_full_timed(ctx, img_w, img_h, duration) \
+    mandelbrot_render_region_timed(ctx, img_w, img_h, REGION_X1, REGION_Y1, REGION_X2, REGION_Y2, duration)
+
+#define render_cmd_set_full(cmd, img_w, img_h) \
+    render_cmd_set(cmd, img_w, img_h, REGION_X1, REGION_Y1, REGION_X2, REGION_Y2)
+