Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@jimp/plugin-gaussian/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
/**
11
* Applies a true Gaussian blur to the image (warning: this is VERY slow)
12
* @param {number} r the pixel radius of the blur
13
* @param {function(Error, Jimp)} cb (optional) a callback for when complete
14
* @returns {Jimp} this for chaining of methods
15
*/
16
var _default = function _default() {
17
return {
18
gaussian: function gaussian(r, cb) {
19
// http://blog.ivank.net/fastest-gaussian-blur.html
20
if (typeof r !== 'number') {
21
return _utils.throwError.call(this, 'r must be a number', cb);
22
}
23
24
if (r < 1) {
25
return _utils.throwError.call(this, 'r must be greater than 0', cb);
26
}
27
28
var rs = Math.ceil(r * 2.57); // significant radius
29
30
var range = rs * 2 + 1;
31
var rr2 = r * r * 2;
32
var rr2pi = rr2 * Math.PI;
33
var weights = [];
34
35
for (var y = 0; y < range; y++) {
36
weights[y] = [];
37
38
for (var x = 0; x < range; x++) {
39
var dsq = Math.pow(x - rs, 2) + Math.pow(y - rs, 2);
40
weights[y][x] = Math.exp(-dsq / rr2) / rr2pi;
41
}
42
}
43
44
for (var _y = 0; _y < this.bitmap.height; _y++) {
45
for (var _x = 0; _x < this.bitmap.width; _x++) {
46
var red = 0;
47
var green = 0;
48
var blue = 0;
49
var alpha = 0;
50
var wsum = 0;
51
52
for (var iy = 0; iy < range; iy++) {
53
for (var ix = 0; ix < range; ix++) {
54
var x1 = Math.min(this.bitmap.width - 1, Math.max(0, ix + _x - rs));
55
var y1 = Math.min(this.bitmap.height - 1, Math.max(0, iy + _y - rs));
56
var weight = weights[iy][ix];
57
58
var _idx = y1 * this.bitmap.width + x1 << 2;
59
60
red += this.bitmap.data[_idx] * weight;
61
green += this.bitmap.data[_idx + 1] * weight;
62
blue += this.bitmap.data[_idx + 2] * weight;
63
alpha += this.bitmap.data[_idx + 3] * weight;
64
wsum += weight;
65
}
66
67
var idx = _y * this.bitmap.width + _x << 2;
68
this.bitmap.data[idx] = Math.round(red / wsum);
69
this.bitmap.data[idx + 1] = Math.round(green / wsum);
70
this.bitmap.data[idx + 2] = Math.round(blue / wsum);
71
this.bitmap.data[idx + 3] = Math.round(alpha / wsum);
72
}
73
}
74
}
75
76
if ((0, _utils.isNodePattern)(cb)) {
77
cb.call(this, null, this);
78
}
79
80
return this;
81
}
82
};
83
};
84
85
exports["default"] = _default;
86
module.exports = exports.default;
87
//# sourceMappingURL=index.js.map
88