Skip to content

Commit

Permalink
test: add image resolver API test case for Wasm
Browse files Browse the repository at this point in the history
...and add pixels diff.
  • Loading branch information
yisibl committed Nov 16, 2022
1 parent c47c75d commit 018b02d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
22 changes: 15 additions & 7 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import { Resvg, renderAsync } from '../index'
import { jimpToRgbaPixels } from './helper'

test('Use href to load a JPG image without alpha', async (t) => {
const imgUrl = 'http://tva2.sinaimg.cn/crop.0.0.250.250.80/534b48acjw8ehw72edguyj206y06yq32.jpg'
const svg = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<image href="http://tva2.sinaimg.cn/crop.0.0.250.250.80/534b48acjw8ehw72edguyj206y06yq32.jpg" width="80" height="80"/>
<image href="${imgUrl}" width="80" height="80"/>
</svg>`
const opts = {
const resvg = new Resvg(svg, {
font: {
loadSystemFonts: false,
},
}
const resvg = new Resvg(svg, opts)
})
const resolved = await Promise.all(
resvg.imagesToResolve().map(async (url) => {
console.info('image url', url)
Expand All @@ -37,10 +37,18 @@ test('Use href to load a JPG image without alpha', async (t) => {
}
}
const pngData = resvg.render()
const pngBuffer = pngData.asPng()
const resvgPngBuffer = pngData.asPng()
const result1 = await jimp.read(resvgPngBuffer)

const result = await jimp.read(pngBuffer)
t.is(result.hasAlpha(), false)
const jimpImg = await fetch(imgUrl)
const jimpBuffer = await jimpImg.arrayBuffer()
const result2 = await jimp.read(jimpBuffer)

const r1 = new jimp({ data: result1.bitmap.data, width: pngData.width, height: pngData.height })
const r2 = new jimp({ data: result2.bitmap.data, width: result2.bitmap.width, height: result2.bitmap.height })

t.is(result1.hasAlpha(), false)
t.is(jimp.diff(r1, r2, 0.01).percent, 0) // 0 means similar, 1 means not similar
})

test('svg to RGBA pixels Array', async (t) => {
Expand Down
39 changes: 39 additions & 0 deletions __test__/wasm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { join } from 'path'

import test from 'ava'
import jimp from 'jimp-compact'
import fetch from 'node-fetch'

import { Resvg, initWasm } from '../wasm'

Expand All @@ -14,6 +15,44 @@ test.before(async () => {
await initWasm(fs.readFile(join(__dirname, '../wasm/index_bg.wasm')))
})

test('Use href to load a JPG image without alpha', async (t) => {
const imgUrl = 'http://tva2.sinaimg.cn/crop.0.0.250.250.80/534b48acjw8ehw72edguyj206y06yq32.jpg'
const svg = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<image href="${imgUrl}" width="80" height="80"/>
</svg>`
const resvg = new Resvg(svg)
const resolved = await Promise.all(
resvg.imagesToResolve().map(async (url) => {
console.info('image url', url)
const img = await fetch(url)
const buffer = await img.arrayBuffer()
return {
url,
buffer: Buffer.from(buffer),
}
}),
)
if (resolved.length > 0) {
for (const result of resolved) {
const { url, buffer } = result
resvg.resolveImage(url, buffer)
}
}
const pngData = resvg.render()
const resvgPngBuffer = Buffer.from(pngData.asPng())
const result1 = await jimp.read(resvgPngBuffer)

const jimpImg = await fetch(imgUrl)
const jimpBuffer = Buffer.from(await jimpImg.arrayBuffer())
const result2 = await jimp.read(jimpBuffer)

const r1 = new jimp({ data: result1.bitmap.data, width: pngData.width, height: pngData.height })
const r2 = new jimp({ data: result2.bitmap.data, width: result2.bitmap.width, height: result2.bitmap.height })

t.is(result1.hasAlpha(), false)
t.is(jimp.diff(r1, r2, 0.01).percent, 0) // 0 means similar, 1 means not similar
})

test('svg to RGBA pixels Array', async (t) => {
const svg = `<svg width="10px" height="5px" viewBox="0 0 10 5" version="1.1" xmlns="http://www.w3.org/2000/svg">
<rect fill="red" x="0" y="0" width="5" height="5"></rect>
Expand Down
Binary file modified example/url-out.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified wasm/index_bg.wasm
Binary file not shown.

0 comments on commit 018b02d

Please sign in to comment.