Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GaussianFilter.h « image « intern « freestyle « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 625e357eddf34e2b54e8c27ac0a22ad8de925890 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#ifndef __GAUSSIANFILTER_H__
#define __GAUSSIANFILTER_H__

/** \file
 * \ingroup freestyle
 * \brief Class to perform gaussian filtering operations on an image
 */

#include <cstdlib>   // for abs
#include <string.h>  // for memcpy

#include "../system/FreestyleConfig.h"

#include "BLI_math.h"

#ifdef WITH_CXX_GUARDEDALLOC
#  include "MEM_guardedalloc.h"
#endif

namespace Freestyle {

class GaussianFilter {
 protected:
  /* The mask is a symmetrical 2d array (with respect to the middle point).
   * Thus, M(i,j) = M(-i,j) = M(i,-j) = M(-i,-j).
   * For this reason, to represent a NxN array (N odd), we only store a ((N+1)/2)x((N+1)/2) array.
   */
  float _sigma;
  float *_mask;
  int _bound;
  // the real mask size (must be odd)(the size of the mask we store is
  // ((_maskSize+1)/2)*((_maskSize+1)/2))
  int _maskSize;
  int _storedMaskSize;  // (_maskSize+1)/2)

 public:
  GaussianFilter(float iSigma = 1.0f);
  GaussianFilter(const GaussianFilter &);
  GaussianFilter &operator=(const GaussianFilter &);
  virtual ~GaussianFilter();

  /*! Returns the value for pixel x,y of image "map" after a gaussian blur, made using the sigma
   * value. The sigma value determines the mask size (~ 2 x sigma).
   * \param map: The image we wish to work on.
   * The Map template must implement the following methods:
   * - float pixel(unsigned int x,unsigned int y) const;
   * - unsigned width() const;
   * - unsigned height() const;
   *  \param x:
   *    The abscissa of the pixel where we want to evaluate the gaussian blur.
   *  \param y:
   *    The ordinate of the pixel where we want to evaluate the gaussian blur.
   *  \param sigma:
   *    The sigma value of the gaussian function.
   */
  template<class Map> float getSmoothedPixel(Map *map, int x, int y);

  /*! Compute the mask size and returns the REAL mask size ((2*_maskSize)-1)
   *  This method is provided for convenience.
   */
  static int computeMaskSize(float sigma);

  /*! accessors */
  inline float sigma() const
  {
    return _sigma;
  }

  inline int maskSize() const
  {
    return _maskSize;
  }

  inline int getBound()
  {
    return _bound;
  }

  /*! modifiers */
  void setSigma(float sigma);
#if 0
  void SetMaskSize(int size)
  {
    _maskSize = size;
    _storedMaskSize = (_maskSize + 1) >> 1;
  }
#endif

 protected:
  void computeMask();

#ifdef WITH_CXX_GUARDEDALLOC
  MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:GaussianFilter")
#endif
};

/*
 * #############################################
 * #############################################
 * #############################################
 * ######                                 ######
 * ######   I M P L E M E N T A T I O N   ######
 * ######                                 ######
 * #############################################
 * #############################################
 * #############################################
 */

template<class Map> float GaussianFilter::getSmoothedPixel(Map *map, int x, int y)
{
  float sum = 0.0f;
  float L = 0.0f;
  int w = (int)map->width();   // soc
  int h = (int)map->height();  // soc

  // Current pixel is x,y
  // Sum surrounding pixels L value:
  for (int i = -_bound; i <= _bound; ++i) {
    if ((y + i < 0) || (y + i >= h)) {
      continue;
    }
    for (int j = -_bound; j <= _bound; ++j) {
      if ((x + j < 0) || (x + j >= w)) {
        continue;
      }

      float tmpL = map->pixel(x + j, y + i);
      float m = _mask[abs(i) * _storedMaskSize + abs(j)];
      L += m * tmpL;
      sum += m;
    }
  }
  // L /= sum;
  return L;
}

} /* namespace Freestyle */

#endif  // __GAUSSIANFILTER_H__