2 * Copyright (c) 1997 by Massimino Pascal <Pascal.Massimon@ens.fr>
4 * ifs.c: modified iterated functions system for goom.
6 * Permission to use, copy, modify, and distribute this software and its
7 * documentation for any purpose and without fee is hereby granted,
8 * provided that the above copyright notice appear in all copies and that
9 * both that copyright notice and this permission notice appear in
10 * supporting documentation.
12 * This file is provided AS IS with no warranties of any kind. The author
13 * shall have no liability with respect to the infringement of copyrights,
14 * trade secrets or any patents by this file or any part thereof. In no
15 * event will the author be liable for any lost revenue or profits or
16 * other special, indirect and consequential damages.
18 * If this mode is weird and you have an old MetroX server, it is buggy.
19 * There is a free SuSE-enhanced MetroX X server that is fine.
21 * When shown ifs, Diana Rose (4 years old) said, "It looks like dancing."
24 * 13-Dec-2003: Added some goom specific stuffs (to make ifs a VisualFX).
25 * 11-Apr-2002: jeko@ios-software.com: Make ifs.c system-indendant. (ifs.h added)
26 * 01-Nov-2000: Allocation checks
27 * 10-May-1997: jwz@jwz.org: turned into a standalone program.
28 * Made it render into an offscreen bitmap and then copy
29 * that onto the screen, to reduce flicker.
32 /* #ifdef STANDALONE */
38 #include "goom_config.h"
44 #include "goom_graphic.h"
46 #include "goom_tools.h"
48 typedef struct _ifsPoint
57 #define PROGCLASS "IFS"
59 #define HACK_INIT init_ifs
60 #define HACK_DRAW draw_ifs
62 #define ifs_opts xlockmore_opts
64 #define DEFAULTS "*delay: 20000 \n" \
69 #define LRAND() ((long) (goom_random(goomInfo->gRandom) & 0x7fffffff))
70 #define NRAND(n) ((int) (LRAND() % (n)))
72 #if RAND_MAX < 0x10000
73 #define MAXRAND (((float)(RAND_MAX<16)+((float)RAND_MAX)+1.0f)/127.0f)
75 #define MAXRAND (2147483648.0/127.0) /* unsigned 1<<31 / 127.0 (cf goom_tools) as a float */
78 /*****************************************************/
83 /* typedef float F_PT; */
85 /*****************************************************/
88 #define UNIT ( 1<<FIX )
91 #define MAX_DEPTH_2 10
99 * settings for a PC 120Mhz... *
100 #define MAX_DEPTH_2 10
101 #define MAX_DEPTH_3 6
102 #define MAX_DEPTH_4 4
103 #define MAX_DEPTH_5 3
106 #define DBL_To_F_PT(x) (F_PT)( (DBL)(UNIT)*(x) )
108 typedef struct Similitude_Struct SIMI;
109 typedef struct Fractal_Struct FRACTAL;
111 struct Similitude_Struct
116 F_PT Ct, St, Ct2, St2;
122 struct Fractal_Struct
126 SIMI Components[5 * MAX_SIMI];
129 int Width, Height, Lx, Ly;
130 DBL r_mean, dr_mean, dr2_mean;
133 IFSPoint *Buffer1, *Buffer2;
136 typedef struct _IFS_DATA
141 /* Used by the Trace recursive method */
148 /*****************************************************/
151 Gauss_Rand (PluginInfo * goomInfo, DBL c, DBL A, DBL S)
155 y = (DBL) LRAND () / MAXRAND;
156 y = A * (1.0 - exp (-y * y * S)) / (1.0 - exp (-S));
163 Half_Gauss_Rand (PluginInfo * goomInfo, DBL c, DBL A, DBL S)
167 y = (DBL) LRAND () / MAXRAND;
168 y = A * (1.0 - exp (-y * y * S)) / (1.0 - exp (-S));
173 Random_Simis (PluginInfo * goomInfo, FRACTAL * F, SIMI * Cur, int i)
176 Cur->c_x = Gauss_Rand (goomInfo, 0.0, .8, 4.0);
177 Cur->c_y = Gauss_Rand (goomInfo, 0.0, .8, 4.0);
178 Cur->r = Gauss_Rand (goomInfo, F->r_mean, F->dr_mean, 3.0);
179 Cur->r2 = Half_Gauss_Rand (goomInfo, 0.0, F->dr2_mean, 2.0);
180 Cur->A = Gauss_Rand (goomInfo, 0.0, 360.0, 4.0) * (G_PI / 180.0);
181 Cur->A2 = Gauss_Rand (goomInfo, 0.0, 360.0, 4.0) * (G_PI / 180.0);
187 free_ifs_buffers (FRACTAL * Fractal)
189 if (Fractal->Buffer1 != NULL) {
190 (void) free ((void *) Fractal->Buffer1);
191 Fractal->Buffer1 = (IFSPoint *) NULL;
193 if (Fractal->Buffer2 != NULL) {
194 (void) free ((void *) Fractal->Buffer2);
195 Fractal->Buffer2 = (IFSPoint *) NULL;
201 free_ifs (FRACTAL * Fractal)
203 free_ifs_buffers (Fractal);
206 /***************************************************************/
209 init_ifs (PluginInfo * goomInfo, IfsData * data)
213 int width = goomInfo->screen.width;
214 int height = goomInfo->screen.height;
216 if (data->Root == NULL) {
217 data->Root = (FRACTAL *) malloc (sizeof (FRACTAL));
218 if (data->Root == NULL)
220 data->Root->Buffer1 = (IFSPoint *) NULL;
221 data->Root->Buffer2 = (IFSPoint *) NULL;
223 Fractal = data->Root;
225 free_ifs_buffers (Fractal);
227 i = (NRAND (4)) + 2; /* Number of centers */
230 Fractal->Depth = MAX_DEPTH_3;
231 Fractal->r_mean = .6;
232 Fractal->dr_mean = .4;
233 Fractal->dr2_mean = .3;
237 Fractal->Depth = MAX_DEPTH_4;
238 Fractal->r_mean = .5;
239 Fractal->dr_mean = .4;
240 Fractal->dr2_mean = .3;
244 Fractal->Depth = MAX_DEPTH_5;
245 Fractal->r_mean = .5;
246 Fractal->dr_mean = .4;
247 Fractal->dr2_mean = .3;
252 Fractal->Depth = MAX_DEPTH_2;
253 Fractal->r_mean = .7;
254 Fractal->dr_mean = .3;
255 Fractal->dr2_mean = .4;
258 Fractal->Nb_Simi = i;
259 Fractal->Max_Pt = Fractal->Nb_Simi - 1;
260 for (i = 0; i <= Fractal->Depth + 2; ++i)
261 Fractal->Max_Pt *= Fractal->Nb_Simi;
263 if ((Fractal->Buffer1 = (IFSPoint *) calloc (Fractal->Max_Pt,
264 sizeof (IFSPoint))) == NULL) {
268 if ((Fractal->Buffer2 = (IFSPoint *) calloc (Fractal->Max_Pt,
269 sizeof (IFSPoint))) == NULL) {
275 Fractal->Width = width; /* modif by JeKo */
276 Fractal->Height = height; /* modif by JeKo */
279 Fractal->Lx = (Fractal->Width - 1) / 2;
280 Fractal->Ly = (Fractal->Height - 1) / 2;
281 Fractal->Col = rand () % (width * height); /* modif by JeKo */
283 Random_Simis (goomInfo, Fractal, Fractal->Components, 5 * MAX_SIMI);
287 /***************************************************************/
290 Transform (SIMI * Simi, F_PT xo, F_PT yo, F_PT * x, F_PT * y)
295 xo = (xo * Simi->R) >> FIX; /* / UNIT; */
297 yo = (yo * Simi->R) >> FIX; /* / UNIT; */
300 xx = (xx * Simi->R2) >> FIX; /* / UNIT; */
302 yy = (yy * Simi->R2) >> FIX; /* / UNIT; */
304 *x = ((xo * Simi->Ct - yo * Simi->St + xx * Simi->Ct2 - yy * Simi->St2)
305 >> FIX /* / UNIT */ ) + Simi->Cx;
306 *y = ((xo * Simi->St + yo * Simi->Ct + xx * Simi->St2 + yy * Simi->Ct2)
307 >> FIX /* / UNIT */ ) + Simi->Cy;
310 /***************************************************************/
313 Trace (FRACTAL * F, F_PT xo, F_PT yo, IfsData * data)
318 Cur = data->Cur_F->Components;
319 for (i = data->Cur_F->Nb_Simi; i; --i, Cur++) {
320 Transform (Cur, xo, yo, &x, &y);
322 data->Buf->x = F->Lx + ((x * F->Lx) >> (FIX + 1) /* /(UNIT*2) */ );
323 data->Buf->y = F->Ly - ((y * F->Ly) >> (FIX + 1) /* /(UNIT*2) */ );
328 if (F->Depth && ((x - xo) >> 4) && ((y - yo) >> 4)) {
330 Trace (F, x, y, data);
337 Draw_Fractal (IfsData * data)
339 FRACTAL *F = data->Root;
344 for (Cur = F->Components, i = F->Nb_Simi; i; --i, Cur++) {
345 Cur->Cx = DBL_To_F_PT (Cur->c_x);
346 Cur->Cy = DBL_To_F_PT (Cur->c_y);
348 Cur->Ct = DBL_To_F_PT (cos (Cur->A));
349 Cur->St = DBL_To_F_PT (sin (Cur->A));
350 Cur->Ct2 = DBL_To_F_PT (cos (Cur->A2));
351 Cur->St2 = DBL_To_F_PT (sin (Cur->A2));
353 Cur->R = DBL_To_F_PT (Cur->r);
354 Cur->R2 = DBL_To_F_PT (Cur->r2);
360 data->Buf = F->Buffer2;
361 for (Cur = F->Components, i = F->Nb_Simi; i; --i, Cur++) {
364 for (Simi = F->Components, j = F->Nb_Simi; j; --j, Simi++) {
367 Transform (Simi, xo, yo, &x, &y);
368 Trace (F, x, y, data);
374 F->Cur_Pt = data->Cur_Pt;
375 data->Buf = F->Buffer1;
376 F->Buffer1 = F->Buffer2;
377 F->Buffer2 = data->Buf;
382 draw_ifs (PluginInfo * goomInfo, int *nbpt, IfsData * data)
385 DBL u, uu, v, vv, u0, u1, u2, u3;
386 SIMI *S, *S1, *S2, *S3, *S4;
389 if (data->Root == NULL)
392 if (F->Buffer1 == NULL)
395 u = (DBL) (F->Count) * (DBL) (F->Speed) / 1000.0;
406 S2 = S1 + F->Nb_Simi;
407 S3 = S2 + F->Nb_Simi;
408 S4 = S3 + F->Nb_Simi;
410 for (i = F->Nb_Simi; i; --i, S++, S1++, S2++, S3++, S4++) {
411 S->c_x = u0 * S1->c_x + u1 * S2->c_x + u2 * S3->c_x + u3 * S4->c_x;
412 S->c_y = u0 * S1->c_y + u1 * S2->c_y + u2 * S3->c_y + u3 * S4->c_y;
413 S->r = u0 * S1->r + u1 * S2->r + u2 * S3->r + u3 * S4->r;
414 S->r2 = u0 * S1->r2 + u1 * S2->r2 + u2 * S3->r2 + u3 * S4->r2;
415 S->A = u0 * S1->A + u1 * S2->A + u2 * S3->A + u3 * S4->A;
416 S->A2 = u0 * S1->A2 + u1 * S2->A2 + u2 * S3->A2 + u3 * S4->A2;
421 if (F->Count >= 1000 / F->Speed) {
424 S2 = S1 + F->Nb_Simi;
425 S3 = S2 + F->Nb_Simi;
426 S4 = S3 + F->Nb_Simi;
428 for (i = F->Nb_Simi; i; --i, S++, S1++, S2++, S3++, S4++) {
429 S2->c_x = 2.0 * S4->c_x - S3->c_x;
430 S2->c_y = 2.0 * S4->c_y - S3->c_y;
431 S2->r = 2.0 * S4->r - S3->r;
432 S2->r2 = 2.0 * S4->r2 - S3->r2;
433 S2->A = 2.0 * S4->A - S3->A;
434 S2->A2 = 2.0 * S4->A2 - S3->A2;
438 Random_Simis (goomInfo, F, F->Components + 3 * F->Nb_Simi, F->Nb_Simi);
440 Random_Simis (goomInfo, F, F->Components + 4 * F->Nb_Simi, F->Nb_Simi);
448 (*nbpt) = data->Cur_Pt;
453 /***************************************************************/
456 release_ifs (IfsData * data)
458 if (data->Root != NULL) {
459 free_ifs (data->Root);
460 (void) free ((void *) data->Root);
461 data->Root = (FRACTAL *) NULL;
465 #define RAND() goom_random(goomInfo->gRandom)
468 ifs_update (PluginInfo * goomInfo, Pixel * data, Pixel * back, int increment,
471 static unsigned int couleur = 0xc0c0c0c0;
472 static int v[4] = { 2, 4, 3, 2 };
473 static int col[4] = { 2, 4, 3, 2 };
478 static int mode = MOD_MERVER;
479 static int justChanged = 0;
480 static int cycle = 0;
487 unsigned int couleursl = couleur;
488 int width = goomInfo->screen.width;
489 int height = goomInfo->screen.height;
496 cycle10 = cycle / 10;
498 cycle10 = 7 - cycle / 10;
501 unsigned char *tmp = (unsigned char *) &couleursl;
503 for (i = 0; i < 4; i++) {
504 *tmp = (*tmp) >> cycle10;
509 points = draw_ifs (goomInfo, &nbpt, fx_data);
513 movd_m2r (couleursl, mm1);
514 punpckldq_r2r (mm1, mm1);
515 for (i = 0; i < nbpt; i += increment) {
519 if ((x < width) && (y < height) && (x > 0) && (y > 0)) {
520 int pos = x + (y * width);
522 movd_m2r (back[pos], mm0);
523 paddusb_r2r (mm1, mm0);
524 movd_r2m (mm0, data[pos]);
527 emms (); /*__asm__ __volatile__ ("emms");*/
529 for (i = 0; i < nbpt; i += increment) {
530 int x = (int) points[i].x & 0x7fffffff;
531 int y = (int) points[i].y & 0x7fffffff;
533 if ((x < width) && (y < height)) {
534 int pos = x + (int) (y * width);
536 unsigned char *bra = (unsigned char *) &back[pos];
537 unsigned char *dra = (unsigned char *) &data[pos];
538 unsigned char *cra = (unsigned char *) &couleursl;
555 col[ALPHA] = couleur >> (ALPHA * 8) & 0xff;
556 col[BLEU] = couleur >> (BLEU * 8) & 0xff;
557 col[VERT] = couleur >> (VERT * 8) & 0xff;
558 col[ROUGE] = couleur >> (ROUGE * 8) & 0xff;
560 if (mode == MOD_MER) {
561 col[BLEU] += v[BLEU];
562 if (col[BLEU] > 255) {
564 v[BLEU] = -(RAND () % 4) - 1;
566 if (col[BLEU] < 32) {
568 v[BLEU] = (RAND () % 4) + 1;
571 col[VERT] += v[VERT];
572 if (col[VERT] > 200) {
574 v[VERT] = -(RAND () % 3) - 2;
576 if (col[VERT] > col[BLEU]) {
577 col[VERT] = col[BLEU];
580 if (col[VERT] < 32) {
582 v[VERT] = (RAND () % 3) + 2;
585 col[ROUGE] += v[ROUGE];
586 if (col[ROUGE] > 64) {
588 v[ROUGE] = -(RAND () % 4) - 1;
590 if (col[ROUGE] < 0) {
592 v[ROUGE] = (RAND () % 4) + 1;
595 col[ALPHA] += v[ALPHA];
596 if (col[ALPHA] > 0) {
598 v[ALPHA] = -(RAND () % 4) - 1;
600 if (col[ALPHA] < 0) {
602 v[ALPHA] = (RAND () % 4) + 1;
605 if (((col[VERT] > 32) && (col[ROUGE] < col[VERT] + 40)
606 && (col[VERT] < col[ROUGE] + 20) && (col[BLEU] < 64)
607 && (RAND () % 20 == 0)) && (justChanged < 0)) {
608 mode = (RAND () % 3) ? MOD_FEU : MOD_MERVER;
611 } else if (mode == MOD_MERVER) {
612 col[BLEU] += v[BLEU];
613 if (col[BLEU] > 128) {
615 v[BLEU] = -(RAND () % 4) - 1;
617 if (col[BLEU] < 16) {
619 v[BLEU] = (RAND () % 4) + 1;
622 col[VERT] += v[VERT];
623 if (col[VERT] > 200) {
625 v[VERT] = -(RAND () % 3) - 2;
627 if (col[VERT] > col[ALPHA]) {
628 col[VERT] = col[ALPHA];
631 if (col[VERT] < 32) {
633 v[VERT] = (RAND () % 3) + 2;
636 col[ROUGE] += v[ROUGE];
637 if (col[ROUGE] > 128) {
639 v[ROUGE] = -(RAND () % 4) - 1;
641 if (col[ROUGE] < 0) {
643 v[ROUGE] = (RAND () % 4) + 1;
646 col[ALPHA] += v[ALPHA];
647 if (col[ALPHA] > 255) {
649 v[ALPHA] = -(RAND () % 4) - 1;
651 if (col[ALPHA] < 0) {
653 v[ALPHA] = (RAND () % 4) + 1;
656 if (((col[VERT] > 32) && (col[ROUGE] < col[VERT] + 40)
657 && (col[VERT] < col[ROUGE] + 20) && (col[BLEU] < 64)
658 && (RAND () % 20 == 0)) && (justChanged < 0)) {
659 mode = (RAND () % 3) ? MOD_FEU : MOD_MER;
662 } else if (mode == MOD_FEU) {
664 col[BLEU] += v[BLEU];
665 if (col[BLEU] > 64) {
667 v[BLEU] = -(RAND () % 4) - 1;
671 v[BLEU] = (RAND () % 4) + 1;
674 col[VERT] += v[VERT];
675 if (col[VERT] > 200) {
677 v[VERT] = -(RAND () % 3) - 2;
679 if (col[VERT] > col[ROUGE] + 20) {
680 col[VERT] = col[ROUGE] + 20;
681 v[VERT] = -(RAND () % 3) - 2;
682 v[ROUGE] = (RAND () % 4) + 1;
683 v[BLEU] = (RAND () % 4) + 1;
687 v[VERT] = (RAND () % 3) + 2;
690 col[ROUGE] += v[ROUGE];
691 if (col[ROUGE] > 255) {
693 v[ROUGE] = -(RAND () % 4) - 1;
695 if (col[ROUGE] > col[VERT] + 40) {
696 col[ROUGE] = col[VERT] + 40;
697 v[ROUGE] = -(RAND () % 4) - 1;
699 if (col[ROUGE] < 0) {
701 v[ROUGE] = (RAND () % 4) + 1;
704 col[ALPHA] += v[ALPHA];
705 if (col[ALPHA] > 0) {
707 v[ALPHA] = -(RAND () % 4) - 1;
709 if (col[ALPHA] < 0) {
711 v[ALPHA] = (RAND () % 4) + 1;
714 if (((col[ROUGE] < 64) && (col[VERT] > 32) && (col[VERT] < col[BLEU])
716 && (RAND () % 20 == 0)) && (justChanged < 0)) {
717 mode = (RAND () % 2) ? MOD_MER : MOD_MERVER;
722 couleur = (col[ALPHA] << (ALPHA * 8))
723 | (col[BLEU] << (BLEU * 8))
724 | (col[VERT] << (VERT * 8))
725 | (col[ROUGE] << (ROUGE * 8));
728 /** VISUAL_FX WRAPPER FOR IFS */
731 ifs_vfx_apply (VisualFX * _this, Pixel * src, Pixel * dest,
732 PluginInfo * goomInfo)
735 IfsData *data = (IfsData *) _this->fx_data;
737 if (!data->initalized) {
738 data->initalized = 1;
739 init_ifs (goomInfo, data);
741 ifs_update (goomInfo, dest, src, goomInfo->update.ifs_incr, data);
742 /*TODO: trouver meilleur soluce pour increment (mettre le code de gestion de l'ifs dans ce fichier: ifs_vfx_apply) */
746 ifs_vfx_init (VisualFX * _this, PluginInfo * info)
749 IfsData *data = (IfsData *) malloc (sizeof (IfsData));
751 data->Root = (FRACTAL *) NULL;
752 data->initalized = 0;
753 _this->fx_data = data;
757 ifs_vfx_free (VisualFX * _this)
759 IfsData *data = (IfsData *) _this->fx_data;
766 ifs_visualfx_create (VisualFX * vfx)
769 vfx->init = ifs_vfx_init;
770 vfx->free = ifs_vfx_free;
771 vfx->apply = ifs_vfx_apply;