My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
from __future__ import with_statement
from pymt import *
import pyglet

class Filter:
def __init__(self):
self.shader = None
self.current_shader = 0
self.fbo = None
self.current_texture = None

def blur(self,texture,texture_size,value):
blurv_shader_src = """
void main()

{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}
"""

blurf_shader_src = """
uniform sampler2D tex;
uniform float direction;
uniform float kernel_size;
uniform float size_x ;
uniform float size_y ;
uniform float value;
void main (void) {
float rho = 10.0;
vec2 dir = direction < 0.5 ? vec2(1.0,0.0) : vec2(0.0,1.0);

float dx = 1.0 / size_x;
float dy = 1.0 / size_y;

vec2 st = gl_TexCoord [0].st;

vec4 color = vec4 (0.0, 0.0, 0.0, 0.0);
float weight = 0.0;
for (float i = -1.0*kernel_size ; i <= kernel_size ; i+=1.0) {
float fac = exp (-(i * i) / (2.0 * rho * rho));
weight += fac;
color += texture2D (tex, st + vec2 (dx*i, dy*i) * dir) * fac;
}
gl_FragColor = color / weight;
}
"""

if self.current_shader != 1 :
self.shader = Shader(vertex_source=blurv_shader_src, fragment_source=blurf_shader_src)
self.current_shader = 1

if self.current_texture != texture :
self.current_texture = texture
self.fbo = Fbo(size=texture_size, with_depthbuffer=False)

first_pass_fbo = Fbo(size=texture_size, with_depthbuffer=False)
#horizontal pass
with first_pass_fbo:
self.shader.use()
#self.shader['value'] = value
self.shader['size_x'] = float(texture_size[0])
self.shader['size_y'] = float(texture_size[1])
self.shader['kernel_size'] = value
self.shader['direction'] = 0.0
self.shader['value'] = value
set_color(1, 1, 1)
drawTexturedRectangle(self.current_texture, size=self.fbo.size)
self.shader.stop()

#vertical pass
with self.fbo:
self.shader.use()
#self.shader['value'] = value
self.shader['size_x'] = float(texture_size[0])
self.shader['size_y'] = float(texture_size[1])
self.shader['kernel_size'] = value
self.shader['direction'] = 1.0
self.shader['value'] = value
set_color(1, 1, 1)
drawTexturedRectangle(first_pass_fbo.texture, size=self.fbo.size)
self.shader.stop()

return self.fbo.texture

def sharpen(self,texture,texture_size,value):
sharpenv_shader_src = """
void main() {

gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();

}
"""

sharpenf_shader_src = """
#define KERNEL_SIZE 9

float kernel[KERNEL_SIZE];

uniform sampler2D colorMap;
uniform float width;
uniform float height;

uniform float value;

float step_w = value/width;
float step_h = value/height;

vec2 offset[KERNEL_SIZE];

void main(void)
{
int i = 0;
vec4 sum = vec4(0.0);

offset[0] = vec2(-step_w, -step_h);
offset[1] = vec2(0.0, -step_h);
offset[2] = vec2(step_w, -step_h);

offset[3] = vec2(-step_w, 0.0);
offset[4] = vec2(0.0, 0.0);
offset[5] = vec2(step_w, 0.0);

offset[6] = vec2(-step_w, step_h);
offset[7] = vec2(0.0, step_h);
offset[8] = vec2(step_w, step_h);

kernel[0] = -1.0; kernel[1] = -1.0; kernel[2] = -1.0;
kernel[3] = -1.0; kernel[4] = 9.0; kernel[5] = -1.0;
kernel[6] = -1.0; kernel[7] = -1.0; kernel[8] = -1.0;


for( i=0; i<KERNEL_SIZE; i++ )
{
vec4 tmp = texture2D(colorMap, gl_TexCoord[0].st + offset[i]);
sum += tmp * kernel[i];
}

gl_FragColor = sum;
}
"""

if self.current_shader != 2 :
self.shader = Shader(vertex_source=sharpenv_shader_src, fragment_source=sharpenf_shader_src)
self.current_shader = 2

if self.current_texture != texture :
self.current_texture = texture
self.fbo = Fbo(size=texture_size, with_depthbuffer=False)

#run glsl code
with self.fbo:
self.shader.use()
self.shader['width'] = float(texture_size[0])
self.shader['height'] = float(texture_size[1])
self.shader['value'] = value
set_color(1, 1, 1)
drawTexturedRectangle(self.current_texture, size=self.fbo.size)
self.shader.stop()

return self.fbo.texture

def brightness(self,texture,texture_size,value):
brightnessv_shader_src = """
void main() {
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}
"""

brightnessf_shader_src = """
uniform float value;
uniform sampler2D last_spot;
void main()
{
vec4 col = texture2D(last_spot, gl_TexCoord[0].st);
gl_FragColor = mix(gl_FragColor, col, value);
}
"""

if self.current_shader != 3 :
self.shader = Shader(vertex_source=brightnessv_shader_src, fragment_source=brightnessf_shader_src)
self.current_shader = 3

if self.current_texture != texture :
self.current_texture = texture
self.fbo = Fbo(size=texture_size, with_depthbuffer=False)

#run glsl code
with self.fbo:
self.shader.use()
self.shader['value'] = value
set_color(1, 1, 1)
drawTexturedRectangle(self.current_texture, size=self.fbo.size)
self.shader.stop()

return self.fbo.texture

def contrast(self,texture,texture_size,value):
contrastv_shader_src = """
void main() {
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}
"""

contrastf_shader_src = """
uniform float value;
uniform sampler2D last_spot;
void main()
{
vec4 col = texture2D(last_spot, gl_TexCoord[0].st);

// Increase or decrease theese values to adjust r, g and b color channels seperately
const float AvgLumR = 0.5;
const float AvgLumG = 0.5;
const float AvgLumB = 0.5;
const vec4 LumCoeff = vec4(0.2125, 0.7154, 0.0721, 1.0);

vec4 AvgLumin = vec4(AvgLumR, AvgLumG, AvgLumB, 1.0);
vec4 brtColor = col * 1.0;
vec4 intensity = vec4(dot(brtColor, LumCoeff));
vec4 satColor = mix(intensity, brtColor, 1.0);
vec4 conColor = mix(AvgLumin, satColor, value);

gl_FragColor = mix(gl_FragColor, conColor, 1.0);
}

"""

if self.current_shader != 4 :
self.shader = Shader(fragment_source=contrastf_shader_src)
self.current_shader = 4

if self.current_texture != texture :
self.current_texture = texture
self.fbo = Fbo(size=texture_size, with_depthbuffer=False)

#run glsl code
with self.fbo:
self.shader.use()
self.shader['value'] = value
set_color(1, 1, 1)
drawTexturedRectangle(self.current_texture, size=self.fbo.size)
self.shader.stop()

return self.fbo.texture

def saturation(self,texture,texture_size,value):
saturationf_shader_src = """
uniform float value;
uniform sampler2D last_spot;
void main()
{
vec4 col = texture2D(last_spot, gl_TexCoord[0].st);

// Increase or decrease theese values to adjust r, g and b color channels seperately
const float AvgLumR = 0.5;
const float AvgLumG = 0.5;
const float AvgLumB = 0.5;
const vec4 LumCoeff = vec4(0.2125, 0.7154, 0.0721, 1.0);

vec4 AvgLumin = vec4(AvgLumR, AvgLumG, AvgLumB, 1.0);
vec4 brtColor = col * 1.0;
vec4 intensity = vec4(dot(brtColor, LumCoeff));
vec4 satColor = mix(intensity, brtColor, value);

gl_FragColor = mix(gl_FragColor, satColor, 1.0);
}

"""

if self.current_shader != 5 :
self.shader = Shader(fragment_source=saturationf_shader_src)
self.current_shader = 5

if self.current_texture != texture :
self.current_texture = texture
self.fbo = Fbo(size=texture_size, with_depthbuffer=False)

#run glsl code
with self.fbo:
self.shader.use()
self.shader['value'] = value
set_color(1, 1, 1)
drawTexturedRectangle(self.current_texture, size=self.fbo.size)
self.shader.stop()

return self.fbo.texture

def bw(self,texture,texture_size,value):
bwf_shader_src = """
uniform float value;
uniform sampler2D last_spot;
void main()
{
vec4 col = texture2D(last_spot, gl_TexCoord[0].st);
float or,og,ob,r,g,b;
or = col.r;
og = col.g;
ob = col.b;

r = (or * 0.3086 + og * 0.6094 + ob * 0.0820);
g = (or * 0.3086 + og * 0.6094 + ob * 0.0820);
b = (or * 0.3086 + og * 0.6094 + ob * 0.0820);

vec4 finalColor = vec4(r,g,b,1.0);
gl_FragColor = mix(gl_FragColor, finalColor, 1.0);
}

"""

if self.current_shader != 6 :
self.shader = Shader(fragment_source=bwf_shader_src)
self.current_shader = 6

if self.current_texture != texture :
self.current_texture = texture
self.fbo = Fbo(size=texture_size, with_depthbuffer=False)

#run glsl code
with self.fbo:
self.shader.use()
self.shader['value'] = value
set_color(1, 1, 1)
drawTexturedRectangle(self.current_texture, size=self.fbo.size)
self.shader.stop()

return self.fbo.texture

def sepia(self,texture,texture_size,value):
sepiaf_shader_src = """
uniform float value;
uniform sampler2D last_spot;
void main()
{
vec4 col = texture2D(last_spot, gl_TexCoord[0].st);
float or,og,ob,r,g,b;
or = col.r;
og = col.g;
ob = col.b;

r = (or * 0.393 + og * 0.769 + ob * 0.189);
g = (or * 0.349 + og * 0.686 + ob * 0.168);
b = (or * 0.272 + og * 0.534 + ob * 0.131);

vec4 finalColor = vec4(r,g,b,1.0);
gl_FragColor = mix(gl_FragColor, finalColor, 1.0);
}

"""

if self.current_shader != 7 :
self.shader = Shader(fragment_source=sepiaf_shader_src)
self.current_shader = 7

if self.current_texture != texture :
self.current_texture = texture
self.fbo = Fbo(size=texture_size, with_depthbuffer=False)

#run glsl code
with self.fbo:
self.shader.use()
self.shader['value'] = value
set_color(1, 1, 1)
drawTexturedRectangle(self.current_texture, size=self.fbo.size)
self.shader.stop()

return self.fbo.texture

def circularblur(self,texture,texture_size,value):
blurv_shader_src = """
varying vec3 position;
void main()

{
gl_TexCoord[0] = gl_MultiTexCoord0;
position = vec3(gl_MultiTexCoord0 - 0.5) * 5.0;
gl_Position = ftransform();
}
"""

blurf_shader_src = """
uniform sampler2D tex;
uniform float direction;
uniform float kernel_size;
uniform float size_x ;
uniform float size_y ;
uniform float value;
varying vec3 position;
void main (void) {
float rho = 10.0;
vec2 dir = direction < 0.5 ? vec2(1.0,0.0) : vec2(0.0,1.0);
vec4 orgcolor = texture2D(tex, gl_TexCoord[0].st);

float dx = 1.0 / size_x;
float dy = 1.0 / size_y;

vec2 st = gl_TexCoord [0].st;

vec4 color = vec4 (0.0, 0.0, 0.0, 0.0);
float weight = 0.0;
for (float i = -1.0*kernel_size ; i <= kernel_size ; i+=1.0) {
float fac = exp (-(i * i) / (2.0 * rho * rho));
weight += fac;
color += texture2D (tex, st + vec2 (dx*i, dy*i) * dir) * fac;
}
float radius = 0.5;

float point_dist = sqrt((0.5-gl_TexCoord[0].s)*(0.5-gl_TexCoord[0].s)+(0.5-gl_TexCoord[0].t)*(0.5-gl_TexCoord[0].t));

if (point_dist < 0.5) {
gl_FragColor = color / weight;
}
else
{
gl_FragColor = vec4(0,0,0,0.0);//vec4(1,0,0,0.0);
}
}
"""

if self.current_shader != 8 :
self.shader = Shader(vertex_source=blurv_shader_src, fragment_source=blurf_shader_src)
self.current_shader = 8

if self.current_texture != texture :
self.current_texture = texture
self.fbo = Fbo(size=texture_size, with_depthbuffer=False)

first_pass_fbo = Fbo(size=texture_size, with_depthbuffer=False)
#horizontal pass
with first_pass_fbo:
self.shader.use()
#self.shader['value'] = value
self.shader['size_x'] = float(texture_size[0])
self.shader['size_y'] = float(texture_size[1])
self.shader['kernel_size'] = value
self.shader['direction'] = 0.0
self.shader['value'] = value
set_color(1, 1, 1)
drawTexturedRectangle(self.current_texture, size=self.fbo.size)
self.shader.stop()

#vertical pass
with self.fbo:
self.shader.use()
#self.shader['value'] = value
self.shader['size_x'] = float(texture_size[0])
self.shader['size_y'] = float(texture_size[1])
self.shader['kernel_size'] = value
self.shader['direction'] = 1.0
self.shader['value'] = value
set_color(1, 1, 1)
drawTexturedRectangle(first_pass_fbo.texture, size=self.fbo.size)
self.shader.stop()

return self.fbo.texture

def eraser(self,texture,texture_size,value):
erasev_shader_src = """
void main(void)
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
"""

erasef_shader_src = """
uniform sampler2D tex;
void main (void) {
vec4 orgcolor = texture2D(tex, gl_TexCoord[0].st);

float point_dist = sqrt((0.5-gl_TexCoord[0].s)*(0.5-gl_TexCoord[0].s)+(0.5-gl_TexCoord[0].t)*(0.5-gl_TexCoord[0].t));

if (point_dist < 0.5) {
gl_FragColor = vec4(0,1,0,1.0);
}
else
{
gl_FragColor = vec4(1,0,0,1.0);
}
}
"""

if self.current_shader != 9 :
self.shader = Shader(vertex_source=erasev_shader_src, fragment_source=erasef_shader_src)
self.current_shader = 9

if self.current_texture != texture :
self.current_texture = texture
self.fbo = Fbo(size=texture_size, with_depthbuffer=False)

#horizontal pass
with self.fbo:
self.shader.use()
set_color(1, 1, 1)
drawTexturedRectangle(self.current_texture, size=self.fbo.size)
self.shader.stop()

return self.fbo.texture
Show details Hide details

Change log

r90 by sharath.patali on Aug 16, 2009   Diff
Core: Implemented Erase and Smudge GLSL
Brushes, and Bug fixes all over
Go to: 
Project members, sign in to write a code review

Older revisions

r87 by sharath.patali on Aug 16, 2009   Diff
Core: Implemented cut copy paste.
Fixed  issue 2  (File Browser opening
issue)
r77 by sharath.patali on Aug 14, 2009   Diff
Brushes And Smudge: Fixes and
Optimization
r64 by sharath.patali on Aug 07, 2009   Diff
Core: Minor changes to the core init
All revisions of this file

File info

Size: 18884 bytes, 501 lines
Hosted by Google Code