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.
 
 
 
 
 
 

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