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.
 
 
 
 
 
 

174 lines
4.8 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. #define FLAG_GRAY ((FLAGS) & SET_FLAG_GRAY)
  26. #define FLAG_WRAP ((FLAGS) & SET_FLAG_WRAP)
  27. #define SET_FLAG_GRAY 0x01
  28. #define SET_FLAG_WRAP 0x02
  29. static pipi_image_t *conv_std_rgba_f(pipi_image_t *src,
  30. int m, int n, double mat[]);
  31. static pipi_image_t *conv_sep_rgba_f(pipi_image_t *src,
  32. int m, double hvec[],
  33. int n, double vvec[]);
  34. #undef FUNC1
  35. #undef FUNC2
  36. #undef PIXEL
  37. #undef FLAGS
  38. #define FUNC1 conv_std_rgba_f
  39. #define FUNC2 conv_sep_rgba_f
  40. #define PIXEL float
  41. #define FLAGS 0
  42. #include "convolution_template.h"
  43. static pipi_image_t *conv_std_y_f(pipi_image_t *src,
  44. int m, int n, double mat[]);
  45. static pipi_image_t *conv_sep_y_f(pipi_image_t *src,
  46. int m, double hvec[],
  47. int n, double vvec[]);
  48. #undef FUNC1
  49. #undef FUNC2
  50. #undef PIXEL
  51. #undef FLAGS
  52. #define FUNC1 conv_std_y_f
  53. #define FUNC2 conv_sep_y_f
  54. #define PIXEL float
  55. #define FLAGS SET_FLAG_GRAY
  56. #include "convolution_template.h"
  57. static pipi_image_t *wrap_std_rgba_f(pipi_image_t *src,
  58. int m, int n, double mat[]);
  59. static pipi_image_t *wrap_sep_rgba_f(pipi_image_t *src,
  60. int m, double hvec[],
  61. int n, double vvec[]);
  62. #undef FUNC1
  63. #undef FUNC2
  64. #undef PIXEL
  65. #undef FLAGS
  66. #define FUNC1 wrap_std_rgba_f
  67. #define FUNC2 wrap_sep_rgba_f
  68. #define PIXEL float
  69. #define FLAGS SET_FLAG_WRAP
  70. #include "convolution_template.h"
  71. static pipi_image_t *wrap_std_y_f(pipi_image_t *src,
  72. int m, int n, double mat[]);
  73. static pipi_image_t *wrap_sep_y_f(pipi_image_t *src,
  74. int m, double hvec[],
  75. int n, double vvec[]);
  76. #undef FUNC1
  77. #undef FUNC2
  78. #undef PIXEL
  79. #undef FLAGS
  80. #define FUNC1 wrap_std_y_f
  81. #define FUNC2 wrap_sep_y_f
  82. #define PIXEL float
  83. #define FLAGS SET_FLAG_GRAY|SET_FLAG_WRAP
  84. #include "convolution_template.h"
  85. pipi_image_t *pipi_convolution(pipi_image_t *src, int m, int n, double mat[])
  86. {
  87. pipi_image_t *ret;
  88. double tmp;
  89. double *hvec, *vvec;
  90. int i, j, besti = -1, bestj = -1;
  91. /* Find the cell with the largest value */
  92. tmp = 0.0;
  93. for(i = 0; i < m * n; i++)
  94. if(mat[i] * mat[i] > tmp)
  95. {
  96. tmp = mat[i] * mat[i];
  97. besti = i % m;
  98. bestj = i / m;
  99. }
  100. /* If the kernel is empty, return an empty picture */
  101. if(tmp == 0.0)
  102. return pipi_new(src->w, src->h);
  103. /* Check whether the matrix rank is 1 */
  104. for(j = 0; j < n; j++)
  105. {
  106. if(j == bestj)
  107. continue;
  108. for(i = 0; i < m; i++)
  109. {
  110. double p, q;
  111. if(i == besti)
  112. continue;
  113. p = mat[j * m + i] * mat[bestj * m + besti];
  114. q = mat[bestj * m + i] * mat[j * m + besti];
  115. if(fabs(p - q) > 0.0001 * 0.0001)
  116. {
  117. if(src->last_modified == PIPI_PIXELS_Y_F)
  118. {
  119. if(src->wrap)
  120. return wrap_std_y_f(src, m, n, mat);
  121. return conv_std_y_f(src, m, n, mat);
  122. }
  123. else
  124. {
  125. if(src->wrap)
  126. return wrap_std_rgba_f(src, m, n, mat);
  127. return conv_std_rgba_f(src, m, n, mat);
  128. }
  129. }
  130. }
  131. }
  132. /* Matrix rank is 1! Separate the filter */
  133. hvec = malloc(m * sizeof(double));
  134. vvec = malloc(n * sizeof(double));
  135. tmp = sqrt(fabs(mat[bestj * m + besti]));
  136. for(i = 0; i < m; i++)
  137. hvec[i] = mat[bestj * m + i] / tmp;
  138. for(j = 0; j < n; j++)
  139. vvec[j] = mat[j * m + besti] / tmp;
  140. if(src->last_modified == PIPI_PIXELS_Y_F)
  141. ret = src->wrap ? wrap_sep_y_f(src, m, hvec, n, vvec)
  142. : conv_sep_y_f(src, m, hvec, n, vvec);
  143. else
  144. ret = src->wrap ? wrap_sep_rgba_f(src, m, hvec, n, vvec)
  145. : conv_sep_rgba_f(src, m, hvec, n, vvec);
  146. free(hvec);
  147. free(vvec);
  148. return ret;
  149. }