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