wasm-webp 0.0.1-beta.1

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.
Binary file
@@ -0,0 +1,70 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ // @ts-ignore
11
+ import Module from './webp-wasm';
12
+ // default webp config
13
+ const defaultWebpConfig = {
14
+ lossless: 0,
15
+ quality: 100,
16
+ };
17
+ export const encoderVersion = () => __awaiter(void 0, void 0, void 0, function* () {
18
+ const module = yield Module();
19
+ return module.encoder_version();
20
+ });
21
+ export const encodeRGB = (rgb, width, height, quality) => __awaiter(void 0, void 0, void 0, function* () {
22
+ const module = yield Module();
23
+ quality = typeof quality !== 'number' ? 100 : Math.min(100, Math.max(0, quality));
24
+ return module.encodeRGB(rgb, width, height, quality);
25
+ });
26
+ export const encodeRGBA = (rgba, width, height, quality) => __awaiter(void 0, void 0, void 0, function* () {
27
+ const module = yield Module();
28
+ quality = typeof quality !== 'number' ? 100 : Math.min(100, Math.max(0, quality));
29
+ return module.encodeRGBA(rgba, width, height, quality);
30
+ });
31
+ export const encode = (data, width, height, hasAlpha, config) => __awaiter(void 0, void 0, void 0, function* () {
32
+ const module = yield Module();
33
+ const webpConfig = Object.assign(Object.assign({}, defaultWebpConfig), config);
34
+ webpConfig.lossless = Math.min(1, Math.max(0, webpConfig.lossless));
35
+ webpConfig.quality = Math.min(100, Math.max(0, webpConfig.quality));
36
+ return module.encode(data, width, height, hasAlpha, webpConfig);
37
+ });
38
+ export const encodeAnimation = (width, height, hasAlpha, frames) => __awaiter(void 0, void 0, void 0, function* () {
39
+ const module = yield Module();
40
+ const durations = [];
41
+ const dataLength = frames.reduce((acc, frame) => {
42
+ acc += frame.data.length;
43
+ return acc;
44
+ }, 0);
45
+ const data = new Uint8Array(dataLength);
46
+ let offset = 0;
47
+ frames.forEach(frame => {
48
+ data.set(frame.data, offset);
49
+ offset += frame.data.length;
50
+ durations.push(frame.duration);
51
+ });
52
+ return module.encodeAnimation(width, height, hasAlpha, durations, data);
53
+ });
54
+ export const decoderVersion = () => __awaiter(void 0, void 0, void 0, function* () {
55
+ const module = yield Module();
56
+ return module.decoder_version();
57
+ });
58
+ export const decodeRGB = (data) => __awaiter(void 0, void 0, void 0, function* () {
59
+ const module = yield Module();
60
+ return module.decodeRGB(data);
61
+ });
62
+ export const decodeRGBA = (data) => __awaiter(void 0, void 0, void 0, function* () {
63
+ const module = yield Module();
64
+ return module.decodeRGBA(data);
65
+ });
66
+ // TODO:
67
+ // export const decode = async (data: Uint8Array, hasAlpha: boolean) => {
68
+ // const module = await Module()
69
+ // return module.decode(data, hasAlpha)
70
+ // }