eliminate use vars
[imager.git] / dynfilt / mandelbrot.c
CommitLineData
02d1d628 1#include "pluginst.h"
4d0f50fa 2#include <stdlib.h>
02d1d628
AMH
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
8d14daab
TC
17mandel(double x, double y) {
18 double xn, yn;
19 double 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;
8d14daab
TC
43 int i;
44 i_img_dim x,y;
02d1d628
AMH
45 int idx;
46
8d14daab
TC
47 double xs, ys;
48 double div;
02d1d628
AMH
49
50 i_color icl[256];
51 srand(12235);
52 for(i=1;i<256; i++) {
ce2d9ae2
TC
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));
02d1d628
AMH
56 }
57
58 icl[0].rgb.r = 0;
59 icl[0].rgb.g = 0;
ce2d9ae2 60 icl[0].rgb.b = 0;
02d1d628
AMH
61
62
63
64 if ( !getOBJ("image","Imager::ImgRaw",&im) ) { fprintf(stderr,"Error: image is missing\n"); }
65
919e0000 66 fprintf(stderr,"mandlebrot: parameters: (im %p)\n",im);
02d1d628 67
8d14daab
TC
68 fprintf(stderr, "mandlebrot: image info:\n size (" i_DFp ")\n channels (%d)\n",
69 i_DFcp(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