/* mandel01jpeg.c --- Jean-Michel Sarlat --- 8 juin 2003                       */

#include <unistd.h>
#include <gd.h>
#include <stdlib.h>
#include <math.h>

typedef int RGB[3];

int MI,LA;

double PR, PI, X1, Y1, X2, Y2, XC, YC, RC, VP;

/* --- polynôme cubique pour « parcourir » l'espace RVB --------------------- */
float polycubique (int n, float x) {
    int i;
    for (i = 0; i < n ; i++) {
	x = 16 * x * (x - 0.75) * (x - 0.75);
    }
    return x;
}



int Mandelbrot (double x, double y) {
    int iter = 0; 
    double t;
    PR = x; PI = y;
    while ( x * x + y * y < 16  && iter < MI ) {
        t = x * x - y * y + PR;
        y = 2 * x * y + PI;
        x = t;
        iter++;
    }
    return iter * 255 / MI;
}

int main (int argc, char *argv[]) {
    gdImagePtr image;
    int i,j,rouge,vert,bleu,R,V,B;
    float t;
    
    XC = atof(argv[1]);
    YC = atof(argv[2]);
    RC = atof(argv[3]);
    
    sscanf(argv[4],"%d",&LA);
    sscanf(argv[5],"%d",&MI);

    sscanf(argv[6],"%d",&R);
    sscanf(argv[7],"%d",&V);
    sscanf(argv[8],"%d",&B);
    
    X1 = XC - RC; Y1 = YC - RC; X2 = XC + RC; Y2 = YC + RC;
    VP = 2 * RC / LA;						
        
    image = gdImageCreate(LA,LA);

    for (i = 0; i <= 255 ; i++ ) {
	t = (double) i / 255;
	rouge  = (int) 255 * polycubique(R,t);
	vert   = (int) 255 * polycubique(V,t);
	bleu   = (int) 255 * polycubique(B,t);
	j = gdImageColorAllocate(image,rouge,vert,bleu);
    } 
    
    for(i = 0 ; i < LA ; i++) {
	for(j = 0 ; j < LA ; j++) {
    	    gdImageSetPixel(image,i,j, 255 - Mandelbrot(X1 + i * VP, Y2 - j * VP));
	}
    }
    
    gdImageJpeg(image,stdout,95);
    gdImageDestroy(image);
    exit(0);
}


syntax highlighted by Code2HTML, v. 0.9.1