degate  0.1.2
FilterKernel.h
Go to the documentation of this file.
00001 /* -*-c++-*-
00002 
00003  This file is part of the IC reverse engineering tool degate.
00004 
00005  Copyright 2008, 2009, 2010 by Martin Schobert
00006 
00007  Degate is free software: you can redistribute it and/or modify
00008  it under the terms of the GNU General Public License as published by
00009  the Free Software Foundation, either version 3 of the License, or
00010  any later version.
00011 
00012  Degate is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU General Public License for more details.
00016 
00017  You should have received a copy of the GNU General Public License
00018  along with degate. If not, see <http://www.gnu.org/licenses/>.
00019 
00020 */
00021 
00022 #ifndef __FILTERKERNEL_H__
00023 #define __FILTERKERNEL_H__
00024 
00025 #include <vector>
00026 #include <memory>
00027 #include <iostream>
00028 #include <math.h>
00029 #include <boost/format.hpp>
00030 
00031 namespace degate {
00032 
00033   /**
00034    * The class FilterKernel implemements a container for 2D filter kernel data.
00035    */
00036 
00037   class FilterKernel {
00038 
00039   private:
00040     unsigned int columns, rows;
00041     std::vector<double> data;
00042 
00043   public:
00044 
00045     FilterKernel(unsigned int _columns, unsigned int _rows) :
00046       columns(_columns),
00047       rows(_rows),
00048       data(_columns * _rows) {
00049     }
00050 
00051     virtual ~FilterKernel() {}
00052 
00053     inline unsigned int get_columns() const {
00054       return columns;
00055     }
00056 
00057     inline unsigned int get_rows() const {
00058       return rows;
00059     }
00060 
00061     inline unsigned int get_center_row() const {
00062       return rows >> 1;
00063     }
00064 
00065     inline unsigned int get_center_column() const {
00066       return columns >> 1;
00067     }
00068 
00069     inline double get(int column, int row) const {
00070       return data[row * columns + column];
00071     }
00072 
00073     inline void set(int column, int row, double val) {
00074       data[row * columns + column] = val;
00075     }
00076 
00077     void print() const {
00078       unsigned int x, y;
00079       for(y = 0; y < columns; y++) {
00080         for(x = 0; x < rows; x++) {
00081           boost::format f("%5.10f ");
00082           f % get(x, y);
00083           std::cout << f.str();
00084         }
00085         std::cout << std::endl;
00086       }
00087     }
00088   };
00089 
00090   typedef std::shared_ptr<FilterKernel> FilterKernel_shptr;
00091 
00092   class SobelXOperator : public FilterKernel {
00093   public:
00094     SobelXOperator() : FilterKernel(3, 3) {
00095       set(0, 0, 1);
00096       set(0, 1, 2);
00097       set(0, 2, 1);
00098 
00099       set(1, 0, 0);
00100       set(1, 1, 0);
00101       set(1, 2, 0);
00102 
00103       set(2, 0, -1);
00104       set(2, 1, -2);
00105       set(2, 2, -1);
00106     }
00107     virtual ~SobelXOperator() {}
00108   };
00109 
00110   typedef std::shared_ptr<SobelXOperator> SobelXOperator_shptr;
00111 
00112   class SobelYOperator : public FilterKernel {
00113   public:
00114     SobelYOperator() : FilterKernel(3, 3) {
00115       set(0, 0, 1);
00116       set(0, 1, 0);
00117       set(0, 2, -1);
00118 
00119       set(1, 0, 2);
00120       set(1, 1, 0);
00121       set(1, 2, -2);
00122 
00123       set(2, 0, 1);
00124       set(2, 1, 0);
00125       set(2, 2, -1);
00126     }
00127     virtual ~SobelYOperator() {}
00128   };
00129 
00130   typedef std::shared_ptr<SobelYOperator> SobelYOperator_shptr;
00131 
00132 
00133   class SobelOperator : public FilterKernel {
00134   public:
00135     SobelOperator() : FilterKernel(3, 3) {
00136       set(0, 0, 0);
00137       set(0, 1, 1);
00138       set(0, 2, 2);
00139 
00140       set(1, 0, -1);
00141       set(1, 1, 0);
00142       set(1, 2, 1);
00143 
00144       set(2, 0, -2);
00145       set(2, 1, -1);
00146       set(2, 2, 0);
00147     }
00148     virtual ~SobelOperator() {}
00149   };
00150 
00151   typedef std::shared_ptr<SobelOperator> SobelOperator_shptr;
00152 
00153   class GaussianBlur : public FilterKernel {
00154   public:
00155     GaussianBlur(unsigned int width, unsigned int height, double sigma = 1.4) :
00156       FilterKernel(width, height) {
00157       unsigned int x, y;
00158 
00159       for(y = 0; y < get_rows(); y++) {
00160         for(x = 0; x < get_columns(); x++) {
00161 
00162           double _x = (double)x - (double)get_center_column();
00163           double _y = (double)y - (double)get_center_row();
00164 
00165           double v = 1.0/(2.0 * M_PI * pow(sigma, 2)) * exp(-(pow(_x, 2) + pow(_y, 2)) / (2*pow(sigma,2)));
00166 
00167           set(x, y, v);
00168         }
00169 
00170       }
00171     }
00172 
00173     virtual ~GaussianBlur() {}
00174   };
00175 
00176   /**
00177    * Implements a Laplacian of Gaussian.
00178    */
00179   class LoG : public FilterKernel {
00180   public:
00181     LoG(unsigned int width, unsigned int height, double sigma = 1.4) : FilterKernel(width, height) {
00182       unsigned int x, y;
00183 
00184       for(y = 0; y < get_rows(); y++) {
00185         for(x = 0; x < get_columns(); x++) {
00186 
00187           double _x = (double)x - (double)get_center_column();
00188           double _y = (double)y - (double)get_center_row();
00189 
00190           double v =
00191             -1.0/(M_PI * pow(sigma, 4)) *
00192             (1.0 - (pow(_x, 2) + pow(_y, 2)) / (2.0*pow(sigma, 2))) *
00193             exp(-(pow(_x, 2) + pow(_y, 2)) / (2.0*pow(sigma,2)));
00194 
00195           set(x, y, v);
00196         }
00197 
00198       }
00199     }
00200 
00201    virtual ~LoG() {}
00202   };
00203 
00204   typedef std::shared_ptr<LoG> LoG_shptr;
00205 
00206 }
00207 
00208 #endif