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.
 
 
 
 
 
 

136 lines
3.6 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. static double const samples[] =
  61. {
  62. -.50, -.50, 0.5,
  63. .50, -.50, 0.5,
  64. .0, .0, 1,
  65. -.50, .50, 0.5,
  66. .50, .50, 0.5
  67. };
  68. double u, v, ex, ey, d = 0.;
  69. int k;
  70. for(k = 0; k < 5; k++)
  71. {
  72. u = ((double)i + samples[k * 3]) * cost
  73. - ((double)j + samples[k * 3 + 1]) * sint + dx;
  74. v = ((double)i + samples[k * 3]) * sint
  75. + ((double)j + samples[k * 3 + 1]) * cost + dy;
  76. ex = Kx * u * u;
  77. ey = Ky * v * v;
  78. d += samples[k * 3 + 2] * exp(ex + ey);
  79. }
  80. kernel[(j + kry) * m + i + krx] = d;
  81. t += d;
  82. }
  83. }
  84. for(j = 0; j < n; j++)
  85. for(i = 0; i < m; i++)
  86. kernel[j * m + i] /= t;
  87. ret = pipi_convolution(src, m, n, kernel);
  88. free(kernel);
  89. return ret;
  90. }
  91. /* FIXME: box blur would be incredibly faster using an accumulator instead
  92. * of a convolution filter... */
  93. pipi_image_t *pipi_box_blur(pipi_image_t *src, int size)
  94. {
  95. return pipi_box_blur_ext(src, size, size);
  96. }
  97. pipi_image_t *pipi_box_blur_ext(pipi_image_t *src, int m, int n)
  98. {
  99. pipi_image_t *ret;
  100. double *kernel;
  101. int i;
  102. kernel = malloc(m * n * sizeof(double));
  103. for(i = 0; i < m * n; i++)
  104. kernel[i] = 1. / (m * n);
  105. ret = pipi_convolution(src, m, n, kernel);
  106. free(kernel);
  107. return ret;
  108. }