BoxBlur.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #include "Imaging.h"
  2. #define MAX(x, y) (((x) > (y)) ? (x) : (y))
  3. #define MIN(x, y) (((x) < (y)) ? (x) : (y))
  4. typedef UINT8 pixel[4];
  5. void static inline
  6. ImagingLineBoxBlur32(pixel *lineOut, pixel *lineIn, int lastx, int radius, int edgeA,
  7. int edgeB, UINT32 ww, UINT32 fw)
  8. {
  9. int x;
  10. UINT32 acc[4];
  11. UINT32 bulk[4];
  12. #define MOVE_ACC(acc, subtract, add) \
  13. acc[0] += lineIn[add][0] - lineIn[subtract][0]; \
  14. acc[1] += lineIn[add][1] - lineIn[subtract][1]; \
  15. acc[2] += lineIn[add][2] - lineIn[subtract][2]; \
  16. acc[3] += lineIn[add][3] - lineIn[subtract][3];
  17. #define ADD_FAR(bulk, acc, left, right) \
  18. bulk[0] = (acc[0] * ww) + (lineIn[left][0] + lineIn[right][0]) * fw; \
  19. bulk[1] = (acc[1] * ww) + (lineIn[left][1] + lineIn[right][1]) * fw; \
  20. bulk[2] = (acc[2] * ww) + (lineIn[left][2] + lineIn[right][2]) * fw; \
  21. bulk[3] = (acc[3] * ww) + (lineIn[left][3] + lineIn[right][3]) * fw;
  22. #define SAVE(x, bulk) \
  23. lineOut[x][0] = (UINT8)((bulk[0] + (1 << 23)) >> 24); \
  24. lineOut[x][1] = (UINT8)((bulk[1] + (1 << 23)) >> 24); \
  25. lineOut[x][2] = (UINT8)((bulk[2] + (1 << 23)) >> 24); \
  26. lineOut[x][3] = (UINT8)((bulk[3] + (1 << 23)) >> 24);
  27. /* Compute acc for -1 pixel (outside of image):
  28. From "-radius-1" to "-1" get first pixel,
  29. then from "0" to "radius-1". */
  30. acc[0] = lineIn[0][0] * (radius + 1);
  31. acc[1] = lineIn[0][1] * (radius + 1);
  32. acc[2] = lineIn[0][2] * (radius + 1);
  33. acc[3] = lineIn[0][3] * (radius + 1);
  34. /* As radius can be bigger than xsize, iterate to edgeA -1. */
  35. for (x = 0; x < edgeA - 1; x++) {
  36. acc[0] += lineIn[x][0];
  37. acc[1] += lineIn[x][1];
  38. acc[2] += lineIn[x][2];
  39. acc[3] += lineIn[x][3];
  40. }
  41. /* Then multiply remainder to last x. */
  42. acc[0] += lineIn[lastx][0] * (radius - edgeA + 1);
  43. acc[1] += lineIn[lastx][1] * (radius - edgeA + 1);
  44. acc[2] += lineIn[lastx][2] * (radius - edgeA + 1);
  45. acc[3] += lineIn[lastx][3] * (radius - edgeA + 1);
  46. if (edgeA <= edgeB)
  47. {
  48. /* Subtract pixel from left ("0").
  49. Add pixels from radius. */
  50. for (x = 0; x < edgeA; x++) {
  51. MOVE_ACC(acc, 0, x + radius);
  52. ADD_FAR(bulk, acc, 0, x + radius + 1);
  53. SAVE(x, bulk);
  54. }
  55. /* Subtract previous pixel from "-radius".
  56. Add pixels from radius. */
  57. for (x = edgeA; x < edgeB; x++) {
  58. MOVE_ACC(acc, x - radius - 1, x + radius);
  59. ADD_FAR(bulk, acc, x - radius - 1, x + radius + 1);
  60. SAVE(x, bulk);
  61. }
  62. /* Subtract previous pixel from "-radius".
  63. Add last pixel. */
  64. for (x = edgeB; x <= lastx; x++) {
  65. MOVE_ACC(acc, x - radius - 1, lastx);
  66. ADD_FAR(bulk, acc, x - radius - 1, lastx);
  67. SAVE(x, bulk);
  68. }
  69. }
  70. else
  71. {
  72. for (x = 0; x < edgeB; x++) {
  73. MOVE_ACC(acc, 0, x + radius);
  74. ADD_FAR(bulk, acc, 0, x + radius + 1);
  75. SAVE(x, bulk);
  76. }
  77. for (x = edgeB; x < edgeA; x++) {
  78. MOVE_ACC(acc, 0, lastx);
  79. ADD_FAR(bulk, acc, 0, lastx);
  80. SAVE(x, bulk);
  81. }
  82. for (x = edgeA; x <= lastx; x++) {
  83. MOVE_ACC(acc, x - radius - 1, lastx);
  84. ADD_FAR(bulk, acc, x - radius - 1, lastx);
  85. SAVE(x, bulk);
  86. }
  87. }
  88. #undef MOVE_ACC
  89. #undef ADD_FAR
  90. #undef SAVE
  91. }
  92. void static inline
  93. ImagingLineBoxBlur8(UINT8 *lineOut, UINT8 *lineIn, int lastx, int radius, int edgeA,
  94. int edgeB, UINT32 ww, UINT32 fw)
  95. {
  96. int x;
  97. UINT32 acc;
  98. UINT32 bulk;
  99. #define MOVE_ACC(acc, subtract, add) \
  100. acc += lineIn[add] - lineIn[subtract];
  101. #define ADD_FAR(bulk, acc, left, right) \
  102. bulk = (acc * ww) + (lineIn[left] + lineIn[right]) * fw;
  103. #define SAVE(x, bulk) \
  104. lineOut[x] = (UINT8)((bulk + (1 << 23)) >> 24)
  105. acc = lineIn[0] * (radius + 1);
  106. for (x = 0; x < edgeA - 1; x++) {
  107. acc += lineIn[x];
  108. }
  109. acc += lineIn[lastx] * (radius - edgeA + 1);
  110. if (edgeA <= edgeB)
  111. {
  112. for (x = 0; x < edgeA; x++) {
  113. MOVE_ACC(acc, 0, x + radius);
  114. ADD_FAR(bulk, acc, 0, x + radius + 1);
  115. SAVE(x, bulk);
  116. }
  117. for (x = edgeA; x < edgeB; x++) {
  118. MOVE_ACC(acc, x - radius - 1, x + radius);
  119. ADD_FAR(bulk, acc, x - radius - 1, x + radius + 1);
  120. SAVE(x, bulk);
  121. }
  122. for (x = edgeB; x <= lastx; x++) {
  123. MOVE_ACC(acc, x - radius - 1, lastx);
  124. ADD_FAR(bulk, acc, x - radius - 1, lastx);
  125. SAVE(x, bulk);
  126. }
  127. }
  128. else
  129. {
  130. for (x = 0; x < edgeB; x++) {
  131. MOVE_ACC(acc, 0, x + radius);
  132. ADD_FAR(bulk, acc, 0, x + radius + 1);
  133. SAVE(x, bulk);
  134. }
  135. for (x = edgeB; x < edgeA; x++) {
  136. MOVE_ACC(acc, 0, lastx);
  137. ADD_FAR(bulk, acc, 0, lastx);
  138. SAVE(x, bulk);
  139. }
  140. for (x = edgeA; x <= lastx; x++) {
  141. MOVE_ACC(acc, x - radius - 1, lastx);
  142. ADD_FAR(bulk, acc, x - radius - 1, lastx);
  143. SAVE(x, bulk);
  144. }
  145. }
  146. #undef MOVE_ACC
  147. #undef ADD_FAR
  148. #undef SAVE
  149. }
  150. Imaging
  151. ImagingHorizontalBoxBlur(Imaging imOut, Imaging imIn, float floatRadius)
  152. {
  153. ImagingSectionCookie cookie;
  154. int y;
  155. int radius = (int) floatRadius;
  156. UINT32 ww = (UINT32) (1 << 24) / (floatRadius * 2 + 1);
  157. UINT32 fw = ((1 << 24) - (radius * 2 + 1) * ww) / 2;
  158. int edgeA = MIN(radius + 1, imIn->xsize);
  159. int edgeB = MAX(imIn->xsize - radius - 1, 0);
  160. UINT32 *lineOut = calloc(imIn->xsize, sizeof(UINT32));
  161. if (lineOut == NULL)
  162. return ImagingError_MemoryError();
  163. // printf(">>> %d %d %d\n", radius, ww, fw);
  164. ImagingSectionEnter(&cookie);
  165. if (imIn->image8)
  166. {
  167. for (y = 0; y < imIn->ysize; y++) {
  168. ImagingLineBoxBlur8(
  169. (imIn == imOut ? (UINT8 *) lineOut : imOut->image8[y]),
  170. imIn->image8[y],
  171. imIn->xsize - 1,
  172. radius, edgeA, edgeB,
  173. ww, fw
  174. );
  175. if (imIn == imOut) {
  176. // Commit.
  177. memcpy(imOut->image8[y], lineOut, imIn->xsize);
  178. }
  179. }
  180. }
  181. else
  182. {
  183. for (y = 0; y < imIn->ysize; y++) {
  184. ImagingLineBoxBlur32(
  185. imIn == imOut ? (pixel *) lineOut : (pixel *) imOut->image32[y],
  186. (pixel *) imIn->image32[y],
  187. imIn->xsize - 1,
  188. radius, edgeA, edgeB,
  189. ww, fw
  190. );
  191. if (imIn == imOut) {
  192. // Commit.
  193. memcpy(imOut->image32[y], lineOut, imIn->xsize * 4);
  194. }
  195. }
  196. }
  197. ImagingSectionLeave(&cookie);
  198. free(lineOut);
  199. return imOut;
  200. }
  201. Imaging
  202. ImagingBoxBlur(Imaging imOut, Imaging imIn, float radius, int n)
  203. {
  204. int i;
  205. Imaging imTransposed;
  206. if (n < 1) {
  207. return ImagingError_ValueError(
  208. "number of passes must be greater than zero"
  209. );
  210. }
  211. if (strcmp(imIn->mode, imOut->mode) ||
  212. imIn->type != imOut->type ||
  213. imIn->bands != imOut->bands ||
  214. imIn->xsize != imOut->xsize ||
  215. imIn->ysize != imOut->ysize)
  216. return ImagingError_Mismatch();
  217. if (imIn->type != IMAGING_TYPE_UINT8)
  218. return ImagingError_ModeError();
  219. if (!(strcmp(imIn->mode, "RGB") == 0 ||
  220. strcmp(imIn->mode, "RGBA") == 0 ||
  221. strcmp(imIn->mode, "RGBa") == 0 ||
  222. strcmp(imIn->mode, "RGBX") == 0 ||
  223. strcmp(imIn->mode, "CMYK") == 0 ||
  224. strcmp(imIn->mode, "L") == 0 ||
  225. strcmp(imIn->mode, "LA") == 0 ||
  226. strcmp(imIn->mode, "La") == 0))
  227. return ImagingError_ModeError();
  228. imTransposed = ImagingNewDirty(imIn->mode, imIn->ysize, imIn->xsize);
  229. if (!imTransposed)
  230. return NULL;
  231. /* Apply blur in one dimension.
  232. Use imOut as a destination at first pass,
  233. then use imOut as a source too. */
  234. ImagingHorizontalBoxBlur(imOut, imIn, radius);
  235. for (i = 1; i < n; i ++) {
  236. ImagingHorizontalBoxBlur(imOut, imOut, radius);
  237. }
  238. /* Transpose result for blur in another direction. */
  239. ImagingTranspose(imTransposed, imOut);
  240. /* Reuse imTransposed as a source and destination there. */
  241. for (i = 0; i < n; i ++) {
  242. ImagingHorizontalBoxBlur(imTransposed, imTransposed, radius);
  243. }
  244. /* Restore original orientation. */
  245. ImagingTranspose(imOut, imTransposed);
  246. ImagingDelete(imTransposed);
  247. return imOut;
  248. }
  249. Imaging ImagingGaussianBlur(Imaging imOut, Imaging imIn, float radius,
  250. int passes)
  251. {
  252. float sigma2, L, l, a;
  253. sigma2 = radius * radius / passes;
  254. // from http://www.mia.uni-saarland.de/Publications/gwosdek-ssvm11.pdf
  255. // [7] Box length.
  256. L = sqrt(12.0 * sigma2 + 1.0);
  257. // [11] Integer part of box radius.
  258. l = floor((L - 1.0) / 2.0);
  259. // [14], [Fig. 2] Fractional part of box radius.
  260. a = (2 * l + 1) * (l * (l + 1) - 3 * sigma2);
  261. a /= 6 * (sigma2 - (l + 1) * (l + 1));
  262. return ImagingBoxBlur(imOut, imIn, l + a, passes);
  263. }