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.

convolution.c 7.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. * convolution.c: generic convolution 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. static pipi_image_t *pipi_convolution_standard(pipi_image_t *src,
  26. int m, int n, double mat[]);
  27. static pipi_image_t *pipi_convolution_separable(pipi_image_t *src,
  28. int m, double hvec[],
  29. int n, double vvec[]);
  30. pipi_image_t *pipi_convolution(pipi_image_t *src, int m, int n, double mat[])
  31. {
  32. double tmp;
  33. double *hvec, *vvec;
  34. int i, j, besti = -1, bestj = -1;
  35. /* Find the cell with the largest value */
  36. tmp = 0.0;
  37. for(i = 0; i < m * n; i++)
  38. if(mat[i] * mat[i] > tmp)
  39. {
  40. tmp = mat[i] * mat[i];
  41. besti = i % m;
  42. bestj = i / m;
  43. }
  44. /* If the kernel is empty, return an empty picture */
  45. if(tmp == 0.0)
  46. return pipi_new(src->w, src->h);
  47. /* Check whether the matrix rank is 1 */
  48. for(j = 0; j < n; j++)
  49. {
  50. if(j == bestj)
  51. continue;
  52. for(i = 0; i < m; i++)
  53. {
  54. double p, q;
  55. if(i == besti)
  56. continue;
  57. p = mat[j * m + i] * mat[bestj * m + besti];
  58. q = mat[bestj * m + i] * mat[j * m + besti];
  59. if(fabs(p - q) > 0.0001 * 0.0001)
  60. return pipi_convolution_standard(src, m, n, mat);
  61. }
  62. }
  63. printf("separabble!\n");
  64. /* Matrix rank is 1! Separate the filter */
  65. /* FIXME: memleak */
  66. hvec = malloc(m * sizeof(double));
  67. vvec = malloc(n * sizeof(double));
  68. tmp = sqrt(fabs(mat[bestj * m + besti]));
  69. for(i = 0; i < m; i++)
  70. hvec[i] = mat[bestj * m + i] / tmp;
  71. for(j = 0; j < n; j++)
  72. vvec[j] = mat[j * m + besti] / tmp;
  73. return pipi_convolution_separable(src, m, hvec, n, vvec);
  74. }
  75. static pipi_image_t *pipi_convolution_standard(pipi_image_t *src,
  76. int m, int n, double mat[])
  77. {
  78. pipi_image_t *dst;
  79. pipi_pixels_t *srcp, *dstp;
  80. float *srcdata, *dstdata;
  81. int x, y, i, j, w, h, gray;
  82. w = src->w;
  83. h = src->h;
  84. gray = (src->last_modified == PIPI_PIXELS_Y_F);
  85. srcp = gray ? pipi_getpixels(src, PIPI_PIXELS_Y_F)
  86. : pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
  87. srcdata = (float *)srcp->pixels;
  88. dst = pipi_new(w, h);
  89. dstp = gray ? pipi_getpixels(dst, PIPI_PIXELS_Y_F)
  90. : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  91. dstdata = (float *)dstp->pixels;
  92. for(y = 0; y < h; y++)
  93. {
  94. for(x = 0; x < w; x++)
  95. {
  96. if(gray)
  97. {
  98. double Y = 0.;
  99. int x2, y2;
  100. for(j = 0; j < n; j++)
  101. {
  102. y2 = y + j - n / 2;
  103. if(y2 < 0) y2 = 0;
  104. else if(y2 >= h) y2 = h - 1;
  105. for(i = 0; i < m; i++)
  106. {
  107. x2 = x + i - m / 2;
  108. if(x2 < 0) x2 = 0;
  109. else if(x2 >= w) x2 = w - 1;
  110. Y += mat[j * m + i] * srcdata[y2 * w + x2];
  111. }
  112. }
  113. dstdata[y * w + x] = Y;
  114. }
  115. else
  116. {
  117. double R = 0., G = 0., B = 0.;
  118. int x2, y2, off = 4 * (y * w + x);
  119. for(j = 0; j < n; j++)
  120. {
  121. y2 = y + j - n / 2;
  122. if(y2 < 0) y2 = 0;
  123. else if(y2 >= h) y2 = h - 1;
  124. for(i = 0; i < m; i++)
  125. {
  126. double f = mat[j * m + i];
  127. x2 = x + i - m / 2;
  128. if(x2 < 0) x2 = 0;
  129. else if(x2 >= w) x2 = w - 1;
  130. R += f * srcdata[(y2 * w + x2) * 4];
  131. G += f * srcdata[(y2 * w + x2) * 4 + 1];
  132. B += f * srcdata[(y2 * w + x2) * 4 + 2];
  133. }
  134. }
  135. dstdata[off] = R;
  136. dstdata[off + 1] = G;
  137. dstdata[off + 2] = B;
  138. }
  139. }
  140. }
  141. return dst;
  142. }
  143. static pipi_image_t *pipi_convolution_separable(pipi_image_t *src,
  144. int m, double hvec[],
  145. int n, double vvec[])
  146. {
  147. pipi_image_t *dst;
  148. pipi_pixels_t *srcp, *dstp;
  149. float *srcdata, *dstdata;
  150. double *buffer;
  151. int x, y, i, j, w, h, gray;
  152. w = src->w;
  153. h = src->h;
  154. gray = (src->last_modified == PIPI_PIXELS_Y_F);
  155. srcp = gray ? pipi_getpixels(src, PIPI_PIXELS_Y_F)
  156. : pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
  157. srcdata = (float *)srcp->pixels;
  158. dst = pipi_new(w, h);
  159. dstp = gray ? pipi_getpixels(dst, PIPI_PIXELS_Y_F)
  160. : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  161. dstdata = (float *)dstp->pixels;
  162. buffer = malloc(w * h * (gray ? 1 : 4) * sizeof(double));
  163. for(y = 0; y < h; y++)
  164. {
  165. for(x = 0; x < w; x++)
  166. {
  167. if(gray)
  168. {
  169. double Y = 0.;
  170. int x2;
  171. for(i = 0; i < m; i++)
  172. {
  173. x2 = x + i - m / 2;
  174. if(x2 < 0) x2 = 0;
  175. else if(x2 >= w) x2 = w - 1;
  176. Y += hvec[i] * srcdata[y * w + x2];
  177. }
  178. buffer[y * w + x] = Y;
  179. }
  180. else
  181. {
  182. double R = 0., G = 0., B = 0.;
  183. int x2, off = 4 * (y * w + x);
  184. for(i = 0; i < m; i++)
  185. {
  186. double f = hvec[i];
  187. x2 = x + i - m / 2;
  188. if(x2 < 0) x2 = 0;
  189. else if(x2 >= w) x2 = w - 1;
  190. R += f * srcdata[(y * w + x2) * 4];
  191. G += f * srcdata[(y * w + x2) * 4 + 1];
  192. B += f * srcdata[(y * w + x2) * 4 + 2];
  193. }
  194. buffer[off] = R;
  195. buffer[off + 1] = G;
  196. buffer[off + 2] = B;
  197. }
  198. }
  199. }
  200. for(y = 0; y < h; y++)
  201. {
  202. for(x = 0; x < w; x++)
  203. {
  204. if(gray)
  205. {
  206. double Y = 0.;
  207. int y2;
  208. for(j = 0; j < n; j++)
  209. {
  210. y2 = y + j - n / 2;
  211. if(y2 < 0) y2 = 0;
  212. else if(y2 >= h) y2 = h - 1;
  213. Y += vvec[j] * buffer[y2 * w + x];
  214. }
  215. dstdata[y * w + x] = Y;
  216. }
  217. else
  218. {
  219. double R = 0., G = 0., B = 0.;
  220. int y2, off = 4 * (y * w + x);
  221. for(j = 0; j < n; j++)
  222. {
  223. double f = vvec[j];
  224. y2 = y + j - n / 2;
  225. if(y2 < 0) y2 = 0;
  226. else if(y2 >= h) y2 = h - 1;
  227. R += f * buffer[(y2 * w + x) * 4];
  228. G += f * buffer[(y2 * w + x) * 4 + 1];
  229. B += f * buffer[(y2 * w + x) * 4 + 2];
  230. }
  231. dstdata[off] = R;
  232. dstdata[off + 1] = G;
  233. dstdata[off + 2] = B;
  234. }
  235. }
  236. }
  237. free(buffer);
  238. return dst;
  239. }