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