@build198x/web 0.0.0-stage → 0.4.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 CHANGED
@@ -1,3 +1,54 @@
1
- # Temporary Holding Version
1
+ # @build198x/web
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
+ Build198x in the browser. For now it holds the image lint: it reports where
4
+ an image breaks what a retro machine can show, without changing the image.
5
+ It's the same Rust as `build198x image lint`, compiled to WebAssembly, so the
6
+ page and the command line can't disagree. Code198x's sprite course checks
7
+ learners' art with it.
8
+
9
+ ```js
10
+ import init, { lintImage, LintOptions } from '@build198x/web';
11
+
12
+ await init();
13
+ const options = new LintOptions();
14
+ options.setMask(maskRgba, 16, 16); // optional
15
+ const findings = JSON.parse(lintImage('sinclair-zx-spectrum', rgba, 16, 16, options));
16
+ options.free();
17
+ ```
18
+
19
+ `rgba` is four bytes a pixel, row by row, as a canvas's `getImageData` gives
20
+ it. `findings` is an array, empty when the image passes. Each finding has a
21
+ `rule`, a `cell` (`{ column, row }`, or `null`) and the rule's own data:
22
+
23
+ | `rule` | Data |
24
+ |---|---|
25
+ | `size` | `width`, `height`, `expected` (`[w, h]` or `null`), `cell_width`, `cell_height` |
26
+ | `mask-size` | `width`, `height`, `mask_width`, `mask_height` |
27
+ | `see-through` | `pixels` |
28
+ | `off-palette` | `colour` (`#rrggbb`), `pixels`, `nearest` (palette index) |
29
+ | `cell-colours` | `colours` (palette indices) |
30
+ | `mask-colours` | `pixels` |
31
+ | `mask-gap` | `pixels` |
32
+
33
+ `LintOptions` also has `setSize(width, height)`, `setPaper(index)`,
34
+ `setMode(name)` and `setPalette(name)`. `lintImage` throws for an unknown
35
+ machine, mode, palette or paper, a machine the lint doesn't support yet, or a
36
+ buffer that isn't `width × height × 4` bytes.
37
+
38
+ ## The Spectrum's rules
39
+
40
+ - Every 8 × 8 cell holds one ink and one paper, both from the same BRIGHT
41
+ half. Black goes with either.
42
+ - There are no see-through pixels: every pixel is its cell's ink or paper. A
43
+ pixel that isn't fully opaque is a `see-through` finding; a sprite's mask
44
+ does that job.
45
+ - A mask is black where the sprite covers the background and white where the
46
+ background shows through, as its bytes are. The sprite's shape is every
47
+ pixel that isn't its paper (black unless `setPaper` says otherwise), and
48
+ each needs black under it. Mask beyond the shape, such as an outline, is
49
+ allowed.
50
+
51
+ Only the ZX Spectrum is supported so far.
52
+
53
+ Source: [build198x/build198x](https://github.com/build198x/build198x),
54
+ `crates/build198x-web`. Licence: GPL-2.0-or-later.
@@ -0,0 +1,87 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /**
5
+ * What to lint against, beyond the machine. Every setting is optional.
6
+ */
7
+ export class LintOptions {
8
+ free(): void;
9
+ [Symbol.dispose](): void;
10
+ constructor();
11
+ /**
12
+ * A mask as RGBA: on the Spectrum, black where the sprite covers the
13
+ * background and white where it shows through.
14
+ */
15
+ setMask(rgba: Uint8Array | number[], width: number, height: number): void;
16
+ /**
17
+ * The screen mode, by name; the machine's first mode otherwise.
18
+ */
19
+ setMode(mode: string): void;
20
+ /**
21
+ * The palette interpretation, by name; the machine's default otherwise.
22
+ */
23
+ setPalette(palette: string): void;
24
+ /**
25
+ * The palette index of the sprite's paper; black (0) otherwise. Every
26
+ * other colour is the sprite's shape, which the mask must cover.
27
+ */
28
+ setPaper(index: number): void;
29
+ /**
30
+ * The width and height the image must have.
31
+ */
32
+ setSize(width: number, height: number): void;
33
+ }
34
+
35
+ /**
36
+ * Lints an RGBA image against `machine` and returns the findings as a JSON
37
+ * array, empty when the image passes.
38
+ *
39
+ * # Errors
40
+ *
41
+ * An unknown machine, mode, palette or paper; a machine the lint doesn't
42
+ * support yet; a buffer that isn't `width × height × 4` bytes; or an empty
43
+ * image.
44
+ */
45
+ export function lintImage(machine: string, rgba: Uint8Array | number[], width: number, height: number, options: LintOptions): string;
46
+
47
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
48
+
49
+ export interface InitOutput {
50
+ readonly memory: WebAssembly.Memory;
51
+ readonly __wbg_lintoptions_free: (a: number, b: number) => void;
52
+ readonly lintImage: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number, number];
53
+ readonly lintoptions_new: () => number;
54
+ readonly lintoptions_setMask: (a: number, b: number, c: number, d: number, e: number) => void;
55
+ readonly lintoptions_setMode: (a: number, b: number, c: number) => void;
56
+ readonly lintoptions_setPalette: (a: number, b: number, c: number) => void;
57
+ readonly lintoptions_setPaper: (a: number, b: number) => void;
58
+ readonly lintoptions_setSize: (a: number, b: number, c: number) => void;
59
+ readonly __wbindgen_externrefs: WebAssembly.Table;
60
+ readonly __wbindgen_malloc: (a: number, b: number) => number;
61
+ readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
62
+ readonly __wbindgen_free: (a: number, b: number, c: number) => void;
63
+ readonly __externref_table_dealloc: (a: number) => void;
64
+ readonly __wbindgen_start: () => void;
65
+ }
66
+
67
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
68
+
69
+ /**
70
+ * Instantiates the given `module`, which can either be bytes or
71
+ * a precompiled `WebAssembly.Module`.
72
+ *
73
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
74
+ *
75
+ * @returns {InitOutput}
76
+ */
77
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
78
+
79
+ /**
80
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
81
+ * for everything else, calls `WebAssembly.instantiate` directly.
82
+ *
83
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
84
+ *
85
+ * @returns {Promise<InitOutput>}
86
+ */
87
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
@@ -0,0 +1,352 @@
1
+ /* @ts-self-types="./build198x_web.d.ts" */
2
+
3
+ /**
4
+ * What to lint against, beyond the machine. Every setting is optional.
5
+ */
6
+ export class LintOptions {
7
+ __destroy_into_raw() {
8
+ const ptr = this.__wbg_ptr;
9
+ this.__wbg_ptr = 0;
10
+ LintOptionsFinalization.unregister(this);
11
+ return ptr;
12
+ }
13
+ free() {
14
+ const ptr = this.__destroy_into_raw();
15
+ wasm.__wbg_lintoptions_free(ptr, 0);
16
+ }
17
+ constructor() {
18
+ const ret = wasm.lintoptions_new();
19
+ this.__wbg_ptr = ret;
20
+ LintOptionsFinalization.register(this, this.__wbg_ptr, this);
21
+ return this;
22
+ }
23
+ /**
24
+ * A mask as RGBA: on the Spectrum, black where the sprite covers the
25
+ * background and white where it shows through.
26
+ * @param {Uint8Array | number[]} rgba
27
+ * @param {number} width
28
+ * @param {number} height
29
+ */
30
+ setMask(rgba, width, height) {
31
+ const val2 = +width;
32
+ const val3 = +height;
33
+ const ptr0 = passArray8ToWasm0(rgba, wasm.__wbindgen_malloc);
34
+ const len0 = WASM_VECTOR_LEN;
35
+ wasm.lintoptions_setMask(this.__wbg_ptr, ptr0, len0, val2, val3);
36
+ }
37
+ /**
38
+ * The screen mode, by name; the machine's first mode otherwise.
39
+ * @param {string} mode
40
+ */
41
+ setMode(mode) {
42
+ const ptr0 = passStringToWasm0(mode, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
43
+ const len0 = WASM_VECTOR_LEN;
44
+ wasm.lintoptions_setMode(this.__wbg_ptr, ptr0, len0);
45
+ }
46
+ /**
47
+ * The palette interpretation, by name; the machine's default otherwise.
48
+ * @param {string} palette
49
+ */
50
+ setPalette(palette) {
51
+ const ptr0 = passStringToWasm0(palette, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
52
+ const len0 = WASM_VECTOR_LEN;
53
+ wasm.lintoptions_setPalette(this.__wbg_ptr, ptr0, len0);
54
+ }
55
+ /**
56
+ * The palette index of the sprite's paper; black (0) otherwise. Every
57
+ * other colour is the sprite's shape, which the mask must cover.
58
+ * @param {number} index
59
+ */
60
+ setPaper(index) {
61
+ wasm.lintoptions_setPaper(this.__wbg_ptr, index);
62
+ }
63
+ /**
64
+ * The width and height the image must have.
65
+ * @param {number} width
66
+ * @param {number} height
67
+ */
68
+ setSize(width, height) {
69
+ wasm.lintoptions_setSize(this.__wbg_ptr, width, height);
70
+ }
71
+ }
72
+ if (Symbol.dispose) LintOptions.prototype[Symbol.dispose] = LintOptions.prototype.free;
73
+
74
+ /**
75
+ * Lints an RGBA image against `machine` and returns the findings as a JSON
76
+ * array, empty when the image passes.
77
+ *
78
+ * # Errors
79
+ *
80
+ * An unknown machine, mode, palette or paper; a machine the lint doesn't
81
+ * support yet; a buffer that isn't `width × height × 4` bytes; or an empty
82
+ * image.
83
+ * @param {string} machine
84
+ * @param {Uint8Array | number[]} rgba
85
+ * @param {number} width
86
+ * @param {number} height
87
+ * @param {LintOptions} options
88
+ * @returns {string}
89
+ */
90
+ export function lintImage(machine, rgba, width, height, options) {
91
+ let deferred6_0;
92
+ let deferred6_1;
93
+ try {
94
+ _assertClass(options, LintOptions);
95
+ const val3 = +width;
96
+ const val4 = +height;
97
+ let ptr0, len0, ptr1, len1;
98
+ try {
99
+ ptr0 = passStringToWasm0(machine, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
100
+ len0 = WASM_VECTOR_LEN;
101
+ ptr1 = passArray8ToWasm0(rgba, wasm.__wbindgen_malloc);
102
+ len1 = WASM_VECTOR_LEN;
103
+ } catch (e) {
104
+ if (ptr1) { wasm.__wbindgen_free(ptr1, len1 * 1, 1); }
105
+ if (ptr0) { wasm.__wbindgen_free(ptr0, len0 * 1, 1); }
106
+ throw e;
107
+ }
108
+ const ret = wasm.lintImage(ptr0, len0, ptr1, len1, val3, val4, options.__wbg_ptr);
109
+ var ptr5 = ret[0];
110
+ var len5 = ret[1];
111
+ if (ret[3]) {
112
+ ptr5 = 0; len5 = 0;
113
+ throw takeFromExternrefTable0(ret[2]);
114
+ }
115
+ deferred6_0 = ptr5;
116
+ deferred6_1 = len5;
117
+ return getStringFromWasm0(ptr5, len5);
118
+ } finally {
119
+ wasm.__wbindgen_free(deferred6_0, deferred6_1, 1);
120
+ }
121
+ }
122
+ function __wbg_get_imports() {
123
+ const import0 = {
124
+ __proto__: null,
125
+ __wbg_Error_208446807d0d1e30: function(arg0, arg1) {
126
+ const ret = Error(getStringFromWasm0(arg0, arg1));
127
+ return ret;
128
+ },
129
+ __wbg___wbindgen_throw_4c88d8bf264fd984: function(arg0, arg1) {
130
+ throw new Error(getStringFromWasm0(arg0, arg1));
131
+ },
132
+ __wbindgen_init_externref_table: function() {
133
+ const table = wasm.__wbindgen_externrefs;
134
+ table.set(0, undefined);
135
+ table.set(1024, undefined);
136
+ table.set(1025, null);
137
+ table.set(1026, true);
138
+ table.set(1027, false);
139
+ },
140
+ };
141
+ return {
142
+ __proto__: null,
143
+ "./build198x_web_bg.js": import0,
144
+ };
145
+ }
146
+
147
+ const LintOptionsFinalization = (typeof FinalizationRegistry === 'undefined')
148
+ ? { register: () => {}, unregister: () => {} }
149
+ : new FinalizationRegistry(ptr => wasm.__wbg_lintoptions_free(ptr, 1));
150
+
151
+ function _assertClass(instance, klass) {
152
+ if (!(instance instanceof klass)) {
153
+ throw new Error(`expected instance of ${klass.name}`);
154
+ }
155
+ }
156
+
157
+ function getStringFromWasm0(ptr, len) {
158
+ ptr = ptr >>> 0;
159
+ return decodeText(getUint8ArrayMemory0().subarray(ptr, ptr + len));
160
+ }
161
+
162
+ let cachedUint8ArrayMemory0 = null;
163
+ function getUint8ArrayMemory0() {
164
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
165
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
166
+ }
167
+ return cachedUint8ArrayMemory0;
168
+ }
169
+
170
+ function passArray8ToWasm0(arg, malloc) {
171
+ const ptr = malloc(arg.length * 1, 1) >>> 0;
172
+ getUint8ArrayMemory0().set(arg, ptr / 1);
173
+ WASM_VECTOR_LEN = arg.length;
174
+ return ptr;
175
+ }
176
+
177
+ function passStringToWasm0(arg, malloc, realloc) {
178
+ if (realloc === undefined) {
179
+ const buf = cachedTextEncoder.encode(arg);
180
+ const ptr = malloc(buf.length, 1) >>> 0;
181
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
182
+ WASM_VECTOR_LEN = buf.length;
183
+ return ptr;
184
+ }
185
+
186
+ let len = arg.length;
187
+ let ptr = malloc(len, 1) >>> 0;
188
+
189
+ const mem = getUint8ArrayMemory0();
190
+
191
+ let offset = 0;
192
+
193
+ for (; offset < len; offset++) {
194
+ const code = arg.charCodeAt(offset);
195
+ if (code > 0x7F) break;
196
+ mem[ptr + offset] = code;
197
+ }
198
+ if (offset !== len) {
199
+ if (offset !== 0) {
200
+ arg = arg.slice(offset);
201
+ }
202
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
203
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
204
+ const ret = cachedTextEncoder.encodeInto(arg, view);
205
+
206
+ offset += ret.written;
207
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
208
+ }
209
+
210
+ WASM_VECTOR_LEN = offset;
211
+ return ptr;
212
+ }
213
+
214
+ function takeFromExternrefTable0(idx) {
215
+ const value = wasm.__wbindgen_externrefs.get(idx);
216
+ wasm.__externref_table_dealloc(idx);
217
+ return value;
218
+ }
219
+
220
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true });
221
+ cachedTextDecoder.decode();
222
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
223
+ let numBytesDecoded = 0;
224
+ function decodeText(bytes) {
225
+ numBytesDecoded += bytes.length;
226
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
227
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true });
228
+ cachedTextDecoder.decode();
229
+ numBytesDecoded = bytes.length;
230
+ }
231
+ return cachedTextDecoder.decode(bytes);
232
+ }
233
+
234
+ const cachedTextEncoder = new TextEncoder();
235
+
236
+ if (!('encodeInto' in cachedTextEncoder)) {
237
+ cachedTextEncoder.encodeInto = function (arg, view) {
238
+ const buf = cachedTextEncoder.encode(arg);
239
+ view.set(buf);
240
+ return {
241
+ read: arg.length,
242
+ written: buf.length
243
+ };
244
+ };
245
+ }
246
+
247
+ let WASM_VECTOR_LEN = 0;
248
+
249
+ let wasmModule, wasmInstance, wasm;
250
+ function __wbg_finalize_init(instance, module) {
251
+ wasmInstance = instance;
252
+ wasm = instance.exports;
253
+ wasmModule = module;
254
+ cachedUint8ArrayMemory0 = null;
255
+ wasm.__wbindgen_start();
256
+ return wasm;
257
+ }
258
+
259
+ async function __wbg_load(module, imports) {
260
+ if (typeof Response === 'function' && module instanceof Response) {
261
+ if (!module.ok) {
262
+ throw new Error(`failed to fetch Wasm: ${module.status} ${module.statusText} fetching '${module.url}'`);
263
+ }
264
+
265
+ if (typeof WebAssembly.instantiateStreaming === 'function') {
266
+ try {
267
+ return await WebAssembly.instantiateStreaming(module, imports);
268
+ } catch (e) {
269
+ const validResponse = expectedResponseType(module.type);
270
+
271
+ if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
272
+ 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);
273
+
274
+ } else { throw e; }
275
+ }
276
+ }
277
+
278
+ const bytes = await module.arrayBuffer();
279
+ return await WebAssembly.instantiate(bytes, imports);
280
+ } else {
281
+ const instance = await WebAssembly.instantiate(module, imports);
282
+
283
+ if (instance instanceof WebAssembly.Instance) {
284
+ return { instance, module };
285
+ } else {
286
+ return instance;
287
+ }
288
+ }
289
+
290
+ function expectedResponseType(type) {
291
+ switch (type) {
292
+ case 'basic': case 'cors': case 'default': return true;
293
+ }
294
+ return false;
295
+ }
296
+ }
297
+
298
+ function initSync(module) {
299
+ if (wasm !== undefined) return wasm;
300
+
301
+
302
+ if (module !== undefined) {
303
+ if (Object.getPrototypeOf(module) === Object.prototype) {
304
+ ({module} = module)
305
+ } else {
306
+ console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
307
+ }
308
+ }
309
+
310
+ const imports = __wbg_get_imports();
311
+ if (!(module instanceof WebAssembly.Module)) {
312
+ module = new WebAssembly.Module(module);
313
+ }
314
+ const instance = new WebAssembly.Instance(module, imports);
315
+ return __wbg_finalize_init(instance, module);
316
+ }
317
+
318
+ let __wbg_init_promise;
319
+ async function __wbg_init(module_or_path) {
320
+ if (wasm !== undefined) return wasm;
321
+ if (__wbg_init_promise === undefined) {
322
+ __wbg_init_promise = __wbg_init_once(module_or_path);
323
+ __wbg_init_promise.catch(() => { __wbg_init_promise = undefined; });
324
+ }
325
+ return __wbg_init_promise;
326
+ }
327
+
328
+ async function __wbg_init_once(module_or_path) {
329
+
330
+ if (module_or_path !== undefined) {
331
+ if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
332
+ ({module_or_path} = module_or_path)
333
+ } else {
334
+ console.warn('using deprecated parameters for the initialization function; pass a single object instead')
335
+ }
336
+ }
337
+
338
+ if (module_or_path === undefined) {
339
+ module_or_path = new URL('build198x_web_bg.wasm', import.meta.url);
340
+ }
341
+ const imports = __wbg_get_imports();
342
+
343
+ if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
344
+ module_or_path = fetch(module_or_path);
345
+ }
346
+
347
+ const { instance, module } = await __wbg_load(await module_or_path, imports);
348
+
349
+ return __wbg_finalize_init(instance, module);
350
+ }
351
+
352
+ export { initSync, __wbg_init as default };
Binary file
package/package.json CHANGED
@@ -1,6 +1,23 @@
1
1
  {
2
2
  "name": "@build198x/web",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "type": "module",
4
+ "description": "Build198x's image lint, compiled to WebAssembly for the browser.",
5
+ "version": "0.4.2",
6
+ "license": "GPL-2.0-or-later",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/build198x/build198x"
10
+ },
11
+ "files": [
12
+ "build198x_web_bg.wasm",
13
+ "build198x_web.js",
14
+ "build198x_web.d.ts",
15
+ "README.md"
16
+ ],
17
+ "main": "build198x_web.js",
18
+ "homepage": "https://build198x.github.io",
19
+ "types": "build198x_web.d.ts",
20
+ "sideEffects": [
21
+ "./snippets/*"
22
+ ]
23
+ }