PICCANTE  0.4
The hottest HDR imaging library!
filter_bilateral_2df.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_GL_FILTERING_FILTER_BILATERAL_2DF_HPP
19 #define PIC_GL_FILTERING_FILTER_BILATERAL_2DF_HPP
20 
21 #include "../../gl/filtering/filter.hpp"
22 
23 namespace pic {
24 
31 {
32 protected:
33  float sigma_s, sigma_r;
34 
38  void initShaders();
39 
43  void FragmentShader();
44 
45 public:
46 
51 
57  FilterGLBilateral2DF(float sigma_s, float sigma_r);
58 
60 
66  void update(float sigma_s, float sigma_r);
67 
75  {
76  imgOut = allocateOutputMemory(imgIn, imgOut, false);
77 
78  if(imgIn.size() == 1) {
79  param.clear();
80  param.push_back(imgIn[0]);
81  }
82 
83  return imgOut;
84  }
85 };
86 
88 {
89 }
90 
92 {
93  release();
94 }
95 
97  float sigma_r): FilterGL()
98 {
99  //protected values are assigned/computed
100  this->sigma_s = sigma_s;
101  this->sigma_r = sigma_r;
102 
103  FragmentShader();
104  initShaders();
105 }
106 
108 {
110  (
111  uniform sampler2D u_tex;
112  uniform float sigmas2;
113  uniform float sigmar2;
114  uniform int halfKernelSize;
115  out vec4 f_color;
116 
117  void main(void) {
118  vec3 color = vec3(0.0, 0.0, 0.0);
119  ivec2 coordsFrag = ivec2(gl_FragCoord.xy);
120  vec3 tmpCol;
121 
122  vec3 colRef = texelFetch(u_tex, coordsFrag, 0).xyz;
123 
124  float weight = 0.0;
125 
126  for(int i = -halfKernelSize; i <= halfKernelSize; i++) {
127  for(int j = -halfKernelSize; j <= halfKernelSize; j++) {
128  //Coordinates
129  ivec2 coords = ivec2(i, j);
130  //Texture fetch
131  tmpCol = texelFetch(u_tex, coordsFrag.xy + coords.xy, 0).xyz;
132  vec3 tmpCol2 = tmpCol - colRef;
133  float dstR = dot(tmpCol2.xyz, tmpCol2.xyz);
134  float tmp = exp(-dstR / sigmar2 - float(coords.x * coords.x + coords.y *
135  coords.y) / sigmas2);
136  color.xyz += tmpCol * tmp;
137  weight += tmp;
138  }
139  }
140 
141  color = weight > 0.0 ? color / weight : colRef;
142  f_color = vec4(color.xyz, 1.0);
143  }
144  );
145 }
146 
148 {
149  technique.initStandard("330", vertex_source, fragment_source, "FilterGLBilateral2DF");
150 
151  update(-1.0f, -1.0f);
152 }
153 
154 PIC_INLINE void FilterGLBilateral2DF::update(float sigma_s, float sigma_r)
155 {
156  if(sigma_s > 0.0f) {
157  this->sigma_s = sigma_s;
158  }
159 
160  if(sigma_r > 0.0f) {
161  this->sigma_r = sigma_r;
162  }
163 
164  float sigmas2 = 2.0f * this->sigma_s * this->sigma_s;
165  float sigmar2 = 2.0f * this->sigma_r * this->sigma_r;
166 
167  //Precomputation of the Gaussian Kernel
168  int halfKernelSize = PrecomputedGaussian::getKernelSize(this->sigma_s) >> 1;
169 
170  technique.bind();
171  technique.setUniform1i("u_tex", 0);
172  technique.setUniform1f("sigmas2", sigmas2);
173  technique.setUniform1f("sigmar2", sigmar2);
174  technique.setUniform1i("halfKernelSize", halfKernelSize);
175  technique.unbind();
176 }
177 
178 } // end namespace pic
179 
180 #endif /* PIC_GL_FILTERING_FILTER_BILATERAL_2DF_HPP */
181 
TechniqueGL technique
Definition: display.hpp:45
static int getKernelSize(float sigma)
KernelSize computes the size of a kernel in pixel give its sigma.
Definition: precomputed_gaussian.hpp:121
FilterGLBilateral2DF()
FilterGLBilateral2DF.
ImageGL * allocateOutputMemory(ImageGLVec imgIn, ImageGL *imgOut, bool bDelete)
allocateOutputMemory
Definition: display.hpp:217
void bind()
bind
Definition: display.hpp:189
void FragmentShader()
FragmentShader.
#define MAKE_STRING(input_string)
void update(float sigma_s, float sigma_r)
update
float sigma_s
Definition: filter_bilateral_2df.hpp:33
The ImageGL class.
Definition: image.hpp:42
void setUniform1f(const char *name_uniform, float value0)
SetUniform1f.
Definition: display.hpp:247
The FilterGL class.
Definition: filter.hpp:35
The FilterGL class.
Definition: display.hpp:35
#define PIC_INLINE
Definition: base.hpp:33
bool initStandard(std::string version_number, std::string vertex_shader_source, std::string fragment_shader_source, std::string name)
initStandard
Definition: display.hpp:114
ImageGLVec param
Definition: display.hpp:49
The FilterGLBilateral2DF class provides an HW accelerated bilateral filter implementation without app...
Definition: filter_bilateral_2df.hpp:30
ImageGL * setupAux(ImageGLVec imgIn, ImageGL *imgOut)
setupAux
Definition: filter_bilateral_2df.hpp:74
void unbind()
unbind
Definition: display.hpp:197
std::string vertex_source
Definition: display.hpp:57
Definition: bilateral_separation.hpp:25
float sigma_r
Definition: filter_bilateral_2df.hpp:33
void initShaders()
initShaders
std::string fragment_source
Definition: display.hpp:57
std::vector< ImageGL * > ImageGLVec
ImageGLVec an std::vector of pic::ImageGL.
Definition: image_vec.hpp:32
void release()
release
Definition: display.hpp:85
void setUniform1i(const char *name_uniform, int value0)
SetUniform.
Definition: display.hpp:236