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.
 
 
 
 
 
 

314 lines
8.8 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. #if !defined TEMPLATE_FILE /* This file uses the template system */
  26. #define TEMPLATE_FLAGS SET_FLAG_GRAY | SET_FLAG_WRAP
  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->wrap)
  111. {
  112. if(src->last_modified == PIPI_PIXELS_Y_F)
  113. return boxblur_gray_wrap(src, m, n);
  114. return boxblur_wrap(src, m, n);
  115. }
  116. else
  117. {
  118. if(src->last_modified == PIPI_PIXELS_Y_F)
  119. return boxblur_gray(src, m, n);
  120. return boxblur(src, m, n);
  121. }
  122. }
  123. #else /* XXX: the following functions use the template system */
  124. static pipi_image_t *T(boxblur)(pipi_image_t *src, int m, int n)
  125. {
  126. pipi_image_t *dst;
  127. pipi_pixels_t *srcp, *dstp;
  128. float *srcdata, *dstdata;
  129. double *acc;
  130. int x, y, w, h, i, j, i2, j2, size;
  131. w = src->w;
  132. h = src->h;
  133. size = (2 * m + 1) * (2 * n + 1);
  134. srcp = FLAG_GRAY ? pipi_getpixels(src, PIPI_PIXELS_Y_F)
  135. : pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
  136. srcdata = (float *)srcp->pixels;
  137. dst = pipi_new(w, h);
  138. dstp = FLAG_GRAY ? pipi_getpixels(dst, PIPI_PIXELS_Y_F)
  139. : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  140. dstdata = (float *)dstp->pixels;
  141. acc = malloc(w * (FLAG_GRAY ? 1 : 4) * sizeof(double));
  142. /* Step 1: fill the accumulator */
  143. for(x = 0; x < w; x++)
  144. {
  145. double r = 0., g = 0., b = 0., a = 0.;
  146. double t = 0.;
  147. for(j = -n; j <= n; j++)
  148. {
  149. if(FLAG_WRAP)
  150. j2 = (j < 0) ? h - 1 - ((-j - 1) % h) : j % h;
  151. else
  152. j2 = (j < 0) ? 0 : (j >= h) ? h - 1 : j;
  153. if(FLAG_GRAY)
  154. t += srcdata[j2 * w + x];
  155. else
  156. {
  157. r += srcdata[4 * (j2 * w + x)];
  158. g += srcdata[4 * (j2 * w + x) + 1];
  159. b += srcdata[4 * (j2 * w + x) + 2];
  160. a += srcdata[4 * (j2 * w + x) + 3];
  161. }
  162. }
  163. if(FLAG_GRAY)
  164. acc[x] = t;
  165. else
  166. {
  167. acc[4 * x] = r;
  168. acc[4 * x + 1] = g;
  169. acc[4 * x + 2] = b;
  170. acc[4 * x + 3] = a;
  171. }
  172. }
  173. /* Step 2: blur the image, line by line */
  174. for(y = 0; y < h; y++)
  175. {
  176. double r = 0., g = 0., b = 0., a = 0.;
  177. double t = 0.;
  178. /* 2.1: compute the first pixel */
  179. for(i = -m; i <= m; i++)
  180. {
  181. if(FLAG_WRAP)
  182. i2 = (i < 0) ? w - 1 - ((-i - 1) % w) : i % w;
  183. else
  184. i2 = (i < 0) ? 0 : (i >= w) ? w - 1 : i;
  185. if(FLAG_GRAY)
  186. t += acc[i2];
  187. else
  188. {
  189. r += acc[4 * i2];
  190. g += acc[4 * i2 + 1];
  191. b += acc[4 * i2 + 2];
  192. a += acc[4 * i2 + 3];
  193. }
  194. }
  195. /* 2.2: iterate on the whole line */
  196. for(x = 0; x < w; x++)
  197. {
  198. int u, u2, v, v2;
  199. if(FLAG_GRAY)
  200. {
  201. dstdata[y * w + x] = t / size;
  202. }
  203. else
  204. {
  205. dstdata[4 * (y * w + x)] = r / size;
  206. dstdata[4 * (y * w + x) + 1] = g / size;
  207. dstdata[4 * (y * w + x) + 2] = b / size;
  208. dstdata[4 * (y * w + x) + 3] = a / size;
  209. }
  210. u = x - m;
  211. if(FLAG_WRAP)
  212. u2 = (u < 0) ? w - 1 - ((-u - 1) % w) : u % w;
  213. else
  214. u2 = (u < 0) ? 0 : (u >= w) ? w - 1 : u;
  215. v = x + m + 1;
  216. if(FLAG_WRAP)
  217. v2 = (v < 0) ? w - 1 - ((-v - 1) % w) : v % w;
  218. else
  219. v2 = (v < 0) ? 0 : (v >= w) ? w - 1 : v;
  220. if(FLAG_GRAY)
  221. {
  222. t = t - acc[u2] + acc[v2];
  223. }
  224. else
  225. {
  226. r = r - acc[4 * u2] + acc[4 * v2];
  227. g = g - acc[4 * u2 + 1] + acc[4 * v2 + 1];
  228. b = b - acc[4 * u2 + 2] + acc[4 * v2 + 2];
  229. a = a - acc[4 * u2 + 3] + acc[4 * v2 + 3];
  230. }
  231. }
  232. /* 2.3: update the accumulator */
  233. for(x = 0; x < w; x++)
  234. {
  235. int u, u2, v, v2;
  236. u = y - n;
  237. if(FLAG_WRAP)
  238. u2 = (u < 0) ? w - 1 - ((-u - 1) % w) : u % w;
  239. else
  240. u2 = (u < 0) ? 0 : (u >= w) ? w - 1 : u;
  241. v = y + n + 1;
  242. if(FLAG_WRAP)
  243. v2 = (v < 0) ? w - 1 - ((-v - 1) % w) : v % w;
  244. else
  245. v2 = (v < 0) ? 0 : (v >= w) ? w - 1 : v;
  246. if(FLAG_GRAY)
  247. {
  248. acc[x] += srcdata[v2 * w + x] - srcdata[u2 * w + x];
  249. }
  250. else
  251. {
  252. int uoff = 4 * (u2 * w + x);
  253. int voff = 4 * (v2 * w + x);
  254. acc[4 * x] += srcdata[voff] - srcdata[uoff];
  255. acc[4 * x + 1] += srcdata[voff + 1] - srcdata[uoff + 1];
  256. acc[4 * x + 2] += srcdata[voff + 2] - srcdata[uoff + 2];
  257. acc[4 * x + 3] += srcdata[voff + 3] - srcdata[uoff + 3];
  258. }
  259. }
  260. }
  261. free(acc);
  262. return dst;
  263. }
  264. #endif