@tonpc64/vixel-wasm 0.0.0-stage → 0.2.0

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Chanwit Piromplad
4
+
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
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,119 @@
1
- # Temporary Holding Version
1
+ # Process images in the browser with WebAssembly
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Use Vixel's WebAssembly module to resize and convert local images in a modern browser. The browser reads the file and runs the transformation locally.
4
+
5
+ ## Install with npm
6
+
7
+ Once the package is published, add it to your frontend project.
8
+
9
+ ```sh
10
+ npm install @tonpc64/vixel-wasm
11
+ ```
12
+
13
+ In your browser code, initialize the module before calling `resize`. See the example below for reading a file and downloading the result.
14
+
15
+ ```js
16
+ import init, { resize } from "@tonpc64/vixel-wasm";
17
+
18
+ await init();
19
+ const output = resize(input, 800, 600, "webp");
20
+ ```
21
+
22
+ ## Get the browser bundle
23
+
24
+ Download and extract `vixel-web.tar.gz`. The `vixel-web/` directory contains the JavaScript module, the `.wasm` binary, and TypeScript declarations. [Download the browser bundle](https://github.com/TonPC64/vixel/releases/latest/download/vixel-web.tar.gz)
25
+
26
+ ```sh
27
+ tar -xzf vixel-web.tar.gz
28
+ ```
29
+
30
+ To build the same bundle from source, run the following commands from your Vixel checkout.
31
+
32
+ ```sh
33
+ rustup target add wasm32-unknown-unknown
34
+ cargo install --locked wasm-bindgen-cli --version "$(cargo pkgid --locked wasm-bindgen | sed 's/.*@//')"
35
+ make wasm
36
+ ```
37
+
38
+ The build writes the bundle to `target/vixel-web/`. Copy that directory alongside your web page.
39
+
40
+ ## Resize a file in the browser
41
+
42
+ Save this page as `index.html` next to the `vixel-web/` directory.
43
+
44
+ ```html
45
+ <!doctype html>
46
+ <html lang="en">
47
+ <meta charset="utf-8">
48
+ <title>Vixel thumbnail</title>
49
+ <label>Choose an image <input id="image" type="file" accept="image/*" disabled></label>
50
+ <p id="status" role="status">Loading Vixel…</p>
51
+ <a id="download" download="thumbnail.webp" hidden>Download thumbnail</a>
52
+
53
+ <script type="module">
54
+ import init, { resize } from "./vixel-web/vixel.js";
55
+
56
+ const picker = document.querySelector("#image");
57
+ const status = document.querySelector("#status");
58
+ const download = document.querySelector("#download");
59
+
60
+ try {
61
+ await init();
62
+ picker.disabled = false;
63
+ status.textContent = "Choose an image to resize.";
64
+ } catch (error) {
65
+ status.textContent = error.message;
66
+ }
67
+
68
+ picker.addEventListener("change", async () => {
69
+ const file = picker.files[0];
70
+ if (!file) return;
71
+
72
+ picker.disabled = true;
73
+ download.hidden = true;
74
+ if (download.hasAttribute("href")) {
75
+ URL.revokeObjectURL(download.href);
76
+ download.removeAttribute("href");
77
+ }
78
+
79
+ try {
80
+ const input = new Uint8Array(await file.arrayBuffer());
81
+ const output = resize(input, 800, 600, "webp");
82
+ download.href = URL.createObjectURL(new Blob([output], { type: "image/webp" }));
83
+ download.hidden = false;
84
+ status.textContent = "Thumbnail ready.";
85
+ } catch (error) {
86
+ status.textContent = error.message;
87
+ } finally {
88
+ picker.disabled = false;
89
+ }
90
+ });
91
+ </script>
92
+ </html>
93
+ ```
94
+
95
+ Serve the directory over HTTP so the browser can load the JavaScript and WebAssembly files together.
96
+
97
+ ```sh
98
+ python3 -m http.server --bind 127.0.0.1 8000
99
+ ```
100
+
101
+ Open `http://127.0.0.1:8000`, choose a supported image, and download `thumbnail.webp`. Vixel applies EXIF orientation and fits the image within 800×600 while preserving its aspect ratio. It leaves smaller images at their original size.
102
+
103
+ ## Use `resize` in your own page
104
+
105
+ After calling `await init()` once, pass a `Uint8Array` to `resize(input, maxWidth, maxHeight, format, quality?)`. It returns a new `Uint8Array` with the encoded image. Each bound must be an integer from 1 to 8192. For JPEG, the optional quality argument is an integer from 1 to 100 and defaults to 82.
106
+
107
+ The module reads JPEG, PNG, WebP, GIF, TIFF, BMP, and ICO. It writes JPEG, PNG, lossless WebP, GIF, and TIFF. Animated inputs produce a still image. Input limits are 32 MiB, 24 megapixels, and 8192 pixels per side. Run `resize` in a Web Worker to keep the page responsive while processing large files.
108
+
109
+ To retrieve source metadata, use `vixel img info` or call the server's `/info` endpoint. [CLI metadata](https://github.com/TonPC64/vixel/blob/main/docs/cli.md#inspect-an-image) · [Server metadata](https://github.com/TonPC64/vixel/blob/main/docs/server.md#inspect-image-metadata)
110
+
111
+ ## Publish the npm package
112
+
113
+ For the first version, run `make wasm-npm` with the build tools listed above. The command prints the npm tarball filename and uses the Cargo package version. Sign in with `npm login` and publish the tarball. For version `0.2.0`, run the following command.
114
+
115
+ ```sh
116
+ npm publish ./target/tonpc64-vixel-wasm-0.2.0.tgz --access public
117
+ ```
118
+
119
+ In the npm package settings, add a GitHub Actions trusted publisher for owner `TonPC64`, repository `vixel`, and workflow `release.yml`. Allow `npm publish` and leave the environment field empty. Subsequent GitHub releases publish the matching version to npm. [npm trusted publishing](https://docs.npmjs.com/trusted-publishers/)
package/package.json CHANGED
@@ -1,6 +1,23 @@
1
1
  {
2
2
  "name": "@tonpc64/vixel-wasm",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "description": "Resize and convert images in the browser with Vixel WebAssembly",
4
+ "type": "module",
5
+ "main": "./vixel.js",
6
+ "types": "./vixel.d.ts",
7
+ "files": [
8
+ "vixel.js",
9
+ "vixel.d.ts",
10
+ "vixel_bg.wasm",
11
+ "vixel_bg.wasm.d.ts"
12
+ ],
13
+ "license": "MIT",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/TonPC64/vixel.git"
17
+ },
18
+ "publishConfig": {
19
+ "access": "public",
20
+ "registry": "https://registry.npmjs.org/"
21
+ },
22
+ "version": "0.2.0"
23
+ }
package/vixel.d.ts ADDED
@@ -0,0 +1,42 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /**
5
+ * Fits an image inside a bounding box without enlargement and encodes it into the requested format.
6
+ */
7
+ export function resize(input: Uint8Array, max_width: number, max_height: number, format: string, quality?: number | null): Uint8Array;
8
+
9
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
10
+
11
+ export interface InitOutput {
12
+ readonly memory: WebAssembly.Memory;
13
+ readonly resize: (a: any, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number, number];
14
+ readonly __wbindgen_externrefs: WebAssembly.Table;
15
+ readonly __wbindgen_malloc: (a: number, b: number) => number;
16
+ readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
17
+ readonly __wbindgen_free: (a: number, b: number, c: number) => void;
18
+ readonly __externref_table_dealloc: (a: number) => void;
19
+ readonly __wbindgen_start: () => void;
20
+ }
21
+
22
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
23
+
24
+ /**
25
+ * Instantiates the given `module`, which can either be bytes or
26
+ * a precompiled `WebAssembly.Module`.
27
+ *
28
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
29
+ *
30
+ * @returns {InitOutput}
31
+ */
32
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
33
+
34
+ /**
35
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
36
+ * for everything else, calls `WebAssembly.instantiate` directly.
37
+ *
38
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
39
+ *
40
+ * @returns {Promise<InitOutput>}
41
+ */
42
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
package/vixel.js ADDED
@@ -0,0 +1,255 @@
1
+ /* @ts-self-types="./vixel.d.ts" */
2
+
3
+ /**
4
+ * Fits an image inside a bounding box without enlargement and encodes it into the requested format.
5
+ * @param {Uint8Array} input
6
+ * @param {number} max_width
7
+ * @param {number} max_height
8
+ * @param {string} format
9
+ * @param {number | null} [quality]
10
+ * @returns {Uint8Array}
11
+ */
12
+ export function resize(input, max_width, max_height, format, quality) {
13
+ const val2 = +max_width;
14
+ const val3 = +max_height;
15
+ const val4 = +(isLikeNone(quality) ? 0 : quality);
16
+ const ptr0 = passStringToWasm0(format, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
17
+ const len0 = WASM_VECTOR_LEN;
18
+ const ret = wasm.resize(input, val2, val3, ptr0, len0, !isLikeNone(quality), val4);
19
+ if (ret[3]) {
20
+ throw takeFromExternrefTable0(ret[2]);
21
+ }
22
+ var v5 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
23
+ wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
24
+ return v5;
25
+ }
26
+ function __wbg_get_imports() {
27
+ const import0 = {
28
+ __proto__: null,
29
+ __wbg_Error_208446807d0d1e30: function(arg0, arg1) {
30
+ const ret = Error(getStringFromWasm0(arg0, arg1));
31
+ return ret;
32
+ },
33
+ __wbg___wbindgen_throw_4c88d8bf264fd984: function(arg0, arg1) {
34
+ throw new Error(getStringFromWasm0(arg0, arg1));
35
+ },
36
+ __wbg_length_186163212ffe79c3: function(arg0) {
37
+ const ret = arg0.length;
38
+ return ret;
39
+ },
40
+ __wbg_prototypesetcall_13162965d1fe7aa3: function(arg0, arg1, arg2) {
41
+ Uint8Array.prototype.set.call(getArrayU8FromWasm0(arg0, arg1), arg2);
42
+ },
43
+ __wbindgen_init_externref_table: function() {
44
+ const table = wasm.__wbindgen_externrefs;
45
+ table.set(0, undefined);
46
+ table.set(1024, undefined);
47
+ table.set(1025, null);
48
+ table.set(1026, true);
49
+ table.set(1027, false);
50
+ },
51
+ };
52
+ return {
53
+ __proto__: null,
54
+ "./vixel_bg.js": import0,
55
+ };
56
+ }
57
+
58
+ function getArrayU8FromWasm0(ptr, len) {
59
+ ptr = ptr >>> 0;
60
+ return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
61
+ }
62
+
63
+ function getStringFromWasm0(ptr, len) {
64
+ ptr = ptr >>> 0;
65
+ return decodeText(getUint8ArrayMemory0().subarray(ptr, ptr + len));
66
+ }
67
+
68
+ let cachedUint8ArrayMemory0 = null;
69
+ function getUint8ArrayMemory0() {
70
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
71
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
72
+ }
73
+ return cachedUint8ArrayMemory0;
74
+ }
75
+
76
+ function isLikeNone(x) {
77
+ return x === undefined || x === null;
78
+ }
79
+
80
+ function passStringToWasm0(arg, malloc, realloc) {
81
+ if (realloc === undefined) {
82
+ const buf = cachedTextEncoder.encode(arg);
83
+ const ptr = malloc(buf.length, 1) >>> 0;
84
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
85
+ WASM_VECTOR_LEN = buf.length;
86
+ return ptr;
87
+ }
88
+
89
+ let len = arg.length;
90
+ let ptr = malloc(len, 1) >>> 0;
91
+
92
+ const mem = getUint8ArrayMemory0();
93
+
94
+ let offset = 0;
95
+
96
+ for (; offset < len; offset++) {
97
+ const code = arg.charCodeAt(offset);
98
+ if (code > 0x7F) break;
99
+ mem[ptr + offset] = code;
100
+ }
101
+ if (offset !== len) {
102
+ if (offset !== 0) {
103
+ arg = arg.slice(offset);
104
+ }
105
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
106
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
107
+ const ret = cachedTextEncoder.encodeInto(arg, view);
108
+
109
+ offset += ret.written;
110
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
111
+ }
112
+
113
+ WASM_VECTOR_LEN = offset;
114
+ return ptr;
115
+ }
116
+
117
+ function takeFromExternrefTable0(idx) {
118
+ const value = wasm.__wbindgen_externrefs.get(idx);
119
+ wasm.__externref_table_dealloc(idx);
120
+ return value;
121
+ }
122
+
123
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true });
124
+ cachedTextDecoder.decode();
125
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
126
+ let numBytesDecoded = 0;
127
+ function decodeText(bytes) {
128
+ numBytesDecoded += bytes.length;
129
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
130
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true });
131
+ cachedTextDecoder.decode();
132
+ numBytesDecoded = bytes.length;
133
+ }
134
+ return cachedTextDecoder.decode(bytes);
135
+ }
136
+
137
+ const cachedTextEncoder = new TextEncoder();
138
+
139
+ if (!('encodeInto' in cachedTextEncoder)) {
140
+ cachedTextEncoder.encodeInto = function (arg, view) {
141
+ const buf = cachedTextEncoder.encode(arg);
142
+ view.set(buf);
143
+ return {
144
+ read: arg.length,
145
+ written: buf.length
146
+ };
147
+ };
148
+ }
149
+
150
+ let WASM_VECTOR_LEN = 0;
151
+
152
+ let wasmModule, wasmInstance, wasm;
153
+ function __wbg_finalize_init(instance, module) {
154
+ wasmInstance = instance;
155
+ wasm = instance.exports;
156
+ wasmModule = module;
157
+ cachedUint8ArrayMemory0 = null;
158
+ wasm.__wbindgen_start();
159
+ return wasm;
160
+ }
161
+
162
+ async function __wbg_load(module, imports) {
163
+ if (typeof Response === 'function' && module instanceof Response) {
164
+ if (!module.ok) {
165
+ throw new Error(`failed to fetch Wasm: ${module.status} ${module.statusText} fetching '${module.url}'`);
166
+ }
167
+
168
+ if (typeof WebAssembly.instantiateStreaming === 'function') {
169
+ try {
170
+ return await WebAssembly.instantiateStreaming(module, imports);
171
+ } catch (e) {
172
+ const validResponse = expectedResponseType(module.type);
173
+
174
+ if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
175
+ console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
176
+
177
+ } else { throw e; }
178
+ }
179
+ }
180
+
181
+ const bytes = await module.arrayBuffer();
182
+ return await WebAssembly.instantiate(bytes, imports);
183
+ } else {
184
+ const instance = await WebAssembly.instantiate(module, imports);
185
+
186
+ if (instance instanceof WebAssembly.Instance) {
187
+ return { instance, module };
188
+ } else {
189
+ return instance;
190
+ }
191
+ }
192
+
193
+ function expectedResponseType(type) {
194
+ switch (type) {
195
+ case 'basic': case 'cors': case 'default': return true;
196
+ }
197
+ return false;
198
+ }
199
+ }
200
+
201
+ function initSync(module) {
202
+ if (wasm !== undefined) return wasm;
203
+
204
+
205
+ if (module !== undefined) {
206
+ if (Object.getPrototypeOf(module) === Object.prototype) {
207
+ ({module} = module)
208
+ } else {
209
+ console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
210
+ }
211
+ }
212
+
213
+ const imports = __wbg_get_imports();
214
+ if (!(module instanceof WebAssembly.Module)) {
215
+ module = new WebAssembly.Module(module);
216
+ }
217
+ const instance = new WebAssembly.Instance(module, imports);
218
+ return __wbg_finalize_init(instance, module);
219
+ }
220
+
221
+ let __wbg_init_promise;
222
+ async function __wbg_init(module_or_path) {
223
+ if (wasm !== undefined) return wasm;
224
+ if (__wbg_init_promise === undefined) {
225
+ __wbg_init_promise = __wbg_init_once(module_or_path);
226
+ __wbg_init_promise.catch(() => { __wbg_init_promise = undefined; });
227
+ }
228
+ return __wbg_init_promise;
229
+ }
230
+
231
+ async function __wbg_init_once(module_or_path) {
232
+
233
+ if (module_or_path !== undefined) {
234
+ if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
235
+ ({module_or_path} = module_or_path)
236
+ } else {
237
+ console.warn('using deprecated parameters for the initialization function; pass a single object instead')
238
+ }
239
+ }
240
+
241
+ if (module_or_path === undefined) {
242
+ module_or_path = new URL('vixel_bg.wasm', import.meta.url);
243
+ }
244
+ const imports = __wbg_get_imports();
245
+
246
+ if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
247
+ module_or_path = fetch(module_or_path);
248
+ }
249
+
250
+ const { instance, module } = await __wbg_load(await module_or_path, imports);
251
+
252
+ return __wbg_finalize_init(instance, module);
253
+ }
254
+
255
+ export { initSync, __wbg_init as default };
package/vixel_bg.wasm ADDED
Binary file
@@ -0,0 +1,10 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ export const memory: WebAssembly.Memory;
4
+ export const resize: (a: any, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number, number];
5
+ export const __wbindgen_externrefs: WebAssembly.Table;
6
+ export const __wbindgen_malloc: (a: number, b: number) => number;
7
+ export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
8
+ export const __wbindgen_free: (a: number, b: number, c: number) => void;
9
+ export const __externref_table_dealloc: (a: number) => void;
10
+ export const __wbindgen_start: () => void;