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.
 
 
 
 
 
 

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