選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

178 行
4.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 other warping effects
  16. */
  17. #include "config.h"
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #ifndef M_PI
  23. # define M_PI 3.14159265358979323846
  24. #endif
  25. #include "pipi.h"
  26. #include "pipi_internals.h"
  27. #define BORDER 64
  28. pipi_image_t *pipi_wave(pipi_image_t *src, double dw, double dh,
  29. double d, double a)
  30. {
  31. pipi_image_t *dst;
  32. pipi_pixels_t *srcp, *dstp;
  33. float *srcdata, *dstdata;
  34. double sina, cosa;
  35. int x, y, w, h, i, gray;
  36. w = src->w;
  37. h = src->h;
  38. gray = (src->last_modified == PIPI_PIXELS_Y_F32);
  39. srcp = gray ? pipi_get_pixels(src, PIPI_PIXELS_Y_F32)
  40. : pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
  41. srcdata = (float *)srcp->pixels;
  42. dst = pipi_new(w, h);
  43. dstp = gray ? pipi_get_pixels(dst, PIPI_PIXELS_Y_F32)
  44. : pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
  45. dstdata = (float *)dstp->pixels;
  46. sina = sin(a);
  47. cosa = cos(a);
  48. for(y = 0; y < h; y++)
  49. {
  50. for(x = 0; x < w; x++)
  51. {
  52. double angle = 2 * M_PI / dw * ((x - w / 2) * cosa
  53. + (y - h / 2) * sina - d);
  54. double displacement = dh * sin(angle);
  55. double dx, dy;
  56. int x2, y2;
  57. dx = cosa * displacement;
  58. dy = sina * displacement;
  59. if(x < BORDER) dx = dx * x / BORDER;
  60. if(x > w - 1 - BORDER) dx = dx * (w - 1 - x) / BORDER;
  61. if(y < BORDER) dy = dy * y / BORDER;
  62. if(y > h - 1 - BORDER) dy = dy * (h - 1 - y) / BORDER;
  63. x2 = x + dx;
  64. y2 = y + dy;
  65. /* Just in case... */
  66. if(x2 < 0) x2 = 0;
  67. else if(x2 >= w) x2 = w - 1;
  68. if(y2 < 0) y2 = 0;
  69. else if(y2 >= h) y2 = h - 1;
  70. if(gray)
  71. {
  72. dstdata[y * w + x] = srcdata[y2 * w + x2];
  73. }
  74. else
  75. {
  76. for(i = 0; i < 4; i++)
  77. {
  78. dstdata[4 * (y * w + x) + i]
  79. = srcdata[4 * (y2 * w + x2) + i];
  80. }
  81. }
  82. }
  83. }
  84. return dst;
  85. }
  86. pipi_image_t *pipi_sine(pipi_image_t *src, double dw, double dh,
  87. double d, double a)
  88. {
  89. pipi_image_t *dst;
  90. pipi_pixels_t *srcp, *dstp;
  91. float *srcdata, *dstdata;
  92. double sina, cosa;
  93. int x, y, w, h, i, gray;
  94. w = src->w;
  95. h = src->h;
  96. gray = (src->last_modified == PIPI_PIXELS_Y_F32);
  97. srcp = gray ? pipi_get_pixels(src, PIPI_PIXELS_Y_F32)
  98. : pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
  99. srcdata = (float *)srcp->pixels;
  100. dst = pipi_new(w, h);
  101. dstp = gray ? pipi_get_pixels(dst, PIPI_PIXELS_Y_F32)
  102. : pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
  103. dstdata = (float *)dstp->pixels;
  104. sina = sin(a);
  105. cosa = cos(a);
  106. for(y = 0; y < h; y++)
  107. {
  108. for(x = 0; x < w; x++)
  109. {
  110. double angle = 2 * M_PI / dw * ((x - w / 2) * cosa
  111. + (y - h / 2) * sina - d);
  112. double displacement = dh * sin(angle);
  113. double dx, dy;
  114. int x2, y2;
  115. dx = -sina * displacement;
  116. dy = cosa * displacement;
  117. if(x < BORDER) dx = dx * x / BORDER;
  118. if(x > w - 1 - BORDER) dx = dx * (w - 1 - x) / BORDER;
  119. if(y < BORDER) dy = dy * y / BORDER;
  120. if(y > h - 1 - BORDER) dy = dy * (h - 1 - y) / BORDER;
  121. x2 = x + dx;
  122. y2 = y + dy;
  123. /* Just in case... */
  124. if(x2 < 0) x2 = 0;
  125. else if(x2 >= w) x2 = w - 1;
  126. if(y2 < 0) y2 = 0;
  127. else if(y2 >= h) y2 = h - 1;
  128. if(gray)
  129. {
  130. dstdata[y * w + x] = srcdata[y2 * w + x2];
  131. }
  132. else
  133. {
  134. for(i = 0; i < 4; i++)
  135. {
  136. dstdata[4 * (y * w + x) + i]
  137. = srcdata[4 * (y2 * w + x2) + i];
  138. }
  139. }
  140. }
  141. }
  142. return dst;
  143. }