Path: blob/master/node_modules/@jimp/plugin-crop/test/autocrop.test.js
1126 views
import { Jimp, mkJGD } from '@jimp/test-utils';1import configure from '@jimp/custom';23import crop from '../src';45const jimp = configure({ plugins: [crop] }, Jimp);67describe('Autocrop', () => {8it('image with transparent surround color', async () => {9const imgSrc = await jimp.read(10mkJGD(11' ',12' ◆◆ ',13' ◆▦▦◆ ',14' ◆▦▦▦▦◆ ',15' ◆▦▦◆ ',16' ◆◆ ',17' '18)19);2021imgSrc22.autocrop()23.getJGDSync()24.should.be.sameJGD(25mkJGD(' ◆◆ ', ' ◆▦▦◆ ', '◆▦▦▦▦◆', ' ◆▦▦◆ ', ' ◆◆ ')26);27});2829it('image with opaque surround color', async () => {30const imgSrc = await jimp.read(31mkJGD(32'▥▥▥▥▥▥▥▥▥▥',33'▥▥▥▥◆◆▥▥▥▥',34'▥▥▥◆▦▦◆▥▥▥',35'▥▥◆▦▦▦▦◆▥▥',36'▥▥▥◆▦▦◆▥▥▥',37'▥▥▥▥◆◆▥▥▥▥',38'▥▥▥▥▥▥▥▥▥▥'39)40);4142imgSrc43.autocrop()44.getJGDSync()45.should.be.sameJGD(46mkJGD('▥▥◆◆▥▥', '▥◆▦▦◆▥', '◆▦▦▦▦◆', '▥◆▦▦◆▥', '▥▥◆◆▥▥')47);48});4950it('image with one color border', async () => {51const imgSrc = await jimp.read(52mkJGD(53'▥▥▥▥▥▥▥▥▥▥▥▥',54'▥▥▥▥▥▥▥▥▥▥▥▥',55'▥▥ ◆◆ ▥▥',56'▥▥ ◆▦▦◆ ▥▥',57'▥▥ ◆▦▦▦▦◆ ▥▥',58'▥▥ ◆▦▦◆ ▥▥',59'▥▥ ◆◆ ▥▥',60'▥▥▥▥▥▥▥▥▥▥▥▥',61'▥▥▥▥▥▥▥▥▥▥▥▥'62)63);6465imgSrc66.autocrop()67.getJGDSync()68.should.be.sameJGD(69mkJGD(' ◆◆ ', ' ◆▦▦◆ ', ' ◆▦▦▦▦◆ ', ' ◆▦▦◆ ', ' ◆◆ ')70);71});7273it('image border with small variation', async () => {74const imgSrc = await jimp.read(75mkJGD(76'323232323232',77'232323232323',78'32 ◆◆ 32',79'23 ◆▦▦◆ 23',80'32 ◆▦▦▦▦◆ 32',81'23 ◆▦▦◆ 23',82'32 ◆◆ 32',83'232323232323',84'323232323232'85)86);87imgSrc88.clone()89.autocrop()90.getJGDSync()91.should.be.sameJGD(92mkJGD(93'323232323232',94'232323232323',95'32 ◆◆ 32',96'23 ◆▦▦◆ 23',97'32 ◆▦▦▦▦◆ 32',98'23 ◆▦▦◆ 23',99'32 ◆◆ 32',100'232323232323',101'323232323232'102)103);104imgSrc105.clone()106.autocrop(0.005)107.getJGDSync()108.should.be.sameJGD(109mkJGD(' ◆◆ ', ' ◆▦▦◆ ', ' ◆▦▦▦▦◆ ', ' ◆▦▦◆ ', ' ◆◆ ')110);111});112113it('image border with small variation configured by options', async () => {114const imgSrc = await Jimp.read(115mkJGD(116'323232323232',117'232323232323',118'32 ◆◆ 32',119'23 ◆▦▦◆ 23',120'32 ◆▦▦▦▦◆ 32',121'23 ◆▦▦◆ 23',122'32 ◆◆ 32',123'232323232323',124'323232323232'125)126);127imgSrc128.clone()129.autocrop()130.getJGDSync()131.should.be.sameJGD(132mkJGD(133'323232323232',134'232323232323',135'32 ◆◆ 32',136'23 ◆▦▦◆ 23',137'32 ◆▦▦▦▦◆ 32',138'23 ◆▦▦◆ 23',139'32 ◆◆ 32',140'232323232323',141'323232323232'142)143);144imgSrc145.clone()146.autocrop({ tolerance: 0.005 })147.getJGDSync()148.should.be.sameJGD(149mkJGD(' ◆◆ ', ' ◆▦▦◆ ', ' ◆▦▦▦▦◆ ', ' ◆▦▦◆ ', ' ◆◆ ')150);151});152153it('image without frame', async () => {154const imgSrc = await Jimp.read(155mkJGD(156'▥▥ ◆◆ ',157'▥▥ ◆▦▦◆ ',158'▥▥ ◆▦▦▦▦◆ ',159'▥▥ ◆▦▦◆ ',160'▥▥ ◆◆ ',161'▥▥▥▥▥▥▥▥▥▥',162'▥▥▥▥▥▥▥▥▥▥'163)164);165166imgSrc167.autocrop(false)168.getJGDSync()169.should.be.sameJGD(170mkJGD(' ◆◆ ', ' ◆▦▦◆ ', ' ◆▦▦▦▦◆ ', ' ◆▦▦◆ ', ' ◆◆ ')171);172});173174it('image without frame configured by options', async () => {175const imgSrc = await Jimp.read(176mkJGD(177'▥▥ ◆◆ ',178'▥▥ ◆▦▦◆ ',179'▥▥ ◆▦▦▦▦◆ ',180'▥▥ ◆▦▦◆ ',181'▥▥ ◆◆ ',182'▥▥▥▥▥▥▥▥▥▥',183'▥▥▥▥▥▥▥▥▥▥'184)185);186187imgSrc188.autocrop({ cropOnlyFrames: false })189.getJGDSync()190.should.be.sameJGD(191mkJGD(' ◆◆ ', ' ◆▦▦◆ ', ' ◆▦▦▦▦◆ ', ' ◆▦▦◆ ', ' ◆◆ ')192);193});194195it('image with symmetric border configured by options', async () => {196const imgSrc = await Jimp.read(197mkJGD(198'▥▥▥▥▥▥▥▥▥▥▥▥▥▥',199'▥▥ ◆◆ ▥▥▥▥',200'▥▥ ◆▦▦◆ ▥▥▥▥',201'▥▥ ◆▦▦▦▦◆ ▥▥▥▥',202'▥▥ ◆▦▦◆ ▥▥▥▥',203'▥▥ ◆◆ ▥▥▥▥',204'▥▥▥▥▥▥▥▥▥▥▥▥▥▥',205'▥▥▥▥▥▥▥▥▥▥▥▥▥▥'206)207);208209imgSrc210.autocrop({ cropSymmetric: true })211.getJGDSync()212.should.be.sameJGD(213mkJGD(214' ◆◆ ▥▥',215' ◆▦▦◆ ▥▥',216' ◆▦▦▦▦◆ ▥▥',217' ◆▦▦◆ ▥▥',218' ◆◆ ▥▥',219'▥▥▥▥▥▥▥▥▥▥'220)221);222});223224it('image without frame and with symmetric border configured by options', async () => {225const imgSrc = await Jimp.read(226mkJGD(227'▥▥ ◆◆ ▥▥▥▥',228'▥▥ ◆▦▦◆ ▥▥▥▥',229'▥▥ ◆▦▦▦▦◆ ▥▥▥▥',230'▥▥ ◆▦▦◆ ▥▥▥▥',231'▥▥ ◆◆ ▥▥▥▥',232'▥▥▥▥▥▥▥▥▥▥▥▥▥▥',233'▥▥▥▥▥▥▥▥▥▥▥▥▥▥'234)235);236imgSrc237.autocrop({ cropSymmetric: true, cropOnlyFrames: false })238.getJGDSync()239.should.be.sameJGD(240mkJGD(241' ◆◆ ▥▥',242' ◆▦▦◆ ▥▥',243' ◆▦▦▦▦◆ ▥▥',244' ◆▦▦◆ ▥▥',245' ◆◆ ▥▥',246'▥▥▥▥▥▥▥▥▥▥',247'▥▥▥▥▥▥▥▥▥▥'248)249);250});251252it('image without frame and with some border left', async () => {253const imgSrc = await Jimp.read(254mkJGD(255'323232323232',256'232323232323',257'32 ◆◆ 32',258'23 ◆▦▦◆ 23',259'32 ◆▦▦▦▦◆ 32',260'23 ◆▦▦◆ 23',261'32 ◆◆ 32',262'232323232323',263'323232323232'264)265);266267imgSrc268.autocrop({269tolerance: 0.005,270leaveBorder: 1271})272.getJGDSync()273.should.be.sameJGD(274mkJGD(275'3232323232',276'2 ◆◆ 3',277'3 ◆▦▦◆ 2',278'2 ◆▦▦▦▦◆ 3',279'3 ◆▦▦◆ 2',280'2 ◆◆ 3',281'3232323232'282)283);284});285286it('image not cropped given an out of bounds "leaveBorder" value ', async () => {287const imgSrc = await Jimp.read(288mkJGD(289'323232323232',290'232323232323',291'32 ◆◆ 32',292'23 ◆▦▦◆ 23',293'32 ◆▦▦▦▦◆ 32',294'23 ◆▦▦◆ 23',295'32 ◆◆ 32',296'232323232323',297'323232323232'298)299);300301imgSrc302.autocrop({303tolerance: 0.005,304leaveBorder: 100305})306.getJGDSync()307.should.be.sameJGD(308mkJGD(309'323232323232',310'232323232323',311'32 ◆◆ 32',312'23 ◆▦▦◆ 23',313'32 ◆▦▦▦▦◆ 32',314'23 ◆▦▦◆ 23',315'32 ◆◆ 32',316'232323232323',317'323232323232'318)319);320});321322it('image with top and bottom frame and leaveBorder', async () => {323const imgSrc = await Jimp.read(324mkJGD(325'▥▥▥▥▥▥▥▥',326'▥▥▥▥▥▥▥▥',327'▥▥▥▥▥▥▥▥',328' ◆◆ ',329' ◆▦▦◆ ',330' ◆▦▦▦▦◆ ',331' ◆▦▦◆ ',332' ◆◆ ',333'▥▥▥▥▥▥▥▥',334'▥▥▥▥▥▥▥▥',335'▥▥▥▥▥▥▥▥'336)337);338imgSrc339.autocrop({ cropSymmetric: true, cropOnlyFrames: false, leaveBorder: 2 })340.getJGDSync()341.should.be.sameJGD(342mkJGD(343'▥▥▥▥▥▥▥▥',344'▥▥▥▥▥▥▥▥',345' ◆◆ ',346' ◆▦▦◆ ',347' ◆▦▦▦▦◆ ',348' ◆▦▦◆ ',349' ◆◆ ',350'▥▥▥▥▥▥▥▥',351'▥▥▥▥▥▥▥▥'352)353);354});355356it('ignore sides north', async () => {357const imgSrc = await jimp.read(358mkJGD(359' ',360' ◆◆ ',361' ◆▦▦◆ ',362' ◆▦▦▦▦◆ ',363' ◆▦▦◆ ',364' ◆◆ ',365' '366)367);368369imgSrc370.autocrop({ cropOnlyFrames: false, ignoreSides: { north: true } })371.getJGDSync()372.should.be.sameJGD(373mkJGD(' ', ' ◆◆ ', ' ◆▦▦◆ ', '◆▦▦▦▦◆', ' ◆▦▦◆ ', ' ◆◆ ')374);375});376377it('ignore sides south and west', async () => {378const imgSrc = await jimp.read(379mkJGD(380' ',381' ◆◆ ',382' ◆▦▦◆ ',383' ◆▦▦▦▦◆ ',384' ◆▦▦◆ ',385' ◆◆ ',386' '387)388);389390imgSrc391.autocrop({392cropOnlyFrames: false,393ignoreSides: { west: true, south: true }394})395.getJGDSync()396.should.be.sameJGD(397mkJGD(398' ◆◆ ',399' ◆▦▦◆ ',400'◆▦▦▦▦◆ ',401' ◆▦▦◆ ',402' ◆◆ ',403' '404)405);406});407408it('ignore sides east', async () => {409const imgSrc = await jimp.read(410mkJGD(411' ',412' ◆◆ ',413' ◆▦▦◆ ',414' ◆▦▦▦▦◆ ',415' ◆▦▦◆ ',416' ◆◆ ',417' '418)419);420421imgSrc422.autocrop({ cropOnlyFrames: false, ignoreSides: { east: true } })423.getJGDSync()424.should.be.sameJGD(425mkJGD(' ◆◆ ', ' ◆▦▦◆ ', ' ◆▦▦▦▦◆', ' ◆▦▦◆ ', ' ◆◆ ')426);427});428});429430431