import { describe, it } from 'node:test' import assert from 'node:assert' import { MultiMath, unsharp_mask_plugin } from '../lib/index.mjs' import hsl_l16 from '../lib/unsharp_mask_plugin/hsl_l16.mjs' import { blurMono16 } from 'glur' function fill (target, arr) { if (!Array.isArray(arr)) arr = [arr] for (let i = 0; i < target.length; i++) target[i] = arr[i % arr.length] } describe('unsharp_mask', function () { describe('hsl_l16', function () { it('js', function () { const sample = new Uint8Array(100 * 100 * 4) fill(sample, [255, 180, 55, 0]) // r, g, b, a const result = hsl_l16(sample, 100, 100) for (let i = 0; i < result.length; i++) { assert.equal(result[i], 39835) } }) it('wasm', function () { const mlib_wasm = new MultiMath({ js: false }).use(unsharp_mask_plugin) const width = 100, height = 100 const sample = new Uint8Array(width * height * 4) fill(sample, [255, 180, 55, 0]) // r, g, b, a const result_js = hsl_l16(sample, width, height) // Invoke wasm implementation manually const pixels = width * height const res_offset = pixels * 4 const instance = mlib_wasm.__instance( 'unsharp_mask', pixels * 4 + pixels * 2, { exp: Math.exp } ) const mem = new Uint8Array(mlib_wasm.__memory.buffer) mem.set(sample) const fn = instance.exports.hsl_l16 || instance.exports._hsl_l16 fn(0, res_offset, width, height) const result_wasm = new Uint16Array(mlib_wasm.__memory.buffer.slice(res_offset, res_offset + pixels * 2)) assert.deepEqual(result_wasm, result_js) }) }) describe('glur_mono16', function () { it('js', function () { const sample = new Uint16Array(100 * 100) fill(sample, 33333) const orig = sample.slice(0, sample.length) blurMono16(sample, 100, 100, 2) assert.deepEqual(sample, orig) }) it('wasm', function () { const mlib_wasm = new MultiMath({ js: false }).use(unsharp_mask_plugin) // unsharp_mask_plugin wasm module does not provide API for direct glur16 call // Here is simple wrapper for testing function glur16_wasm_invoke (thisobj, src, width, height, radius) { // src = grayscale, 16 bits const pixels = width * height const src_byte_cnt = pixels * 2 const out_byte_cnt = pixels * 2 const tmp_byte_cnt = pixels * 2 const line_byte_cnt = Math.max(width, height) * 4 // float32 array const coeffs_byte_cnt = 8 * 4 const src_offset = 0 const out_offset = src_offset + src_byte_cnt const tmp_offset = out_offset + out_byte_cnt const line_offset = tmp_offset + tmp_byte_cnt const coeffs_offset = line_offset + line_byte_cnt const instance = thisobj.__instance( 'unsharp_mask', coeffs_offset + coeffs_byte_cnt, { exp: Math.exp } ) const mem32 = new Uint16Array(thisobj.__memory.buffer) mem32.set(src) const fn = instance.exports.blurMono16 || instance.exports._blurMono16 fn(src_offset, out_offset, tmp_offset, line_offset, coeffs_offset, width, height, radius) return new Uint16Array(thisobj.__memory.buffer.slice(out_offset, out_offset + out_byte_cnt)) } const sample = new Uint16Array(100 * 100) fill(sample, [0, 255]) const sample_js = sample.slice(0, sample.length) blurMono16(sample_js, 100, 100, 2) const sample_wasm = glur16_wasm_invoke(mlib_wasm, sample, 100, 100, 2) assert.deepEqual(sample_js, sample_wasm) }) }) describe('unsharp_mask', function () { function createSample (width, height) { const result = new Uint8Array(width * height * 4) for (let i = 0; i < result.length; i++) result[i] = 20 + i return result } it('js should not throw without wasm', function () { const mlib = new MultiMath({ wasm: false }).use(unsharp_mask_plugin) const sample = createSample(100, 100) mlib.unsharp_mask(sample, 100, 100, 80, 2, 2) }) it('wasm', function () { const mlib_js = new MultiMath({ wasm: false }).use(unsharp_mask_plugin) const mlib_wasm = new MultiMath({ js: false }).use(unsharp_mask_plugin) const sample_js = createSample(100, 100) const sample_wasm = createSample(100, 100) mlib_js.unsharp_mask(sample_js, 100, 100, 80, 2, 2) mlib_wasm.unsharp_mask(sample_wasm, 100, 100, 80, 2, 2) assert.deepEqual(sample_js, sample_wasm) }) }) })