Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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