Skip to content

Commit 2046318

Browse files
committed
Dependencies and js wrapper update.
1 parent f8f111d commit 2046318

12 files changed

+137
-57
lines changed

Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "avif"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
edition = "2021"
55

66
[lib]
@@ -12,8 +12,10 @@ avif-serialize = "0.8"
1212
rav1e = { version = "0.6.6", default-features = false, features = ["wasm"] }
1313

1414
[profile.release]
15+
debug = false
1516
opt-level = "s"
1617
lto = true
18+
#panic = "abort"
1719

1820
[dev-dependencies]
1921
jpeg-decoder = "0.3"

avif.d.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* tslint:disable */
2+
/* eslint-disable */
3+
declare module 'avif' {
4+
/**
5+
* Encodes the supplied ImageData rgba array.
6+
* @param {Uint8Array} bytes
7+
* @param {number} width
8+
* @param {number} height
9+
* @param {number} [quality=50] (1 to 100)
10+
* @param {number} [speed=6] (1 to 10)
11+
* @return {Promise<Uint8Array>}
12+
*/
13+
export function avif(bytes: Uint8Array, width: number, height: number, quality: number, speed: number): Uint8Array;
14+
export default avif;
15+
}

avif.mjs

+31-55
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,39 @@
1-
const url=new URL('avif.wasm',import.meta.url);
2-
await (await fetch(url)).arrayBuffer();
3-
const src=()=>`(async()=>{
4-
const mod=await WebAssembly.compileStreaming(await fetch('${url}',{cache:'force-cache'}));
5-
const imports={
6-
wbg:{
7-
__wbg_log_d415f1170b035824: (p,n)=>{
8-
console.log(
9-
new TextDecoder().decode(new Uint8Array(wasm.memory.buffer).subarray(p,p+n))
10-
)
11-
}
1+
const url=new URL('./avif.wasm',import.meta.url);
2+
const imports={
3+
wbg:{
4+
__wbg_log_d415f1170b035824: (p,n)=>{
5+
console.log(
6+
new TextDecoder().decode(new Uint8Array(wasm.memory.buffer).subarray(p,p+n))
7+
)
128
}
13-
};
14-
const wasm=(await WebAssembly.instantiate(mod,imports)).exports;
15-
const malloc=wasm.__wbindgen_malloc;
16-
const free=wasm.__wbindgen_free;
17-
const pointer=wasm.__wbindgen_add_to_stack_pointer;
18-
const fn=({data,width,height,quality,speed})=>{
19-
const n1=data.length;
20-
const p1=malloc(n1,1);
21-
const r=pointer(-16);
22-
try{
23-
new Uint8Array(wasm.memory.buffer).set(data,p1);
24-
wasm.avif_from_imagedata(r,p1,n1,width,height,quality,speed);
25-
const arr=new Int32Array(wasm.memory.buffer);
26-
const p2=arr[r/4];const n2=arr[r/4+1];
27-
const res=new Uint8Array(wasm.memory.buffer).subarray(p2,p2+n2).slice();
28-
free(p2,n2);
29-
return res;
30-
}finally{pointer(16)}
31-
};
32-
onmessage=async msg=>postMessage(fn(msg.data));
33-
postMessage('ready');
34-
})();`
35-
const worker=await new Promise(r=>{
36-
const worker=new Worker(URL.createObjectURL(new Blob([src()],{type:'application/javascript'})),{type:'module'});
37-
worker.onmessage=msg=>{
38-
if(msg.data==='ready'){
39-
worker.onmessage=null;
40-
r(worker);
41-
}
42-
};
43-
});
9+
}
10+
};
11+
const {instance:{exports:wasm}}=await WebAssembly.instantiateStreaming(await fetch(url,{cache:'force-cache'}),imports);
12+
const malloc=wasm.__wbindgen_malloc;
13+
const free=wasm.__wbindgen_free;
14+
const pointer=wasm.__wbindgen_add_to_stack_pointer;
4415
/**
4516
* Encodes the supplied ImageData rgba array.
46-
* @param {Uint8Array} data
17+
* @param {Uint8Array} bytes
4718
* @param {number} width
4819
* @param {number} height
49-
* @param {number} quality
50-
* @param {number} speed
20+
* @param {number} quality (1 to 100)
21+
* @param {number} speed (1 to 10)
5122
* @return {Promise<Uint8Array>}
5223
*/
53-
const avif=(data,width,height, quality=50,speed=6)=>new Promise(r=>{
54-
worker.onmessage=msg=>{
55-
worker.onmessage=null;
56-
r(msg.data);
57-
}
58-
worker.postMessage({data,width,height,quality,speed});
59-
});
60-
61-
export {
62-
avif
24+
const avif=(bytes,width,height, quality=50,speed=6)=>{
25+
const n1=bytes.length;
26+
const p1=malloc(n1,1);
27+
const r=pointer(-16);
28+
try{
29+
new Uint8Array(wasm.memory.buffer).set(bytes,p1);
30+
wasm.avif_from_imagedata(r,p1,n1,width,height,quality,speed);
31+
const arr=new Int32Array(wasm.memory.buffer);
32+
const p2=arr[r/4];const n2=arr[r/4+1];
33+
const res=new Uint8Array(wasm.memory.buffer).subarray(p2,p2+n2).slice();
34+
free(p2,n2);
35+
return res;
36+
}finally{pointer(16)}
6337
};
38+
export {avif};
39+
export default avif;

avif.mjs.br

-756 Bytes
Binary file not shown.

avif.wasm

-1.76 KB
Binary file not shown.

avif.wasm.br

467 Bytes
Binary file not shown.

avif_for_importScripts.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const avif=(async()=>{
2+
const imports={
3+
wbg:{
4+
__wbg_log_d415f1170b035824: (p,n)=>{
5+
console.log(
6+
new TextDecoder().decode(new Uint8Array(wasm.memory.buffer).subarray(p,p+n))
7+
)
8+
}
9+
}
10+
};
11+
const {instance:{exports:wasm}}=await WebAssembly.instantiateStreaming(await fetch('./avif.wasm',{cache:'force-cache'}),imports);
12+
const malloc=wasm.__wbindgen_malloc;
13+
const free=wasm.__wbindgen_free;
14+
const pointer=wasm.__wbindgen_add_to_stack_pointer;
15+
return (it,width,height,quality=50,speed=6)=>{
16+
const n1=it.length;
17+
const p1=malloc(n1,1);
18+
const r=pointer(-16);
19+
try{
20+
new Uint8Array(wasm.memory.buffer).set(data,p1);
21+
wasm.avif_from_imagedata(r,p1,n1,width,height,quality,speed);
22+
const arr=new Int32Array(wasm.memory.buffer);
23+
const p2=arr[r/4];const n2=arr[r/4+1];
24+
const res=new Uint8Array(wasm.memory.buffer).subarray(p2,p2+n2).slice();
25+
free(p2,n2);
26+
return res;
27+
}finally{pointer(16)}
28+
};
29+
})();

avif_worker.d.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* tslint:disable */
2+
/* eslint-disable */
3+
declare module 'avif_worker' {
4+
/**
5+
* Encodes the supplied ImageData rgba array.
6+
* @param {Uint8Array} bytes
7+
* @param {number} width
8+
* @param {number} height
9+
* @param {number} [quality=50] (1 to 100)
10+
* @param {number} [speed=6] (1 to 10)
11+
* @return {Promise<Uint8Array>}
12+
*/
13+
export function avif(bytes: Uint8Array, width: number, height: number, quality: number, speed: number): Promise<Uint8Array>;
14+
export default avif;
15+
}

avif_worker.mjs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const url=new URL('avif.wasm',import.meta.url);
2+
await (await fetch(url)).arrayBuffer();
3+
const worker=await new Promise(r=>{
4+
// For browsers that don't support type: module on workers (firefox < 114, safari < 15)
5+
// const worker=new Worker(new URL('./avif_worker_script.js',import.meta.url));
6+
const worker=new Worker(new URL('./avif_worker_script.mjs',import.meta.url),{type:'module'});
7+
worker.onmessage=msg=>{
8+
if(msg.data==='ready'){
9+
worker.onmessage=null;
10+
r(worker);
11+
}
12+
};
13+
});
14+
/**
15+
* Encodes the supplied ImageData rgba array.
16+
* @param {Uint8Array} bytes
17+
* @param {number} width
18+
* @param {number} height
19+
* @param {number} quality (1 to 100)
20+
* @param {number} speed (1 to 10)
21+
* @return {Promise<Uint8Array>}
22+
*/
23+
const avif=(bytes,width,height, quality=50,speed=6)=>new Promise(r=>{
24+
worker.onmessage=msg=>{
25+
worker.onmessage=null;
26+
r(msg.data);
27+
}
28+
worker.postMessage({bytes,width,height,quality,speed});
29+
});
30+
31+
export {avif};
32+
export default avif;

avif_worker_script.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
importScripts('./avif_for_importScripts.js');
2+
(async()=>{
3+
const fn=await avif;
4+
onmessage=async msg=>{
5+
postMessage(fn(msg.data.bytes,msg.data.width,msg.data.height,msg.data.quality,msg.data.speed));
6+
}
7+
postMessage('ready');
8+
})();

avif_worker_script.mjs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import {avif} from "./avif.mjs";
2+
onmessage=async({data:{bytes,width,height,quality,speed}})=>postMessage(avif(bytes,width,height,quality,speed));
3+
postMessage('ready');

test.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
const context=canvas.getContext('2d');
2323
context.drawImage(image,0,0);
2424
const data=context.getImageData(0,0,canvas.width,canvas.height);
25-
const {avif}=await import('./avif.mjs');
25+
const {avif}=await import('./avif_worker.mjs');
2626
const rgba=new Uint8Array(data.data);
2727
const quality=50.0;
2828
for (const speed of [10,9,8,7,6,5,4,3,2,1]){

0 commit comments

Comments
 (0)