Debian Codegraph @

libs/CCFbo/shaders/Bilateral/bilateral.frag

raw
1 //////////////////////////////////////////////////////////////////////////////////////
2 //
3 //
4 // Bilateral filtering
5 //
6 // C.B. - 08/16/2008
7 // D.G-M. - 10/20/2010
8 // D.G-M. - 02/18/2014
9 //
10 // IN:
11 // s2_I - Image to blur
12 // s2_D - Modulating depth image
13 //
14 // OUT:
15 // Filtered image
16 //
17 ///////////////////////////////////////////////////////////////////////////////////////
18 //#extension GL_ARB_draw_buffers : enable
19
20
21 /****************************************************/
22 uniform sampler2D s2_I;
23 uniform sampler2D s2_D;
24 uniform float SX;
25 uniform float SY;
26 uniform int NHalf; // half filter size (<= 7!)
27 uniform float DistCoefs[64]; // pixel distance based damping coefs (max = 8*8).
28 uniform float SigmaDepth; // pixel depth distribution variance
29 /****************************************************/
30
31 void main (void)
32 {
33 float z = texture2D(s2_D,gl_TexCoord[0].st).r;
34
35 float wsum = 0.0; // sum of all weights
36 vec3 csum = vec3(0.0); // sum of all contributions
37 vec2 coordi = vec2(0.0,0.0); // ith neighbor position
38
39 for(int c=-NHalf; c<=NHalf; c++)
40 {
41 //neighbor position (X)
42 coordi.x = float(c)/SX;
43 int cabs;
44 if (c < 0)
45 cabs = -c;
46 else
47 cabs = c;
48
49 for(int d=-NHalf; d<=NHalf; d++)
50 {
51 //neighbor position (Y)
52 coordi.y = float(d)/SY;
53
54 //neighbor color
55 vec3 ci = texture2D(s2_I,gl_TexCoord[0].st+coordi).rgb;
56
57 //pixel distance based damping
58 int dabs;
59 if (d < 0)
60 dabs = -d;
61 else
62 dabs = d;
63 float fi = DistCoefs[cabs*(NHalf+1)+dabs];
64
65 //pixel depth difference based damping
66 if (SigmaDepth > 0.0)
67 {
68 //neighbor depth
69 float zi = texture2D(s2_D,gl_TexCoord[0].st+coordi).r;
70 float dz = (z-zi)/SigmaDepth;
71 fi *= exp(-dz*dz/2.0);
72 }
73
74 csum += ci * fi;
75 wsum += fi;
76 }
77 }
78
79 //output
80 gl_FragColor = vec4(csum/wsum,1.0);
81 }
82