#include "bitmap.h" #include #include #include #include #include #include int iteration_to_color( int i, int max ); int iterations_at_point( double x, double y, int max ); void compute_image( struct bitmap *bm, double xmin, double xmax, double ymin, double ymax, int max ); void show_help() { printf("Use: mandel [options]\n"); printf("Where options are:\n"); printf("-m The maximum number of iterations per point. (default=1000)\n"); printf("-x X coordinate of image center point. (default=0)\n"); printf("-y Y coordinate of image center point. (default=0)\n"); printf("-s Scale of the image in Mandlebrot coordinates. (default=4)\n"); printf("-W Width of the image in pixels. (default=500)\n"); printf("-H Height of the image in pixels. (default=500)\n"); printf("-o Set output file. (default=mandel.bmp)\n"); printf("-h Show this help text.\n"); printf("\nSome examples are:\n"); printf("mandel -x -0.5 -y -0.5 -s 0.2\n"); printf("mandel -x -.38 -y -.665 -s .05 -m 100\n"); printf("mandel -x 0.286932 -y 0.014287 -s .0005 -m 1000\n\n"); } int main( int argc, char *argv[] ) { char c; // These are the default configuration values used // if no command line arguments are given. const char *outfile = "mandel.bmp"; double xcenter = 0; double ycenter = 0; double scale = 4; int image_width = 500; int image_height = 500; int max = 1000; // For each command line argument given, // override the appropriate configuration value. while((c = getopt(argc,argv,"x:y:s:W:H:m:o:h"))!=-1) { switch(c) { case 'x': xcenter = atof(optarg); break; case 'y': ycenter = atof(optarg); break; case 's': scale = atof(optarg); break; case 'W': image_width = atoi(optarg); break; case 'H': image_height = atoi(optarg); break; case 'm': max = atoi(optarg); break; case 'o': outfile = optarg; break; case 'h': show_help(); exit(1); break; } } // Display the configuration of the image. printf("mandel: x=%lf y=%lf scale=%lf max=%d outfile=%s\n",xcenter,ycenter,scale,max,outfile); // Create a bitmap of the appropriate size. struct bitmap *bm = bitmap_create(image_width,image_height); // Fill it with a dark blue, for debugging bitmap_reset(bm,MAKE_RGBA(0,0,255,0)); // Compute the Mandelbrot image compute_image(bm,xcenter-scale,xcenter+scale,ycenter-scale,ycenter+scale,max); // Save the image in the stated file. if(!bitmap_save(bm,outfile)) { fprintf(stderr,"mandel: couldn't write to %s: %s\n",outfile,strerror(errno)); return 1; } return 0; } /* Compute an entire Mandelbrot image, writing each point to the given bitmap. Scale the image to the range (xmin-xmax,ymin-ymax), limiting iterations to "max" */ void compute_image( struct bitmap *bm, double xmin, double xmax, double ymin, double ymax, int max ) { int i,j; int width = bitmap_width(bm); int height = bitmap_height(bm); // For every pixel in the image... for(j=0;j