Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@jimp/plugin-crop/test/autocrop.test.js
1126 views
1
import { Jimp, mkJGD } from '@jimp/test-utils';
2
import configure from '@jimp/custom';
3
4
import crop from '../src';
5
6
const jimp = configure({ plugins: [crop] }, Jimp);
7
8
describe('Autocrop', () => {
9
it('image with transparent surround color', async () => {
10
const imgSrc = await jimp.read(
11
mkJGD(
12
' ',
13
' ◆◆ ',
14
' ◆▦▦◆ ',
15
' ◆▦▦▦▦◆ ',
16
' ◆▦▦◆ ',
17
' ◆◆ ',
18
' '
19
)
20
);
21
22
imgSrc
23
.autocrop()
24
.getJGDSync()
25
.should.be.sameJGD(
26
mkJGD(' ◆◆ ', ' ◆▦▦◆ ', '◆▦▦▦▦◆', ' ◆▦▦◆ ', ' ◆◆ ')
27
);
28
});
29
30
it('image with opaque surround color', async () => {
31
const imgSrc = await jimp.read(
32
mkJGD(
33
'▥▥▥▥▥▥▥▥▥▥',
34
'▥▥▥▥◆◆▥▥▥▥',
35
'▥▥▥◆▦▦◆▥▥▥',
36
'▥▥◆▦▦▦▦◆▥▥',
37
'▥▥▥◆▦▦◆▥▥▥',
38
'▥▥▥▥◆◆▥▥▥▥',
39
'▥▥▥▥▥▥▥▥▥▥'
40
)
41
);
42
43
imgSrc
44
.autocrop()
45
.getJGDSync()
46
.should.be.sameJGD(
47
mkJGD('▥▥◆◆▥▥', '▥◆▦▦◆▥', '◆▦▦▦▦◆', '▥◆▦▦◆▥', '▥▥◆◆▥▥')
48
);
49
});
50
51
it('image with one color border', async () => {
52
const imgSrc = await jimp.read(
53
mkJGD(
54
'▥▥▥▥▥▥▥▥▥▥▥▥',
55
'▥▥▥▥▥▥▥▥▥▥▥▥',
56
'▥▥ ◆◆ ▥▥',
57
'▥▥ ◆▦▦◆ ▥▥',
58
'▥▥ ◆▦▦▦▦◆ ▥▥',
59
'▥▥ ◆▦▦◆ ▥▥',
60
'▥▥ ◆◆ ▥▥',
61
'▥▥▥▥▥▥▥▥▥▥▥▥',
62
'▥▥▥▥▥▥▥▥▥▥▥▥'
63
)
64
);
65
66
imgSrc
67
.autocrop()
68
.getJGDSync()
69
.should.be.sameJGD(
70
mkJGD(' ◆◆ ', ' ◆▦▦◆ ', ' ◆▦▦▦▦◆ ', ' ◆▦▦◆ ', ' ◆◆ ')
71
);
72
});
73
74
it('image border with small variation', async () => {
75
const imgSrc = await jimp.read(
76
mkJGD(
77
'323232323232',
78
'232323232323',
79
'32 ◆◆ 32',
80
'23 ◆▦▦◆ 23',
81
'32 ◆▦▦▦▦◆ 32',
82
'23 ◆▦▦◆ 23',
83
'32 ◆◆ 32',
84
'232323232323',
85
'323232323232'
86
)
87
);
88
imgSrc
89
.clone()
90
.autocrop()
91
.getJGDSync()
92
.should.be.sameJGD(
93
mkJGD(
94
'323232323232',
95
'232323232323',
96
'32 ◆◆ 32',
97
'23 ◆▦▦◆ 23',
98
'32 ◆▦▦▦▦◆ 32',
99
'23 ◆▦▦◆ 23',
100
'32 ◆◆ 32',
101
'232323232323',
102
'323232323232'
103
)
104
);
105
imgSrc
106
.clone()
107
.autocrop(0.005)
108
.getJGDSync()
109
.should.be.sameJGD(
110
mkJGD(' ◆◆ ', ' ◆▦▦◆ ', ' ◆▦▦▦▦◆ ', ' ◆▦▦◆ ', ' ◆◆ ')
111
);
112
});
113
114
it('image border with small variation configured by options', async () => {
115
const imgSrc = await Jimp.read(
116
mkJGD(
117
'323232323232',
118
'232323232323',
119
'32 ◆◆ 32',
120
'23 ◆▦▦◆ 23',
121
'32 ◆▦▦▦▦◆ 32',
122
'23 ◆▦▦◆ 23',
123
'32 ◆◆ 32',
124
'232323232323',
125
'323232323232'
126
)
127
);
128
imgSrc
129
.clone()
130
.autocrop()
131
.getJGDSync()
132
.should.be.sameJGD(
133
mkJGD(
134
'323232323232',
135
'232323232323',
136
'32 ◆◆ 32',
137
'23 ◆▦▦◆ 23',
138
'32 ◆▦▦▦▦◆ 32',
139
'23 ◆▦▦◆ 23',
140
'32 ◆◆ 32',
141
'232323232323',
142
'323232323232'
143
)
144
);
145
imgSrc
146
.clone()
147
.autocrop({ tolerance: 0.005 })
148
.getJGDSync()
149
.should.be.sameJGD(
150
mkJGD(' ◆◆ ', ' ◆▦▦◆ ', ' ◆▦▦▦▦◆ ', ' ◆▦▦◆ ', ' ◆◆ ')
151
);
152
});
153
154
it('image without frame', async () => {
155
const imgSrc = await Jimp.read(
156
mkJGD(
157
'▥▥ ◆◆ ',
158
'▥▥ ◆▦▦◆ ',
159
'▥▥ ◆▦▦▦▦◆ ',
160
'▥▥ ◆▦▦◆ ',
161
'▥▥ ◆◆ ',
162
'▥▥▥▥▥▥▥▥▥▥',
163
'▥▥▥▥▥▥▥▥▥▥'
164
)
165
);
166
167
imgSrc
168
.autocrop(false)
169
.getJGDSync()
170
.should.be.sameJGD(
171
mkJGD(' ◆◆ ', ' ◆▦▦◆ ', ' ◆▦▦▦▦◆ ', ' ◆▦▦◆ ', ' ◆◆ ')
172
);
173
});
174
175
it('image without frame configured by options', async () => {
176
const imgSrc = await Jimp.read(
177
mkJGD(
178
'▥▥ ◆◆ ',
179
'▥▥ ◆▦▦◆ ',
180
'▥▥ ◆▦▦▦▦◆ ',
181
'▥▥ ◆▦▦◆ ',
182
'▥▥ ◆◆ ',
183
'▥▥▥▥▥▥▥▥▥▥',
184
'▥▥▥▥▥▥▥▥▥▥'
185
)
186
);
187
188
imgSrc
189
.autocrop({ cropOnlyFrames: false })
190
.getJGDSync()
191
.should.be.sameJGD(
192
mkJGD(' ◆◆ ', ' ◆▦▦◆ ', ' ◆▦▦▦▦◆ ', ' ◆▦▦◆ ', ' ◆◆ ')
193
);
194
});
195
196
it('image with symmetric border configured by options', async () => {
197
const imgSrc = await Jimp.read(
198
mkJGD(
199
'▥▥▥▥▥▥▥▥▥▥▥▥▥▥',
200
'▥▥ ◆◆ ▥▥▥▥',
201
'▥▥ ◆▦▦◆ ▥▥▥▥',
202
'▥▥ ◆▦▦▦▦◆ ▥▥▥▥',
203
'▥▥ ◆▦▦◆ ▥▥▥▥',
204
'▥▥ ◆◆ ▥▥▥▥',
205
'▥▥▥▥▥▥▥▥▥▥▥▥▥▥',
206
'▥▥▥▥▥▥▥▥▥▥▥▥▥▥'
207
)
208
);
209
210
imgSrc
211
.autocrop({ cropSymmetric: true })
212
.getJGDSync()
213
.should.be.sameJGD(
214
mkJGD(
215
' ◆◆ ▥▥',
216
' ◆▦▦◆ ▥▥',
217
' ◆▦▦▦▦◆ ▥▥',
218
' ◆▦▦◆ ▥▥',
219
' ◆◆ ▥▥',
220
'▥▥▥▥▥▥▥▥▥▥'
221
)
222
);
223
});
224
225
it('image without frame and with symmetric border configured by options', async () => {
226
const imgSrc = await Jimp.read(
227
mkJGD(
228
'▥▥ ◆◆ ▥▥▥▥',
229
'▥▥ ◆▦▦◆ ▥▥▥▥',
230
'▥▥ ◆▦▦▦▦◆ ▥▥▥▥',
231
'▥▥ ◆▦▦◆ ▥▥▥▥',
232
'▥▥ ◆◆ ▥▥▥▥',
233
'▥▥▥▥▥▥▥▥▥▥▥▥▥▥',
234
'▥▥▥▥▥▥▥▥▥▥▥▥▥▥'
235
)
236
);
237
imgSrc
238
.autocrop({ cropSymmetric: true, cropOnlyFrames: false })
239
.getJGDSync()
240
.should.be.sameJGD(
241
mkJGD(
242
' ◆◆ ▥▥',
243
' ◆▦▦◆ ▥▥',
244
' ◆▦▦▦▦◆ ▥▥',
245
' ◆▦▦◆ ▥▥',
246
' ◆◆ ▥▥',
247
'▥▥▥▥▥▥▥▥▥▥',
248
'▥▥▥▥▥▥▥▥▥▥'
249
)
250
);
251
});
252
253
it('image without frame and with some border left', async () => {
254
const imgSrc = await Jimp.read(
255
mkJGD(
256
'323232323232',
257
'232323232323',
258
'32 ◆◆ 32',
259
'23 ◆▦▦◆ 23',
260
'32 ◆▦▦▦▦◆ 32',
261
'23 ◆▦▦◆ 23',
262
'32 ◆◆ 32',
263
'232323232323',
264
'323232323232'
265
)
266
);
267
268
imgSrc
269
.autocrop({
270
tolerance: 0.005,
271
leaveBorder: 1
272
})
273
.getJGDSync()
274
.should.be.sameJGD(
275
mkJGD(
276
'3232323232',
277
'2 ◆◆ 3',
278
'3 ◆▦▦◆ 2',
279
'2 ◆▦▦▦▦◆ 3',
280
'3 ◆▦▦◆ 2',
281
'2 ◆◆ 3',
282
'3232323232'
283
)
284
);
285
});
286
287
it('image not cropped given an out of bounds "leaveBorder" value ', async () => {
288
const imgSrc = await Jimp.read(
289
mkJGD(
290
'323232323232',
291
'232323232323',
292
'32 ◆◆ 32',
293
'23 ◆▦▦◆ 23',
294
'32 ◆▦▦▦▦◆ 32',
295
'23 ◆▦▦◆ 23',
296
'32 ◆◆ 32',
297
'232323232323',
298
'323232323232'
299
)
300
);
301
302
imgSrc
303
.autocrop({
304
tolerance: 0.005,
305
leaveBorder: 100
306
})
307
.getJGDSync()
308
.should.be.sameJGD(
309
mkJGD(
310
'323232323232',
311
'232323232323',
312
'32 ◆◆ 32',
313
'23 ◆▦▦◆ 23',
314
'32 ◆▦▦▦▦◆ 32',
315
'23 ◆▦▦◆ 23',
316
'32 ◆◆ 32',
317
'232323232323',
318
'323232323232'
319
)
320
);
321
});
322
323
it('image with top and bottom frame and leaveBorder', async () => {
324
const imgSrc = await Jimp.read(
325
mkJGD(
326
'▥▥▥▥▥▥▥▥',
327
'▥▥▥▥▥▥▥▥',
328
'▥▥▥▥▥▥▥▥',
329
' ◆◆ ',
330
' ◆▦▦◆ ',
331
' ◆▦▦▦▦◆ ',
332
' ◆▦▦◆ ',
333
' ◆◆ ',
334
'▥▥▥▥▥▥▥▥',
335
'▥▥▥▥▥▥▥▥',
336
'▥▥▥▥▥▥▥▥'
337
)
338
);
339
imgSrc
340
.autocrop({ cropSymmetric: true, cropOnlyFrames: false, leaveBorder: 2 })
341
.getJGDSync()
342
.should.be.sameJGD(
343
mkJGD(
344
'▥▥▥▥▥▥▥▥',
345
'▥▥▥▥▥▥▥▥',
346
' ◆◆ ',
347
' ◆▦▦◆ ',
348
' ◆▦▦▦▦◆ ',
349
' ◆▦▦◆ ',
350
' ◆◆ ',
351
'▥▥▥▥▥▥▥▥',
352
'▥▥▥▥▥▥▥▥'
353
)
354
);
355
});
356
357
it('ignore sides north', async () => {
358
const imgSrc = await jimp.read(
359
mkJGD(
360
' ',
361
' ◆◆ ',
362
' ◆▦▦◆ ',
363
' ◆▦▦▦▦◆ ',
364
' ◆▦▦◆ ',
365
' ◆◆ ',
366
' '
367
)
368
);
369
370
imgSrc
371
.autocrop({ cropOnlyFrames: false, ignoreSides: { north: true } })
372
.getJGDSync()
373
.should.be.sameJGD(
374
mkJGD(' ', ' ◆◆ ', ' ◆▦▦◆ ', '◆▦▦▦▦◆', ' ◆▦▦◆ ', ' ◆◆ ')
375
);
376
});
377
378
it('ignore sides south and west', async () => {
379
const imgSrc = await jimp.read(
380
mkJGD(
381
' ',
382
' ◆◆ ',
383
' ◆▦▦◆ ',
384
' ◆▦▦▦▦◆ ',
385
' ◆▦▦◆ ',
386
' ◆◆ ',
387
' '
388
)
389
);
390
391
imgSrc
392
.autocrop({
393
cropOnlyFrames: false,
394
ignoreSides: { west: true, south: true }
395
})
396
.getJGDSync()
397
.should.be.sameJGD(
398
mkJGD(
399
' ◆◆ ',
400
' ◆▦▦◆ ',
401
'◆▦▦▦▦◆ ',
402
' ◆▦▦◆ ',
403
' ◆◆ ',
404
' '
405
)
406
);
407
});
408
409
it('ignore sides east', async () => {
410
const imgSrc = await jimp.read(
411
mkJGD(
412
' ',
413
' ◆◆ ',
414
' ◆▦▦◆ ',
415
' ◆▦▦▦▦◆ ',
416
' ◆▦▦◆ ',
417
' ◆◆ ',
418
' '
419
)
420
);
421
422
imgSrc
423
.autocrop({ cropOnlyFrames: false, ignoreSides: { east: true } })
424
.getJGDSync()
425
.should.be.sameJGD(
426
mkJGD(' ◆◆ ', ' ◆▦▦◆ ', ' ◆▦▦▦▦◆', ' ◆▦▦◆ ', ' ◆◆ ')
427
);
428
});
429
});
430
431