Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

gaussblur.cpp

00001 /***************************************************************************
00002                         gaussblur.cpp  -  description
00003                             -------------------
00004     begin                : july 2nd, 2006
00005     copyright            : (C) 2006 by Frédéric RODRIGO
00006     email                : f.rodrigo free.fr
00007     
00008     $Id: gaussblur.cpp 83 2006-11-05 20:46:42Z neoneurone $
00009  ***************************************************************************/
00010 
00011 /***************************************************************************
00012  *                                                                         *
00013  *   This program is free software; you can redistribute it and/or modify  *
00014  *   it under the terms of the GNU General Public License as published by  *
00015  *   the Free Software Foundation; either version 2 of the License, or     *
00016  *   any later version.                                                    *
00017  *                                                                         *
00018  ***************************************************************************/
00019 
00020 #include "gaussblur.h"
00021 
00022 #include <cmath>
00023 
00024 namespace MapGen
00025 {
00026 
00027    /*=====================================================================*/
00028 GaussBlur::GaussBlur( const uint length ):
00029 _length(length)
00030 {
00031     _convulsion = _blur( _length );
00032 }
00033 
00034 
00035    /*=====================================================================*/
00036 GaussBlur::~GaussBlur()
00037 {
00038     delete [] _convulsion;
00039 }
00040 
00041 
00042    /*=====================================================================*/
00043 void GaussBlur::apply( Map* map )
00044 {
00045 
00046     Map* tMap = new Map( map->getW(), map->getH() );
00047 
00048     for( uint x=0 ; x<map->getW() ; ++x )
00049         for( uint y=0 ; y<map->getH() ; ++y )
00050         {
00051             float sum = 0;
00052             for( uint i=0; i<_length*2+1 ; ++i )
00053                 sum += _convulsion[i] * map->getAt( x+i-_length, y );
00054                 tMap->setAt( x, y, sum );
00055         }
00056 
00057     for( uint x=0 ; x<map->getW() ; ++x )
00058         for( uint y=0 ; y<map->getH() ; ++y )
00059         {
00060             float sum = 0;
00061             for( uint i=0; i<_length*2+1 ; ++i )
00062                 sum += _convulsion[i] * tMap->getAt( x, y+i-_length );
00063                 map->setAt( x, y, sum );
00064         }
00065 
00066     delete tMap;
00067 }
00068 
00069 
00070    /*=====================================================================*/
00071 float GaussBlur::_gauss(
00072     const uint x,
00073     const uint width )
00074 {
00075     return exp( -1.0 / width * x * x );
00076 }
00077 
00078 
00079    /*=====================================================================*/
00080 float *GaussBlur::_blur( const uint length )
00081 {
00082     float *weights = new float[length*2+1];
00083     float sum = 0;
00084 
00085     for( uint x=0 ; x<length*2+1 ; ++x )
00086         sum += weights[x] = _gauss(x-length,length);
00087 
00088     for( uint x=0 ; x<length*2+1 ; ++x )
00089             weights[x] /= sum;
00090 
00091     return weights;
00092 }
00093 
00094 }

Generated on Sat Nov 11 10:21:09 2006 for OpenCity by  doxygen 1.4.2