25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

290 satır
8.3 KiB

  1. /*
  2. * libpipi Pathetic image processing interface 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 <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include "pipi.h"
  23. #include "pipi_internals.h"
  24. #if !defined TEMPLATE_FILE /* This file uses the template system */
  25. #define TEMPLATE_FLAGS SET_FLAG_GRAY | SET_FLAG_WRAP
  26. #define TEMPLATE_FILE "filter/convolution.c"
  27. #include "pipi_template.h"
  28. pipi_image_t *pipi_convolution(pipi_image_t *src, int m, int n, double mat[])
  29. {
  30. pipi_image_t *ret;
  31. double tmp;
  32. double *hvec, *vvec;
  33. int i, j, besti = -1, bestj = -1;
  34. /* Find the cell with the largest value */
  35. tmp = 0.0;
  36. for(i = 0; i < m * n; i++)
  37. if(mat[i] * mat[i] > tmp)
  38. {
  39. tmp = mat[i] * mat[i];
  40. besti = i % m;
  41. bestj = i / m;
  42. }
  43. /* If the kernel is empty, return an empty picture */
  44. if(tmp == 0.0)
  45. return pipi_new(src->w, src->h);
  46. /* Check whether the matrix rank is 1 */
  47. for(j = 0; j < n; j++)
  48. {
  49. if(j == bestj)
  50. continue;
  51. for(i = 0; i < m; i++)
  52. {
  53. double p, q;
  54. if(i == besti)
  55. continue;
  56. p = mat[j * m + i] * mat[bestj * m + besti];
  57. q = mat[bestj * m + i] * mat[j * m + besti];
  58. if(fabs(p - q) > 0.0001 * 0.0001)
  59. {
  60. if(src->last_modified == PIPI_PIXELS_Y_F32)
  61. {
  62. if(src->wrap)
  63. return conv_gray_wrap(src, m, n, mat);
  64. return conv_gray(src, m, n, mat);
  65. }
  66. else
  67. {
  68. if(src->wrap)
  69. return conv_wrap(src, m, n, mat);
  70. return conv(src, m, n, mat);
  71. }
  72. }
  73. }
  74. }
  75. /* Matrix rank is 1! Separate the filter */
  76. hvec = malloc(m * sizeof(double));
  77. vvec = malloc(n * sizeof(double));
  78. tmp = sqrt(fabs(mat[bestj * m + besti]));
  79. for(i = 0; i < m; i++)
  80. hvec[i] = mat[bestj * m + i] / tmp;
  81. for(j = 0; j < n; j++)
  82. vvec[j] = mat[j * m + besti] / tmp;
  83. if(src->last_modified == PIPI_PIXELS_Y_F32)
  84. ret = src->wrap ? sepconv_gray_wrap(src, m, hvec, n, vvec)
  85. : sepconv_gray(src, m, hvec, n, vvec);
  86. else
  87. ret = src->wrap ? sepconv_wrap(src, m, hvec, n, vvec)
  88. : sepconv(src, m, hvec, n, vvec);
  89. free(hvec);
  90. free(vvec);
  91. return ret;
  92. }
  93. #else /* XXX: the following functions use the template system */
  94. static pipi_image_t *T(conv)(pipi_image_t *src, int m, int n, double mat[])
  95. {
  96. pipi_image_t *dst;
  97. pipi_pixels_t *srcp, *dstp;
  98. float *srcdata, *dstdata;
  99. int x, y, i, j, w, h;
  100. w = src->w;
  101. h = src->h;
  102. srcp = FLAG_GRAY ? pipi_get_pixels(src, PIPI_PIXELS_Y_F32)
  103. : pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
  104. srcdata = (float *)srcp->pixels;
  105. dst = pipi_new(w, h);
  106. dstp = FLAG_GRAY ? pipi_get_pixels(dst, PIPI_PIXELS_Y_F32)
  107. : pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
  108. dstdata = (float *)dstp->pixels;
  109. for(y = 0; y < h; y++)
  110. {
  111. for(x = 0; x < w; x++)
  112. {
  113. double R = 0., G = 0., B = 0., A = 0.;
  114. double Y = 0.;
  115. int x2, y2, off = 4 * (y * w + x);
  116. for(j = 0; j < n; j++)
  117. {
  118. y2 = y + j - n / 2;
  119. if(y2 < 0) y2 = FLAG_WRAP ? h - 1 - ((-y2 - 1) % h) : 0;
  120. else if(y2 >= h) y2 = FLAG_WRAP ? y2 % h : h - 1;
  121. for(i = 0; i < m; i++)
  122. {
  123. double f = mat[j * m + i];
  124. x2 = x + i - m / 2;
  125. if(x2 < 0) x2 = FLAG_WRAP ? w - 1 - ((-x2 - 1) % w) : 0;
  126. else if(x2 >= w) x2 = FLAG_WRAP ? x2 % w : w - 1;
  127. if(FLAG_GRAY)
  128. Y += f * srcdata[y2 * w + x2];
  129. else
  130. {
  131. R += f * srcdata[(y2 * w + x2) * 4];
  132. G += f * srcdata[(y2 * w + x2) * 4 + 1];
  133. B += f * srcdata[(y2 * w + x2) * 4 + 2];
  134. A += f * srcdata[(y2 * w + x2) * 4 + 3];
  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. dstdata[off + 3] = A < 0.0 ? 0.0 : A > 1.0 ? 1.0 : A;
  146. }
  147. }
  148. }
  149. return dst;
  150. }
  151. static pipi_image_t *T(sepconv)(pipi_image_t *src,
  152. int m, double hvec[], int n, double vvec[])
  153. {
  154. pipi_image_t *dst;
  155. pipi_pixels_t *srcp, *dstp;
  156. float *srcdata, *dstdata;
  157. double *buffer;
  158. int x, y, i, j, w, h;
  159. w = src->w;
  160. h = src->h;
  161. srcp = FLAG_GRAY ? pipi_get_pixels(src, PIPI_PIXELS_Y_F32)
  162. : pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
  163. srcdata = (float *)srcp->pixels;
  164. dst = pipi_new(w, h);
  165. dstp = FLAG_GRAY ? pipi_get_pixels(dst, PIPI_PIXELS_Y_F32)
  166. : pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
  167. dstdata = (float *)dstp->pixels;
  168. buffer = malloc(w * h * (FLAG_GRAY ? 1 : 4) * sizeof(double));
  169. for(y = 0; y < h; y++)
  170. {
  171. for(x = 0; x < w; x++)
  172. {
  173. double R = 0., G = 0., B = 0., A = 0.;
  174. double Y = 0.;
  175. int x2, off = 4 * (y * w + x);
  176. for(i = 0; i < m; i++)
  177. {
  178. double f = hvec[i];
  179. x2 = x + i - m / 2;
  180. if(x2 < 0) x2 = FLAG_WRAP ? w - 1 - ((-x2 - 1) % w) : 0;
  181. else if(x2 >= w) x2 = FLAG_WRAP ? x2 % w : w - 1;
  182. if(FLAG_GRAY)
  183. Y += f * srcdata[y * w + x2];
  184. else
  185. {
  186. R += f * srcdata[(y * w + x2) * 4];
  187. G += f * srcdata[(y * w + x2) * 4 + 1];
  188. B += f * srcdata[(y * w + x2) * 4 + 2];
  189. A += f * srcdata[(y * w + x2) * 4 + 3];
  190. }
  191. }
  192. if(FLAG_GRAY)
  193. buffer[y * w + x] = Y;
  194. else
  195. {
  196. buffer[off] = R;
  197. buffer[off + 1] = G;
  198. buffer[off + 2] = B;
  199. buffer[off + 3] = A;
  200. }
  201. }
  202. }
  203. for(y = 0; y < h; y++)
  204. {
  205. for(x = 0; x < w; x++)
  206. {
  207. double R = 0., G = 0., B = 0., A = 0.;
  208. double Y = 0.;
  209. int y2, off = 4 * (y * w + x);
  210. for(j = 0; j < n; j++)
  211. {
  212. double f = vvec[j];
  213. y2 = y + j - n / 2;
  214. if(y2 < 0) y2 = FLAG_WRAP ? h - 1 - ((-y2 - 1) % h) : 0;
  215. else if(y2 >= h) y2 = FLAG_WRAP ? y2 % h : h - 1;
  216. if(FLAG_GRAY)
  217. Y += f * buffer[y2 * w + x];
  218. else
  219. {
  220. R += f * buffer[(y2 * w + x) * 4];
  221. G += f * buffer[(y2 * w + x) * 4 + 1];
  222. B += f * buffer[(y2 * w + x) * 4 + 2];
  223. A += f * buffer[(y2 * w + x) * 4 + 3];
  224. }
  225. }
  226. if(FLAG_GRAY)
  227. dstdata[y * w + x] = Y < 0.0 ? 0.0 : Y > 1.0 ? 1.0 : Y;
  228. else
  229. {
  230. dstdata[off] = R < 0.0 ? 0.0 : R > 1.0 ? 1.0 : R;
  231. dstdata[off + 1] = G < 0.0 ? 0.0 : G > 1.0 ? 1.0 : G;
  232. dstdata[off + 2] = B < 0.0 ? 0.0 : B > 1.0 ? 1.0 : B;
  233. dstdata[off + 3] = A < 0.0 ? 0.0 : A > 1.0 ? 1.0 : A;
  234. }
  235. }
  236. }
  237. free(buffer);
  238. return dst;
  239. }
  240. #endif