Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@jimp/plugin-mask/src/index.js
1126 views
1
import { isNodePattern, throwError } from '@jimp/utils';
2
3
/**
4
* Masks a source image on to this image using average pixel colour. A completely black pixel on the mask will turn a pixel in the image completely transparent.
5
* @param {Jimp} src the source Jimp instance
6
* @param {number} x the horizontal position to blit the image
7
* @param {number} y the vertical position to blit the image
8
* @param {function(Error, Jimp)} cb (optional) a callback for when complete
9
* @returns {Jimp} this for chaining of methods
10
*/
11
export default () => ({
12
mask(src, x = 0, y = 0, cb) {
13
if (!(src instanceof this.constructor)) {
14
return throwError.call(this, 'The source must be a Jimp image', cb);
15
}
16
17
if (typeof x !== 'number' || typeof y !== 'number') {
18
return throwError.call(this, 'x and y must be numbers', cb);
19
}
20
21
// round input
22
x = Math.round(x);
23
y = Math.round(y);
24
25
const w = this.bitmap.width;
26
const h = this.bitmap.height;
27
const baseImage = this;
28
29
src.scanQuiet(0, 0, src.bitmap.width, src.bitmap.height, function(
30
sx,
31
sy,
32
idx
33
) {
34
const destX = x + sx;
35
const destY = y + sy;
36
37
if (destX >= 0 && destY >= 0 && destX < w && destY < h) {
38
const dstIdx = baseImage.getPixelIndex(destX, destY);
39
const { data } = this.bitmap;
40
const avg = (data[idx + 0] + data[idx + 1] + data[idx + 2]) / 3;
41
42
baseImage.bitmap.data[dstIdx + 3] *= avg / 255;
43
}
44
});
45
46
if (isNodePattern(cb)) {
47
cb.call(this, null, this);
48
}
49
50
return this;
51
}
52
});
53
54