convolve.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. Copyright (C) 2006 Pedro Felzenszwalb
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. /* convolution */
  16. #ifndef CONVOLVE_H
  17. #define CONVOLVE_H
  18. #include <vector>
  19. #include <algorithm>
  20. #include <cmath>
  21. #include "image.h"
  22. /* convolve src with mask. dst is flipped! */
  23. static void convolve_even(image<float> *src, image<float> *dst,
  24. std::vector<float> &mask) {
  25. int width = src->width();
  26. int height = src->height();
  27. int len = mask.size();
  28. for (int y = 0; y < height; y++) {
  29. for (int x = 0; x < width; x++) {
  30. float sum = mask[0] * imRef(src, x, y);
  31. for (int i = 1; i < len; i++) {
  32. sum += mask[i] *
  33. (imRef(src, std::max(x-i,0), y) +
  34. imRef(src, std::min(x+i, width-1), y));
  35. }
  36. imRef(dst, y, x) = sum;
  37. }
  38. }
  39. }
  40. /* convolve src with mask. dst is flipped! */
  41. static void convolve_odd(image<float> *src, image<float> *dst,
  42. std::vector<float> &mask) {
  43. int width = src->width();
  44. int height = src->height();
  45. int len = mask.size();
  46. for (int y = 0; y < height; y++) {
  47. for (int x = 0; x < width; x++) {
  48. float sum = mask[0] * imRef(src, x, y);
  49. for (int i = 1; i < len; i++) {
  50. sum += mask[i] *
  51. (imRef(src, std::max(x-i,0), y) -
  52. imRef(src, std::min(x+i, width-1), y));
  53. }
  54. imRef(dst, y, x) = sum;
  55. }
  56. }
  57. }
  58. #endif