Skip to content

Commit ddea197

Browse files
committed
add filter sketches
1 parent 11250df commit ddea197

35 files changed

+1911
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Simple demo Rakefile to autorun samples in current directory
2+
# adjust path to rp5 executable, and or opts as required
3+
4+
SAMPLES_DIR="./"
5+
6+
desc 'run demo'
7+
task :default => [:demo]
8+
9+
desc 'demo'
10+
task :demo do
11+
samples_list.shuffle.each{|sample| run_sample sample}
12+
end
13+
14+
def samples_list
15+
files = []
16+
Dir.chdir(SAMPLES_DIR)
17+
Dir.glob("*.rb").each do |file|
18+
files << File.join(SAMPLES_DIR, file)
19+
end
20+
return files
21+
end
22+
23+
def run_sample(sample_name)
24+
puts "Running #{sample_name}...quit to run next sample"
25+
open("|jruby #{sample_name}", "r") do |io|
26+
while l = io.gets
27+
puts(l.chop)
28+
end
29+
end
30+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env jruby
2+
require 'propane'
3+
# Bicubic filtering (see bicubic.glsl file for details)
4+
# Uncheck 'apply_filter' to show regular texture map filtering
5+
# Ported to propane by Martin Prout
6+
class BicubicFilter < Propane::App
7+
load_library :control_panel
8+
9+
attr_reader :bicubic, :my_image, :hide, :panel, :zoom
10+
11+
def settings
12+
size(512, 512, P2D)
13+
end
14+
15+
def setup
16+
sketch_title 'Bicubic Filtering'
17+
control_panel do |c|
18+
c.look_feel 'Nimbus'
19+
c.title = 'zoom / bicubic'
20+
c.slider :zoom, 0.0..100, 60.0
21+
c.checkbox :apply_filter, true
22+
@panel = c
23+
end
24+
@hide = false
25+
@my_image = load_image(data_path('texture.jpg'))
26+
@bicubic = load_shader(data_path('bicubic.glsl'))
27+
bicubic.set('sketchSize', width.to_f, height.to_f)
28+
end
29+
30+
def draw
31+
unless hide
32+
@hide = true
33+
panel.set_visible(hide)
34+
end
35+
background(0)
36+
# Draw the image on the scene
37+
image(my_image, 0, 0)
38+
mode = @apply_filter ? 1 : 0
39+
bicubic.set('mode', mode)
40+
level = map1d(zoom, (0..100), (0.5..0.1))
41+
bicubic.set('zoomLevel', level)
42+
# Applies the shader to everything that has already been drawn
43+
filter(bicubic)
44+
end
45+
46+
def mouse_pressed
47+
@hide = false if hide
48+
end
49+
end
50+
51+
BicubicFilter.new
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env jruby
2+
require 'propane'
3+
# Bilateral smoothing
4+
# Original shader by mrharicot
5+
# https://www.shadertoy.com/view/4dfGDH
6+
# Ported to Processing by Raphaël de Courville <twitter: @sableRaph>
7+
# Ported to propane by Martin Prout
8+
# Keep mouse pressed to show unfiltered image
9+
class BilateralFilter < Propane::App
10+
attr_reader :apply, :bilateral, :my_image
11+
12+
def settings
13+
size(512, 512, P2D)
14+
end
15+
16+
def setup
17+
sketch_title 'Bilateral Filtering'
18+
@my_image = load_image(data_path('texture.jpg'))
19+
@bilateral = load_shader(data_path('bilateral.glsl'))
20+
bilateral.set('sketchSize', width.to_f, height.to_f)
21+
end
22+
23+
def draw
24+
background(0)
25+
# Draw the image on the scene
26+
image(my_image, 0, 0)
27+
# Applies the shader to everything that has already been drawn
28+
return if mouse_pressed?
29+
filter(bilateral)
30+
end
31+
end
32+
33+
BilateralFilter.new
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env jruby
2+
require 'propane'
3+
class ContrastSatBright < Propane::App
4+
load_library :control_panel
5+
6+
attr_reader :my_filter, :my_image, :panel, :hide,
7+
:brightness, :contrast, :saturation
8+
9+
def settings
10+
size(512, 512, P2D)
11+
end
12+
13+
def setup
14+
sketch_title 'Contrast Saturation Brightness'
15+
control_panel do |c|
16+
c.look_feel 'Nimbus'
17+
c.title = 'Control'
18+
c.slider :brightness, 0..100, 60
19+
c.slider :saturation, 0..100, 70
20+
c.slider :contrast, 50..150, 100
21+
@panel = c
22+
end
23+
@hide = false
24+
@my_image = load_image(data_path('texture.jpg'))
25+
@my_filter = load_shader(data_path('shader.glsl'))
26+
end
27+
28+
def draw
29+
unless hide
30+
@hide = true
31+
panel.set_visible(hide)
32+
end
33+
background(0)
34+
# Draw the image on the scene
35+
image(my_image, 0, 0)
36+
# contrast settings: 1.0 = 100% 0.5 = 50% 1.5 = 150%
37+
c = map1d(contrast, (50..150), (0.5..1.5))
38+
s = map1d(saturation, (0..100), (0.0..1.5))
39+
b = map1d(brightness, (0..100), (0.3..1.5))
40+
# Pass the parameters to the shader
41+
my_filter.set('contrast', c)
42+
my_filter.set('saturation', s)
43+
my_filter.set('brightness', b)
44+
# Applies the shader to everything that has already been drawn
45+
filter(my_filter)
46+
end
47+
end
48+
49+
ContrastSatBright.new
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// Created by inigo quilez - iq/2013
2+
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
3+
//
4+
// This is the implementation for my article "improved texture interpolation"
5+
//
6+
// http://www.iquilezles.org/www/articles/texture/texture.htm
7+
//
8+
// It shows how to get some smooth texture interpolation without resorting to the regular
9+
// bicubic filtering, which is pretty expensive because it needs 9 texels instead of the
10+
// 4 the hardware uses for bilinear interpolation.
11+
//
12+
// With this techinique here, you van get smooth interpolation while the hardware still
13+
// uses only 4 texels, by tricking the hardware. The idea is to get the fractional part
14+
// of the texel coordinates and apply a smooth curve to it such that the derivatives are
15+
// zero at the extremes. The regular cubic or quintic smoothstep functions are just
16+
// perfect for this task.
17+
18+
// Modified to do bicubic filtering, sgreen/2013
19+
20+
// Adapted for Processing by Raphaël de Courville <@sableRaph>
21+
22+
uniform sampler2D texture;
23+
24+
uniform vec2 sketchSize;
25+
26+
uniform float zoomLevel;
27+
28+
uniform int mode;
29+
30+
vec4 texture2D_smooth(sampler2D tex, vec2 uv, vec2 res)
31+
{
32+
uv = uv*res + 0.5;
33+
vec2 iuv = floor( uv );
34+
vec2 fuv = fract( uv );
35+
uv = iuv + fuv*fuv*(3.0-2.0*fuv); // fuv*fuv*fuv*(fuv*(fuv*6.0-15.0)+10.0);;
36+
uv = (uv - 0.5)/res;
37+
return texture2D( texture, uv );
38+
}
39+
40+
// 4x4 bicubic filter using 4 bilinear texture lookups
41+
// See GPU Gems 2: "Fast Third-Order Texture Filtering", Sigg & Hadwiger:
42+
// http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter20.html
43+
44+
// w0, w1, w2, and w3 are the four cubic B-spline basis functions
45+
float w0(float a)
46+
{
47+
return (1.0/6.0)*(a*(a*(-a + 3.0) - 3.0) + 1.0);
48+
}
49+
50+
float w1(float a)
51+
{
52+
return (1.0/6.0)*(a*a*(3.0*a - 6.0) + 4.0);
53+
}
54+
55+
float w2(float a)
56+
{
57+
return (1.0/6.0)*(a*(a*(-3.0*a + 3.0) + 3.0) + 1.0);
58+
}
59+
60+
float w3(float a)
61+
{
62+
return (1.0/6.0)*(a*a*a);
63+
}
64+
65+
// g0 and g1 are the two amplitude functions
66+
float g0(float a)
67+
{
68+
return w0(a) + w1(a);
69+
}
70+
71+
float g1(float a)
72+
{
73+
return w2(a) + w3(a);
74+
}
75+
76+
// h0 and h1 are the two offset functions
77+
float h0(float a)
78+
{
79+
return -1.0 + w1(a) / (w0(a) + w1(a));
80+
}
81+
82+
float h1(float a)
83+
{
84+
return 1.0 + w3(a) / (w2(a) + w3(a));
85+
}
86+
87+
vec4 texture2D_bicubic(sampler2D tex, vec2 uv, vec2 res)
88+
{
89+
uv = uv*res + 0.5;
90+
vec2 iuv = floor( uv );
91+
vec2 fuv = fract( uv );
92+
93+
float g0x = g0(fuv.x);
94+
float g1x = g1(fuv.x);
95+
float h0x = h0(fuv.x);
96+
float h1x = h1(fuv.x);
97+
float h0y = h0(fuv.y);
98+
float h1y = h1(fuv.y);
99+
100+
vec2 p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - 0.5) / res;
101+
vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - 0.5) / res;
102+
vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - 0.5) / res;
103+
vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - 0.5) / res;
104+
105+
return g0(fuv.y) * (g0x * texture2D(tex, p0) +
106+
g1x * texture2D(tex, p1)) +
107+
g1(fuv.y) * (g0x * texture2D(tex, p2) +
108+
g1x * texture2D(tex, p3));
109+
}
110+
111+
112+
void main(void)
113+
{
114+
vec2 p = gl_FragCoord.xy / sketchSize.xy;
115+
vec2 uv = p * zoomLevel;
116+
117+
float textureResolution = 512.0;
118+
119+
vec3 col;
120+
121+
//---------------------------------------------
122+
// regular texture map filtering
123+
//---------------------------------------------
124+
//vec3 colA = texture2D( texture, uv ).xyz;
125+
if( mode == 0 ) {
126+
col = texture2D_smooth( texture, uv, vec2(textureResolution) ).xyz;
127+
}
128+
129+
//---------------------------------------------
130+
// my own filtering
131+
//---------------------------------------------
132+
//vec3 colB = texture2D_smooth( texture, uv, vec2(textureResolution) ).xyz;
133+
else {
134+
col = texture2D_bicubic( texture, uv, vec2(textureResolution) ).xyz;
135+
}
136+
137+
gl_FragColor = vec4( col, 1.0 );
138+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
// Original shader by mrharicot
3+
// https://www.shadertoy.com/view/4dfGDH
4+
5+
// Ported to Processing by Raphaël de Courville <twitter: @sableRaph>
6+
7+
8+
#ifdef GL_ES
9+
precision mediump float;
10+
precision mediump int;
11+
#endif
12+
13+
#define SIGMA 10.0
14+
#define BSIGMA 0.1
15+
#define MSIZE 20
16+
17+
uniform sampler2D texture;
18+
19+
uniform vec2 sketchSize;
20+
21+
float normpdf(in float x, in float sigma)
22+
{
23+
return 0.39894*exp(-0.5*x*x/(sigma*sigma))/sigma;
24+
}
25+
26+
float normpdf3(in vec3 v, in float sigma)
27+
{
28+
return 0.39894*exp(-0.5*dot(v,v)/(sigma*sigma))/sigma;
29+
}
30+
31+
32+
void main(void)
33+
{
34+
vec3 c = texture2D( texture, vec2(0.0, 0.0) + (gl_FragCoord.xy / sketchSize.xy) ).rgb;
35+
36+
//declare stuff
37+
const int kSize = (MSIZE-1)/2;
38+
float kernel[MSIZE];
39+
vec3 final_colour = vec3(0.0);
40+
41+
//create the 1-D kernel
42+
float Z = 0.0;
43+
for (int j = 0; j <= kSize; ++j)
44+
{
45+
kernel[kSize+j] = kernel[kSize-j] = normpdf(float(j), SIGMA);
46+
}
47+
48+
49+
vec3 cc;
50+
float factor;
51+
float bZ = 1.0/normpdf(0.0, BSIGMA);
52+
//read out the texels
53+
for (int i=-kSize; i <= kSize; ++i)
54+
{
55+
for (int j=-kSize; j <= kSize; ++j)
56+
{
57+
cc = texture2D(texture, vec2(0.0, 0.0) + ( gl_FragCoord.xy + vec2(float(i),float(j)) ) / sketchSize.xy ).rgb;
58+
factor = normpdf3(cc-c, BSIGMA)*bZ*kernel[kSize+j]*kernel[kSize+i];
59+
Z += factor;
60+
final_colour += factor*cc;
61+
62+
}
63+
}
64+
65+
gl_FragColor = vec4(final_colour/Z, 1.0);
66+
67+
}

0 commit comments

Comments
 (0)