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