http://www.jwz.org/xscreensaver/xscreensaver-5.12.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
43 # define SMOOTH_COLORS
44 # define refresh_strange 0
45 # define reshape_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 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 }
491
492
493 /***************************************************************/
494
495 ENTRYPOINT void
496 init_strange(ModeInfo * mi)
497 {
498         Display    *display = MI_DISPLAY(mi);
499         Window      window = MI_WINDOW(mi);
500 #ifndef NO_DBUF
501         GC          gc = MI_GC(mi);
502 #endif
503         ATTRACTOR  *Attractor;
504
505 #ifdef POINTS_HISTORY
506         startedClearing=0;
507         oldPointsIndex=0;
508 #endif
509
510         if (Root == NULL) {
511                 if ((Root = (ATTRACTOR *) calloc(MI_NUM_SCREENS(mi),
512                                 sizeof (ATTRACTOR))) == NULL)
513                         return;
514         }
515         Attractor = &Root[MI_SCREEN(mi)];
516
517         if (Attractor->Fold == NULL) {
518                 int         i;
519
520                 if ((Attractor->Fold = (PRM *) calloc(UNIT2 + 1,
521                                 sizeof (PRM))) == NULL) {
522                         free_strange(display, Attractor);
523                         return;
524                 }
525                 for (i = 0; i <= UNIT2; ++i) {
526                         DBL         x;
527
528                         /* x = ( DBL )(i)/UNIT2; */
529                         /* x = sin( M_PI/2.0*x ); */
530                         /* x = sqrt( x ); */
531                         /* x = x*x; */
532                         /* x = x*(1.0-x)*4.0; */
533                         x = (DBL) (i) / UNIT;
534                         x = sin(x);
535                         Attractor->Fold[i] = DBL_To_PRM(x);
536                 }
537         }
538
539         Attractor->Max_Pt = points;
540
541         if (Attractor->Buffer1 == NULL)
542                 if ((Attractor->Buffer1 = (XPoint *) calloc(Attractor->Max_Pt,
543                                 sizeof (XPoint))) == NULL) {
544                         free_strange(display, Attractor);
545                         return;
546                 }
547         if (Attractor->Buffer2 == NULL)
548                 if ((Attractor->Buffer2 = (XPoint *) calloc(Attractor->Max_Pt,
549                                 sizeof (XPoint))) == NULL) {
550                         free_strange(display, Attractor);
551                         return;
552                 }
553
554         Attractor->Width = MI_WIDTH(mi);
555         Attractor->Height = MI_HEIGHT(mi);
556         Attractor->Cur_Pt = 0;
557         Attractor->Count = 0;
558         Attractor->Col = NRAND(MI_NPIXELS(mi));
559         Attractor->Speed = 4;
560
561         Attractor->Iterate = Funcs[NRAND(2)];
562         Random_Prm(Attractor->Prm1);
563         Random_Prm(Attractor->Prm2);
564 #ifndef NO_DBUF
565         if (Attractor->dbuf != None)
566                 XFreePixmap(display, Attractor->dbuf);
567 #ifdef useAccumulator
568 #define colorDepth ( useAccumulator ? MI_DEPTH(mi) : 1 )
569 #else
570 #define colorDepth 1
571 #endif
572         Attractor->dbuf = XCreatePixmap(display, window,
573              Attractor->Width, Attractor->Height, colorDepth);
574         /* Allocation checked */
575         if (Attractor->dbuf != None) {
576                 XGCValues   gcv;
577
578                 gcv.foreground = 0;
579                 gcv.background = 0;
580 #ifndef HAVE_COCOA
581                 gcv.graphics_exposures = False;
582 #endif /* HAVE_COCOA */
583                 gcv.function = GXcopy;
584
585                 if (Attractor->dbuf_gc != None)
586                         XFreeGC(display, Attractor->dbuf_gc);
587
588                 if ((Attractor->dbuf_gc = XCreateGC(display, Attractor->dbuf,
589 #ifndef HAVE_COCOA
590                                 GCGraphicsExposures |
591 #endif /* HAVE_COCOA */
592                                GCFunction | GCForeground | GCBackground,
593                                 &gcv)) == None) {
594                         XFreePixmap(display, Attractor->dbuf);
595                         Attractor->dbuf = None;
596                 } else {
597                         XFillRectangle(display, Attractor->dbuf, Attractor->dbuf_gc,
598                                 0, 0, Attractor->Width, Attractor->Height);
599                         XSetBackground(display, gc, MI_BLACK_PIXEL(mi));
600                         XSetFunction(display, gc, GXcopy);
601                 }
602         }
603 #endif
604
605
606 #ifdef useAccumulator
607         #define A Attractor
608         if (useAccumulator) {
609                 XWindowAttributes xgwa;
610                 int i,j;
611                 XGetWindowAttributes (display, window, &xgwa);
612                 /* cmap = xgwa.colormap; */
613                 /* cmap = XCreateColormap(display, window, MI_VISUAL(mi), AllocAll); */
614                 Attractor->accMap = (int**)calloc(Attractor->Width,sizeof(int*));
615                 for (i=0;i<Attractor->Width;i++) {
616                         Attractor->accMap[i] = (int*)calloc(Attractor->Height,sizeof(int));
617                         for (j=0;j<Attractor->Height;j++) {
618                                 Attractor->accMap[i][j] = 0;
619                         }
620                 }
621 #ifdef POINTS_HISTORY
622                 numOldPoints = A->Max_Pt * MERGE_FRAMES;
623                 oldPointsX = (int*)calloc(numOldPoints,sizeof(int));
624                 oldPointsY = (int*)calloc(numOldPoints,sizeof(int));
625 #endif
626                 cols = (XColor*)calloc(NUM_COLS,sizeof(XColor));
627                 for (i=0;i<NUM_COLS;i++) {
628                         float li;
629                         #define MINBLUE 1
630                         #define FULLBLUE 128
631                         li = MINBLUE + (255.0-MINBLUE) * log(1.0 + ACC_GAMMA*(float)i/NUM_COLS) / log(1.0 + ACC_GAMMA);
632                         if (li<FULLBLUE) {
633                                 cols[i].red = 0;
634                                 cols[i].green = 0;
635                                 cols[i].blue = 65536*li/FULLBLUE;
636                         } else {
637                                 cols[i].red = 65536*(li-FULLBLUE)/(256-FULLBLUE);
638                                 cols[i].green = 65536*(li-FULLBLUE)/(256-FULLBLUE);
639                                 cols[i].blue = 65535;
640                         }
641                         XAllocColor (display, xgwa.colormap, &cols[i]);
642                         /*
643                         if (!XAllocColor(MI_DISPLAY(mi), cmap, &cols[i])) {
644                         if (!XAllocColor(display, cmap, &cols[i])) {
645                                 cols[i].pixel = WhitePixel (display, DefaultScreen (display));
646                                 cols[i].red = cols[i].green = cols[i].blue = 0xFFFF;
647                         }
648                         */
649                 }
650                 /*
651                 XSetWindowColormap(display, window, cmap);
652                 (void) XSetWMColormapWindows(display, window, &window, 1);
653                 XInstallColormap(display, cmap);
654                 XStoreColors(display, cmap, cols, 256);
655                 */
656         }
657         #undef A
658 #endif
659         MI_CLEARWINDOW(mi);
660
661         /* Do not want any exposure events from XCopyPlane */
662         XSetGraphicsExposures(display, MI_GC(mi), False);
663 }
664
665 /***************************************************************/
666
667 ENTRYPOINT void
668 release_strange(ModeInfo * mi)
669 {
670         if (Root != NULL) {
671                 int         screen;
672
673 #ifdef useAccumulator
674                 int i;
675                 (void) free((void *) cols);
676                 for (i=0;i<Root->Width;i++) {
677                         (void) free((void *) Root->accMap[i]);
678                 }
679                 (void) free((void *) Root->accMap);
680 #endif
681 #ifdef POINTS_HISTORY
682                 free(oldPointsX);
683                 free(oldPointsY);
684 #endif
685                 for (screen = 0; screen < MI_NUM_SCREENS(mi); ++screen)
686                         free_strange(MI_DISPLAY(mi), &Root[screen]);
687                 (void) free((void *) Root);
688                 Root = (ATTRACTOR *) NULL; 
689         }
690 }
691
692 XSCREENSAVER_MODULE ("Strange", strange)
693
694 #endif /* MODE_strange */