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.
 
 
 
 
 
 

143 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);
  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 *dst;
  37. pipi_pixels_t *srcp, *dstp;
  38. float *srcdata, *dstdata;
  39. double *kernel, *buffer;
  40. double K;
  41. int x, y, i, w, h, kr, kw;
  42. if(rx < BLUR_EPSILON) rx = BLUR_EPSILON;
  43. if(ry < BLUR_EPSILON) ry = BLUR_EPSILON;
  44. w = src->w;
  45. h = src->h;
  46. srcp = pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
  47. srcdata = (float *)srcp->pixels;
  48. dst = pipi_new(w, h);
  49. dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  50. dstdata = (float *)dstp->pixels;
  51. buffer = malloc(w * h * 4 * sizeof(double));
  52. kr = (int)(3. * rx + 1.99999);
  53. kw = 2 * kr + 1;
  54. K = -1. / (2. * rx * rx);
  55. kernel = malloc(kw * sizeof(double));
  56. for(i = -kr; i <= kr; i++)
  57. kernel[i + kr] = exp(K * ((double)i - dx) * ((double)i - dx));
  58. for(y = 0; y < h; y++)
  59. {
  60. for(x = 0; x < w; x++)
  61. {
  62. double R = 0., G = 0., B = 0., t = 0.;
  63. int x2, off = 4 * (y * w + x);
  64. for(i = -kr; i <= kr; i++)
  65. {
  66. double f = kernel[i + kr];
  67. x2 = x + i;
  68. if(x2 < 0) x2 = 0;
  69. else if(x2 >= w) x2 = w - 1;
  70. R += f * srcdata[(y * w + x2) * 4];
  71. G += f * srcdata[(y * w + x2) * 4 + 1];
  72. B += f * srcdata[(y * w + x2) * 4 + 2];
  73. t += f;
  74. }
  75. buffer[off] = R / t;
  76. buffer[off + 1] = G / t;
  77. buffer[off + 2] = B / t;
  78. }
  79. }
  80. free(kernel);
  81. kr = (int)(3. * ry + 1.99999);
  82. kw = 2 * kr + 1;
  83. K = -1. / (2. * ry * ry);
  84. kernel = malloc(kw * sizeof(double));
  85. for(i = -kr; i <= kr; i++)
  86. kernel[i + kr] = exp(K * ((double)i - dy) * ((double)i - dy));
  87. for(y = 0; y < h; y++)
  88. {
  89. for(x = 0; x < w; x++)
  90. {
  91. double R = 0., G = 0., B = 0., t = 0.;
  92. int y2, off = 4 * (y * w + x);
  93. for(i = -kr; i <= kr; i++)
  94. {
  95. double f = kernel[i + kr];
  96. y2 = y + i;
  97. if(y2 < 0) y2 = 0;
  98. else if(y2 >= h) y2 = h - 1;
  99. R += f * buffer[(y2 * w + x) * 4];
  100. G += f * buffer[(y2 * w + x) * 4 + 1];
  101. B += f * buffer[(y2 * w + x) * 4 + 2];
  102. t += f;
  103. }
  104. dstdata[off] = R / t;
  105. dstdata[off + 1] = G / t;
  106. dstdata[off + 2] = B / t;
  107. }
  108. }
  109. free(buffer);
  110. free(kernel);
  111. return dst;
  112. }