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.
 
 
 
 
 
 

112 lines
2.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. /* 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);
  32. }
  33. pipi_image_t *pipi_gaussian_blur_ext(pipi_image_t *src, float rx, float ry,
  34. float dx, float dy)
  35. {
  36. pipi_image_t *ret;
  37. double *kernel;
  38. double Kx, Ky, t = 0.0;
  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. /* FIXME: the kernel becomes far too big with large values of dx, because
  43. * we grow both left and right. Fix the growing direction. */
  44. krx = (int)(3. * rx + .99999 + ceil(abs(dx)));
  45. m = 2 * krx + 1;
  46. Kx = -1. / (2. * rx * rx);
  47. kry = (int)(3. * ry + .99999 + ceil(abs(dy)));
  48. n = 2 * kry + 1;
  49. Ky = -1. / (2. * ry * ry);
  50. kernel = malloc(m * n * sizeof(double));
  51. for(j = -kry; j <= kry; j++)
  52. {
  53. double ey = Ky * ((double)j + dy) * ((double)j + dy);
  54. for(i = -krx; i <= krx; i++)
  55. {
  56. double ex = Kx * ((double)i + dx) * ((double)i + dx);
  57. double d = exp(ex + ey);
  58. kernel[(j + kry) * m + i + krx] = d;
  59. t += d;
  60. }
  61. }
  62. for(j = 0; j < n; j++)
  63. for(i = 0; i < m; i++)
  64. kernel[j * m + i] /= t;
  65. ret = pipi_convolution(src, m, n, kernel);
  66. free(kernel);
  67. return ret;
  68. }
  69. /* FIXME: box blur would be incredibly faster using an accumulator instead
  70. * of a convolution filter... */
  71. pipi_image_t *pipi_box_blur(pipi_image_t *src, int size)
  72. {
  73. return pipi_box_blur_ext(src, size, size);
  74. }
  75. pipi_image_t *pipi_box_blur_ext(pipi_image_t *src, int m, int n)
  76. {
  77. pipi_image_t *ret;
  78. double *kernel;
  79. int i;
  80. kernel = malloc(m * n * sizeof(double));
  81. for(i = 0; i < m * n; i++)
  82. kernel[i] = 1. / (m * n);
  83. ret = pipi_convolution(src, m, n, kernel);
  84. free(kernel);
  85. return ret;
  86. }