- prevent a cast to integer warning on x64 builds in datatypes.c
[imager.git] / dynfilt / mandelbrot.c
CommitLineData
02d1d628
AMH
1#include "pluginst.h"
2
3
4char 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
15static
16int
17mandel(float x, float y) {
18 float xn, yn;
19 float xo, yo;
02d1d628
AMH
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
40void mandlebrot(void *INP) {
41
42 i_img *im;
02d1d628
AMH
43 int i,bytes,x,y;
44 int idx;
45
46 float xs, ys;
47 float div;
48
49 i_color icl[256];
50 srand(12235);
51 for(i=1;i<256; i++) {
52 icl[i].rgb.r = 100+(int) (155.0*rand()/(RAND_MAX+1.0));
53 icl[i].rgb.g = 100+(int) (155.0*rand()/(RAND_MAX+1.0));
54 icl[i].rgb.g = 100+(int) (155.0*rand()/(RAND_MAX+1.0));
55 }
56
57 icl[0].rgb.r = 0;
58 icl[0].rgb.g = 0;
59 icl[0].rgb.g = 0;
60
61
62
63 if ( !getOBJ("image","Imager::ImgRaw",&im) ) { fprintf(stderr,"Error: image is missing\n"); }
64
919e0000 65 fprintf(stderr,"mandlebrot: parameters: (im %p)\n",im);
02d1d628
AMH
66 bytes=im->bytes;
67
8a2cd317
TC
68 fprintf(stderr, "mandlebrot: image info:\n size (%d,%d)\n channels (%d)\n",
69 im->xsize,im->ysize,im->channels);
02d1d628
AMH
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
90func_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