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.
 
 
 
 
 
 

280 lines
7.9 KiB

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