filters.c 23.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Goom Project
 * Copyright (C) <2003> iOS-Software
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
16 17
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
18
 */
19 20
// --- CHUI EN TRAIN DE SUPPRIMER LES EXTERN RESOLX ET C_RESOLY ---

21
/* filter.c version 0.7
22 23 24 25 26 27 28 29 30
* contient les filtres applicable a un buffer
* creation : 01/10/2000
*  -ajout de sinFilter()
*  -ajout de zoomFilter()
*  -copie de zoomFilter() en zoomFilterRGB(), gerant les 3 couleurs
*  -optimisation de sinFilter (utilisant une table de sin)
*	-asm
*	-optimisation de la procedure de generation du buffer de transformation
*		la vitesse est maintenant comprise dans [0..128] au lieu de [0..100]
31 32
*/

33
/* #define _DEBUG_PIXEL */
34

35 36 37 38
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

39
#include <string.h>
40 41 42
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
43 44

#ifdef HAVE_INTTYPES_H
45
#include <inttypes.h>
46
#endif
47

48 49 50 51 52 53
#include "goom_filters.h"
#include "goom_graphic.h"
#include "goom_tools.h"
#include "goom_plugin_info.h"
#include "goom_fx.h"
#include "v3d.h"
54

55 56 57 58 59 60
/* TODO : MOVE THIS AWAY !!! */
/* jeko: j'ai essayer de le virer, mais si on veut les laisser inline c'est un peu lourdo... */
static inline void
setPixelRGB (PluginInfo * goomInfo, Pixel * buffer, Uint x, Uint y, Color c)
{
  Pixel i;
61

62 63 64 65 66
  i.channels.b = c.b;
  i.channels.g = c.v;
  i.channels.r = c.r;
  *(buffer + (x + y * goomInfo->screen.width)) = i;
}
67

68 69 70 71 72 73 74
static inline void
setPixelRGB_ (Pixel * buffer, Uint x, Color c)
{
  buffer[x].channels.r = c.r;
  buffer[x].channels.g = c.v;
  buffer[x].channels.b = c.b;
}
75

76 77 78 79
static inline void
getPixelRGB_ (Pixel * buffer, Uint x, Color * c)
{
  Pixel i = *(buffer + x);
80

81 82 83 84
  c->b = i.channels.b;
  c->v = i.channels.g;
  c->r = i.channels.r;
}
85

86
/* END TODO */
87 88


89 90 91 92 93 94 95 96
/* DEPRECATED */
// retourne x>>s , en testant le signe de x
//#define ShiftRight(_x,_s) (((_x)<0) ? -(-(_x)>>(_s)) : ((_x)>>(_s)))
//#define EFFECT_DISTORS 4
//#define EFFECT_DISTORS_SL 2
//#define INTERLACE_ADD 9
//#define INTERLACE_AND 0xf
/* END DEPRECATED */
97

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
#define BUFFPOINTNB 16
#define BUFFPOINTNBF 16.0f
#define BUFFPOINTMASK 0xffff

#define sqrtperte 16
/* faire : a % sqrtperte <=> a & pertemask */
#define PERTEMASK 0xf
/* faire : a / sqrtperte <=> a >> PERTEDEC */
#define PERTEDEC 4

/* pure c version of the zoom filter */
static void c_zoom (Pixel * expix1, Pixel * expix2, unsigned int prevX,
    unsigned int prevY, signed int *brutS, signed int *brutD, int buffratio,
    int precalCoef[BUFFPOINTNB][BUFFPOINTNB]);

/* simple wrapper to give it the same proto than the others */
void
zoom_filter_c (int sizeX, int sizeY, Pixel * src, Pixel * dest, int *brutS,
    int *brutD, int buffratio, int precalCoef[16][16])
117
{
118
  c_zoom (src, dest, sizeX, sizeY, brutS, brutD, buffratio, precalCoef);
119 120
}

121 122 123 124
static void generatePrecalCoef (int precalCoef[BUFFPOINTNB][BUFFPOINTNB]);


typedef struct _ZOOM_FILTER_FX_WRAPPER_DATA
125
{
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

  PluginParam enabled_bp;
  PluginParameters params;

  unsigned int *coeffs, *freecoeffs;

  signed int *brutS, *freebrutS;        /* source */
  signed int *brutD, *freebrutD;        /* dest */
  signed int *brutT, *freebrutT;        /* temp (en cours de generation) */

  guint32 zoom_width;

  unsigned int prevX, prevY;

  float general_speed;
  int reverse;                  /* reverse the speed */
  char theMode;
  int waveEffect;
  int hypercosEffect;
  int vPlaneEffect;
  int hPlaneEffect;
  char noisify;
148
  int middleX, middleY;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
149

150 151
  int mustInitBuffers;
  int interlace_start;
152

153 154 155
    /** modif by jeko : fixedpoint : buffration = (16:16) (donc 0<=buffration<=2^16) */
  int buffratio;
  int *firedec;
156

157 158
    /** modif d'optim by Jeko : precalcul des 4 coefs resultant des 2 pos */
  int precalCoef[BUFFPOINTNB][BUFFPOINTNB];
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
159

160 161 162
    /** calculatePXandPY statics */
  int wave;
  int wavesp;
163

164
} ZoomFilterFXWrapperData;
165

166 167 168



169 170
static inline void
zoomVector (v2g * vecteur, ZoomFilterFXWrapperData * data, float X, float Y)
171
{
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  float vx, vy;
  float sq_dist = X * X + Y * Y;

  /*    sx = (X < 0.0f) ? -1.0f : 1.0f;
     sy = (Y < 0.0f) ? -1.0f : 1.0f;
   */
  float coefVitesse = (1.0f + data->general_speed) / 50.0f;

  // Effects

  /* Centralized FX */

  switch (data->theMode) {
    case CRYSTAL_BALL_MODE:
      coefVitesse -= (sq_dist - 0.3f) / 15.0f;
      break;
    case AMULETTE_MODE:
      coefVitesse += sq_dist * 3.5f;
      break;
    case WAVE_MODE:
      coefVitesse += sin (sq_dist * 20.0f) / 100.0f;
      break;
    case SCRUNCH_MODE:
      coefVitesse += sq_dist / 10.0f;
      break;
      //case HYPERCOS1_MODE:
      break;
      //case HYPERCOS2_MODE:
      break;
      //case YONLY_MODE:
      break;
    case SPEEDWAY_MODE:
      coefVitesse *= 4.0f * Y;
      break;
    default:
      break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
208 209
  }

210 211 212 213
  if (coefVitesse < -2.01f)
    coefVitesse = -2.01f;
  if (coefVitesse > 2.01f)
    coefVitesse = 2.01f;
214

215 216
  vx = coefVitesse * X;
  vy = coefVitesse * Y;
217

218 219 220
  /* Amulette 2 */
  // vx = X * tan(dist);
  // vy = Y * tan(dist);
221

222 223 224 225 226 227
  /* Rotate */
  //vx = (X+Y)*0.1;
  //vy = (Y-X)*0.1;


  // Effects adds-on
228

229 230
  /* Noise */
  if (data->noisify) {
231 232
    vx += (((float) rand ()) / ((float) RAND_MAX) - 0.5f) / 50.0f;
    vy += (((float) rand ()) / ((float) RAND_MAX) - 0.5f) / 50.0f;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
233
  }
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

  /* Hypercos */
  if (data->hypercosEffect) {
    vx += sin (Y * 10.0f) / 120.0f;
    vy += sin (X * 10.0f) / 120.0f;
  }

  /* H Plane */
  if (data->hPlaneEffect)
    vx += Y * 0.0025f * data->hPlaneEffect;

  /* V Plane */
  if (data->vPlaneEffect)
    vy += X * 0.0025f * data->vPlaneEffect;

  /* TODO : Water Mode */
  //    if (data->waveEffect)

252 253
  vecteur->x = vx;
  vecteur->y = vy;
254 255 256
}


257 258 259 260 261 262 263 264 265
/*
 * Makes a stripe of a transform buffer (brutT)
 *
 * The transform is (in order) :
 * Translation (-data->middleX, -data->middleY)
 * Homothetie (Center : 0,0   Coeff : 2/data->prevX)
 */
static void
makeZoomBufferStripe (ZoomFilterFXWrapperData * data, int INTERLACE_INCR)
266
{
267 268
  // Position of the pixel to compute in pixmap coordinates
  Uint x, y;
269

270
  // Where (verticaly) to stop generating the buffer stripe
271
  int maxEnd;
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

  // Ratio from pixmap to normalized coordinates
  float ratio = 2.0f / ((float) data->prevX);

  // Ratio from normalized to virtual pixmap coordinates
  float inv_ratio = BUFFPOINTNBF / ratio;
  float min = ratio / BUFFPOINTNBF;

  // Y position of the pixel to compute in normalized coordinates
  float Y = ((float) (data->interlace_start - data->middleY)) * ratio;

  maxEnd = data->prevY;
  if (maxEnd > (data->interlace_start + INTERLACE_INCR))
    maxEnd = (data->interlace_start + INTERLACE_INCR);

  for (y = data->interlace_start;
      (y < data->prevY) && ((signed int) y < maxEnd); y++) {
    Uint premul_y_prevX = y * data->prevX * 2;
    float X = -((float) data->middleX) * ratio;

    for (x = 0; x < data->prevX; x++) {
293 294 295
      v2g vector;

      zoomVector (&vector, data, X, Y);
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

      /* Finish and avoid null displacement */
      if (fabs (vector.x) < min)
        vector.x = (vector.x < 0.0f) ? -min : min;
      if (fabs (vector.y) < min)
        vector.y = (vector.y < 0.0f) ? -min : min;

      data->brutT[premul_y_prevX] =
          ((int) ((X - vector.x) * inv_ratio) +
          ((int) (data->middleX * BUFFPOINTNB)));
      data->brutT[premul_y_prevX + 1] =
          ((int) ((Y - vector.y) * inv_ratio) +
          ((int) (data->middleY * BUFFPOINTNB)));
      premul_y_prevX += 2;
      X += ratio;
    }
    Y += ratio;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
313
  }
314 315 316
  data->interlace_start += INTERLACE_INCR;
  if (y >= data->prevY - 1)
    data->interlace_start = -1;
317 318
}

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443

/*
 * calculer px et py en fonction de x,y,middleX,middleY et theMode
 * px et py indique la nouvelle position (en sqrtperte ieme de pixel)
 * (valeur * 16)
 
 inline void calculatePXandPY (PluginInfo *goomInfo, ZoomFilterFXWrapperData *data, int x, int y, int *px, int *py)
 {
     if (data->theMode == WATER_MODE) {
         int yy;
         
         yy = y + goom_irand(goomInfo->gRandom, 4) - goom_irand(goomInfo->gRandom, 4) + data->wave / 10;
         if (yy < 0)
             yy = 0;
         if (yy >= (signed int)goomInfo->screen.height)
             yy = goomInfo->screen.height - 1;
         
         *px = (x << 4) + data->firedec[yy] + (data->wave / 10);
         *py = (y << 4) + 132 - ((data->vitesse < 131) ? data->vitesse : 130);
         
         data->wavesp += goom_irand(goomInfo->gRandom, 3) - goom_irand(goomInfo->gRandom, 3);
         if (data->wave < -10)
             data->wavesp += 2;
         if (data->wave > 10)
             data->wavesp -= 2;
         data->wave += (data->wavesp / 10) + goom_irand(goomInfo->gRandom, 3) - goom_irand(goomInfo->gRandom, 3);
         if (data->wavesp > 100)
             data->wavesp = (data->wavesp * 9) / 10;
     }
     else {
         int     dist = 0, vx9, vy9;
         int     vx, vy;
         int     ppx, ppy;
         int     fvitesse = data->vitesse << 4;
         
         if (data->noisify) {
             x += goom_irand(goomInfo->gRandom, data->noisify) - goom_irand(goomInfo->gRandom, data->noisify);
             y += goom_irand(goomInfo->gRandom, data->noisify) - goom_irand(goomInfo->gRandom, data->noisify);
         }
         vx = (x - data->middleX) << 9;
         vy = (y - data->middleY) << 9;
         
         if (data->hPlaneEffect)
             vx += data->hPlaneEffect * (y - data->middleY);
         
         if (data->vPlaneEffect)
             vy += data->vPlaneEffect * (x - data->middleX);
         
         if (data->waveEffect) {
             fvitesse *=
             1024 +
             ShiftRight (goomInfo->sintable
                         [(unsigned short) (dist * 0xffff + EFFECT_DISTORS)], 6);
             fvitesse /= 1024;
         }
         
         if (data->hypercosEffect) {
             vx += ShiftRight (goomInfo->sintable[(-vy + dist) & 0xffff], 1);
             vy += ShiftRight (goomInfo->sintable[(vx + dist) & 0xffff], 1);
         }
         
         vx9 = ShiftRight (vx, 9);
         vy9 = ShiftRight (vy, 9);
         dist = vx9 * vx9 + vy9 * vy9;
         
         switch (data->theMode) {
             case WAVE_MODE:
                 fvitesse *=
                 1024 +
                 ShiftRight (goomInfo->sintable
                             [(unsigned short) (dist * 0xffff * EFFECT_DISTORS)], 6);
                 fvitesse>>=10;
                 break;
             case CRYSTAL_BALL_MODE:
                 fvitesse += (dist >> (10-EFFECT_DISTORS_SL));
                 break;
             case AMULETTE_MODE:
                 fvitesse -= (dist >> (4 - EFFECT_DISTORS_SL));
                 break;
             case SCRUNCH_MODE:
                 fvitesse -= (dist >> (10 - EFFECT_DISTORS_SL));
                 break;
             case HYPERCOS1_MODE:
                 vx = vx + ShiftRight (goomInfo->sintable[(-vy + dist) & 0xffff], 1);
                 vy = vy + ShiftRight (goomInfo->sintable[(vx + dist) & 0xffff], 1);
                 break;
             case HYPERCOS2_MODE:
                 vx =
                 vx + ShiftRight (goomInfo->sintable[(-ShiftRight (vy, 1) + dist) & 0xffff], 0);
                 vy =
                     vy + ShiftRight (goomInfo->sintable[(ShiftRight (vx, 1) + dist) & 0xffff], 0);
                 fvitesse = 128 << 4;
                 break;
             case YONLY_MODE:
                 fvitesse *= 1024 + ShiftRight (goomInfo->sintable[vy & 0xffff], 6);
                 fvitesse >>= 10;
                 break;
             case SPEEDWAY_MODE:
                 fvitesse -= (ShiftRight(vy,10-EFFECT_DISTORS_SL));
                 break;
         }
         
         if (fvitesse < -3024)
             fvitesse = -3024;
         
         if (vx < 0)									// pb avec decalage sur nb negatif
             ppx = -(-(vx * fvitesse) >> 16);
         // 16 = 9 + 7 (7 = nb chiffre virgule de vitesse * (v = 128 => immobile)
         //    * * * * * 9 = nb chiffre virgule de vx) 
         else
             ppx = ((vx * fvitesse) >> 16);
         
         if (vy < 0)
             ppy = -(-(vy * fvitesse) >> 16);
         else
             ppy = ((vy * fvitesse) >> 16);
         
         *px = (data->middleX << 4) + ppx;
         *py = (data->middleY << 4) + ppy;
     }
 }
 */



444
static void
445 446
c_zoom (Pixel * expix1, Pixel * expix2, unsigned int prevX, unsigned int prevY,
    signed int *brutS, signed int *brutD, int buffratio, int precalCoef[16][16])
447
{
448 449
  int myPos, myPos2;
  Color couleur;
450

451
  unsigned int ax = (prevX - 1) << PERTEDEC, ay = (prevY - 1) << PERTEDEC;
452

453 454
  int bufsize = prevX * prevY * 2;
  int bufwidth = prevX;
455

456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
  expix1[0].val = expix1[prevX - 1].val = expix1[prevX * prevY - 1].val =
      expix1[prevX * prevY - prevX].val = 0;

  for (myPos = 0; myPos < bufsize; myPos += 2) {
    Color col1, col2, col3, col4;
    int c1, c2, c3, c4, px, py;
    int pos;
    int coeffs;

    int brutSmypos = brutS[myPos];

    myPos2 = myPos + 1;

    px = brutSmypos + (((brutD[myPos] -
                brutSmypos) * buffratio) >> BUFFPOINTNB);
    brutSmypos = brutS[myPos2];
    py = brutSmypos + (((brutD[myPos2] -
                brutSmypos) * buffratio) >> BUFFPOINTNB);

    if ((py >= ay) || (px >= ax)) {
      pos = coeffs = 0;
    } else {
      pos = ((px >> PERTEDEC) + prevX * (py >> PERTEDEC));
      /* coef en modulo 15 */
      coeffs = precalCoef[px & PERTEMASK][py & PERTEMASK];
481
    }
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
    getPixelRGB_ (expix1, pos, &col1);
    getPixelRGB_ (expix1, pos + 1, &col2);
    getPixelRGB_ (expix1, pos + bufwidth, &col3);
    getPixelRGB_ (expix1, pos + bufwidth + 1, &col4);

    c1 = coeffs;
    c2 = (c1 >> 8) & 0xFF;
    c3 = (c1 >> 16) & 0xFF;
    c4 = (c1 >> 24) & 0xFF;
    c1 = c1 & 0xff;

    couleur.r = col1.r * c1 + col2.r * c2 + col3.r * c3 + col4.r * c4;
    if (couleur.r > 5)
      couleur.r -= 5;
    couleur.r >>= 8;

    couleur.v = col1.v * c1 + col2.v * c2 + col3.v * c3 + col4.v * c4;
    if (couleur.v > 5)
      couleur.v -= 5;
    couleur.v >>= 8;

    couleur.b = col1.b * c1 + col2.b * c2 + col3.b * c3 + col4.b * c4;
    if (couleur.b > 5)
      couleur.b -= 5;
    couleur.b >>= 8;

    setPixelRGB_ (expix2, myPos >> 1, couleur);
509 510
  }
}
511

512 513 514 515
/** generate the water fx horizontal direction buffer */
static void
generateTheWaterFXHorizontalDirectionBuffer (PluginInfo * goomInfo,
    ZoomFilterFXWrapperData * data)
516
{
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555

  int loopv;
  int decc = goom_irand (goomInfo->gRandom, 8) - 4;
  int spdc = goom_irand (goomInfo->gRandom, 8) - 4;
  int accel = goom_irand (goomInfo->gRandom, 8) - 4;

  for (loopv = data->prevY; loopv != 0;) {

    loopv--;
    data->firedec[loopv] = decc;
    decc += spdc / 10;
    spdc +=
        goom_irand (goomInfo->gRandom, 3) - goom_irand (goomInfo->gRandom, 3);

    if (decc > 4)
      spdc -= 1;
    if (decc < -4)
      spdc += 1;

    if (spdc > 30)
      spdc = spdc - goom_irand (goomInfo->gRandom, 3) + accel / 10;
    if (spdc < -30)
      spdc = spdc + goom_irand (goomInfo->gRandom, 3) + accel / 10;

    if (decc > 8 && spdc > 1)
      spdc -= goom_irand (goomInfo->gRandom, 3) - 2;

    if (decc < -8 && spdc < -1)
      spdc += goom_irand (goomInfo->gRandom, 3) + 2;

    if (decc > 8 || decc < -8)
      decc = decc * 8 / 9;

    accel +=
        goom_irand (goomInfo->gRandom, 2) - goom_irand (goomInfo->gRandom, 2);
    if (accel > 20)
      accel -= 2;
    if (accel < -20)
      accel += 2;
556
  }
557
}
558

559 560 561 562 563 564 565 566 567 568 569 570 571 572 573


/**
* Main work for the dynamic displacement map.
 * 
 * Reads data from pix1, write to pix2.
 *
 * Useful datas for this FX are stored in ZoomFilterData.
 * 
 * If you think that this is a strange function name, let me say that a long time ago,
 *  there has been a slow version and a gray-level only one. Then came these function,
 *  fast and workin in RGB colorspace ! nice but it only was applying a zoom to the image.
 *  So that is why you have this name, for the nostalgy of the first days of goom
 *  when it was just a tiny program writen in Turbo Pascal on my i486...
 */
574
void
575 576
zoomFilterFastRGB (PluginInfo * goomInfo, Pixel * pix1, Pixel * pix2,
    ZoomFilterData * zf, Uint resx, Uint resy, int switchIncr, float switchMult)
577
{
578
  Uint x, y;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
579

580 581
  ZoomFilterFXWrapperData *data =
      (ZoomFilterFXWrapperData *) goomInfo->zoomFilter_fx.fx_data;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
582

583 584
  if (!BVAL (data->enabled_bp))
    return;
585

586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
    /** changement de taille **/
  if ((data->prevX != resx) || (data->prevY != resy)) {
    data->prevX = resx;
    data->prevY = resy;

    if (data->brutS)
      free (data->freebrutS);
    data->brutS = 0;
    if (data->brutD)
      free (data->freebrutD);
    data->brutD = 0;
    if (data->brutT)
      free (data->freebrutT);
    data->brutT = 0;

    data->middleX = resx / 2;
    data->middleY = resy / 2;
    data->mustInitBuffers = 1;
    if (data->firedec)
      free (data->firedec);
    data->firedec = 0;
  }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
608

609 610
  if (data->interlace_start != -2)
    zf = NULL;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
611

612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
    /** changement de config **/
  if (zf) {
    data->reverse = zf->reverse;
    data->general_speed = (float) (zf->vitesse - 128) / 128.0f;
    if (data->reverse)
      data->general_speed = -data->general_speed;
    data->middleX = zf->middleX;
    data->middleY = zf->middleY;
    data->theMode = zf->mode;
    data->hPlaneEffect = zf->hPlaneEffect;
    data->vPlaneEffect = zf->vPlaneEffect;
    data->waveEffect = zf->waveEffect;
    data->hypercosEffect = zf->hypercosEffect;
    data->noisify = zf->noisify;
    data->interlace_start = 0;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
627 628
  }

629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677

  if (data->mustInitBuffers) {

    data->mustInitBuffers = 0;
    data->freebrutS =
        (signed int *) calloc (resx * resy * 2 + 128, sizeof (unsigned int));
    data->brutS =
        (gint32 *) ((1 + ((uintptr_t) (data->freebrutS)) / 128) * 128);

    data->freebrutD =
        (signed int *) calloc (resx * resy * 2 + 128, sizeof (unsigned int));
    data->brutD =
        (gint32 *) ((1 + ((uintptr_t) (data->freebrutD)) / 128) * 128);

    data->freebrutT =
        (signed int *) calloc (resx * resy * 2 + 128, sizeof (unsigned int));
    data->brutT =
        (gint32 *) ((1 + ((uintptr_t) (data->freebrutT)) / 128) * 128);

    data->buffratio = 0;

    data->firedec = (int *) malloc (data->prevY * sizeof (int));
    generateTheWaterFXHorizontalDirectionBuffer (goomInfo, data);

    data->interlace_start = 0;
    makeZoomBufferStripe (data, resy);

    /* Copy the data from temp to dest and source */
    memcpy (data->brutS, data->brutT, resx * resy * 2 * sizeof (int));
    memcpy (data->brutD, data->brutT, resx * resy * 2 * sizeof (int));
  }

  /* generation du buffer de trans */
  if (data->interlace_start == -1) {

    /* sauvegarde de l'etat actuel dans la nouvelle source
     * TODO: write that in MMX (has been done in previous version, but did not follow some new fonctionnalities) */
    y = data->prevX * data->prevY * 2;
    for (x = 0; x < y; x += 2) {
      int brutSmypos = data->brutS[x];
      int x2 = x + 1;

      data->brutS[x] =
          brutSmypos + (((data->brutD[x] -
                  brutSmypos) * data->buffratio) >> BUFFPOINTNB);
      brutSmypos = data->brutS[x2];
      data->brutS[x2] =
          brutSmypos + (((data->brutD[x2] -
                  brutSmypos) * data->buffratio) >> BUFFPOINTNB);
678
    }
679
    data->buffratio = 0;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
680
  }
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707

  if (data->interlace_start == -1) {
    signed int *tmp;

    tmp = data->brutD;
    data->brutD = data->brutT;
    data->brutT = tmp;
    tmp = data->freebrutD;
    data->freebrutD = data->freebrutT;
    data->freebrutT = tmp;
    data->interlace_start = -2;
  }

  if (data->interlace_start >= 0) {
    /* creation de la nouvelle destination */
    makeZoomBufferStripe (data, resy / 16);
  }

  if (switchIncr != 0) {
    data->buffratio += switchIncr;
    if (data->buffratio > BUFFPOINTMASK)
      data->buffratio = BUFFPOINTMASK;
  }

  if (switchMult != 1.0f) {
    data->buffratio = (int) ((float) BUFFPOINTMASK * (1.0f - switchMult) +
        (float) data->buffratio * switchMult);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
708
  }
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733

  data->zoom_width = data->prevX;

  goomInfo->methods.zoom_filter (data->prevX, data->prevY, pix1, pix2,
      data->brutS, data->brutD, data->buffratio, data->precalCoef);
}

static void
generatePrecalCoef (int precalCoef[16][16])
{
  int coefh, coefv;

  for (coefh = 0; coefh < 16; coefh++) {
    for (coefv = 0; coefv < 16; coefv++) {

      int i;
      int diffcoeffh;
      int diffcoeffv;

      diffcoeffh = sqrtperte - coefh;
      diffcoeffv = sqrtperte - coefv;

      if (!(coefh || coefv)) {
        i = 255;
      } else {
734
        Uint i1, i2, i3, i4;
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754

        i1 = diffcoeffh * diffcoeffv;
        i2 = coefh * diffcoeffv;
        i3 = diffcoeffh * coefv;
        i4 = coefh * coefv;

        // TODO: faire mieux...
        if (i1)
          i1--;
        if (i2)
          i2--;
        if (i3)
          i3--;
        if (i4)
          i4--;

        i = (i1) | (i2 << 8) | (i3 << 16) | (i4 << 24);
      }
      precalCoef[coefh][coefv] = i;
    }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
755
  }
756 757
}

758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
/* VisualFX Wrapper */

static void
zoomFilterVisualFXWrapper_init (struct _VISUAL_FX *_this, PluginInfo * info)
{
  ZoomFilterFXWrapperData *data =
      (ZoomFilterFXWrapperData *) malloc (sizeof (ZoomFilterFXWrapperData));

  data->coeffs = 0;
  data->freecoeffs = 0;
  data->brutS = 0;
  data->freebrutS = 0;
  data->brutD = 0;
  data->freebrutD = 0;
  data->brutT = 0;
  data->freebrutT = 0;
  data->prevX = 0;
  data->prevY = 0;

  data->mustInitBuffers = 1;
  data->interlace_start = -2;

  data->general_speed = 0.0f;
  data->reverse = 0;
  data->theMode = AMULETTE_MODE;
  data->waveEffect = 0;
  data->hypercosEffect = 0;
  data->vPlaneEffect = 0;
  data->hPlaneEffect = 0;
  data->noisify = 2;

    /** modif by jeko : fixedpoint : buffration = (16:16) (donc 0<=buffration<=2^16) */
  data->buffratio = 0;
  data->firedec = 0;

  data->wave = data->wavesp = 0;

795
  secure_b_param (&data->enabled_bp, "Enabled", 1);
796

797
  plugin_parameters (&data->params, "Zoom Filter", 1);
798 799 800 801 802 803 804 805 806 807 808 809
  data->params.params[0] = &data->enabled_bp;

  _this->params = &data->params;
  _this->fx_data = (void *) data;

    /** modif d'optim by Jeko : precalcul des 4 coefs resultant des 2 pos */
  generatePrecalCoef (data->precalCoef);
}

static void
zoomFilterVisualFXWrapper_free (struct _VISUAL_FX *_this)
{
810 811 812 813 814 815 816 817 818 819 820 821 822
  ZoomFilterFXWrapperData *data = (ZoomFilterFXWrapperData *) _this->fx_data;

  if (data->freebrutT)
    free (data->freebrutT);
  if (data->freebrutS)
    free (data->freebrutS);
  if (data->freebrutD)
    free (data->freebrutD);
  if (data->firedec)
    free (data->firedec);

  goom_plugin_parameters_free (_this->params);

823 824 825 826 827 828 829 830 831
  free (_this->fx_data);
}

static void
zoomFilterVisualFXWrapper_apply (struct _VISUAL_FX *_this, Pixel * src,
    Pixel * dest, PluginInfo * info)
{
}

832 833
void
zoomFilterVisualFXWrapper_create (VisualFX * fx)
834
{
835 836 837 838 839
  fx->init = zoomFilterVisualFXWrapper_init;
  fx->free = zoomFilterVisualFXWrapper_free;
  fx->apply = zoomFilterVisualFXWrapper_apply;
  fx->params = NULL;
  fx->fx_data = NULL;
840 841 842 843
}


/* TODO : MOVE THIS AWAY */
844

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
845
void
846 847
pointFilter (PluginInfo * goomInfo, Pixel * pix1, Color c, float t1, float t2,
    float t3, float t4, Uint cycle)
848
{
849 850 851 852 853 854 855 856 857 858 859 860
  Uint x = (Uint) ((int) (goomInfo->screen.width / 2)
      + (int) (t1 * cos ((float) cycle / t3)));
  Uint y = (Uint) ((int) (goomInfo->screen.height / 2)
      + (int) (t2 * sin ((float) cycle / t4)));

  if ((x > 1) && (y > 1) && (x < goomInfo->screen.width - 2)
      && (y < goomInfo->screen.height - 2)) {
    setPixelRGB (goomInfo, pix1, x + 1, y, c);
    setPixelRGB (goomInfo, pix1, x, y + 1, c);
    setPixelRGB (goomInfo, pix1, x + 1, y + 1, WHITE);
    setPixelRGB (goomInfo, pix1, x + 2, y + 1, c);
    setPixelRGB (goomInfo, pix1, x + 1, y + 2, c);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
861
  }
862
}