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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. #if !defined TEMPLATE_FILE /* This file uses the template system */
  26. #define TEMPLATE_FLAGS SET_FLAG_GRAY | SET_FLAG_WRAP
  27. #define TEMPLATE_FILE "filter/convolution.c"
  28. #include "pipi_template.h"
  29. pipi_image_t *pipi_convolution(pipi_image_t *src, int m, int n, double mat[])
  30. {
  31. pipi_image_t *ret;
  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. {
  61. if(src->last_modified == PIPI_PIXELS_Y_F)
  62. {
  63. if(src->wrap)
  64. return conv_gray_wrap(src, m, n, mat);
  65. return conv_gray(src, m, n, mat);
  66. }
  67. else
  68. {
  69. if(src->wrap)
  70. return conv_wrap(src, m, n, mat);
  71. return conv(src, m, n, mat);
  72. }
  73. }
  74. }
  75. }
  76. /* Matrix rank is 1! Separate the filter */
  77. hvec = malloc(m * sizeof(double));
  78. vvec = malloc(n * sizeof(double));
  79. tmp = sqrt(fabs(mat[bestj * m + besti]));
  80. for(i = 0; i < m; i++)
  81. hvec[i] = mat[bestj * m + i] / tmp;
  82. for(j = 0; j < n; j++)
  83. vvec[j] = mat[j * m + besti] / tmp;
  84. if(src->last_modified == PIPI_PIXELS_Y_F)
  85. ret = src->wrap ? sepconv_gray_wrap(src, m, hvec, n, vvec)
  86. : sepconv_gray(src, m, hvec, n, vvec);
  87. else
  88. ret = src->wrap ? sepconv_wrap(src, m, hvec, n, vvec)
  89. : sepconv(src, m, hvec, n, vvec);
  90. free(hvec);
  91. free(vvec);
  92. return ret;
  93. }
  94. #else /* XXX: the following functions use the template system */
  95. static pipi_image_t *T(conv)(pipi_image_t *src, int m, int n, double mat[])
  96. {
  97. pipi_image_t *dst;
  98. pipi_pixels_t *srcp, *dstp;
  99. float *srcdata, *dstdata;
  100. int x, y, i, j, w, h;
  101. w = src->w;
  102. h = src->h;
  103. srcp = FLAG_GRAY ? pipi_getpixels(src, PIPI_PIXELS_Y_F)
  104. : pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
  105. srcdata = (float *)srcp->pixels;
  106. dst = pipi_new(w, h);
  107. dstp = FLAG_GRAY ? pipi_getpixels(dst, PIPI_PIXELS_Y_F)
  108. : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  109. dstdata = (float *)dstp->pixels;
  110. for(y = 0; y < h; y++)
  111. {
  112. for(x = 0; x < w; x++)
  113. {
  114. double R = 0., G = 0., B = 0.;
  115. double Y = 0.;
  116. int x2, y2, off = 4 * (y * w + x);
  117. for(j = 0; j < n; j++)
  118. {
  119. y2 = y + j - n / 2;
  120. if(y2 < 0) y2 = FLAG_WRAP ? h - 1 - ((-y2 - 1) % h) : 0;
  121. else if(y2 >= h) y2 = FLAG_WRAP ? y2 % h : h - 1;
  122. for(i = 0; i < m; i++)
  123. {
  124. double f = mat[j * m + i];
  125. x2 = x + i - m / 2;
  126. if(x2 < 0) x2 = FLAG_WRAP ? w - 1 - ((-x2 - 1) % w) : 0;
  127. else if(x2 >= w) x2 = FLAG_WRAP ? x2 % w : w - 1;
  128. if(FLAG_GRAY)
  129. Y += f * srcdata[y2 * w + x2];
  130. else
  131. {
  132. R += f * srcdata[(y2 * w + x2) * 4];
  133. G += f * srcdata[(y2 * w + x2) * 4 + 1];
  134. B += f * srcdata[(y2 * w + x2) * 4 + 2];
  135. }
  136. }
  137. }
  138. if(FLAG_GRAY)
  139. dstdata[y * w + x] = Y < 0.0 ? 0.0 : Y > 1.0 ? 1.0 : Y;
  140. else
  141. {
  142. dstdata[off] = R < 0.0 ? 0.0 : R > 1.0 ? 1.0 : R;
  143. dstdata[off + 1] = G < 0.0 ? 0.0 : G > 1.0 ? 1.0 : G;
  144. dstdata[off + 2] = B < 0.0 ? 0.0 : B > 1.0 ? 1.0 : B;
  145. }
  146. }
  147. }
  148. return dst;
  149. }
  150. static pipi_image_t *T(sepconv)(pipi_image_t *src,
  151. int m, double hvec[], int n, double vvec[])
  152. {
  153. pipi_image_t *dst;
  154. pipi_pixels_t *srcp, *dstp;
  155. float *srcdata, *dstdata;
  156. double *buffer;
  157. int x, y, i, j, w, h;
  158. w = src->w;
  159. h = src->h;
  160. srcp = FLAG_GRAY ? pipi_getpixels(src, PIPI_PIXELS_Y_F)
  161. : pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
  162. srcdata = (float *)srcp->pixels;
  163. dst = pipi_new(w, h);
  164. dstp = FLAG_GRAY ? pipi_getpixels(dst, PIPI_PIXELS_Y_F)
  165. : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  166. dstdata = (float *)dstp->pixels;
  167. buffer = malloc(w * h * (FLAG_GRAY ? 1 : 4) * sizeof(double));
  168. for(y = 0; y < h; y++)
  169. {
  170. for(x = 0; x < w; x++)
  171. {
  172. double R = 0., G = 0., B = 0.;
  173. double Y = 0.;
  174. int x2, off = 4 * (y * w + x);
  175. for(i = 0; i < m; i++)
  176. {
  177. double f = hvec[i];
  178. x2 = x + i - m / 2;
  179. if(x2 < 0) x2 = FLAG_WRAP ? w - 1 - ((-x2 - 1) % w) : 0;
  180. else if(x2 >= w) x2 = FLAG_WRAP ? x2 % w : w - 1;
  181. if(FLAG_GRAY)
  182. Y += f * srcdata[y * w + x2];
  183. else
  184. {
  185. R += f * srcdata[(y * w + x2) * 4];
  186. G += f * srcdata[(y * w + x2) * 4 + 1];
  187. B += f * srcdata[(y * w + x2) * 4 + 2];
  188. }
  189. }
  190. if(FLAG_GRAY)
  191. buffer[y * w + x] = Y;
  192. else
  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. double R = 0., G = 0., B = 0.;
  205. double Y = 0.;
  206. int y2, off = 4 * (y * w + x);
  207. for(j = 0; j < n; j++)
  208. {
  209. double f = vvec[j];
  210. y2 = y + j - n / 2;
  211. if(y2 < 0) y2 = FLAG_WRAP ? h - 1 - ((-y2 - 1) % h) : 0;
  212. else if(y2 >= h) y2 = FLAG_WRAP ? y2 % h : h - 1;
  213. if(FLAG_GRAY)
  214. Y += f * buffer[y2 * w + x];
  215. else
  216. {
  217. R += f * buffer[(y2 * w + x) * 4];
  218. G += f * buffer[(y2 * w + x) * 4 + 1];
  219. B += f * buffer[(y2 * w + x) * 4 + 2];
  220. }
  221. }
  222. if(FLAG_GRAY)
  223. dstdata[y * w + x] = Y < 0.0 ? 0.0 : Y > 1.0 ? 1.0 : Y;
  224. else
  225. {
  226. dstdata[off] = R < 0.0 ? 0.0 : R > 1.0 ? 1.0 : R;
  227. dstdata[off + 1] = G < 0.0 ? 0.0 : G > 1.0 ? 1.0 : G;
  228. dstdata[off + 2] = B < 0.0 ? 0.0 : B > 1.0 ? 1.0 : B;
  229. }
  230. }
  231. }
  232. free(buffer);
  233. return dst;
  234. }
  235. #endif