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.
 
 
 
 
 
 

103 lines
2.6 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. #define BORDER 64
  25. pipi_image_t *pipi_wave(pipi_image_t *src, double dw, double dh,
  26. double d, double a)
  27. {
  28. pipi_image_t *dst;
  29. pipi_pixels_t *srcp, *dstp;
  30. float *srcdata, *dstdata;
  31. double sina, cosa;
  32. int x, y, w, h, i, gray;
  33. w = src->w;
  34. h = src->h;
  35. gray = (src->last_modified == PIPI_PIXELS_Y_F32);
  36. srcp = gray ? pipi_get_pixels(src, PIPI_PIXELS_Y_F32)
  37. : pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
  38. srcdata = (float *)srcp->pixels;
  39. dst = pipi_new(w, h);
  40. dstp = gray ? pipi_get_pixels(dst, PIPI_PIXELS_Y_F32)
  41. : pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
  42. dstdata = (float *)dstp->pixels;
  43. sina = sin(a);
  44. cosa = cos(a);
  45. for(y = 0; y < h; y++)
  46. {
  47. for(x = 0; x < w; x++)
  48. {
  49. double angle = 2 * M_PI / dw * ((x - w / 2) * cosa
  50. + (y - h / 2) * sina - d);
  51. double displacement = dh * sin(angle);
  52. double dx, dy;
  53. int x2, y2;
  54. dx = -sina * displacement;
  55. dy = cosa * displacement;
  56. if(x < BORDER) dx = dx * x / BORDER;
  57. if(x > w - 1 - BORDER) dx = dx * (w - 1 - x) / BORDER;
  58. if(y < BORDER) dy = dy * y / BORDER;
  59. if(y > h - 1 - BORDER) dy = dy * (h - 1 - y) / BORDER;
  60. x2 = x + dx;
  61. y2 = y + dy;
  62. /* Just in case... */
  63. if(x2 < 0) x2 = 0;
  64. else if(x2 >= w) x2 = w - 1;
  65. if(y2 < 0) y2 = 0;
  66. else if(y2 >= h) y2 = h - 1;
  67. if(gray)
  68. {
  69. dstdata[y * w + x] = srcdata[y2 * w + x2];
  70. }
  71. else
  72. {
  73. for(i = 0; i < 4; i++)
  74. {
  75. dstdata[4 * (y * w + x) + i]
  76. = srcdata[4 * (y2 * w + x2) + i];
  77. }
  78. }
  79. }
  80. }
  81. return dst;
  82. }