Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@jimp/plugin-blur/src/index.js
1126 views
1
import { throwError, isNodePattern } from '@jimp/utils';
2
import { mulTable, shgTable } from './blur-tables';
3
4
/*
5
Superfast Blur (0.5)
6
http://www.quasimondo.com/BoxBlurForCanvas/FastBlur.js
7
8
Copyright (c) 2011 Mario Klingemann
9
10
Permission is hereby granted, free of charge, to any person
11
obtaining a copy of this software and associated documentation
12
files (the "Software"), to deal in the Software without
13
restriction, including without limitation the rights to use,
14
copy, modify, merge, publish, distribute, sublicense, and/or sell
15
copies of the Software, and to permit persons to whom the
16
Software is furnished to do so, subject to the following
17
conditions:
18
19
The above copyright notice and this permission notice shall be
20
included in all copies or substantial portions of the Software.
21
22
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
24
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
27
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29
OTHER DEALINGS IN THE SOFTWARE.
30
*/
31
32
export default () => ({
33
/**
34
* A fast blur algorithm that produces similar effect to a Gaussian blur - but MUCH quicker
35
* @param {number} r the pixel radius of the blur
36
* @param {function(Error, Jimp)} cb (optional) a callback for when complete
37
* @returns {Jimp} this for chaining of methods
38
*/
39
blur(r, cb) {
40
if (typeof r !== 'number')
41
return throwError.call(this, 'r must be a number', cb);
42
if (r < 1) return throwError.call(this, 'r must be greater than 0', cb);
43
44
let rsum;
45
let gsum;
46
let bsum;
47
let asum;
48
let x;
49
let y;
50
let i;
51
let p;
52
let p1;
53
let p2;
54
let yp;
55
let yi;
56
let yw;
57
let pa;
58
59
const wm = this.bitmap.width - 1;
60
const hm = this.bitmap.height - 1;
61
// const wh = this.bitmap.width * this.bitmap.height;
62
const rad1 = r + 1;
63
64
const mulSum = mulTable[r];
65
const shgSum = shgTable[r];
66
67
const red = [];
68
const green = [];
69
const blue = [];
70
const alpha = [];
71
72
const vmin = [];
73
const vmax = [];
74
75
let iterations = 2;
76
77
while (iterations-- > 0) {
78
yi = 0;
79
yw = 0;
80
81
for (y = 0; y < this.bitmap.height; y++) {
82
rsum = this.bitmap.data[yw] * rad1;
83
gsum = this.bitmap.data[yw + 1] * rad1;
84
bsum = this.bitmap.data[yw + 2] * rad1;
85
asum = this.bitmap.data[yw + 3] * rad1;
86
87
for (i = 1; i <= r; i++) {
88
p = yw + ((i > wm ? wm : i) << 2);
89
rsum += this.bitmap.data[p++];
90
gsum += this.bitmap.data[p++];
91
bsum += this.bitmap.data[p++];
92
asum += this.bitmap.data[p];
93
}
94
95
for (x = 0; x < this.bitmap.width; x++) {
96
red[yi] = rsum;
97
green[yi] = gsum;
98
blue[yi] = bsum;
99
alpha[yi] = asum;
100
101
if (y === 0) {
102
vmin[x] = ((p = x + rad1) < wm ? p : wm) << 2;
103
vmax[x] = (p = x - r) > 0 ? p << 2 : 0;
104
}
105
106
p1 = yw + vmin[x];
107
p2 = yw + vmax[x];
108
109
rsum += this.bitmap.data[p1++] - this.bitmap.data[p2++];
110
gsum += this.bitmap.data[p1++] - this.bitmap.data[p2++];
111
bsum += this.bitmap.data[p1++] - this.bitmap.data[p2++];
112
asum += this.bitmap.data[p1] - this.bitmap.data[p2];
113
114
yi++;
115
}
116
117
yw += this.bitmap.width << 2;
118
}
119
120
for (x = 0; x < this.bitmap.width; x++) {
121
yp = x;
122
rsum = red[yp] * rad1;
123
gsum = green[yp] * rad1;
124
bsum = blue[yp] * rad1;
125
asum = alpha[yp] * rad1;
126
127
for (i = 1; i <= r; i++) {
128
yp += i > hm ? 0 : this.bitmap.width;
129
rsum += red[yp];
130
gsum += green[yp];
131
bsum += blue[yp];
132
asum += alpha[yp];
133
}
134
135
yi = x << 2;
136
137
for (y = 0; y < this.bitmap.height; y++) {
138
pa = (asum * mulSum) >>> shgSum;
139
this.bitmap.data[yi + 3] = pa;
140
141
// normalize alpha
142
if (pa > 255) {
143
this.bitmap.data[yi + 3] = 255;
144
}
145
146
if (pa > 0) {
147
pa = 255 / pa;
148
this.bitmap.data[yi] = ((rsum * mulSum) >>> shgSum) * pa;
149
this.bitmap.data[yi + 1] = ((gsum * mulSum) >>> shgSum) * pa;
150
this.bitmap.data[yi + 2] = ((bsum * mulSum) >>> shgSum) * pa;
151
} else {
152
this.bitmap.data[yi + 2] = 0;
153
this.bitmap.data[yi + 1] = 0;
154
this.bitmap.data[yi] = 0;
155
}
156
157
if (x === 0) {
158
vmin[y] = ((p = y + rad1) < hm ? p : hm) * this.bitmap.width;
159
vmax[y] = (p = y - r) > 0 ? p * this.bitmap.width : 0;
160
}
161
162
p1 = x + vmin[y];
163
p2 = x + vmax[y];
164
165
rsum += red[p1] - red[p2];
166
gsum += green[p1] - green[p2];
167
bsum += blue[p1] - blue[p2];
168
asum += alpha[p1] - alpha[p2];
169
170
yi += this.bitmap.width << 2;
171
}
172
}
173
}
174
175
if (isNodePattern(cb)) {
176
cb.call(this, null, this);
177
}
178
179
return this;
180
}
181
});
182
183