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.
 
 
 
 
 
 

317 lines
9.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. * 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 *SUFFIX(conv)(pipi_image_t *src,
  96. int m, int n, double mat[])
  97. {
  98. pipi_image_t *dst;
  99. pipi_pixels_t *srcp, *dstp;
  100. float *srcdata, *dstdata;
  101. int x, y, i, j, w, h;
  102. w = src->w;
  103. h = src->h;
  104. srcp = FLAG_GRAY ? pipi_getpixels(src, PIPI_PIXELS_Y_F)
  105. : pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
  106. srcdata = (float *)srcp->pixels;
  107. dst = pipi_new(w, h);
  108. dstp = FLAG_GRAY ? pipi_getpixels(dst, PIPI_PIXELS_Y_F)
  109. : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  110. dstdata = (float *)dstp->pixels;
  111. for(y = 0; y < h; y++)
  112. {
  113. for(x = 0; x < w; x++)
  114. {
  115. if(FLAG_GRAY)
  116. {
  117. double Y = 0.;
  118. int x2, y2;
  119. for(j = 0; j < n; j++)
  120. {
  121. y2 = y + j - n / 2;
  122. if(y2 < 0) y2 = FLAG_WRAP ? h - 1 - ((-y2 - 1) % h) : 0;
  123. else if(y2 >= h) y2 = FLAG_WRAP ? y2 % h : h - 1;
  124. for(i = 0; i < m; i++)
  125. {
  126. x2 = x + i - m / 2;
  127. if(x2 < 0) x2 = FLAG_WRAP ? w - 1 - ((-x2 - 1) % w) : 0;
  128. else if(x2 >= w) x2 = FLAG_WRAP ? x2 % w : w - 1;
  129. Y += mat[j * m + i] * srcdata[y2 * w + x2];
  130. }
  131. }
  132. dstdata[y * w + x] = Y < 0.0 ? 0.0 : Y > 1.0 ? 1.0 : Y;
  133. }
  134. else
  135. {
  136. double R = 0., G = 0., B = 0.;
  137. int x2, y2, off = 4 * (y * w + x);
  138. for(j = 0; j < n; j++)
  139. {
  140. y2 = y + j - n / 2;
  141. if(y2 < 0) y2 = FLAG_WRAP ? h - 1 - ((-y2 - 1) % h) : 0;
  142. else if(y2 >= h) y2 = FLAG_WRAP ? y2 % h : h - 1;
  143. for(i = 0; i < m; i++)
  144. {
  145. double f = mat[j * m + i];
  146. x2 = x + i - m / 2;
  147. if(x2 < 0) x2 = FLAG_WRAP ? w - 1 - ((-x2 - 1) % w) : 0;
  148. else if(x2 >= w) x2 = FLAG_WRAP ? x2 % w : w - 1;
  149. R += f * srcdata[(y2 * w + x2) * 4];
  150. G += f * srcdata[(y2 * w + x2) * 4 + 1];
  151. B += f * srcdata[(y2 * w + x2) * 4 + 2];
  152. }
  153. }
  154. dstdata[off] = R < 0.0 ? 0.0 : R > 1.0 ? 1.0 : R;
  155. dstdata[off + 1] = G < 0.0 ? 0.0 : G > 1.0 ? 1.0 : G;
  156. dstdata[off + 2] = B < 0.0 ? 0.0 : B > 1.0 ? 1.0 : B;
  157. }
  158. }
  159. }
  160. return dst;
  161. }
  162. static pipi_image_t *SUFFIX(sepconv)(pipi_image_t *src,
  163. int m, double hvec[], int n, double vvec[])
  164. {
  165. pipi_image_t *dst;
  166. pipi_pixels_t *srcp, *dstp;
  167. float *srcdata, *dstdata;
  168. double *buffer;
  169. int x, y, i, j, w, h;
  170. w = src->w;
  171. h = src->h;
  172. srcp = FLAG_GRAY ? pipi_getpixels(src, PIPI_PIXELS_Y_F)
  173. : pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
  174. srcdata = (float *)srcp->pixels;
  175. dst = pipi_new(w, h);
  176. dstp = FLAG_GRAY ? pipi_getpixels(dst, PIPI_PIXELS_Y_F)
  177. : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  178. dstdata = (float *)dstp->pixels;
  179. buffer = malloc(w * h * (FLAG_GRAY ? 1 : 4) * sizeof(double));
  180. for(y = 0; y < h; y++)
  181. {
  182. for(x = 0; x < w; x++)
  183. {
  184. if(FLAG_GRAY)
  185. {
  186. double Y = 0.;
  187. int x2;
  188. for(i = 0; i < m; i++)
  189. {
  190. x2 = x + i - m / 2;
  191. if(x2 < 0) x2 = FLAG_WRAP ? w - 1 - ((-x2 - 1) % w) : 0;
  192. else if(x2 >= w) x2 = FLAG_WRAP ? x2 % w : w - 1;
  193. Y += hvec[i] * srcdata[y * w + x2];
  194. }
  195. buffer[y * w + x] = Y;
  196. }
  197. else
  198. {
  199. double R = 0., G = 0., B = 0.;
  200. int x2, off = 4 * (y * w + x);
  201. for(i = 0; i < m; i++)
  202. {
  203. double f = hvec[i];
  204. x2 = x + i - m / 2;
  205. if(x2 < 0) x2 = FLAG_WRAP ? w - 1 - ((-x2 - 1) % w) : 0;
  206. else if(x2 >= w) x2 = FLAG_WRAP ? x2 % w : w - 1;
  207. R += f * srcdata[(y * w + x2) * 4];
  208. G += f * srcdata[(y * w + x2) * 4 + 1];
  209. B += f * srcdata[(y * w + x2) * 4 + 2];
  210. }
  211. buffer[off] = R;
  212. buffer[off + 1] = G;
  213. buffer[off + 2] = B;
  214. }
  215. }
  216. }
  217. for(y = 0; y < h; y++)
  218. {
  219. for(x = 0; x < w; x++)
  220. {
  221. if(FLAG_GRAY)
  222. {
  223. double Y = 0.;
  224. int y2;
  225. for(j = 0; j < n; j++)
  226. {
  227. y2 = y + j - n / 2;
  228. if(y2 < 0) y2 = FLAG_WRAP ? h - 1 - ((-y2 - 1) % h) : 0;
  229. else if(y2 >= h) y2 = FLAG_WRAP ? y2 % h : h - 1;
  230. Y += vvec[j] * buffer[y2 * w + x];
  231. }
  232. dstdata[y * w + x] = Y < 0.0 ? 0.0 : Y > 1.0 ? 1.0 : Y;
  233. }
  234. else
  235. {
  236. double R = 0., G = 0., B = 0.;
  237. int y2, off = 4 * (y * w + x);
  238. for(j = 0; j < n; j++)
  239. {
  240. double f = vvec[j];
  241. y2 = y + j - n / 2;
  242. if(y2 < 0) y2 = FLAG_WRAP ? h - 1 - ((-y2 - 1) % h) : 0;
  243. else if(y2 >= h) y2 = FLAG_WRAP ? y2 % h : h - 1;
  244. R += f * buffer[(y2 * w + x) * 4];
  245. G += f * buffer[(y2 * w + x) * 4 + 1];
  246. B += f * buffer[(y2 * w + x) * 4 + 2];
  247. }
  248. dstdata[off] = R < 0.0 ? 0.0 : R > 1.0 ? 1.0 : R;
  249. dstdata[off + 1] = G < 0.0 ? 0.0 : G > 1.0 ? 1.0 : G;
  250. dstdata[off + 2] = B < 0.0 ? 0.0 : B > 1.0 ? 1.0 : B;
  251. }
  252. }
  253. }
  254. free(buffer);
  255. return dst;
  256. }
  257. #endif