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.
 
 
 
 
 
 

191 lines
5.0 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, 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. kr = (int)(3. * rx + 1.99999);
  56. kw = 2 * kr + 1;
  57. K = -1. / (2. * rx * rx);
  58. kernel = malloc(kw * sizeof(double));
  59. for(i = -kr; i <= kr; i++)
  60. kernel[i + kr] = exp(K * ((double)i - dx) * ((double)i - dx));
  61. for(y = 0; y < h; y++)
  62. {
  63. for(x = 0; x < w; x++)
  64. {
  65. if(gray)
  66. {
  67. double Y = 0., t = 0.;
  68. int x2;
  69. for(i = -kr; i <= kr; i++)
  70. {
  71. double f = kernel[i + kr];
  72. x2 = x + i;
  73. if(x2 < 0) x2 = 0;
  74. else if(x2 >= w) x2 = w - 1;
  75. Y += f * srcdata[y * w + x2];
  76. t += f;
  77. }
  78. buffer[y * w + x] = Y / t;
  79. }
  80. else
  81. {
  82. double R = 0., G = 0., B = 0., t = 0.;
  83. int x2, off = 4 * (y * w + x);
  84. for(i = -kr; i <= kr; i++)
  85. {
  86. double f = kernel[i + kr];
  87. x2 = x + i;
  88. if(x2 < 0) x2 = 0;
  89. else if(x2 >= w) x2 = w - 1;
  90. R += f * srcdata[(y * w + x2) * 4];
  91. G += f * srcdata[(y * w + x2) * 4 + 1];
  92. B += f * srcdata[(y * w + x2) * 4 + 2];
  93. t += f;
  94. }
  95. buffer[off] = R / t;
  96. buffer[off + 1] = G / t;
  97. buffer[off + 2] = B / t;
  98. }
  99. }
  100. }
  101. free(kernel);
  102. kr = (int)(3. * ry + 1.99999);
  103. kw = 2 * kr + 1;
  104. K = -1. / (2. * ry * ry);
  105. kernel = malloc(kw * sizeof(double));
  106. for(i = -kr; i <= kr; i++)
  107. kernel[i + kr] = exp(K * ((double)i - dy) * ((double)i - dy));
  108. for(y = 0; y < h; y++)
  109. {
  110. for(x = 0; x < w; x++)
  111. {
  112. if(gray)
  113. {
  114. double Y = 0., t = 0.;
  115. int y2;
  116. for(i = -kr; i <= kr; i++)
  117. {
  118. double f = kernel[i + kr];
  119. y2 = y + i;
  120. if(y2 < 0) y2 = 0;
  121. else if(y2 >= h) y2 = h - 1;
  122. Y += f * buffer[y2 * w + x];
  123. t += f;
  124. }
  125. dstdata[y * w + x] = Y / t;
  126. }
  127. else
  128. {
  129. double R = 0., G = 0., B = 0., t = 0.;
  130. int y2, off = 4 * (y * w + x);
  131. for(i = -kr; i <= kr; i++)
  132. {
  133. double f = kernel[i + kr];
  134. y2 = y + i;
  135. if(y2 < 0) y2 = 0;
  136. else if(y2 >= h) y2 = h - 1;
  137. R += f * buffer[(y2 * w + x) * 4];
  138. G += f * buffer[(y2 * w + x) * 4 + 1];
  139. B += f * buffer[(y2 * w + x) * 4 + 2];
  140. t += f;
  141. }
  142. dstdata[off] = R / t;
  143. dstdata[off + 1] = G / t;
  144. dstdata[off + 2] = B / t;
  145. }
  146. }
  147. }
  148. free(buffer);
  149. free(kernel);
  150. return dst;
  151. }