OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * ***** BEGIN GPL LICENSE BLOCK ***** |
| 3 * |
| 4 * This program is free software; you can redistribute it and/or |
| 5 * modify it under the terms of the GNU General Public License |
| 6 * as published by the Free Software Foundation; either version 2 |
| 7 * of the License, or (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 Foundation, |
| 16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 * |
| 18 * The Original Code is Copyright (C) 2010 Blender Foundation. |
| 19 * All rights reserved. |
| 20 * |
| 21 * The Original Code is: all of this file. |
| 22 * |
| 23 * Contributor(s): none yet. |
| 24 * |
| 25 * ***** END GPL LICENSE BLOCK ***** |
| 26 */ |
| 27 |
| 28 /** \file blender/freestyle/intern/image/GaussianFilter.cpp |
| 29 * \ingroup freestyle |
| 30 * \brief Class to perform gaussian filtering operations on an image |
| 31 * \author Stephane Grabli |
| 32 * \date 20/05/2003 |
| 33 */ |
| 34 |
| 35 #include <stdlib.h> |
| 36 |
| 37 #include "GaussianFilter.h" |
| 38 |
| 39 GaussianFilter::GaussianFilter(float iSigma) |
| 40 { |
| 41 _sigma = iSigma; |
| 42 _mask = 0; |
| 43 computeMask(); |
| 44 } |
| 45 |
| 46 GaussianFilter::GaussianFilter(const GaussianFilter& iBrother) |
| 47 { |
| 48 _sigma = iBrother._sigma; |
| 49 _maskSize = iBrother._maskSize; |
| 50 _bound = iBrother._bound; |
| 51 _storedMaskSize = iBrother._storedMaskSize; |
| 52 _mask = new float[_maskSize * _maskSize]; |
| 53 memcpy(_mask, iBrother._mask, _maskSize * _maskSize * sizeof(float)); |
| 54 } |
| 55 |
| 56 GaussianFilter& GaussianFilter::operator=(const GaussianFilter& iBrother) |
| 57 { |
| 58 _sigma = iBrother._sigma; |
| 59 _maskSize = iBrother._maskSize; |
| 60 _bound = iBrother._bound; |
| 61 _storedMaskSize = iBrother._storedMaskSize; |
| 62 _mask = new float[_storedMaskSize * _storedMaskSize]; |
| 63 memcpy(_mask, iBrother._mask, _storedMaskSize * _storedMaskSize * sizeof
(float)); |
| 64 return *this; |
| 65 } |
| 66 |
| 67 |
| 68 GaussianFilter::~GaussianFilter() |
| 69 { |
| 70 if (0 != _mask) { |
| 71 delete[] _mask; |
| 72 } |
| 73 } |
| 74 |
| 75 int GaussianFilter::computeMaskSize(float sigma) |
| 76 { |
| 77 int maskSize = (int)floor(4 * sigma) + 1; |
| 78 if (0 == (maskSize % 2)) |
| 79 ++maskSize; |
| 80 |
| 81 return maskSize; |
| 82 } |
| 83 |
| 84 void GaussianFilter::setSigma(float sigma) |
| 85 { |
| 86 _sigma = sigma; |
| 87 computeMask(); |
| 88 } |
| 89 |
| 90 void GaussianFilter::computeMask() |
| 91 { |
| 92 if (0 != _mask) { |
| 93 delete[] _mask; |
| 94 } |
| 95 |
| 96 _maskSize = computeMaskSize(_sigma); |
| 97 _storedMaskSize = (_maskSize + 1) >> 1; |
| 98 _bound = _storedMaskSize - 1; |
| 99 |
| 100 float norm = _sigma * _sigma * 2.0f * M_PI; |
| 101 float invNorm = 1.0f / norm; |
| 102 _mask = new float[_storedMaskSize * _storedMaskSize * sizeof(float)]; |
| 103 for (int i = 0; i < _storedMaskSize; ++i) { |
| 104 for (int j = 0; j < _storedMaskSize; ++j) { |
| 105 #if 0 |
| 106 _mask[i * _storedMaskSize + j] = exp(-(i * i + j * j) /
(2.0 * _sigma * _sigma)); |
| 107 #else |
| 108 _mask[i * _storedMaskSize + j] = invNorm * exp(-(i * i +
j * j) / (2.0 * _sigma * _sigma)); |
| 109 #endif |
| 110 } |
| 111 } |
| 112 } |
OLD | NEW |