Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@jimp/plugin-blit/src/index.js
1126 views
1
import { throwError, isNodePattern } from '@jimp/utils';
2
3
export default () => ({
4
/**
5
* Blits a source image on to this image
6
* @param {Jimp} src the source Jimp instance
7
* @param {number} x the x position to blit the image
8
* @param {number} y the y position to blit the image
9
* @param {number} srcx (optional) the x position from which to crop the source image
10
* @param {number} srcy (optional) the y position from which to crop the source image
11
* @param {number} srcw (optional) the width to which to crop the source image
12
* @param {number} srch (optional) the height to which to crop the source image
13
* @param {function(Error, Jimp)} cb (optional) a callback for when complete
14
* @returns {Jimp} this for chaining of methods
15
*/
16
blit(src, x, y, srcx, srcy, srcw, srch, cb) {
17
if (!(src instanceof this.constructor)) {
18
return throwError.call(this, 'The source must be a Jimp image', cb);
19
}
20
21
if (typeof x !== 'number' || typeof y !== 'number') {
22
return throwError.call(this, 'x and y must be numbers', cb);
23
}
24
25
if (typeof srcx === 'function') {
26
cb = srcx;
27
srcx = 0;
28
srcy = 0;
29
srcw = src.bitmap.width;
30
srch = src.bitmap.height;
31
} else if (
32
typeof srcx === typeof srcy &&
33
typeof srcy === typeof srcw &&
34
typeof srcw === typeof srch
35
) {
36
srcx = srcx || 0;
37
srcy = srcy || 0;
38
srcw = srcw || src.bitmap.width;
39
srch = srch || src.bitmap.height;
40
} else {
41
return throwError.call(
42
this,
43
'srcx, srcy, srcw, srch must be numbers',
44
cb
45
);
46
}
47
48
// round input
49
x = Math.round(x);
50
y = Math.round(y);
51
52
// round input
53
srcx = Math.round(srcx);
54
srcy = Math.round(srcy);
55
srcw = Math.round(srcw);
56
srch = Math.round(srch);
57
58
const maxWidth = this.bitmap.width;
59
const maxHeight = this.bitmap.height;
60
const baseImage = this;
61
62
src.scanQuiet(srcx, srcy, srcw, srch, function(sx, sy, idx) {
63
const xOffset = x + sx - srcx;
64
const yOffset = y + sy - srcy;
65
66
if (
67
xOffset >= 0 &&
68
yOffset >= 0 &&
69
maxWidth - xOffset > 0 &&
70
maxHeight - yOffset > 0
71
) {
72
const dstIdx = baseImage.getPixelIndex(xOffset, yOffset);
73
const src = {
74
r: this.bitmap.data[idx],
75
g: this.bitmap.data[idx + 1],
76
b: this.bitmap.data[idx + 2],
77
a: this.bitmap.data[idx + 3]
78
};
79
80
const dst = {
81
r: baseImage.bitmap.data[dstIdx],
82
g: baseImage.bitmap.data[dstIdx + 1],
83
b: baseImage.bitmap.data[dstIdx + 2],
84
a: baseImage.bitmap.data[dstIdx + 3]
85
};
86
87
baseImage.bitmap.data[dstIdx] =
88
((src.a * (src.r - dst.r) - dst.r + 255) >> 8) + dst.r;
89
baseImage.bitmap.data[dstIdx + 1] =
90
((src.a * (src.g - dst.g) - dst.g + 255) >> 8) + dst.g;
91
baseImage.bitmap.data[dstIdx + 2] =
92
((src.a * (src.b - dst.b) - dst.b + 255) >> 8) + dst.b;
93
baseImage.bitmap.data[dstIdx + 3] = this.constructor.limit255(
94
dst.a + src.a
95
);
96
}
97
});
98
99
if (isNodePattern(cb)) {
100
cb.call(this, null, this);
101
}
102
103
return this;
104
}
105
});
106
107