Debian Codegraph @

src/filter/water/water.cpp

raw symbols SCIP
1 /* Water filter
2 *
3 * (c) Copyright 2000-2025 Denis Roio <[email protected]>
4 *
5 * from an original idea of water algorithm by Federico 'Pix' Feroldi
6 *
7 * this code contains optimizations by Jason Hood and Scott Scriven
8 *
9 * animated background, 32bit colorspace and interactivity by jaromil
10 * ported to C++ and frei0r plugin API in 2007
11 *
12 * This source code is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Public License as published
14 * by the Free Software Foundation; either version 2 of the License,
15 * or (at your option) any later version.
16 *
17 * This source code is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 * Please refer to the GNU Public License for more details.
21 *
22 * You should have received a copy of the GNU Public License along with
23 * this source code; if not, write to:
24 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 */
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <time.h>
32
33 #include <frei0r.hpp>
34
35 #define CLIP_EDGES \
36 if(x - radius < 1) left -= (x-radius-1); \
37 if(y - radius < 1) top -= (y-radius-1); \
38 if(x + radius > geo->w-1) right -= (x+radius-geo->w+1); \
39 if(y + radius > geo->h-1) bottom-= (y+radius-geo->h+1);
40
41 /* water physics */
42 #define WATER 1
43 #define JELLY 2
44 #define SLUDGE 3
45 #define SUPER_SLUDGE 4
46
47 /* precalculated sinusoidal tables */
48 #include <math.h>
49 #define FSINMAX 2047
50 #define SINFIX 16
51 #define FSINBITS 16
52 #ifndef PI
53 #define PI 3.14159265358979323846
54 #endif
55
56 typedef struct {
57 int16_t w;
58 int16_t h;
59 uint8_t bpp;
60 uint32_t size;
61 } ScreenGeometry;
62
63 class Water: public frei0r::filter {
64 public:
65
66 f0r_param_double physics;
67 f0r_param_bool swirl;
68 f0r_param_bool rain;
69 f0r_param_bool surfer;
70 f0r_param_bool smooth;
71 f0r_param_bool distort;
72 f0r_param_position position;
73 //bool randomize_swirl;
74
75 Water(unsigned int width, unsigned int height) {
76 physics = 1.0;
77 swirl = 1;
78 rain = 0;
79 surfer = 0;
80 distort = 0;
81 smooth = 0;
82 position.x = 0.0;
83 position.y = 0.0;
84 //randomize_swirl = false;
85 register_param(physics, "physics", "water density: from 0.0 to 1.0");
86 register_param(swirl, "swirl", "swirling whirpool in the center");
87 register_param(rain, "rain", "rain drops all over");
88 register_param(surfer, "surfer", "surf the surface with a wandering finger");
89 register_param(smooth, "smooth", "smooth up all perturbations on the surface");
90 register_param(distort, "distort", "distort all surface like dropping a bucket to the floor");
91 register_param(position, "position", "swirl position coordinate, Relative center coordinate");
92 //register_param(randomize_swirl, "randomize_swirl", "randomize the swirling angle");
93
94 Hpage = 0;
95 ox = 80;
96 oy = 80;
97 done = 0;
98 mode = 0x4000;
99
100 BkGdImagePre = BkGdImage = BkGdImagePost = 0;
101 Height[0] = Height[1] = 0;
102
103 /* default physics */
104 density = 4;
105 pheight = 600;
106 radius = 30;
107
108 raincount = 0;
109 blend = 0;
110
111 fastsrand(::time(NULL));
112
113 FCreateSines();
114
115 geo = new ScreenGeometry();
116 geo->w = width;
117 geo->h = height;
118 geo->size = width*(height+1)*sizeof(uint32_t);
119
120 water_surfacesize = geo->size;
121 calc_optimization = (height)*(width);
122
123 xang = fastrand()%2048;
124 yang = fastrand()%2048;
125 swirlangle = fastrand()%2048;
126
127 /* buffer allocation tango */
128 if ( width*height > 0 ) {
129 Height[0] = (uint32_t*)calloc(width*(height+1), sizeof(uint32_t));
130 Height[1] = (uint32_t*)calloc(width*(height+1), sizeof(uint32_t));
131 }
132 if ( geo->size > 0 ) {
133 BkGdImagePre = (uint32_t*) malloc(geo->size);
134 BkGdImage = (uint32_t*) malloc(geo->size);
135 BkGdImagePost = (uint32_t*)malloc(geo->size);
136 }
137
138 // Initialize surface to NULL
139 surface = NULL;
140 }
141
142 ~Water() {
143 delete geo;
144 free(Height[0]);
145 free(Height[1]);
146 free(BkGdImagePre);
147 free(BkGdImage);
148 free(BkGdImagePost);
149 }
150
151 virtual void update(double time,
152 uint32_t* out,
153 const uint32_t* in) {
154 memcpy(BkGdImage, in, width*height*sizeof(uint32_t));
155 water_update(out);
156 }
157
158 private:
159 ScreenGeometry *geo;
160
161 /* 2 pages of Height field */
162 uint32_t *Height[2];
163 /* 3 copies of the background */
164 uint32_t *BkGdImagePre;
165 uint32_t *BkGdImage;
166 uint32_t *BkGdImagePost;
167
168 // uint32_t *buffer;
169
170 void *surface;
171
172 /* water effect variables */
173 int Hpage;
174 int xang, yang;
175 int swirlangle;
176 int x, y, ox, oy;
177 int done;
178 int mode;
179
180 /* precalculated to optimize a bit */
181 int water_surfacesize;
182 int calc_optimization;
183
184 /* density: water density (step 1)
185 pheight: splash height (step 40)
186 radius: waterdrop radius (step 1) */
187 int density, pheight, radius;
188 int offset;
189
190 int raincount;
191 int blend;
192
193 void water_clear();
194 void water_distort();
195 void water_setphysics(double physics);
196 void water_update(uint32_t *out);
197 void water_drop(int x, int y);
198 void water_bigsplash(int x, int y);
199 void water_surfer();
200 void water_swirl();
201 void water_3swirls();
202
203 void DrawWater(int page, uint32_t* out);
204 void CalcWater(int npage, int density);
205 void CalcWaterBigFilter(int npage, int density);
206
207 void SmoothWater(int npage);
208
209 void HeightBlob(int x, int y, int radius, int height, int page);
210 void HeightBox (int x, int y, int radius, int height, int page);
211
212 void WarpBlob(int x, int y, int radius, int height, int page);
213 void SineBlob(int x, int y, int radius, int height, int page);
214
215 /* precalculated sinusoidal tables */
216 int FSinTab[2048], FCosTab[2048];
217 int FSin(int angle) { return FSinTab[angle&FSINMAX]; }
218 int FCos(int angle) { return FCosTab[angle&FSINMAX]; }
219 void FCreateSines() {
220 int i; double angle;
221 for(i=0; i<2048; i++) {
222 angle = (float)i * (PI/1024.0);
223 FSinTab[i] = (int)(sin(angle) * (float)0x10000);
224 FCosTab[i] = (int)(cos(angle) * (float)0x10000);
225 }
226 }
227
228 /* cheap & fast randomizer (by Fukuchi Kentarou) */
229 uint32_t randval;
230 uint32_t fastrand() { return (randval=randval*1103515245+12345); };
231 void fastsrand(uint32_t seed) { randval = seed; };
232
233 /* integer optimized square root by jaromil */
234 int isqrt(unsigned int x) {
235 unsigned int m, y, b; m = 0x40000000;
236 y = 0; while(m != 0) { b = y | m; y = y>>1;
237 if(x>=b) { x=x-b; y=y|m; }
238 m=m>>2; } return y;
239 }
240
241 };
242
243 void Water::water_clear() {
244 memset(Height[0], 0, water_surfacesize);
245 memset(Height[1], 0, water_surfacesize);
246 }
247
248 void Water::water_distort() {
249 memset(Height[Hpage], 0, water_surfacesize);
250 }
251
252 void Water::water_setphysics(double physics) {
253 if(physics<0.25) {
254 // case WATER:
255 mode |= 0x4000;
256 density=4;
257 pheight=600;
258 } else if(physics<0.50) {
259 // case JELLY:
260 mode &= 0xBFFF;
261 density=3;
262 pheight=400;
263 } else if(physics<0.75) {
264 // case SLUDGE:
265 mode &= 0xBFFF;
266 density=6;
267 pheight=400;
268 } else {
269 // case SUPER_SLUDGE:
270 mode &=0xBFFF;
271 density=8;
272 pheight=400;
273 }
274 }
275
276 void Water::water_update(uint32_t* out) {
277 if(distort && !rain) water_distort();
278 if(smooth) SmoothWater(Hpage);
279
280 if(rain) {
281 raincount++;
282 if(raincount>3) {
283 water_drop( (fastrand()%geo->w-40)+20 , (fastrand()%geo->h-40)+20 );
284 raincount=0;
285 }
286 }
287
288 if(swirl) water_swirl();
289 if(surfer) water_surfer();
290 DrawWater(Hpage,out);
291
292 CalcWater(Hpage^1, density);
293 Hpage ^=1 ;
294 }
295
296 void Water::water_drop(int x, int y) {
297 if(mode & 0x4000)
298 HeightBlob(x,y, radius>>2, pheight, Hpage);
299 else
300 WarpBlob(x, y, radius, pheight, Hpage);
301 }
302
303 void Water::water_bigsplash(int x, int y) {
304 if(mode & 0x4000)
305 HeightBlob(x, y, (radius>>1), pheight, Hpage);
306 else
307 SineBlob(x, y, radius, -pheight*6, Hpage);
308 }
309
310 void Water::water_surfer() {
311 int x, y;
312 x = (geo->w>>1)
313 + ((
314 (
315 (FSin( (xang* 65) >>8) >>8) *
316 (FSin( (xang*349) >>8) >>8)
317 ) * ((geo->w-8)>>1)
318 ) >> 16);
319 y = (geo->h>>1)
320 + ((
321 (
322 (FSin( (yang*377) >>8) >>8) *
323 (FSin( (yang* 84) >>8) >>8)
324 ) * ((geo->h-8)>>1)
325 ) >> 16);
326 xang += 13;
327 yang += 12;
328
329 if(mode & 0x4000)
330 {
331 offset = (oy+y)/2*geo->w + ((ox+x)>>1); // QUAAA
332 Height[Hpage][offset] = pheight;
333 Height[Hpage][offset + 1] =
334 Height[Hpage][offset - 1] =
335 Height[Hpage][offset + geo->w] =
336 Height[Hpage][offset - geo->w] = pheight >> 1;
337
338 offset = y*geo->w + x;
339 Height[Hpage][offset] = pheight<<1;
340 Height[Hpage][offset + 1] =
341 Height[Hpage][offset - 1] =
342 Height[Hpage][offset + geo->w] =
343 Height[Hpage][offset - geo->w] = pheight;
344 }
345 else
346 {
347 SineBlob(((ox+x)>>1), ((oy+y)>>1), 3, -1200, Hpage);
348 SineBlob(x, y, 4, -2000, Hpage);
349 }
350
351 ox = x;
352 oy = y;
353 }
354
355 void Water::water_swirl() {
356 int x, y;
357 x = (geo->w>>1)
358 + ((
359 (FCos(swirlangle)) * (25)
360 ) >> 16);
361
362 y = (geo->h>>1)
363 + ((
364 (FSin(swirlangle)) * (25)
365 ) >> 16);
366 x += (int)(position.x * geo->w);
367 y += (int)(position.y * geo->h);
368
369 swirlangle += 50;
370 if(mode & 0x4000)
371 HeightBlob(x, y, radius>>2, pheight, Hpage);
372 else
373 WarpBlob(x, y, radius, pheight, Hpage);
374 }
375
376 void Water::water_3swirls() {
377 #define ANGLE 15
378 int x, y;
379 x = (95)
380 + ((
381 (FCos(swirlangle)) * (ANGLE)
382 ) >> 16);
383 y = (45)
384 + ((
385 (FSin(swirlangle)) * (ANGLE)
386 ) >> 16);
387
388 if(mode & 0x4000) HeightBlob(x,y, radius>>2, pheight, Hpage);
389 else WarpBlob(x, y, radius, pheight, Hpage);
390
391 x = (95)
392 + ((
393 (FCos(swirlangle)) * (ANGLE)
394 ) >> 16);
395 y = (255)
396 + ((
397 (FSin(swirlangle)) * (ANGLE)
398 ) >> 16);
399
400 if(mode & 0x4000) HeightBlob(x,y, radius>>2, pheight, Hpage);
401 else WarpBlob(x, y, radius, pheight, Hpage);
402
403 x = (345)
404 + ((
405 (FCos(swirlangle)) * (ANGLE)
406 ) >> 16);
407 y = (150)
408 + ((
409 (FSin(swirlangle)) * (ANGLE)
410 ) >> 16);
411
412 if(mode & 0x4000) HeightBlob(x,y, radius>>2, pheight, Hpage);
413 else WarpBlob(x, y, radius, pheight, Hpage);
414
415 swirlangle += 50;
416 }
417
418 /* internal physics routines */
419 void Water::DrawWater(int page,uint32_t* out) {
420 int dx, dy;
421 int offset=geo->w + 1;
422 int newoffset;
423 int maxoffset = geo->w * geo->h;
424 int *ptr = (int*)&Height[page][0];
425
426 for (int y = calc_optimization; offset < y; offset += 2) {
427 for (int x = offset+geo->w-2; offset < x; offset++) {
428 dx = ptr[offset] - ptr[offset+1];
429 dy = ptr[offset] - ptr[offset+geo->w];
430 newoffset = offset + geo->w*(dy>>3) + (dx>>3);
431 if (newoffset >= 0 && newoffset < maxoffset) {
432 out[offset] = BkGdImage[newoffset];
433 }
434
435 offset++;
436 dx = ptr[offset] - ptr[offset+1];
437 dy = ptr[offset] - ptr[offset+geo->w];
438 newoffset = offset + geo->w*(dy>>3) + (dx>>3);
439 if (newoffset >= 0 && newoffset < maxoffset) {
440 out[offset] = BkGdImage[newoffset];
441 }
442 }
443 }
444 }
445
446 void Water::CalcWater(int npage, int density) {
447 int newh;
448 int count = geo->w + 1;
449 int *newptr = (int*) &Height[npage][0];
450 int *oldptr = (int*) &Height[npage^1][0];
451
452 for (int y = calc_optimization; count < y; count += 2) {
453 for (int x = count+geo->w-2; count < x; count++) {
454 /* eight pixels */
455 newh = ((oldptr[count + geo->w]
456 + oldptr[count - geo->w]
457 + oldptr[count + 1]
458 + oldptr[count - 1]
459 + oldptr[count - geo->w - 1]
460 + oldptr[count - geo->w + 1]
461 + oldptr[count + geo->w - 1]
462 + oldptr[count + geo->w + 1]
463 ) >> 2 )
464 - newptr[count];
465 newptr[count] = newh - (newh >> density);
466 }
467 }
468 }
469
470 void Water::SmoothWater(int npage) {
471 int newh;
472 int count = geo->w + 1;
473 int *newptr = (int*) &Height[npage][0];
474 int *oldptr = (int*) &Height[npage^1][0];
475
476 for(int y=1; y<geo->h-1; y++) {
477 for(int x=1; x<geo->w-1; x++) {
478 /* eight pixel */
479 newh = ((oldptr[count + geo->w]
480 + oldptr[count - geo->w]
481 + oldptr[count + 1]
482 + oldptr[count - 1]
483 + oldptr[count - geo->w - 1]
484 + oldptr[count - geo->w + 1]
485 + oldptr[count + geo->w - 1]
486 + oldptr[count + geo->w + 1]
487 ) >> 3 )
488 + newptr[count];
489
490
491 newptr[count] = newh>>1;
492 count++;
493 }
494 count += 2;
495 }
496 }
497
498 void Water::CalcWaterBigFilter(int npage, int density) {
499 int newh;
500 int count = (geo->w<<1) + 2;
501 int *newptr = (int*) &Height[npage][0];
502 int *oldptr = (int*) &Height[npage^1][0];
503
504 for(int y=2; y<geo->h-2; y++) {
505 for(int x=2; x<geo->w-2; x++) {
506 /* 25 pixels */
507 newh = (
508 (
509 (
510 (oldptr[count + geo->w]
511 + oldptr[count - geo->w]
512 + oldptr[count + 1]
513 + oldptr[count - 1]
514 )<<1)
515 + ((oldptr[count - geo->w - 1]
516 + oldptr[count - geo->w + 1]
517 + oldptr[count + geo->w - 1]
518 + oldptr[count + geo->w + 1]))
519 + ( (
520 oldptr[count - (geo->w<<1)]
521 + oldptr[count + (geo->w<<1)]
522 + oldptr[count - 2]
523 + oldptr[count + 2]
524 ) >> 1 )
525 + ( (
526 oldptr[count - (geo->w<<1) - 1]
527 + oldptr[count - (geo->w<<1) + 1]
528 + oldptr[count + (geo->w<<1) - 1]
529 + oldptr[count + (geo->w<<1) + 1]
530 + oldptr[count - 2 - geo->w]
531 + oldptr[count - 2 + geo->w]
532 + oldptr[count + 2 - geo->w]
533 + oldptr[count + 2 + geo->w]
534 ) >> 2 )
535 )
536 >> 3)
537 - (newptr[count]);
538 newptr[count] = newh - (newh >> density);
539 count++;
540 }
541 count += 4;
542 }
543 }
544
545 void Water::HeightBlob(int x, int y, int radius, int height, int page) {
546 int rquad;
547 int cx, cy, cyq;
548 int left, top, right, bottom;
549
550 rquad = radius * radius;
551
552 /* Make a randomly-placed blob... */
553 if(x<0) x = 1+radius+ fastrand()%(geo->w-2*radius-1);
554 if(y<0) y = 1+radius+ fastrand()%(geo->h-2*radius-1);
555
556 left=-radius; right = radius;
557 top=-radius; bottom = radius;
558
559 CLIP_EDGES
560
561 for(cy = top; cy < bottom; cy++) {
562 cyq = cy*cy;
563 for(cx = left; cx < right; cx++) {
564 if(cx*cx + cyq < rquad) {
565 int index = geo->w*(cy+y) + (cx+x);
566 // Bounds check
567 if (index >= 0 && index < geo->w * geo->h) {
568 Height[page][index] += height;
569 }
570 }
571 }
572 }
573 }
574
575
576 void Water::HeightBox (int x, int y, int radius, int height, int page) {
577 int cx, cy;
578 int left, top, right, bottom;
579
580 if(x<0) x = 1+radius+ fastrand()%(geo->w-2*radius-1);
581 if(y<0) y = 1+radius+ fastrand()%(geo->h-2*radius-1);
582
583 left=-radius; right = radius;
584 top=-radius; bottom = radius;
585
586 CLIP_EDGES
587
588 for(cy = top; cy < bottom; cy++) {
589 for(cx = left; cx < right; cx++) {
590 int index = geo->w*(cy+y) + (cx+x);
591 // Bounds check
592 if (index >= 0 && index < geo->w * geo->h) {
593 Height[page][index] = height;
594 }
595 }
596 }
597 }
598
599 void Water::WarpBlob(int x, int y, int radius, int height, int page) {
600 int cx, cy;
601 int left,top,right,bottom;
602 int square;
603 int radsquare = radius * radius;
604
605 radsquare = (radius*radius);
606
607 height = height>>5;
608
609 left=-radius; right = radius;
610 top=-radius; bottom = radius;
611
612 CLIP_EDGES
613
614 for(cy = top; cy < bottom; cy++) {
615 for(cx = left; cx < right; cx++) {
616 square = cy*cy + cx*cx;
617 if(square < radsquare) {
618 int index = geo->w*(cy+y) + cx+x;
619 // Bounds check
620 if (index >= 0 && index < geo->w * geo->h) {
621 Height[page][index] += (int)((radius-isqrt(square))*(float)(height));
622 }
623 }
624 }
625 }
626 }
627
628 void Water::SineBlob(int x, int y, int radius, int height, int page) {
629 int cx, cy;
630 int left,top,right,bottom;
631 int square, dist;
632 int radsquare = radius * radius;
633 float length = (1024.0/(float)radius)*(1024.0/(float)radius);
634
635 if(x<0) x = 1+radius+ fastrand()%(geo->w-2*radius-1);
636 if(y<0) y = 1+radius+ fastrand()%(geo->h-2*radius-1);
637
638 radsquare = (radius*radius);
639 left=-radius; right = radius;
640 top=-radius; bottom = radius;
641
642 CLIP_EDGES
643
644 for(cy = top; cy < bottom; cy++) {
645 for(cx = left; cx < right; cx++) {
646 square = cy*cy + cx*cx;
647 if(square < radsquare) {
648 dist = (int)(isqrt(square*length));
649 int index = geo->w*(cy+y) + cx+x;
650 // Bounds check
651 if (index >= 0 && index < geo->w * geo->h) {
652 Height[page][index] += (int)((FCos(dist)+0xffff)*(height)) >> 19;
653 }
654 }
655 }
656 }
657 }
658
659 frei0r::construct<Water> plugin("Water",
660 "water drops on a video surface",
661 "Jaromil",
662 4,0);