You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

91 lines
2.2 KiB

  1. /*
  2. * libpipi Pathetic image processing interface library
  3. * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * wave.c: wave and warping effects
  16. */
  17. #include "config.h"
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include "pipi.h"
  23. #include "pipi_internals.h"
  24. pipi_image_t *pipi_wave(pipi_image_t *src, double freq, double phase,
  25. double theta, double xamp, double yamp)
  26. {
  27. pipi_image_t *dst;
  28. pipi_pixels_t *srcp, *dstp;
  29. float *srcdata, *dstdata;
  30. double sint, cost;
  31. int x, y, w, h, i, gray;
  32. w = src->w;
  33. h = src->h;
  34. gray = (src->last_modified == PIPI_PIXELS_Y_F32);
  35. srcp = gray ? pipi_get_pixels(src, PIPI_PIXELS_Y_F32)
  36. : pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
  37. srcdata = (float *)srcp->pixels;
  38. dst = pipi_new(w, h);
  39. dstp = gray ? pipi_get_pixels(dst, PIPI_PIXELS_Y_F32)
  40. : pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
  41. dstdata = (float *)dstp->pixels;
  42. sint = sin(theta);
  43. cost = cos(theta);
  44. for(y = 0; y < h; y++)
  45. {
  46. for(x = 0; x < w; x++)
  47. {
  48. double t = cost * (x - w / 2) + sint * (y - h / 2);
  49. double step = sin(t * freq + phase);
  50. double dx = xamp * step;
  51. double dy = yamp * step;
  52. int x2 = x + dx;
  53. int y2 = y + dy;
  54. if(x2 < 0) x2 = 0;
  55. else if(x2 >= w) x2 = w - 1;
  56. if(y2 < 0) y2 = 0;
  57. else if(y2 >= h) y2 = h - 1;
  58. if(gray)
  59. {
  60. dstdata[y * w + x] = srcdata[y2 * w + x2];
  61. }
  62. else
  63. {
  64. for(i = 0; i < 4; i++)
  65. {
  66. dstdata[4 * (y * w + x) + i]
  67. = srcdata[4 * (y2 * w + x2) + i];
  68. }
  69. }
  70. }
  71. }
  72. return dst;
  73. }