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.
 
 
 
 
 
 

120 lines
2.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 *src, float radius)
  26. {
  27. pipi_image_t *dst;
  28. pipi_pixels_t *srcp, *dstp;
  29. float *srcdata, *dstdata;
  30. double *kernel, *buffer;
  31. double K, L;
  32. int x, y, i, w, h, kr, kw;
  33. w = src->w;
  34. h = src->h;
  35. srcp = pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
  36. srcdata = (float *)srcp->pixels;
  37. dst = pipi_new(w, h);
  38. dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  39. dstdata = (float *)dstp->pixels;
  40. kr = (int)(3. * radius + 0.99999);
  41. kw = 2 * kr + 1;
  42. K = 1. / (sqrt(2. * M_PI) * radius);
  43. L = -1. / (2. * radius * radius);
  44. kernel = malloc(kw * sizeof(double));
  45. for(i = -kr; i <= kr; i++)
  46. kernel[i + kr] = exp(L * i * i) * K;
  47. buffer = malloc(w * h * 4 * sizeof(double));
  48. for(y = 0; y < h; y++)
  49. {
  50. for(x = 0; x < w; x++)
  51. {
  52. double R = 0., G = 0., B = 0., t = 0.;
  53. int x2;
  54. for(i = -kr; i <= kr; i++)
  55. {
  56. double f = kernel[i + kr];
  57. x2 = x + i;
  58. if(x2 < 0) x2 = 0;
  59. else if(x2 >= w) x2 = w - 1;
  60. R += f * srcdata[(y * w + x2) * 4];
  61. G += f * srcdata[(y * w + x2) * 4 + 1];
  62. B += f * srcdata[(y * w + x2) * 4 + 2];
  63. t += f;
  64. }
  65. buffer[(y * w + x) * 4] = R / t;
  66. buffer[(y * w + x) * 4 + 1] = G / t;
  67. buffer[(y * w + x) * 4 + 2] = B / t;
  68. }
  69. }
  70. for(y = 0; y < h; y++)
  71. {
  72. for(x = 0; x < w; x++)
  73. {
  74. double R = 0., G = 0., B = 0., t = 0.;
  75. int y2;
  76. for(i = -kr; i <= kr; i++)
  77. {
  78. double f = kernel[i + kr];
  79. y2 = y + i;
  80. if(y2 < 0) y2 = 0;
  81. else if(y2 >= h) y2 = h - 1;
  82. R += f * buffer[(y2 * w + x) * 4];
  83. G += f * buffer[(y2 * w + x) * 4 + 1];
  84. B += f * buffer[(y2 * w + x) * 4 + 2];
  85. t += f;
  86. }
  87. dstdata[(y * w + x) * 4] = R / t;
  88. dstdata[(y * w + x) * 4 + 1] = G / t;
  89. dstdata[(y * w + x) * 4 + 2] = B / t;
  90. }
  91. }
  92. free(buffer);
  93. free(kernel);
  94. return dst;
  95. }