-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathseriously.edge.js
More file actions
170 lines (148 loc) · 4.5 KB
/
seriously.edge.js
File metadata and controls
170 lines (148 loc) · 4.5 KB
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
/* global define, require */
(function (root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['seriously'], factory);
} else if (typeof exports === 'object') {
// Node/CommonJS
factory(require('seriously'));
} else {
if (!root.Seriously) {
root.Seriously = { plugin: function (name, opt) { this[name] = opt; } };
}
factory(root.Seriously);
}
}(window, function (Seriously) {
'use strict';
// Adapted from http://rastergrid.com/blog/2011/01/frei-chen-edge-detector/
var sqrt = Math.sqrt,
i, j,
flatMatrices = [],
matrices,
freiChenMatrixConstants,
sobelMatrixConstants;
//initialize shader matrix arrays
function multiplyArray(factor, a) {
var i;
for (i = 0; i < a.length; i++) {
a[i] *= factor;
}
return a;
}
matrices = [
multiplyArray(1.0 / (2.0 * sqrt(2.0)), [ 1.0, sqrt(2.0), 1.0, 0.0, 0.0, 0.0, -1.0, -sqrt(2.0), -1.0 ]),
multiplyArray(1.0 / (2.0 * sqrt(2.0)), [1.0, 0.0, -1.0, sqrt(2.0), 0.0, -sqrt(2.0), 1.0, 0.0, -1.0]),
multiplyArray(1.0 / (2.0 * sqrt(2.0)), [0.0, -1.0, sqrt(2.0), 1.0, 0.0, -1.0, -sqrt(2.0), 1.0, 0.0]),
multiplyArray(1.0 / (2.0 * sqrt(2.0)), [sqrt(2.0), -1.0, 0.0, -1.0, 0.0, 1.0, 0.0, 1.0, -sqrt(2.0)]),
multiplyArray(1.0 / 2.0, [0.0, 1.0, 0.0, -1.0, 0.0, -1.0, 0.0, 1.0, 0.0]),
multiplyArray(1.0 / 2.0, [-1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, -1.0]),
multiplyArray(1.0 / 6.0, [1.0, -2.0, 1.0, -2.0, 4.0, -2.0, 1.0, -2.0, 1.0]),
multiplyArray(1.0 / 6.0, [-2.0, 1.0, -2.0, 1.0, 4.0, 1.0, -2.0, 1.0, -2.0]),
multiplyArray(1.0 / 3.0, [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
];
for (i = 0; i < matrices.length; i++) {
for (j = 0; j < matrices[i].length; j++) {
flatMatrices.push(matrices[i][j]);
}
}
freiChenMatrixConstants = new Float32Array(flatMatrices);
sobelMatrixConstants = new Float32Array([
1.0, 2.0, 1.0, 0.0, 0.0, 0.0, -1.0, -2.0, -1.0,
1.0, 0.0, -1.0, 2.0, 0.0, -2.0, 1.0, 0.0, -1.0
]);
Seriously.plugin('edge', {
initialize: function (initialize) {
initialize();
this.uniforms.pixelWidth = 1 / this.width;
this.uniforms.pixelHeight = 1 / this.height;
if (this.inputs.mode === 'sobel') {
this.uniforms.G = sobelMatrixConstants;
} else {
this.uniforms.G = freiChenMatrixConstants;
}
},
shader: function (inputs, shaderSource) {
var defines;
if (inputs.mode === 'sobel') {
defines = '#define N_MATRICES 2\n' +
'#define SOBEL\n';
} else {
//frei-chen
defines = '#define N_MATRICES 9\n';
}
shaderSource.fragment = [
defines,
'precision mediump float;',
'varying vec2 vTexCoord;',
'uniform sampler2D source;',
'uniform float pixelWidth;',
'uniform float pixelHeight;',
'uniform mat3 G[9];',
'void main(void) {',
' mat3 I;',
' float dp3, cnv[9];',
' vec3 tc;',
// fetch the 3x3 neighbourhood and use the RGB vector's length as intensity value
' float fi = 0.0, fj = 0.0;',
' for (int i = 0; i < 3; i++) {',
' fj = 0.0;',
' for (int j = 0; j < 3; j++) {',
' I[i][j] = length( ' +
'texture2D(source, ' +
'vTexCoord + vec2((fi - 1.0) * pixelWidth, (fj - 1.0) * pixelHeight)' +
').rgb );',
' fj += 1.0;',
' };',
' fi += 1.0;',
' };',
// calculate the convolution values for all the masks
' for (int i = 0; i < N_MATRICES; i++) {',
' dp3 = dot(G[i][0], I[0]) + dot(G[i][1], I[1]) + dot(G[i][2], I[2]);',
' cnv[i] = dp3 * dp3;',
' };',
//Sobel
'#ifdef SOBEL',
' tc = vec3(0.5 * sqrt(cnv[0]*cnv[0]+cnv[1]*cnv[1]));',
'#else',
//Frei-Chen
// Line detector
' float M = (cnv[4] + cnv[5]) + (cnv[6] + cnv[7]);',
' float S = (cnv[0] + cnv[1]) + (cnv[2] + cnv[3]) + (cnv[4] + cnv[5]) + (cnv[6] + cnv[7]) + cnv[8];',
' tc = vec3(sqrt(M/S));',
'#endif',
' gl_FragColor = vec4(tc, 1.0);',
'}'
].join('\n');
return shaderSource;
},
resize: function () {
this.uniforms.pixelWidth = 1 / this.width;
this.uniforms.pixelHeight = 1 / this.height;
},
inputs: {
source: {
type: 'image',
uniform: 'source'
},
mode: {
type: 'enum',
shaderDirty: true,
defaultValue: 'sobel',
options: [
['sobel', 'Sobel'],
['frei-chen', 'Frei-Chen']
],
update: function () {
if (this.inputs.mode === 'sobel') {
this.uniforms.G = sobelMatrixConstants;
} else {
this.uniforms.G = freiChenMatrixConstants;
}
}
}
},
description: 'Edge Detect',
title: 'Edge Detect'
});
}));