wasm-image-optimization 1.4.6 → 2.0.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.
- package/LICENSE +5 -5
- package/README.md +152 -97
- package/dist/child-workers.d.ts +6 -0
- package/dist/child-workers.js +20 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +47 -0
- package/dist/core.d.ts +76 -0
- package/dist/core.js +174 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +7 -0
- package/dist/single.d.ts +14 -0
- package/dist/single.js +28 -0
- package/dist/wasm-image-optimization-single.js +0 -0
- package/dist/wasm-image-optimization.js +2 -0
- package/dist/wasm-image-optimization.wasm +0 -0
- package/dist/web-workers.js +2958 -0
- package/dist/workerd.d.ts +19 -0
- package/dist/workerd.js +48 -0
- package/dist/workers.d.ts +7 -0
- package/dist/workers.js +50 -0
- package/package.json +64 -91
- package/dist/cjs/dummy/index.d.ts +0 -9
- package/dist/cjs/dummy/index.js +0 -22
- package/dist/cjs/dummy/index.js.map +0 -1
- package/dist/cjs/lib/optimizeImage.d.ts +0 -14
- package/dist/cjs/lib/optimizeImage.js +0 -24
- package/dist/cjs/lib/optimizeImage.js.map +0 -1
- package/dist/cjs/libImage.js +0 -2
- package/dist/cjs/libImage.wasm +0 -0
- package/dist/cjs/node/_node-worker.d.ts +0 -12
- package/dist/cjs/node/_node-worker.js +0 -9
- package/dist/cjs/node/_node-worker.js.map +0 -1
- package/dist/cjs/node/cjs-worker.d.ts +0 -12
- package/dist/cjs/node/cjs-worker.js +0 -23
- package/dist/cjs/node/cjs-worker.js.map +0 -1
- package/dist/cjs/node/index.d.ts +0 -10
- package/dist/cjs/node/index.js +0 -20
- package/dist/cjs/node/index.js.map +0 -1
- package/dist/cjs/node/node-worker.js +0 -4276
- package/dist/cjs/types/index.d.ts +0 -16
- package/dist/cjs/types/index.js +0 -3
- package/dist/cjs/types/index.js.map +0 -1
- package/dist/cjs/vite-plugin/index.d.ts +0 -3
- package/dist/cjs/vite-plugin/index.js +0 -19
- package/dist/cjs/vite-plugin/index.js.map +0 -1
- package/dist/dummy/index.d.ts +0 -9
- package/dist/dummy/index.js +0 -12
- package/dist/dummy/index.js.map +0 -1
- package/dist/esm/index.d.ts +0 -10
- package/dist/esm/index.js +0 -6
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/libImage.js +0 -0
- package/dist/esm/package.json +0 -3
- package/dist/esm/web-worker.js +0 -4021
- package/dist/lib/optimizeImage.d.ts +0 -14
- package/dist/lib/optimizeImage.js +0 -19
- package/dist/lib/optimizeImage.js.map +0 -1
- package/dist/types/index.d.ts +0 -16
- package/dist/types/index.js +0 -2
- package/dist/types/index.js.map +0 -1
- package/dist/vite-plugin/index.d.ts +0 -3
- package/dist/vite-plugin/index.js +0 -17
- package/dist/vite-plugin/index.js.map +0 -1
- package/dist/web-worker/_web-worker.d.ts +0 -14
- package/dist/web-worker/_web-worker.js +0 -9
- package/dist/web-worker/_web-worker.js.map +0 -1
- package/dist/web-worker/index.d.ts +0 -12
- package/dist/web-worker/index.js +0 -6
- package/dist/web-worker/index.js.map +0 -1
- package/dist/workers/index.d.ts +0 -10
- package/dist/workers/index.js +0 -11
- package/dist/workers/index.js.map +0 -1
package/LICENSE
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
MIT License
|
|
1
|
+
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
3
|
+
Copyright (c) 2026 SoraKumo
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
of this software and associated documentation files (the \"Software\"), to deal
|
|
7
7
|
in the Software without restriction, including without limitation the rights
|
|
8
8
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
9
|
copies of the Software, and to permit persons to whom the Software is
|
|
@@ -12,10 +12,10 @@ furnished to do so, subject to the following conditions:
|
|
|
12
12
|
The above copyright notice and this permission notice shall be included in all
|
|
13
13
|
copies or substantial portions of the Software.
|
|
14
14
|
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
16
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
17
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,112 +1,167 @@
|
|
|
1
1
|
# wasm-image-optimization
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
- Frontend
|
|
6
|
-
- Next.js (Multithreading support)
|
|
7
|
-
- React Router (Multithreading support)
|
|
8
|
-
|
|
9
|
-
- Backend
|
|
10
|
-
- Cloudflare Workers
|
|
11
|
-
- Deno Deploy
|
|
12
|
-
- Node.js (Multithreading support)
|
|
13
|
-
|
|
14
|
-
- Convert
|
|
15
|
-
- source format
|
|
16
|
-
- gif
|
|
17
|
-
- svg
|
|
18
|
-
- jpeg (EXIF orientation is supported)
|
|
19
|
-
- png
|
|
20
|
-
- webp
|
|
21
|
-
- avif
|
|
22
|
-
- output format
|
|
23
|
-
- jpeg
|
|
24
|
-
- png
|
|
25
|
-
- webp
|
|
26
|
-
- thumbhash
|
|
27
|
-
- raw (Uncompressed (width\*height\*4))
|
|
28
|
-
- avif (default)
|
|
29
|
-
|
|
30
|
-
## Example
|
|
31
|
-
|
|
32
|
-
https://next-image-convert.vercel.app/
|
|
33
|
-

|
|
34
|
-
|
|
35
|
-
## Functions
|
|
36
|
-
|
|
37
|
-
- Convert image
|
|
38
|
-
|
|
39
|
-
```ts
|
|
40
|
-
optimizeImage({
|
|
41
|
-
image: BufferSource | Buffer | string; // The input image data
|
|
42
|
-
width?: number; // The desired output width (optional)
|
|
43
|
-
height?: number; // The desired output height (optional)
|
|
44
|
-
quality?: number; // The desired output quality (0-100, optional)
|
|
45
|
-
format?: "jpeg" | "png" | "webp" | "avif" | "thumbhash" | "raw" | "none"; // The desired output format (optional)
|
|
46
|
-
speed?: number; // The desired speed (0-10, Slow-Fast, Optional)
|
|
47
|
-
filter?: boolean; // resize filter (default true)
|
|
48
|
-
):
|
|
49
|
-
Promise<
|
|
50
|
-
ArrayBuffer>
|
|
51
|
-
|
|
52
|
-
optimizeImageExt({
|
|
53
|
-
image: BufferSource | Buffer | string; // The input image data
|
|
54
|
-
width?: number; // The desired output width (optional)
|
|
55
|
-
height?: number; // The desired output height (optional)
|
|
56
|
-
quality?: number; // The desired output quality (0-100, optional)
|
|
57
|
-
format?: "jpeg" | "png" | "webp" | "avif" | "thumbhash" | "raw" | "none"; // The desired output format (optional)
|
|
58
|
-
speed?: number; // The desired speed (0-10, Slow-Fast, Optional)
|
|
59
|
-
filter?: boolean; // resize filter (default true)
|
|
60
|
-
):
|
|
61
|
-
Promise<{
|
|
62
|
-
data: Uint8Array,
|
|
63
|
-
originalWidth: number,
|
|
64
|
-
originalHeight: number,
|
|
65
|
-
width: number,
|
|
66
|
-
height: number}>
|
|
3
|
+
High-performance image conversion and resizing tool leveraging WebAssembly (Emscripten).
|
|
4
|
+
Built on the Skia rendering engine, it supports animations (GIF/WebP) and modern formats (AVIF/SVG/ThumbHash).
|
|
67
5
|
|
|
6
|
+
It provides a lightweight, dependency-free solution for generating high-quality visuals and documents across **Node.js**, **Cloudflare Workers**, **Deno**, and **Web Browsers**.
|
|
7
|
+
|
|
8
|
+
## Playground
|
|
9
|
+
|
|
10
|
+
https://node-libraries.github.io/wasm-image-optimization/
|
|
11
|
+
|
|
12
|
+
## Examples
|
|
13
|
+
|
|
14
|
+
https://github.com/SoraKumo001/wasm-image-optimization-samples
|
|
15
|
+
|
|
16
|
+
## Supported Formats
|
|
17
|
+
|
|
18
|
+
| Format | Input (Decoding) | Output (Encoding) | Notes |
|
|
19
|
+
| :------------ | :--------------: | :---------------: | :------------------------------------------ |
|
|
20
|
+
| **PNG** | ✅ | ✅ | |
|
|
21
|
+
| **JPEG** | ✅ | ✅ | |
|
|
22
|
+
| **WebP** | ✅ | ✅ | Supports animations |
|
|
23
|
+
| **AVIF** | ✅ | ✅ | Powered by AOM encoder / dav1d decoder |
|
|
24
|
+
| **ThumbHash** | ✅ | ✅ | Returns a compact image placeholder hash |
|
|
25
|
+
| **RAW** | ✅ | ✅ | Returns uncompressed 32-bit RGBA pixel data |
|
|
26
|
+
| **GIF** | ✅ | - | Input only (supports animations) |
|
|
27
|
+
| **SVG** | ✅ | - | High-fidelity rendering via SkSVGDOM |
|
|
28
|
+
| **BMP** | ✅ | - | |
|
|
29
|
+
| **None** | ✅ | ✅ | Returns original data with metadata |
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pnpm add wasm-image-optimization
|
|
68
35
|
```
|
|
69
36
|
|
|
70
|
-
##
|
|
37
|
+
## CLI Usage
|
|
38
|
+
|
|
39
|
+
After installation, the `wasm-image-optimization` command will be available.
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
# Basic conversion (default is WebP)
|
|
43
|
+
wasm-image-optimization input.png
|
|
71
44
|
|
|
72
|
-
|
|
45
|
+
# Specify format and quality
|
|
46
|
+
wasm-image-optimization input.jpg -f avif -q 80
|
|
73
47
|
|
|
74
|
-
|
|
48
|
+
# Convert maintaining animation
|
|
49
|
+
wasm-image-optimization input.gif -f webp -a
|
|
75
50
|
|
|
76
|
-
|
|
77
|
-
|
|
51
|
+
# Specify encoding speed (e.g., for AVIF)
|
|
52
|
+
wasm-image-optimization input.png -f avif -s 0
|
|
78
53
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}));
|
|
54
|
+
# Resize and save with a different name
|
|
55
|
+
wasm-image-optimization input.png -w 800 -h 600 -o resized.webp
|
|
82
56
|
```
|
|
83
57
|
|
|
84
|
-
|
|
58
|
+
## Library Usage (TypeScript)
|
|
85
59
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
60
|
+
### Basic Usage
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
import { optimizeImage } from 'wasm-image-optimization';
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
const inputBuffer = ...; // Uint8Array or ArrayBuffer
|
|
67
|
+
|
|
68
|
+
const result = await optimizeImage({
|
|
69
|
+
image: inputBuffer,
|
|
70
|
+
width: 800,
|
|
71
|
+
height: 600,
|
|
72
|
+
fit: 'contain', // 'contain' | 'cover' | 'fill' (default: 'contain')
|
|
73
|
+
format: 'webp', // 'none' | 'png' | 'webp' | 'jpeg' | 'avif' | 'raw' | 'thumbhash'
|
|
74
|
+
quality: 85,
|
|
75
|
+
speed: 6, // 0-10 (encoding speed for AVIF, etc.)
|
|
76
|
+
animation: true // maintain animation if possible
|
|
77
|
+
});
|
|
91
78
|
```
|
|
92
79
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
80
|
+
### Parameters (`OptimizeParams`)
|
|
81
|
+
|
|
82
|
+
| Parameter | Type | Default | Description |
|
|
83
|
+
| :-------------- | :--------------------------------- | :----------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
84
|
+
| **`image`** | `Uint8Array`<br>`ArrayBuffer` | **Required** | The raw binary data of the image to process. |
|
|
85
|
+
| **`width`** | `number` | - | Target width for resizing. If omitted, it's calculated from `height` to maintain aspect ratio. |
|
|
86
|
+
| **`height`** | `number` | - | Target height for resizing. If omitted, it's calculated from `width` to maintain aspect ratio. |
|
|
87
|
+
| **`fit`** | `contain` <br> `cover` <br> `fill` | `'contain'` | Resizing strategy. <br> - `contain`: Fits within bounds (aspect ratio preserved). <br> - `cover`: Fills bounds (may crop). <br> - `fill`: Stretches to match bounds exactly. |
|
|
88
|
+
| **`format`** | `string` | `'webp'` | Output format: `'none'`, `'png'`, `'webp'`, `'jpeg'`, `'avif'`, `'raw'`, `'thumbhash'`. <br> _Note: If `animation: true` and the input is animated, this is forced to `'webp'`._ |
|
|
89
|
+
| **`quality`** | `number` | `100` | Compression quality (0-100). Applies to WebP, JPEG, and AVIF. |
|
|
90
|
+
| **`speed`** | `number` | `6` | Encoding speed (0-10). Primarily used for AVIF. Lower is slower but better compression. |
|
|
91
|
+
| **`animation`** | `boolean` | `false` | If `true`, preserves animation frames (e.g., GIF/animated WebP source). |
|
|
92
|
+
|
|
93
|
+
### Result (`OptimizeResult`)
|
|
94
|
+
|
|
95
|
+
| Property | Type | Description |
|
|
96
|
+
| :---------------------- | :----------- | :--------------------------------------------------- |
|
|
97
|
+
| **`data`** | `Uint8Array` | The processed image data. |
|
|
98
|
+
| **`originalWidth`** | `number` | Width of the input image. |
|
|
99
|
+
| **`originalHeight`** | `number` | Height of the input image. |
|
|
100
|
+
| **`originalAnimation`** | `boolean` | `true` if the input image was animated. |
|
|
101
|
+
| **`originalFormat`** | `string` | Format of the input image (e.g., `"jpeg"`, `"gif"`). |
|
|
102
|
+
| **`width`** | `number` | Width of the output image. |
|
|
103
|
+
| **`height`** | `number` | Height of the output image. |
|
|
104
|
+
| **`animation`** | `boolean` | `true` if the output image is animated. |
|
|
105
|
+
| **`format`** | `string` | Format of the output image. |
|
|
106
|
+
|
|
107
|
+
### Logging & Debugging
|
|
108
|
+
|
|
109
|
+
You can monitor the internal processing and Skia logs.
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
import { ImageConverter } from "wasm-image-optimization";
|
|
113
|
+
|
|
114
|
+
const converter = new ImageConverter();
|
|
115
|
+
|
|
116
|
+
// Set log level: 0 (None), 1 (Error), 2 (Warning), 3 (Info), 4 (Debug)
|
|
117
|
+
converter.logLevel = 3;
|
|
118
|
+
|
|
119
|
+
// Custom log handler
|
|
120
|
+
converter.onLog = (level, message) => {
|
|
121
|
+
console.log(`[ImageConverter][${level}] ${message}`);
|
|
122
|
+
};
|
|
123
|
+
```
|
|
111
124
|
|
|
112
|
-
|
|
125
|
+
### Multi-threading (Workers)
|
|
126
|
+
|
|
127
|
+
Use Workers for high-throughput processing without blocking the main thread.
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
import { optimizeImage } from "wasm-image-optimization/workers";
|
|
131
|
+
|
|
132
|
+
const result = await optimizeImage({
|
|
133
|
+
image: inputBuffer,
|
|
134
|
+
width: 800,
|
|
135
|
+
format: "png",
|
|
136
|
+
});
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Build
|
|
140
|
+
|
|
141
|
+
### Prerequisites
|
|
142
|
+
|
|
143
|
+
- **Emscripten (emsdk)**: Ensure the latest version is installed and environment variables are set.
|
|
144
|
+
- **vcpkg**: Used for managing C++ dependencies.
|
|
145
|
+
|
|
146
|
+
### Steps
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
# Install dependencies
|
|
150
|
+
pnpm install
|
|
151
|
+
|
|
152
|
+
# Configure CMake (using Ninja)
|
|
153
|
+
pnpm wasm:configure
|
|
154
|
+
|
|
155
|
+
# Build Wasm
|
|
156
|
+
pnpm wasm:build
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Tech Stack
|
|
160
|
+
|
|
161
|
+
- **Core**: Skia, libavif (AOM / dav1d), Wuffs, expat
|
|
162
|
+
- **Runtime**: WebAssembly (Emscripten)
|
|
163
|
+
- **Language**: C++20, TypeScript
|
|
164
|
+
|
|
165
|
+
## License
|
|
166
|
+
|
|
167
|
+
MIT
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { initWorker } from "worker-lib";
|
|
2
|
+
import { ImageConverter } from "./single.js";
|
|
3
|
+
let imageConverter;
|
|
4
|
+
const getImageConverter = async () => {
|
|
5
|
+
if (!imageConverter) {
|
|
6
|
+
imageConverter = await ImageConverter.create();
|
|
7
|
+
}
|
|
8
|
+
return imageConverter;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Worker-side implementation for Satoru.
|
|
12
|
+
* Exposes Satoru methods via worker-lib.
|
|
13
|
+
*/
|
|
14
|
+
const actions = {
|
|
15
|
+
async optimizeImage(options) {
|
|
16
|
+
const s = await getImageConverter();
|
|
17
|
+
return await s.optimizeImage(options);
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
const map = initWorker(actions);
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { program } from "commander";
|
|
3
|
+
import fs from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { ImageConverter } from "./single.js";
|
|
6
|
+
// main() execution moved to top level
|
|
7
|
+
program
|
|
8
|
+
.name("wasm-image-optimization")
|
|
9
|
+
.description("High-performance Image to Image converter using WebAssembly (Skia)")
|
|
10
|
+
.version("1.0.0")
|
|
11
|
+
.argument("<input>", "input file path")
|
|
12
|
+
.option("-o, --output <output>", "output file path")
|
|
13
|
+
.option("-w, --width <number>", "resize width", parseInt)
|
|
14
|
+
.option("-h, --height <number>", "resize height", parseInt)
|
|
15
|
+
.option("-f, --format <format>", "output format (none, png, webp, jpeg, avif, raw, thumbhash)", "webp")
|
|
16
|
+
.option("-q, --quality <number>", "output quality (0-100)", parseFloat, 85)
|
|
17
|
+
.option("-s, --speed <number>", "encoding speed (0-10, for AVIF)", parseInt, 6)
|
|
18
|
+
.option("-a, --animation", "maintain animation frames", false)
|
|
19
|
+
.action(async (input, options) => {
|
|
20
|
+
try {
|
|
21
|
+
const inputData = await fs.readFile(input);
|
|
22
|
+
const converter = await ImageConverter.create();
|
|
23
|
+
console.log(`Converting ${input}...`);
|
|
24
|
+
const result = await converter.optimizeImage({
|
|
25
|
+
image: inputData,
|
|
26
|
+
width: options.width,
|
|
27
|
+
height: options.height,
|
|
28
|
+
format: options.format,
|
|
29
|
+
quality: options.quality,
|
|
30
|
+
speed: options.speed,
|
|
31
|
+
animation: options.animation,
|
|
32
|
+
});
|
|
33
|
+
let outputPath = options.output;
|
|
34
|
+
if (!outputPath) {
|
|
35
|
+
const ext = options.format || "webp";
|
|
36
|
+
const parsed = path.parse(input);
|
|
37
|
+
outputPath = path.join(parsed.dir, `${parsed.name}.${ext}`);
|
|
38
|
+
}
|
|
39
|
+
await fs.writeFile(outputPath, result.data);
|
|
40
|
+
console.log(`Successfully converted to ${outputPath} (${result.width}x${result.height})`);
|
|
41
|
+
}
|
|
42
|
+
catch (err) {
|
|
43
|
+
console.error(`Error: ${err.message}`);
|
|
44
|
+
process.exit(1);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
program.parseAsync(process.argv);
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
export declare enum LogLevel {
|
|
2
|
+
None = 0,
|
|
3
|
+
Error = 1,
|
|
4
|
+
Warning = 2,
|
|
5
|
+
Info = 3,
|
|
6
|
+
Debug = 4
|
|
7
|
+
}
|
|
8
|
+
export interface ImageConverterModule {
|
|
9
|
+
create_instance: () => any;
|
|
10
|
+
destroy_instance: (inst: any) => void;
|
|
11
|
+
load_image: (inst: any, data: Uint8Array) => boolean;
|
|
12
|
+
encode: (inst: any, format: number, quality: number, speed: number, animation: boolean) => Uint8Array | null;
|
|
13
|
+
resize: (inst: any, width: number, height: number, fit: number) => boolean;
|
|
14
|
+
get_original_width: (inst: any) => number;
|
|
15
|
+
get_original_height: (inst: any) => number;
|
|
16
|
+
is_original_animation: (inst: any) => boolean;
|
|
17
|
+
get_original_format: (inst: any) => string;
|
|
18
|
+
get_width: (inst: any) => number;
|
|
19
|
+
get_height: (inst: any) => number;
|
|
20
|
+
is_animation: (inst: any) => boolean;
|
|
21
|
+
get_format: (inst: any) => string;
|
|
22
|
+
set_log_level: (level: number) => void;
|
|
23
|
+
onLog?: (level: LogLevel, message: string) => void;
|
|
24
|
+
logLevel: LogLevel;
|
|
25
|
+
}
|
|
26
|
+
export declare enum RenderFormat {
|
|
27
|
+
None = -1,
|
|
28
|
+
SVG = 0,
|
|
29
|
+
PNG = 1,
|
|
30
|
+
WebP = 2,
|
|
31
|
+
PDF = 3,
|
|
32
|
+
JPEG = 4,
|
|
33
|
+
AVIF = 5,
|
|
34
|
+
RAW = 6,
|
|
35
|
+
ThumbHash = 7
|
|
36
|
+
}
|
|
37
|
+
export declare enum FitMode {
|
|
38
|
+
Contain = 0,
|
|
39
|
+
Cover = 1,
|
|
40
|
+
Fill = 2
|
|
41
|
+
}
|
|
42
|
+
export type OptimizeParams = {
|
|
43
|
+
image: Uint8Array | ArrayBuffer;
|
|
44
|
+
width?: number;
|
|
45
|
+
height?: number;
|
|
46
|
+
fit?: "contain" | "cover" | "fill";
|
|
47
|
+
format?: "none" | "png" | "webp" | "jpeg" | "avif" | "raw" | "thumbhash";
|
|
48
|
+
quality?: number;
|
|
49
|
+
speed?: number;
|
|
50
|
+
animation?: boolean;
|
|
51
|
+
};
|
|
52
|
+
export type OptimizeResult = {
|
|
53
|
+
data: Uint8Array;
|
|
54
|
+
originalWidth: number;
|
|
55
|
+
originalHeight: number;
|
|
56
|
+
originalAnimation: boolean;
|
|
57
|
+
originalFormat: string;
|
|
58
|
+
width: number;
|
|
59
|
+
height: number;
|
|
60
|
+
animation: boolean;
|
|
61
|
+
format: string;
|
|
62
|
+
};
|
|
63
|
+
export declare abstract class ImageConverterBase {
|
|
64
|
+
private factory;
|
|
65
|
+
private modPromise?;
|
|
66
|
+
private _onLog?;
|
|
67
|
+
private _logLevel;
|
|
68
|
+
protected constructor(factory: any);
|
|
69
|
+
get onLog(): ((level: LogLevel, message: string) => void) | undefined;
|
|
70
|
+
set onLog(val: ((level: LogLevel, message: string) => void) | undefined);
|
|
71
|
+
get logLevel(): LogLevel;
|
|
72
|
+
set logLevel(val: LogLevel);
|
|
73
|
+
private getModule;
|
|
74
|
+
setLogLevel(level: LogLevel): Promise<void>;
|
|
75
|
+
optimizeImage(params: OptimizeParams): Promise<OptimizeResult>;
|
|
76
|
+
}
|
package/dist/core.js
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
export var LogLevel;
|
|
2
|
+
(function (LogLevel) {
|
|
3
|
+
LogLevel[LogLevel["None"] = 0] = "None";
|
|
4
|
+
LogLevel[LogLevel["Error"] = 1] = "Error";
|
|
5
|
+
LogLevel[LogLevel["Warning"] = 2] = "Warning";
|
|
6
|
+
LogLevel[LogLevel["Info"] = 3] = "Info";
|
|
7
|
+
LogLevel[LogLevel["Debug"] = 4] = "Debug";
|
|
8
|
+
})(LogLevel || (LogLevel = {}));
|
|
9
|
+
export var RenderFormat;
|
|
10
|
+
(function (RenderFormat) {
|
|
11
|
+
RenderFormat[RenderFormat["None"] = -1] = "None";
|
|
12
|
+
RenderFormat[RenderFormat["SVG"] = 0] = "SVG";
|
|
13
|
+
RenderFormat[RenderFormat["PNG"] = 1] = "PNG";
|
|
14
|
+
RenderFormat[RenderFormat["WebP"] = 2] = "WebP";
|
|
15
|
+
RenderFormat[RenderFormat["PDF"] = 3] = "PDF";
|
|
16
|
+
RenderFormat[RenderFormat["JPEG"] = 4] = "JPEG";
|
|
17
|
+
RenderFormat[RenderFormat["AVIF"] = 5] = "AVIF";
|
|
18
|
+
RenderFormat[RenderFormat["RAW"] = 6] = "RAW";
|
|
19
|
+
RenderFormat[RenderFormat["ThumbHash"] = 7] = "ThumbHash";
|
|
20
|
+
})(RenderFormat || (RenderFormat = {}));
|
|
21
|
+
export var FitMode;
|
|
22
|
+
(function (FitMode) {
|
|
23
|
+
FitMode[FitMode["Contain"] = 0] = "Contain";
|
|
24
|
+
FitMode[FitMode["Cover"] = 1] = "Cover";
|
|
25
|
+
FitMode[FitMode["Fill"] = 2] = "Fill";
|
|
26
|
+
})(FitMode || (FitMode = {}));
|
|
27
|
+
export class ImageConverterBase {
|
|
28
|
+
factory;
|
|
29
|
+
modPromise;
|
|
30
|
+
_onLog;
|
|
31
|
+
_logLevel = LogLevel.None;
|
|
32
|
+
constructor(factory) {
|
|
33
|
+
this.factory = factory;
|
|
34
|
+
}
|
|
35
|
+
get onLog() {
|
|
36
|
+
return this._onLog;
|
|
37
|
+
}
|
|
38
|
+
set onLog(val) {
|
|
39
|
+
this._onLog = val;
|
|
40
|
+
this.getModule().then((mod) => {
|
|
41
|
+
mod.onLog = val;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
get logLevel() {
|
|
45
|
+
return this._logLevel;
|
|
46
|
+
}
|
|
47
|
+
set logLevel(val) {
|
|
48
|
+
this._logLevel = val;
|
|
49
|
+
this.getModule().then((mod) => {
|
|
50
|
+
mod.logLevel = val;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
async getModule() {
|
|
54
|
+
if (!this.modPromise) {
|
|
55
|
+
this.modPromise = (async () => {
|
|
56
|
+
let currentLogLevel = this._logLevel;
|
|
57
|
+
let currentUserOnLog = this._onLog;
|
|
58
|
+
const initialModule = {
|
|
59
|
+
onLog: (level, message) => {
|
|
60
|
+
if (this._logLevel !== LogLevel.None &&
|
|
61
|
+
level <= this._logLevel &&
|
|
62
|
+
this._onLog) {
|
|
63
|
+
this._onLog(level, message);
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
print: (text) => {
|
|
67
|
+
if (this._logLevel !== LogLevel.None &&
|
|
68
|
+
LogLevel.Info <= this._logLevel &&
|
|
69
|
+
this._onLog) {
|
|
70
|
+
this._onLog(LogLevel.Info, text);
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
printErr: (text) => {
|
|
74
|
+
if (this._logLevel !== LogLevel.None &&
|
|
75
|
+
LogLevel.Error <= this._logLevel &&
|
|
76
|
+
this._onLog) {
|
|
77
|
+
this._onLog(LogLevel.Error, text);
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
const mod = (await this.factory(initialModule));
|
|
82
|
+
mod.set_log_level(this._logLevel);
|
|
83
|
+
// In modularized mode, the factory returns the module object.
|
|
84
|
+
// EM_JS might be looking for functions on this object.
|
|
85
|
+
mod.onLog = initialModule.onLog;
|
|
86
|
+
mod.logLevel = this._logLevel;
|
|
87
|
+
const originalSetLogLevel = mod.set_log_level;
|
|
88
|
+
mod.set_log_level = (level) => {
|
|
89
|
+
this._logLevel = level;
|
|
90
|
+
originalSetLogLevel(level);
|
|
91
|
+
};
|
|
92
|
+
let internalOnLog = mod.onLog;
|
|
93
|
+
Object.defineProperty(mod, "onLog", {
|
|
94
|
+
get: () => internalOnLog,
|
|
95
|
+
set: (val) => {
|
|
96
|
+
internalOnLog = val;
|
|
97
|
+
this._onLog = val;
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
let internalLogLevel = mod.logLevel;
|
|
101
|
+
Object.defineProperty(mod, "logLevel", {
|
|
102
|
+
get: () => internalLogLevel,
|
|
103
|
+
set: (val) => {
|
|
104
|
+
internalLogLevel = val;
|
|
105
|
+
this._logLevel = val;
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
return mod;
|
|
109
|
+
})();
|
|
110
|
+
}
|
|
111
|
+
return this.modPromise;
|
|
112
|
+
}
|
|
113
|
+
async setLogLevel(level) {
|
|
114
|
+
this.logLevel = level;
|
|
115
|
+
}
|
|
116
|
+
async optimizeImage(params) {
|
|
117
|
+
const mod = await this.getModule();
|
|
118
|
+
const inst = mod.create_instance();
|
|
119
|
+
const { image: imageInput, width, height, fit = "contain", format, quality, speed = 6, animation = false, } = params;
|
|
120
|
+
const image = imageInput instanceof Uint8Array
|
|
121
|
+
? imageInput
|
|
122
|
+
: new Uint8Array(imageInput);
|
|
123
|
+
try {
|
|
124
|
+
if (!mod.load_image(inst, image)) {
|
|
125
|
+
throw new Error("Failed to load image");
|
|
126
|
+
}
|
|
127
|
+
const originalWidth = mod.get_original_width(inst);
|
|
128
|
+
const originalHeight = mod.get_original_height(inst);
|
|
129
|
+
const originalAnimation = mod.is_original_animation(inst);
|
|
130
|
+
const originalFormat = mod.get_original_format(inst);
|
|
131
|
+
let targetFormat = format || "webp";
|
|
132
|
+
if (targetFormat !== "none" && animation && originalAnimation) {
|
|
133
|
+
targetFormat = "webp";
|
|
134
|
+
}
|
|
135
|
+
if (targetFormat !== "none" && (width || height)) {
|
|
136
|
+
const fitMap = {
|
|
137
|
+
contain: FitMode.Contain,
|
|
138
|
+
cover: FitMode.Cover,
|
|
139
|
+
fill: FitMode.Fill,
|
|
140
|
+
};
|
|
141
|
+
const fMode = fitMap[fit] ?? FitMode.Contain;
|
|
142
|
+
mod.resize(inst, width ?? 0, height ?? 0, fMode);
|
|
143
|
+
}
|
|
144
|
+
const formatMap = {
|
|
145
|
+
none: RenderFormat.None,
|
|
146
|
+
png: RenderFormat.PNG,
|
|
147
|
+
webp: RenderFormat.WebP,
|
|
148
|
+
jpeg: RenderFormat.JPEG,
|
|
149
|
+
avif: RenderFormat.AVIF,
|
|
150
|
+
raw: RenderFormat.RAW,
|
|
151
|
+
thumbhash: RenderFormat.ThumbHash,
|
|
152
|
+
};
|
|
153
|
+
const f = formatMap[targetFormat] ?? RenderFormat.WebP;
|
|
154
|
+
const result = mod.encode(inst, f, quality ?? 100, speed, animation);
|
|
155
|
+
if (!result) {
|
|
156
|
+
throw new Error("Failed to encode image");
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
data: new Uint8Array(result),
|
|
160
|
+
originalWidth,
|
|
161
|
+
originalHeight,
|
|
162
|
+
originalAnimation,
|
|
163
|
+
originalFormat,
|
|
164
|
+
width: mod.get_width(inst),
|
|
165
|
+
height: mod.get_height(inst),
|
|
166
|
+
animation: mod.is_animation(inst),
|
|
167
|
+
format: mod.get_format(inst),
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
finally {
|
|
171
|
+
mod.destroy_instance(inst);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/single.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ImageConverter as ImageConverterBase, OptimizeParams, OptimizeResult } from "./index.js";
|
|
2
|
+
export * from "./core.js";
|
|
3
|
+
/**
|
|
4
|
+
* Single-file specialized wrapper for Satoru.
|
|
5
|
+
* Uses the satoru-single.js artifact with embedded WASM.
|
|
6
|
+
*/
|
|
7
|
+
export declare class ImageConverter extends ImageConverterBase {
|
|
8
|
+
private constructor();
|
|
9
|
+
/**
|
|
10
|
+
* Create Satoru instance with embedded WASM.
|
|
11
|
+
*/
|
|
12
|
+
static create(): Promise<ImageConverter>;
|
|
13
|
+
}
|
|
14
|
+
export declare function optimizeImage(params: OptimizeParams): Promise<OptimizeResult>;
|