-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7be7a38
commit 957a27b
Showing
15 changed files
with
186 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { getBuiltInImages, builtInImageNames, extractInfo, execute, compare } from '../../src'; | ||
import pMap from 'p-map'; | ||
|
||
export default describe('util/imageBuiltIn', () => { | ||
|
||
it('should get all builtIn images', async done => { | ||
const all = await getBuiltInImages() | ||
builtInImageNames.forEach(name => { | ||
expect(all.map(f => f.name)).toContain(name, 'does not contain image ' + name) | ||
}) | ||
done() | ||
}) | ||
|
||
it('should be equal and info should match', async done => { | ||
const all = await getBuiltInImages() | ||
const formats = { 'rose:': 'PPM', 'logo:': 'GIF', 'wizard:': 'GIF', 'granite:': 'GIF', 'netscape:': 'GIF' } | ||
await pMap(all, async img => { | ||
const info = await extractInfo(img) | ||
expect(info[0].image.format).toBe(formats[img.name]) | ||
const { outputFiles } = await execute({ commands: `convert ${img.name} 'output.png` }) | ||
expect(await compare(outputFiles[0], img)).toBe(true) | ||
}, { concurrency: 1 }) | ||
done() | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { buildInputFile, Call, compare, MagickInputFile, outputFileToInputFile } from '../../src'; | ||
|
||
export default describe('util/imageCompare', () => { | ||
|
||
async function test(img1: MagickInputFile | string, img2: MagickInputFile | string, expectedResult: boolean) { | ||
const result = await compare(img1, img2) | ||
expect(result).toBe(expectedResult, `Expected compareImage(${typeof img1 === 'string' ? img1 : img1.name} and ${typeof img2 === 'string' ? img2 : img2.name}) to return ${expectedResult} but returned ${result}`) | ||
} | ||
|
||
it('should return true if image is the same', async done => { | ||
await test(await buildInputFile('fn.png', 'img1.png'), await buildInputFile('fn.png', 'img2.png'), true) | ||
done() | ||
}) | ||
|
||
it('should return false if images are different', async done => { | ||
await test(await buildInputFile('fn.png', 'img1.png'), await buildInputFile('holocaust.jpg', 'img2.png'), false) | ||
done() | ||
}) | ||
|
||
it('should return true if images are equal but different formats', async done => { | ||
const result = await Call([await buildInputFile('fn.png')], ['convert', 'fn.png', 'fn.jpg']) | ||
const img1 = await outputFileToInputFile(result[0]) | ||
const img2 = await buildInputFile('fn.png', 'img2.png') | ||
await test(img1, img2, true) | ||
done() | ||
}) | ||
|
||
it('should let me work with builtin images', async done => { | ||
await test('rose:', await outputFileToInputFile((await Call([], ['convert', 'rose:', 'fn.png']))[0]), true) | ||
await test('rose:', await outputFileToInputFile((await Call([], ['convert', 'wizard:', 'fn.png']))[0]), false) | ||
done() | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import pMap from "p-map"; | ||
import { MagickInputFile, extractInfo , execute, asInputFile} from ".."; | ||
|
||
let builtInImages: MagickInputFile[] | ||
export const builtInImageNames = ['rose:', 'logo:', 'wizard:', 'granite:', 'netscape:'] | ||
|
||
export async function getBuiltInImages(): Promise<MagickInputFile[]> { | ||
if(!builtInImages){ | ||
builtInImages = await pMap(builtInImageNames, async name=>{ | ||
const info = await extractInfo(name) | ||
const {outputFiles} = await execute({commands:`convert ${name} ${`output1.${info[0].image.format.toLowerCase()}`}`} ) | ||
outputFiles[0].name = name | ||
return await asInputFile(outputFiles[0]) | ||
}) | ||
} | ||
return builtInImages | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { asInputFile, Call, MagickFile, blobToString, MagickInputFile } from '..' | ||
|
||
export async function compare(img1: MagickFile | string, img2: MagickFile | string, error: number = 0.01): Promise<boolean> { | ||
const identical = await compareNumber(img1, img2) | ||
return identical <= error | ||
} | ||
|
||
export async function compareNumber(img1: MagickFile | string, img2: MagickFile | string): Promise<number> { | ||
let name1: string, imgs: MagickInputFile[] = [], name2: string | ||
if (typeof img1 !== 'string') { | ||
const inputFile = await asInputFile(img1) | ||
imgs.push(inputFile) | ||
name1 = inputFile.name | ||
} | ||
else { | ||
name1 = img1 | ||
} | ||
if (typeof img2 !== 'string') { | ||
const inputFile = await asInputFile(img2) | ||
imgs.push(inputFile) | ||
name2 = inputFile.name | ||
} | ||
else { | ||
name2 = img2 | ||
} | ||
const result = await Call( | ||
imgs, | ||
['convert', name1, name2, '-resize', '256x256^!', '-metric', 'RMSE', '-format', '%[distortion]', '-compare', 'info:info.txt'], | ||
) | ||
const n = await blobToString(result[0].blob) | ||
return parseFloat(n) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export * from './file' | ||
export * from './cli' | ||
export * from './html' | ||
export * from './image' | ||
export * from './imageBuiltIn' | ||
export * from './imageCompare' | ||
export * from './imageExtractInfo' | ||
export * from './support' |