filters.c 13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
/* filter.c version 0.7
 * contient les filtres applicable a un buffer
 * creation : 01/10/2000
 *  -ajout de sinFilter()
 *  -ajout de zoomFilter()
 *  -copie de zoomFilter() en zoomFilterRGB(), grant les 3 couleurs
 *  -optimisation de sinFilter (utilisant une table de sin)
 *      -asm
 *      -optimisation de la procedure de gnration du buffer de transformation
 *              la vitesse est maintenant comprise dans [0..128] au lieu de [0..100]
*/

/*#define _DEBUG_PIXEL; */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "filters.h"
#include "graphic.h"
#include "goom_tools.h"
#include "goom_core.h"
#include <stdlib.h>
#include <math.h>
#include <stdio.h>

#ifdef MMX
#define USE_ASM
#endif
#ifdef POWERPC
#define USE_ASM
#endif

#ifdef USE_ASM
#define EFFECT_DISTORS 4
#else
#define EFFECT_DISTORS 10
#endif


#ifdef USE_ASM

#ifdef MMX
int mmx_zoom ();
guint32 mmx_zoom_size;
#endif /* MMX */

#ifdef POWERPC
extern unsigned int useAltivec;
extern void ppc_zoom (void);
extern void ppc_zoom_altivec (void);
unsigned int ppcsize4;
#endif /* PowerPC */


unsigned int *coeffs = 0, *freecoeffs = 0;
guint32 *expix1 = 0;            /* pointeur exporte vers p1 */
guint32 *expix2 = 0;            /* pointeur exporte vers p2 */
guint32 zoom_width;
#endif /* ASM */


static int firstTime = 1;
static int sintable[0xffff];

ZoomFilterData *
67
zoomFilterNew (void)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
{
  ZoomFilterData *zf = malloc (sizeof (ZoomFilterData));

  zf->vitesse = 128;
  zf->pertedec = 8;
  zf->sqrtperte = 16;
  zf->middleX = 1;
  zf->middleY = 1;
  zf->reverse = 0;
  zf->mode = WAVE_MODE;
  zf->hPlaneEffect = 0;
  zf->vPlaneEffect = 0;
  zf->noisify = 0;
  zf->buffsize = 0;
  zf->res_x = 0;
  zf->res_y = 0;

  zf->buffer = NULL;
  zf->firedec = NULL;

  zf->wave = 0;
  zf->wavesp = 0;

  return zf;
}

/* retourne x>>s , en testant le signe de x */
static inline int
ShiftRight (int x, const unsigned char s)
{
  if (x < 0)
    return -(-x >> s);
  else
    return x >> s;
}

/*
  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)
*/
109
static void
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 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 444 445 446 447 448 449 450 451 452 453 454 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 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 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
calculatePXandPY (GoomData * gd, int x, int y, int *px, int *py)
{
  ZoomFilterData *zf = gd->zfd;
  int middleX, middleY;
  guint32 resoly = zf->res_y;
  int vPlaneEffect = zf->vPlaneEffect;
  int hPlaneEffect = zf->hPlaneEffect;
  int vitesse = zf->vitesse;
  char theMode = zf->mode;

  if (theMode == WATER_MODE) {
    int wavesp = zf->wavesp;
    int wave = zf->wave;
    int yy = y + RAND (gd) % 4 + wave / 10;

    yy -= RAND (gd) % 4;
    if (yy < 0)
      yy = 0;
    if (yy >= resoly)
      yy = resoly - 1;

    *px = (x << 4) + zf->firedec[yy] + (wave / 10);
    *py = (y << 4) + 132 - ((vitesse < 132) ? vitesse : 131);

    wavesp += RAND (gd) % 3;
    wavesp -= RAND (gd) % 3;
    if (wave < -10)
      wavesp += 2;
    if (wave > 10)
      wavesp -= 2;
    wave += (wavesp / 10) + RAND (gd) % 3;
    wave -= RAND (gd) % 3;
    if (wavesp > 100)
      wavesp = (wavesp * 9) / 10;

    zf->wavesp = wavesp;
    zf->wave = wave;
  } else {
    int dist;
    register int vx, vy;
    int fvitesse = vitesse << 4;

    middleX = zf->middleX;
    middleY = zf->middleY;

    if (zf->noisify) {
      x += RAND (gd) % zf->noisify;
      x -= RAND (gd) % zf->noisify;
      y += RAND (gd) % zf->noisify;
      y -= RAND (gd) % zf->noisify;
    }

    if (hPlaneEffect)
      vx = ((x - middleX) << 9) + hPlaneEffect * (y - middleY);
    else
      vx = (x - middleX) << 9;

    if (vPlaneEffect)
      vy = ((y - middleY) << 9) + vPlaneEffect * (x - middleX);
    else
      vy = (y - middleY) << 9;

    switch (theMode) {
      case WAVE_MODE:
        dist =
            ShiftRight (vx, 9) * ShiftRight (vx, 9) + ShiftRight (vy,
            9) * ShiftRight (vy, 9);
        fvitesse *=
            1024 +
            ShiftRight (sintable[(unsigned short) (0xffff * dist *
                    EFFECT_DISTORS)], 6);
        fvitesse /= 1024;
        break;
      case CRYSTAL_BALL_MODE:
        dist =
            ShiftRight (vx, 9) * ShiftRight (vx, 9) + ShiftRight (vy,
            9) * ShiftRight (vy, 9);
        fvitesse += (dist * EFFECT_DISTORS >> 10);
        break;
      case AMULETTE_MODE:
        dist =
            ShiftRight (vx, 9) * ShiftRight (vx, 9) + ShiftRight (vy,
            9) * ShiftRight (vy, 9);
        fvitesse -= (dist * EFFECT_DISTORS >> 4);
        break;
      case SCRUNCH_MODE:
        dist =
            ShiftRight (vx, 9) * ShiftRight (vx, 9) + ShiftRight (vy,
            9) * ShiftRight (vy, 9);
        fvitesse -= (dist * EFFECT_DISTORS >> 9);
        break;
    }
    if (vx < 0)
      *px = (middleX << 4) - (-(vx * fvitesse) >> 16);
    else
      *px = (middleX << 4) + ((vx * fvitesse) >> 16);
    if (vy < 0)
      *py = (middleY << 4) - (-(vy * fvitesse) >> 16);
    else
      *py = (middleY << 4) + ((vy * fvitesse) >> 16);
  }
}

/*#define _DEBUG */

static inline void
setPixelRGB (Uint * buffer, Uint x, Uint y, Color c,
    guint32 resolx, guint32 resoly)
{
/*              buffer[ y*WIDTH + x ] = (c.r<<16)|(c.v<<8)|c.b */
#ifdef _DEBUG_PIXEL
  if (x + y * resolx >= resolx * resoly) {
    fprintf (stderr, "setPixel ERROR : hors du tableau... %i, %i\n", x, y);
    /*exit (1) ; */
  }
#endif

#ifdef USE_DGA
  buffer[y * resolx + x] = (c.b << 16) | (c.v << 8) | c.r;
#else
  buffer[y * resolx + x] = (c.r << 16) | (c.v << 8) | c.b;
#endif
}


static inline void
setPixelRGB_ (Uint * buffer, Uint x, Color c, guint32 resolx, guint32 resoly)
{
#ifdef _DEBUG
  if (x >= resolx * resoly) {
    printf ("setPixel ERROR : hors du tableau... %i >= %i*%i (%i)\n", x, resolx,
        resoly, resolx * resoly);
    exit (1);
  }
#endif

#ifdef USE_DGA
  buffer[x] = (c.b << 16) | (c.v << 8) | c.r;
#else
  buffer[x] = (c.r << 16) | (c.v << 8) | c.b;
#endif
}

static inline void
getPixelRGB_ (Uint * buffer, Uint x, Color * c, guint32 resolx, guint32 resoly)
{
  register unsigned char *tmp8;

#ifdef _DEBUG
  if (x >= resolx * resoly) {
    printf ("getPixel ERROR : hors du tableau... %i\n", x);
    exit (1);
  }
#endif

#ifdef __BIG_ENDIAN__
  c->b = *(unsigned char *) (tmp8 = (unsigned char *) (buffer + x));
  c->r = *(unsigned char *) (++tmp8);
  c->v = *(unsigned char *) (++tmp8);
  c->b = *(unsigned char *) (++tmp8);

#else
  /* ATTENTION AU PETIT INDIEN  */
  tmp8 = (unsigned char *) (buffer + x);
  c->b = *(unsigned char *) (tmp8++);
  c->v = *(unsigned char *) (tmp8++);
  c->r = *(unsigned char *) (tmp8);
/*      *c = (Color) buffer[x+y*WIDTH] ; */
#endif
}

static void
zoomFilterSetResolution (GoomData * gd, ZoomFilterData * zf)
{
  unsigned short us;

  if (zf->buffsize >= gd->buffsize) {
    zf->res_x = gd->resolx;
    zf->res_y = gd->resoly;
    zf->middleX = gd->resolx / 2;
    zf->middleY = gd->resoly - 1;

    return;
  }
#ifndef USE_ASM
  if (zf->buffer)
    free (zf->buffer);
  zf->buffer = 0;
#else
  if (coeffs)
    free (freecoeffs);
  coeffs = 0;
#endif
  zf->middleX = gd->resolx / 2;
  zf->middleY = gd->resoly - 1;
  zf->res_x = gd->resolx;
  zf->res_y = gd->resoly;

  if (zf->firedec)
    free (zf->firedec);
  zf->firedec = 0;

  zf->buffsize = gd->resolx * gd->resoly * sizeof (unsigned int);

#ifdef USE_ASM
  freecoeffs = (unsigned int *)
      malloc (resx * resy * 2 * sizeof (unsigned int) + 128);
  coeffs = (guint32 *) ((1 + ((unsigned int) (freecoeffs)) / 128) * 128);

#else
  zf->buffer = calloc (sizeof (guint32), zf->buffsize * 5);
  zf->pos10 = zf->buffer;
  zf->c[0] = zf->pos10 + zf->buffsize;
  zf->c[1] = zf->c[0] + zf->buffsize;
  zf->c[2] = zf->c[1] + zf->buffsize;
  zf->c[3] = zf->c[2] + zf->buffsize;
#endif
  zf->firedec = (int *) malloc (zf->res_y * sizeof (int));

  if (firstTime) {
    firstTime = 0;

    /* generation d'une table de sinus */
    for (us = 0; us < 0xffff; us++) {
      sintable[us] = (int) (1024.0f * sin (us * 2 * 3.31415f / 0xffff));
    }
  }
}

void
zoomFilterDestroy (ZoomFilterData * zf)
{
  if (zf) {
    if (zf->firedec)
      free (zf->firedec);
    if (zf->buffer)
      free (zf->buffer);
    free (zf);
  }
}

/*===============================================================*/
void
zoomFilterFastRGB (GoomData * goomdata, ZoomFilterData * zf, int zfd_update)
{
  guint32 prevX = goomdata->resolx;
  guint32 prevY = goomdata->resoly;

  guint32 *pix1 = goomdata->p1;
  guint32 *pix2 = goomdata->p2;
  unsigned int *pos10;
  unsigned int **c;

  Uint x, y;

/*  static unsigned int prevX = 0, prevY = 0; */

#ifdef USE_ASM
  expix1 = pix1;
  expix2 = pix2;
#else
  Color couleur;
  Color col1, col2, col3, col4;
  Uint position;
#endif

  if ((goomdata->resolx != zf->res_x) || (goomdata->resoly != zf->res_y)) {
    zoomFilterSetResolution (goomdata, zf);
  }

  pos10 = zf->pos10;
  c = zf->c;

  if (zfd_update) {
    guchar sqrtperte = zf->sqrtperte;
    gint start_y = 0;

    if (zf->reverse)
      zf->vitesse = 256 - zf->vitesse;

    /* generation du buffer */
    for (y = 0; y < zf->res_y; y++) {
      gint y_16 = y << 4;
      gint max_px = (prevX - 1) * sqrtperte;
      gint max_py = (prevY - 1) * sqrtperte;

      for (x = 0; x < zf->res_x; x++) {
        gint px, py;
        guchar coefv, coefh;

        /* calculer px et py en fonction de */
        /*   x,y,middleX,middleY et theMode */
        calculatePXandPY (goomdata, x, y, &px, &py);

        if ((px == x << 4) && (py == y_16))
          py += 8;

        if ((py < 0) || (px < 0) || (py >= max_py) || (px >= max_px)) {
#ifdef USE_ASM
          coeffs[(y * prevX + x) * 2] = 0;
          coeffs[(y * prevX + x) * 2 + 1] = 0;
#else
          pos10[start_y + x] = 0;
          c[0][start_y + x] = 0;
          c[1][start_y + x] = 0;
          c[2][start_y + x] = 0;
          c[3][start_y + x] = 0;
#endif
        } else {
          int npx10;
          int npy10;
          int pos;

          npx10 = (px / sqrtperte);
          npy10 = (py / sqrtperte);

/*                        if (npx10 >= prevX) fprintf(stderr,"error npx:%d",npx10);
                          if (npy10 >= prevY) fprintf(stderr,"error npy:%d",npy10);
*/
          coefh = px % sqrtperte;
          coefv = py % sqrtperte;
#ifdef USE_ASM
          pos = (y * prevX + x) * 2;
          coeffs[pos] = (npx10 + prevX * npy10) * 4;

          if (!(coefh || coefv))
            coeffs[pos + 1] = (sqrtperte * sqrtperte - 1);
          else
            coeffs[pos + 1] = ((sqrtperte - coefh) * (sqrtperte - coefv));

          coeffs[pos + 1] |= (coefh * (sqrtperte - coefv)) << 8;
          coeffs[pos + 1] |= ((sqrtperte - coefh) * coefv) << 16;
          coeffs[pos + 1] |= (coefh * coefv) << 24;
#else
          pos = start_y + x;
          pos10[pos] = npx10 + prevX * npy10;

          if (!(coefh || coefv))
            c[0][pos] = sqrtperte * sqrtperte - 1;
          else
            c[0][pos] = (sqrtperte - coefh) * (sqrtperte - coefv);

          c[1][pos] = coefh * (sqrtperte - coefv);
          c[2][pos] = (sqrtperte - coefh) * coefv;
          c[3][pos] = coefh * coefv;
#endif
        }
      }
      /* Advance start of line index */
      start_y += prevX;
    }
  }
#ifdef USE_ASM
#ifdef MMX
  zoom_width = prevX;
  mmx_zoom_size = prevX * prevY;
  mmx_zoom ();
#endif

#ifdef POWERPC
  zoom_width = prevX;
  if (useAltivec) {
    ppcsize4 = ((unsigned int) (prevX * prevY)) / 4;
    ppc_zoom_altivec ();
  } else {
    ppcsize4 = ((unsigned int) (prevX * prevY));
    ppc_zoom ();
  }
#endif
#else
  for (position = 0; position < prevX * prevY; position++) {
    getPixelRGB_ (pix1, pos10[position], &col1, goomdata->resolx,
        goomdata->resoly);
    getPixelRGB_ (pix1, pos10[position] + 1, &col2, goomdata->resolx,
        goomdata->resoly);
    getPixelRGB_ (pix1, pos10[position] + prevX, &col3, goomdata->resolx,
        goomdata->resoly);
    getPixelRGB_ (pix1, pos10[position] + prevX + 1, &col4, goomdata->resolx,
        goomdata->resoly);

    couleur.r = col1.r * c[0][position]
        + col2.r * c[1][position]
        + col3.r * c[2][position]
        + col4.r * c[3][position];
    couleur.r >>= zf->pertedec;

    couleur.v = col1.v * c[0][position]
        + col2.v * c[1][position]
        + col3.v * c[2][position]
        + col4.v * c[3][position];
    couleur.v >>= zf->pertedec;

    couleur.b = col1.b * c[0][position]
        + col2.b * c[1][position]
        + col3.b * c[2][position]
        + col4.b * c[3][position];
    couleur.b >>= zf->pertedec;

    setPixelRGB_ (pix2, position, couleur, goomdata->resolx, goomdata->resoly);
  }
#endif
}


void
pointFilter (GoomData * goomdata, Color c,
    float t1, float t2, float t3, float t4, Uint cycle)
{
  Uint *pix1 = goomdata->p1;
  ZoomFilterData *zf = goomdata->zfd;
  Uint x = (Uint) (zf->middleX + (int) (t1 * cos ((float) cycle / t3)));
  Uint y = (Uint) (zf->middleY + (int) (t2 * sin ((float) cycle / t4)));

  if ((x > 1) && (y > 1) && (x < goomdata->resolx - 2)
      && (y < goomdata->resoly - 2)) {
    setPixelRGB (pix1, x + 1, y, c, goomdata->resolx, goomdata->resoly);
    setPixelRGB (pix1, x, y + 1, c, goomdata->resolx, goomdata->resoly);
    setPixelRGB (pix1, x + 1, y + 1, WHITE, goomdata->resolx, goomdata->resoly);
    setPixelRGB (pix1, x + 2, y + 1, c, goomdata->resolx, goomdata->resoly);
    setPixelRGB (pix1, x + 1, y + 2, c, goomdata->resolx, goomdata->resoly);
  }
}