PICCANTE  0.4
The hottest HDR imaging library!
filter_bilateral_2das.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_2DAS_HPP
19 #define PIC_GL_FILTERING_FILTER_BILATERAL_2DAS_HPP
20 
21 #include "../../util/vec.hpp"
22 #include "../../util/std_util.hpp"
23 #include "../../gl/filtering/filter.hpp"
24 #include "../../gl/filtering/filter_sampling_map.hpp"
25 #include "../../gl/point_samplers/sampler_random_m.hpp"
26 
27 namespace pic {
28 
30 {
31 protected:
32  float sigma_s, sigma_r;
34 
35  //Random numbers tile
37 
38  //Sampling map
41 
45  void initShaders();
46 
50  void FragmentShader();
51 
55  void updateParam()
56  {
57  param.clear();
58 
59  param.push_back(ms->getImage());
60  param.push_back(ms->getImageLevelsR());
61  param.push_back(imageRand);
62 
63  if(sampleMap != NULL) {
64  param.push_back(sampleMap);
65  }
66  }
67 
71  void setNULL()
72  {
73  ms = NULL;
74  imageRand = NULL;
75  fGLsm = NULL;
76  sampleMap = NULL;
77  sigma_s = -1.0f;
78  sigma_r = -1.0f;
79  }
80 
81 public:
82 
87 
89 
93  void releaseAux()
94  {
95  delete_s(ms);
97  delete_s(fGLsm);
99  }
100 
106  FilterGLBilateral2DAS(float sigma_s, float sigma_r);
107 
113  void update(float sigma_s, float sigma_r);
114 
122  {
123  //calculate the sampling map
124  sampleMap = fGLsm->Process(imgIn, sampleMap);
125 
126  if(param.size() == 3) {
127  param.push_back(sampleMap);
128  }
129 
130  return allocateOutputMemory(imgIn, imgOut, bDelete);
131  }
132 
140  static ImageGL *execute(ImageGL *imgIn, float sigma_s, float sigma_r)
141  {
143 
144  ImageGL *imgOut = filter->Process(SingleGL(imgIn), NULL);
145 
146  delete filter;
147  return imgOut;
148  }
149 };
150 
152 {
153  setNULL();
154 }
155 
157 {
158  release();
159 }
160 
162  float sigma_r): FilterGL()
163 {
164  setNULL();
165 
166  FragmentShader();
167 
168  initShaders();
169 
171 }
172 
174 {
176  (
177  uniform sampler2D u_tex;
178  uniform isampler2D u_poisson;
179  uniform sampler2D u_sample;
180  uniform isampler2D u_rand;
181  uniform isampler2D u_levelsR;
182  uniform float sigmas2;
183  uniform float sigmar2;
184  uniform int levelsR_Size;
185  out vec4 f_color;
186 
187  //Calculate the number of samples
188  int CalculateSamples(int shifter, ivec2 tSize) {
189  //Fetch to the sampling map
190  float levelVal = dot(texture(u_sample, gl_FragCoord.xy / tSize.xy).xyz, vec3(1.0)) / 3.0;
191  levelVal = clamp(1.0f - levelVal, 0.0, 1.0) * float(levelsR_Size);
192 
193  int levelInt = int(floor(levelVal));
194 
195  int nSamples = texelFetch(u_levelsR, ivec2(levelInt, shifter), 0).x;
196 
197  if(levelInt < (levelsR_Size - 1)) {
198  float tmp = (levelVal - float(levelInt));
199 
200  if(tmp > 0.0) {
201  int nSamples1 = texelFetch(u_levelsR, ivec2(levelInt + 1, shifter), 0).x;
202 
203  nSamples += int(float(nSamples1 - nSamples) * tmp);
204  }
205  }
206 
207  return nSamples / 2;
208  }
209 
210  void main(void) {
211  ivec2 coordsFrag = ivec2(gl_FragCoord.xy);
212 
213  //Coordinates
214  int shifter = texelFetch(u_rand, coordsFrag.xy % 128, 0).x;
215 
216  //Calculating the number of samples
217  ivec2 tSize = textureSize(u_tex, 0);
218 
219  int nSamples = CalculateSamples(shifter, tSize);
220 
221  //Filtering
222  vec3 tmpCol;
223  vec3 colRef = texelFetch(u_tex, coordsFrag, 0).xyz;
224  vec3 color = vec3(0.0);
225  float weight = 0.0;
226 
227  for(int i = 0; i < nSamples; i++) {
228  ivec4 coords = texelFetch(u_poisson, ivec2(i, shifter), 0);
229 
230  //Texture fetch
231  tmpCol = texelFetch(u_tex, coordsFrag.xy + coords.xy, 0).xyz;
232  vec3 tmpCol2 = tmpCol - colRef;
233  float dstR = dot(tmpCol2.xyz, tmpCol2.xyz);
234  float tmp = exp(-dstR / sigmar2 - float(coords.z) / sigmas2);
235  color += tmpCol * tmp;
236  weight += tmp;
237  }
238 
239  color = weight > 0.0 ? color / weight : colRef;
240  f_color = vec4(color, 1.0);
241  }
242  );
243 }
244 
246 {
247  technique.initStandard("330", vertex_source, fragment_source, "FilterGLBilateral2DAS");
248 }
249 
250 PIC_INLINE void FilterGLBilateral2DAS::update(float sigma_s, float sigma_r)
251 {
252  bool flag = false;
253 
254  if(sigma_s > 0.0f) {
255  this->sigma_s = sigma_s;
256  flag = (this->sigma_s == sigma_s);
257  }
258 
259  if(sigma_r > 0.0f) {
260  this->sigma_r = sigma_r;
261  flag = flag || (this->sigma_r == sigma_r);
262  }
263 
264  if(fGLsm == NULL) {
266  }
267 
268  int nRand = 32;
269 
270  if(imageRand == NULL) {
271  Image tmp_image_rand(1, 128, 128, 1);
272  tmp_image_rand.setRand(1);
273  tmp_image_rand *= float(nRand - 1);
274 
275  imageRand = new ImageGL(&tmp_image_rand, true);
276  imageRand->generateTextureGL(GL_TEXTURE_2D, GL_INT, false);
277  }
278 
279  if(flag) {
280  //shader update
281  int kernelSize = PrecomputedGaussian::getKernelSize(this->sigma_s);
282  int halfKernelSize = kernelSize >> 1;
283  Vec2i window = Vec2i(halfKernelSize, halfKernelSize);
284 
285  //Poisson samples
286  #ifdef PIC_DEBUG
287  printf("Window: %d\n", halfKernelSize);
288  #endif
289 
290  if(ms == NULL) {
291  ms = new MRSamplersGL<2>(ST_BRIDSON, window, halfKernelSize, 3, nRand);
292  ms->generateTexture();
294  #ifdef PIC_DEBUG
295  printf("Number of samples: %d\n", ms->nSamples >> 1);
296  #endif
297  } else {
298  ms->updateGL(window, halfKernelSize);
299  }
300 
301  updateParam();
302  }
303 
304  //shader update
305  float sigmas2 = 2.0f * this->sigma_s * this->sigma_s;
306  float sigmar2 = 2.0f * this->sigma_r * this->sigma_r;
307 
308  technique.bind();
309  technique.setUniform1i("u_tex", 0);
310  technique.setUniform1i("u_poisson", 1);
311  technique.setUniform1i("u_levelsR", 2);
312  technique.setUniform1i("u_rand", 3);
313  technique.setUniform1i("u_sample", 4);
314  technique.setUniform1f("sigmas2", sigmas2);
315  technique.setUniform1f("sigmar2", sigmar2);
316  technique.setUniform1i("levelsR_Size", ms->nLevels);
317  technique.unbind();
318 }
319 /*
320  //Textures
321 
322  glActiveTexture(GL_TEXTURE3);
323  imgTmp->bindTexture();
324 
325  glActiveTexture(GL_TEXTURE4);
326  glBindTexture(GL_TEXTURE_2D, ms->getLevelsRTexture());
327 
328  glActiveTexture(GL_TEXTURE2);
329  imageRand->bindTexture();
330 
331  glActiveTexture(GL_TEXTURE1);
332  glBindTexture(GL_TEXTURE_2D, ms->getTexture());
333 
334  glActiveTexture(GL_TEXTURE0);
335  base->bindTexture();
336  */
337 
338 } // end namespace pic
339 
340 #endif /* PIC_GL_FILTERING_FILTER_BILATERAL_2DAS_HPP */
341 
MRSamplersGL< 2 > * ms
Definition: filter_bilateral_2das.hpp:33
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
FilterGLBilateral2DAS()
FilterGLBilateral2DAS.
T * delete_s(T *data)
delete_s
Definition: std_util.hpp:123
GLuint generateLevelsRTexture()
generateLevelsRTexture
Definition: sampler_random_m.hpp:222
GLuint generateTextureGL(GLenum target, GLenum format_type, bool mipmap)
generateTextureGL
ImageGL * allocateOutputMemory(ImageGLVec imgIn, ImageGL *imgOut, bool bDelete)
allocateOutputMemory
Definition: display.hpp:217
void updateParam()
updateParam
Definition: filter_bilateral_2das.hpp:55
void bind()
bind
Definition: display.hpp:189
#define MAKE_STRING(input_string)
Definition: filter_bilateral_2das.hpp:29
The Image class stores an image as buffer of float.
Definition: filter_radial_basis_function.hpp:60
bool bDelete
Definition: display.hpp:54
PIC_INLINE ImageGLVec SingleGL(ImageGL *img)
SingleGL creates a single for filters input.
Definition: image_vec.hpp:39
void setNULL()
setNULL
Definition: filter_bilateral_2das.hpp:71
ImageGL * Process(ImageGLVec imgIn, ImageGL *imgOut)
Process.
Definition: filter_npasses.hpp:323
The ImageGL class.
Definition: image.hpp:42
Vec< 2, int > Vec2i
Vec2i.
Definition: vec.hpp:829
void setUniform1f(const char *name_uniform, float value0)
SetUniform1f.
Definition: display.hpp:247
ImageGL * setupAux(ImageGLVec imgIn, ImageGL *imgOut)
setupAux
Definition: filter_bilateral_2das.hpp:121
void initShaders()
initShaders
ImageGL * sampleMap
Definition: filter_bilateral_2das.hpp:40
virtual ImageGL * Process(ImageGLVec imgIn, ImageGL *imgOut)
Process.
Definition: display.hpp:258
void update(float sigma_s, float sigma_r)
update
int nLevels
Definition: display.hpp:44
void updateGL(Vec< N, int > window, int nSamples)
updateGL
Definition: sampler_random_m.hpp:129
The FilterGL class.
Definition: filter.hpp:35
ImageGL * imageRand
Definition: filter_bilateral_2das.hpp:36
void setRand(unsigned int seed)
setRand
Definition: filter_radial_basis_function.hpp:1392
GLuint generateTexture()
generateTexture
Definition: sampler_random_m.hpp:151
void FragmentShader()
FragmentShader.
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
The Vec class.
Definition: display.hpp:35
bool initStandard(std::string version_number, std::string vertex_shader_source, std::string fragment_shader_source, std::string name)
initStandard
Definition: display.hpp:114
static ImageGL * execute(ImageGL *imgIn, float sigma_s, float sigma_r)
execute
Definition: filter_bilateral_2das.hpp:140
ImageGLVec param
Definition: display.hpp:49
FilterGLSamplingMap * fGLsm
Definition: filter_bilateral_2das.hpp:39
Definition: point_samplers.hpp:51
void releaseAux()
releaseAux
Definition: filter_bilateral_2das.hpp:93
ImageGL * getImageLevelsR()
getImageLevelsR
Definition: display.hpp:100
void unbind()
unbind
Definition: display.hpp:197
float sigma_s
Definition: filter_bilateral_2das.hpp:32
std::string vertex_source
Definition: display.hpp:57
Definition: bilateral_separation.hpp:25
float sigma_r
Definition: filter_bilateral_2das.hpp:32
std::string fragment_source
Definition: display.hpp:57
The FilterGLSamplingMap class.
Definition: filter_sampling_map.hpp:38
int nSamples
Definition: display.hpp:53
ImageGL * getImage()
getImage
Definition: display.hpp:89
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
The FilterGLSamplingMap class.
Definition: display.hpp:38