Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

120 rindas
3.2 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. double u = (double)i * cost - (double)j * sint + dx;
  61. double v = (double)j * cost + (double)i * sint + dy;
  62. double ex = Kx * u * u;
  63. double ey = Ky * v * v;
  64. double d = exp(ex + ey);
  65. kernel[(j + kry) * m + i + krx] = d;
  66. t += d;
  67. }
  68. }
  69. for(j = 0; j < n; j++)
  70. for(i = 0; i < m; i++)
  71. kernel[j * m + i] /= t;
  72. ret = pipi_convolution(src, m, n, kernel);
  73. free(kernel);
  74. return ret;
  75. }
  76. /* FIXME: box blur would be incredibly faster using an accumulator instead
  77. * of a convolution filter... */
  78. pipi_image_t *pipi_box_blur(pipi_image_t *src, int size)
  79. {
  80. return pipi_box_blur_ext(src, size, size);
  81. }
  82. pipi_image_t *pipi_box_blur_ext(pipi_image_t *src, int m, int n)
  83. {
  84. pipi_image_t *ret;
  85. double *kernel;
  86. int i;
  87. kernel = malloc(m * n * sizeof(double));
  88. for(i = 0; i < m * n; i++)
  89. kernel[i] = 1. / (m * n);
  90. ret = pipi_convolution(src, m, n, kernel);
  91. free(kernel);
  92. return ret;
  93. }