wasm-webp 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -8
- package/dist/esm/index.js +5 -1
- package/dist/esm/webp-wasm.js +4 -0
- package/dist/esm/webp-wasm.wasm +0 -0
- package/dist/index.d.ts +28 -18
- package/dist/types.d.ts +5 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
# webp.wasm
|
|
2
2
|
|
|
3
|
-
webp.wasm is a pure Webassembly / Javascript port of libwebp.
|
|
3
|
+
webp.wasm is a pure Webassembly / Javascript port of libwebp. The library supports encoding animated WebP.
|
|
4
4
|
|
|
5
5
|

|
|
6
|
+

|
|
6
7
|
|
|
7
8
|
## APIs
|
|
8
9
|
|
|
@@ -72,7 +73,7 @@ const webpData = await encodeRGBA(imgData.data, canvas.width, canvas.height)
|
|
|
72
73
|
...
|
|
73
74
|
```
|
|
74
75
|
|
|
75
|
-
#### encode
|
|
76
|
+
#### encode
|
|
76
77
|
|
|
77
78
|
A more advanced API is based on the WebPConfig. <b>Only the lossless and quality parameters are supported now !!!</b>. You can generate low-quality webp with this function.
|
|
78
79
|
|
|
@@ -174,7 +175,7 @@ fr.onload = () => {
|
|
|
174
175
|
canvas.style.height = `${result.height}px`
|
|
175
176
|
canvas.width = result.width
|
|
176
177
|
canvas.height = result.height
|
|
177
|
-
ctx.putImageData(result, 0, 0)
|
|
178
|
+
ctx.putImageData(new ImageData(new Uint8ClampedArray(result.data)), 0, 0)
|
|
178
179
|
}
|
|
179
180
|
// read webp file
|
|
180
181
|
fr.readAsArrayBuffer(file)
|
|
@@ -206,28 +207,73 @@ fr.readAsArrayBuffer(file)
|
|
|
206
207
|
...
|
|
207
208
|
```
|
|
208
209
|
|
|
210
|
+
#### decodeAnimation
|
|
211
|
+
|
|
212
|
+
Decoding animated WebP image. Returns an array of frames.
|
|
213
|
+
|
|
214
|
+
`function decodeAnimation(data: Uint8Array, hasAlpha: boolean): Promise<Nullable<DecodedWebPAnimationFrame[]>>`
|
|
215
|
+
|
|
216
|
+
##### Example
|
|
217
|
+
|
|
218
|
+
```javascript
|
|
219
|
+
...
|
|
220
|
+
const fr = new FileReader()
|
|
221
|
+
fr.onload = () => {
|
|
222
|
+
if (!fr.result) {
|
|
223
|
+
return
|
|
224
|
+
}
|
|
225
|
+
webpData = fr.result as Uint8Array
|
|
226
|
+
const result = await decodeRGBA(webpData)
|
|
227
|
+
// draw imageData
|
|
228
|
+
...
|
|
229
|
+
}
|
|
230
|
+
// webp file
|
|
231
|
+
fr.readAsArrayBuffer(file)
|
|
232
|
+
...
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
#### DecodedWebPAnimationFrame
|
|
236
|
+
|
|
237
|
+
The object have the following properties:
|
|
238
|
+
|
|
239
|
+
- DecodedWebPAnimationFrame.width: `number`
|
|
240
|
+
|
|
241
|
+
The frame image width.
|
|
242
|
+
|
|
243
|
+
- DecodedWebPAnimationFrame.height: `number`
|
|
244
|
+
|
|
245
|
+
The frame image height.
|
|
246
|
+
|
|
247
|
+
- DecodedWebPAnimationFrame.duration: `number`
|
|
248
|
+
|
|
249
|
+
The frame display duration.
|
|
250
|
+
|
|
251
|
+
- DecodedWebPAnimationFrame.data: `Uint8Array`
|
|
252
|
+
|
|
253
|
+
Raw data in pixels.
|
|
254
|
+
|
|
209
255
|
#### WebPDecodedImageData
|
|
210
256
|
|
|
211
257
|
The object have the following properties:
|
|
212
258
|
|
|
213
|
-
- WebPDecodedImageData.width: number
|
|
259
|
+
- WebPDecodedImageData.width: `number`
|
|
214
260
|
|
|
215
261
|
The image width in pixels.
|
|
216
262
|
|
|
217
|
-
- WebPDecodedImageData.height: number
|
|
263
|
+
- WebPDecodedImageData.height: `number`
|
|
218
264
|
|
|
219
265
|
The image height in pixels.
|
|
220
266
|
|
|
221
|
-
- WebPDecodedImageData.data: Uint8Array
|
|
267
|
+
- WebPDecodedImageData.data: `Uint8Array`
|
|
222
268
|
|
|
223
269
|
Raw data in pixels.
|
|
224
270
|
|
|
225
|
-
> Note: It
|
|
271
|
+
> Note: It has same properties as browser `ImageData` object, but it is not. There is actually no `ImageData` in node.
|
|
226
272
|
|
|
227
273
|
## Playing Examples
|
|
228
274
|
|
|
229
275
|
```shell
|
|
230
|
-
npm run dev
|
|
276
|
+
npm run build-wasm:dev && npm run dev
|
|
231
277
|
```
|
|
232
278
|
|
|
233
279
|
## Building
|
package/dist/esm/index.js
CHANGED
|
@@ -44,7 +44,7 @@ export const encodeAnimation = (width, height, hasAlpha, frames) => __awaiter(vo
|
|
|
44
44
|
}, 0);
|
|
45
45
|
const data = new Uint8Array(dataLength);
|
|
46
46
|
let offset = 0;
|
|
47
|
-
frames.forEach(frame => {
|
|
47
|
+
frames.forEach((frame) => {
|
|
48
48
|
data.set(frame.data, offset);
|
|
49
49
|
offset += frame.data.length;
|
|
50
50
|
durations.push(frame.duration);
|
|
@@ -68,3 +68,7 @@ export const decodeRGBA = (data) => __awaiter(void 0, void 0, void 0, function*
|
|
|
68
68
|
// const module = await Module()
|
|
69
69
|
// return module.decode(data, hasAlpha)
|
|
70
70
|
// }
|
|
71
|
+
export const decodeAnimation = (data, hasAlpha) => __awaiter(void 0, void 0, void 0, function* () {
|
|
72
|
+
const module = yield Module();
|
|
73
|
+
return module.decodeAnimation(data, hasAlpha);
|
|
74
|
+
});
|
package/dist/esm/webp-wasm.js
CHANGED
|
@@ -2572,6 +2572,8 @@ function dbg(...args) {
|
|
|
2572
2572
|
return Emval.toHandle(handle[key]);
|
|
2573
2573
|
};
|
|
2574
2574
|
|
|
2575
|
+
var __emval_new_array = () => Emval.toHandle([]);
|
|
2576
|
+
|
|
2575
2577
|
|
|
2576
2578
|
var __emval_new_cstring = (v) => Emval.toHandle(getStringOrSymbol(v));
|
|
2577
2579
|
|
|
@@ -2802,6 +2804,8 @@ var wasmImports = {
|
|
|
2802
2804
|
/** @export */
|
|
2803
2805
|
_emval_get_property: __emval_get_property,
|
|
2804
2806
|
/** @export */
|
|
2807
|
+
_emval_new_array: __emval_new_array,
|
|
2808
|
+
/** @export */
|
|
2805
2809
|
_emval_new_cstring: __emval_new_cstring,
|
|
2806
2810
|
/** @export */
|
|
2807
2811
|
_emval_new_object: __emval_new_object,
|
package/dist/esm/webp-wasm.wasm
CHANGED
|
Binary file
|
package/dist/index.d.ts
CHANGED
|
@@ -1,30 +1,40 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
WebPConfig,
|
|
3
|
+
Nullable,
|
|
4
|
+
WebPAnimationFrame,
|
|
5
|
+
WebPDecodedImageData,
|
|
6
|
+
DecodedWebPAnimationFrame,
|
|
7
|
+
} from './types'
|
|
2
8
|
export declare const encoderVersion: () => Promise<string>
|
|
3
9
|
export declare const encodeRGB: (
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
10
|
+
rgb: Uint8Array,
|
|
11
|
+
width: number,
|
|
12
|
+
height: number,
|
|
13
|
+
quality?: number
|
|
8
14
|
) => Promise<Nullable<Uint8Array>>
|
|
9
15
|
export declare const encodeRGBA: (
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
16
|
+
rgba: Uint8Array,
|
|
17
|
+
width: number,
|
|
18
|
+
height: number,
|
|
19
|
+
quality?: number
|
|
14
20
|
) => Promise<Nullable<Uint8Array>>
|
|
15
21
|
export declare const encode: (
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
22
|
+
data: Uint8Array,
|
|
23
|
+
width: number,
|
|
24
|
+
height: number,
|
|
25
|
+
hasAlpha: boolean,
|
|
26
|
+
config: Partial<WebPConfig>
|
|
21
27
|
) => Promise<Nullable<Uint8Array>>
|
|
22
28
|
export declare const encodeAnimation: (
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
29
|
+
width: number,
|
|
30
|
+
height: number,
|
|
31
|
+
hasAlpha: boolean,
|
|
32
|
+
frames: WebPAnimationFrame[]
|
|
27
33
|
) => Promise<Nullable<Uint8Array>>
|
|
28
34
|
export declare const decoderVersion: () => Promise<string>
|
|
29
35
|
export declare const decodeRGB: (data: Uint8Array) => Promise<Nullable<WebPDecodedImageData>>
|
|
30
36
|
export declare const decodeRGBA: (data: Uint8Array) => Promise<Nullable<WebPDecodedImageData>>
|
|
37
|
+
export declare const decodeAnimation: (
|
|
38
|
+
data: Uint8Array,
|
|
39
|
+
hasAlpha: boolean
|
|
40
|
+
) => Promise<Nullable<DecodedWebPAnimationFrame[]>>
|
package/dist/types.d.ts
CHANGED
|
@@ -8,6 +8,11 @@ export interface WebPAnimationFrame {
|
|
|
8
8
|
duration: number
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
export interface DecodedWebPAnimationFrame extends WebPAnimationFrame {
|
|
12
|
+
width: number
|
|
13
|
+
height: number
|
|
14
|
+
}
|
|
15
|
+
|
|
11
16
|
export interface WebPDecodedImageData {
|
|
12
17
|
width: number
|
|
13
18
|
height: number
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wasm-webp",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
"build-wasm:dev": "bash ./build.sh -dev",
|
|
29
29
|
"build-wasm:esm": "bash ./build.sh -es6",
|
|
30
30
|
"build-wasm:cjs": "bash ./build.sh",
|
|
31
|
+
"build": "npm run build:esm",
|
|
31
32
|
"build:esm": "npm run build-wasm:esm && tsc -m ES6 --outDir ./dist/esm && cp -r src/*.d.ts dist",
|
|
32
33
|
"build:cjs": "npm run build-wasm:cjs && tsc -m commonjs --outDir ./dist/cjs && cp -r src/*.d.ts dist",
|
|
33
34
|
"test": "node --experimental-wasm-modules --experimental-vm-modules ./node_modules/jest/bin/jest.js",
|