2 * Copyright (C) <2003> iOS-Software
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
19 // --- CHUI EN TRAIN DE SUPPRIMER LES EXTERN RESOLX ET C_RESOLY ---
21 /* filter.c version 0.7
22 * contient les filtres applicable a un buffer
23 * creation : 01/10/2000
24 * -ajout de sinFilter()
25 * -ajout de zoomFilter()
26 * -copie de zoomFilter() en zoomFilterRGB(), gerant les 3 couleurs
27 * -optimisation de sinFilter (utilisant une table de sin)
29 * -optimisation de la procedure de generation du buffer de transformation
30 * la vitesse est maintenant comprise dans [0..128] au lieu de [0..100]
33 /* #define _DEBUG_PIXEL */
44 #ifdef HAVE_INTTYPES_H
48 #include "goom_filters.h"
49 #include "goom_graphic.h"
50 #include "goom_tools.h"
51 #include "goom_plugin_info.h"
55 /* TODO : MOVE THIS AWAY !!! */
56 /* jeko: j'ai essayer de le virer, mais si on veut les laisser inline c'est un peu lourdo... */
58 setPixelRGB (PluginInfo * goomInfo, Pixel * buffer, Uint x, Uint y, Color c)
65 *(buffer + (x + y * goomInfo->screen.width)) = i;
69 setPixelRGB_ (Pixel * buffer, Uint x, Color c)
71 buffer[x].channels.r = c.r;
72 buffer[x].channels.g = c.v;
73 buffer[x].channels.b = c.b;
77 getPixelRGB (PluginInfo * goomInfo, Pixel * buffer, Uint x, Uint y, Color * c)
79 Pixel i = *(buffer + (x + y * goomInfo->screen.width));
87 getPixelRGB_ (Pixel * buffer, Uint x, Color * c)
89 Pixel i = *(buffer + x);
100 // retourne x>>s , en testant le signe de x
101 //#define ShiftRight(_x,_s) (((_x)<0) ? -(-(_x)>>(_s)) : ((_x)>>(_s)))
102 //#define EFFECT_DISTORS 4
103 //#define EFFECT_DISTORS_SL 2
104 //#define INTERLACE_ADD 9
105 //#define INTERLACE_AND 0xf
108 #define BUFFPOINTNB 16
109 #define BUFFPOINTNBF 16.0f
110 #define BUFFPOINTMASK 0xffff
113 /* faire : a % sqrtperte <=> a & pertemask */
114 #define PERTEMASK 0xf
115 /* faire : a / sqrtperte <=> a >> PERTEDEC */
118 /* pure c version of the zoom filter */
119 static void c_zoom (Pixel * expix1, Pixel * expix2, unsigned int prevX,
120 unsigned int prevY, signed int *brutS, signed int *brutD, int buffratio,
121 int precalCoef[BUFFPOINTNB][BUFFPOINTNB]);
123 /* simple wrapper to give it the same proto than the others */
125 zoom_filter_c (int sizeX, int sizeY, Pixel * src, Pixel * dest, int *brutS,
126 int *brutD, int buffratio, int precalCoef[16][16])
128 c_zoom (src, dest, sizeX, sizeY, brutS, brutD, buffratio, precalCoef);
131 static void generatePrecalCoef (int precalCoef[BUFFPOINTNB][BUFFPOINTNB]);
134 typedef struct _ZOOM_FILTER_FX_WRAPPER_DATA
137 PluginParam enabled_bp;
138 PluginParameters params;
140 unsigned int *coeffs, *freecoeffs;
142 signed int *brutS, *freebrutS; /* source */
143 signed int *brutD, *freebrutD; /* dest */
144 signed int *brutT, *freebrutT; /* temp (en cours de generation) */
148 unsigned int prevX, prevY;
151 int reverse; /* reverse the speed */
158 int middleX, middleY;
163 /** modif by jeko : fixedpoint : buffration = (16:16) (donc 0<=buffration<=2^16) */
167 /** modif d'optim by Jeko : precalcul des 4 coefs resultant des 2 pos */
168 int precalCoef[BUFFPOINTNB][BUFFPOINTNB];
170 /** calculatePXandPY statics */
174 } ZoomFilterFXWrapperData;
180 zoomVector (v2g * vecteur, ZoomFilterFXWrapperData * data, float X, float Y)
183 float sq_dist = X * X + Y * Y;
185 /* sx = (X < 0.0f) ? -1.0f : 1.0f;
186 sy = (Y < 0.0f) ? -1.0f : 1.0f;
188 float coefVitesse = (1.0f + data->general_speed) / 50.0f;
194 switch (data->theMode) {
195 case CRYSTAL_BALL_MODE:
196 coefVitesse -= (sq_dist - 0.3f) / 15.0f;
199 coefVitesse += sq_dist * 3.5f;
202 coefVitesse += sin (sq_dist * 20.0f) / 100.0f;
205 coefVitesse += sq_dist / 10.0f;
207 //case HYPERCOS1_MODE:
209 //case HYPERCOS2_MODE:
214 coefVitesse *= 4.0f * Y;
220 if (coefVitesse < -2.01f)
221 coefVitesse = -2.01f;
222 if (coefVitesse > 2.01f)
225 vx = coefVitesse * X;
226 vy = coefVitesse * Y;
229 // vx = X * tan(dist);
230 // vy = Y * tan(dist);
241 vx += (((float) rand ()) / ((float) RAND_MAX) - 0.5f) / 50.0f;
242 vy += (((float) rand ()) / ((float) RAND_MAX) - 0.5f) / 50.0f;
246 if (data->hypercosEffect) {
247 vx += sin (Y * 10.0f) / 120.0f;
248 vy += sin (X * 10.0f) / 120.0f;
252 if (data->hPlaneEffect)
253 vx += Y * 0.0025f * data->hPlaneEffect;
256 if (data->vPlaneEffect)
257 vy += X * 0.0025f * data->vPlaneEffect;
259 /* TODO : Water Mode */
260 // if (data->waveEffect)
268 * Makes a stripe of a transform buffer (brutT)
270 * The transform is (in order) :
271 * Translation (-data->middleX, -data->middleY)
272 * Homothetie (Center : 0,0 Coeff : 2/data->prevX)
275 makeZoomBufferStripe (ZoomFilterFXWrapperData * data, int INTERLACE_INCR)
277 // Position of the pixel to compute in pixmap coordinates
280 // Where (verticaly) to stop generating the buffer stripe
283 // Ratio from pixmap to normalized coordinates
284 float ratio = 2.0f / ((float) data->prevX);
286 // Ratio from normalized to virtual pixmap coordinates
287 float inv_ratio = BUFFPOINTNBF / ratio;
288 float min = ratio / BUFFPOINTNBF;
290 // Y position of the pixel to compute in normalized coordinates
291 float Y = ((float) (data->interlace_start - data->middleY)) * ratio;
293 maxEnd = data->prevY;
294 if (maxEnd > (data->interlace_start + INTERLACE_INCR))
295 maxEnd = (data->interlace_start + INTERLACE_INCR);
297 for (y = data->interlace_start;
298 (y < data->prevY) && ((signed int) y < maxEnd); y++) {
299 Uint premul_y_prevX = y * data->prevX * 2;
300 float X = -((float) data->middleX) * ratio;
302 for (x = 0; x < data->prevX; x++) {
305 zoomVector (&vector, data, X, Y);
307 /* Finish and avoid null displacement */
308 if (fabs (vector.x) < min)
309 vector.x = (vector.x < 0.0f) ? -min : min;
310 if (fabs (vector.y) < min)
311 vector.y = (vector.y < 0.0f) ? -min : min;
313 data->brutT[premul_y_prevX] =
314 ((int) ((X - vector.x) * inv_ratio) +
315 ((int) (data->middleX * BUFFPOINTNB)));
316 data->brutT[premul_y_prevX + 1] =
317 ((int) ((Y - vector.y) * inv_ratio) +
318 ((int) (data->middleY * BUFFPOINTNB)));
324 data->interlace_start += INTERLACE_INCR;
325 if (y >= data->prevY - 1)
326 data->interlace_start = -1;
331 * calculer px et py en fonction de x,y,middleX,middleY et theMode
332 * px et py indique la nouvelle position (en sqrtperte ieme de pixel)
335 inline void calculatePXandPY (PluginInfo *goomInfo, ZoomFilterFXWrapperData *data, int x, int y, int *px, int *py)
337 if (data->theMode == WATER_MODE) {
340 yy = y + goom_irand(goomInfo->gRandom, 4) - goom_irand(goomInfo->gRandom, 4) + data->wave / 10;
343 if (yy >= (signed int)goomInfo->screen.height)
344 yy = goomInfo->screen.height - 1;
346 *px = (x << 4) + data->firedec[yy] + (data->wave / 10);
347 *py = (y << 4) + 132 - ((data->vitesse < 131) ? data->vitesse : 130);
349 data->wavesp += goom_irand(goomInfo->gRandom, 3) - goom_irand(goomInfo->gRandom, 3);
350 if (data->wave < -10)
354 data->wave += (data->wavesp / 10) + goom_irand(goomInfo->gRandom, 3) - goom_irand(goomInfo->gRandom, 3);
355 if (data->wavesp > 100)
356 data->wavesp = (data->wavesp * 9) / 10;
359 int dist = 0, vx9, vy9;
362 int fvitesse = data->vitesse << 4;
365 x += goom_irand(goomInfo->gRandom, data->noisify) - goom_irand(goomInfo->gRandom, data->noisify);
366 y += goom_irand(goomInfo->gRandom, data->noisify) - goom_irand(goomInfo->gRandom, data->noisify);
368 vx = (x - data->middleX) << 9;
369 vy = (y - data->middleY) << 9;
371 if (data->hPlaneEffect)
372 vx += data->hPlaneEffect * (y - data->middleY);
374 if (data->vPlaneEffect)
375 vy += data->vPlaneEffect * (x - data->middleX);
377 if (data->waveEffect) {
380 ShiftRight (goomInfo->sintable
381 [(unsigned short) (dist * 0xffff + EFFECT_DISTORS)], 6);
385 if (data->hypercosEffect) {
386 vx += ShiftRight (goomInfo->sintable[(-vy + dist) & 0xffff], 1);
387 vy += ShiftRight (goomInfo->sintable[(vx + dist) & 0xffff], 1);
390 vx9 = ShiftRight (vx, 9);
391 vy9 = ShiftRight (vy, 9);
392 dist = vx9 * vx9 + vy9 * vy9;
394 switch (data->theMode) {
398 ShiftRight (goomInfo->sintable
399 [(unsigned short) (dist * 0xffff * EFFECT_DISTORS)], 6);
402 case CRYSTAL_BALL_MODE:
403 fvitesse += (dist >> (10-EFFECT_DISTORS_SL));
406 fvitesse -= (dist >> (4 - EFFECT_DISTORS_SL));
409 fvitesse -= (dist >> (10 - EFFECT_DISTORS_SL));
412 vx = vx + ShiftRight (goomInfo->sintable[(-vy + dist) & 0xffff], 1);
413 vy = vy + ShiftRight (goomInfo->sintable[(vx + dist) & 0xffff], 1);
417 vx + ShiftRight (goomInfo->sintable[(-ShiftRight (vy, 1) + dist) & 0xffff], 0);
419 vy + ShiftRight (goomInfo->sintable[(ShiftRight (vx, 1) + dist) & 0xffff], 0);
423 fvitesse *= 1024 + ShiftRight (goomInfo->sintable[vy & 0xffff], 6);
427 fvitesse -= (ShiftRight(vy,10-EFFECT_DISTORS_SL));
431 if (fvitesse < -3024)
434 if (vx < 0) // pb avec decalage sur nb negatif
435 ppx = -(-(vx * fvitesse) >> 16);
436 // 16 = 9 + 7 (7 = nb chiffre virgule de vitesse * (v = 128 => immobile)
437 // * * * * * 9 = nb chiffre virgule de vx)
439 ppx = ((vx * fvitesse) >> 16);
442 ppy = -(-(vy * fvitesse) >> 16);
444 ppy = ((vy * fvitesse) >> 16);
446 *px = (data->middleX << 4) + ppx;
447 *py = (data->middleY << 4) + ppy;
455 c_zoom (Pixel * expix1, Pixel * expix2, unsigned int prevX, unsigned int prevY,
456 signed int *brutS, signed int *brutD, int buffratio, int precalCoef[16][16])
461 unsigned int ax = (prevX - 1) << PERTEDEC, ay = (prevY - 1) << PERTEDEC;
463 int bufsize = prevX * prevY * 2;
464 int bufwidth = prevX;
466 expix1[0].val = expix1[prevX - 1].val = expix1[prevX * prevY - 1].val =
467 expix1[prevX * prevY - prevX].val = 0;
469 for (myPos = 0; myPos < bufsize; myPos += 2) {
470 Color col1, col2, col3, col4;
471 int c1, c2, c3, c4, px, py;
475 int brutSmypos = brutS[myPos];
479 px = brutSmypos + (((brutD[myPos] -
480 brutSmypos) * buffratio) >> BUFFPOINTNB);
481 brutSmypos = brutS[myPos2];
482 py = brutSmypos + (((brutD[myPos2] -
483 brutSmypos) * buffratio) >> BUFFPOINTNB);
485 if ((py >= ay) || (px >= ax)) {
488 pos = ((px >> PERTEDEC) + prevX * (py >> PERTEDEC));
489 /* coef en modulo 15 */
490 coeffs = precalCoef[px & PERTEMASK][py & PERTEMASK];
492 getPixelRGB_ (expix1, pos, &col1);
493 getPixelRGB_ (expix1, pos + 1, &col2);
494 getPixelRGB_ (expix1, pos + bufwidth, &col3);
495 getPixelRGB_ (expix1, pos + bufwidth + 1, &col4);
498 c2 = (c1 >> 8) & 0xFF;
499 c3 = (c1 >> 16) & 0xFF;
500 c4 = (c1 >> 24) & 0xFF;
503 couleur.r = col1.r * c1 + col2.r * c2 + col3.r * c3 + col4.r * c4;
508 couleur.v = col1.v * c1 + col2.v * c2 + col3.v * c3 + col4.v * c4;
513 couleur.b = col1.b * c1 + col2.b * c2 + col3.b * c3 + col4.b * c4;
518 setPixelRGB_ (expix2, myPos >> 1, couleur);
522 /** generate the water fx horizontal direction buffer */
524 generateTheWaterFXHorizontalDirectionBuffer (PluginInfo * goomInfo,
525 ZoomFilterFXWrapperData * data)
529 int decc = goom_irand (goomInfo->gRandom, 8) - 4;
530 int spdc = goom_irand (goomInfo->gRandom, 8) - 4;
531 int accel = goom_irand (goomInfo->gRandom, 8) - 4;
533 for (loopv = data->prevY; loopv != 0;) {
536 data->firedec[loopv] = decc;
539 goom_irand (goomInfo->gRandom, 3) - goom_irand (goomInfo->gRandom, 3);
547 spdc = spdc - goom_irand (goomInfo->gRandom, 3) + accel / 10;
549 spdc = spdc + goom_irand (goomInfo->gRandom, 3) + accel / 10;
551 if (decc > 8 && spdc > 1)
552 spdc -= goom_irand (goomInfo->gRandom, 3) - 2;
554 if (decc < -8 && spdc < -1)
555 spdc += goom_irand (goomInfo->gRandom, 3) + 2;
557 if (decc > 8 || decc < -8)
561 goom_irand (goomInfo->gRandom, 2) - goom_irand (goomInfo->gRandom, 2);
572 * Main work for the dynamic displacement map.
574 * Reads data from pix1, write to pix2.
576 * Useful datas for this FX are stored in ZoomFilterData.
578 * If you think that this is a strange function name, let me say that a long time ago,
579 * there has been a slow version and a gray-level only one. Then came these function,
580 * fast and workin in RGB colorspace ! nice but it only was applying a zoom to the image.
581 * So that is why you have this name, for the nostalgy of the first days of goom
582 * when it was just a tiny program writen in Turbo Pascal on my i486...
585 zoomFilterFastRGB (PluginInfo * goomInfo, Pixel * pix1, Pixel * pix2,
586 ZoomFilterData * zf, Uint resx, Uint resy, int switchIncr, float switchMult)
590 ZoomFilterFXWrapperData *data =
591 (ZoomFilterFXWrapperData *) goomInfo->zoomFilter_fx.fx_data;
593 if (!BVAL (data->enabled_bp))
596 /** changement de taille **/
597 if ((data->prevX != resx) || (data->prevY != resy)) {
602 free (data->freebrutS);
605 free (data->freebrutD);
608 free (data->freebrutT);
611 data->middleX = resx / 2;
612 data->middleY = resy / 2;
613 data->mustInitBuffers = 1;
615 free (data->firedec);
619 if (data->interlace_start != -2)
622 /** changement de config **/
624 data->reverse = zf->reverse;
625 data->general_speed = (float) (zf->vitesse - 128) / 128.0f;
627 data->general_speed = -data->general_speed;
628 data->middleX = zf->middleX;
629 data->middleY = zf->middleY;
630 data->theMode = zf->mode;
631 data->hPlaneEffect = zf->hPlaneEffect;
632 data->vPlaneEffect = zf->vPlaneEffect;
633 data->waveEffect = zf->waveEffect;
634 data->hypercosEffect = zf->hypercosEffect;
635 data->noisify = zf->noisify;
636 data->interlace_start = 0;
640 if (data->mustInitBuffers) {
642 data->mustInitBuffers = 0;
644 (signed int *) calloc (resx * resy * 2 + 128, sizeof (unsigned int));
646 (gint32 *) ((1 + ((uintptr_t) (data->freebrutS)) / 128) * 128);
649 (signed int *) calloc (resx * resy * 2 + 128, sizeof (unsigned int));
651 (gint32 *) ((1 + ((uintptr_t) (data->freebrutD)) / 128) * 128);
654 (signed int *) calloc (resx * resy * 2 + 128, sizeof (unsigned int));
656 (gint32 *) ((1 + ((uintptr_t) (data->freebrutT)) / 128) * 128);
660 data->firedec = (int *) malloc (data->prevY * sizeof (int));
661 generateTheWaterFXHorizontalDirectionBuffer (goomInfo, data);
663 data->interlace_start = 0;
664 makeZoomBufferStripe (data, resy);
666 /* Copy the data from temp to dest and source */
667 memcpy (data->brutS, data->brutT, resx * resy * 2 * sizeof (int));
668 memcpy (data->brutD, data->brutT, resx * resy * 2 * sizeof (int));
671 /* generation du buffer de trans */
672 if (data->interlace_start == -1) {
674 /* sauvegarde de l'etat actuel dans la nouvelle source
675 * TODO: write that in MMX (has been done in previous version, but did not follow some new fonctionnalities) */
676 y = data->prevX * data->prevY * 2;
677 for (x = 0; x < y; x += 2) {
678 int brutSmypos = data->brutS[x];
682 brutSmypos + (((data->brutD[x] -
683 brutSmypos) * data->buffratio) >> BUFFPOINTNB);
684 brutSmypos = data->brutS[x2];
686 brutSmypos + (((data->brutD[x2] -
687 brutSmypos) * data->buffratio) >> BUFFPOINTNB);
692 if (data->interlace_start == -1) {
696 data->brutD = data->brutT;
698 tmp = data->freebrutD;
699 data->freebrutD = data->freebrutT;
700 data->freebrutT = tmp;
701 data->interlace_start = -2;
704 if (data->interlace_start >= 0) {
705 /* creation de la nouvelle destination */
706 makeZoomBufferStripe (data, resy / 16);
709 if (switchIncr != 0) {
710 data->buffratio += switchIncr;
711 if (data->buffratio > BUFFPOINTMASK)
712 data->buffratio = BUFFPOINTMASK;
715 if (switchMult != 1.0f) {
716 data->buffratio = (int) ((float) BUFFPOINTMASK * (1.0f - switchMult) +
717 (float) data->buffratio * switchMult);
720 data->zoom_width = data->prevX;
722 goomInfo->methods.zoom_filter (data->prevX, data->prevY, pix1, pix2,
723 data->brutS, data->brutD, data->buffratio, data->precalCoef);
727 generatePrecalCoef (int precalCoef[16][16])
731 for (coefh = 0; coefh < 16; coefh++) {
732 for (coefv = 0; coefv < 16; coefv++) {
738 diffcoeffh = sqrtperte - coefh;
739 diffcoeffv = sqrtperte - coefv;
741 if (!(coefh || coefv)) {
746 i1 = diffcoeffh * diffcoeffv;
747 i2 = coefh * diffcoeffv;
748 i3 = diffcoeffh * coefv;
751 // TODO: faire mieux...
761 i = (i1) | (i2 << 8) | (i3 << 16) | (i4 << 24);
763 precalCoef[coefh][coefv] = i;
768 /* VisualFX Wrapper */
771 zoomFilterVisualFXWrapper_init (struct _VISUAL_FX *_this, PluginInfo * info)
773 ZoomFilterFXWrapperData *data =
774 (ZoomFilterFXWrapperData *) malloc (sizeof (ZoomFilterFXWrapperData));
777 data->freecoeffs = 0;
787 data->mustInitBuffers = 1;
788 data->interlace_start = -2;
790 data->general_speed = 0.0f;
792 data->theMode = AMULETTE_MODE;
793 data->waveEffect = 0;
794 data->hypercosEffect = 0;
795 data->vPlaneEffect = 0;
796 data->hPlaneEffect = 0;
799 /** modif by jeko : fixedpoint : buffration = (16:16) (donc 0<=buffration<=2^16) */
803 data->wave = data->wavesp = 0;
805 secure_b_param (&data->enabled_bp, "Enabled", 1);
807 plugin_parameters (&data->params, "Zoom Filter", 1);
808 data->params.params[0] = &data->enabled_bp;
810 _this->params = &data->params;
811 _this->fx_data = (void *) data;
813 /** modif d'optim by Jeko : precalcul des 4 coefs resultant des 2 pos */
814 generatePrecalCoef (data->precalCoef);
818 zoomFilterVisualFXWrapper_free (struct _VISUAL_FX *_this)
820 ZoomFilterFXWrapperData *data = (ZoomFilterFXWrapperData *) _this->fx_data;
823 free (data->freebrutT);
825 free (data->freebrutS);
827 free (data->freebrutD);
829 free (data->firedec);
831 goom_plugin_parameters_free (_this->params);
833 free (_this->fx_data);
837 zoomFilterVisualFXWrapper_apply (struct _VISUAL_FX *_this, Pixel * src,
838 Pixel * dest, PluginInfo * info)
843 zoomFilterVisualFXWrapper_create (VisualFX * fx)
845 fx->init = zoomFilterVisualFXWrapper_init;
846 fx->free = zoomFilterVisualFXWrapper_free;
847 fx->apply = zoomFilterVisualFXWrapper_apply;
853 /* TODO : MOVE THIS AWAY */
856 pointFilter (PluginInfo * goomInfo, Pixel * pix1, Color c, float t1, float t2,
857 float t3, float t4, Uint cycle)
859 Uint x = (Uint) ((int) (goomInfo->screen.width / 2)
860 + (int) (t1 * cos ((float) cycle / t3)));
861 Uint y = (Uint) ((int) (goomInfo->screen.height / 2)
862 + (int) (t2 * sin ((float) cycle / t4)));
864 if ((x > 1) && (y > 1) && (x < goomInfo->screen.width - 2)
865 && (y < goomInfo->screen.height - 2)) {
866 setPixelRGB (goomInfo, pix1, x + 1, y, c);
867 setPixelRGB (goomInfo, pix1, x, y + 1, c);
868 setPixelRGB (goomInfo, pix1, x + 1, y + 1, WHITE);
869 setPixelRGB (goomInfo, pix1, x + 2, y + 1, c);
870 setPixelRGB (goomInfo, pix1, x + 1, y + 2, c);