render_file.c
changeset 11 082bfaf38cf0
parent 8 4d38ccbeb93e
equal deleted inserted replaced
10:9daa832ab9c4 11:082bfaf38cf0
     4 #include <string.h>
     4 #include <string.h>
     5 #include <sys/types.h>
     5 #include <sys/types.h>
     6 #include <sys/socket.h>
     6 #include <sys/socket.h>
     7 #include <arpa/inet.h>
     7 #include <arpa/inet.h>
     8 
     8 
       
     9 #include "common.h"
     9 #include "render.h"
    10 #include "render.h"
    10 #include "mandelbrot.h"
    11 #include "render_local.h"
    11 #include "common.h"
       
    12 
    12 
    13 static int verbose;
    13 static int verbose;
    14 
    14 
    15 void render_local (int img_w, int img_h, FILE *output) {
    15 int render_file_local (struct render *ctx, FILE *fh) {
    16     render_t ctx;
       
    17 
       
    18     render_init(&ctx, RENDER_PNG);
       
    19     render_set_size(&ctx, img_w, img_h);
       
    20     render_region_full(&ctx);
       
    21     render_io_stream(&ctx, output);
       
    22 
       
    23     double duration;
    16     double duration;
    24 
    17 
    25     if (mandelbrot_render_timed(&ctx, &duration) != MANDELBROT_OK)
    18     if (render_io_stream(ctx, fh))
    26         err_exit("mandelbrot_render_region failed");
    19         ERROR("render_io_stream");
       
    20 
       
    21     if (render_local(ctx, &duration))
       
    22         ERROR("render_local");
       
    23 
       
    24     if (verbose) {
       
    25         u_int32_t img_w, img_h;
       
    26 
       
    27         if (render_get_size(ctx, &img_w, &img_h))
       
    28             ERROR("render_get_size");
       
    29 
       
    30         fprintf(stdout, "rendered %dx%d mandelbrot in %f seconds\n", img_w, img_h, duration);
       
    31     }
    27     
    32     
    28     if (verbose) 
    33     return 0;
    29         fprintf(stdout, "rendered %dx%d mandelbrot in %f seconds\n", img_w, img_h, duration);
    34     
       
    35 error:
       
    36     return -1;
       
    37 }
       
    38 
       
    39 int main (int argc, char **argv) {
       
    40     int error = 1;
       
    41 
       
    42     struct render *ctx = NULL;
       
    43 
       
    44     // parse arguments
       
    45     int opt;
       
    46     FILE *output = NULL;
       
    47     int img_w = 256, img_h = 256;
       
    48 
       
    49     while ((opt = getopt(argc, argv, "w:h:o:v")) != -1) {
       
    50         switch(opt) {
       
    51             case 'w' :
       
    52                 img_w = atoi(optarg);
       
    53                 break;
       
    54 
       
    55             case 'h' :
       
    56                 img_h = atoi(optarg);
       
    57                 break;
       
    58 
       
    59             case 'o' :
       
    60                 if (output)
       
    61                     err_exit("Only use -o once");
       
    62 
       
    63                 output = fopen(optarg, "w");
       
    64 
       
    65                 if (!output)
       
    66                     die(optarg);
       
    67 
       
    68                 break;
       
    69 
       
    70             case 'v' :
       
    71                 verbose = 1;
       
    72                 break;
       
    73 
       
    74 
       
    75             default :
       
    76                 err_exit("Usage: %s [-w img_w] [-h img_h] [-o output_file] [-v]", argv[0]);
       
    77         }
       
    78     }
       
    79 
       
    80     if (!output)
       
    81         output = stdout;
       
    82     
       
    83     // setup the struct render
       
    84     if (!(ctx = render_alloc()))
       
    85         ERROR("render_alloc");
       
    86 
       
    87     if (render_set_mode(ctx, RENDER_PNG))
       
    88         ERROR("render_set_mode");
       
    89 
       
    90     if (render_set_size(ctx, img_w, img_h))
       
    91         ERROR("render_set_size");
       
    92 
       
    93     if (render_region_full(ctx))
       
    94         ERROR("render_region_full");
       
    95     
       
    96     // do the render!
       
    97     if (verbose)
       
    98         fprintf(stderr, "Render [%dx%d] mandelbrot locally...\n", img_w, img_h);
       
    99     
       
   100     if (render_file_local(ctx, output))
       
   101         ERROR("render_local");
       
   102     
       
   103     // success
       
   104     error = 0;
       
   105 
       
   106 error:
       
   107     if (output)
       
   108         fclose(output);
       
   109 
       
   110     free(ctx);
       
   111 
       
   112     return error;
    30 }
   113 }
    31 
   114 
    32 #if 0
   115 #if 0
    33 void render_remote (int img_w, int img_h, FILE *output, FILE *remote) {
   116 void render_remote (int img_w, int img_h, FILE *output, FILE *remote) {
    34     render_t ctx;
   117     render_t ctx;
   103 
   186 
   104     return fh;
   187     return fh;
   105 }
   188 }
   106 #endif
   189 #endif
   107 
   190 
   108 int main (int argc, char **argv) {
       
   109     int opt;
       
   110     
       
   111     FILE *output = NULL /*, *remote = NULL */ ;
       
   112     int img_w = 256, img_h = 256;
       
   113     
       
   114 
   191 
   115     while ((opt = getopt(argc, argv, "w:h:o:vr:")) != -1) {
       
   116         switch(opt) {
       
   117             case 'w' :
       
   118                 img_w = atoi(optarg);
       
   119                 break;
       
   120 
       
   121             case 'h' :
       
   122                 img_h = atoi(optarg);
       
   123                 break;
       
   124 
       
   125             case 'o' :
       
   126                 if (output)
       
   127                     err_exit("Only use -o once");
       
   128 
       
   129                 output = fopen(optarg, "w");
       
   130 
       
   131                 if (!output)
       
   132                     die(optarg);
       
   133 
       
   134                 break;
       
   135 
       
   136             case 'v' :
       
   137                 verbose = 1;
       
   138                 break;
       
   139 
       
   140 #if 0
       
   141             case 'r' :
       
   142                 if (remote)
       
   143                     err_exit("Only use -r once");
       
   144 
       
   145                 remote = open_remote(optarg);
       
   146 
       
   147                 break;
       
   148 #endif
       
   149 
       
   150             default :
       
   151                 err_exit("Usage: %s [-w img_w] [-h img_h] [-o output_file] [-v] [-r host[:port]]", argv[0]);
       
   152         }
       
   153     }
       
   154 
       
   155     if (!output)
       
   156         output = stdout;
       
   157 
       
   158 #if 0
       
   159     if (remote) {
       
   160         if (verbose)
       
   161             fprintf(stderr, "Render [%dx%d] mandelbrot remotely\n", img_w, img_h);
       
   162 
       
   163         render_remote(img_w, img_h, output, remote);
       
   164 
       
   165         fclose(remote);
       
   166     } else
       
   167 #endif    
       
   168     {
       
   169         if (verbose)
       
   170             fprintf(stderr, "Render [%dx%d] mandelbrot locally\n", img_w, img_h);
       
   171 
       
   172         render_local(img_w, img_h, output);
       
   173     }
       
   174 
       
   175     fclose(output);
       
   176 
       
   177     return 0;
       
   178 }