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.

blur.c 8.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * libpipi Proper image processing implementation 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. * blur.c: blur functions
  16. */
  17. #include "config.h"
  18. #include "common.h"
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <math.h>
  23. #include "pipi.h"
  24. #include "pipi_internals.h"
  25. #if !defined TEMPLATE_FILE /* This file uses the template system */
  26. #define TEMPLATE_FLAGS SET_FLAG_GRAY
  27. #define TEMPLATE_FILE "filter/blur.c"
  28. #include "pipi_template.h"
  29. /* Any standard deviation below this value will be rounded up, in order
  30. * to avoid ridiculously low values. exp(-1/(2*0.2*0.2)) is < 10^-5 so
  31. * there is little chance that any value below 0.2 will be useful. */
  32. #define BLUR_EPSILON 0.2
  33. pipi_image_t *pipi_gaussian_blur(pipi_image_t *src, float radius)
  34. {
  35. return pipi_gaussian_blur_ext(src, radius, radius, 0.0, 0.0, 0.0);
  36. }
  37. pipi_image_t *pipi_gaussian_blur_ext(pipi_image_t *src, float rx, float ry,
  38. float angle, float dx, float dy)
  39. {
  40. pipi_image_t *ret;
  41. double *kernel;
  42. double Kx, Ky, t = 0.0, sint, cost, bbx, bby;
  43. int i, j, krx, kry, m, n;
  44. if(rx < BLUR_EPSILON) rx = BLUR_EPSILON;
  45. if(ry < BLUR_EPSILON) ry = BLUR_EPSILON;
  46. sint = sin(angle * M_PI / 180.);
  47. cost = cos(angle * M_PI / 180.);
  48. /* Compute the final ellipse's bounding box */
  49. bbx = sqrt(rx * rx * cost * cost + ry * ry * sint * sint);
  50. bby = sqrt(ry * ry * cost * cost + rx * rx * sint * sint);
  51. /* FIXME: the kernel becomes far too big with large values of dx, because
  52. * we grow both left and right. Fix the growing direction. */
  53. krx = (int)(3. * bbx + .99999 + ceil(abs(dx)));
  54. m = 2 * krx + 1;
  55. Kx = -1. / (2. * rx * rx);
  56. kry = (int)(3. * bby + .99999 + ceil(abs(dy)));
  57. n = 2 * kry + 1;
  58. Ky = -1. / (2. * ry * ry);
  59. kernel = malloc(m * n * sizeof(double));
  60. for(j = -kry; j <= kry; j++)
  61. {
  62. for(i = -krx; i <= krx; i++)
  63. {
  64. /* FIXME: this level of interpolation sucks. We should
  65. * interpolate on the full NxN grid for better quality. */
  66. static double const samples[] =
  67. {
  68. .0, .0, 1,
  69. -.40, -.40, 0.8,
  70. -.30, .0, 0.9,
  71. -.40, .40, 0.8,
  72. .0, .30, 0.9,
  73. .40, .40, 0.8,
  74. .30, .0, 0.9,
  75. .40, -.40, 0.8,
  76. .0, -.30, 0.9,
  77. };
  78. double u, v, ex, ey, d = 0.;
  79. unsigned int k;
  80. for(k = 0; k < sizeof(samples) / sizeof(*samples) / 3; k++)
  81. {
  82. u = ((double)i + samples[k * 3]) * cost
  83. - ((double)j + samples[k * 3 + 1]) * sint + dx;
  84. v = ((double)i + samples[k * 3]) * sint
  85. + ((double)j + samples[k * 3 + 1]) * cost + dy;
  86. ex = Kx * u * u;
  87. ey = Ky * v * v;
  88. d += samples[k * 3 + 2] * exp(ex + ey);
  89. /* Do not interpolate if this is a standard gaussian. */
  90. if(!dx && !dy && !angle)
  91. break;
  92. }
  93. kernel[(j + kry) * m + i + krx] = d;
  94. t += d;
  95. }
  96. }
  97. for(j = 0; j < n; j++)
  98. for(i = 0; i < m; i++)
  99. kernel[j * m + i] /= t;
  100. ret = pipi_convolution(src, m, n, kernel);
  101. free(kernel);
  102. return ret;
  103. }
  104. pipi_image_t *pipi_box_blur(pipi_image_t *src, int size)
  105. {
  106. return pipi_box_blur_ext(src, size, size);
  107. }
  108. pipi_image_t *pipi_box_blur_ext(pipi_image_t *src, int m, int n)
  109. {
  110. if(src->last_modified == PIPI_PIXELS_Y_F)
  111. return boxblur_gray(src, m, n);
  112. return boxblur(src, m, n);
  113. }
  114. #else /* XXX: the following functions use the template system */
  115. /* FIXME: FLAG_WRAP */
  116. static pipi_image_t *SUFFIX(boxblur)(pipi_image_t *src, int m, int n)
  117. {
  118. pipi_image_t *dst;
  119. pipi_pixels_t *srcp, *dstp;
  120. float *srcdata, *dstdata;
  121. double *acc;
  122. int x, y, w, h, i, j, size;
  123. w = src->w;
  124. h = src->h;
  125. size = (2 * m + 1) * (2 * n + 1);
  126. srcp = FLAG_GRAY ? pipi_getpixels(src, PIPI_PIXELS_Y_F)
  127. : pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
  128. srcdata = (float *)srcp->pixels;
  129. dst = pipi_new(w, h);
  130. dstp = FLAG_GRAY ? pipi_getpixels(dst, PIPI_PIXELS_Y_F)
  131. : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  132. dstdata = (float *)dstp->pixels;
  133. acc = malloc(w * (FLAG_GRAY ? 1 : 4) * sizeof(double));
  134. /* Step 1: fill the accumulator */
  135. for(x = 0; x < w; x++)
  136. {
  137. double r = 0., g = 0., b = 0., a = 0.;
  138. double t = 0.;
  139. for(j = -n; j <= n; j++)
  140. {
  141. int j2 = (j < 0) ? h - 1 - ((-j - 1) % h) : j % h;
  142. if(FLAG_GRAY)
  143. t += srcdata[j2 * w + x];
  144. else
  145. {
  146. r += srcdata[4 * (j2 * w + x)];
  147. g += srcdata[4 * (j2 * w + x) + 1];
  148. b += srcdata[4 * (j2 * w + x) + 2];
  149. a += srcdata[4 * (j2 * w + x) + 3];
  150. }
  151. }
  152. if(FLAG_GRAY)
  153. acc[x] = t;
  154. else
  155. {
  156. acc[4 * x] = r;
  157. acc[4 * x + 1] = g;
  158. acc[4 * x + 2] = b;
  159. acc[4 * x + 3] = a;
  160. }
  161. }
  162. /* Step 2: blur the image, line by line */
  163. for(y = 0; y < h; y++)
  164. {
  165. double r = 0., g = 0., b = 0., a = 0.;
  166. double t = 0.;
  167. /* 2.1: compute the first pixel */
  168. for(i = -m; i <= m; i++)
  169. {
  170. int i2 = (i < 0) ? w - 1 - ((-i - 1) % w) : i % w;
  171. if(FLAG_GRAY)
  172. t += acc[i2];
  173. else
  174. {
  175. r += acc[4 * i2];
  176. g += acc[4 * i2 + 1];
  177. b += acc[4 * i2 + 2];
  178. a += acc[4 * i2 + 3];
  179. }
  180. }
  181. /* 2.2: iterate on the whole line */
  182. for(x = 0; x < w; x++)
  183. {
  184. int u, u2, v, v2;
  185. if(FLAG_GRAY)
  186. {
  187. dstdata[y * w + x] = t / size;
  188. }
  189. else
  190. {
  191. dstdata[4 * (y * w + x)] = r / size;
  192. dstdata[4 * (y * w + x) + 1] = g / size;
  193. dstdata[4 * (y * w + x) + 2] = b / size;
  194. dstdata[4 * (y * w + x) + 3] = a / size;
  195. }
  196. u = x - m;
  197. u2 = (u < 0) ? w - 1 - ((-u - 1) % w) : u % w;
  198. v = x + m + 1;
  199. v2 = (v < 0) ? w - 1 - ((-v - 1) % w) : v % w;
  200. if(FLAG_GRAY)
  201. {
  202. t = t - acc[u2] + acc[v2];
  203. }
  204. else
  205. {
  206. r = r - acc[4 * u2] + acc[4 * v2];
  207. g = g - acc[4 * u2 + 1] + acc[4 * v2 + 1];
  208. b = b - acc[4 * u2 + 2] + acc[4 * v2 + 2];
  209. a = a - acc[4 * u2 + 3] + acc[4 * v2 + 3];
  210. }
  211. }
  212. /* 2.3: update the accumulator */
  213. for(x = 0; x < w; x++)
  214. {
  215. int u, u2, v, v2;
  216. u = y - n;
  217. u2 = (u < 0) ? w - 1 - ((-u - 1) % w) : u % w;
  218. v = y + n + 1;
  219. v2 = (v < 0) ? w - 1 - ((-v - 1) % w) : v % w;
  220. if(FLAG_GRAY)
  221. {
  222. acc[x] += srcdata[v2 * w + x] - srcdata[u2 * w + x];
  223. }
  224. else
  225. {
  226. int uoff = 4 * (u2 * w + x);
  227. int voff = 4 * (v2 * w + x);
  228. acc[4 * x] += srcdata[voff] - srcdata[uoff];
  229. acc[4 * x + 1] += srcdata[voff + 1] - srcdata[uoff + 1];
  230. acc[4 * x + 2] += srcdata[voff + 2] - srcdata[uoff + 2];
  231. acc[4 * x + 3] += srcdata[voff + 3] - srcdata[uoff + 3];
  232. }
  233. }
  234. }
  235. free(acc);
  236. return dst;
  237. }
  238. #endif