UNPKG

multimath

Version:

Core to create fast image math in WebAssembly and JS.

56 lines (43 loc) 1.73 kB
'use strict'; module.exports = function unsharp(img, width, height, amount, radius, threshold) { if (amount === 0 || radius < 0.5) { return; } if (radius > 2.0) { radius = 2.0; } var pixels = width * height; var img_bytes_cnt = pixels * 4; var hsl_bytes_cnt = pixels * 2; var blur_bytes_cnt = pixels * 2; var blur_line_byte_cnt = Math.max(width, height) * 4; // float32 array var blur_coeffs_byte_cnt = 8 * 4; // float32 array var img_offset = 0; var hsl_offset = img_bytes_cnt; var blur_offset = hsl_offset + hsl_bytes_cnt; var blur_tmp_offset = blur_offset + blur_bytes_cnt; var blur_line_offset = blur_tmp_offset + blur_bytes_cnt; var blur_coeffs_offset = blur_line_offset + blur_line_byte_cnt; var instance = this.__instance( 'unsharp_mask', img_bytes_cnt + hsl_bytes_cnt + blur_bytes_cnt * 2 + blur_line_byte_cnt + blur_coeffs_byte_cnt, { exp: Math.exp } ); // 32-bit copy is much faster in chrome var img32 = new Uint32Array(img.buffer); var mem32 = new Uint32Array(this.__memory.buffer); mem32.set(img32); // HSL var fn = instance.exports.hsl_l16 || instance.exports._hsl_l16; fn(img_offset, hsl_offset, width, height); // BLUR fn = instance.exports.blurMono16 || instance.exports._blurMono16; fn(hsl_offset, blur_offset, blur_tmp_offset, blur_line_offset, blur_coeffs_offset, width, height, radius); // UNSHARP fn = instance.exports.unsharp || instance.exports._unsharp; fn(img_offset, img_offset, hsl_offset, blur_offset, width, height, amount, threshold); // 32-bit copy is much faster in chrome img32.set(new Uint32Array(this.__memory.buffer, 0, pixels)); };