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.

blur.c 3.3 KiB

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