1 /* -*- Mode: C; tab-width: 4 -*-
2 * swirl --- swirly color-cycling patterns.
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)swirl.c 4.00 97/01/01 xlockmore";
8 /* Copyright (c) 1994 M.Dobie <mrd@ecs.soton.ac.uk>
10 * Permission to use, copy, modify, and distribute this software and its
11 * documentation for any purpose and without fee is hereby granted,
12 * provided that the above copyright notice appear in all copies and that
13 * both that copyright notice and this permission notice appear in
14 * supporting documentation.
16 * This file is provided AS IS with no warranties of any kind. The author
17 * shall have no liability with respect to the infringement of copyrights,
18 * trade secrets or any patents by this file or any part thereof. In no
19 * event will the author be liable for any lost revenue or profits or
20 * other special, indirect and consequential damages.
22 * 13-May-97: jwz@jwz.org: turned into a standalone program.
23 * 21-Apr-95: improved startup time for TrueColour displays
24 * (limited to 16bpp to save memory) S.Early <sde1000@cam.ac.uk>
25 * 09-Jan-95: fixed colour maps (more colourful) and the image now spirals
26 * outwards from the centre with a fixed number of points drawn
27 * every iteration. Thanks to M.Dobie <mrd@ecs.soton.ac.uk>.
28 * 1994: written. Copyright (c) 1994 M.Dobie <mrd@ecs.soton.ac.uk>
29 * based on original code by R.Taylor
33 # define PROGCLASS "Swirl"
34 # define HACK_INIT init_swirl
35 # define HACK_DRAW draw_swirl
36 # define swirl_opts xlockmore_opts
37 # define DEFAULTS "*count: 5 \n" \
41 # define SMOOTH_COLORS
42 # define WRITABLE_COLORS
43 # include "xlockmore.h" /* from the xscreensaver distribution */
44 # include <X11/Xutil.h>
45 # ifdef HAVE_XSHM_EXTENSION
47 # endif /* HAVE_XSHM_EXTENSION */
48 #else /* !STANDALONE */
49 # include "xlock.h" /* from the xlockmore distribution */
50 # undef HAVE_XSHM_EXTENSION
51 #endif /* !STANDALONE */
53 ModeSpecOpt swirl_opts = {
54 0, NULL, 0, NULL, NULL };
58 /****************************************************************/
60 #define MASS 4 /* maximum mass of a knot */
61 #define MIN_RES 5 /* minimim resolution (>= MIN_RES) */
62 #define MAX_RES 1 /* maximum resolution (>0) */
63 #define TWO_PLANE_PCNT 30 /* probability for two plane mode (0-100) */
64 #define RESTART 2500 /* number of cycles before restart */
65 #define BATCH_DRAW 100 /* points to draw per iteration */
80 int x, y; /* position */
82 KNOT_T t; /* type in the first (or only) plane */
83 KNOT_T T; /* type in second plane if there is one */
84 int M; /* mass in second plane if there is one */
87 /* a colour specification */
88 typedef struct Colour {
89 unsigned short r, g, b;
92 /* drawing direction */
94 DRAW_RIGHT, DRAW_DOWN, DRAW_LEFT, DRAW_UP
97 /****************************************************************/
99 /* data associated with a swirl window */
100 typedef struct swirl_data {
101 /* window paramaters */
102 Window win; /* the window */
103 int width, height; /* window size */
104 int depth; /* depth */
105 int rdepth; /* real depth (for XImage) */
106 Visual *visual; /* visual */
108 /* swirl drawing parameters */
109 int n_knots; /* number of knots */
110 KNOT_P knots; /* knot details */
111 KNOT_T knot_type; /* general type of knots */
112 int resolution; /* drawing resolution, 1..5 */
113 int max_resolution; /* maximum resolution, MAX_RES */
114 int r; /* pixel step */
115 Bool two_plane; /* two plane mode? */
116 Bool first_plane; /* doing first plane? */
117 int start_again; /* when to restart */
119 /* spiral drawing parameters */
120 int x, y; /* current point */
121 DIR_T direction; /* current direction */
122 int dir_todo, dir_done; /* how many points in current direction? */
123 int batch_todo, batch_done; /* how many points in this batch */
124 Bool started, drawing; /* are we drawing? */
127 unsigned char *image; /* image data */
131 int colours; /* how many colours possible */
132 int dcolours; /* how many colours for shading */
134 Bool fixed_colourmap; /* fixed colourmap? */
135 #endif /* !STANDALONE */
136 Bool monochrome; /* monochrome? */
137 Colormap cmap; /* colour map for the window */
138 XColor *rgb_values; /* colour definitions array */
140 int current_map; /* current colour map, 0..dcolours-1 */
141 unsigned long fg, bg, white, black; /* black and white pixel values */
142 int shift; /* colourmap shift */
143 int dshift; /* colourmap shift while drawing */
144 XColor fgcol, bgcol; /* foreground and background colour specs */
145 #endif /* !STANDALONE */
149 #define SWIRLCOLOURS 13
153 static COLOUR basic_colours[SWIRLCOLOURS];
154 #endif /* !STANDALONE */
156 /* an array of swirls for each screen */
157 static SWIRL_P swirls = NULL;
162 Return a random integer between 0 and n inclusive
164 - n is the maximum number
166 Returns a random integer */
169 random_no(unsigned int n)
171 return ((int) ((n + 1) * (double) LRAND() / MAXRAND));
174 /****************************************************************/
179 Initialise all the swirl data
181 - swirl is the swirl data */
184 initialise_swirl(ModeInfo * mi, SWIRL_P swirl)
187 Display *display = MI_DISPLAY(mi);
188 #endif /* !STANDALONE */
190 swirl->width = 0; /* width and height of window */
194 swirl->visual = NULL;
195 swirl->resolution = MIN_RES + 1; /* current resolution */
196 swirl->max_resolution = MAX_RES; /* maximum resolution */
197 swirl->n_knots = 0; /* number of knots */
198 swirl->knot_type = ALL; /* general type of knots */
199 swirl->two_plane = False; /* two plane mode? */
200 swirl->first_plane = False; /* doing first plane? */
201 swirl->start_again = -1; /* restart counter */
203 /* drawing parameters */
206 swirl->started = False;
207 swirl->drawing = False;
210 swirl->image = NULL; /* image data */
211 swirl->ximage = NULL;
214 swirl->colours = 0; /* how many colours possible */
215 swirl->dcolours = 0; /* how many colours for shading */
216 swirl->cmap = (Colormap) NULL;
217 swirl->rgb_values = NULL; /* colour definitions array */
219 swirl->current_map = 0; /* current colour map, 0..dcolours-1 */
221 /* set up fg fb colour specs */
222 swirl->white = MI_WIN_WHITE_PIXEL(mi);
223 swirl->black = MI_WIN_BLACK_PIXEL(mi);
224 #endif /* !STANDALONE */
228 # define MI_COLORMAP MI_WIN_COLORMAP
229 #else /* !STANDALONE */
230 swirl->fg = MI_FG_COLOR(mi);
231 swirl->bg = MI_BG_COLOR(mi);
232 swirl->fgcol.pixel = swirl->fg;
233 swirl->bgcol.pixel = swirl->bg;
234 XQueryColor(display, MI_COLORMAP(mi), &(swirl->fgcol));
235 XQueryColor(display, MI_COLORMAP(mi), &(swirl->bgcol));
236 #endif /* !STANDALONE */
239 /****************************************************************/
244 * Initialise the image for drawing to
246 * - swirl is the swirl data
249 initialise_image(ModeInfo * mi, SWIRL_P swirl)
251 Display *dpy = MI_DISPLAY(mi);
253 if (swirl->ximage != NULL)
254 XDestroyImage(swirl->ximage);
257 #ifdef HAVE_XSHM_EXTENSION
260 swirl->ximage = create_xshm_image(dpy, swirl->visual, swirl->rdepth,
261 ZPixmap, 0, &mi->shm_info,
262 swirl->width, swirl->height);
266 #endif /* HAVE_XSHM_EXTENSION */
270 swirl->ximage = XCreateImage(dpy, swirl->visual, swirl->rdepth, ZPixmap,
271 0, 0, swirl->width, swirl->height,
273 swirl->ximage->data = swirl->image =
274 (char *) calloc(swirl->height, swirl->ximage->bytes_per_line);
278 /****************************************************************/
284 * Initialise the list of colours from which the colourmaps are derived
286 * - colours is the array to initialise
287 * - saturation is the saturation value to use 0->grey,
288 * 1.0->full saturation
291 initialise_colours(COLOUR * colours, float saturate)
295 /* start off fully saturated, medium and bright colours */
296 colours[0].r = 0xA000;
297 colours[0].g = 0x0000;
298 colours[0].b = 0x0000;
299 colours[1].r = 0xD000;
300 colours[1].g = 0x0000;
301 colours[1].b = 0x0000;
302 colours[2].r = 0x0000;
303 colours[2].g = 0x6000;
304 colours[2].b = 0x0000;
305 colours[3].r = 0x0000;
306 colours[3].g = 0x9000;
307 colours[3].b = 0x0000;
308 colours[4].r = 0x0000;
309 colours[4].g = 0x0000;
310 colours[4].b = 0xC000;
311 colours[5].r = 0x0000;
312 colours[5].g = 0x0000;
313 colours[5].b = 0xF000;
314 colours[6].r = 0xA000;
315 colours[6].g = 0x6000;
316 colours[6].b = 0x0000;
317 colours[7].r = 0xD000;
318 colours[7].g = 0x9000;
319 colours[7].b = 0x0000;
320 colours[8].r = 0xA000;
321 colours[8].g = 0x0000;
322 colours[8].b = 0xC000;
323 colours[9].r = 0xD000;
324 colours[9].g = 0x0000;
325 colours[9].b = 0xF000;
326 colours[10].r = 0x0000;
327 colours[10].g = 0x6000;
328 colours[10].b = 0xC000;
329 colours[11].r = 0x0000;
330 colours[11].g = 0x9000;
331 colours[11].b = 0xF000;
332 colours[12].r = 0xA000;
333 colours[12].g = 0xA000;
334 colours[12].b = 0xA000;
336 /* add white for low saturation */
337 for (i = 0; i < SWIRLCOLOURS - 1; i++) {
338 unsigned short max_rg, max;
340 /* what is the max intensity for this colour? */
341 max_rg = (colours[i].r > colours[i].g) ? colours[i].r : colours[i].g;
342 max = (max_rg > colours[i].b) ? max_rg : colours[i].b;
344 /* bring elements up to max as saturation approaches 0.0 */
345 colours[i].r += (unsigned short) ((float) (1.0 - saturate) *
346 ((float) max - colours[i].r));
347 colours[i].g += (unsigned short) ((float) (1.0 - saturate) *
348 ((float) max - colours[i].g));
349 colours[i].b += (unsigned short) ((float) (1.0 - saturate) *
350 ((float) max - colours[i].b));
353 #endif /* !STANDALONE */
355 /****************************************************************/
359 * set_black_and_white
361 * Set the entries for foreground & background pixels and
362 * WhitePixel & BlackPixel in an array of colour specifications.
364 * - swirl is the swirl data
365 * - values is the array of specifications
368 set_black_and_white(SWIRL_P swirl, XColor * values)
370 unsigned long white, black;
372 /* where is black and white? */
373 white = swirl->white;
374 black = swirl->black;
376 /* set black and white up */
377 values[white].flags = DoRed | DoGreen | DoBlue;
378 values[white].pixel = white;
379 values[white].red = 0xFFFF;
380 values[white].green = 0xFFFF;
381 values[white].blue = 0xFFFF;
382 values[black].flags = DoRed | DoGreen | DoBlue;
383 values[black].pixel = black;
384 values[black].red = 0;
385 values[black].green = 0;
386 values[black].blue = 0;
388 /* copy the colour specs from the original entries */
389 values[swirl->fg] = swirl->fgcol;
390 values[swirl->bg] = swirl->bgcol;
393 /****************************************************************/
398 * Set an entry in an array of XColor specifications. The given entry will be
399 * set to the given colour. If the entry corresponds to the foreground,
400 * background, WhitePixel, or BlackPixel it is ignored and the given colour
401 * is is put in the next entry.
403 * Therefore, the given colour may be placed up to four places after the
404 * specified entry in the array, if foreground, background, white, or black
407 * - swirl is the swirl data
408 * - value points to a pointer to the array entry. It gets updated to
409 * point to the next free entry.
410 * - pixel points to the current pixel number. It gets updated.
411 * - c points to the colour to add
414 set_colour(SWIRL_P swirl, XColor ** value, unsigned long *pixel, COLOUR_P c)
417 unsigned long fg, bg, white, black;
419 /* where are foreground, background, white, and black? */
422 white = swirl->white;
423 black = swirl->black;
425 /* haven't set it yet */
428 /* try and set the colour */
430 (**value).flags = DoRed | DoGreen | DoBlue;
431 (**value).pixel = *pixel;
433 /* white, black, fg, bg, or a colour? */
434 if ((*pixel != fg) && (*pixel != bg) &&
435 (*pixel != white) && (*pixel != black)) {
436 (**value).red = c->r;
437 (**value).green = c->g;
438 (**value).blue = c->b;
440 /* now we've done it */
449 /****************************************************************/
454 * Get an entry from an array of XColor specifications. The next colour from
455 * the array will be returned. Foreground, background, WhitePixel, or
456 * BlackPixel will be ignored.
458 * - swirl is the swirl data
459 * - value points the array entry. It is updated to point to the entry
460 * following the one returned.
461 * - c is set to the colour found
464 get_colour(SWIRL_P swirl, XColor ** value, COLOUR_P c)
467 unsigned long fg, bg, white, black;
469 /* where is white and black? */
472 white = swirl->white;
473 black = swirl->black;
475 /* haven't set it yet */
478 /* try and set the colour */
480 /* black, white or a colour? */
481 if (((*value)->pixel != fg) && ((*value)->pixel != bg) &&
482 ((*value)->pixel != white) && ((*value)->pixel != black)) {
483 c->r = (*value)->red;
484 c->g = (*value)->green;
485 c->b = (*value)->blue;
487 /* now we've done it */
494 #endif /* !STANDALONE */
496 /****************************************************************/
502 * Generate n colours between c1 and c2. n XColors at *value are set up with
503 * ascending pixel values.
505 * If the pixel range includes BlackPixel or WhitePixel they are set to black
506 * and white respectively but otherwise ignored. Therefore, up to n+2 colours
507 * may actually be set by this function.
509 * - swirl is the swirl data
510 * - values points a pointer to an array of XColors to update
511 * - pixel points to the pixel number to start at
512 * - k n is the number of colours to generate
513 * - c1, c2 are the colours to interpolate between
516 interpolate(SWIRL_P swirl, XColor ** values, unsigned long *pixel, int n, COLOUR_P c1, COLOUR_P c2)
525 for (i = 0; i < n / 2 && (int) *pixel < swirl->colours; i++) {
526 /* work out the colour */
527 r = c1->r + 2 * i * ((int) c2->r) / n;
528 c.r = (r > (int) maxv) ? maxv : r;
529 g = c1->g + 2 * i * ((int) c2->g) / n;
530 c.g = (g > (int) maxv) ? maxv : g;
531 b = c1->b + 2 * i * ((int) c2->b) / n;
532 c.b = (b > (int) maxv) ? maxv : b;
535 set_colour(swirl, values, pixel, &c);
537 for (i = n / 2; i >= 0 && (int) *pixel < swirl->colours; i--) {
538 r = c2->r + 2 * i * ((int) c1->r) / n;
539 c.r = (r > (int) maxv) ? maxv : r;
540 g = c2->g + 2 * i * ((int) c1->g) / n;
541 c.g = (g > (int) maxv) ? maxv : g;
542 b = c2->b + 2 * i * ((int) c1->b) / n;
543 c.b = (b > (int) maxv) ? maxv : b;
546 set_colour(swirl, values, pixel, &c);
550 /****************************************************************/
555 * Generate a `random' closed loop colourmap that occupies the whole colour
558 * - swirl is the swirl data
559 * - values is the array of colour definitions to set up
562 basic_map(SWIRL_P swirl, XColor * values)
566 unsigned short r1, g1, b1, r2, g2, b2, r3, g3, b3;
571 /* start at the beginning of the colour map */
575 /* choose 3 different basic colours at random */
576 for (i = 0; i < 3;) {
580 /* choose colour i */
581 c[i] = basic_colours[random_no(SWIRLCOLOURS - 1)];
583 /* assume different */
586 /* different from the rest? */
587 for (j = 0; j < i; j++)
588 if ((c[i].r == c[j].r) &&
589 (c[i].g == c[j].g) &&
593 /* ready for the next colour? */
598 /* extract components into variables */
609 /* work out the lengths of each side of the triangle */
610 L1 = (int) sqrt((((double) r1 - r2) * ((double) r1 - r2) +
611 ((double) g1 - g2) * ((double) g1 - g2) +
612 ((double) b1 - b2) * ((double) b1 - b2)));
614 L2 = (int) sqrt((((double) r3 - r2) * ((double) r3 - r2) +
615 ((double) g3 - g2) * ((double) g3 - g2) +
616 ((double) b3 - b2) * ((double) b3 - b2)));
618 L3 = (int) sqrt((((double) r1 - r3) * ((double) r1 - r3) +
619 ((double) g1 - g3) * ((double) g1 - g3) +
620 ((double) b1 - b3) * ((double) b1 - b3)));
624 /* allocate colours in proportion to the lengths of the sides */
625 interpolate(swirl, &value, &pixel,
626 (int) ((double) swirl->dcolours * ((double) L1 / (double) L)) + 1, c, c + 1);
627 interpolate(swirl, &value, &pixel,
628 (int) ((double) swirl->dcolours * ((double) L2 / (double) L)) + 1, c + 1, c + 2);
629 interpolate(swirl, &value, &pixel,
630 (int) ((double) swirl->dcolours * ((double) L3 / (double) L)) + 1, c + 2, c);
632 /* fill up any remaining slots (due to rounding) */
633 while ((int) pixel < swirl->colours) {
634 /* repeat the last colour */
635 set_colour(swirl, &value, &pixel, c);
638 /* ensure black and white are correct */
639 if (!swirl->fixed_colourmap)
640 set_black_and_white(swirl, values);
643 /****************************************************************/
648 * Generate pre-rotated versions of the colour specifications
650 * - swirl is the swirl data
651 * - values is an array of colour specifications
654 pre_rotate(SWIRL_P swirl, XColor * values)
661 /* how many colours to display? */
662 dcolours = swirl->dcolours;
664 /* start at the first map */
666 dest = values + swirl->colours;
668 /* generate dcolours-1 rotated maps */
669 for (i = 0; i < dcolours - 1; i++) {
672 /* start at the first pixel */
675 /* remember the first one and skip it */
676 get_colour(swirl, &src, &first);
678 /* put a rotated version of src at dest */
679 for (j = 0; j < dcolours - 1; j++) {
682 /* get the source colour */
683 get_colour(swirl, &src, &c);
686 set_colour(swirl, &dest, &pixel, &c);
689 /* put the first one at the end */
690 set_colour(swirl, &dest, &pixel, &first);
692 /* NB: src and dest should now be ready for the next table */
694 /* ensure black and white are properly set */
695 set_black_and_white(swirl, src);
699 /****************************************************************/
704 * Create a read/write colourmap to use
706 * - swirl is the swirl data
710 create_colourmap(ModeInfo * mi, SWIRL_P swirl)
712 Display *display = MI_DISPLAY(mi);
717 unsigned long redmask, greenmask, bluemask;
719 swirl->fixed_colourmap = !setupColormap(mi, &(swirl->colours),
720 &truecolor, &redmask, &greenmask, &bluemask);
721 preserve = preserveColors(swirl->fg, swirl->bg, swirl->white, swirl->black);
723 /* how many colours should we animate? */
724 swirl->dcolours = (swirl->colours > preserve + 1) ?
725 swirl->colours - preserve : swirl->colours;
727 if (MI_NPIXELS(mi) < 2)
730 /* how fast to shift the colourmap? */
731 swirl->shift = (swirl->colours > 64) ? swirl->colours / 64 : 1;
732 swirl->dshift = (swirl->shift > 1) ? swirl->shift * 2 : 1;
734 /* how may colour map rotations are there? */
735 n_rotations = (swirl->fixed_colourmap) ? 1 : swirl->dcolours;
737 /* allocate space for colour definitions (if not already there) */
738 if (swirl->rgb_values == NULL) {
739 swirl->rgb_values = (XColor *) calloc((swirl->colours + 3) * n_rotations,
742 /* create a colour map */
743 if (!swirl->fixed_colourmap)
745 XCreateColormap(display, swirl->win, swirl->visual, AllocAll);
747 /* select a set of colours for the colour map */
748 basic_map(swirl, swirl->rgb_values);
750 /* are we rotating them? */
751 if (!swirl->fixed_colourmap) {
752 /* generate rotations of the colour maps */
753 pre_rotate(swirl, swirl->rgb_values);
755 /* store the colours in the colour map */
756 XStoreColors(display, swirl->cmap, swirl->rgb_values, swirl->colours);
763 for (i = 0; (int) t > 0; i++, t >>= 1);
766 for (i = 0; (int) t > 0; i++, t >>= 1);
769 for (i = 0; (int) t > 0; i++, t >>= 1);
771 for (i = 0; i < swirl->colours; i++)
772 swirl->rgb_values[i].pixel =
773 ((rsh > 0 ? (swirl->rgb_values[i].red) >> rsh :
774 (swirl->rgb_values[i].red) << (-rsh)) & redmask) |
775 ((gsh > 0 ? (swirl->rgb_values[i].green) >> gsh :
776 (swirl->rgb_values[i].green) << (-gsh)) & greenmask) |
777 ((bsh > 0 ? (swirl->rgb_values[i].blue) >> bsh :
778 (swirl->rgb_values[i].blue) << (-bsh)) & bluemask);
780 /* lookup the colours in the fixed colour map */
781 for (i = 0; i < swirl->colours; i++)
782 (void) XAllocColor(display, MI_COLORMAP(mi),
783 &(swirl->rgb_values[i]));
788 /****************************************************************/
793 * Install a new set of colours into the colour map
795 * - dpy is the display
796 * - swirl is the swirl data
797 * - shift is the amount to rotate the colour map by
800 install_map(Display * dpy, SWIRL_P swirl, int shift)
802 if (!swirl->fixed_colourmap) {
803 /* shift the colour map */
804 swirl->current_map = (swirl->current_map + shift) %
808 XStoreColors(dpy, swirl->cmap,
810 swirl->current_map * swirl->colours,
814 #endif /* !STANDALONE */
816 /****************************************************************/
821 * Initialise the array of knot
823 * swirl is the swirl data
826 create_knots(SWIRL_P swirl)
829 Bool orbit, wheel, picasso, ray, hook;
832 /* create array for knots */
834 (void) free((void *) swirl->knots);
835 swirl->knots = (KNOT_P) calloc(swirl->n_knots, sizeof (KNOT));
838 orbit = wheel = picasso = ray = hook = False;
840 /* what types do we have? */
841 if ((int) swirl->knot_type & (int) ALL) {
842 orbit = wheel = ray = hook = True;
844 if ((int) swirl->knot_type & (int) ORBIT)
846 if ((int) swirl->knot_type & (int) WHEEL)
848 if ((int) swirl->knot_type & (int) PICASSO)
850 if ((int) swirl->knot_type & (int) RAY)
852 if ((int) swirl->knot_type & (int) HOOK)
856 /* initialise each knot */
858 for (k = 0; k < swirl->n_knots; k++) {
860 knot->x = random_no((unsigned int) swirl->width);
861 knot->y = random_no((unsigned int) swirl->height);
864 knot->m = random_no(MASS) + 1;
866 /* can be negative */
867 if (random_no(100) > 50)
872 while (knot->t == NONE) {
873 /* choose a random one from the types available */
874 switch (random_no(4)) {
898 /* if two planes, do same for second plane */
899 if (swirl->two_plane) {
901 while (knot->T == NONE || knot->T == knot->t) {
902 /* choose a different type */
903 switch (random_no(4)) {
932 /****************************************************************/
937 * Work out the pixel value at i, j. Ensure it does not clash with BlackPixel
940 * - swirl is the swirl data
941 * - i, j is the point to calculate
943 * Returns the value of the point
946 do_point(SWIRL_P swirl, int i, int j)
948 int tT, k, value, add;
949 double dx, dy, theta, dist;
950 int dcolours, qcolours;
954 /* how many colours? */
955 dcolours = swirl->dcolours;
956 qcolours = dcolours / 4;
958 /* colour step round a circle */
959 rads = (double) dcolours / (2.0 * M_PI);
964 /* go through all the knots */
966 for (k = 0; k < swirl->n_knots; k++) {
970 /* in two_plane mode get the appropriate knot type */
971 if (swirl->two_plane)
972 tT = (int) ((swirl->first_plane) ? knot->t : knot->T);
976 /* distance from knot */
977 dist = sqrt(dx * dx + dy * dy);
979 /* nothing to add at first */
982 /* work out the contribution (if close enough) */
986 add = (int) (dcolours / (1.0 + 0.01 * abs(knot->m) * dist));
989 /* Avoid atan2: DOMAIN error message */
990 if (dy == 0.0 && dx == 0.0)
993 theta = (atan2(dy, dx) + M_PI) / M_PI;
995 add = (int) (dcolours * theta +
996 sin(0.1 * knot->m * dist) *
997 qcolours * exp(-0.01 * dist));
999 add = (int) (dcolours * (theta - 1.0) +
1000 sin(0.1 * knot->m * dist) *
1001 qcolours * exp(-0.01 * dist));
1004 add = (int) (dcolours *
1005 fabs(cos(0.002 * knot->m * dist)));
1008 /* Avoid atan2: DOMAIN error message */
1009 if (dy == 0.0 && dx == 0.0)
1012 add = (int) (dcolours * fabs(sin(2.0 * atan2(dy, dx))));
1016 /* Avoid atan2: DOMAIN error message */
1017 if (dy == 0.0 && dx == 0.0)
1018 add = (int) (0.05 * (abs(knot->m) - 1) * dist);
1020 add = (int) (rads * atan2(dy, dx) +
1021 0.05 * (abs(knot->m) - 1) * dist);
1024 /* for a +ve mass add on the contribution else take it off */
1035 swirl->first_plane = (!swirl->first_plane);
1037 /* make sure we handle -ve values properly */
1039 value = (value % dcolours) + 2;
1041 value = dcolours - (abs(value) % (dcolours - 1));
1044 /* if fg and bg are 1 and 0 we should be OK, but just in case */
1045 while ((dcolours > 2) &&
1046 (((value % swirl->colours) == (int) swirl->fg) ||
1047 ((value % swirl->colours) == (int) swirl->bg) ||
1048 ((value % swirl->colours) == (int) swirl->white) ||
1049 ((value % swirl->colours) == (int) swirl->black))) {
1052 #endif /* !STANDALONE */
1054 /* definitely make sure it is in range */
1055 value = value % swirl->colours;
1057 /* lookup the pixel value if necessary */
1059 if (swirl->fixed_colourmap && swirl->dcolours > 2)
1061 value = swirl->rgb_values[value].pixel;
1064 return ((unsigned long) value);
1067 /****************************************************************/
1072 * Draw a square block of points with the same value.
1074 * - ximage is the XImage to draw on.
1075 * - x, y is the top left corner
1076 * - s is the length of each side
1080 draw_block(XImage * ximage, int x, int y, int s, unsigned long v)
1084 for (a = 0; a < s; a++)
1085 for (b = 0; b < s; b++) {
1086 XPutPixel(ximage, x + b, y + a, v);
1090 /****************************************************************/
1093 * draw_point Draw the current point in a swirl pattern onto the XImage
1095 * - swirl is the swirl
1096 * - win is the window to update
1099 draw_point(ModeInfo * mi, SWIRL_P swirl)
1104 /* get current point coordinates and resolution */
1109 /* check we are within the window */
1110 if ((x < 0) || (x > swirl->width - r) || (y < 0) || (y > swirl->height - r))
1113 /* what style are we drawing? */
1114 if (swirl->two_plane) {
1117 /* halve the block size */
1120 /* interleave blocks at half r */
1121 draw_block(swirl->ximage, x, y, r2, do_point(swirl, x, y));
1122 draw_block(swirl->ximage, x + r2, y, r2, do_point(swirl, x + r2, y));
1123 draw_block(swirl->ximage, x + r2, y + r2, r2, do_point(swirl,
1125 draw_block(swirl->ximage, x, y + r2, r2, do_point(swirl, x, y + r2));
1127 draw_block(swirl->ximage, x, y, r, do_point(swirl, x, y));
1129 /* update the screen */
1131 #ifdef HAVE_XSHM_EXTENSION
1133 XShmPutImage(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi), swirl->ximage,
1134 x, y, x, y, r, r, False);
1136 #endif /* !HAVE_XSHM_EXTENSION */
1137 /* PURIFY 4.0.1 on SunOS4 and on Solaris 2 reports a 256 byte memory
1138 leak on the next line. */
1139 XPutImage(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi), swirl->ximage,
1143 /****************************************************************/
1146 * next_point Move to the next point in the spiral pattern
1147 * - swirl is the swirl
1148 * - win is the window to update
1151 next_point(SWIRL_P swirl)
1153 /* more to do in this direction? */
1154 if (swirl->dir_done < swirl->dir_todo) {
1155 /* move in the current direction */
1156 switch (swirl->direction) {
1158 swirl->x += swirl->r;
1161 swirl->y += swirl->r;
1164 swirl->x -= swirl->r;
1167 swirl->y -= swirl->r;
1171 /* done another point */
1174 /* none drawn yet */
1175 swirl->dir_done = 0;
1177 /* change direction - check and record if off screen */
1178 switch (swirl->direction) {
1180 swirl->direction = DRAW_DOWN;
1181 if (swirl->x > swirl->width - swirl->r) {
1182 /* skip these points */
1183 swirl->dir_done = swirl->dir_todo;
1184 swirl->y += (swirl->dir_todo * swirl->r);
1186 /* check for finish */
1187 if (swirl->off_screen)
1188 swirl->drawing = False;
1189 swirl->off_screen = True;
1191 swirl->off_screen = False;
1194 swirl->direction = DRAW_LEFT;
1196 if (swirl->y > swirl->height - swirl->r) {
1197 /* skip these points */
1198 swirl->dir_done = swirl->dir_todo;
1199 swirl->x -= (swirl->dir_todo * swirl->r);
1201 /* check for finish */
1202 if (swirl->off_screen)
1203 swirl->drawing = False;
1204 swirl->off_screen = True;
1206 swirl->off_screen = False;
1209 swirl->direction = DRAW_UP;
1211 /* skip these points */
1212 swirl->dir_done = swirl->dir_todo;
1213 swirl->y -= (swirl->dir_todo * swirl->r);
1215 /* check for finish */
1216 if (swirl->off_screen)
1217 swirl->drawing = False;
1218 swirl->off_screen = True;
1220 swirl->off_screen = False;
1223 swirl->direction = DRAW_RIGHT;
1226 /* skip these points */
1227 swirl->dir_done = swirl->dir_todo;
1228 swirl->x += (swirl->dir_todo * swirl->r);
1230 /* check for finish */
1231 if (swirl->off_screen)
1232 swirl->drawing = False;
1233 swirl->off_screen = True;
1235 swirl->off_screen = False;
1241 /****************************************************************/
1246 * Initialise things for swirling
1248 * - win is the window to draw in
1251 init_swirl(ModeInfo * mi)
1253 Display *display = MI_DISPLAY(mi);
1254 Window window = MI_WINDOW(mi);
1257 /* does the swirls array exist? */
1258 if (swirls == NULL) {
1261 /* allocate an array, one entry for each screen */
1262 swirls = (SWIRL_P) calloc(ScreenCount(display), sizeof (SWIRL));
1264 /* initialise them all */
1265 for (i = 0; i < ScreenCount(display); i++)
1266 initialise_swirl(mi, &swirls[i]);
1268 /* get a pointer to this swirl */
1269 swirl = &(swirls[MI_SCREEN(mi)]);
1271 /* get window parameters */
1272 swirl->win = window;
1273 swirl->width = MI_WIN_WIDTH(mi);
1274 swirl->height = MI_WIN_HEIGHT(mi);
1275 swirl->depth = MI_WIN_DEPTH(mi);
1276 swirl->rdepth = swirl->depth;
1277 swirl->visual = MI_VISUAL(mi);
1279 if (swirl->depth > 16)
1282 /* initialise image for speeding up drawing */
1283 initialise_image(mi, swirl);
1285 /* clear the window (before setting the colourmap) */
1286 XClearWindow(display, MI_WINDOW(mi));
1290 swirl->rgb_values = mi->colors;
1291 swirl->colours = mi->npixels;
1292 swirl->dcolours = swirl->colours;
1293 /* swirl->fixed_colourmap = !mi->writable_p;*/
1295 #else /* !STANDALONE */
1297 /* initialise the colours from which the colourmap is derived */
1298 initialise_colours(basic_colours, MI_SATURATION(mi));
1300 /* set up the colour map */
1301 create_colourmap(mi, swirl);
1303 /* attach the colour map to the window (if we have one) */
1304 if (!swirl->fixed_colourmap) {
1306 setColormap(display, window, swirl->cmap, MI_WIN_IS_INWINDOW(mi));
1308 XSetWindowColormap(display, window, swirl->cmap);
1309 (void) XSetWMColormapWindows(display, window, &window, 1);
1310 XInstallColormap(display, swirl->cmap);
1313 #endif /* STANDALONE */
1315 /* resolution starts off chunky */
1316 swirl->resolution = MIN_RES + 1;
1318 /* calculate the pixel step for this resulution */
1319 swirl->r = (1 << (swirl->resolution - 1));
1321 /* how many knots? */
1322 swirl->n_knots = random_no((unsigned int) MI_BATCHCOUNT(mi) / 2) +
1323 MI_BATCHCOUNT(mi) + 1;
1325 /* what type of knots? */
1326 swirl->knot_type = ALL; /* for now */
1328 /* use two_plane mode occaisionally */
1329 if (random_no(100) <= TWO_PLANE_PCNT) {
1330 swirl->two_plane = swirl->first_plane = True;
1331 swirl->max_resolution = 2;
1333 swirl->two_plane = False;
1335 /* fix the knot values */
1336 create_knots(swirl);
1339 swirl->started = True;
1340 swirl->drawing = False;
1343 /****************************************************************/
1348 * Draw one iteration of swirling
1350 * - win is the window to draw in
1353 draw_swirl(ModeInfo * mi)
1355 SWIRL_P swirl = &(swirls[MI_SCREEN(mi)]);
1358 if (swirl->started) {
1359 /* in the middle of drawing? */
1360 if (swirl->drawing) {
1363 rotate_colors(MI_DISPLAY(mi), MI_COLORMAP(mi),
1364 swirl->rgb_values, swirl->colours, 1);
1365 #else /* !STANDALONE */
1366 /* rotate the colours */
1367 install_map(MI_DISPLAY(mi), swirl, swirl->dshift);
1368 #endif /* !STANDALONE */
1370 /* draw a batch of points */
1371 swirl->batch_todo = BATCH_DRAW;
1372 while ((swirl->batch_todo > 0) && swirl->drawing) {
1374 draw_point(mi, swirl);
1376 /* move to the next point */
1380 swirl->batch_todo--;
1385 rotate_colors(MI_DISPLAY(mi), MI_COLORMAP(mi),
1386 swirl->rgb_values, swirl->colours, 1);
1387 #else /* !STANDALONE */
1388 /* rotate the colours */
1389 install_map(MI_DISPLAY(mi), swirl, swirl->shift);
1390 #endif /* !STANDALONE */
1392 /* time for a higher resolution? */
1393 if (swirl->resolution > swirl->max_resolution) {
1394 /* move to higher resolution */
1395 swirl->resolution--;
1397 /* calculate the pixel step for this resulution */
1398 swirl->r = (1 << (swirl->resolution - 1));
1400 /* start drawing again */
1401 swirl->drawing = True;
1403 /* start in the middle of the screen */
1404 swirl->x = (swirl->width - swirl->r) / 2;
1405 swirl->y = (swirl->height - swirl->r) / 2;
1407 /* initialise spiral drawing parameters */
1408 swirl->direction = DRAW_RIGHT;
1409 swirl->dir_todo = 1;
1410 swirl->dir_done = 0;
1412 /* all done, decide when to restart */
1413 if (swirl->start_again == -1) {
1414 /* start the counter */
1415 swirl->start_again = RESTART;
1416 } else if (swirl->start_again == 0) {
1417 /* reset the counter */
1418 swirl->start_again = -1;
1421 /* Pick a new colormap! */
1422 XClearWindow (MI_DISPLAY(mi), MI_WINDOW(mi));
1423 free_colors (MI_DISPLAY(mi), MI_COLORMAP(mi),
1424 mi->colors, mi->npixels);
1425 make_smooth_colormap (MI_DISPLAY(mi),
1428 mi->colors, &mi->npixels, True,
1429 &mi->writable_p, True);
1430 swirl->colours = mi->npixels;
1431 #endif /* STANDALONE */
1436 /* decrement the counter */
1437 swirl->start_again--;
1443 /****************************************************************/
1446 release_swirl(ModeInfo * mi)
1448 /* does the swirls array exist? */
1449 if (swirls != NULL) {
1453 for (i = 0; i < MI_NUM_SCREENS(mi); i++) {
1454 SWIRL_P swirl = &(swirls[i]);
1456 if (swirl->cmap != (Colormap) NULL)
1457 XFreeColormap(MI_DISPLAY(mi), swirl->cmap);
1458 if (swirl->rgb_values != NULL)
1459 XFree((void *) swirl->rgb_values);
1460 if (swirl->ximage != NULL)
1461 XDestroyImage(swirl->ximage);
1463 (void) free((void *) swirl->knots);
1465 /* deallocate an array, one entry for each screen */
1466 (void) free((void *) swirls);
1471 /****************************************************************/
1474 refresh_swirl(ModeInfo * mi)
1476 SWIRL_P swirl = &(swirls[MI_SCREEN(mi)]);
1478 if (swirl->started) {
1480 swirl->resolution = swirl->resolution + 1;
1481 swirl->drawing = False;