Path: blob/master/node_modules/@jimp/plugin-blit/test/blit.test.js
1126 views
import { Jimp, mkJGD, getTestDir } from '@jimp/test-utils';1import jpeg from '@jimp/jpeg';2import configure from '@jimp/custom';34import blit from '../src';56const jimp = configure({ types: [jpeg], plugins: [blit] }, Jimp);7const testDir = getTestDir(__dirname);89describe('Blit over image', function() {10this.timeout(15000);11const targetJGD = mkJGD(12'▴▴▴▴▸▸▸▸',13'▴▴▴▴▸▸▸▸',14'▴▴▴▴▸▸▸▸',15'▴▴▴▴▸▸▸▸',16'▾▾▾▾◆◆◆◆',17'▾▾▾▾◆◆◆◆',18'▾▾▾▾◆◆◆◆',19'▾▾▾▾◆◆◆◆'20);21const srcJGD = mkJGD(22'□□□□□□',23'□▥▥▥▥□',24'□▥■■▥□',25'□▥■■▥□',26'□▥▥▥▥□',27'□□□□□□'28);2930let targetImg;31let srcImg; // stores the Jimp instances of the JGD images above.3233before(done => {34const img1 = jimp.read(targetJGD);35const img2 = jimp.read(srcJGD);36Promise.all([img1, img2])37.then(images => {38targetImg = images[0];39srcImg = images[1];40done();41})42.catch(done);43});4445it('blit on top, with no crop', () => {46targetImg47.clone()48.blit(srcImg, 0, 0)49.getJGDSync()50.should.be.sameJGD(51mkJGD(52'□□□□□□▸▸',53'□▥▥▥▥□▸▸',54'□▥■■▥□▸▸',55'□▥■■▥□▸▸',56'□▥▥▥▥□◆◆',57'□□□□□□◆◆',58'▾▾▾▾◆◆◆◆',59'▾▾▾▾◆◆◆◆'60)61);62});6364it('blit on middle, with no crop', () => {65targetImg66.clone()67.blit(srcImg, 1, 1)68.getJGDSync()69.should.be.sameJGD(70mkJGD(71'▴▴▴▴▸▸▸▸',72'▴□□□□□□▸',73'▴□▥▥▥▥□▸',74'▴□▥■■▥□▸',75'▾□▥■■▥□◆',76'▾□▥▥▥▥□◆',77'▾□□□□□□◆',78'▾▾▾▾◆◆◆◆'79)80);81});8283it('blit on middle, with x,y crop', () => {84targetImg85.clone()86.blit(srcImg, 2, 2, 1, 1, 5, 5)87.getJGDSync()88.should.be.sameJGD(89mkJGD(90'▴▴▴▴▸▸▸▸',91'▴▴▴▴▸▸▸▸',92'▴▴▥▥▥▥□▸',93'▴▴▥■■▥□▸',94'▾▾▥■■▥□◆',95'▾▾▥▥▥▥□◆',96'▾▾□□□□□◆',97'▾▾▾▾◆◆◆◆'98)99);100});101102it('blit on middle, with x,y,w,h crop', () => {103targetImg104.clone()105.blit(srcImg, 2, 2, 1, 1, 4, 4)106.getJGDSync()107.should.be.sameJGD(108mkJGD(109'▴▴▴▴▸▸▸▸',110'▴▴▴▴▸▸▸▸',111'▴▴▥▥▥▥▸▸',112'▴▴▥■■▥▸▸',113'▾▾▥■■▥◆◆',114'▾▾▥▥▥▥◆◆',115'▾▾▾▾◆◆◆◆',116'▾▾▾▾◆◆◆◆'117)118);119});120121it('blit partially out, on top-left', () => {122targetImg123.clone()124.blit(srcImg, -1, -1)125.getJGDSync()126.should.be.sameJGD(127mkJGD(128'▥▥▥▥□▸▸▸',129'▥■■▥□▸▸▸',130'▥■■▥□▸▸▸',131'▥▥▥▥□▸▸▸',132'□□□□□◆◆◆',133'▾▾▾▾◆◆◆◆',134'▾▾▾▾◆◆◆◆',135'▾▾▾▾◆◆◆◆'136)137);138});139140it('blit partially out, on bottom-right', () => {141targetImg142.clone()143.blit(srcImg, 3, 3)144.getJGDSync()145.should.be.sameJGD(146mkJGD(147'▴▴▴▴▸▸▸▸',148'▴▴▴▴▸▸▸▸',149'▴▴▴▴▸▸▸▸',150'▴▴▴□□□□□',151'▾▾▾□▥▥▥▥',152'▾▾▾□▥■■▥',153'▾▾▾□▥■■▥',154'▾▾▾□▥▥▥▥'155)156);157});158159it('blit alpha', async () => {160const expectedImg = await Jimp.read(testDir + '/images/blit-alpha.png');161const dice = await Jimp.read(testDir + '/images/dice.png');162const image = await Jimp.read(testDir + '/images/cops.jpg');163164image165.blit(dice, 0, 0)166.bitmap.data.should.be.deepEqual(expectedImg.bitmap.data);167});168169async function createCat(catNum, len) {170let imgHeight = 60;171172const butt = await Jimp.read(testDir + '/images/cat_butt.png');173const head = await Jimp.read(testDir + '/images/cat_head.png');174const fuzz = await Jimp.read(testDir + '/images/cat_fuzz.png');175176let longCat = len;177longCat = longCat > 20 ? 20 : longCat;178longCat = longCat <= 1 ? 1 : longCat;179180const cat =181Math.floor(catNum * (head.bitmap.height / imgHeight)) * imgHeight;182183const newImage = await Jimp.create(184butt.bitmap.width + head.bitmap.width + fuzz.bitmap.width * longCat,185imgHeight,1860x00000000187);188189newImage.blit(butt, 0, 0, 0, cat, butt.bitmap.width, imgHeight);190for (let i = 0; i < longCat; i++) {191newImage.blit(192fuzz,193butt.bitmap.width + fuzz.bitmap.width * i,1940,1950,196cat,197fuzz.bitmap.width,198imgHeight199);200}201202newImage.blit(203head,204butt.bitmap.width + fuzz.bitmap.width * longCat,2050,2060,207cat,208head.bitmap.width,209imgHeight210);211212return newImage;213}214215it('uses src params correctly', async () => {216const expectedSmall = await Jimp.read(217testDir + '/images/cat-results/small-cat.png'218);219const small = await createCat(0.3, 1);220small.bitmap.data.should.be.deepEqual(expectedSmall.bitmap.data);221222const expectedMedium = await Jimp.read(223testDir + '/images/cat-results/medium-cat.png'224);225const medium = await createCat(0.6, 7);226medium.bitmap.data.should.be.deepEqual(expectedMedium.bitmap.data);227228const expectedLarge = await Jimp.read(229testDir + '/images/cat-results/large-cat.png'230);231const large = await createCat(0.9, 20);232large.bitmap.data.should.be.deepEqual(expectedLarge.bitmap.data);233});234});235236237