Path: blob/master/node_modules/@jimp/plugin-blur/es/index.js
1126 views
"use strict";12Object.defineProperty(exports, "__esModule", {3value: true4});5exports["default"] = void 0;67var _utils = require("@jimp/utils");89var _blurTables = require("./blur-tables");1011/*12Superfast Blur (0.5)13http://www.quasimondo.com/BoxBlurForCanvas/FastBlur.js1415Copyright (c) 2011 Mario Klingemann1617Permission is hereby granted, free of charge, to any person18obtaining a copy of this software and associated documentation19files (the "Software"), to deal in the Software without20restriction, including without limitation the rights to use,21copy, modify, merge, publish, distribute, sublicense, and/or sell22copies of the Software, and to permit persons to whom the23Software is furnished to do so, subject to the following24conditions:2526The above copyright notice and this permission notice shall be27included in all copies or substantial portions of the Software.2829THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,30EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES31OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND32NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT33HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,34WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING35FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR36OTHER DEALINGS IN THE SOFTWARE.37*/38var _default = function _default() {39return {40/**41* A fast blur algorithm that produces similar effect to a Gaussian blur - but MUCH quicker42* @param {number} r the pixel radius of the blur43* @param {function(Error, Jimp)} cb (optional) a callback for when complete44* @returns {Jimp} this for chaining of methods45*/46blur: function blur(r, cb) {47if (typeof r !== 'number') return _utils.throwError.call(this, 'r must be a number', cb);48if (r < 1) return _utils.throwError.call(this, 'r must be greater than 0', cb);49var rsum;50var gsum;51var bsum;52var asum;53var x;54var y;55var i;56var p;57var p1;58var p2;59var yp;60var yi;61var yw;62var pa;63var wm = this.bitmap.width - 1;64var hm = this.bitmap.height - 1; // const wh = this.bitmap.width * this.bitmap.height;6566var rad1 = r + 1;67var mulSum = _blurTables.mulTable[r];68var shgSum = _blurTables.shgTable[r];69var red = [];70var green = [];71var blue = [];72var alpha = [];73var vmin = [];74var vmax = [];75var iterations = 2;7677while (iterations-- > 0) {78yi = 0;79yw = 0;8081for (y = 0; y < this.bitmap.height; y++) {82rsum = this.bitmap.data[yw] * rad1;83gsum = this.bitmap.data[yw + 1] * rad1;84bsum = this.bitmap.data[yw + 2] * rad1;85asum = this.bitmap.data[yw + 3] * rad1;8687for (i = 1; i <= r; i++) {88p = yw + ((i > wm ? wm : i) << 2);89rsum += this.bitmap.data[p++];90gsum += this.bitmap.data[p++];91bsum += this.bitmap.data[p++];92asum += this.bitmap.data[p];93}9495for (x = 0; x < this.bitmap.width; x++) {96red[yi] = rsum;97green[yi] = gsum;98blue[yi] = bsum;99alpha[yi] = asum;100101if (y === 0) {102vmin[x] = ((p = x + rad1) < wm ? p : wm) << 2;103vmax[x] = (p = x - r) > 0 ? p << 2 : 0;104}105106p1 = yw + vmin[x];107p2 = yw + vmax[x];108rsum += this.bitmap.data[p1++] - this.bitmap.data[p2++];109gsum += this.bitmap.data[p1++] - this.bitmap.data[p2++];110bsum += this.bitmap.data[p1++] - this.bitmap.data[p2++];111asum += this.bitmap.data[p1] - this.bitmap.data[p2];112yi++;113}114115yw += this.bitmap.width << 2;116}117118for (x = 0; x < this.bitmap.width; x++) {119yp = x;120rsum = red[yp] * rad1;121gsum = green[yp] * rad1;122bsum = blue[yp] * rad1;123asum = alpha[yp] * rad1;124125for (i = 1; i <= r; i++) {126yp += i > hm ? 0 : this.bitmap.width;127rsum += red[yp];128gsum += green[yp];129bsum += blue[yp];130asum += alpha[yp];131}132133yi = x << 2;134135for (y = 0; y < this.bitmap.height; y++) {136pa = asum * mulSum >>> shgSum;137this.bitmap.data[yi + 3] = pa; // normalize alpha138139if (pa > 255) {140this.bitmap.data[yi + 3] = 255;141}142143if (pa > 0) {144pa = 255 / pa;145this.bitmap.data[yi] = (rsum * mulSum >>> shgSum) * pa;146this.bitmap.data[yi + 1] = (gsum * mulSum >>> shgSum) * pa;147this.bitmap.data[yi + 2] = (bsum * mulSum >>> shgSum) * pa;148} else {149this.bitmap.data[yi + 2] = 0;150this.bitmap.data[yi + 1] = 0;151this.bitmap.data[yi] = 0;152}153154if (x === 0) {155vmin[y] = ((p = y + rad1) < hm ? p : hm) * this.bitmap.width;156vmax[y] = (p = y - r) > 0 ? p * this.bitmap.width : 0;157}158159p1 = x + vmin[y];160p2 = x + vmax[y];161rsum += red[p1] - red[p2];162gsum += green[p1] - green[p2];163bsum += blue[p1] - blue[p2];164asum += alpha[p1] - alpha[p2];165yi += this.bitmap.width << 2;166}167}168}169170if ((0, _utils.isNodePattern)(cb)) {171cb.call(this, null, this);172}173174return this;175}176};177};178179exports["default"] = _default;180//# sourceMappingURL=index.js.map181182