您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

142 行
3.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. }
  86. kernel[(j + kry) * m + i + krx] = d;
  87. t += d;
  88. }
  89. }
  90. for(j = 0; j < n; j++)
  91. for(i = 0; i < m; i++)
  92. kernel[j * m + i] /= t;
  93. ret = pipi_convolution(src, m, n, kernel);
  94. free(kernel);
  95. return ret;
  96. }
  97. /* FIXME: box blur would be incredibly faster using an accumulator instead
  98. * of a convolution filter... */
  99. pipi_image_t *pipi_box_blur(pipi_image_t *src, int size)
  100. {
  101. return pipi_box_blur_ext(src, size, size);
  102. }
  103. pipi_image_t *pipi_box_blur_ext(pipi_image_t *src, int m, int n)
  104. {
  105. pipi_image_t *ret;
  106. double *kernel;
  107. int i;
  108. kernel = malloc(m * n * sizeof(double));
  109. for(i = 0; i < m * n; i++)
  110. kernel[i] = 1. / (m * n);
  111. ret = pipi_convolution(src, m, n, kernel);
  112. free(kernel);
  113. return ret;
  114. }