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 5.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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, gray;
  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. gray = (src->last_modified == PIPI_PIXELS_Y_F);
  47. srcp = gray ? pipi_getpixels(src, PIPI_PIXELS_Y_F)
  48. : pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
  49. srcdata = (float *)srcp->pixels;
  50. dst = pipi_new(w, h);
  51. dstp = gray ? pipi_getpixels(dst, PIPI_PIXELS_Y_F)
  52. : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  53. dstdata = (float *)dstp->pixels;
  54. buffer = malloc(w * h * (gray ? 1 : 4) * sizeof(double));
  55. /* FIXME: the kernel becomes far too big with large values of dx, because
  56. * we grow both left and right. Fix the growing direction. */
  57. kr = (int)(3. * rx + .99999 + ceil(abs(dx)));
  58. kw = 2 * kr + 1;
  59. K = -1. / (2. * rx * rx);
  60. kernel = malloc(kw * sizeof(double));
  61. for(i = -kr; i <= kr; i++)
  62. kernel[i + kr] = exp(K * ((double)i + dx) * ((double)i + dx));
  63. for(y = 0; y < h; y++)
  64. {
  65. for(x = 0; x < w; x++)
  66. {
  67. if(gray)
  68. {
  69. double Y = 0., t = 0.;
  70. int x2;
  71. for(i = -kr; i <= kr; i++)
  72. {
  73. double f = kernel[i + kr];
  74. x2 = x + i;
  75. if(x2 < 0) x2 = 0;
  76. else if(x2 >= w) x2 = w - 1;
  77. Y += f * srcdata[y * w + x2];
  78. t += f;
  79. }
  80. buffer[y * w + x] = Y / t;
  81. }
  82. else
  83. {
  84. double R = 0., G = 0., B = 0., t = 0.;
  85. int x2, off = 4 * (y * w + x);
  86. for(i = -kr; i <= kr; i++)
  87. {
  88. double f = kernel[i + kr];
  89. x2 = x + i;
  90. if(x2 < 0) x2 = 0;
  91. else if(x2 >= w) x2 = w - 1;
  92. R += f * srcdata[(y * w + x2) * 4];
  93. G += f * srcdata[(y * w + x2) * 4 + 1];
  94. B += f * srcdata[(y * w + x2) * 4 + 2];
  95. t += f;
  96. }
  97. buffer[off] = R / t;
  98. buffer[off + 1] = G / t;
  99. buffer[off + 2] = B / t;
  100. }
  101. }
  102. }
  103. free(kernel);
  104. kr = (int)(3. * ry + .99999 + ceil(abs(dy)));
  105. kw = 2 * kr + 1;
  106. K = -1. / (2. * ry * ry);
  107. kernel = malloc(kw * sizeof(double));
  108. for(i = -kr; i <= kr; i++)
  109. kernel[i + kr] = exp(K * ((double)i + dy) * ((double)i + dy));
  110. for(y = 0; y < h; y++)
  111. {
  112. for(x = 0; x < w; x++)
  113. {
  114. if(gray)
  115. {
  116. double Y = 0., t = 0.;
  117. int y2;
  118. for(i = -kr; i <= kr; i++)
  119. {
  120. double f = kernel[i + kr];
  121. y2 = y + i;
  122. if(y2 < 0) y2 = 0;
  123. else if(y2 >= h) y2 = h - 1;
  124. Y += f * buffer[y2 * w + x];
  125. t += f;
  126. }
  127. dstdata[y * w + x] = Y / t;
  128. }
  129. else
  130. {
  131. double R = 0., G = 0., B = 0., t = 0.;
  132. int y2, off = 4 * (y * w + x);
  133. for(i = -kr; i <= kr; i++)
  134. {
  135. double f = kernel[i + kr];
  136. y2 = y + i;
  137. if(y2 < 0) y2 = 0;
  138. else if(y2 >= h) y2 = h - 1;
  139. R += f * buffer[(y2 * w + x) * 4];
  140. G += f * buffer[(y2 * w + x) * 4 + 1];
  141. B += f * buffer[(y2 * w + x) * 4 + 2];
  142. t += f;
  143. }
  144. dstdata[off] = R / t;
  145. dstdata[off + 1] = G / t;
  146. dstdata[off + 2] = B / t;
  147. }
  148. }
  149. }
  150. free(buffer);
  151. free(kernel);
  152. return dst;
  153. }