render_file.c
changeset 11 082bfaf38cf0
parent 8 4d38ccbeb93e
--- a/render_file.c	Fri Jun 06 16:05:26 2008 +0300
+++ b/render_file.c	Fri Jun 06 18:35:46 2008 +0300
@@ -6,27 +6,110 @@
 #include <sys/socket.h>
 #include <arpa/inet.h>
 
+#include "common.h"
 #include "render.h"
-#include "mandelbrot.h"
-#include "common.h"
+#include "render_local.h"
 
 static int verbose;
 
-void render_local (int img_w, int img_h, FILE *output) {
-    render_t ctx;
-
-    render_init(&ctx, RENDER_PNG);
-    render_set_size(&ctx, img_w, img_h);
-    render_region_full(&ctx);
-    render_io_stream(&ctx, output);
-
+int render_file_local (struct render *ctx, FILE *fh) {
     double duration;
 
-    if (mandelbrot_render_timed(&ctx, &duration) != MANDELBROT_OK)
-        err_exit("mandelbrot_render_region failed");
+    if (render_io_stream(ctx, fh))
+        ERROR("render_io_stream");
+
+    if (render_local(ctx, &duration))
+        ERROR("render_local");
+
+    if (verbose) {
+        u_int32_t img_w, img_h;
+
+        if (render_get_size(ctx, &img_w, &img_h))
+            ERROR("render_get_size");
+
+        fprintf(stdout, "rendered %dx%d mandelbrot in %f seconds\n", img_w, img_h, duration);
+    }
     
-    if (verbose) 
-        fprintf(stdout, "rendered %dx%d mandelbrot in %f seconds\n", img_w, img_h, duration);
+    return 0;
+    
+error:
+    return -1;
+}
+
+int main (int argc, char **argv) {
+    int error = 1;
+
+    struct render *ctx = NULL;
+
+    // parse arguments
+    int opt;
+    FILE *output = NULL;
+    int img_w = 256, img_h = 256;
+
+    while ((opt = getopt(argc, argv, "w:h:o:v")) != -1) {
+        switch(opt) {
+            case 'w' :
+                img_w = atoi(optarg);
+                break;
+
+            case 'h' :
+                img_h = atoi(optarg);
+                break;
+
+            case 'o' :
+                if (output)
+                    err_exit("Only use -o once");
+
+                output = fopen(optarg, "w");
+
+                if (!output)
+                    die(optarg);
+
+                break;
+
+            case 'v' :
+                verbose = 1;
+                break;
+
+
+            default :
+                err_exit("Usage: %s [-w img_w] [-h img_h] [-o output_file] [-v]", argv[0]);
+        }
+    }
+
+    if (!output)
+        output = stdout;
+    
+    // setup the struct render
+    if (!(ctx = render_alloc()))
+        ERROR("render_alloc");
+
+    if (render_set_mode(ctx, RENDER_PNG))
+        ERROR("render_set_mode");
+
+    if (render_set_size(ctx, img_w, img_h))
+        ERROR("render_set_size");
+
+    if (render_region_full(ctx))
+        ERROR("render_region_full");
+    
+    // do the render!
+    if (verbose)
+        fprintf(stderr, "Render [%dx%d] mandelbrot locally...\n", img_w, img_h);
+    
+    if (render_file_local(ctx, output))
+        ERROR("render_local");
+    
+    // success
+    error = 0;
+
+error:
+    if (output)
+        fclose(output);
+
+    free(ctx);
+
+    return error;
 }
 
 #if 0
@@ -105,74 +188,4 @@
 }
 #endif
 
-int main (int argc, char **argv) {
-    int opt;
-    
-    FILE *output = NULL /*, *remote = NULL */ ;
-    int img_w = 256, img_h = 256;
-    
-
-    while ((opt = getopt(argc, argv, "w:h:o:vr:")) != -1) {
-        switch(opt) {
-            case 'w' :
-                img_w = atoi(optarg);
-                break;
-
-            case 'h' :
-                img_h = atoi(optarg);
-                break;
-
-            case 'o' :
-                if (output)
-                    err_exit("Only use -o once");
-
-                output = fopen(optarg, "w");
-
-                if (!output)
-                    die(optarg);
-
-                break;
-
-            case 'v' :
-                verbose = 1;
-                break;
 
-#if 0
-            case 'r' :
-                if (remote)
-                    err_exit("Only use -r once");
-
-                remote = open_remote(optarg);
-
-                break;
-#endif
-
-            default :
-                err_exit("Usage: %s [-w img_w] [-h img_h] [-o output_file] [-v] [-r host[:port]]", argv[0]);
-        }
-    }
-
-    if (!output)
-        output = stdout;
-
-#if 0
-    if (remote) {
-        if (verbose)
-            fprintf(stderr, "Render [%dx%d] mandelbrot remotely\n", img_w, img_h);
-
-        render_remote(img_w, img_h, output, remote);
-
-        fclose(remote);
-    } else
-#endif    
-    {
-        if (verbose)
-            fprintf(stderr, "Render [%dx%d] mandelbrot locally\n", img_w, img_h);
-
-        render_local(img_w, img_h, output);
-    }
-
-    fclose(output);
-
-    return 0;
-}