-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathavif.mjs
42 lines (42 loc) · 1.28 KB
/
avif.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const url=new URL('avif.wasm',import.meta.url);
let wasm;
const imports={
wbg: {
__wbg_log_aa11a1e72555df69: (p,n)=>{
console.log(new TextDecoder().decode(new Uint8Array(wasm.memory.buffer).subarray(p,p+n)));
},
__wbindgen_init_externref_table:function(){
const table=wasm.__wbindgen_export_0;
const offset=table.grow(4);
table.set(0);
table.set(offset);
table.set(offset+1,null);
table.set(offset+2,true);
table.set(offset+3,false);
}
}
};
const {instance}=await WebAssembly.instantiateStreaming(await fetch(url,{cache: 'force-cache'}),imports);
wasm=instance.exports;
const malloc=wasm.__wbindgen_malloc;
const free=wasm.__wbindgen_free;
/**
* Encodes the supplied ImageData rgba array.
* @param {Uint8Array} bytes
* @param {number} width
* @param {number} height
* @param {number} quality (1 to 100)
* @param {number} speed (1 to 10)
* @return {Uint8Array}
*/
const avif=(bytes,width,height,quality=50,speed=6)=>{
const n1=bytes.length;
const p1=malloc(n1,1);
new Uint8Array(wasm.memory.buffer).set(bytes,p1);
const [p2,n2]=wasm.avif_from_imagedata(p1,n1,width,height,quality,speed);
const res=new Uint8Array(wasm.memory.buffer).subarray(p2,p2+n2).slice();
free(p2,n2);
return res;
};
export {avif};
export default avif;