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