Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@jimp/plugin-blit/test/blit.test.js
1126 views
1
import { Jimp, mkJGD, getTestDir } from '@jimp/test-utils';
2
import jpeg from '@jimp/jpeg';
3
import configure from '@jimp/custom';
4
5
import blit from '../src';
6
7
const jimp = configure({ types: [jpeg], plugins: [blit] }, Jimp);
8
const testDir = getTestDir(__dirname);
9
10
describe('Blit over image', function() {
11
this.timeout(15000);
12
const targetJGD = mkJGD(
13
'▴▴▴▴▸▸▸▸',
14
'▴▴▴▴▸▸▸▸',
15
'▴▴▴▴▸▸▸▸',
16
'▴▴▴▴▸▸▸▸',
17
'▾▾▾▾◆◆◆◆',
18
'▾▾▾▾◆◆◆◆',
19
'▾▾▾▾◆◆◆◆',
20
'▾▾▾▾◆◆◆◆'
21
);
22
const srcJGD = mkJGD(
23
'□□□□□□',
24
'□▥▥▥▥□',
25
'□▥■■▥□',
26
'□▥■■▥□',
27
'□▥▥▥▥□',
28
'□□□□□□'
29
);
30
31
let targetImg;
32
let srcImg; // stores the Jimp instances of the JGD images above.
33
34
before(done => {
35
const img1 = jimp.read(targetJGD);
36
const img2 = jimp.read(srcJGD);
37
Promise.all([img1, img2])
38
.then(images => {
39
targetImg = images[0];
40
srcImg = images[1];
41
done();
42
})
43
.catch(done);
44
});
45
46
it('blit on top, with no crop', () => {
47
targetImg
48
.clone()
49
.blit(srcImg, 0, 0)
50
.getJGDSync()
51
.should.be.sameJGD(
52
mkJGD(
53
'□□□□□□▸▸',
54
'□▥▥▥▥□▸▸',
55
'□▥■■▥□▸▸',
56
'□▥■■▥□▸▸',
57
'□▥▥▥▥□◆◆',
58
'□□□□□□◆◆',
59
'▾▾▾▾◆◆◆◆',
60
'▾▾▾▾◆◆◆◆'
61
)
62
);
63
});
64
65
it('blit on middle, with no crop', () => {
66
targetImg
67
.clone()
68
.blit(srcImg, 1, 1)
69
.getJGDSync()
70
.should.be.sameJGD(
71
mkJGD(
72
'▴▴▴▴▸▸▸▸',
73
'▴□□□□□□▸',
74
'▴□▥▥▥▥□▸',
75
'▴□▥■■▥□▸',
76
'▾□▥■■▥□◆',
77
'▾□▥▥▥▥□◆',
78
'▾□□□□□□◆',
79
'▾▾▾▾◆◆◆◆'
80
)
81
);
82
});
83
84
it('blit on middle, with x,y crop', () => {
85
targetImg
86
.clone()
87
.blit(srcImg, 2, 2, 1, 1, 5, 5)
88
.getJGDSync()
89
.should.be.sameJGD(
90
mkJGD(
91
'▴▴▴▴▸▸▸▸',
92
'▴▴▴▴▸▸▸▸',
93
'▴▴▥▥▥▥□▸',
94
'▴▴▥■■▥□▸',
95
'▾▾▥■■▥□◆',
96
'▾▾▥▥▥▥□◆',
97
'▾▾□□□□□◆',
98
'▾▾▾▾◆◆◆◆'
99
)
100
);
101
});
102
103
it('blit on middle, with x,y,w,h crop', () => {
104
targetImg
105
.clone()
106
.blit(srcImg, 2, 2, 1, 1, 4, 4)
107
.getJGDSync()
108
.should.be.sameJGD(
109
mkJGD(
110
'▴▴▴▴▸▸▸▸',
111
'▴▴▴▴▸▸▸▸',
112
'▴▴▥▥▥▥▸▸',
113
'▴▴▥■■▥▸▸',
114
'▾▾▥■■▥◆◆',
115
'▾▾▥▥▥▥◆◆',
116
'▾▾▾▾◆◆◆◆',
117
'▾▾▾▾◆◆◆◆'
118
)
119
);
120
});
121
122
it('blit partially out, on top-left', () => {
123
targetImg
124
.clone()
125
.blit(srcImg, -1, -1)
126
.getJGDSync()
127
.should.be.sameJGD(
128
mkJGD(
129
'▥▥▥▥□▸▸▸',
130
'▥■■▥□▸▸▸',
131
'▥■■▥□▸▸▸',
132
'▥▥▥▥□▸▸▸',
133
'□□□□□◆◆◆',
134
'▾▾▾▾◆◆◆◆',
135
'▾▾▾▾◆◆◆◆',
136
'▾▾▾▾◆◆◆◆'
137
)
138
);
139
});
140
141
it('blit partially out, on bottom-right', () => {
142
targetImg
143
.clone()
144
.blit(srcImg, 3, 3)
145
.getJGDSync()
146
.should.be.sameJGD(
147
mkJGD(
148
'▴▴▴▴▸▸▸▸',
149
'▴▴▴▴▸▸▸▸',
150
'▴▴▴▴▸▸▸▸',
151
'▴▴▴□□□□□',
152
'▾▾▾□▥▥▥▥',
153
'▾▾▾□▥■■▥',
154
'▾▾▾□▥■■▥',
155
'▾▾▾□▥▥▥▥'
156
)
157
);
158
});
159
160
it('blit alpha', async () => {
161
const expectedImg = await Jimp.read(testDir + '/images/blit-alpha.png');
162
const dice = await Jimp.read(testDir + '/images/dice.png');
163
const image = await Jimp.read(testDir + '/images/cops.jpg');
164
165
image
166
.blit(dice, 0, 0)
167
.bitmap.data.should.be.deepEqual(expectedImg.bitmap.data);
168
});
169
170
async function createCat(catNum, len) {
171
let imgHeight = 60;
172
173
const butt = await Jimp.read(testDir + '/images/cat_butt.png');
174
const head = await Jimp.read(testDir + '/images/cat_head.png');
175
const fuzz = await Jimp.read(testDir + '/images/cat_fuzz.png');
176
177
let longCat = len;
178
longCat = longCat > 20 ? 20 : longCat;
179
longCat = longCat <= 1 ? 1 : longCat;
180
181
const cat =
182
Math.floor(catNum * (head.bitmap.height / imgHeight)) * imgHeight;
183
184
const newImage = await Jimp.create(
185
butt.bitmap.width + head.bitmap.width + fuzz.bitmap.width * longCat,
186
imgHeight,
187
0x00000000
188
);
189
190
newImage.blit(butt, 0, 0, 0, cat, butt.bitmap.width, imgHeight);
191
for (let i = 0; i < longCat; i++) {
192
newImage.blit(
193
fuzz,
194
butt.bitmap.width + fuzz.bitmap.width * i,
195
0,
196
0,
197
cat,
198
fuzz.bitmap.width,
199
imgHeight
200
);
201
}
202
203
newImage.blit(
204
head,
205
butt.bitmap.width + fuzz.bitmap.width * longCat,
206
0,
207
0,
208
cat,
209
head.bitmap.width,
210
imgHeight
211
);
212
213
return newImage;
214
}
215
216
it('uses src params correctly', async () => {
217
const expectedSmall = await Jimp.read(
218
testDir + '/images/cat-results/small-cat.png'
219
);
220
const small = await createCat(0.3, 1);
221
small.bitmap.data.should.be.deepEqual(expectedSmall.bitmap.data);
222
223
const expectedMedium = await Jimp.read(
224
testDir + '/images/cat-results/medium-cat.png'
225
);
226
const medium = await createCat(0.6, 7);
227
medium.bitmap.data.should.be.deepEqual(expectedMedium.bitmap.data);
228
229
const expectedLarge = await Jimp.read(
230
testDir + '/images/cat-results/large-cat.png'
231
);
232
const large = await createCat(0.9, 20);
233
large.bitmap.data.should.be.deepEqual(expectedLarge.bitmap.data);
234
});
235
});
236
237