]> git.imager.perl.org - imager.git/blob - dynfilt/mandelbrot.c
hopefully avoid coverity complaining about a float vs int comparison
[imager.git] / dynfilt / mandelbrot.c
1 #include "pluginst.h"
2 #include <stdlib.h>
3
4 char evalstr[]="Mandlebrot renderer";
5
6 /* Example Mandlebrot generator */
7
8 /* input parameters
9    image is the image object.
10 */
11
12
13 #define MXITER 256
14
15 static
16 int
17 mandel(double x, double y) {
18   double xn, yn;
19   double xo, yo;
20   int iter = 1;
21   /*    Z(n+1) = Z(n) ^2 + c */
22
23   /* printf("(%.2f, %.2f) -> \n", x,y);   */
24
25   xo = x;
26   yo = y;
27
28   while( xo*xo+yo*yo <= 10 && iter < MXITER) {
29     xn = xo*xo-yo*yo + x;
30     yn = 2*xo*yo     + y;
31     xo=xn;
32     yo=yn;
33     iter++;
34   }
35   return (iter == MXITER)?0:iter;
36 }
37
38
39
40 void mandlebrot(void *INP) {
41
42   i_img *im;
43   int i;
44   i_img_dim x,y;
45   int idx;
46   
47   double xs, ys;
48   double div;
49
50   i_color icl[256];
51   srand(12235);
52   for(i=1;i<256; i++) {
53     icl[i].rgb.r = 100+(int) (156.0*rand()/(RAND_MAX+1.0));
54     icl[i].rgb.g = 100+(int) (156.0*rand()/(RAND_MAX+1.0));
55     icl[i].rgb.b = 100+(int) (156.0*rand()/(RAND_MAX+1.0));
56   }
57
58   icl[0].rgb.r = 0;
59   icl[0].rgb.g = 0;
60   icl[0].rgb.b = 0;
61     
62
63   
64   if ( !getOBJ("image","Imager::ImgRaw",&im) ) { fprintf(stderr,"Error: image is missing\n"); }
65   
66   fprintf(stderr,"mandlebrot: parameters: (im %p)\n",im);
67
68   fprintf(stderr, "mandlebrot: image info:\n size (" i_DFp ")\n channels (%d)\n",
69           i_DFcp(im->xsize,im->ysize),im->channels); 
70   div = 2.5;
71
72   xs = 0.8*div;
73   ys = 0.5*div;
74   
75   div /= im->xsize;
76
77
78   fprintf(stderr, "Divider: %f \n", div);
79   for(y = 0; y < im->ysize; y ++) {
80     for(x = 0; x < im->xsize; x ++ ) {
81       idx = mandel(x*div-xs , y*div-ys);
82       idx = (idx>255)?255:idx;
83       i_ppix(im,x,y,&icl[idx]); 
84     }
85   }
86 }
87
88
89
90 func_ptr function_list[]={
91   {
92     "mandlebrot",
93     mandlebrot,
94     "callseq => ['image'], \
95     callsub => sub { my %hsh=@_; DSO_call($DSO_handle,0,\\%hsh); } \
96     "
97   },
98   {NULL,NULL,NULL}};
99
100
101 /* Remember to double backslash backslashes within Double quotes in C */
102