Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 #ifndef CONVOLVE_H
00022 #define CONVOLVE_H
00023 
00024 #include <vector>
00025 #include <algorithm>
00026 #include <cmath>
00027 #include "image.h"
00028 
00029 
00030 static void convolve_even(image<float> *src, image<float> *dst, 
00031                           std::vector<float> &mask) {
00032   int width = src->width();
00033   int height = src->height();
00034   int len = mask.size();
00035 
00036   for (int y = 0; y < height; y++) {
00037     for (int x = 0; x < width; x++) {
00038       float sum = mask[0] * imRef(src, x, y);
00039       for (int i = 1; i < len; i++) {
00040         sum += mask[i] * 
00041           (imRef(src, std::max(x-i,0), y) + 
00042            imRef(src, std::min(x+i, width-1), y));
00043       }
00044       imRef(dst, y, x) = sum;
00045     }
00046   }
00047 }
00048 
00049 
00050 static void convolve_odd(image<float> *src, image<float> *dst, 
00051                          std::vector<float> &mask) {
00052   int width = src->width();
00053   int height = src->height();
00054   int len = mask.size();
00055 
00056   for (int y = 0; y < height; y++) {
00057     for (int x = 0; x < width; x++) {
00058       float sum = mask[0] * imRef(src, x, y);
00059       for (int i = 1; i < len; i++) {
00060         sum += mask[i] * 
00061           (imRef(src, std::max(x-i,0), y) - 
00062            imRef(src, std::min(x+i, width-1), y));
00063       }
00064       imRef(dst, y, x) = sum;
00065     }
00066   }
00067 }
00068 
00069 #endif