index.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = void 0;
  6. var _utils = require("@jimp/utils");
  7. /**
  8. * Applies a true Gaussian blur to the image (warning: this is VERY slow)
  9. * @param {number} r the pixel radius of the blur
  10. * @param {function(Error, Jimp)} cb (optional) a callback for when complete
  11. * @returns {Jimp} this for chaining of methods
  12. */
  13. var _default = function _default() {
  14. return {
  15. gaussian: function gaussian(r, cb) {
  16. // http://blog.ivank.net/fastest-gaussian-blur.html
  17. if (typeof r !== 'number') {
  18. return _utils.throwError.call(this, 'r must be a number', cb);
  19. }
  20. if (r < 1) {
  21. return _utils.throwError.call(this, 'r must be greater than 0', cb);
  22. }
  23. var rs = Math.ceil(r * 2.57); // significant radius
  24. var range = rs * 2 + 1;
  25. var rr2 = r * r * 2;
  26. var rr2pi = rr2 * Math.PI;
  27. var weights = [];
  28. for (var y = 0; y < range; y++) {
  29. weights[y] = [];
  30. for (var x = 0; x < range; x++) {
  31. var dsq = Math.pow(x - rs, 2) + Math.pow(y - rs, 2);
  32. weights[y][x] = Math.exp(-dsq / rr2) / rr2pi;
  33. }
  34. }
  35. for (var _y = 0; _y < this.bitmap.height; _y++) {
  36. for (var _x = 0; _x < this.bitmap.width; _x++) {
  37. var red = 0;
  38. var green = 0;
  39. var blue = 0;
  40. var alpha = 0;
  41. var wsum = 0;
  42. for (var iy = 0; iy < range; iy++) {
  43. for (var ix = 0; ix < range; ix++) {
  44. var x1 = Math.min(this.bitmap.width - 1, Math.max(0, ix + _x - rs));
  45. var y1 = Math.min(this.bitmap.height - 1, Math.max(0, iy + _y - rs));
  46. var weight = weights[iy][ix];
  47. var _idx = y1 * this.bitmap.width + x1 << 2;
  48. red += this.bitmap.data[_idx] * weight;
  49. green += this.bitmap.data[_idx + 1] * weight;
  50. blue += this.bitmap.data[_idx + 2] * weight;
  51. alpha += this.bitmap.data[_idx + 3] * weight;
  52. wsum += weight;
  53. }
  54. var idx = _y * this.bitmap.width + _x << 2;
  55. this.bitmap.data[idx] = Math.round(red / wsum);
  56. this.bitmap.data[idx + 1] = Math.round(green / wsum);
  57. this.bitmap.data[idx + 2] = Math.round(blue / wsum);
  58. this.bitmap.data[idx + 3] = Math.round(alpha / wsum);
  59. }
  60. }
  61. }
  62. if ((0, _utils.isNodePattern)(cb)) {
  63. cb.call(this, null, this);
  64. }
  65. return this;
  66. }
  67. };
  68. };
  69. exports["default"] = _default;
  70. //# sourceMappingURL=index.js.map