@pixagram/renderart 0.1.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/dist/index.mjs ADDED
@@ -0,0 +1,1819 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropNames = Object.getOwnPropertyNames;
3
+ var __esm = (fn, res) => function __init() {
4
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
5
+ };
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+
11
+ // pkg/renderart_wasm.js
12
+ var renderart_wasm_exports = {};
13
+ __export(renderart_wasm_exports, {
14
+ CrtConfig: () => CrtConfig,
15
+ HexConfig: () => HexConfig,
16
+ HexOrientation: () => HexOrientation,
17
+ UpscaleResult: () => UpscaleResult,
18
+ XbrzConfig: () => XbrzConfig,
19
+ crt_upscale: () => crt_upscale,
20
+ crt_upscale_config: () => crt_upscale_config,
21
+ default: () => renderart_wasm_default,
22
+ get_memory: () => get_memory,
23
+ hex_get_dimensions: () => hex_get_dimensions,
24
+ hex_upscale: () => hex_upscale,
25
+ hex_upscale_config: () => hex_upscale_config,
26
+ init: () => init,
27
+ initSync: () => initSync,
28
+ xbrz_upscale: () => xbrz_upscale,
29
+ xbrz_upscale_config: () => xbrz_upscale_config
30
+ });
31
+ function _assertClass(instance, klass) {
32
+ if (!(instance instanceof klass)) {
33
+ throw new Error(`expected instance of ${klass.name}`);
34
+ }
35
+ }
36
+ function getArrayU32FromWasm0(ptr, len) {
37
+ ptr = ptr >>> 0;
38
+ return getUint32ArrayMemory0().subarray(ptr / 4, ptr / 4 + len);
39
+ }
40
+ function getDataViewMemory0() {
41
+ if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || cachedDataViewMemory0.buffer.detached === void 0 && cachedDataViewMemory0.buffer !== wasm.memory.buffer) {
42
+ cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
43
+ }
44
+ return cachedDataViewMemory0;
45
+ }
46
+ function getStringFromWasm0(ptr, len) {
47
+ ptr = ptr >>> 0;
48
+ return decodeText(ptr, len);
49
+ }
50
+ function getUint32ArrayMemory0() {
51
+ if (cachedUint32ArrayMemory0 === null || cachedUint32ArrayMemory0.byteLength === 0) {
52
+ cachedUint32ArrayMemory0 = new Uint32Array(wasm.memory.buffer);
53
+ }
54
+ return cachedUint32ArrayMemory0;
55
+ }
56
+ function getUint8ArrayMemory0() {
57
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
58
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
59
+ }
60
+ return cachedUint8ArrayMemory0;
61
+ }
62
+ function passArray8ToWasm0(arg, malloc) {
63
+ const ptr = malloc(arg.length * 1, 1) >>> 0;
64
+ getUint8ArrayMemory0().set(arg, ptr / 1);
65
+ WASM_VECTOR_LEN = arg.length;
66
+ return ptr;
67
+ }
68
+ function passStringToWasm0(arg, malloc, realloc) {
69
+ if (realloc === void 0) {
70
+ const buf = cachedTextEncoder.encode(arg);
71
+ const ptr2 = malloc(buf.length, 1) >>> 0;
72
+ getUint8ArrayMemory0().subarray(ptr2, ptr2 + buf.length).set(buf);
73
+ WASM_VECTOR_LEN = buf.length;
74
+ return ptr2;
75
+ }
76
+ let len = arg.length;
77
+ let ptr = malloc(len, 1) >>> 0;
78
+ const mem = getUint8ArrayMemory0();
79
+ let offset = 0;
80
+ for (; offset < len; offset++) {
81
+ const code = arg.charCodeAt(offset);
82
+ if (code > 127) break;
83
+ mem[ptr + offset] = code;
84
+ }
85
+ if (offset !== len) {
86
+ if (offset !== 0) {
87
+ arg = arg.slice(offset);
88
+ }
89
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
90
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
91
+ const ret = cachedTextEncoder.encodeInto(arg, view);
92
+ offset += ret.written;
93
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
94
+ }
95
+ WASM_VECTOR_LEN = offset;
96
+ return ptr;
97
+ }
98
+ function takeFromExternrefTable0(idx) {
99
+ const value = wasm.__wbindgen_externrefs.get(idx);
100
+ wasm.__externref_table_dealloc(idx);
101
+ return value;
102
+ }
103
+ function decodeText(ptr, len) {
104
+ numBytesDecoded += len;
105
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
106
+ cachedTextDecoder = new TextDecoder("utf-8", { ignoreBOM: true, fatal: true });
107
+ cachedTextDecoder.decode();
108
+ numBytesDecoded = len;
109
+ }
110
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
111
+ }
112
+ function crt_upscale(input, src_w, src_h, scale) {
113
+ const ptr0 = passArray8ToWasm0(input, wasm.__wbindgen_malloc);
114
+ const len0 = WASM_VECTOR_LEN;
115
+ const ret = wasm.crt_upscale(ptr0, len0, src_w, src_h, scale);
116
+ if (ret[2]) {
117
+ throw takeFromExternrefTable0(ret[1]);
118
+ }
119
+ return UpscaleResult.__wrap(ret[0]);
120
+ }
121
+ function crt_upscale_config(input, src_w, src_h, scale, config) {
122
+ const ptr0 = passArray8ToWasm0(input, wasm.__wbindgen_malloc);
123
+ const len0 = WASM_VECTOR_LEN;
124
+ _assertClass(config, CrtConfig);
125
+ const ret = wasm.crt_upscale_config(ptr0, len0, src_w, src_h, scale, config.__wbg_ptr);
126
+ if (ret[2]) {
127
+ throw takeFromExternrefTable0(ret[1]);
128
+ }
129
+ return UpscaleResult.__wrap(ret[0]);
130
+ }
131
+ function get_memory() {
132
+ const ret = wasm.get_memory();
133
+ return ret;
134
+ }
135
+ function hex_get_dimensions(src_w, src_h, scale, orientation) {
136
+ const ret = wasm.hex_get_dimensions(src_w, src_h, scale, orientation);
137
+ var v1 = getArrayU32FromWasm0(ret[0], ret[1]).slice();
138
+ wasm.__wbindgen_free(ret[0], ret[1] * 4, 4);
139
+ return v1;
140
+ }
141
+ function hex_upscale(input, src_w, src_h, scale) {
142
+ const ptr0 = passArray8ToWasm0(input, wasm.__wbindgen_malloc);
143
+ const len0 = WASM_VECTOR_LEN;
144
+ const ret = wasm.hex_upscale(ptr0, len0, src_w, src_h, scale);
145
+ if (ret[2]) {
146
+ throw takeFromExternrefTable0(ret[1]);
147
+ }
148
+ return UpscaleResult.__wrap(ret[0]);
149
+ }
150
+ function hex_upscale_config(input, src_w, src_h, scale, config) {
151
+ const ptr0 = passArray8ToWasm0(input, wasm.__wbindgen_malloc);
152
+ const len0 = WASM_VECTOR_LEN;
153
+ _assertClass(config, HexConfig);
154
+ const ret = wasm.hex_upscale_config(ptr0, len0, src_w, src_h, scale, config.__wbg_ptr);
155
+ if (ret[2]) {
156
+ throw takeFromExternrefTable0(ret[1]);
157
+ }
158
+ return UpscaleResult.__wrap(ret[0]);
159
+ }
160
+ function init() {
161
+ wasm.init();
162
+ }
163
+ function xbrz_upscale(input, src_w, src_h, scale) {
164
+ const ptr0 = passArray8ToWasm0(input, wasm.__wbindgen_malloc);
165
+ const len0 = WASM_VECTOR_LEN;
166
+ const ret = wasm.xbrz_upscale(ptr0, len0, src_w, src_h, scale);
167
+ if (ret[2]) {
168
+ throw takeFromExternrefTable0(ret[1]);
169
+ }
170
+ return UpscaleResult.__wrap(ret[0]);
171
+ }
172
+ function xbrz_upscale_config(input, src_w, src_h, scale, config) {
173
+ const ptr0 = passArray8ToWasm0(input, wasm.__wbindgen_malloc);
174
+ const len0 = WASM_VECTOR_LEN;
175
+ _assertClass(config, XbrzConfig);
176
+ const ret = wasm.xbrz_upscale_config(ptr0, len0, src_w, src_h, scale, config.__wbg_ptr);
177
+ if (ret[2]) {
178
+ throw takeFromExternrefTable0(ret[1]);
179
+ }
180
+ return UpscaleResult.__wrap(ret[0]);
181
+ }
182
+ async function __wbg_load(module, imports) {
183
+ if (typeof Response === "function" && module instanceof Response) {
184
+ if (typeof WebAssembly.instantiateStreaming === "function") {
185
+ try {
186
+ return await WebAssembly.instantiateStreaming(module, imports);
187
+ } catch (e) {
188
+ const validResponse = module.ok && EXPECTED_RESPONSE_TYPES.has(module.type);
189
+ if (validResponse && module.headers.get("Content-Type") !== "application/wasm") {
190
+ 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);
191
+ } else {
192
+ throw e;
193
+ }
194
+ }
195
+ }
196
+ const bytes = await module.arrayBuffer();
197
+ return await WebAssembly.instantiate(bytes, imports);
198
+ } else {
199
+ const instance = await WebAssembly.instantiate(module, imports);
200
+ if (instance instanceof WebAssembly.Instance) {
201
+ return { instance, module };
202
+ } else {
203
+ return instance;
204
+ }
205
+ }
206
+ }
207
+ function __wbg_get_imports() {
208
+ const imports = {};
209
+ imports.wbg = {};
210
+ imports.wbg.__wbg___wbindgen_memory_a342e963fbcabd68 = function() {
211
+ const ret = wasm.memory;
212
+ return ret;
213
+ };
214
+ imports.wbg.__wbg___wbindgen_throw_dd24417ed36fc46e = function(arg0, arg1) {
215
+ throw new Error(getStringFromWasm0(arg0, arg1));
216
+ };
217
+ imports.wbg.__wbg_error_7534b8e9a36f1ab4 = function(arg0, arg1) {
218
+ let deferred0_0;
219
+ let deferred0_1;
220
+ try {
221
+ deferred0_0 = arg0;
222
+ deferred0_1 = arg1;
223
+ console.error(getStringFromWasm0(arg0, arg1));
224
+ } finally {
225
+ wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
226
+ }
227
+ };
228
+ imports.wbg.__wbg_new_8a6f238a6ece86ea = function() {
229
+ const ret = new Error();
230
+ return ret;
231
+ };
232
+ imports.wbg.__wbg_stack_0ed75d68575b0f3c = function(arg0, arg1) {
233
+ const ret = arg1.stack;
234
+ const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
235
+ const len1 = WASM_VECTOR_LEN;
236
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
237
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
238
+ };
239
+ imports.wbg.__wbindgen_cast_2241b6af4c4b2941 = function(arg0, arg1) {
240
+ const ret = getStringFromWasm0(arg0, arg1);
241
+ return ret;
242
+ };
243
+ imports.wbg.__wbindgen_init_externref_table = function() {
244
+ const table = wasm.__wbindgen_externrefs;
245
+ const offset = table.grow(4);
246
+ table.set(0, void 0);
247
+ table.set(offset + 0, void 0);
248
+ table.set(offset + 1, null);
249
+ table.set(offset + 2, true);
250
+ table.set(offset + 3, false);
251
+ };
252
+ return imports;
253
+ }
254
+ function __wbg_finalize_init(instance, module) {
255
+ wasm = instance.exports;
256
+ __wbg_init.__wbindgen_wasm_module = module;
257
+ cachedDataViewMemory0 = null;
258
+ cachedUint32ArrayMemory0 = null;
259
+ cachedUint8ArrayMemory0 = null;
260
+ wasm.__wbindgen_start();
261
+ return wasm;
262
+ }
263
+ function initSync(module) {
264
+ if (wasm !== void 0) return wasm;
265
+ if (typeof module !== "undefined") {
266
+ if (Object.getPrototypeOf(module) === Object.prototype) {
267
+ ({ module } = module);
268
+ } else {
269
+ console.warn("using deprecated parameters for `initSync()`; pass a single object instead");
270
+ }
271
+ }
272
+ const imports = __wbg_get_imports();
273
+ if (!(module instanceof WebAssembly.Module)) {
274
+ module = new WebAssembly.Module(module);
275
+ }
276
+ const instance = new WebAssembly.Instance(module, imports);
277
+ return __wbg_finalize_init(instance, module);
278
+ }
279
+ async function __wbg_init(module_or_path) {
280
+ if (wasm !== void 0) return wasm;
281
+ if (typeof module_or_path !== "undefined") {
282
+ if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
283
+ ({ module_or_path } = module_or_path);
284
+ } else {
285
+ console.warn("using deprecated parameters for the initialization function; pass a single object instead");
286
+ }
287
+ }
288
+ if (typeof module_or_path === "undefined") {
289
+ module_or_path = new URL("renderart_wasm_bg.wasm", import.meta.url);
290
+ }
291
+ const imports = __wbg_get_imports();
292
+ if (typeof module_or_path === "string" || typeof Request === "function" && module_or_path instanceof Request || typeof URL === "function" && module_or_path instanceof URL) {
293
+ module_or_path = fetch(module_or_path);
294
+ }
295
+ const { instance, module } = await __wbg_load(await module_or_path, imports);
296
+ return __wbg_finalize_init(instance, module);
297
+ }
298
+ var wasm, cachedDataViewMemory0, cachedUint32ArrayMemory0, cachedUint8ArrayMemory0, cachedTextDecoder, MAX_SAFARI_DECODE_BYTES, numBytesDecoded, cachedTextEncoder, WASM_VECTOR_LEN, CrtConfigFinalization, HexConfigFinalization, UpscaleResultFinalization, XbrzConfigFinalization, CrtConfig, HexConfig, HexOrientation, UpscaleResult, XbrzConfig, EXPECTED_RESPONSE_TYPES, renderart_wasm_default;
299
+ var init_renderart_wasm = __esm({
300
+ "pkg/renderart_wasm.js"() {
301
+ cachedDataViewMemory0 = null;
302
+ cachedUint32ArrayMemory0 = null;
303
+ cachedUint8ArrayMemory0 = null;
304
+ cachedTextDecoder = new TextDecoder("utf-8", { ignoreBOM: true, fatal: true });
305
+ cachedTextDecoder.decode();
306
+ MAX_SAFARI_DECODE_BYTES = 2146435072;
307
+ numBytesDecoded = 0;
308
+ cachedTextEncoder = new TextEncoder();
309
+ if (!("encodeInto" in cachedTextEncoder)) {
310
+ cachedTextEncoder.encodeInto = function(arg, view) {
311
+ const buf = cachedTextEncoder.encode(arg);
312
+ view.set(buf);
313
+ return {
314
+ read: arg.length,
315
+ written: buf.length
316
+ };
317
+ };
318
+ }
319
+ WASM_VECTOR_LEN = 0;
320
+ CrtConfigFinalization = typeof FinalizationRegistry === "undefined" ? { register: () => {
321
+ }, unregister: () => {
322
+ } } : new FinalizationRegistry((ptr) => wasm.__wbg_crtconfig_free(ptr >>> 0, 1));
323
+ HexConfigFinalization = typeof FinalizationRegistry === "undefined" ? { register: () => {
324
+ }, unregister: () => {
325
+ } } : new FinalizationRegistry((ptr) => wasm.__wbg_hexconfig_free(ptr >>> 0, 1));
326
+ UpscaleResultFinalization = typeof FinalizationRegistry === "undefined" ? { register: () => {
327
+ }, unregister: () => {
328
+ } } : new FinalizationRegistry((ptr) => wasm.__wbg_upscaleresult_free(ptr >>> 0, 1));
329
+ XbrzConfigFinalization = typeof FinalizationRegistry === "undefined" ? { register: () => {
330
+ }, unregister: () => {
331
+ } } : new FinalizationRegistry((ptr) => wasm.__wbg_xbrzconfig_free(ptr >>> 0, 1));
332
+ CrtConfig = class _CrtConfig {
333
+ static __wrap(ptr) {
334
+ ptr = ptr >>> 0;
335
+ const obj = Object.create(_CrtConfig.prototype);
336
+ obj.__wbg_ptr = ptr;
337
+ CrtConfigFinalization.register(obj, obj.__wbg_ptr, obj);
338
+ return obj;
339
+ }
340
+ __destroy_into_raw() {
341
+ const ptr = this.__wbg_ptr;
342
+ this.__wbg_ptr = 0;
343
+ CrtConfigFinalization.unregister(this);
344
+ return ptr;
345
+ }
346
+ free() {
347
+ const ptr = this.__destroy_into_raw();
348
+ wasm.__wbg_crtconfig_free(ptr, 0);
349
+ }
350
+ /**
351
+ * Horizontal warp intensity (0.0 - 0.1)
352
+ * @returns {number}
353
+ */
354
+ get warp_x() {
355
+ const ret = wasm.__wbg_get_crtconfig_warp_x(this.__wbg_ptr);
356
+ return ret;
357
+ }
358
+ /**
359
+ * Horizontal warp intensity (0.0 - 0.1)
360
+ * @param {number} arg0
361
+ */
362
+ set warp_x(arg0) {
363
+ wasm.__wbg_set_crtconfig_warp_x(this.__wbg_ptr, arg0);
364
+ }
365
+ /**
366
+ * Vertical warp intensity (0.0 - 0.1)
367
+ * @returns {number}
368
+ */
369
+ get warp_y() {
370
+ const ret = wasm.__wbg_get_crtconfig_warp_y(this.__wbg_ptr);
371
+ return ret;
372
+ }
373
+ /**
374
+ * Vertical warp intensity (0.0 - 0.1)
375
+ * @param {number} arg0
376
+ */
377
+ set warp_y(arg0) {
378
+ wasm.__wbg_set_crtconfig_warp_y(this.__wbg_ptr, arg0);
379
+ }
380
+ /**
381
+ * Scanline hardness (-10.0 to 0.0, more negative = sharper)
382
+ * @returns {number}
383
+ */
384
+ get scan_hardness() {
385
+ const ret = wasm.__wbg_get_crtconfig_scan_hardness(this.__wbg_ptr);
386
+ return ret;
387
+ }
388
+ /**
389
+ * Scanline hardness (-10.0 to 0.0, more negative = sharper)
390
+ * @param {number} arg0
391
+ */
392
+ set scan_hardness(arg0) {
393
+ wasm.__wbg_set_crtconfig_scan_hardness(this.__wbg_ptr, arg0);
394
+ }
395
+ /**
396
+ * Scanline opacity (0.0 - 1.0)
397
+ * @returns {number}
398
+ */
399
+ get scan_opacity() {
400
+ const ret = wasm.__wbg_get_crtconfig_scan_opacity(this.__wbg_ptr);
401
+ return ret;
402
+ }
403
+ /**
404
+ * Scanline opacity (0.0 - 1.0)
405
+ * @param {number} arg0
406
+ */
407
+ set scan_opacity(arg0) {
408
+ wasm.__wbg_set_crtconfig_scan_opacity(this.__wbg_ptr, arg0);
409
+ }
410
+ /**
411
+ * Shadow mask opacity (0.0 - 1.0)
412
+ * @returns {number}
413
+ */
414
+ get mask_opacity() {
415
+ const ret = wasm.__wbg_get_crtconfig_mask_opacity(this.__wbg_ptr);
416
+ return ret;
417
+ }
418
+ /**
419
+ * Shadow mask opacity (0.0 - 1.0)
420
+ * @param {number} arg0
421
+ */
422
+ set mask_opacity(arg0) {
423
+ wasm.__wbg_set_crtconfig_mask_opacity(this.__wbg_ptr, arg0);
424
+ }
425
+ /**
426
+ * Enable barrel distortion
427
+ * @returns {boolean}
428
+ */
429
+ get enable_warp() {
430
+ const ret = wasm.__wbg_get_crtconfig_enable_warp(this.__wbg_ptr);
431
+ return ret !== 0;
432
+ }
433
+ /**
434
+ * Enable barrel distortion
435
+ * @param {boolean} arg0
436
+ */
437
+ set enable_warp(arg0) {
438
+ wasm.__wbg_set_crtconfig_enable_warp(this.__wbg_ptr, arg0);
439
+ }
440
+ /**
441
+ * Enable scanlines
442
+ * @returns {boolean}
443
+ */
444
+ get enable_scanlines() {
445
+ const ret = wasm.__wbg_get_crtconfig_enable_scanlines(this.__wbg_ptr);
446
+ return ret !== 0;
447
+ }
448
+ /**
449
+ * Enable scanlines
450
+ * @param {boolean} arg0
451
+ */
452
+ set enable_scanlines(arg0) {
453
+ wasm.__wbg_set_crtconfig_enable_scanlines(this.__wbg_ptr, arg0);
454
+ }
455
+ /**
456
+ * Enable shadow mask
457
+ * @returns {boolean}
458
+ */
459
+ get enable_mask() {
460
+ const ret = wasm.__wbg_get_crtconfig_enable_mask(this.__wbg_ptr);
461
+ return ret !== 0;
462
+ }
463
+ /**
464
+ * Enable shadow mask
465
+ * @param {boolean} arg0
466
+ */
467
+ set enable_mask(arg0) {
468
+ wasm.__wbg_set_crtconfig_enable_mask(this.__wbg_ptr, arg0);
469
+ }
470
+ constructor() {
471
+ const ret = wasm.crtconfig_new();
472
+ this.__wbg_ptr = ret >>> 0;
473
+ CrtConfigFinalization.register(this, this.__wbg_ptr, this);
474
+ return this;
475
+ }
476
+ /**
477
+ * Create preset: Authentic CRT look
478
+ * @returns {CrtConfig}
479
+ */
480
+ static preset_authentic() {
481
+ const ret = wasm.crtconfig_preset_authentic();
482
+ return _CrtConfig.__wrap(ret);
483
+ }
484
+ /**
485
+ * Create preset: Subtle modern look
486
+ * @returns {CrtConfig}
487
+ */
488
+ static preset_subtle() {
489
+ const ret = wasm.crtconfig_preset_subtle();
490
+ return _CrtConfig.__wrap(ret);
491
+ }
492
+ /**
493
+ * Create preset: Flat screen (no curvature)
494
+ * @returns {CrtConfig}
495
+ */
496
+ static preset_flat() {
497
+ const ret = wasm.crtconfig_preset_flat();
498
+ return _CrtConfig.__wrap(ret);
499
+ }
500
+ };
501
+ if (Symbol.dispose) CrtConfig.prototype[Symbol.dispose] = CrtConfig.prototype.free;
502
+ HexConfig = class _HexConfig {
503
+ static __wrap(ptr) {
504
+ ptr = ptr >>> 0;
505
+ const obj = Object.create(_HexConfig.prototype);
506
+ obj.__wbg_ptr = ptr;
507
+ HexConfigFinalization.register(obj, obj.__wbg_ptr, obj);
508
+ return obj;
509
+ }
510
+ __destroy_into_raw() {
511
+ const ptr = this.__wbg_ptr;
512
+ this.__wbg_ptr = 0;
513
+ HexConfigFinalization.unregister(this);
514
+ return ptr;
515
+ }
516
+ free() {
517
+ const ptr = this.__destroy_into_raw();
518
+ wasm.__wbg_hexconfig_free(ptr, 0);
519
+ }
520
+ /**
521
+ * Hexagon orientation
522
+ * @returns {HexOrientation}
523
+ */
524
+ get orientation() {
525
+ const ret = wasm.__wbg_get_hexconfig_orientation(this.__wbg_ptr);
526
+ return ret;
527
+ }
528
+ /**
529
+ * Hexagon orientation
530
+ * @param {HexOrientation} arg0
531
+ */
532
+ set orientation(arg0) {
533
+ wasm.__wbg_set_hexconfig_orientation(this.__wbg_ptr, arg0);
534
+ }
535
+ /**
536
+ * Draw hexagon borders
537
+ * @returns {boolean}
538
+ */
539
+ get draw_borders() {
540
+ const ret = wasm.__wbg_get_hexconfig_draw_borders(this.__wbg_ptr);
541
+ return ret !== 0;
542
+ }
543
+ /**
544
+ * Draw hexagon borders
545
+ * @param {boolean} arg0
546
+ */
547
+ set draw_borders(arg0) {
548
+ wasm.__wbg_set_hexconfig_draw_borders(this.__wbg_ptr, arg0);
549
+ }
550
+ /**
551
+ * Border color (RGBA packed as u32: 0xRRGGBBAA)
552
+ * @returns {number}
553
+ */
554
+ get border_color() {
555
+ const ret = wasm.__wbg_get_hexconfig_border_color(this.__wbg_ptr);
556
+ return ret >>> 0;
557
+ }
558
+ /**
559
+ * Border color (RGBA packed as u32: 0xRRGGBBAA)
560
+ * @param {number} arg0
561
+ */
562
+ set border_color(arg0) {
563
+ wasm.__wbg_set_hexconfig_border_color(this.__wbg_ptr, arg0);
564
+ }
565
+ /**
566
+ * Border thickness in pixels
567
+ * @returns {number}
568
+ */
569
+ get border_thickness() {
570
+ const ret = wasm.__wbg_get_hexconfig_border_thickness(this.__wbg_ptr);
571
+ return ret >>> 0;
572
+ }
573
+ /**
574
+ * Border thickness in pixels
575
+ * @param {number} arg0
576
+ */
577
+ set border_thickness(arg0) {
578
+ wasm.__wbg_set_hexconfig_border_thickness(this.__wbg_ptr, arg0);
579
+ }
580
+ /**
581
+ * Background color (RGBA packed as u32)
582
+ * @returns {number}
583
+ */
584
+ get background_color() {
585
+ const ret = wasm.__wbg_get_hexconfig_background_color(this.__wbg_ptr);
586
+ return ret >>> 0;
587
+ }
588
+ /**
589
+ * Background color (RGBA packed as u32)
590
+ * @param {number} arg0
591
+ */
592
+ set background_color(arg0) {
593
+ wasm.__wbg_set_hexconfig_background_color(this.__wbg_ptr, arg0);
594
+ }
595
+ constructor() {
596
+ const ret = wasm.hexconfig_new();
597
+ this.__wbg_ptr = ret >>> 0;
598
+ HexConfigFinalization.register(this, this.__wbg_ptr, this);
599
+ return this;
600
+ }
601
+ /**
602
+ * Create preset with borders
603
+ * @returns {HexConfig}
604
+ */
605
+ static preset_bordered() {
606
+ const ret = wasm.hexconfig_preset_bordered();
607
+ return _HexConfig.__wrap(ret);
608
+ }
609
+ /**
610
+ * Create preset for pointy-top hexagons
611
+ * @returns {HexConfig}
612
+ */
613
+ static preset_pointy() {
614
+ const ret = wasm.hexconfig_preset_pointy();
615
+ return _HexConfig.__wrap(ret);
616
+ }
617
+ };
618
+ if (Symbol.dispose) HexConfig.prototype[Symbol.dispose] = HexConfig.prototype.free;
619
+ HexOrientation = Object.freeze({
620
+ /**
621
+ * Flat edge at top, vertices on left/right
622
+ */
623
+ FlatTop: 0,
624
+ "0": "FlatTop",
625
+ /**
626
+ * Vertex at top, flat edges on left/right
627
+ */
628
+ PointyTop: 1,
629
+ "1": "PointyTop"
630
+ });
631
+ UpscaleResult = class _UpscaleResult {
632
+ static __wrap(ptr) {
633
+ ptr = ptr >>> 0;
634
+ const obj = Object.create(_UpscaleResult.prototype);
635
+ obj.__wbg_ptr = ptr;
636
+ UpscaleResultFinalization.register(obj, obj.__wbg_ptr, obj);
637
+ return obj;
638
+ }
639
+ __destroy_into_raw() {
640
+ const ptr = this.__wbg_ptr;
641
+ this.__wbg_ptr = 0;
642
+ UpscaleResultFinalization.unregister(this);
643
+ return ptr;
644
+ }
645
+ free() {
646
+ const ptr = this.__destroy_into_raw();
647
+ wasm.__wbg_upscaleresult_free(ptr, 0);
648
+ }
649
+ /**
650
+ * Pointer to the output buffer in WASM memory
651
+ * @returns {number}
652
+ */
653
+ get ptr() {
654
+ const ret = wasm.upscaleresult_ptr(this.__wbg_ptr);
655
+ return ret >>> 0;
656
+ }
657
+ /**
658
+ * Length of the output buffer in bytes
659
+ * @returns {number}
660
+ */
661
+ get len() {
662
+ const ret = wasm.upscaleresult_len(this.__wbg_ptr);
663
+ return ret >>> 0;
664
+ }
665
+ /**
666
+ * Output image width
667
+ * @returns {number}
668
+ */
669
+ get width() {
670
+ const ret = wasm.upscaleresult_width(this.__wbg_ptr);
671
+ return ret >>> 0;
672
+ }
673
+ /**
674
+ * Output image height
675
+ * @returns {number}
676
+ */
677
+ get height() {
678
+ const ret = wasm.upscaleresult_height(this.__wbg_ptr);
679
+ return ret >>> 0;
680
+ }
681
+ };
682
+ if (Symbol.dispose) UpscaleResult.prototype[Symbol.dispose] = UpscaleResult.prototype.free;
683
+ XbrzConfig = class _XbrzConfig {
684
+ static __wrap(ptr) {
685
+ ptr = ptr >>> 0;
686
+ const obj = Object.create(_XbrzConfig.prototype);
687
+ obj.__wbg_ptr = ptr;
688
+ XbrzConfigFinalization.register(obj, obj.__wbg_ptr, obj);
689
+ return obj;
690
+ }
691
+ __destroy_into_raw() {
692
+ const ptr = this.__wbg_ptr;
693
+ this.__wbg_ptr = 0;
694
+ XbrzConfigFinalization.unregister(this);
695
+ return ptr;
696
+ }
697
+ free() {
698
+ const ptr = this.__destroy_into_raw();
699
+ wasm.__wbg_xbrzconfig_free(ptr, 0);
700
+ }
701
+ /**
702
+ * Luminance weight for edge detection (0.0 - 1.0)
703
+ * @returns {number}
704
+ */
705
+ get luminance_weight() {
706
+ const ret = wasm.__wbg_get_crtconfig_warp_x(this.__wbg_ptr);
707
+ return ret;
708
+ }
709
+ /**
710
+ * Luminance weight for edge detection (0.0 - 1.0)
711
+ * @param {number} arg0
712
+ */
713
+ set luminance_weight(arg0) {
714
+ wasm.__wbg_set_crtconfig_warp_x(this.__wbg_ptr, arg0);
715
+ }
716
+ /**
717
+ * Equal color tolerance (0 - 50)
718
+ * @returns {number}
719
+ */
720
+ get equal_color_tolerance() {
721
+ const ret = wasm.__wbg_get_hexconfig_border_thickness(this.__wbg_ptr);
722
+ return ret >>> 0;
723
+ }
724
+ /**
725
+ * Equal color tolerance (0 - 50)
726
+ * @param {number} arg0
727
+ */
728
+ set equal_color_tolerance(arg0) {
729
+ wasm.__wbg_set_hexconfig_border_thickness(this.__wbg_ptr, arg0);
730
+ }
731
+ /**
732
+ * Dominant direction threshold (3.5 - 6.0)
733
+ * @returns {number}
734
+ */
735
+ get dominant_direction_threshold() {
736
+ const ret = wasm.__wbg_get_crtconfig_scan_hardness(this.__wbg_ptr);
737
+ return ret;
738
+ }
739
+ /**
740
+ * Dominant direction threshold (3.5 - 6.0)
741
+ * @param {number} arg0
742
+ */
743
+ set dominant_direction_threshold(arg0) {
744
+ wasm.__wbg_set_crtconfig_scan_hardness(this.__wbg_ptr, arg0);
745
+ }
746
+ /**
747
+ * Steep direction threshold (2.0 - 3.0)
748
+ * @returns {number}
749
+ */
750
+ get steep_direction_threshold() {
751
+ const ret = wasm.__wbg_get_crtconfig_scan_opacity(this.__wbg_ptr);
752
+ return ret;
753
+ }
754
+ /**
755
+ * Steep direction threshold (2.0 - 3.0)
756
+ * @param {number} arg0
757
+ */
758
+ set steep_direction_threshold(arg0) {
759
+ wasm.__wbg_set_crtconfig_scan_opacity(this.__wbg_ptr, arg0);
760
+ }
761
+ constructor() {
762
+ const ret = wasm.xbrzconfig_new();
763
+ this.__wbg_ptr = ret >>> 0;
764
+ XbrzConfigFinalization.register(this, this.__wbg_ptr, this);
765
+ return this;
766
+ }
767
+ /**
768
+ * Preset for sharp edges
769
+ * @returns {XbrzConfig}
770
+ */
771
+ static preset_sharp() {
772
+ const ret = wasm.xbrzconfig_preset_sharp();
773
+ return _XbrzConfig.__wrap(ret);
774
+ }
775
+ /**
776
+ * Preset for smooth output
777
+ * @returns {XbrzConfig}
778
+ */
779
+ static preset_smooth() {
780
+ const ret = wasm.xbrzconfig_preset_smooth();
781
+ return _XbrzConfig.__wrap(ret);
782
+ }
783
+ };
784
+ if (Symbol.dispose) XbrzConfig.prototype[Symbol.dispose] = XbrzConfig.prototype.free;
785
+ EXPECTED_RESPONSE_TYPES = /* @__PURE__ */ new Set(["basic", "cors", "default"]);
786
+ renderart_wasm_default = __wbg_init;
787
+ }
788
+ });
789
+
790
+ // src/crt-gpu.ts
791
+ var VERTEX_SHADER = `#version 300 es
792
+ layout(location = 0) in vec2 position;
793
+ out vec2 vUv;
794
+
795
+ void main() {
796
+ vUv = position * 0.5 + 0.5;
797
+ gl_Position = vec4(position, 0.0, 1.0);
798
+ }`;
799
+ var FRAGMENT_SHADER = `#version 300 es
800
+ precision highp float;
801
+
802
+ uniform sampler2D uTex;
803
+ uniform vec2 uRes;
804
+ uniform vec2 uWarp;
805
+ uniform float uScanHardness;
806
+ uniform float uScanOpacity;
807
+ uniform float uMaskOpacity;
808
+ uniform int uEnableWarp;
809
+ uniform int uEnableScanlines;
810
+ uniform int uEnableMask;
811
+
812
+ in vec2 vUv;
813
+ out vec4 outColor;
814
+
815
+ // Gamma 2.0 approximation
816
+ vec3 toLinear(vec3 c) { return c * c; }
817
+ vec3 toSrgb(vec3 c) { return sqrt(c); }
818
+
819
+ vec2 warp(vec2 uv) {
820
+ if (uEnableWarp == 0) return uv;
821
+ vec2 dc = abs(0.5 - uv);
822
+ vec2 dc2 = dc * dc;
823
+ uv.x -= 0.5; uv.x *= 1.0 + (dc2.y * (0.3 * uWarp.x)); uv.x += 0.5;
824
+ uv.y -= 0.5; uv.y *= 1.0 + (dc2.x * (0.4 * uWarp.y)); uv.y += 0.5;
825
+ return uv;
826
+ }
827
+
828
+ float scanline(float y, float sourceHeight) {
829
+ if (uEnableScanlines == 0) return 1.0;
830
+ float v = fract(y * sourceHeight);
831
+ float d = abs(v - 0.5);
832
+ float line = exp(d * d * uScanHardness);
833
+ return mix(1.0, line, uScanOpacity);
834
+ }
835
+
836
+ vec3 mask(vec2 pos) {
837
+ if (uEnableMask == 0) return vec3(1.0);
838
+ float x = fract(pos.x / 6.0);
839
+ vec3 m = vec3(1.0);
840
+ float step1 = 0.333;
841
+ float step2 = 0.666;
842
+
843
+ m.r = step(0.0, x) - step(step1, x);
844
+ m.g = step(step1, x) - step(step2, x);
845
+ m.b = step(step2, x) - step(1.0, x);
846
+
847
+ return mix(vec3(1.0), m, uMaskOpacity);
848
+ }
849
+
850
+ void main() {
851
+ // 1. Geometry
852
+ vec2 uv = warp(vUv);
853
+
854
+ // 2. Bounds Check
855
+ if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) {
856
+ outColor = vec4(0.0);
857
+ return;
858
+ }
859
+
860
+ // 3. Texture Sample
861
+ vec4 texSample = texture(uTex, uv);
862
+
863
+ if (texSample.a == 0.0) {
864
+ outColor = vec4(0.0);
865
+ return;
866
+ }
867
+
868
+ vec3 linearColor = toLinear(texSample.rgb);
869
+
870
+ // 4. CRT Effects
871
+ ivec2 texSize = textureSize(uTex, 0);
872
+
873
+ // Smart Bloom
874
+ float luma = dot(linearColor, vec3(0.299, 0.587, 0.114));
875
+ float bloom = luma * 0.7;
876
+
877
+ // Calculate patterns
878
+ float scan = scanline(uv.y, float(texSize.y));
879
+ vec3 m = mask(gl_FragCoord.xy);
880
+
881
+ // Apply effects
882
+ vec3 effects = m * scan;
883
+ vec3 finalRGB = linearColor * mix(effects, vec3(1.0), bloom);
884
+
885
+ // 5. Output
886
+ outColor = vec4(toSrgb(finalRGB), texSample.a);
887
+ }`;
888
+ var CrtGpuRenderer = class _CrtGpuRenderer {
889
+ constructor() {
890
+ this.gl = null;
891
+ this.canvas = null;
892
+ this.program = null;
893
+ this.texture = null;
894
+ this.uniforms = {};
895
+ this.initialized = false;
896
+ this.currentCanvasSize = { width: 0, height: 0 };
897
+ this.currentTexSize = { width: 0, height: 0 };
898
+ }
899
+ /** Create a new CRT GPU renderer */
900
+ static create() {
901
+ const renderer = new _CrtGpuRenderer();
902
+ renderer.init();
903
+ return renderer;
904
+ }
905
+ init() {
906
+ if (typeof OffscreenCanvas === "undefined") {
907
+ throw new Error("OffscreenCanvas not supported");
908
+ }
909
+ this.canvas = new OffscreenCanvas(1, 1);
910
+ this.gl = this.canvas.getContext("webgl2", {
911
+ alpha: true,
912
+ premultipliedAlpha: false,
913
+ desynchronized: true,
914
+ powerPreference: "high-performance",
915
+ antialias: false
916
+ });
917
+ if (!this.gl) {
918
+ throw new Error("WebGL2 not supported");
919
+ }
920
+ const gl = this.gl;
921
+ const vs = this.createShader(gl.VERTEX_SHADER, VERTEX_SHADER);
922
+ const fs = this.createShader(gl.FRAGMENT_SHADER, FRAGMENT_SHADER);
923
+ this.program = gl.createProgram();
924
+ gl.attachShader(this.program, vs);
925
+ gl.attachShader(this.program, fs);
926
+ gl.linkProgram(this.program);
927
+ if (!gl.getProgramParameter(this.program, gl.LINK_STATUS)) {
928
+ throw new Error("Shader program link failed: " + gl.getProgramInfoLog(this.program));
929
+ }
930
+ gl.useProgram(this.program);
931
+ this.uniforms = {
932
+ uTex: gl.getUniformLocation(this.program, "uTex"),
933
+ uRes: gl.getUniformLocation(this.program, "uRes"),
934
+ uWarp: gl.getUniformLocation(this.program, "uWarp"),
935
+ uScanHardness: gl.getUniformLocation(this.program, "uScanHardness"),
936
+ uScanOpacity: gl.getUniformLocation(this.program, "uScanOpacity"),
937
+ uMaskOpacity: gl.getUniformLocation(this.program, "uMaskOpacity"),
938
+ uEnableWarp: gl.getUniformLocation(this.program, "uEnableWarp"),
939
+ uEnableScanlines: gl.getUniformLocation(this.program, "uEnableScanlines"),
940
+ uEnableMask: gl.getUniformLocation(this.program, "uEnableMask")
941
+ };
942
+ gl.uniform1i(this.uniforms.uTex, 0);
943
+ const buf = gl.createBuffer();
944
+ gl.bindBuffer(gl.ARRAY_BUFFER, buf);
945
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);
946
+ gl.enableVertexAttribArray(0);
947
+ gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
948
+ this.texture = gl.createTexture();
949
+ gl.bindTexture(gl.TEXTURE_2D, this.texture);
950
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
951
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
952
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
953
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
954
+ gl.clearColor(0, 0, 0, 0);
955
+ this.initialized = true;
956
+ }
957
+ createShader(type, source) {
958
+ const gl = this.gl;
959
+ const shader = gl.createShader(type);
960
+ gl.shaderSource(shader, source);
961
+ gl.compileShader(shader);
962
+ if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
963
+ const info = gl.getShaderInfoLog(shader);
964
+ gl.deleteShader(shader);
965
+ throw new Error("Shader compile failed: " + info);
966
+ }
967
+ return shader;
968
+ }
969
+ /** Check if renderer is ready */
970
+ isReady() {
971
+ return this.initialized;
972
+ }
973
+ /** Render CRT effect */
974
+ render(input, options = {}) {
975
+ if (!this.initialized || !this.gl || !this.canvas) {
976
+ throw new Error("Renderer not initialized");
977
+ }
978
+ const gl = this.gl;
979
+ const data = input instanceof ImageData ? input.data : input.data;
980
+ const width = input.width;
981
+ const height = input.height;
982
+ const scale = Math.min(32, Math.max(2, options.scale ?? 3));
983
+ const outWidth = width * scale;
984
+ const outHeight = height * scale;
985
+ if (this.currentCanvasSize.width !== outWidth || this.currentCanvasSize.height !== outHeight) {
986
+ this.canvas.width = outWidth;
987
+ this.canvas.height = outHeight;
988
+ this.currentCanvasSize = { width: outWidth, height: outHeight };
989
+ gl.viewport(0, 0, outWidth, outHeight);
990
+ }
991
+ gl.uniform2f(this.uniforms.uRes, outWidth, outHeight);
992
+ gl.uniform2f(this.uniforms.uWarp, options.warpX ?? 0.015, options.warpY ?? 0.02);
993
+ gl.uniform1f(this.uniforms.uScanHardness, options.scanHardness ?? -4);
994
+ gl.uniform1f(this.uniforms.uScanOpacity, options.scanOpacity ?? 0.5);
995
+ gl.uniform1f(this.uniforms.uMaskOpacity, options.maskOpacity ?? 0.3);
996
+ gl.uniform1i(this.uniforms.uEnableWarp, options.enableWarp !== false ? 1 : 0);
997
+ gl.uniform1i(this.uniforms.uEnableScanlines, options.enableScanlines !== false ? 1 : 0);
998
+ gl.uniform1i(this.uniforms.uEnableMask, options.enableMask !== false ? 1 : 0);
999
+ gl.bindTexture(gl.TEXTURE_2D, this.texture);
1000
+ if (this.currentTexSize.width !== width || this.currentTexSize.height !== height) {
1001
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
1002
+ this.currentTexSize = { width, height };
1003
+ } else {
1004
+ gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, data);
1005
+ }
1006
+ gl.clear(gl.COLOR_BUFFER_BIT);
1007
+ gl.drawArrays(gl.TRIANGLES, 0, 3);
1008
+ const pixels = new Uint8ClampedArray(outWidth * outHeight * 4);
1009
+ gl.readPixels(0, 0, outWidth, outHeight, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
1010
+ return {
1011
+ data: pixels,
1012
+ width: outWidth,
1013
+ height: outHeight
1014
+ };
1015
+ }
1016
+ /** Dispose resources */
1017
+ dispose() {
1018
+ if (this.gl) {
1019
+ if (this.texture) this.gl.deleteTexture(this.texture);
1020
+ if (this.program) this.gl.deleteProgram(this.program);
1021
+ this.gl = null;
1022
+ }
1023
+ this.canvas = null;
1024
+ this.initialized = false;
1025
+ }
1026
+ };
1027
+ var CRT_PRESETS = {
1028
+ default: {},
1029
+ authentic: {
1030
+ warpX: 0.02,
1031
+ warpY: 0.025,
1032
+ scanHardness: -6,
1033
+ scanOpacity: 0.6,
1034
+ maskOpacity: 0.4
1035
+ },
1036
+ subtle: {
1037
+ warpX: 8e-3,
1038
+ warpY: 0.01,
1039
+ scanHardness: -3,
1040
+ scanOpacity: 0.3,
1041
+ maskOpacity: 0.15
1042
+ },
1043
+ flat: {
1044
+ warpX: 0,
1045
+ warpY: 0,
1046
+ enableWarp: false,
1047
+ scanHardness: -4,
1048
+ scanOpacity: 0.5,
1049
+ maskOpacity: 0.3
1050
+ }
1051
+ };
1052
+
1053
+ // src/hex-gpu.ts
1054
+ var VERTEX_SHADER2 = `#version 300 es
1055
+ layout(location = 0) in vec2 position;
1056
+ out vec2 vUv;
1057
+
1058
+ void main() {
1059
+ vUv = position * 0.5 + 0.5;
1060
+ gl_Position = vec4(position, 0.0, 1.0);
1061
+ }`;
1062
+ var FRAGMENT_SHADER2 = `#version 300 es
1063
+ precision highp float;
1064
+
1065
+ uniform sampler2D uTex;
1066
+ uniform vec2 uOutputRes;
1067
+ uniform vec2 uInputRes;
1068
+ uniform float uScale;
1069
+ uniform int uOrientation; // 0 = flat-top, 1 = pointy-top
1070
+ uniform int uDrawBorders;
1071
+ uniform vec4 uBorderColor;
1072
+ uniform float uBorderThickness;
1073
+ uniform vec4 uBackgroundColor;
1074
+
1075
+ in vec2 vUv;
1076
+ out vec4 outColor;
1077
+
1078
+ const float SQRT3 = 1.732050808;
1079
+ const float INV_SQRT3 = 0.577350269;
1080
+
1081
+ // Convert output pixel to hex coordinate (flat-top)
1082
+ vec2 pixelToHexFlat(vec2 pos, float scale) {
1083
+ float hSpacing = scale * 1.5;
1084
+ float vSpacing = scale * SQRT3;
1085
+
1086
+ float col = pos.x / hSpacing;
1087
+ int colInt = int(floor(col));
1088
+
1089
+ // Check if odd column (offset)
1090
+ bool isOffset = (colInt & 1) == 1;
1091
+ float yOffset = isOffset ? vSpacing * 0.5 : 0.0;
1092
+
1093
+ float row = (pos.y - yOffset) / vSpacing;
1094
+ int rowInt = int(floor(row));
1095
+
1096
+ // Refine with corner detection
1097
+ float cellX = pos.x - float(colInt) * hSpacing;
1098
+ float cellY = pos.y - float(rowInt) * vSpacing - yOffset;
1099
+
1100
+ float quarterW = scale * 0.5;
1101
+ float halfH = vSpacing * 0.5;
1102
+
1103
+ if (cellX < quarterW) {
1104
+ float distFromCenter = abs(cellY - halfH);
1105
+ float edgeX = distFromCenter * INV_SQRT3;
1106
+
1107
+ if (cellX < quarterW - edgeX) {
1108
+ if (cellY < halfH) {
1109
+ rowInt -= 1;
1110
+ }
1111
+ colInt -= 1;
1112
+ }
1113
+ }
1114
+
1115
+ return vec2(float(colInt), float(rowInt));
1116
+ }
1117
+
1118
+ // Convert output pixel to hex coordinate (pointy-top)
1119
+ vec2 pixelToHexPointy(vec2 pos, float scale) {
1120
+ float hSpacing = scale * SQRT3;
1121
+ float vSpacing = scale * 1.5;
1122
+
1123
+ float row = pos.y / vSpacing;
1124
+ int rowInt = int(floor(row));
1125
+
1126
+ // Check if odd row (offset)
1127
+ bool isOffset = (rowInt & 1) == 1;
1128
+ float xOffset = isOffset ? hSpacing * 0.5 : 0.0;
1129
+
1130
+ float col = (pos.x - xOffset) / hSpacing;
1131
+ int colInt = int(floor(col));
1132
+
1133
+ // Refine with corner detection
1134
+ float cellX = pos.x - float(colInt) * hSpacing - xOffset;
1135
+ float cellY = pos.y - float(rowInt) * vSpacing;
1136
+
1137
+ float halfW = hSpacing * 0.5;
1138
+ float quarterH = scale * 0.5;
1139
+
1140
+ if (cellY < quarterH) {
1141
+ float distFromCenter = abs(cellX - halfW);
1142
+ float edgeY = distFromCenter * SQRT3;
1143
+
1144
+ if (cellY < edgeY) {
1145
+ if (cellX < halfW && isOffset) {
1146
+ colInt -= 1;
1147
+ }
1148
+ rowInt -= 1;
1149
+ }
1150
+ }
1151
+
1152
+ return vec2(float(colInt), float(rowInt));
1153
+ }
1154
+
1155
+ // Check if pixel is on hex border
1156
+ bool isBorderPixel(vec2 pos, float scale, int orientation, float thickness) {
1157
+ for (float dy = -thickness; dy <= thickness; dy += 1.0) {
1158
+ for (float dx = -thickness; dx <= thickness; dx += 1.0) {
1159
+ if (dx == 0.0 && dy == 0.0) continue;
1160
+
1161
+ vec2 h1 = orientation == 0
1162
+ ? pixelToHexFlat(pos, scale)
1163
+ : pixelToHexPointy(pos, scale);
1164
+ vec2 h2 = orientation == 0
1165
+ ? pixelToHexFlat(pos + vec2(dx, dy), scale)
1166
+ : pixelToHexPointy(pos + vec2(dx, dy), scale);
1167
+
1168
+ if (h1 != h2) return true;
1169
+ }
1170
+ }
1171
+ return false;
1172
+ }
1173
+
1174
+ void main() {
1175
+ vec2 pixelPos = vUv * uOutputRes;
1176
+
1177
+ // Get hex coordinate
1178
+ vec2 hexCoord = uOrientation == 0
1179
+ ? pixelToHexFlat(pixelPos, uScale)
1180
+ : pixelToHexPointy(pixelPos, uScale);
1181
+
1182
+ // Check bounds
1183
+ if (hexCoord.x < 0.0 || hexCoord.y < 0.0 ||
1184
+ hexCoord.x >= uInputRes.x || hexCoord.y >= uInputRes.y) {
1185
+ outColor = uBackgroundColor;
1186
+ return;
1187
+ }
1188
+
1189
+ // Check for border
1190
+ if (uDrawBorders == 1 && uBorderThickness > 0.0) {
1191
+ if (isBorderPixel(pixelPos, uScale, uOrientation, uBorderThickness)) {
1192
+ outColor = uBorderColor;
1193
+ return;
1194
+ }
1195
+ }
1196
+
1197
+ // Sample source pixel (add 0.5 for pixel center)
1198
+ vec2 texCoord = (hexCoord + 0.5) / uInputRes;
1199
+ outColor = texture(uTex, texCoord);
1200
+ }`;
1201
+ function parseColor(color, defaultColor) {
1202
+ if (color === void 0) return defaultColor;
1203
+ if (typeof color === "number") {
1204
+ return [
1205
+ (color >> 24 & 255) / 255,
1206
+ (color >> 16 & 255) / 255,
1207
+ (color >> 8 & 255) / 255,
1208
+ (color & 255) / 255
1209
+ ];
1210
+ }
1211
+ if (color === "transparent") return [0, 0, 0, 0];
1212
+ if (color.startsWith("#")) {
1213
+ const hex = color.slice(1);
1214
+ if (hex.length === 6) {
1215
+ return [
1216
+ parseInt(hex.slice(0, 2), 16) / 255,
1217
+ parseInt(hex.slice(2, 4), 16) / 255,
1218
+ parseInt(hex.slice(4, 6), 16) / 255,
1219
+ 1
1220
+ ];
1221
+ }
1222
+ if (hex.length === 8) {
1223
+ return [
1224
+ parseInt(hex.slice(0, 2), 16) / 255,
1225
+ parseInt(hex.slice(2, 4), 16) / 255,
1226
+ parseInt(hex.slice(4, 6), 16) / 255,
1227
+ parseInt(hex.slice(6, 8), 16) / 255
1228
+ ];
1229
+ }
1230
+ }
1231
+ return defaultColor;
1232
+ }
1233
+ function hexGetDimensions(srcWidth, srcHeight, scale, orientation = "flat-top") {
1234
+ const SQRT3 = 1.732050808;
1235
+ if (orientation === "flat-top") {
1236
+ const hSpacing = scale * 1.5;
1237
+ const vSpacing = scale * SQRT3;
1238
+ const cellWidth = scale * 2;
1239
+ const cellHeight = scale * SQRT3;
1240
+ return {
1241
+ width: Math.ceil(srcWidth * hSpacing + cellWidth),
1242
+ height: Math.ceil(srcHeight * vSpacing + cellHeight)
1243
+ };
1244
+ } else {
1245
+ const hSpacing = scale * SQRT3;
1246
+ const vSpacing = scale * 1.5;
1247
+ const cellWidth = scale * SQRT3;
1248
+ const cellHeight = scale * 2;
1249
+ return {
1250
+ width: Math.ceil(srcWidth * hSpacing + cellWidth),
1251
+ height: Math.ceil(srcHeight * vSpacing + cellHeight)
1252
+ };
1253
+ }
1254
+ }
1255
+ var HexGpuRenderer = class _HexGpuRenderer {
1256
+ constructor() {
1257
+ this.gl = null;
1258
+ this.canvas = null;
1259
+ this.program = null;
1260
+ this.texture = null;
1261
+ this.uniforms = {};
1262
+ this.initialized = false;
1263
+ this.currentCanvasSize = { width: 0, height: 0 };
1264
+ this.currentTexSize = { width: 0, height: 0 };
1265
+ }
1266
+ /** Create a new HEX GPU renderer */
1267
+ static create() {
1268
+ const renderer = new _HexGpuRenderer();
1269
+ renderer.init();
1270
+ return renderer;
1271
+ }
1272
+ init() {
1273
+ if (typeof OffscreenCanvas === "undefined") {
1274
+ throw new Error("OffscreenCanvas not supported");
1275
+ }
1276
+ this.canvas = new OffscreenCanvas(1, 1);
1277
+ this.gl = this.canvas.getContext("webgl2", {
1278
+ alpha: true,
1279
+ premultipliedAlpha: false,
1280
+ desynchronized: true,
1281
+ powerPreference: "high-performance",
1282
+ antialias: false
1283
+ });
1284
+ if (!this.gl) {
1285
+ throw new Error("WebGL2 not supported");
1286
+ }
1287
+ const gl = this.gl;
1288
+ const vs = this.createShader(gl.VERTEX_SHADER, VERTEX_SHADER2);
1289
+ const fs = this.createShader(gl.FRAGMENT_SHADER, FRAGMENT_SHADER2);
1290
+ this.program = gl.createProgram();
1291
+ gl.attachShader(this.program, vs);
1292
+ gl.attachShader(this.program, fs);
1293
+ gl.linkProgram(this.program);
1294
+ if (!gl.getProgramParameter(this.program, gl.LINK_STATUS)) {
1295
+ throw new Error("Shader program link failed: " + gl.getProgramInfoLog(this.program));
1296
+ }
1297
+ gl.useProgram(this.program);
1298
+ this.uniforms = {
1299
+ uTex: gl.getUniformLocation(this.program, "uTex"),
1300
+ uOutputRes: gl.getUniformLocation(this.program, "uOutputRes"),
1301
+ uInputRes: gl.getUniformLocation(this.program, "uInputRes"),
1302
+ uScale: gl.getUniformLocation(this.program, "uScale"),
1303
+ uOrientation: gl.getUniformLocation(this.program, "uOrientation"),
1304
+ uDrawBorders: gl.getUniformLocation(this.program, "uDrawBorders"),
1305
+ uBorderColor: gl.getUniformLocation(this.program, "uBorderColor"),
1306
+ uBorderThickness: gl.getUniformLocation(this.program, "uBorderThickness"),
1307
+ uBackgroundColor: gl.getUniformLocation(this.program, "uBackgroundColor")
1308
+ };
1309
+ gl.uniform1i(this.uniforms.uTex, 0);
1310
+ const buf = gl.createBuffer();
1311
+ gl.bindBuffer(gl.ARRAY_BUFFER, buf);
1312
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);
1313
+ gl.enableVertexAttribArray(0);
1314
+ gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
1315
+ this.texture = gl.createTexture();
1316
+ gl.bindTexture(gl.TEXTURE_2D, this.texture);
1317
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
1318
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
1319
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
1320
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
1321
+ gl.clearColor(0, 0, 0, 0);
1322
+ this.initialized = true;
1323
+ }
1324
+ createShader(type, source) {
1325
+ const gl = this.gl;
1326
+ const shader = gl.createShader(type);
1327
+ gl.shaderSource(shader, source);
1328
+ gl.compileShader(shader);
1329
+ if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
1330
+ const info = gl.getShaderInfoLog(shader);
1331
+ gl.deleteShader(shader);
1332
+ throw new Error("Shader compile failed: " + info);
1333
+ }
1334
+ return shader;
1335
+ }
1336
+ /** Check if renderer is ready */
1337
+ isReady() {
1338
+ return this.initialized;
1339
+ }
1340
+ /** Render hexagonal effect */
1341
+ render(input, options = {}) {
1342
+ if (!this.initialized || !this.gl || !this.canvas) {
1343
+ throw new Error("Renderer not initialized");
1344
+ }
1345
+ const gl = this.gl;
1346
+ const data = input instanceof ImageData ? input.data : input.data;
1347
+ const width = input.width;
1348
+ const height = input.height;
1349
+ const scale = Math.min(32, Math.max(2, options.scale ?? 16));
1350
+ const orientation = options.orientation ?? "flat-top";
1351
+ const { width: outWidth, height: outHeight } = hexGetDimensions(width, height, scale, orientation);
1352
+ if (this.currentCanvasSize.width !== outWidth || this.currentCanvasSize.height !== outHeight) {
1353
+ this.canvas.width = outWidth;
1354
+ this.canvas.height = outHeight;
1355
+ this.currentCanvasSize = { width: outWidth, height: outHeight };
1356
+ gl.viewport(0, 0, outWidth, outHeight);
1357
+ }
1358
+ gl.uniform2f(this.uniforms.uOutputRes, outWidth, outHeight);
1359
+ gl.uniform2f(this.uniforms.uInputRes, width, height);
1360
+ gl.uniform1f(this.uniforms.uScale, scale);
1361
+ gl.uniform1i(this.uniforms.uOrientation, orientation === "flat-top" ? 0 : 1);
1362
+ gl.uniform1i(this.uniforms.uDrawBorders, options.drawBorders ? 1 : 0);
1363
+ gl.uniform1f(this.uniforms.uBorderThickness, options.borderThickness ?? 1);
1364
+ const borderColor = parseColor(options.borderColor, [0.16, 0.16, 0.16, 1]);
1365
+ gl.uniform4f(this.uniforms.uBorderColor, ...borderColor);
1366
+ const bgColor = parseColor(options.backgroundColor, [0, 0, 0, 0]);
1367
+ gl.uniform4f(this.uniforms.uBackgroundColor, ...bgColor);
1368
+ gl.bindTexture(gl.TEXTURE_2D, this.texture);
1369
+ if (this.currentTexSize.width !== width || this.currentTexSize.height !== height) {
1370
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
1371
+ this.currentTexSize = { width, height };
1372
+ } else {
1373
+ gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, data);
1374
+ }
1375
+ gl.clear(gl.COLOR_BUFFER_BIT);
1376
+ gl.drawArrays(gl.TRIANGLES, 0, 3);
1377
+ const pixels = new Uint8ClampedArray(outWidth * outHeight * 4);
1378
+ gl.readPixels(0, 0, outWidth, outHeight, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
1379
+ return {
1380
+ data: pixels,
1381
+ width: outWidth,
1382
+ height: outHeight
1383
+ };
1384
+ }
1385
+ /** Dispose resources */
1386
+ dispose() {
1387
+ if (this.gl) {
1388
+ if (this.texture) this.gl.deleteTexture(this.texture);
1389
+ if (this.program) this.gl.deleteProgram(this.program);
1390
+ this.gl = null;
1391
+ }
1392
+ this.canvas = null;
1393
+ this.initialized = false;
1394
+ }
1395
+ };
1396
+ var HEX_PRESETS = {
1397
+ default: {},
1398
+ bordered: {
1399
+ drawBorders: true,
1400
+ borderColor: "#282828",
1401
+ borderThickness: 1
1402
+ },
1403
+ pointy: {
1404
+ orientation: "pointy-top",
1405
+ drawBorders: false
1406
+ }
1407
+ };
1408
+
1409
+ // src/wasm-loader.ts
1410
+ var wasmModule = null;
1411
+ var wasmMemory = null;
1412
+ var loadPromise = null;
1413
+ async function loadWasm() {
1414
+ if (wasmModule) return wasmModule;
1415
+ if (loadPromise) return loadPromise;
1416
+ loadPromise = (async () => {
1417
+ try {
1418
+ const wasm2 = await Promise.resolve().then(() => (init_renderart_wasm(), renderart_wasm_exports));
1419
+ await wasm2.default();
1420
+ wasmModule = wasm2;
1421
+ wasmMemory = wasmModule.get_memory();
1422
+ return wasmModule;
1423
+ } catch (e) {
1424
+ throw new Error(`Failed to load WASM module: ${e}`);
1425
+ }
1426
+ })();
1427
+ return loadPromise;
1428
+ }
1429
+ function isWasmLoaded() {
1430
+ return wasmModule !== null;
1431
+ }
1432
+ function getOutputData(result) {
1433
+ if (!wasmMemory) throw new Error("WASM memory not available");
1434
+ const buffer = new Uint8Array(wasmMemory.buffer, result.ptr, result.len);
1435
+ return new Uint8ClampedArray(buffer.slice());
1436
+ }
1437
+ function toUint8Array(input) {
1438
+ const data = input instanceof ImageData ? input.data : input.data;
1439
+ return data instanceof Uint8Array ? data : new Uint8Array(data);
1440
+ }
1441
+ function parseColorToU32(color, defaultVal) {
1442
+ if (color === void 0) return defaultVal;
1443
+ if (typeof color === "number") return color;
1444
+ if (color === "transparent") return 0;
1445
+ if (color.startsWith("#")) {
1446
+ const hex = color.slice(1);
1447
+ if (hex.length === 6) {
1448
+ return parseInt(hex, 16) << 8 | 255;
1449
+ }
1450
+ if (hex.length === 8) {
1451
+ return parseInt(hex, 16);
1452
+ }
1453
+ }
1454
+ return defaultVal;
1455
+ }
1456
+ var CrtCpuRenderer = class _CrtCpuRenderer {
1457
+ constructor() {
1458
+ this.ready = false;
1459
+ }
1460
+ /** Create and initialize renderer */
1461
+ static async create() {
1462
+ const renderer = new _CrtCpuRenderer();
1463
+ await renderer.init();
1464
+ return renderer;
1465
+ }
1466
+ async init() {
1467
+ await loadWasm();
1468
+ this.ready = true;
1469
+ }
1470
+ /** Check if renderer is ready */
1471
+ isReady() {
1472
+ return this.ready;
1473
+ }
1474
+ /** Render CRT effect */
1475
+ render(input, options = {}) {
1476
+ if (!this.ready || !wasmModule) {
1477
+ throw new Error("Renderer not initialized");
1478
+ }
1479
+ const data = toUint8Array(input);
1480
+ const width = input.width;
1481
+ const height = input.height;
1482
+ const scale = Math.min(32, Math.max(2, options.scale ?? 3));
1483
+ const config = new wasmModule.CrtConfig();
1484
+ config.warp_x = options.warpX ?? 0.015;
1485
+ config.warp_y = options.warpY ?? 0.02;
1486
+ config.scan_hardness = options.scanHardness ?? -4;
1487
+ config.scan_opacity = options.scanOpacity ?? 0.5;
1488
+ config.mask_opacity = options.maskOpacity ?? 0.3;
1489
+ config.enable_warp = options.enableWarp !== false;
1490
+ config.enable_scanlines = options.enableScanlines !== false;
1491
+ config.enable_mask = options.enableMask !== false;
1492
+ const result = wasmModule.crt_upscale_config(data, width, height, scale, config);
1493
+ return {
1494
+ data: getOutputData(result),
1495
+ width: result.width,
1496
+ height: result.height
1497
+ };
1498
+ }
1499
+ /** Dispose resources */
1500
+ dispose() {
1501
+ this.ready = false;
1502
+ }
1503
+ };
1504
+ var HexCpuRenderer = class _HexCpuRenderer {
1505
+ constructor() {
1506
+ this.ready = false;
1507
+ }
1508
+ /** Create and initialize renderer */
1509
+ static async create() {
1510
+ const renderer = new _HexCpuRenderer();
1511
+ await renderer.init();
1512
+ return renderer;
1513
+ }
1514
+ async init() {
1515
+ await loadWasm();
1516
+ this.ready = true;
1517
+ }
1518
+ /** Check if renderer is ready */
1519
+ isReady() {
1520
+ return this.ready;
1521
+ }
1522
+ /** Render hexagonal effect */
1523
+ render(input, options = {}) {
1524
+ if (!this.ready || !wasmModule) {
1525
+ throw new Error("Renderer not initialized");
1526
+ }
1527
+ const data = toUint8Array(input);
1528
+ const width = input.width;
1529
+ const height = input.height;
1530
+ const scale = Math.min(32, Math.max(2, options.scale ?? 16));
1531
+ const config = new wasmModule.HexConfig();
1532
+ config.orientation = options.orientation === "pointy-top" ? 1 : 0;
1533
+ config.draw_borders = options.drawBorders ?? false;
1534
+ config.border_color = parseColorToU32(options.borderColor, 673720575);
1535
+ config.border_thickness = options.borderThickness ?? 1;
1536
+ config.background_color = parseColorToU32(options.backgroundColor, 0);
1537
+ const result = wasmModule.hex_upscale_config(data, width, height, scale, config);
1538
+ return {
1539
+ data: getOutputData(result),
1540
+ width: result.width,
1541
+ height: result.height
1542
+ };
1543
+ }
1544
+ /** Get output dimensions */
1545
+ getDimensions(srcWidth, srcHeight, scale, orientation = "flat-top") {
1546
+ if (!this.ready || !wasmModule) {
1547
+ throw new Error("Renderer not initialized");
1548
+ }
1549
+ const dims = wasmModule.hex_get_dimensions(srcWidth, srcHeight, scale, orientation === "pointy-top" ? 1 : 0);
1550
+ return { width: dims[0], height: dims[1] };
1551
+ }
1552
+ /** Dispose resources */
1553
+ dispose() {
1554
+ this.ready = false;
1555
+ }
1556
+ };
1557
+ var XbrzCpuRenderer = class _XbrzCpuRenderer {
1558
+ constructor() {
1559
+ this.ready = false;
1560
+ }
1561
+ /** Create and initialize renderer */
1562
+ static async create() {
1563
+ const renderer = new _XbrzCpuRenderer();
1564
+ await renderer.init();
1565
+ return renderer;
1566
+ }
1567
+ async init() {
1568
+ await loadWasm();
1569
+ this.ready = true;
1570
+ }
1571
+ /** Check if renderer is ready */
1572
+ isReady() {
1573
+ return this.ready;
1574
+ }
1575
+ /** Render xBRZ scaling */
1576
+ render(input, options = {}) {
1577
+ if (!this.ready || !wasmModule) {
1578
+ throw new Error("Renderer not initialized");
1579
+ }
1580
+ const data = toUint8Array(input);
1581
+ const width = input.width;
1582
+ const height = input.height;
1583
+ const scale = Math.min(6, Math.max(2, options.scale ?? 2));
1584
+ const config = new wasmModule.XbrzConfig();
1585
+ config.luminance_weight = options.luminanceWeight ?? 1;
1586
+ config.equal_color_tolerance = options.equalColorTolerance ?? 30;
1587
+ config.dominant_direction_threshold = options.dominantDirectionThreshold ?? 4.4;
1588
+ config.steep_direction_threshold = options.steepDirectionThreshold ?? 2.2;
1589
+ const result = wasmModule.xbrz_upscale_config(data, width, height, scale, config);
1590
+ return {
1591
+ data: getOutputData(result),
1592
+ width: result.width,
1593
+ height: result.height
1594
+ };
1595
+ }
1596
+ /** Dispose resources */
1597
+ dispose() {
1598
+ this.ready = false;
1599
+ }
1600
+ };
1601
+ var XBRZ_PRESETS = {
1602
+ default: {},
1603
+ sharp: {
1604
+ luminanceWeight: 1,
1605
+ equalColorTolerance: 20,
1606
+ dominantDirectionThreshold: 4.4,
1607
+ steepDirectionThreshold: 2.2
1608
+ },
1609
+ smooth: {
1610
+ luminanceWeight: 0.8,
1611
+ equalColorTolerance: 35,
1612
+ dominantDirectionThreshold: 5,
1613
+ steepDirectionThreshold: 2.5
1614
+ }
1615
+ };
1616
+
1617
+ // src/renderart.ts
1618
+ function checkWebGL2() {
1619
+ if (typeof OffscreenCanvas === "undefined") return false;
1620
+ try {
1621
+ const canvas = new OffscreenCanvas(1, 1);
1622
+ return !!canvas.getContext("webgl2");
1623
+ } catch {
1624
+ return false;
1625
+ }
1626
+ }
1627
+ function getMaxTextureSize() {
1628
+ if (!checkWebGL2()) return 0;
1629
+ try {
1630
+ const canvas = new OffscreenCanvas(1, 1);
1631
+ const gl = canvas.getContext("webgl2");
1632
+ return gl?.getParameter(gl.MAX_TEXTURE_SIZE) ?? 0;
1633
+ } catch {
1634
+ return 0;
1635
+ }
1636
+ }
1637
+ var CrtEngine = class {
1638
+ constructor(gpuAvailable, cpuAvailable) {
1639
+ this.gpuRenderer = null;
1640
+ this.cpuRenderer = null;
1641
+ this.gpuAvailable = gpuAvailable;
1642
+ this.cpuAvailable = cpuAvailable;
1643
+ }
1644
+ /** Initialize GPU renderer */
1645
+ ensureGpu() {
1646
+ if (!this.gpuRenderer && this.gpuAvailable) {
1647
+ this.gpuRenderer = CrtGpuRenderer.create();
1648
+ }
1649
+ if (!this.gpuRenderer) throw new Error("GPU renderer not available");
1650
+ return this.gpuRenderer;
1651
+ }
1652
+ /** Initialize CPU renderer */
1653
+ async ensureCpu() {
1654
+ if (!this.cpuRenderer && this.cpuAvailable) {
1655
+ this.cpuRenderer = await CrtCpuRenderer.create();
1656
+ }
1657
+ if (!this.cpuRenderer) throw new Error("CPU renderer not available");
1658
+ return this.cpuRenderer;
1659
+ }
1660
+ async render(input, optionsOrPreset, overrides) {
1661
+ let options = {};
1662
+ if (typeof optionsOrPreset === "string") {
1663
+ options = { ...CRT_PRESETS[optionsOrPreset], ...overrides };
1664
+ } else if (optionsOrPreset) {
1665
+ options = optionsOrPreset;
1666
+ }
1667
+ const backend = options.backend ?? "auto";
1668
+ if (backend === "gpu" || backend === "auto" && this.gpuAvailable) {
1669
+ try {
1670
+ return this.ensureGpu().render(input, options);
1671
+ } catch (e) {
1672
+ if (backend === "gpu") throw e;
1673
+ }
1674
+ }
1675
+ const cpu = await this.ensureCpu();
1676
+ return cpu.render(input, options);
1677
+ }
1678
+ /** Render synchronously (GPU only) */
1679
+ renderSync(input, options) {
1680
+ return this.ensureGpu().render(input, options ?? {});
1681
+ }
1682
+ /** Dispose resources */
1683
+ dispose() {
1684
+ this.gpuRenderer?.dispose();
1685
+ this.cpuRenderer?.dispose();
1686
+ this.gpuRenderer = null;
1687
+ this.cpuRenderer = null;
1688
+ }
1689
+ };
1690
+ var HexEngine = class {
1691
+ constructor(gpuAvailable, cpuAvailable) {
1692
+ this.gpuRenderer = null;
1693
+ this.cpuRenderer = null;
1694
+ this.gpuAvailable = gpuAvailable;
1695
+ this.cpuAvailable = cpuAvailable;
1696
+ }
1697
+ ensureGpu() {
1698
+ if (!this.gpuRenderer && this.gpuAvailable) {
1699
+ this.gpuRenderer = HexGpuRenderer.create();
1700
+ }
1701
+ if (!this.gpuRenderer) throw new Error("GPU renderer not available");
1702
+ return this.gpuRenderer;
1703
+ }
1704
+ async ensureCpu() {
1705
+ if (!this.cpuRenderer && this.cpuAvailable) {
1706
+ this.cpuRenderer = await HexCpuRenderer.create();
1707
+ }
1708
+ if (!this.cpuRenderer) throw new Error("CPU renderer not available");
1709
+ return this.cpuRenderer;
1710
+ }
1711
+ async render(input, optionsOrPreset, overrides) {
1712
+ let options = {};
1713
+ if (typeof optionsOrPreset === "string") {
1714
+ options = { ...HEX_PRESETS[optionsOrPreset], ...overrides };
1715
+ } else if (optionsOrPreset) {
1716
+ options = optionsOrPreset;
1717
+ }
1718
+ const backend = options.backend ?? "auto";
1719
+ if (backend === "gpu" || backend === "auto" && this.gpuAvailable) {
1720
+ try {
1721
+ return this.ensureGpu().render(input, options);
1722
+ } catch (e) {
1723
+ if (backend === "gpu") throw e;
1724
+ }
1725
+ }
1726
+ const cpu = await this.ensureCpu();
1727
+ return cpu.render(input, options);
1728
+ }
1729
+ /** Render synchronously (GPU only) */
1730
+ renderSync(input, options) {
1731
+ return this.ensureGpu().render(input, options ?? {});
1732
+ }
1733
+ /** Get output dimensions */
1734
+ getDimensions(srcWidth, srcHeight, options) {
1735
+ const scale = options?.scale ?? 16;
1736
+ const orientation = options?.orientation ?? "flat-top";
1737
+ return hexGetDimensions(srcWidth, srcHeight, scale, orientation);
1738
+ }
1739
+ dispose() {
1740
+ this.gpuRenderer?.dispose();
1741
+ this.cpuRenderer?.dispose();
1742
+ this.gpuRenderer = null;
1743
+ this.cpuRenderer = null;
1744
+ }
1745
+ };
1746
+ var XbrzEngine = class {
1747
+ constructor(cpuAvailable) {
1748
+ this.cpuRenderer = null;
1749
+ this.cpuAvailable = cpuAvailable;
1750
+ }
1751
+ async ensureCpu() {
1752
+ if (!this.cpuRenderer && this.cpuAvailable) {
1753
+ this.cpuRenderer = await XbrzCpuRenderer.create();
1754
+ }
1755
+ if (!this.cpuRenderer) throw new Error("CPU renderer not available");
1756
+ return this.cpuRenderer;
1757
+ }
1758
+ async render(input, optionsOrPreset, overrides) {
1759
+ let options = {};
1760
+ if (typeof optionsOrPreset === "string") {
1761
+ options = { ...XBRZ_PRESETS[optionsOrPreset], ...overrides };
1762
+ } else if (optionsOrPreset) {
1763
+ options = optionsOrPreset;
1764
+ }
1765
+ const cpu = await this.ensureCpu();
1766
+ return cpu.render(input, options);
1767
+ }
1768
+ /** Get output dimensions */
1769
+ getDimensions(srcWidth, srcHeight, scale = 2) {
1770
+ const s = Math.min(6, Math.max(2, scale));
1771
+ return { width: srcWidth * s, height: srcHeight * s };
1772
+ }
1773
+ dispose() {
1774
+ this.cpuRenderer?.dispose();
1775
+ this.cpuRenderer = null;
1776
+ }
1777
+ };
1778
+ var RenderArt = class _RenderArt {
1779
+ constructor(gpuAvailable, cpuAvailable) {
1780
+ this.crt = new CrtEngine(gpuAvailable, cpuAvailable);
1781
+ this.hex = new HexEngine(gpuAvailable, cpuAvailable);
1782
+ this.xbrz = new XbrzEngine(cpuAvailable);
1783
+ const maxTextureSize = gpuAvailable ? getMaxTextureSize() : 0;
1784
+ this._capabilities = {
1785
+ gpu: gpuAvailable,
1786
+ cpu: cpuAvailable,
1787
+ maxTextureSize,
1788
+ recommendedBackend: gpuAvailable ? "gpu" : cpuAvailable ? "cpu" : "auto"
1789
+ };
1790
+ }
1791
+ /** Create and initialize RenderArt instance */
1792
+ static async create() {
1793
+ const gpuAvailable = checkWebGL2();
1794
+ let cpuAvailable = false;
1795
+ try {
1796
+ await loadWasm();
1797
+ cpuAvailable = isWasmLoaded();
1798
+ } catch {
1799
+ }
1800
+ if (!gpuAvailable && !cpuAvailable) {
1801
+ throw new Error("No rendering backend available. WebGL2 or WASM required.");
1802
+ }
1803
+ return new _RenderArt(gpuAvailable, cpuAvailable);
1804
+ }
1805
+ /** Get renderer capabilities */
1806
+ get capabilities() {
1807
+ return { ...this._capabilities };
1808
+ }
1809
+ /** Dispose all resources */
1810
+ dispose() {
1811
+ this.crt.dispose();
1812
+ this.hex.dispose();
1813
+ this.xbrz.dispose();
1814
+ }
1815
+ };
1816
+
1817
+ export { CRT_PRESETS, CrtCpuRenderer, CrtGpuRenderer, HEX_PRESETS, HexCpuRenderer, HexGpuRenderer, RenderArt, XBRZ_PRESETS, XbrzCpuRenderer, hexGetDimensions, isWasmLoaded, loadWasm };
1818
+ //# sourceMappingURL=index.mjs.map
1819
+ //# sourceMappingURL=index.mjs.map