PICCANTE  0.4
The hottest HDR imaging library!
filter_anisotropic_diffusion.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_ANISOTROPIC_DIFFUSION_HPP
19 #define PIC_GL_FILTERING_FILTER_ANISOTROPIC_DIFFUSION_HPP
20 
21 #include "../../base.hpp"
22 
23 #include "../../util/std_util.hpp"
24 
25 #include "../../gl/filtering/filter.hpp"
26 #include "../../gl/filtering/filter_iterative.hpp"
27 
28 namespace pic {
29 
34 {
35 protected:
39  void initShaders();
40 
44  void FragmentShader();
45 
46  float delta_t, k;
47  unsigned int iterations;
49 
50 public:
56  FilterGLAnisotropicDiffusion(float k, unsigned int iterations);
57 
63  FilterGLAnisotropicDiffusion(float sigma_s, float sigma_r);
64 
66 
67  void releaseAux()
68  {
69  delete_s(flt);
70  }
71 
76  void update(float k);
77 
85  {
86  if(flt == NULL) {
87  flt = new FilterGLIterative(this, iterations);
88  }
89 
90  return flt->Process(imgIn, imgOut);
91  }
92 };
93 
95  unsigned int iterations): FilterGL()
96 {
97  if(iterations < 1) {
98  iterations = 1;
99  }
100 
101  flt = NULL;
102 
103  this->k = k;
104  this->iterations = iterations;
105 
106  //protected values are assigned/computed
107  FragmentShader();
108  initShaders();
109 
110  update(k);
111 }
112 
114  float sigma_r): FilterGL()
115 {
116  sigma_r = sigma_r <= 0.0f ? 0.11f : sigma_r;
117 
118  flt = NULL;
119 
120  iterations = int(ceilf(5.0f * sigma_s));
121 
122  //protected values are assigned/computed
123  FragmentShader();
124  initShaders();
125 
126  update(sigma_r);
127 
128 }
129 
131 {
132  release();
133 }
134 
136 {
138  (
139  uniform sampler2D u_tex; \n
140  uniform float k_sq; \n
141  uniform float delta_t; \n
142  out vec4 f_color; \n
143 
144  void main(void) {
145  \n
146  ivec2 coords = ivec2(gl_FragCoord.xy);
147  \n
148  vec3 cB = texelFetch(u_tex, coords , 0).xyz;
149  \n
150  vec3 c0 = texelFetch(u_tex, coords + ivec2(1, 0), 0).xyz;
151  \n
152  vec3 c1 = texelFetch(u_tex, coords - ivec2(1, 0), 0).xyz;
153  \n
154  vec3 c2 = texelFetch(u_tex, coords + ivec2(0, 1), 0).xyz;
155  \n
156  vec3 c3 = texelFetch(u_tex, coords - ivec2(0, 1), 0).xyz;
157  \n
158  vec3 gN = c2 - cB;
159  \n
160  vec3 gS = c3 - cB;
161  \n
162  vec3 gW = c1 - cB;
163  \n
164  vec3 gE = c0 - cB;
165  \n
166  vec4 c = vec4(dot(gN, gN), dot(gS, gS), dot(gW, gW), dot(gE, gE));
167  \n
168  c = exp(-c / vec4(k_sq));
169  \n
170  f_color = vec4(cB + delta_t *(c.x * gN + c.y * gS + c.z * gW + c.w * gE), 1.0);
171  \n
172  }
173  );
174 }
175 
177 {
178  FragmentShader();
179  technique.initStandard("330", vertex_source, fragment_source, "FilterGLAnisotropicDiffusion");
180 }
181 
183 {
184  this->k = k > 0.0f ? this->k : 0.11f;
185  float k_sq = this->k * this->k;
186 
187  this->delta_t = 1.0f / 7.0f;
188 
189  technique.bind();
190  technique.setUniform1i("u_tex", 0);
191  technique.setUniform1f("k_sq", k_sq);
192  technique.setUniform1f("delta_t", delta_t);
193  technique.unbind();
194 }
195 
196 } // end namespace pic
197 
198 #endif /* PIC_GL_FILTERING_FILTER_ANISOTROPIC_DIFFUSION_HPP */
199 
FilterGLAnisotropicDiffusion(float k, unsigned int iterations)
FilterGLAnisotropicDiffusion.
FilterGLIterative * flt
Definition: filter_anisotropic_diffusion.hpp:48
float k
Definition: filter_anisotropic_diffusion.hpp:46
TechniqueGL technique
Definition: display.hpp:45
T * delete_s(T *data)
delete_s
Definition: std_util.hpp:123
void bind()
bind
Definition: display.hpp:189
#define MAKE_STRING(input_string)
The FilterGLIterative class.
Definition: filter_iterative.hpp:32
ImageGL * Process(ImageGLVec imgIn, ImageGL *imgOut)
Process.
Definition: filter_npasses.hpp:323
unsigned int iterations
Definition: filter_anisotropic_diffusion.hpp:47
ImageGL * AnisotropicDiffusion(ImageGLVec imgIn, ImageGL *imgOut)
AnisotropicDiffusion.
Definition: filter_anisotropic_diffusion.hpp:84
The ImageGL class.
Definition: image.hpp:42
void setUniform1f(const char *name_uniform, float value0)
SetUniform1f.
Definition: display.hpp:247
float delta_t
Definition: filter_anisotropic_diffusion.hpp:46
The FilterGL class.
Definition: filter.hpp:35
The FilterGL class.
Definition: display.hpp:35
#define PIC_INLINE
Definition: base.hpp:33
void FragmentShader()
FragmentShader.
bool initStandard(std::string version_number, std::string vertex_shader_source, std::string fragment_shader_source, std::string name)
initStandard
Definition: display.hpp:114
void unbind()
unbind
Definition: display.hpp:197
std::string vertex_source
Definition: display.hpp:57
Definition: bilateral_separation.hpp:25
void releaseAux()
releaseAux
Definition: filter_anisotropic_diffusion.hpp:67
The FilterGLAnisotropicDiffusion class.
Definition: filter_anisotropic_diffusion.hpp:33
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