From http://www.jwz.org/xscreensaver/xscreensaver-5.22.tar.gz
[xscreensaver] / hacks / strange.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* strange --- strange attractors */
3
4 #if 0
5 static const char sccsid[] = "@(#)strange.c     5.00 2000/11/01 xlockmore";
6 #endif
7
8 /*-
9 * Copyright (c) 1997 by Massimino Pascal <Pascal.Massimon@ens.fr>
10 *
11 * Permission to use, copy, modify, and distribute this software and its
12 * documentation for any purpose and without fee is hereby granted,
13 * provided that the above copyright notice appear in all copies and that
14 * both that copyright notice and this permission notice appear in
15 * supporting documentation.
16 *
17 * This file is provided AS IS with no warranties of any kind.  The author
18 * shall have no liability with respect to the infringement of copyrights,
19 * trade secrets or any patents by this file or any part thereof.  In no
20 * event will the author be liable for any lost revenue or profits or
21 * other special, indirect and consequential damages.
22 *
23 * Revision History:
24 * 01-Nov-2000: Allocation checks
25 * 10-May-1997: jwz@jwz.org: turned into a standalone program.
26 *              Made it render into an offscreen bitmap and then copy
27 *              that onto the screen, to reduce flicker.
28 *
29 * strange attractors are not so hard to find...
30 */
31
32 /* TODO: Bring over tweaks from 3.x version.
33 * For a good idea of what's missing, diff strange.c.20081107-good2 against strange.c-3.29
34 * We forked from the 3.29 series at 20081107, so anything added since then may be missing.
35 */
36
37 #ifdef STANDALONE
38 # define MODE_strange
39 # define DEFAULTS       "*delay: 10000 \n" \
40                                         "*ncolors: 100 \n" \
41                                         "*fpsSolid: True \n" \
42                                         "*ignoreRotation: True \n" \
43
44 # define SMOOTH_COLORS
45 # define refresh_strange 0
46 # define strange_handle_event 0
47 # include "xlockmore.h"         /* from the xscreensaver distribution */
48 #else /* !STANDALONE */
49 # include "xlock.h"             /* from the xlockmore distribution */
50 #endif /* !STANDALONE */
51
52 #ifdef MODE_strange
53 #define DEF_CURVE  "10"
54 #define DEF_POINTS "5500"
55
56 /*static int curve;*/
57 static int points;
58
59 static XrmOptionDescRec opts[] =
60 {
61 /*              {"-curve",  ".strange.curve",  XrmoptionSepArg, 0}, */
62                 {"-points", ".strange.points", XrmoptionSepArg, 0},
63 };
64 static argtype vars[] =
65 {
66 /*              {&curve,  "curve",  "Curve",  DEF_CURVE,  t_Int},*/
67                 {&points, "points", "Points", DEF_POINTS, t_Int},
68 };
69 static OptionStruct desc[] =
70 {
71 /*              {"-curve", "set the curve factor of the attractors"},*/
72                 {"-points", "change the number of points/iterations each frame"},
73 };
74 ENTRYPOINT ModeSpecOpt strange_opts =
75 {sizeof opts / sizeof opts[0], opts,
76 sizeof vars / sizeof vars[0], vars, desc};
77
78 #ifdef USE_MODULES
79 ModStruct   strange_description =
80 {"strange", "init_strange", "draw_strange", "release_strange",
81 "init_strange", "init_strange", (char *) NULL, &strange_opts,
82 1000, 1, 1, 1, 64, 1.0, "",
83 "Shows strange attractors", 0, NULL};
84 #endif
85
86 #ifdef HAVE_COCOA
87 # define NO_DBUF
88 #endif
89
90 typedef float DBL;
91 typedef int PRM;
92
93 #define UNIT (1<<12)
94 #define UNIT2 (1<<14)
95 /* #define UNIT2 (3140*UNIT/1000) */
96
97 #define SKIP_FIRST      100
98 #define DBL_To_PRM(x)  (PRM)( (DBL)(UNIT)*(x) )
99
100
101 #define DO_FOLD(a) (a)<0 ? -A->Fold[ (-(a))&(UNIT2-1) ] : A->Fold[ (a)&(UNIT2-1) ]
102
103 #if 0
104 #define DO_FOLD(a) (a)<-UNIT2 ? -A->Fold[(-(a))%UNIT2] : (a)<0 ? -A->Fold[ -(a) ] :\
105 (a)>UNIT2 ? A->Fold[ (a)%UNIT2 ] : A->Fold[ (a) ]
106 #define DO_FOLD(a) DBL_To_PRM( sin( (DBL)(a)/UNIT ) )
107 #define DO_FOLD(a) (a)<0 ? DBL_To_PRM( exp( 16.0*(a)/UNIT2 ) )-1.0 : \
108 DBL_To_PRM( 1.0-exp( -16.0*(a)/UNIT2 ) )
109 #endif
110
111 /* useAccumulator performs two functions:
112 * If it is defined, then support for the accumulator will be compiled.
113 * It is also the condition for which the accumulator renderer will engage.
114 */
115 #define useAccumulator (Root->Max_Pt > 6000)
116 #define ACC_GAMMA 10.0
117 #define NUM_COLS 150
118 /* Extra options: */
119 #define VARY_SPEED_TO_AVOID_BOREDOM
120 #define POINTS_HISTORY
121 #define MERGE_FRAMES 3
122
123 /******************************************************************/
124
125 #define MAX_PRM 3*5
126
127 typedef struct _ATTRACTOR {
128         DBL         Prm1[MAX_PRM], Prm2[MAX_PRM];
129         PRM         Prm[MAX_PRM], *Fold;
130         void        (*Iterate) (struct _ATTRACTOR *, PRM, PRM, PRM *, PRM *);
131         XPoint     *Buffer1, *Buffer2;
132         int         Cur_Pt, Max_Pt;
133         int         Col, Count, Speed;
134         int         Width, Height;
135         Pixmap      dbuf;       /* jwz */
136         GC          dbuf_gc;
137         #ifdef useAccumulator
138                 int **accMap;
139         #endif
140 } ATTRACTOR;
141
142 static ATTRACTOR *Root = (ATTRACTOR *) NULL; 
143
144 #ifdef useAccumulator
145 static XColor* cols;
146 #endif
147
148 #ifdef POINTS_HISTORY
149 static int numOldPoints;
150 static int* oldPointsX;
151 static int* oldPointsY;
152 static int oldPointsIndex;
153 static int startedClearing;
154 #endif
155
156 static DBL  Amp_Prm[MAX_PRM] =
157 {
158         1.0, 3.5, 3.5, 2.5, 4.7,
159         1.0, 3.5, 3.6, 2.5, 4.7,
160         1.0, 1.5, 2.2, 2.1, 3.5
161 };
162 static DBL  Mid_Prm[MAX_PRM] =
163 {
164         0.0, 1.5, 0.0, .5, 1.5,
165         0.0, 1.5, 0.0, .5, 1.5,
166         0.0, 1.5, -1.0, -.5, 2.5,
167 };
168
169 static      DBL
170 Gauss_Rand(DBL c, DBL A, DBL S)
171 {
172         DBL         y;
173
174         y = (DBL) LRAND() / MAXRAND;
175         y = A * (1.0 - exp(-y * y * S)) / (1.0 - exp(-S));
176         if (NRAND(2))
177                 return (c + y);
178         else
179                 return (c - y);
180 }
181
182 static void
183 Random_Prm(DBL * Prm)
184 {
185         int         i;
186
187         for (i = 0; i < MAX_PRM; ++i)
188                 Prm[i] = Gauss_Rand(Mid_Prm[i], Amp_Prm[i], 4.0);
189 }
190
191 /***************************************************************/
192
193   /* 2 examples of non-linear map */
194
195 static void
196 Iterate_X2(ATTRACTOR * A, PRM x, PRM y, PRM * xo, PRM * yo)
197 {
198         PRM         xx, yy, xy, x2y, y2x, Tmp;
199
200         xx = (x * x) / UNIT;
201         x2y = (xx * y) / UNIT;
202         yy = (y * y) / UNIT;
203         y2x = (yy * x) / UNIT;
204         xy = (x * y) / UNIT;
205
206         Tmp = A->Prm[1] * xx + A->Prm[2] * xy + A->Prm[3] * yy + A->Prm[4] * x2y;
207         Tmp = A->Prm[0] - y + (Tmp / UNIT);
208         *xo = DO_FOLD(Tmp);
209         Tmp = A->Prm[6] * xx + A->Prm[7] * xy + A->Prm[8] * yy + A->Prm[9] * y2x;
210         Tmp = A->Prm[5] + x + (Tmp / UNIT);
211         *yo = DO_FOLD(Tmp);
212 }
213
214 static void
215 Iterate_X3(ATTRACTOR * A, PRM x, PRM y, PRM * xo, PRM * yo)
216 {
217         PRM         xx, yy, xy, x2y, y2x, Tmp_x, Tmp_y, Tmp_z;
218
219         xx = (x * x) / UNIT;
220         x2y = (xx * y) / UNIT;
221         yy = (y * y) / UNIT;
222         y2x = (yy * x) / UNIT;
223         xy = (x * y) / UNIT;
224
225         Tmp_x = A->Prm[1] * xx + A->Prm[2] * xy + A->Prm[3] * yy + A->Prm[4] * x2y;
226         Tmp_x = A->Prm[0] - y + (Tmp_x / UNIT);
227         Tmp_x = DO_FOLD(Tmp_x);
228
229         Tmp_y = A->Prm[6] * xx + A->Prm[7] * xy + A->Prm[8] * yy + A->Prm[9] * y2x;
230         Tmp_y = A->Prm[5] + x + (Tmp_y / UNIT);
231
232         Tmp_y = DO_FOLD(Tmp_y);
233
234         Tmp_z = A->Prm[11] * xx + A->Prm[12] * xy + A->Prm[13] * yy + A->Prm[14] * y2x;
235         Tmp_z = A->Prm[10] + x + (Tmp_z / UNIT);
236         Tmp_z = UNIT + Tmp_z * Tmp_z / UNIT;
237
238         *xo = (Tmp_x * UNIT) / Tmp_z;
239         *yo = (Tmp_y * UNIT) / Tmp_z;
240 }
241
242 static void (*Funcs[2]) (ATTRACTOR *, PRM, PRM, PRM *, PRM *) = {
243         Iterate_X2, Iterate_X3
244 };
245
246 /***************************************************************/
247
248 static void
249 free_strange(Display *display, ATTRACTOR *A)
250 {
251         if (A->Buffer1 != NULL) {
252                 (void) free((void *) A->Buffer1);
253                 A->Buffer1 = (XPoint *) NULL;
254         }
255         if (A->Buffer2 != NULL) {
256                 (void) free((void *) A->Buffer2);
257                 A->Buffer2 = (XPoint *) NULL;
258         }
259         if (A->dbuf) {
260                 XFreePixmap(display, A->dbuf);
261                 A->dbuf = None;
262         }
263         if (A->dbuf_gc) {
264                 XFreeGC(display, A->dbuf_gc);
265                 A->dbuf_gc = None;
266         }
267         if (A->Fold != NULL) {
268                 (void) free((void *) A->Fold);
269                 A->Fold = (PRM *) NULL;
270         }
271 }
272
273 ENTRYPOINT void
274 draw_strange(ModeInfo * mi)
275 {
276         int         i, j, n, Cur_Pt;
277         PRM         x, y, xo, yo;
278         DBL         u;
279         XPoint     *Buf;
280         Display    *display = MI_DISPLAY(mi);
281         Window      window = MI_WINDOW(mi);
282         GC          gc = MI_GC(mi);
283         DBL         Lx, Ly;
284         void        (*Iterate) (ATTRACTOR *, PRM, PRM, PRM *, PRM *);
285         PRM         xmin, xmax, ymin, ymax;
286         ATTRACTOR  *A;
287
288         if (Root == NULL)
289                 return;
290         A = &Root[MI_SCREEN(mi)];
291         if (A->Fold == NULL)
292                 return;
293
294         Cur_Pt = A->Cur_Pt;
295         Iterate = A->Iterate;
296
297         u = (DBL) (A->Count) / 40000.0;
298         for (j = MAX_PRM - 1; j >= 0; --j)
299                 A->Prm[j] = DBL_To_PRM((1.0 - u) * A->Prm1[j] + u * A->Prm2[j]);
300
301         /* We collect the accumulation of the orbits in the 2d int array field. */
302 #ifndef POINTS_HISTORY
303         #ifdef useAccumulator
304                 if (useAccumulator) {
305                         for (i=0;i<A->Width;i++) {
306                                 for (j=0;j<A->Height;j++) {
307                                         A->accMap[i][j] = 0;
308
309                                 }
310                         }
311                 }
312         #endif
313 #endif
314
315         x = y = DBL_To_PRM(.0);
316         for (n = SKIP_FIRST; n; --n) {
317                 (*Iterate) (A, x, y, &xo, &yo);
318                 x = xo + NRAND(8) - 4;
319                 y = yo + NRAND(8) - 4;
320         }
321
322         xmax = 0;
323         xmin = UNIT * 4;
324         ymax = 0;
325         ymin = UNIT * 4;
326         A->Cur_Pt = 0;
327         Buf = A->Buffer2;
328         Lx = (DBL) A->Width / UNIT / 2.2;
329         Ly = (DBL) A->Height / UNIT / 2.2;
330         for (n = A->Max_Pt; n; --n) {
331                 (*Iterate) (A, x, y, &xo, &yo);
332                 #ifdef useAccumulator
333                 if (useAccumulator) {
334                         int mx,my;
335                         mx = (short) ( A->Width*0.1 + A->Width*0.8 * (xo - xmin) / (xmax - xmin) );
336                         my = (short) ( A->Width*0.1 + (A->Height - A->Width*0.2) * (yo - ymin) / (ymax - ymin) );
337                         if (mx>=0 && my>=0 && mx<A->Width && my<A->Height) {
338                                 A->accMap[mx][my]++;
339                         }
340 #ifdef POINTS_HISTORY
341                 /* #define clearOldPoint(i) { if (startedClearing) { field[oldPoints[i].x][oldPoints[i].y]--; } }
342                 #define saveUnplot(X,Y) { clearOldPoint(oldPointsIndex) oldPoints[oldPointsIndex].x = X; oldPoints[oldPointsIndex].y = Y; oldPointsIndex = (oldPointsIndex + 1) % numOldPoints; if (oldPointsIndex==0) { startedClearing=1; } }
343                 saveUnplot(mx,my) */
344                 if (startedClearing) {
345                         int oldX = oldPointsX[oldPointsIndex];
346                         int oldY = oldPointsY[oldPointsIndex];
347                         if (oldX>=0 && oldY>=0 && oldX<A->Width && oldY<A->Height) {
348                                 A->accMap[oldX][oldY]--;
349                         }
350                 }
351                 oldPointsX[oldPointsIndex] = mx;
352                 oldPointsY[oldPointsIndex] = my;
353                 oldPointsIndex = (oldPointsIndex + 1) % numOldPoints;
354                 if (oldPointsIndex==0) { startedClearing=1; }
355 #endif
356                 } else {
357                 #endif
358                         Buf->x = (int) (Lx * (x + DBL_To_PRM(1.1)));
359                         Buf->y = (int) (Ly * (DBL_To_PRM(1.1) - y));
360                         Buf++;
361                         A->Cur_Pt++;
362                 #ifdef useAccumulator
363                 }
364                 #endif
365                 /* (void) fprintf( stderr, "X,Y: %d %d    ", Buf->x, Buf->y ); */
366                 if (xo > xmax)
367                         xmax = xo;
368                 else if (xo < xmin)
369                         xmin = xo;
370                 if (yo > ymax)
371                         ymax = yo;
372                 else if (yo < ymin)
373                         ymin = yo;
374                 x = xo + NRAND(8) - 4;
375                 y = yo + NRAND(8) - 4;
376         }
377
378         MI_IS_DRAWN(mi) = True;
379
380         XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
381
382         #ifdef useAccumulator
383         if (useAccumulator) {
384                 float colorScale;
385                 int col;
386                 #ifdef VARY_SPEED_TO_AVOID_BOREDOM
387                 int pixelCount = 0;
388                 #endif
389                 colorScale = (A->Width*A->Height/640.0/480.0*800000.0/(float)A->Max_Pt*(float)NUM_COLS/256);
390                 if (A->dbuf != None) {
391                         XSetForeground(display, A->dbuf_gc, 0);
392                         XFillRectangle(display, A->dbuf, A->dbuf_gc, 0, 0, A->Width, A->Height);
393                 } else {
394                         XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
395                         XFillRectangle(display, window, gc, 0, 0, A->Width, A->Height);
396                 }
397                 for (i=0;i<A->Width;i++) {
398                         for (j=0;j<A->Height;j++) {
399                                 if (A->accMap[i][j]>0) {
400                                         col = (float)A->accMap[i][j] * colorScale;
401                                         if (col>NUM_COLS-1) {
402                                                 col = NUM_COLS-1;
403                                         }
404                                         #ifdef VARY_SPEED_TO_AVOID_BOREDOM
405                                         if (col>0) {
406                                                 if (col<NUM_COLS-1)  /* we don't count maxxed out pixels */
407                                                         pixelCount++;
408                                         }
409                                         #endif
410                                         if (MI_NPIXELS(mi) < 2)
411                                                 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
412                                         else
413                                                 /*XSetForeground(display, gc, MI_PIXEL(mi, A->Col % MI_NPIXELS(mi)));*/
414                                                 XSetForeground(display, gc, cols[col].pixel);
415                                         if (A->dbuf != None) {
416                                                 XSetForeground(display, A->dbuf_gc, cols[col].pixel);
417                                                 XDrawPoint(display, A->dbuf, A->dbuf_gc, i, j);
418                                         } else {
419                                                 XSetForeground(display, gc, cols[col].pixel);
420                                                 XDrawPoint(display, window, gc, i, j);
421                                         }
422                                 }
423                         }
424                 }
425                 if (A->dbuf != None) {
426                         XCopyArea(display, A->dbuf, window, gc, 0, 0, A->Width, A->Height, 0, 0);
427                 }
428                 #ifdef VARY_SPEED_TO_AVOID_BOREDOM
429                         /* Increaase the rate of change of the parameters if the attractor has become visually boring. */
430                         if ((xmax - xmin < DBL_To_PRM(.2)) && (ymax - ymin < DBL_To_PRM(.2))) {
431                                 A->Speed *= 1.25;
432                         } else if (pixelCount>0 && pixelCount<A->Width*A->Height/1000) {
433                                 A->Speed *= 1.25;  /* A->Count = 1000; */
434                         } else {
435                                 A->Speed = 4; /* reset to normal/default */
436                         }
437                         if (A->Speed > 32)
438                                 A->Speed = 32;
439                         A->Count += A->Speed;
440                         if (A->Count >= 1000) {
441                                 for (i = MAX_PRM - 1; i >= 0; --i)
442                                         A->Prm1[i] = A->Prm2[i];
443                                 Random_Prm(A->Prm2);
444                                 A->Count = 0;
445                         }
446                 #endif
447         } else {
448         #endif
449
450         if (A->dbuf != None) {          /* jwz */
451                 XSetForeground(display, A->dbuf_gc, 0);
452 /* XDrawPoints(display, A->dbuf, A->dbuf_gc, A->Buffer1,
453   Cur_Pt,CoordModeOrigin); */
454                 XFillRectangle(display, A->dbuf, A->dbuf_gc, 0, 0, A->Width, A->Height);
455         } else
456                 XDrawPoints(display, window, gc, A->Buffer1, Cur_Pt, CoordModeOrigin);
457
458         if (MI_NPIXELS(mi) < 2)
459                 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
460         else
461                 XSetForeground(display, gc, MI_PIXEL(mi, A->Col % MI_NPIXELS(mi)));
462
463         if (A->dbuf != None) {
464                 XSetForeground(display, A->dbuf_gc, 1);
465                 XDrawPoints(display, A->dbuf, A->dbuf_gc, A->Buffer2, A->Cur_Pt,
466                             CoordModeOrigin);
467                 XCopyPlane(display, A->dbuf, window, gc, 0, 0, A->Width, A->Height, 0, 0, 1);
468         } else
469                 XDrawPoints(display, window, gc, A->Buffer2, A->Cur_Pt, CoordModeOrigin);
470
471         #ifdef useAccumulator
472         }
473         #endif
474
475         Buf = A->Buffer1;
476         A->Buffer1 = A->Buffer2;
477         A->Buffer2 = Buf;
478
479         if ((xmax - xmin < DBL_To_PRM(.2)) && (ymax - ymin < DBL_To_PRM(.2)))
480                 A->Count += 4 * A->Speed;
481         else
482                 A->Count += A->Speed;
483         if (A->Count >= 1000) {
484                 for (i = MAX_PRM - 1; i >= 0; --i)
485                         A->Prm1[i] = A->Prm2[i];
486                 Random_Prm(A->Prm2);
487                 A->Count = 0;
488         }
489         A->Col++;
490     mi->recursion_depth = A->Count;
491 }
492
493
494 /***************************************************************/
495
496 ENTRYPOINT void
497 init_strange(ModeInfo * mi)
498 {
499         Display    *display = MI_DISPLAY(mi);
500         Window      window = MI_WINDOW(mi);
501 #ifndef NO_DBUF
502         GC          gc = MI_GC(mi);
503 #endif
504         ATTRACTOR  *Attractor;
505
506 #ifdef POINTS_HISTORY
507         startedClearing=0;
508         oldPointsIndex=0;
509 #endif
510
511         if (Root == NULL) {
512                 if ((Root = (ATTRACTOR *) calloc(MI_NUM_SCREENS(mi),
513                                 sizeof (ATTRACTOR))) == NULL)
514                         return;
515         }
516         Attractor = &Root[MI_SCREEN(mi)];
517
518         if (Attractor->Fold == NULL) {
519                 int         i;
520
521                 if ((Attractor->Fold = (PRM *) calloc(UNIT2 + 1,
522                                 sizeof (PRM))) == NULL) {
523                         free_strange(display, Attractor);
524                         return;
525                 }
526                 for (i = 0; i <= UNIT2; ++i) {
527                         DBL         x;
528
529                         /* x = ( DBL )(i)/UNIT2; */
530                         /* x = sin( M_PI/2.0*x ); */
531                         /* x = sqrt( x ); */
532                         /* x = x*x; */
533                         /* x = x*(1.0-x)*4.0; */
534                         x = (DBL) (i) / UNIT;
535                         x = sin(x);
536                         Attractor->Fold[i] = DBL_To_PRM(x);
537                 }
538         }
539
540         Attractor->Max_Pt = points;
541
542         if (Attractor->Buffer1 == NULL)
543                 if ((Attractor->Buffer1 = (XPoint *) calloc(Attractor->Max_Pt,
544                                 sizeof (XPoint))) == NULL) {
545                         free_strange(display, Attractor);
546                         return;
547                 }
548         if (Attractor->Buffer2 == NULL)
549                 if ((Attractor->Buffer2 = (XPoint *) calloc(Attractor->Max_Pt,
550                                 sizeof (XPoint))) == NULL) {
551                         free_strange(display, Attractor);
552                         return;
553                 }
554
555         Attractor->Width = MI_WIDTH(mi);
556         Attractor->Height = MI_HEIGHT(mi);
557         Attractor->Cur_Pt = 0;
558         Attractor->Count = 0;
559         Attractor->Col = NRAND(MI_NPIXELS(mi));
560         Attractor->Speed = 4;
561
562         Attractor->Iterate = Funcs[NRAND(2)];
563         Random_Prm(Attractor->Prm1);
564         Random_Prm(Attractor->Prm2);
565 #ifndef NO_DBUF
566         if (Attractor->dbuf != None)
567                 XFreePixmap(display, Attractor->dbuf);
568 #ifdef useAccumulator
569 #define colorDepth ( useAccumulator ? MI_DEPTH(mi) : 1 )
570 #else
571 #define colorDepth 1
572 #endif
573         Attractor->dbuf = XCreatePixmap(display, window,
574              Attractor->Width, Attractor->Height, colorDepth);
575         /* Allocation checked */
576         if (Attractor->dbuf != None) {
577                 XGCValues   gcv;
578
579                 gcv.foreground = 0;
580                 gcv.background = 0;
581 #ifndef HAVE_COCOA
582                 gcv.graphics_exposures = False;
583 #endif /* HAVE_COCOA */
584                 gcv.function = GXcopy;
585
586                 if (Attractor->dbuf_gc != None)
587                         XFreeGC(display, Attractor->dbuf_gc);
588
589                 if ((Attractor->dbuf_gc = XCreateGC(display, Attractor->dbuf,
590 #ifndef HAVE_COCOA
591                                 GCGraphicsExposures |
592 #endif /* HAVE_COCOA */
593                                GCFunction | GCForeground | GCBackground,
594                                 &gcv)) == None) {
595                         XFreePixmap(display, Attractor->dbuf);
596                         Attractor->dbuf = None;
597                 } else {
598                         XFillRectangle(display, Attractor->dbuf, Attractor->dbuf_gc,
599                                 0, 0, Attractor->Width, Attractor->Height);
600                         XSetBackground(display, gc, MI_BLACK_PIXEL(mi));
601                         XSetFunction(display, gc, GXcopy);
602                 }
603         }
604 #endif
605
606
607 #ifdef useAccumulator
608         #define A Attractor
609         if (useAccumulator) {
610                 XWindowAttributes xgwa;
611                 int i,j;
612                 XGetWindowAttributes (display, window, &xgwa);
613                 /* cmap = xgwa.colormap; */
614                 /* cmap = XCreateColormap(display, window, MI_VISUAL(mi), AllocAll); */
615                 Attractor->accMap = (int**)calloc(Attractor->Width,sizeof(int*));
616                 for (i=0;i<Attractor->Width;i++) {
617                         Attractor->accMap[i] = (int*)calloc(Attractor->Height,sizeof(int));
618                         for (j=0;j<Attractor->Height;j++) {
619                                 Attractor->accMap[i][j] = 0;
620                         }
621                 }
622 #ifdef POINTS_HISTORY
623                 numOldPoints = A->Max_Pt * MERGE_FRAMES;
624                 oldPointsX = (int*)calloc(numOldPoints,sizeof(int));
625                 oldPointsY = (int*)calloc(numOldPoints,sizeof(int));
626 #endif
627                 cols = (XColor*)calloc(NUM_COLS,sizeof(XColor));
628                 for (i=0;i<NUM_COLS;i++) {
629                         float li;
630                         #define MINBLUE 1
631                         #define FULLBLUE 128
632                         li = MINBLUE + (255.0-MINBLUE) * log(1.0 + ACC_GAMMA*(float)i/NUM_COLS) / log(1.0 + ACC_GAMMA);
633                         if (li<FULLBLUE) {
634                                 cols[i].red = 0;
635                                 cols[i].green = 0;
636                                 cols[i].blue = 65536*li/FULLBLUE;
637                         } else {
638                                 cols[i].red = 65536*(li-FULLBLUE)/(256-FULLBLUE);
639                                 cols[i].green = 65536*(li-FULLBLUE)/(256-FULLBLUE);
640                                 cols[i].blue = 65535;
641                         }
642                         XAllocColor (display, xgwa.colormap, &cols[i]);
643                         /*
644                         if (!XAllocColor(MI_DISPLAY(mi), cmap, &cols[i])) {
645                         if (!XAllocColor(display, cmap, &cols[i])) {
646                                 cols[i].pixel = WhitePixel (display, DefaultScreen (display));
647                                 cols[i].red = cols[i].green = cols[i].blue = 0xFFFF;
648                         }
649                         */
650                 }
651                 /*
652                 XSetWindowColormap(display, window, cmap);
653                 (void) XSetWMColormapWindows(display, window, &window, 1);
654                 XInstallColormap(display, cmap);
655                 XStoreColors(display, cmap, cols, 256);
656                 */
657         }
658         #undef A
659 #endif
660         MI_CLEARWINDOW(mi);
661
662         /* Do not want any exposure events from XCopyPlane */
663         XSetGraphicsExposures(display, MI_GC(mi), False);
664 }
665
666 ENTRYPOINT void
667 reshape_strange(ModeInfo * mi, int width, int height)
668 {
669   XClearWindow (MI_DISPLAY (mi), MI_WINDOW(mi));
670   init_strange (mi);
671 }
672
673 /***************************************************************/
674
675 ENTRYPOINT void
676 release_strange(ModeInfo * mi)
677 {
678         if (Root != NULL) {
679                 int         screen;
680
681 #ifdef useAccumulator
682                 int i;
683                 (void) free((void *) cols);
684                 for (i=0;i<Root->Width;i++) {
685                         (void) free((void *) Root->accMap[i]);
686                 }
687                 (void) free((void *) Root->accMap);
688 #endif
689 #ifdef POINTS_HISTORY
690                 free(oldPointsX);
691                 free(oldPointsY);
692 #endif
693                 for (screen = 0; screen < MI_NUM_SCREENS(mi); ++screen)
694                         free_strange(MI_DISPLAY(mi), &Root[screen]);
695                 (void) free((void *) Root);
696                 Root = (ATTRACTOR *) NULL; 
697         }
698 }
699
700 XSCREENSAVER_MODULE ("Strange", strange)
701
702 #endif /* MODE_strange */