-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandel.c
283 lines (190 loc) · 5.87 KB
/
mandel.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include "skata.h"
#include "mandelcalc.h"
#define WinW 300
#define WinH 300
#define ZoomStepFactor 0.5
#define ZoomIterationFactor 2
static Display *dsp = NULL;
static unsigned long curC;
static Window win;
static GC gc;
/* basic win management rountines */
void openWin(const char *title, int width, int height) {
unsigned long blackC,whiteC;
XSizeHints sh;
XEvent evt;
long evtmsk;
whiteC = WhitePixel(dsp, DefaultScreen(dsp));
blackC = BlackPixel(dsp, DefaultScreen(dsp));
curC = blackC;
win = XCreateSimpleWindow(dsp, DefaultRootWindow(dsp), 0, 0, WinW, WinH, 0, blackC, whiteC);
sh.flags=PSize|PMinSize|PMaxSize;
sh.width=sh.min_width=sh.max_width=WinW;
sh.height=sh.min_height=sh.max_height=WinH;
XSetStandardProperties(dsp, win, title, title, None, NULL, 0, &sh);
XSelectInput(dsp, win, StructureNotifyMask|KeyPressMask);
XMapWindow(dsp, win);
do {
XWindowEvent(dsp, win, StructureNotifyMask, &evt);
} while (evt.type != MapNotify);
gc = XCreateGC(dsp, win, 0, NULL);
}
void closeWin() {
XFreeGC(dsp, gc);
XUnmapWindow(dsp, win);
XDestroyWindow(dsp, win);
}
void flushDrawOps() {
XFlush(dsp);
}
void clearWin() {
XSetForeground(dsp, gc, WhitePixel(dsp, DefaultScreen(dsp)));
XFillRectangle(dsp, win, gc, 0, 0, WinW, WinH);
flushDrawOps();
XSetForeground(dsp, gc, curC);
}
void drawPoint(int x, int y) {
XDrawPoint(dsp, win, gc, x, WinH-y);
flushDrawOps();
}
void getMouseCoords(int *x, int *y) {
XEvent evt;
XSelectInput(dsp, win, ButtonPressMask);
do {
XNextEvent(dsp, &evt);
} while (evt.type != ButtonPress);
*x=evt.xbutton.x; *y=evt.xbutton.y;
}
/* color stuff */
void setColor(char *name) {
XColor clr1,clr2;
if (!XAllocNamedColor(dsp, DefaultColormap(dsp, DefaultScreen(dsp)), name, &clr1, &clr2)) {
printf("failed\n"); return;
}
XSetForeground(dsp, gc, clr1.pixel);
curC = clr1.pixel;
}
char *pickColor(int v, int maxIterations) {
static char cname[128];
if (v == maxIterations) {
return("black");
}
else {
sprintf(cname,"rgb:%x/%x/%x",v%64,v%128,v%256);
return(cname);
}
}
int main(int argc, char *argv[]) {
mandelPars pars;
int i,k,x,y,level,*res,check;
int xoff,yoff, thrCheck;
long double reStep,imStep;
Toolbox **Tool;
pthread_t *thrP;
printf("\n");
printf("This program starts by drawing the default Mandelbrot region\n");
printf("When done, you can click with the mouse on an area of interest\n");
printf("and the program will zoom at this point\n");
printf("\n");
printf("Press enter to continue\n");
getchar();
pars.rePixels = WinW; /* never changes */
pars.imPixels = WinH; /* never changes */
/* default mandelbrot region */
pars.reBeg = (long double) -2.0;
pars.reEnd = (long double) 1.0;
pars.imBeg = (long double) -1.5;
pars.imEnd = (long double) 1.5;
reStep = (pars.reEnd - pars.reBeg) / pars.rePixels;
imStep = (pars.imEnd - pars.imBeg) / pars.imPixels;
printf("enter max iterations (50): ");
scanf("%d",&pars.maxIterations);
printf("enter no of slices: ");
scanf("%d",&pars.slices);
/* adjust slices to divide win height */
while (WinH % pars.slices != 0) { pars.slices++;}
/* allocate result and ready arrays */
res = (int *) malloc(sizeof(int)*pars.rePixels*pars.imPixels);
/* open window for drawing results */
openDisplay();
openWin(argv[0], WinW, WinH);
level = 1;
if (NULL== (Tool=(Toolbox**)malloc(sizeof(Toolbox)*pars.slices))){
perror ("Memory Error");
}
for (i=0; i<pars.slices; i++){
if (NULL== (Tool[i]=(Toolbox*)malloc(sizeof(Toolbox)*pars.slices))){
perror ("Memory Error");
}
if (NULL==(Tool[i]->mand=(mandelPars*)malloc(sizeof(mandelPars)))){
perror ("Memory error");
}
Tool[i]->mand=&pars;
if (NULL==(Tool[i]->res=(int*)malloc(sizeof(int)))){
perror ("Memory error");
}
Tool[i]->res= res;
Tool[i]->count=i;
Tool[i]->rd=-1;
if (NULL== (thrP=(pthread_t*)malloc(sizeof(pthread_t)*pars.slices))){
perror ("Memory Error");
}
thrCheck = pthread_create( &thrP[i], NULL,calcMandel , (void *)Tool[i]);
if(thrCheck)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",thrCheck);
exit(EXIT_FAILURE);
}
}
while (1) {
clearWin();
printf("computing for level %d:\n",level);
printf("reBeg=%Lf reEnd=%Lf reStep=%Lf\n",pars.reBeg,pars.reEnd,reStep);
printf("imBeg=%Lf imEnd=%Lf imStep=%Lf\n",pars.imBeg,pars.imEnd,imStep);
printf("maxIterations=%d\n",pars.maxIterations);
for (i=0; i<pars.slices; i++) {
Tool[i]->level=&level;
Tool[i]->rd=0;
}
/* busywait (and draw results) until all slices done */
k=0;
while (k!=pars.slices) {
for (i=0; i<pars.slices; i++) {
while (Tool[i]->rd!=1){
if (0!=(check= sched_yield())){
perror ("Thread");
}
}
for (y=i*(pars.imPixels/pars.slices); y<(i+1)*(pars.imPixels/pars.slices); y++) {
for (x=0; x<pars.rePixels; x++) {
setColor(pickColor(res[y*pars.rePixels+x],pars.maxIterations));
drawPoint(x,y);
}
}
k++; Tool[i]->rd=2;
}
}
/* get next focus/zoom point */
getMouseCoords(&x,&y);
xoff = WinW/2 - x;
yoff = WinH/2 - (WinH-y);
/* adjust focus */
pars.reBeg = pars.reBeg - xoff*reStep;
pars.reEnd = pars.reBeg + reStep*pars.rePixels;
pars.imBeg = pars.imBeg - yoff*reStep;
pars.imEnd = pars.imBeg + reStep*pars.imPixels;
/* zoom in */
reStep = reStep*ZoomStepFactor;
imStep = imStep*ZoomStepFactor;
pars.reBeg = pars.reBeg + (pars.reEnd-pars.reBeg)/2 - reStep*pars.rePixels/2;
pars.reEnd = pars.reBeg+reStep*pars.rePixels;
pars.imBeg = pars.imBeg + (pars.imEnd-pars.imBeg)/2 - imStep*pars.imPixels/2;
pars.imEnd = pars.imBeg+imStep*pars.imPixels;
pars.maxIterations = pars.maxIterations*ZoomIterationFactor;
level++;
}
/* never reach this point; for cosmetic reasons */
free(res);
closeWin();
closeDisplay();
}