forked from pmndrs/postprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGaussKernel.js
More file actions
195 lines (131 loc) · 3.78 KB
/
GaussKernel.js
File metadata and controls
195 lines (131 loc) · 3.78 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
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
/**
* Uses Pascal's Triangle to generate coefficients in the expansion of any binomial expression.
*
* For details see https://mathworld.wolfram.com/PascalsTriangle.html.
*
* @param {Number} n - The index of the coefficients row in Pascal's Triangle.
* @return {Float64Array} The integer coefficients stored as doubles.
* @ignore
*/
function getCoefficients(n) {
let result;
if(n === 0) {
result = new Float64Array(0);
} else if(n === 1) {
result = new Float64Array([1]);
} else if(n > 1) {
// Incrementally build Pascal's Triangle to get the desired row.
let row0 = new Float64Array(n);
let row1 = new Float64Array(n);
for(let y = 1; y <= n; ++y) {
for(let x = 0; x < y; ++x) {
row1[x] = (x === 0 || x === y - 1) ? 1 : row0[x - 1] + row0[x];
}
result = row1;
row1 = row0;
row0 = result;
}
}
return result;
}
/**
* A Gauss kernel.
*
* Based on https://github.com/Jam3/glsl-fast-gaussian-blur.
*/
export class GaussKernel {
/**
* Constructs a new Gauss kernel.
*
* @param {Number} kernelSize - The kernel size. Should be an odd number in the range [3, 1020].
* @param {Number} [edgeBias=2] - Determines how many edge coefficients should be cut off for increased accuracy.
*/
constructor(kernelSize, edgeBias = 2) {
/**
* The weights for discrete sampling.
*
* @type {Float64Array}
*/
this.weights = null;
/**
* The offsets for discrete sampling.
*
* @type {Float64Array}
*/
this.offsets = null;
/**
* The weights for linear sampling.
*
* @type {Float64Array}
*/
this.linearWeights = null;
/**
* The offsets for linear sampling.
*
* @type {Float64Array}
*/
this.linearOffsets = null;
this.generate(kernelSize, edgeBias);
}
/**
* The number of steps for discrete sampling.
*
* @type {Number}
*/
get steps() {
return (this.offsets === null) ? 0 : this.offsets.length;
}
/**
* The number of steps for linear sampling.
*
* @type {Number}
*/
get linearSteps() {
return (this.linearOffsets === null) ? 0 : this.linearOffsets.length;
}
/**
* Generates the kernel.
*
* @private
* @param {Number} kernelSize - The kernel size.
* @param {Number} edgeBias - The amount of edge coefficients to ignore.
*/
generate(kernelSize, edgeBias) {
if(kernelSize < 3 || kernelSize > 1020) {
throw new Error("The kernel size must be in the range [3, 1020]");
}
const n = kernelSize + edgeBias * 2;
const coefficients = (edgeBias > 0) ?
getCoefficients(n).slice(edgeBias, -edgeBias) :
getCoefficients(n);
const mid = Math.floor((coefficients.length - 1) / 2);
const sum = coefficients.reduce((a, b) => a + b, 0);
const weights = coefficients.slice(mid);
const offsets = [...Array(mid + 1).keys()]; // [0..mid+1]
const linearWeights = new Float64Array(Math.floor(offsets.length / 2));
const linearOffsets = new Float64Array(linearWeights.length);
linearWeights[0] = weights[0] / sum;
for(let i = 1, j = 1, l = offsets.length - 1; i < l; i += 2, ++j) {
const offset0 = offsets[i], offset1 = offsets[i + 1];
const weight0 = weights[i], weight1 = weights[i + 1];
const w = weight0 + weight1;
const o = (offset0 * weight0 + offset1 * weight1) / w;
linearWeights[j] = w / sum;
linearOffsets[j] = o;
}
for(let i = 0, l = weights.length, s = 1.0 / sum; i < l; ++i) {
weights[i] *= s;
}
// Ensure that the weights add up to 1.
const linearWeightSum = (linearWeights.reduce((a, b) => a + b, 0) - linearWeights[0] * 0.5) * 2.0;
if(linearWeightSum !== 0.0) {
for(let i = 0, l = linearWeights.length, s = 1.0 / linearWeightSum; i < l; ++i) {
linearWeights[i] *= s;
}
}
this.offsets = offsets;
this.weights = weights;
this.linearOffsets = linearOffsets;
this.linearWeights = linearWeights;
}
}