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.
 
 
 
 
 
 

81 lines
1.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. pipi_image_t *pipi_gaussian_blur(pipi_image_t const *src, float radius)
  26. {
  27. pipi_image_t *dst;
  28. double *kernel;
  29. double K, L, t = 0.;
  30. int x, y, i, j, w, h, kr, kw;
  31. w = src->width;
  32. h = src->height;
  33. kr = (int)(3. * radius + 0.99999);
  34. kw = 2 * kr + 1;
  35. K = 1. / (2. * M_PI * radius * radius);
  36. L = -1. / (2. * radius * radius);
  37. kernel = malloc(kw * kw * sizeof(double));
  38. for(j = -kr; j <= kr; j++)
  39. for(i = -kr; i <= kr; i++)
  40. t += kernel[(j + kr) * kw + (i + kr)]
  41. = exp(L * (i * i + j * j)) * K;
  42. for(j = -kr; j <= kr; j++)
  43. for(i = -kr; i <= kr; i++)
  44. kernel[(j + kr) * kw + (i + kr)] /= t;
  45. dst = pipi_new(w, h);
  46. for(y = 0; y < h; y++)
  47. {
  48. for(x = 0; x < w; x++)
  49. {
  50. double R = 0., G = 0., B = 0.;
  51. for(j = -kr; j <= kr; j++)
  52. for(i = -kr; i <= kr; i++)
  53. {
  54. double r, g, b, f = kernel[(j + kr) * kw + (i + kr)];
  55. pipi_getpixel(src, x + i, y + j, &r, &g, &b);
  56. R += f * r;
  57. G += f * g;
  58. B += f * b;
  59. }
  60. pipi_setpixel(dst, x, y, R, G, B);
  61. }
  62. }
  63. return dst;
  64. }