1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* strange --- strange attractors */
5 static const char sccsid[] = "@(#)strange.c 5.00 2000/11/01 xlockmore";
9 * Copyright (c) 1997 by Massimino Pascal <Pascal.Massimon@ens.fr>
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.
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.
24 * 10-Apr-2017: dmo2118@gmail.com: Enhancements for accumulator mode:
25 * Performance tuning, varying colors, fixed wobbliness.
26 * New options: point size, zoom, brightness, motion blur.
27 * 08-Apr-2017: dmo2118@gmail.com: Merged with current xlockmore strange.c.
28 * Also resurrected the -curve parameter from XScreenSaver 2.31.
29 * Multi-monitor fixes.
30 * More allocation checks.
31 * 13-Apr-2010: Added useAccumulator, VARY_SPEED_TO_AVOID_BOREDOM.
32 * 22-Dec-2004: TDA: Replace Gauss_Rand with a real Gaussian.
33 * 01-Nov-2000: Allocation checks
34 * 30-Jul-1998: sineswiper@resonatorsoft.com: added curve factor (discovered
35 * while experimenting with the Gauss_Rand function).
36 * 10-May-1997: jwz AT jwz.org: turned into a standalone program.
37 * Made it render into an offscreen bitmap and then copy
38 * that onto the screen, to reduce flicker.
40 * strange attractors are not so hard to find...
44 * ./strange -points 500000 -delay 0 -point-size 3
45 * ./strange -points 500000 -delay 0 -point-size 2 -curve 5 -zoom 1.5 -brightness 0.75
48 /* TODO: Can anything be done about the overflow with -point-size 32? */
52 # define DEFAULTS "*delay: 10000 \n" \
54 "*fpsSolid: True \n" \
55 "*ignoreRotation: True \n" \
57 "*useThreads: True \n" \
59 /* "*lowrez: True \n" \ */
61 # define SMOOTH_COLORS
62 # define release_strange 0
63 # define reshape_strange 0
64 # define strange_handle_event 0
65 # include "xlockmore.h" /* from the xscreensaver distribution */
66 #else /* !STANDALONE */
67 # include "xlock.h" /* from the xlockmore distribution */
69 #endif /* !STANDALONE */
73 #include "thread_util.h"
76 #ifdef HAVE_INTTYPES_H
77 # include <inttypes.h>
81 #define DEF_CURVE "10"
82 #define DEF_POINTS "5500"
83 #define DEF_POINT_SIZE "1"
84 #define DEF_ZOOM "0.9" /* approx. 1 / 1.1 */
85 #define DEF_BRIGHTNESS "1.0"
86 #define DEF_MOTION_BLUR "3.0" /* Formerly MERGE_FRAMES, but it's IIR now. */
92 static float brightness;
93 static float motionBlur;
95 static XrmOptionDescRec opts[] =
97 {"-curve", ".strange.curve", XrmoptionSepArg, 0},
98 {"-points", ".strange.points", XrmoptionSepArg, 0},
99 {"-point-size", ".strange.pointSize", XrmoptionSepArg, 0},
100 {"-zoom", ".strange.zoom", XrmoptionSepArg, 0},
101 {"-brightness", ".strange.brightness", XrmoptionSepArg, 0},
102 {"-motion-blur", ".strange.motionBlur", XrmoptionSepArg, 0},
105 static argtype vars[] =
107 {&curve, "curve", "Curve", DEF_CURVE, t_Int},
108 {&points, "points", "Points", DEF_POINTS, t_Int},
109 {&pointSize, "pointSize", "PointSize", DEF_POINT_SIZE, t_Int},
110 {&zoom, "zoom", "Zoom", DEF_ZOOM, t_Float},
111 {&brightness, "brightness", "Brightness", DEF_BRIGHTNESS, t_Float},
112 {&motionBlur, "motionBlur", "MotionBlur", DEF_MOTION_BLUR, t_Float},
114 static OptionStruct desc[] =
116 {"-curve", "set the curve factor of the attractors"},
117 {"-points", "change the number of points/iterations each frame"},
118 {"-point-size", "change the size of individual points"},
119 {"-zoom", "zoom in or out"},
120 {"-brightness", "adjust the brightness for accumulator mode"},
121 {"-motion-blur", "adds motion blur"},
123 ENTRYPOINT ModeSpecOpt strange_opts =
124 {sizeof opts / sizeof opts[0], opts,
125 sizeof vars / sizeof vars[0], vars, desc};
128 ModStruct strange_description =
129 {"strange", "init_strange", "draw_strange", (char *) NULL,
130 "init_strange", "init_strange", "free_strange", &strange_opts,
131 10000, 1, 1, 1, 64, 1.0, "",
132 "Shows strange attractors", 0, NULL};
143 #define UNIT (1<<UNIT_BITS)
144 #define UNIT2 (1<<14)
145 #define COLOR_BITS 16
146 /* #define UNIT2 (3140*UNIT/1000) */
148 #define SKIP_FIRST 100
149 #define DBL_To_PRM(x) (PRM)( (DBL)(UNIT)*(x) )
152 #define DO_FOLD(a) (a)<0 ? -A->Fold[ (-(a))&(UNIT2-1) ] : A->Fold[ (a)&(UNIT2-1) ]
155 #define DO_FOLD(a) (a)<-UNIT2 ? -A->Fold[(-(a))%UNIT2] : (a)<0 ? -A->Fold[ -(a) ] :\
156 (a)>UNIT2 ? A->Fold[ (a)%UNIT2 ] : A->Fold[ (a) ]
157 #define DO_FOLD(a) DBL_To_PRM( sin( (DBL)(a)/UNIT ) )
158 #define DO_FOLD(a) (a)<0 ? DBL_To_PRM( exp( 16.0*(a)/UNIT2 ) )-1.0 : \
159 DBL_To_PRM( 1.0-exp( -16.0*(a)/UNIT2 ) )
162 /* useAccumulator performs two functions:
163 * If it is defined, then support for the accumulator will be compiled.
164 * It is also the condition for which the accumulator renderer will engage.
166 #define useAccumulator (A->Max_Pt > 6000)
167 #define ACC_GAMMA 10.0
168 #define DEF_NUM_COLS 150
170 #define VARY_SPEED_TO_AVOID_BOREDOM
171 /*#define AUTO_ZOOM*/ /* Works funny, but try it with -curve 5. */
173 /******************************************************************/
177 #if defined(__BIGGEST_ALIGNMENT__) \
178 && (defined(__GNUC__) \
179 && (__GNUC__ == 4 && __GNUC_MINOR__ >= 7 || __GNUC__ >= 5) \
180 || defined(__clang__))
181 # define ALIGNED __attribute__((aligned(__BIGGEST_ALIGNMENT__)))
182 # define ALIGN_HINT(ptr) __builtin_assume_aligned((ptr), __BIGGEST_ALIGNMENT__)
185 # define ALIGN_HINT(ptr) (ptr)
186 # ifndef __BIGGEST_ALIGNMENT__
187 # define __BIGGEST_ALIGNMENT__ (sizeof(void *))
192 #ifdef HAVE_INTTYPES_H
193 typedef uint16_t ALIGNED PIXEL0;
194 typedef uint32_t PIXEL0X;
195 typedef uint32_t ALIGNED PIXEL1;
197 typedef unsigned short ALIGNED PIXEL0;
198 typedef unsigned long PIXEL0X;
199 typedef unsigned long ALIGNED PIXEL1;
203 #ifdef HAVE_INTTYPES_H
207 unsigned short signature;
208 unsigned char bytes[2];
210 } byte_order_union = {MSBFirst};
212 #define LOCAL_BYTE_ORDER byte_order_union.bytes[1]
214 typedef struct _ATTRACTOR {
215 DBL Prm1[MAX_PRM], Prm2[MAX_PRM];
216 PRM Prm[MAX_PRM], *Fold;
217 void (*Iterate) (const struct _ATTRACTOR *, PRM, PRM, PRM *, PRM *);
218 void *Buffer1, *Buffer2; /* Either XPoint or XRectangle. */
220 int Col, Count, Speed;
222 Pixmap dbuf; /* jwz */
224 #ifdef useAccumulator
227 unsigned long rMask, gMask, bMask;
228 unsigned rShift, gShift, bShift;
229 PIXEL0 blurFac; /* == 0 when no motion blur is taking place. */
235 XShmSegmentInfo shmInfo;
236 struct _THREAD **threads;
237 struct threadpool pool;
241 static ATTRACTOR *Root = (ATTRACTOR *) NULL;
243 #ifdef useAccumulator
244 typedef struct _THREAD {
245 const ATTRACTOR *Attractor;
254 PRM xmin, xmax, ymin, ymax;
259 static DBL Amp_Prm[MAX_PRM] =
261 1.0, 3.5, 3.5, 2.5, 4.7,
262 1.0, 3.5, 3.6, 2.5, 4.7,
263 1.0, 1.5, 2.2, 2.1, 3.5
265 static DBL Mid_Prm[MAX_PRM] =
267 0.0, 1.5, 0.0, .5, 1.5,
268 0.0, 1.5, 0.0, .5, 1.5,
269 0.0, 1.5, -1.0, -.5, 2.5,
274 static inline uint64_t frq(void)
279 static inline uint64_t tick(void)
282 gettimeofday(&tv, NULL);
283 return tv.tv_sec * frq() + tv.tv_usec;
289 Old_Gauss_Rand(DBL c, DBL A, DBL S)
293 y = (DBL) LRAND() / MAXRAND;
295 y = A * (z - exp(-y * y * S)) / (z - exp(-S));
303 /* dmo2118: seems to be responsible for lots of boring-looking rings */
305 /* I don't know that it makes a difference, but this one actually *is*
308 /* Generate Gaussian random number: mean c, "amplitude" A (actually
309 A is 3*standard deviation). 'S' is unused. */
311 /* Note this generates a pair of gaussian variables, so it saves one
312 to give out next time it's called */
315 Gauss_Rand(DBL c, DBL A, DBL S)
318 static Bool ready = 0;
325 x = 2.0 * (double)LRAND() / MAXRAND - 1.0;
326 y = 2.0 * (double)LRAND() / MAXRAND - 1.0;
330 w = sqrt((-2 * log(w))/w);
333 return c + A/3 * y * w;
339 Random_Prm(DBL * Prm)
343 for (i = 0; i < MAX_PRM; ++i) {
346 Prm[i] = Gauss_Rand (Mid_Prm[i], Amp_Prm[i], 4.0);
349 Prm[i] = Old_Gauss_Rand (Mid_Prm[i], Amp_Prm[i], 4.0);
353 /***************************************************************/
355 /* 2 examples of non-linear map */
358 Iterate_X2(const ATTRACTOR * A, PRM x, PRM y, PRM * xo, PRM * yo)
360 PRM xx, yy, xy, x2y, y2x, Tmp;
362 xx = (x * x) >> UNIT_BITS;
363 x2y = (xx * y) >> UNIT_BITS;
364 yy = (y * y) >> UNIT_BITS;
365 y2x = (yy * x) >> UNIT_BITS;
366 xy = (x * y) >> UNIT_BITS;
368 Tmp = A->Prm[1] * xx + A->Prm[2] * xy + A->Prm[3] * yy + A->Prm[4] * x2y;
369 Tmp = A->Prm[0] - y + (Tmp >> UNIT_BITS);
371 Tmp = A->Prm[6] * xx + A->Prm[7] * xy + A->Prm[8] * yy + A->Prm[9] * y2x;
372 Tmp = A->Prm[5] + x + (Tmp >> UNIT_BITS);
377 Iterate_X3(const ATTRACTOR * A, PRM x, PRM y, PRM * xo, PRM * yo)
379 PRM xx, yy, xy, x2y, y2x, Tmp_x, Tmp_y, Tmp_z;
381 xx = (x * x) >> UNIT_BITS;
382 x2y = (xx * y) >> UNIT_BITS;
383 yy = (y * y) >> UNIT_BITS;
384 y2x = (yy * x) >> UNIT_BITS;
385 xy = (x * y) >> UNIT_BITS;
387 Tmp_x = A->Prm[1] * xx + A->Prm[2] * xy + A->Prm[3] * yy + A->Prm[4] * x2y;
388 Tmp_x = A->Prm[0] - y + (Tmp_x >> UNIT_BITS);
389 Tmp_x = DO_FOLD(Tmp_x);
391 Tmp_y = A->Prm[6] * xx + A->Prm[7] * xy + A->Prm[8] * yy + A->Prm[9] * y2x;
392 Tmp_y = A->Prm[5] + x + (Tmp_y >> UNIT_BITS);
394 Tmp_y = DO_FOLD(Tmp_y);
396 Tmp_z = A->Prm[11] * xx + A->Prm[12] * xy + A->Prm[13] * yy + A->Prm[14] * y2x;
397 Tmp_z = A->Prm[10] + x + (Tmp_z >> UNIT_BITS);
399 // Without this 'volatile', GCC incorrectly assumes that Tmp_z1
400 // cannot == 0, and optimizes away the 'if (!Tmp_z0)'.
403 PRM Tmp_z0 = UNIT + ((Tmp_z * Tmp_z) >> UNIT_BITS);
405 /* Can happen with -curve 9. */
409 #ifdef HAVE_INTTYPES_H
411 uint64_t Tmp_z1 = (1 << 30) / Tmp_z0;
412 *xo = (Tmp_x * Tmp_z1) >> (30 - UNIT_BITS);
413 *yo = (Tmp_y * Tmp_z1) >> (30 - UNIT_BITS);
416 *xo = (Tmp_x * UNIT) / Tmp_z0;
417 *yo = (Tmp_y * UNIT) / Tmp_z0;
421 static void (*Funcs[2]) (const ATTRACTOR *, PRM, PRM, PRM *, PRM *) = {
422 Iterate_X2, Iterate_X3
425 /***************************************************************/
428 free_strange(ModeInfo *mi)
430 Display *display = MI_DISPLAY(mi);
431 ATTRACTOR *A = &Root[MI_SCREEN(mi)];
433 if (A->Buffer1 != NULL) {
435 A->Buffer1 = (XPoint *) NULL;
437 if (A->Buffer2 != NULL) {
439 A->Buffer2 = (XPoint *) NULL;
442 XFreePixmap(display, A->dbuf);
446 XFreeGC(display, A->dbuf_gc);
449 if (A->Fold != NULL) {
451 A->Fold = (PRM *) NULL;
454 #ifdef useAccumulator
455 if (useAccumulator) {
457 threadpool_destroy (&A->pool);
465 destroy_xshm_image (display, A->accImage, &A->shmInfo);
473 if (A->visualClass != TrueColor && A->numCols > 2)
474 XFreeColors (display, MI_COLORMAP(mi), A->cols, A->numCols, 0);
482 /* NRAND() is also in use; making three PRNGs in total here. */
484 /* ISO/IEC 9899 suggests this one. Doesn't require 64-bit math. */
485 #define GOODRND(seed) ((seed) = (((seed) * 1103515245 + 12345) & 0x7fffffff))
486 #define GOODRND_BITS 31
488 /* Extremely cheap entropy: this is often a single LEA instruction. */
489 #define CHEAPRND(seed) ((seed) = (seed) * 5)
492 init_draw (const ATTRACTOR *A, PRM *x, PRM *y,
493 PRM *xmin, PRM *ymin, PRM *xmax, PRM *ymax, unsigned long *rnd)
503 *x = *y = DBL_To_PRM(.0);
504 for (n = SKIP_FIRST; n; --n) {
505 (*A->Iterate) (A, *x, *y, &xo, &yo);
518 /* Can't use NRAND(), because that modifies global state in a
521 *x = xo + (GOODRND(*rnd) >> (GOODRND_BITS - 3)) - 4;
522 *y = yo + (GOODRND(*rnd) >> (GOODRND_BITS - 3)) - 4;
527 recalc_scale (const ATTRACTOR *A, PRM xmin, PRM ymin, PRM xmax, PRM ymax,
528 DBL *Lx, DBL *Ly, PRM *mx, PRM *my)
537 *Lx = zoom * (DBL) A->Width / (xmax - xmin);
538 *Ly = -zoom * (DBL) A->Height / (ymax - ymin);
539 *mx = A->Width/2 - (xmax + xmin) * *Lx / 2;
540 *my = A->Height/2 - (ymax + ymin) * *Ly / 2;
543 #ifdef useAccumulator
546 thread_destroy (void *Self_Raw)
548 THREAD *T = (THREAD *)Self_Raw;
550 aligned_free (T->accMap[0]);
551 (void) free((void *) T->accMap);
552 aligned_free (T->bloomRows);
553 aligned_free (T->colorRow);
554 aligned_free (T->motionBlur);
558 thread_create (void *Self_Raw, struct threadpool *pool, unsigned id)
560 THREAD *T = (THREAD *)Self_Raw;
562 const ATTRACTOR *A = GET_PARENT_OBJ(ATTRACTOR, pool, pool);
564 memset (T, 0, sizeof(*T));
571 /* The gap between y0 and y1 is to preheat the box blur. */
572 T->y1 = A->Height * id / pool->count;
573 T->y2 = A->Height * (id + 1) / pool->count;
574 T->y0 = T->y1 < pointSize ? 0 : T->y1 - pointSize;
576 T->accMap = (PIXEL0**)calloc(A->Height,sizeof(PIXEL0*));
582 if (aligned_malloc ((void **)&T->accMap[0], __BIGGEST_ALIGNMENT__,
583 A->alignedWidth * A->Height * sizeof(PIXEL0))) {
587 for (i=0;i<A->Height;i++)
588 T->accMap[i] = T->accMap[0] + A->alignedWidth * i;
590 if (aligned_malloc ((void **)&T->bloomRows, __BIGGEST_ALIGNMENT__,
591 A->alignedWidth * (pointSize + 2) * sizeof(*T->bloomRows))) {
595 if (aligned_malloc ((void **)&T->colorRow, __BIGGEST_ALIGNMENT__,
596 A->alignedWidth * sizeof(*T->colorRow))) {
601 if (aligned_malloc ((void **)&T->motionBlur, __BIGGEST_ALIGNMENT__,
602 A->alignedWidth * (T->y2 - T->y1) * sizeof(*T->motionBlur))) {
607 memset (T->motionBlur, 0, A->alignedWidth * (T->y2 - T->y1) * sizeof(*T->motionBlur));
614 points_thread (void *Self_Raw)
616 /* Restricts viewable area to 2^(32-L_Bits). */
617 const unsigned L_Bits = 19; /* Max. image size: 8192x8192. */
619 THREAD *T = (THREAD *)Self_Raw;
620 const ATTRACTOR *A = T->Attractor;
624 PRM iLx, iLy, cx, cy;
625 void (*Iterate) (const ATTRACTOR *, PRM, PRM, PRM *, PRM *);
627 PRM xmax, xmin, ymax, ymin;
629 Iterate = A->Iterate;
631 if (useAccumulator) {
632 memset (T->accMap[0], 0, sizeof(PIXEL0) * A->alignedWidth * A->Height);
635 /* Using CHEAPRND() by itself occasionally gets stuck at 0 mod 8, so seed it
638 init_draw (A, &x, &y, &xmin, &ymin, &xmax, &ymax, &T->Rnd);
639 recalc_scale (A, xmin, ymin, xmax, ymax, &Lx, &Ly, &cx, &cy);
641 Rnd = GOODRND(T->Rnd);
643 iLx = Lx * (1 << L_Bits);
644 iLy = Ly * (1 << L_Bits);
645 if (!iLx) /* Can happen with small windows. */
650 for (n = T->Attractor->Max_Pt / A->pool.count; n; --n) {
652 (*Iterate) (T->Attractor, x, y, &xo, &yo);
653 mx = ((iLx * x) >> L_Bits) + cx;
654 my = ((iLy * y) >> L_Bits) + cy;
655 /* Fun trick: making m[x|y] unsigned means we can skip mx<0 && my<0. */
656 if (mx<A->Width && my<A->Height)
670 recalc_scale (A, xmin, ymin, xmax, ymax, &Lx, &Ly, &cx, &cy);
674 /* Skimp on the randomness. */
675 x = xo + (CHEAPRND(Rnd) >> (sizeof(Rnd) * 8 - 3)) - 4;
676 y = yo + (CHEAPRND(Rnd) >> (sizeof(Rnd) * 8 - 3)) - 4;
681 rasterize_thread (void *Self_Raw)
683 THREAD *T = (THREAD *)Self_Raw;
684 const ATTRACTOR *A = T->Attractor;
686 PRM xmax = 0, xmin = A->Width, ymax = 0, ymin = A->Height;
687 unsigned long colorScale =
688 (double)A->Width * A->Height
689 * (1 << COLOR_BITS) * brightness
691 * (zoom * zoom) / (0.9 * 0.9)
693 / (pointSize * pointSize)
696 * (float)A->numCols/256;
697 #ifdef VARY_SPEED_TO_AVOID_BOREDOM
698 unsigned pixelCount = 0;
700 PIXEL0 *motionBlurRow = T->motionBlur;
701 void *outRow = (char *)A->accImage->data
702 + A->accImage->bytes_per_line * T->y1;
704 /* Clang needs these for loop-vectorizing; A->Width doesn't work. */
705 unsigned w = A->Width, aw = A->alignedWidth;
707 if (A->numCols == 2) /* Brighter for monochrome. */
710 /* bloomRows: row ring buffer, bloom accumulator, in that order. */
711 memset (T->bloomRows, 0, (pointSize + 1) * aw * sizeof(*T->bloomRows));
713 /* This code is highly amenable to auto-vectorization; on GCC use -O3 to get
714 * that. On x86-32, also add -msse2.
716 for (j=T->y0;j<T->y2;j++) {
717 Bool has_col = False;
721 ALIGN_HINT(T->bloomRows + A->alignedWidth * (j % pointSize));
723 ALIGN_HINT(T->bloomRows + A->alignedWidth * pointSize);
724 PIXEL1 *colRow = T->colorRow;
726 /* Moderately fast bloom. */
729 accumRow[i] -= bloomRow[i];
733 for (k=0;k<A->pool.count;k++) {
734 const PIXEL0 *inRow = ALIGN_HINT(A->threads[k]->accMap[j]);
736 /* Lots of last-level cache misses. Such is life. */
737 bloomRow[i] += inRow[i];
741 /* Hardware prefetching works better going forwards than going backwards.
742 * Since this blurs/blooms/convolves in-place, it expands points to the
743 * right instead of to the left.
745 for (i=0;i<pointSize-1;i++)
746 accum += bloomRow[i];
749 PIXEL0 oldBloom = bloomRow[i];
751 /* alignedWidth has extra padding for this one line. */
752 accum += bloomRow[i+pointSize-1];
755 accumRow[i] += accum;
760 PIXEL0 *accumRow1 = A->blurFac ? motionBlurRow : accumRow;
761 PIXEL0 blurFac = A->blurFac;
764 /* TODO: Do I vectorize OK? */
765 if (blurFac == 0x8000) {
766 /* Optimization for the default. */
768 motionBlurRow[i] = (motionBlurRow[i] >> 1) + accumRow[i];
771 motionBlurRow[i] = (PIXEL0)(((PIXEL0X)motionBlurRow[i] * blurFac) >> 16) + accumRow[i];
778 unsigned col = (accumRow1[i] * colorScale) >> COLOR_BITS;
779 if (col>A->numCols-1) {
782 #ifdef VARY_SPEED_TO_AVOID_BOREDOM
784 if (col<A->numCols-1) /* we don't count maxxed out pixels */
793 colRow[i] = A->cols[col];
796 #ifdef VARY_SPEED_TO_AVOID_BOREDOM
807 if (MI_NPIXELS(mi) < 2)
808 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
810 /*XSetForeground(display, gc, MI_PIXEL(mi, A->Col % MI_NPIXELS(mi)));*/
811 XSetForeground(display, gc, cols[col].pixel);
813 if (A->dbuf != None) {
814 XSetForeground(display, A->dbuf_gc, cols[col].pixel);
815 XDrawPoint(display, A->dbuf, A->dbuf_gc, i, j);
817 XSetForeground(display, gc, cols[col].pixel);
818 XDrawPoint(display, window, gc, i, j);
823 if (A->accImage->bits_per_pixel==32 &&
824 A->accImage->byte_order==LOCAL_BYTE_ORDER) {
826 ((uint32_t *)outRow)[i] = colRow[i];
827 } else if (A->accImage->bits_per_pixel==16 &&
828 A->accImage->byte_order==LOCAL_BYTE_ORDER) {
830 ((uint16_t *)outRow)[i] = colRow[i];
831 } else if (A->accImage->bits_per_pixel==8) {
833 ((uint8_t *)outRow)[i] = colRow[i];
836 XPutPixel (A->accImage, i, j, colRow[i]);
839 outRow = (char *)outRow + A->accImage->bytes_per_line;
845 uint64_t t0 = tick();
848 printf("B %g\t(%d,%d)\t%dx%d\t%d\n", 1000.0 * dt / frq(),
849 xmin, ymin, xmax - xmin, ymax - ymin, pixelCount);
856 T->pixelCount = pixelCount;
860 ramp_color (const XColor *color_in, XColor *color_out, unsigned i, unsigned n)
865 #define LOW_COLOR(c) ((c)*li/FULLBLUE)
866 #define HIGH_COLOR(c) ((65535-(c))*(li-FULLBLUE)/(256-FULLBLUE)+(c))
868 + (255.0-MINBLUE) * log(1.0 + ACC_GAMMA*(float)i/n)
869 / log(1.0 + ACC_GAMMA);
871 color_out->red = LOW_COLOR(color_in->red);
872 color_out->green = LOW_COLOR(color_in->green);
873 color_out->blue = LOW_COLOR(color_in->blue);
875 color_out->red = HIGH_COLOR(color_in->red);
876 color_out->green = HIGH_COLOR(color_in->green);
877 color_out->blue = HIGH_COLOR(color_in->blue);
884 draw_points (Display *display, Drawable d, GC gc, const void *Buf,
888 XDrawPoints(display, d, gc, (XPoint *)Buf, Count, CoordModeOrigin);
890 XFillRectangles(display, d, gc, (XRectangle *)Buf, Count);
894 draw_strange(ModeInfo * mi)
900 Display *display = MI_DISPLAY(mi);
901 Window window = MI_WINDOW(mi);
904 void (*Iterate) (const ATTRACTOR *, PRM, PRM, PRM *, PRM *);
905 PRM xmin, xmax, ymin, ymax;
907 unsigned long Rnd = random();
912 A = &Root[MI_SCREEN(mi)];
917 Iterate = A->Iterate;
919 u = (DBL) (A->Count) / 40000.0;
920 for (j = MAX_PRM - 1; j >= 0; --j)
921 A->Prm[j] = DBL_To_PRM((1.0 - u) * A->Prm1[j] + u * A->Prm2[j]);
923 /* We collect the accumulation of the orbits in the 2d int array field. */
925 init_draw (A, &x, &y, &xmin, &ymin, &xmax, &ymax, &Rnd);
926 recalc_scale (A, xmin, ymin, xmax, ymax, &Lx, &Ly, &cx, &cy);
935 MI_IS_DRAWN(mi) = True;
937 #ifdef useAccumulator
938 if (useAccumulator) {
941 threadpool_run (&A->pool, points_thread);
943 if (A->visualClass == TrueColor) {
944 XColor *src_color = &A->palette[A->Col % MI_NPIXELS(mi)];
946 for (i=0;i<A->numCols;i++) {
948 ramp_color (src_color, &color, i, A->numCols);
950 ((((unsigned long)color.red << 16) >> A->rShift) & A->rMask) |
951 ((((unsigned long)color.green << 16) >> A->gShift) & A->gMask) |
952 ((((unsigned long)color.blue << 16) >> A->bShift) & A->bMask);
955 threadpool_wait (&A->pool);
957 threadpool_run(&A->pool, rasterize_thread);
958 threadpool_wait(&A->pool);
960 for (i=0; i!=A->pool.count; ++i) {
961 THREAD *T = A->threads[i];
970 pixelCount += T->pixelCount;
973 put_xshm_image (display, A->dbuf != None ? A->dbuf : window,
974 A->dbuf != None ? A->dbuf_gc : gc, A->accImage,
975 0, 0, 0, 0, A->accImage->width, A->accImage->height,
978 if (A->dbuf != None) {
979 XCopyArea(display, A->dbuf, window, gc, 0, 0, A->Width, A->Height, 0, 0);
981 #ifdef VARY_SPEED_TO_AVOID_BOREDOM
982 /* Increase the rate of change of the parameters if the attractor has become visually boring. */
983 if ((xmax - xmin < Lx * DBL_To_PRM(.2)) && (ymax - ymin < Ly * DBL_To_PRM(.2))) {
985 } else if (pixelCount>0 && pixelCount<A->Width*A->Height/1000) {
986 A->Speed *= 1.25; /* A->Count = 1000; */
988 A->Speed = 4; /* reset to normal/default */
992 A->Count += A->Speed;
993 if (A->Count >= 1000) {
994 for (i = MAX_PRM - 1; i >= 0; --i)
995 A->Prm1[i] = A->Prm2[i];
1002 for (n = 0; n != A->Max_Pt; ++n) {
1004 (*Iterate) (A, x, y, &xo, &yo);
1005 x1 = (int) (Lx * x) + cx;
1006 y1 = (int) (Ly * y) + cy;
1007 if (pointSize == 1) {
1008 XPoint *Buf = &((XPoint *)A->Buffer2)[n];
1012 XRectangle *Buf = &((XRectangle *)A->Buffer2)[n];
1013 /* Position matches bloom in accumulator mode. */
1014 Buf->x = x1 - pointSize + 1;
1016 Buf->width = pointSize;
1017 Buf->height = pointSize;
1030 /* (void) fprintf( stderr, "X,Y: %d %d ", Buf->x, Buf->y ); */
1031 x = xo + NRAND(8) - 4;
1032 y = yo + NRAND(8) - 4;
1035 XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
1037 if (A->dbuf != None) { /* jwz */
1038 XSetForeground(display, A->dbuf_gc, 0);
1039 /* XDrawPoints(display, A->dbuf, A->dbuf_gc, A->Buffer1,
1040 Cur_Pt,CoordModeOrigin); */
1041 XFillRectangle(display, A->dbuf, A->dbuf_gc, 0, 0, A->Width, A->Height);
1043 draw_points(display, window, gc, A->Buffer1, Cur_Pt);
1046 if (MI_NPIXELS(mi) <= 2)
1047 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
1049 XSetForeground(display, gc, MI_PIXEL(mi, A->Col % MI_NPIXELS(mi)));
1051 if (A->dbuf != None) {
1052 XSetForeground(display, A->dbuf_gc, 1);
1053 draw_points(display, A->dbuf, A->dbuf_gc, A->Buffer2, A->Cur_Pt);
1054 XCopyPlane(display, A->dbuf, window, gc, 0, 0, A->Width, A->Height, 0, 0, 1);
1056 draw_points(display, window, gc, A->Buffer2, A->Cur_Pt);
1058 #ifdef useAccumulator
1063 A->Buffer1 = A->Buffer2;
1066 if ((xmax - xmin < Lx * DBL_To_PRM(.2)) && (ymax - ymin < Ly * DBL_To_PRM(.2)))
1067 A->Count += 4 * A->Speed;
1069 A->Count += A->Speed;
1070 if (A->Count >= 1000) {
1071 for (i = MAX_PRM - 1; i >= 0; --i)
1072 A->Prm1[i] = A->Prm2[i];
1073 Random_Prm(A->Prm2);
1078 mi->recursion_depth = A->Count;
1083 /***************************************************************/
1086 init_strange(ModeInfo * mi)
1088 Display *display = MI_DISPLAY(mi);
1092 ATTRACTOR *Attractor;
1093 size_t pointStructSize;
1095 if (MI_WIDTH(mi) > 2560 || MI_HEIGHT(mi) > 2560)
1096 pointSize *= 3; /* Retina displays */
1099 pointSize == 1 ? sizeof (XPoint) : sizeof (XRectangle);
1101 if (curve <= 0) curve = 10;
1104 Attractor = &Root[MI_SCREEN(mi)];
1106 if (Attractor->Fold == NULL) {
1109 if ((Attractor->Fold = (PRM *) calloc(UNIT2 + 1,
1110 sizeof (PRM))) == NULL) {
1114 for (i = 0; i <= UNIT2; ++i) {
1117 /* x = ( DBL )(i)/UNIT2; */
1118 /* x = sin( M_PI/2.0*x ); */
1119 /* x = sqrt( x ); */
1121 /* x = x*(1.0-x)*4.0; */
1122 x = (DBL) (i) / UNIT;
1124 Attractor->Fold[i] = DBL_To_PRM(x);
1128 Attractor->Max_Pt = points;
1130 if (Attractor->Buffer1 == NULL)
1131 if ((Attractor->Buffer1 = calloc(Attractor->Max_Pt,
1132 pointStructSize)) == NULL) {
1136 if (Attractor->Buffer2 == NULL)
1137 if ((Attractor->Buffer2 = calloc(Attractor->Max_Pt,
1138 pointStructSize)) == NULL) {
1143 Attractor->Width = MI_WIDTH(mi);
1144 Attractor->Height = MI_HEIGHT(mi);
1145 Attractor->Cur_Pt = 0;
1146 Attractor->Count = 0;
1147 Attractor->Col = NRAND(MI_NPIXELS(mi));
1148 Attractor->Speed = 4;
1150 Attractor->Iterate = Funcs[NRAND(2)];
1151 if (curve < 10) /* Avoid boring Iterate_X2. */
1152 Attractor->Iterate = Iterate_X3;
1154 Random_Prm(Attractor->Prm1);
1155 Random_Prm(Attractor->Prm2);
1157 if (Attractor->dbuf != None)
1158 XFreePixmap(display, Attractor->dbuf);
1159 #ifdef useAccumulator
1163 Attractor->dbuf = None;
1169 Attractor->dbuf = XCreatePixmap (display, MI_WINDOW(mi),
1170 Attractor->Width, Attractor->Height,
1171 /* useAccumulator ? MI_DEPTH(mi) : */ 1);
1173 /* Allocation checked */
1174 if (Attractor->dbuf != None) {
1180 gcv.graphics_exposures = False;
1181 #endif /* HAVE_JWXYZ */
1182 gcv.function = GXcopy;
1184 if (Attractor->dbuf_gc != None)
1185 XFreeGC(display, Attractor->dbuf_gc);
1187 if ((Attractor->dbuf_gc = XCreateGC(display, Attractor->dbuf,
1189 GCGraphicsExposures |
1190 #endif /* HAVE_JWXYZ */
1191 GCFunction | GCForeground | GCBackground,
1193 XFreePixmap(display, Attractor->dbuf);
1194 Attractor->dbuf = None;
1196 XFillRectangle(display, Attractor->dbuf, Attractor->dbuf_gc,
1197 0, 0, Attractor->Width, Attractor->Height);
1198 XSetBackground(display, gc, MI_BLACK_PIXEL(mi));
1199 XSetFunction(display, gc, GXcopy);
1205 #ifdef useAccumulator
1207 if (useAccumulator) {
1208 static const struct threadpool_class threadClass = {
1213 Screen *screen = MI_SCREENPTR(mi);
1215 unsigned maxThreads, threadCount;
1216 unsigned bpp = visual_pixmap_depth (screen, MI_VISUAL(mi));
1217 size_t threadAlign1 = 8 * thread_memory_alignment(display) - 1;
1219 if (A->visualClass != TrueColor && A->numCols > 2)
1220 XFreeColors (display, MI_COLORMAP(mi), A->cols, A->numCols, 0);
1223 if (MI_NPIXELS(mi) <= 2) {
1225 A->visualClass = StaticColor;
1227 A->numCols = DEF_NUM_COLS;
1228 A->visualClass = visual_class(screen, MI_VISUAL(mi));
1231 A->cols = calloc (A->numCols,sizeof(*A->cols));
1237 if (A->visualClass == TrueColor) {
1238 /* Rebuilds ramp every frame. No need for XAllocColor. */
1239 /* TODO: This could also include PseudoColor. */
1240 visual_rgb_masks (screen, MI_VISUAL(mi),
1241 &A->rMask, &A->gMask, &A->bMask);
1242 A->rShift = 31 - i_log2 (A->rMask);
1243 A->gShift = 31 - i_log2 (A->gMask);
1244 A->bShift = 31 - i_log2 (A->bMask);
1247 A->palette = malloc(MI_NPIXELS(mi) * sizeof(XColor));
1253 for (i=0;i<MI_NPIXELS(mi);i++)
1254 A->palette[i].pixel = MI_PIXEL(mi,i);
1256 XQueryColors (display, MI_COLORMAP(mi), A->palette, MI_NPIXELS(mi));
1257 } else if (A->numCols == 2) {
1258 A->cols[0] = MI_BLACK_PIXEL (mi);
1259 A->cols[1] = MI_WHITE_PIXEL (mi);
1261 /* No changing colors, unfortunately. */
1264 color.pixel = MI_PIXEL(mi,NRAND(MI_NPIXELS(mi)));
1265 XQueryColor (display, MI_COLORMAP(mi), &color);
1268 for (i=0;i<A->numCols;i++) {
1270 ramp_color (&color, &out_color, i, A->numCols);
1271 if (!XAllocColor (display, MI_COLORMAP(mi), &out_color))
1273 A->cols[i] = out_color.pixel;
1275 if (!XAllocColor(MI_DISPLAY(mi), cmap, &cols[i])) {
1276 if (!XAllocColor(display, cmap, &cols[i])) {
1277 cols[i].pixel = WhitePixel (display, DefaultScreen (display));
1278 cols[i].red = cols[i].green = cols[i].blue = 0xFFFF;
1286 XFreeColors (display, MI_COLORMAP(mi), A->cols, i, 0);
1287 A->numCols = A->numCols * 11 / 12;
1288 if (A->numCols < 2) {
1297 /* Add slack for horizontal blur, then round up to the platform's SIMD
1301 (((A->Width + pointSize) * sizeof(PIXEL0) + (__BIGGEST_ALIGNMENT__ - 1)) &
1302 ~(__BIGGEST_ALIGNMENT__ - 1)) / sizeof(PIXEL0);
1305 destroy_xshm_image (display, A->accImage, &A->shmInfo);
1306 A->accImage = create_xshm_image(display, MI_VISUAL(mi),
1307 MI_DEPTH(mi), ZPixmap, &A->shmInfo,
1308 ((A->alignedWidth * bpp + threadAlign1) & ~threadAlign1) / bpp,
1311 A->blurFac = (PIXEL0)(65536 * (motionBlur - 1) / (motionBlur + 1));
1312 A->colorFac = 2 / (motionBlur + 1);
1314 /* Don't overdose on threads. */
1315 threadCount = hardware_concurrency (display);
1316 maxThreads = A->Height / (pointSize * 4);
1319 if (threadCount > maxThreads)
1320 threadCount = maxThreads;
1324 A->threads = malloc (threadCount * sizeof(*A->threads));
1331 threadpool_destroy (&A->pool);
1332 if (threadpool_create (&A->pool, &threadClass, display, threadCount)) {
1342 /* Do not want any exposure events from XCopyPlane */
1343 XSetGraphicsExposures(display, MI_GC(mi), False);
1346 /***************************************************************/
1349 XSCREENSAVER_MODULE ("Strange", strange)
1352 #endif /* MODE_strange */