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.
 
 
 
 
 
 

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