PICCANTE  0.4
The hottest HDR imaging library!
filter_bilateral_3ds.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_3DS_HPP
19 #define PIC_GL_FILTERING_FILTER_BILATERAL_3DS_HPP
20 
21 #include "../../base.hpp"
22 #include "../../util/vec.hpp"
23 #include "../../util/std_util.hpp"
24 #include "../../gl/filtering/filter.hpp"
25 
26 namespace pic {
27 
32 {
33 protected:
38 
39  //Random numbers tile
41 
45  void initShaders();
46 
50  void FragmentShader();
51 
52 public:
53 
60  FilterGLBilateral3DS(float sigma_s, float sigma_r, float sigma_t);
61 
63 
70  void update(float sigma_s, float sigma_r, float sigma_t);
71 
75  void setUniform();
76 
81  void setFrame(int frame)
82  {
83  this->frame = frame;
84  }
85 
89  void nextFrame()
90  {
91  frame++;
92  }
93 
98  int getFrame()
99  {
100  return frame;
101  }
102 
109  ImageGL *Process(ImageGLVec imgIn, ImageGL *imgOut);
110 };
111 
113  float sigma_t): FilterGL()
114 {
115  //protected values are assigned/computed
116  this->sigma_s = sigma_s;
117  this->sigma_r = sigma_r;
118  this->sigma_t = sigma_t;
119 
120  int nRand = 32;
121  imageRand = new ImageGL(1, 256, 256, 1, IMG_CPU, GL_TEXTURE_2D);
122  imageRand->setRand(1);
123  imageRand->generateTextureGL(GL_TEXTURE_2D, GL_FLOAT, false);
124  *imageRand *= float(nRand - 1);
125 
126  //Precomputation of the Gaussian Kernel
127  int kernelSizeSpace = PrecomputedGaussian::getKernelSize(sigma_s);
129 
130  int kernelSize = MAX(kernelSizeSpace, kernelSizeTime);
131  int halfKernelSize = kernelSize >> 1;
132  int halfKernelSizeTime = kernelSizeTime >> 1;
133 
134  frame = halfKernelSizeTime;
135 
136  //Poisson samples
137  Vec3i window = Vec3i(halfKernelSize, halfKernelSize, halfKernelSizeTime);
138  ms = new MRSamplersGL<3>(ST_BRIDSON, window, 2 * kernelSize, 1, nRand);
139  ms->generateTexture();
140 
141 #ifdef PIC_DEBUG
142  printf("Window Space: %d Window Time: %d\n", halfKernelSize,
143  halfKernelSizeTime);
144  printf("Nsamples: %d\n", ms->nSamples / 3);
145 #endif
146 
147  FragmentShader();
148  initShaders();
149 }
150 
152 {
153  release();
154 }
155 
157 {
159  (
160  uniform sampler2DArray u_tex;
161 // uniform sampler3D u_tex;
162  uniform isampler2D u_poisson;
163  uniform sampler2D u_rand;
164  uniform int TOKEN_BANANA;
165  uniform int frame;
166  uniform float sigmas2;
167  uniform float sigmar2;
168  uniform float sigmat2;
169  out vec4 f_color;
170 
171  void main(void) {
172  vec3 tmpCol;
173  vec3 color = vec3(0.0, 0.0, 0.0);
174  ivec3 texSize = textureSize(u_tex, 0);
175  ivec3 coordsFrag = ivec3(gl_FragCoord.xy, frame % texSize.z);
176 
177  vec3 colRef = texelFetch(u_tex, coordsFrag, 0).xyz;
178  float weight = 0.0;
179 
180  for(int i = 0; i < TOKEN_BANANA; i++) {
181  //Coordinates
182  float shifter = texelFetch(u_rand, coordsFrag.xy % 128, 0).x;
183  ivec4 coords = texelFetch(u_poisson, ivec2(i, shifter), 0).xyzw;
184  //Texture fetch
185  ivec3 tmpCoords;
186  tmpCoords.xy = coords.xy + coordsFrag.xy;
187  tmpCoords.z = (frame + coords.z) % texSize.z;
188 
189  tmpCol = texelFetch(u_tex, tmpCoords, 0).xyz;
190  vec3 tmpCol2 = tmpCol - colRef;
191  float dstR = dot(tmpCol2.xyz, tmpCol2.xyz);
192  float tmp = exp(-dstR / sigmar2 - float(coords.w) / sigmas2 - float(
193  coords.z * coords.z) / sigmat2);
194  color.xyz += tmpCol * tmp;
195  weight += tmp;
196  }
197 
198  f_color = vec4(weight > 0.0 ? (color / weight) : vec3(1.0), 1.0);
199  }
200  );
201 }
202 
204 {
205  technique.initStandard("330", vertex_source, fragment_source, "FilterGLBilateral3DS");
206 
207  sigmas2 = 2.0f * sigma_s * sigma_s;
208  sigmat2 = 2.0f * sigma_t * sigma_t;
209  sigmar2 = 2.0f * sigma_r * sigma_r;
210  setUniform();
211 }
212 
213 PIC_INLINE void FilterGLBilateral3DS::update(float sigma_s, float sigma_r, float sigma_t)
214 {
215 
216  bool flag = false;
217 
218  if(sigma_s > 0.0f) {
219  flag = (this->sigma_s == sigma_s);
220  this->sigma_s = sigma_s;
221  }
222 
223  if(sigma_r > 0.0f) {
224  flag = flag || (this->sigma_r == sigma_r);
225  this->sigma_r = sigma_r;
226  }
227 
228  if(sigma_t > 0.0f) {
229  flag = flag || (this->sigma_t == sigma_t);
230  this->sigma_t = sigma_t;
231  }
232 
233  if(!flag) {
234  }
235 
236  int kernelSize = PrecomputedGaussian::getKernelSize(this->sigma_s);
237  int halfKernelSize = kernelSize >> 1;
238 
239  Vec3i window = Vec3i(halfKernelSize, halfKernelSize, halfKernelSize);
240  ms->updateGL(window, halfKernelSize);
241 
242  //shader update
243  sigmas2 = 2.0f * this->sigma_s * this->sigma_s;
244  sigmat2 = 2.0f * this->sigma_t *this->sigma_t;
245  sigmar2 = 2.0f * this->sigma_r * this->sigma_r;
246 
247  setUniform();
248 }
249 
251 {
252  technique.bind();
253  technique.setUniform1i("u_tex", 0);
254  technique.setUniform1i("u_poisson", 1);
255  technique.setUniform1i("u_rand", 2);
256 
257  technique.setUniform1f("sigmas2", sigmas2);
258  technique.setUniform1f("sigmat2", sigmat2);
259  technique.setUniform1f("sigmar2", sigmar2);
260  technique.setUniform1i("TOKEN_BANANA", ms->nSamples / 3);
261  technique.setUniform1i("frame", frame);
262 
263  technique.unbind();
264 }
265 
267  ImageGL *imgOut)
268 {
269  if(imgIn[0] == NULL) {
270  return imgOut;
271  }
272 
273  int w = imgIn[0]->width;
274  int h = imgIn[0]->height;
275 
276  if(imgOut == NULL) {
277  imgOut = new ImageGL(1, w, h, imgIn[0]->channels, IMG_GPU, imgIn[0]->getTarget());
278  }
279 
280  if(fbo == NULL) {
281  fbo = new Fbo();
282  fbo->create(w, h, 1, false, imgOut->getTexture());
283  }
284 
285  ImageGL *base = imgIn[0];
286 
287  //Rendering
288  fbo->bind();
289  glViewport(0, 0, (GLsizei)w, (GLsizei)h);
290 
291  //Shaders
292  technique.bind();
293 
294  //Textures
295  glActiveTexture(GL_TEXTURE2);
297 
298  glActiveTexture(GL_TEXTURE1);
299  glBindTexture(GL_TEXTURE_2D, ms->getTexture());
300 
301  glActiveTexture(GL_TEXTURE0);
302  base->bindTexture();
303 
304  //Rendering aligned quad
305  quad->Render();
306 
307  //Fbo
308  fbo->unbind();
309 
310  //Shaders
311  technique.unbind();
312 
313  //Textures
314  glActiveTexture(GL_TEXTURE2);
316 
317  glActiveTexture(GL_TEXTURE1);
318  glBindTexture(GL_TEXTURE_2D, 0);
319 
320  glActiveTexture(GL_TEXTURE0);
321  base->unBindTexture();
322 
323  return imgOut;
324 }
325 
326 } // end namespace pic
327 
328 #endif /* PIC_GL_FILTERING_FILTER_BILATERAL_3DS_HPP */
329 
TechniqueGL technique
Definition: display.hpp:45
The Fbo class.
Definition: display.hpp:32
float sigma_r
Definition: filter_bilateral_3ds.hpp:34
static int getKernelSize(float sigma)
KernelSize computes the size of a kernel in pixel give its sigma.
Definition: precomputed_gaussian.hpp:121
Vec< 3, int > Vec3i
Vec3i.
Definition: vec.hpp:834
void update(float sigma_s, float sigma_r, float sigma_t)
update
void FragmentShader()
FragmentShader.
float sigma_s
Definition: filter_bilateral_3ds.hpp:34
bool create(int width, int height, bool bDepth)
create
Definition: display.hpp:207
void bindTexture()
bindTexture
int frame
Definition: filter_bilateral_3ds.hpp:36
void bindTexture()
bindTexture
Definition: image.hpp:981
int getFrame()
getFrame
Definition: filter_bilateral_3ds.hpp:98
GLuint generateTextureGL(GLenum target, GLenum format_type, bool mipmap)
generateTextureGL
float sigmas2
Definition: filter_bilateral_3ds.hpp:35
void bind()
bind
Definition: display.hpp:189
GLuint getTexture()
getTexture
Definition: display.hpp:80
#define MAKE_STRING(input_string)
Definition: image.hpp:37
FilterGLBilateral3DS(float sigma_s, float sigma_r, float sigma_t)
FilterGLBilateral3DS.
float sigmat2
Definition: filter_bilateral_3ds.hpp:35
ImageGL * Process(ImageGLVec imgIn, ImageGL *imgOut)
Process.
The ImageGL class.
Definition: image.hpp:42
GLuint getTexture() const
getTexture
Definition: display.hpp:369
void setUniform1f(const char *name_uniform, float value0)
SetUniform1f.
Definition: display.hpp:247
ImageGL * imageRand
Definition: filter_bilateral_3ds.hpp:40
void initShaders()
initShaders
Fbo * fbo
Definition: display.hpp:39
void updateGL(Vec< N, int > window, int nSamples)
updateGL
Definition: sampler_random_m.hpp:129
void bind()
bind
Definition: display.hpp:427
QuadGL * quad
Definition: display.hpp:42
The FilterGL class.
Definition: filter.hpp:35
MRSamplersGL< 3 > * ms
Definition: filter_bilateral_3ds.hpp:37
void nextFrame()
nextFrame
Definition: filter_bilateral_3ds.hpp:89
void unBindTexture()
unBindTexture
void Render()
Render draws a quad on screen.
Definition: display.hpp:140
void setRand(unsigned int seed)
setRand
Definition: filter_radial_basis_function.hpp:1392
GLuint generateTexture()
generateTexture
Definition: sampler_random_m.hpp:151
The FilterGL class.
Definition: display.hpp:35
The MRSamplersGL class.
Definition: sampler_random_m.hpp:45
#define PIC_INLINE
Definition: base.hpp:33
The ImageGL class.
Definition: display.hpp:42
int width
Definition: filter_radial_basis_function.hpp:80
bool initStandard(std::string version_number, std::string vertex_shader_source, std::string fragment_shader_source, std::string name)
initStandard
Definition: display.hpp:114
Definition: point_samplers.hpp:51
void unbind()
unbind
Definition: display.hpp:197
void setFrame(int frame)
setFrame
Definition: filter_bilateral_3ds.hpp:81
void unBindTexture()
unBindTexture
Definition: image.hpp:986
std::string vertex_source
Definition: display.hpp:57
Definition: bilateral_separation.hpp:25
The FilterGLBilateral3DS class.
Definition: filter_bilateral_3ds.hpp:31
int kernelSizeTime
Definition: filter_bilateral_3ds.hpp:36
float sigma_t
Definition: filter_bilateral_3ds.hpp:34
#define MAX(a, b)
Definition: math.hpp:73
void setUniform()
setUniform
void unbind()
unbind
Definition: display.hpp:442
std::vector< ImageGL * > ImageGLVec
ImageGLVec an std::vector of pic::ImageGL.
Definition: display.hpp:32
std::string fragment_source
Definition: display.hpp:57
int nSamples
Definition: display.hpp:53
Definition: image.hpp:37
std::vector< ImageGL * > ImageGLVec
ImageGLVec an std::vector of pic::ImageGL.
Definition: image_vec.hpp:32
void release()
release
Definition: display.hpp:85
float sigmar2
Definition: filter_bilateral_3ds.hpp:35
void setUniform1i(const char *name_uniform, int value0)
SetUniform.
Definition: display.hpp:236