123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- 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);
- });
- });
|