@margoengine/headless 0.0.0-stage → 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.
@@ -0,0 +1,685 @@
1
+ // @margo/core-built 0.1.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+ const r=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
4
+ import { n as e, r as t, t as n } from "./errors-CDgOgzCd.js";
5
+ import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
6
+ //#region src/formats.ts
7
+ function i({ width: t, height: n, data: r }) {
8
+ if (!Number.isInteger(t) || !Number.isInteger(n) || t < 1 || n < 1) throw e("Invalid image size", "headless");
9
+ if (r?.length !== t * n * 4) throw e("Frame data does not match its size", "headless");
10
+ }
11
+ var a = [
12
+ 16,
13
+ 11,
14
+ 10,
15
+ 16,
16
+ 24,
17
+ 40,
18
+ 51,
19
+ 61,
20
+ 12,
21
+ 12,
22
+ 14,
23
+ 19,
24
+ 26,
25
+ 58,
26
+ 60,
27
+ 55,
28
+ 14,
29
+ 13,
30
+ 16,
31
+ 24,
32
+ 40,
33
+ 57,
34
+ 69,
35
+ 56,
36
+ 14,
37
+ 17,
38
+ 22,
39
+ 29,
40
+ 51,
41
+ 87,
42
+ 80,
43
+ 62,
44
+ 18,
45
+ 22,
46
+ 37,
47
+ 56,
48
+ 68,
49
+ 109,
50
+ 103,
51
+ 77,
52
+ 24,
53
+ 35,
54
+ 55,
55
+ 64,
56
+ 81,
57
+ 104,
58
+ 113,
59
+ 92,
60
+ 49,
61
+ 64,
62
+ 78,
63
+ 87,
64
+ 103,
65
+ 121,
66
+ 120,
67
+ 101,
68
+ 72,
69
+ 92,
70
+ 95,
71
+ 98,
72
+ 112,
73
+ 100,
74
+ 103,
75
+ 99
76
+ ], o = [
77
+ 17,
78
+ 18,
79
+ 24,
80
+ 47,
81
+ 99,
82
+ 99,
83
+ 99,
84
+ 99,
85
+ 18,
86
+ 21,
87
+ 26,
88
+ 66,
89
+ 99,
90
+ 99,
91
+ 99,
92
+ 99,
93
+ 24,
94
+ 26,
95
+ 56,
96
+ 99,
97
+ 99,
98
+ 99,
99
+ 99,
100
+ 99,
101
+ 47,
102
+ 66,
103
+ 99,
104
+ 99,
105
+ 99,
106
+ 99,
107
+ 99,
108
+ 99,
109
+ ...Array(32).fill(99)
110
+ ], s = [
111
+ 0,
112
+ 1,
113
+ 8,
114
+ 16,
115
+ 9,
116
+ 2,
117
+ 3,
118
+ 10,
119
+ 17,
120
+ 24,
121
+ 32,
122
+ 25,
123
+ 18,
124
+ 11,
125
+ 4,
126
+ 5,
127
+ 12,
128
+ 19,
129
+ 26,
130
+ 33,
131
+ 40,
132
+ 48,
133
+ 41,
134
+ 34,
135
+ 27,
136
+ 20,
137
+ 13,
138
+ 6,
139
+ 7,
140
+ 14,
141
+ 21,
142
+ 28,
143
+ 35,
144
+ 42,
145
+ 49,
146
+ 56,
147
+ 57,
148
+ 50,
149
+ 43,
150
+ 36,
151
+ 29,
152
+ 22,
153
+ 15,
154
+ 23,
155
+ 30,
156
+ 37,
157
+ 44,
158
+ 51,
159
+ 58,
160
+ 59,
161
+ 52,
162
+ 45,
163
+ 38,
164
+ 31,
165
+ 39,
166
+ 46,
167
+ 53,
168
+ 60,
169
+ 61,
170
+ 54,
171
+ 47,
172
+ 55,
173
+ 62,
174
+ 63
175
+ ], c = [
176
+ 0,
177
+ 1,
178
+ 2,
179
+ 3,
180
+ 4,
181
+ 5,
182
+ 6,
183
+ 7,
184
+ 8,
185
+ 9,
186
+ 10,
187
+ 11
188
+ ], l = [
189
+ 0,
190
+ 1,
191
+ 5,
192
+ 1,
193
+ 1,
194
+ 1,
195
+ 1,
196
+ 1,
197
+ 1,
198
+ 0,
199
+ 0,
200
+ 0,
201
+ 0,
202
+ 0,
203
+ 0,
204
+ 0
205
+ ], u = [
206
+ 0,
207
+ 3,
208
+ 1,
209
+ 1,
210
+ 1,
211
+ 1,
212
+ 1,
213
+ 1,
214
+ 1,
215
+ 1,
216
+ 1,
217
+ 0,
218
+ 0,
219
+ 0,
220
+ 0,
221
+ 0
222
+ ], d = [
223
+ 0,
224
+ 2,
225
+ 1,
226
+ 3,
227
+ 3,
228
+ 2,
229
+ 4,
230
+ 3,
231
+ 5,
232
+ 5,
233
+ 4,
234
+ 4,
235
+ 0,
236
+ 0,
237
+ 1,
238
+ 125
239
+ ], f = [
240
+ 0,
241
+ 2,
242
+ 1,
243
+ 2,
244
+ 4,
245
+ 4,
246
+ 3,
247
+ 4,
248
+ 7,
249
+ 5,
250
+ 4,
251
+ 4,
252
+ 0,
253
+ 1,
254
+ 2,
255
+ 119
256
+ ], p = (e) => e.match(/../g).map((e) => parseInt(e, 16)), m = p("01020300041105122131410613516107227114328191a1082342b1c11552d1f02433627282090a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9fa"), h = p("000102031104052131061241510761711322328108144291a1b1c109233352f0156272d10a162434e125f11718191a262728292a35363738393a434445464748494a535455565758595a636465666768696a737475767778797a82838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae2e3e4e5e6e7e8e9eaf2f3f4f5f6f7f8f9fa");
257
+ function g(e, t) {
258
+ let n = [], r = 0, i = 0;
259
+ return e.forEach((e, a) => {
260
+ for (let o = 0; o < e; o++) n[t[i++]] = [r++, a + 1];
261
+ r <<= 1;
262
+ }), n;
263
+ }
264
+ var _ = class {
265
+ constructor() {
266
+ this.bytes = /* @__PURE__ */ new Uint8Array(65536), this.length = 0, this.#e = 0, this.#t = 0;
267
+ }
268
+ #e;
269
+ #t;
270
+ #n(e) {
271
+ if (this.length === this.bytes.length) {
272
+ let e = new Uint8Array(this.bytes.length * 2);
273
+ e.set(this.bytes), this.bytes = e;
274
+ }
275
+ this.bytes[this.length++] = e;
276
+ }
277
+ write(e, t) {
278
+ for (let n = t - 1; n >= 0; n--) this.#e = this.#e << 1 | e >> n & 1, ++this.#t === 8 && (this.#n(this.#e), this.#e === 255 && this.#n(0), this.#e = 0, this.#t = 0);
279
+ }
280
+ finish() {
281
+ for (; this.#t;) this.write(1, 1);
282
+ }
283
+ }, v = /* @__PURE__ */ new Float64Array(64);
284
+ for (let e = 0; e < 8; e++) for (let t = 0; t < 8; t++) v[e * 8 + t] = (e ? .5 : Math.SQRT1_2 / 2) * Math.cos((2 * t + 1) * e * Math.PI / 16);
285
+ function y(e, t, n) {
286
+ for (let n = 0; n < 8; n++) for (let r = 0; r < 8; r++) {
287
+ let i = 0;
288
+ for (let t = 0; t < 8; t++) i += e[n * 8 + t] * v[r * 8 + t];
289
+ t[n * 8 + r] = i;
290
+ }
291
+ for (let e = 0; e < 8; e++) for (let r = 0; r < 8; r++) {
292
+ let i = 0;
293
+ for (let n = 0; n < 8; n++) i += t[n * 8 + r] * v[e * 8 + n];
294
+ n[e * 8 + r] = i;
295
+ }
296
+ }
297
+ var b = (e) => e ? 32 - Math.clz32(Math.abs(e)) : 0;
298
+ function x(n, r = {}) {
299
+ let { quality: p = 90, subsampling: v = "420", background: x = [
300
+ 255,
301
+ 255,
302
+ 255
303
+ ] } = r;
304
+ i(n);
305
+ let { width: S, height: C, data: w } = n;
306
+ if (!(p >= 1 && p <= 100)) throw e("Invalid JPEG quality", "headless");
307
+ if (v !== "420" && v !== "444") throw t("JPEG subsampling: 420 or 444", "headless");
308
+ if (S > 65535 || C > 65535) throw e("Image too large for this format", "headless");
309
+ let T = p < 50 ? 5e3 / p : 200 - p * 2, E = (e) => e.map((e) => Math.min(255, Math.max(1, Math.floor((e * T + 50) / 100)))), D = E(a), O = E(o), k = S * C, A = new Float32Array(k), j = new Float32Array(k), M = new Float32Array(k);
310
+ for (let e = 0; e < k; e++) {
311
+ let t = w[e * 4 + 3] / 255, n = w[e * 4] * t + x[0] * (1 - t), r = w[e * 4 + 1] * t + x[1] * (1 - t), i = w[e * 4 + 2] * t + x[2] * (1 - t);
312
+ A[e] = .299 * n + .587 * r + .114 * i - 128, j[e] = -.168736 * n - .331264 * r + .5 * i, M[e] = .5 * n - .418688 * r - .081312 * i;
313
+ }
314
+ let N = new _(), P = v === "420" ? 2 : 1, F = {
315
+ luma: [g(l, c), g(d, m)],
316
+ chroma: [g(u, c), g(f, h)]
317
+ }, I = /* @__PURE__ */ new Float64Array(64), L = /* @__PURE__ */ new Float64Array(64), R = /* @__PURE__ */ new Float64Array(64), z = [
318
+ 0,
319
+ 0,
320
+ 0
321
+ ], B = (e, t, n, r) => {
322
+ for (let i = 0; i < 8; i++) for (let a = 0; a < 8; a++) {
323
+ let o = 0;
324
+ for (let s = 0; s < r; s++) {
325
+ let c = Math.min(C - 1, n + i * r + s) * S;
326
+ for (let n = 0; n < r; n++) o += e[c + Math.min(S - 1, t + a * r + n)];
327
+ }
328
+ I[i * 8 + a] = o / (r * r);
329
+ }
330
+ }, V = (e, t, [n, r]) => {
331
+ y(I, L, R);
332
+ let i = Math.round(R[0] / t[0]), a = i - z[e], o = b(a);
333
+ z[e] = i, N.write(n[o][0], n[o][1]), o && N.write(a < 0 ? a + (1 << o) - 1 : a, o);
334
+ let c = 0;
335
+ for (let e = 1; e < 64; e++) {
336
+ let n = s[e], i = Math.round(R[n] / t[n]);
337
+ if (!i) {
338
+ c++;
339
+ continue;
340
+ }
341
+ for (; c > 15; c -= 16) N.write(r[240][0], r[240][1]);
342
+ let a = b(i), o = r[c << 4 | a];
343
+ N.write(o[0], o[1]), N.write(i < 0 ? i + (1 << a) - 1 : i, a), c = 0;
344
+ }
345
+ c && N.write(r[0][0], r[0][1]);
346
+ }, H = 8 * P;
347
+ for (let e = 0; e < C; e += H) for (let t = 0; t < S; t += H) {
348
+ for (let n = 0; n < P; n++) for (let r = 0; r < P; r++) B(A, t + r * 8, e + n * 8, 1), V(0, D, F.luma);
349
+ B(j, t, e, P), V(1, O, F.chroma), B(M, t, e, P), V(2, O, F.chroma);
350
+ }
351
+ N.finish();
352
+ let U = (e, ...t) => [
353
+ 255,
354
+ e,
355
+ t.length + 2 >> 8,
356
+ t.length + 2 & 255,
357
+ ...t
358
+ ], W = (e) => s.map((t) => e[t]), G = [
359
+ 255,
360
+ 216,
361
+ ...U(224, 74, 70, 73, 70, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0),
362
+ ...U(219, 0, ...W(D), 1, ...W(O)),
363
+ ...U(192, 8, C >> 8, C & 255, S >> 8, S & 255, 3, 1, P << 4 | P, 0, 2, 17, 1, 3, 17, 1),
364
+ ...U(196, 0, ...l, ...c, 16, ...d, ...m, 1, ...u, ...c, 17, ...f, ...h),
365
+ ...U(218, 3, 1, 0, 2, 17, 3, 17, 0, 63, 0)
366
+ ], K = new Uint8Array(G.length + N.length + 2);
367
+ return K.set(G), K.set(N.bytes.subarray(0, N.length), G.length), K.set([255, 217], G.length + N.length), K;
368
+ }
369
+ var S = class {
370
+ constructor() {
371
+ this.bytes = /* @__PURE__ */ new Uint8Array(65536), this.length = 0, this.#e = 0, this.#t = 0;
372
+ }
373
+ #e;
374
+ #t;
375
+ write(e, t) {
376
+ for (this.#e |= e << this.#t, this.#t += t; this.#t >= 8;) {
377
+ if (this.length === this.bytes.length) {
378
+ let e = new Uint8Array(this.bytes.length * 2);
379
+ e.set(this.bytes), this.bytes = e;
380
+ }
381
+ this.bytes[this.length++] = this.#e & 255, this.#e >>>= 8, this.#t -= 8;
382
+ }
383
+ }
384
+ finish() {
385
+ this.#t && this.write(0, 8 - this.#t);
386
+ }
387
+ };
388
+ function C(e, t) {
389
+ let n = new Uint8Array(e.length);
390
+ for (let r = 1;; r *= 2) {
391
+ let i = [];
392
+ for (let t = 0; t < e.length; t++) e[t] && i.push({
393
+ weight: Math.max(e[t], r),
394
+ symbol: t
395
+ });
396
+ i.sort((e, t) => e.weight - t.weight);
397
+ let a = [], o = 0, s = 0, c = () => o < i.length && (s >= a.length || i[o].weight <= a[s].weight) ? i[o++] : a[s++];
398
+ for (; i.length - o + a.length - s > 1;) {
399
+ let e = c(), t = c();
400
+ a.push({
401
+ weight: e.weight + t.weight,
402
+ symbol: -1,
403
+ left: e,
404
+ right: t
405
+ });
406
+ }
407
+ let l = a.at(-1), u = 0;
408
+ n.fill(0);
409
+ let d = (e, t) => {
410
+ e.left ? (d(e.left, t + 1), d(e.right, t + 1)) : (n[e.symbol] = t, t > u && (u = t));
411
+ };
412
+ if (l && d(l, 0), u <= t) return n;
413
+ }
414
+ }
415
+ function w(e) {
416
+ let t = /* @__PURE__ */ new Uint16Array(17), n = /* @__PURE__ */ new Uint16Array(17), r = new Uint16Array(e.length);
417
+ for (let n of e) t[n]++;
418
+ t[0] = 0;
419
+ for (let e = 1; e <= 15; e++) n[e + 1] = n[e] + t[e] << 1;
420
+ return e.forEach((e, t) => {
421
+ if (!e) return;
422
+ let i = n[e]++, a = 0;
423
+ for (let t = 0; t < e; t++, i >>= 1) a = a << 1 | i & 1;
424
+ r[t] = a;
425
+ }), r;
426
+ }
427
+ var T = [
428
+ 17,
429
+ 18,
430
+ 0,
431
+ 1,
432
+ 2,
433
+ 3,
434
+ 4,
435
+ 5,
436
+ 16,
437
+ 6,
438
+ 7,
439
+ 8,
440
+ 9,
441
+ 10,
442
+ 11,
443
+ 12,
444
+ 13,
445
+ 14,
446
+ 15
447
+ ], E = 4096, D = (1 << 20) - 121, O = 40, k = 120, A = 3;
448
+ function j(e, t) {
449
+ let n = [];
450
+ for (let e = 0; e < t.length && n.length < 2; e++) t[e] && n.push(e);
451
+ if (n.length < 2) {
452
+ let r = n[0] ?? 0;
453
+ return e.write(1, 1), e.write(0, 1), e.write(r < 2 ? 0 : 1, 1), e.write(r, r < 2 ? 1 : 8), {
454
+ lengths: new Uint8Array(t.length),
455
+ codes: new Uint16Array(t.length)
456
+ };
457
+ }
458
+ let r = C(t, 15), i = [];
459
+ for (let e = 0; e < r.length;) {
460
+ let t = 1;
461
+ if (r[e] === 0) {
462
+ for (; e + t < r.length && r[e + t] === 0 && t < 138;) t++;
463
+ t >= 11 ? i.push([
464
+ 18,
465
+ t - 11,
466
+ 7
467
+ ]) : t >= 3 ? i.push([
468
+ 17,
469
+ t - 3,
470
+ 3
471
+ ]) : (t = 1, i.push([
472
+ 0,
473
+ 0,
474
+ 0
475
+ ]));
476
+ } else i.push([
477
+ r[e],
478
+ 0,
479
+ 0
480
+ ]);
481
+ e += t;
482
+ }
483
+ let a = /* @__PURE__ */ new Uint32Array(19);
484
+ for (let [e] of i) a[e]++;
485
+ a.filter(Boolean).length < 2 && (a[+!!a[0]] = 1);
486
+ let o = C(a, 7), s = w(o), c = 19;
487
+ for (; c > 4 && !o[T[c - 1]];) c--;
488
+ e.write(0, 1), e.write(c - 4, 4);
489
+ for (let t = 0; t < c; t++) e.write(o[T[t]], 3);
490
+ e.write(0, 1);
491
+ for (let [t, n, r] of i) e.write(s[t], o[t]), r && e.write(n, r);
492
+ return {
493
+ lengths: r,
494
+ codes: w(r)
495
+ };
496
+ }
497
+ function M(e) {
498
+ let t = e - 1;
499
+ if (t < 4) return [
500
+ t,
501
+ 0,
502
+ 0
503
+ ];
504
+ let n = 31 - Math.clz32(t), r = n - 1;
505
+ return [
506
+ 2 * n + (t >> r & 1),
507
+ r,
508
+ t & (1 << r) - 1
509
+ ];
510
+ }
511
+ function N(e, t, n) {
512
+ e.write(0, 1), n && e.write(0, 1);
513
+ let r = [], i = [], a = (/* @__PURE__ */ new Int32Array(65536)).fill(-1), o = (e) => (Math.imul(t[e] ^ Math.imul(t[e + 1], 2654435761), 2246822507) ^ t[e + 2]) >>> 16, s = t.length, c = (e) => {
514
+ e + 2 < s && (a[o(e) & 65535] = e);
515
+ };
516
+ for (let e = 0; e < s;) {
517
+ let n = 0, l = 0, u = [e - 1, e + 2 < s ? a[o(e) & 65535] : -1];
518
+ for (let r of u) {
519
+ if (r < 0 || e - r > D) continue;
520
+ let i = 0;
521
+ for (; i < E && e + i < s && t[r + i] === t[e + i];) i++;
522
+ i > n && (n = i, l = e - r);
523
+ }
524
+ if (n >= A) {
525
+ r.push(n), i.push(l);
526
+ for (let t = 0; t < n; t++) c(e + t);
527
+ e += n;
528
+ } else r.push(0), i.push(e), c(e), e++;
529
+ }
530
+ let l = /* @__PURE__ */ new Uint32Array(280), u = /* @__PURE__ */ new Uint32Array(256), d = /* @__PURE__ */ new Uint32Array(256), f = /* @__PURE__ */ new Uint32Array(256), p = new Uint32Array(O);
531
+ r.forEach((e, n) => {
532
+ if (e) l[256 + M(e)[0]]++, p[M(i[n] + k)[0]]++;
533
+ else {
534
+ let e = t[i[n]];
535
+ l[e >>> 8 & 255]++, u[e >>> 16 & 255]++, d[e & 255]++, f[e >>> 24]++;
536
+ }
537
+ });
538
+ let m = j(e, l), h = j(e, u), g = j(e, d), _ = j(e, f), v = j(e, p);
539
+ r.forEach((n, r) => {
540
+ if (n) {
541
+ let [t, a, o] = M(n), [s, c, l] = M(i[r] + k);
542
+ e.write(m.codes[256 + t], m.lengths[256 + t]), a && e.write(o, a), e.write(v.codes[s], v.lengths[s]), c && e.write(l, c);
543
+ } else {
544
+ let n = t[i[r]], a = n >>> 8 & 255, o = n >>> 16 & 255, s = n & 255, c = n >>> 24;
545
+ e.write(m.codes[a], m.lengths[a]), e.write(h.codes[o], h.lengths[o]), e.write(g.codes[s], g.lengths[s]), e.write(_.codes[c], _.lengths[c]);
546
+ }
547
+ });
548
+ }
549
+ var P = (e, t) => ((e >>> 24) - (t >>> 24) & 255) << 24 | ((e >>> 16) - (t >>> 16) & 255) << 16 | ((e >>> 8) - (t >>> 8) & 255) << 8 | (e & 255) - (t & 255) & 255, F = (e, t) => ((e >>> 24) + (t >>> 24) >> 1 & 255) << 24 | ((e >>> 16 & 255) + (t >>> 16 & 255) >> 1 & 255) << 16 | ((e >>> 8 & 255) + (t >>> 8 & 255) >> 1 & 255) << 8 | (e & 255) + (t & 255) >> 1, I = 1, L = 2, R = 7;
550
+ function z(e, t, n) {
551
+ let r = Math.ceil(t / 16), i = Math.ceil(n / 16), a = new Uint32Array(r * i), o = e.slice(), s = (e, n, r) => {
552
+ let i = r * t + n;
553
+ if (!n && !r) return 4278190080;
554
+ if (!r) return o[i - 1];
555
+ if (!n) return o[i - t];
556
+ let a = o[i - 1], s = o[i - t];
557
+ return e === I ? a : e === L ? s : F(a, s);
558
+ }, c = (e) => {
559
+ let t = (e) => Math.min(e, 256 - e);
560
+ return t(e >>> 24) + t(e >>> 16 & 255) + t(e >>> 8 & 255) + t(e & 255);
561
+ };
562
+ for (let l = 0; l < i; l++) for (let i = 0; i < r; i++) {
563
+ let u = Math.min(t, (i + 1) * 16), d = Math.min(n, (l + 1) * 16), f = I, p = Infinity;
564
+ for (let e of [
565
+ I,
566
+ L,
567
+ R
568
+ ]) {
569
+ let n = 0;
570
+ for (let r = l * 16; r < d; r++) for (let a = i * 16; a < u; a++) n += c(P(o[r * t + a], s(e, a, r)));
571
+ n < p && (p = n, f = e);
572
+ }
573
+ a[l * r + i] = 4278190080 | f << 8;
574
+ for (let n = l * 16; n < d; n++) for (let r = i * 16; r < u; r++) e[n * t + r] = P(o[n * t + r], s(f, r, n)) >>> 0;
575
+ }
576
+ return a;
577
+ }
578
+ async function B(t, { quality: r } = {}) {
579
+ i(t);
580
+ let { width: a, height: o, data: s } = t;
581
+ if (a > 16384 || o > 16384) throw e("Image too large for this format", "headless");
582
+ if (r !== void 0) {
583
+ if (!(r >= 0 && r <= 1)) throw e("Invalid WebP quality", "headless");
584
+ if (typeof OffscreenCanvas != "function") throw n("unsupported", "Lossy WebP needs OffscreenCanvas", "headless");
585
+ let t = new OffscreenCanvas(a, o);
586
+ t.getContext("2d").putImageData(new ImageData(new Uint8ClampedArray(s), a, o), 0, 0);
587
+ let i = await t.convertToBlob({
588
+ type: "image/webp",
589
+ quality: r
590
+ });
591
+ if (i.type !== "image/webp") throw n("unsupported", "This runtime cannot encode lossy WebP", "headless");
592
+ return new Uint8Array(await i.arrayBuffer());
593
+ }
594
+ let c = new Uint32Array(a * o), l = !0;
595
+ for (let e = 0; e < c.length; e++) {
596
+ let t = s[e * 4], n = s[e * 4 + 1], r = s[e * 4 + 2], i = s[e * 4 + 3];
597
+ i !== 255 && (l = !1), c[e] = (i << 24 | (t - n & 255) << 16 | n << 8 | r - n & 255) >>> 0;
598
+ }
599
+ let u = new S();
600
+ u.write(47, 8), u.write(a - 1, 14), u.write(o - 1, 14), u.write(+!l, 1), u.write(0, 3), u.write(1, 1), u.write(2, 2), u.write(1, 1), u.write(0, 2), u.write(2, 3), N(u, z(c, a, o), !1), u.write(0, 1), N(u, c, !0), u.finish();
601
+ let d = u.length + (u.length & 1), f = new Uint8Array(20 + d), p = new DataView(f.buffer), m = (e, t) => f.set([...e].map((e) => e.charCodeAt(0)), t);
602
+ return m("RIFF", 0), p.setUint32(4, 12 + d, !0), m("WEBP", 8), m("VP8L", 12), p.setUint32(16, u.length, !0), f.set(u.bytes.subarray(0, u.length), 20), f;
603
+ }
604
+ function V({ width: e, height: t, data: n }) {
605
+ let r = /* @__PURE__ */ new Float32Array(256), i = new Float32Array(n.length);
606
+ for (let e = 0; e < 256; e++) {
607
+ let t = e / 255;
608
+ r[e] = t <= .04045 ? t / 12.92 : ((t + .055) / 1.055) ** 2.4;
609
+ }
610
+ for (let e = 0; e < n.length; e += 4) i[e] = r[n[e]], i[e + 1] = r[n[e + 1]], i[e + 2] = r[n[e + 2]], i[e + 3] = n[e + 3] / 255;
611
+ return {
612
+ width: e,
613
+ height: t,
614
+ data: i
615
+ };
616
+ }
617
+ async function H({ width: r, height: i, data: a }, { type: o = "half", signal: s } = {}) {
618
+ if (!(a instanceof Float32Array) || a.length !== r * i * 4) throw e("Frame data does not match its size", "headless");
619
+ if (o !== "half" && o !== "float") throw t("EXR type: half or float", "headless");
620
+ let c;
621
+ try {
622
+ c = await import("@margoengine/environment/exr-write");
623
+ } catch (e) {
624
+ throw e.code === "ERR_MODULE_NOT_FOUND" ? Object.assign(n("state", "Install @margoengine/environment", "install"), { cause: e }) : e;
625
+ }
626
+ let l = o === "half" ? "HALF" : "FLOAT", u = (e, t) => ({
627
+ name: e,
628
+ type: l,
629
+ data: a,
630
+ offset: t,
631
+ stride: 4
632
+ });
633
+ return c.encodeEXR({
634
+ width: r,
635
+ height: i,
636
+ channels: [
637
+ u("R", 0),
638
+ u("G", 1),
639
+ u("B", 2),
640
+ u("A", 3)
641
+ ]
642
+ }, { signal: s });
643
+ }
644
+ function U(e) {
645
+ let t = e & 32768 ? -1 : 1, n = e >> 10 & 31, r = e & 1023;
646
+ return n === 0 ? t * r * 2 ** -24 : n === 31 ? r ? NaN : t * Infinity : t * (1 + r / 1024) * 2 ** (n - 15);
647
+ }
648
+ var W = 256, G = 1;
649
+ async function K(e, t, { signal: i } = {}) {
650
+ i?.throwIfAborted();
651
+ let a = r(e).gpu;
652
+ if (!a) throw n("unsupported", "readPixels needs the WebGPU backend", "headless");
653
+ let { format: o, width: s, height: c } = t;
654
+ if (o !== "rgba16float" && o !== "rgba32float") throw n("unsupported", "readTexture reads rgba16float or rgba32float only", "headless");
655
+ let l = o === "rgba16float", u = s * (l ? 8 : 16), d = Math.ceil(u / W) * W, f = a.createBuffer({
656
+ label: "headless float readback",
657
+ size: d * c,
658
+ usage: 9
659
+ }), p = () => f.destroy();
660
+ i?.addEventListener("abort", p, { once: !0 });
661
+ try {
662
+ let e = a.createCommandEncoder();
663
+ e.copyTextureToBuffer({ texture: t }, {
664
+ buffer: f,
665
+ bytesPerRow: d,
666
+ rowsPerImage: c
667
+ }, [s, c]), a.queue.submit([e.finish()]), await f.mapAsync(G).catch((e) => {
668
+ throw i?.throwIfAborted(), e;
669
+ });
670
+ let n = f.getMappedRange(), r = new Float32Array(s * c * 4), o = s * 4;
671
+ for (let e = 0; e < c; e++) if (l) {
672
+ let t = new Uint16Array(n, e * d, o);
673
+ for (let n = 0; n < o; n++) r[e * o + n] = U(t[n]);
674
+ } else r.set(new Float32Array(n, e * d, o), e * o);
675
+ return {
676
+ width: s,
677
+ height: c,
678
+ data: r
679
+ };
680
+ } finally {
681
+ i?.removeEventListener("abort", p), f.destroy();
682
+ }
683
+ }
684
+ //#endregion
685
+ export { H as encodeEXR, x as encodeJPEG, B as encodeWebP, V as linearPixels, K as readTexture };
@@ -0,0 +1,33 @@
1
+ import type { Engine } from '@margoengine/core';
2
+ import type { HeadlessPixels } from './index.js';
3
+ /** Options of {@link createFrameReader}. */
4
+ export interface FrameReaderOptions {
5
+ /** Frames held at once, a whole number from 1 to 16. 1 waits for each frame. Default 3. */
6
+ depth?: number;
7
+ /** Disposes the reader; waiting reads reject with the signal's reason. Default none. */
8
+ signal?: AbortSignal;
9
+ }
10
+ /** Reads the frames of one headless engine in the order they were drawn. See {@link createFrameReader}. */
11
+ export interface FrameReader {
12
+ /** Frames held at once. */
13
+ readonly depth: number;
14
+ /** Frames copied and not yet handed out. */
15
+ readonly pending: number;
16
+ /**
17
+ * Copies the frame just rendered, at the moment of the call. Resolves to the oldest held frame once `depth` frames are
18
+ * held, else to `undefined`. Call it once after each `engine.render()`.
19
+ */
20
+ push(): Promise<HeadlessPixels | undefined>;
21
+ /** The frames still held, oldest first. */
22
+ drain(): AsyncGenerator<HeadlessPixels, void, void>;
23
+ /** Frees the buffers. Held frames are lost. Safe to call twice. */
24
+ dispose(): void;
25
+ /** `dispose()`, for `using`. */
26
+ [Symbol.dispose](): void;
27
+ }
28
+ /**
29
+ * A reader for rendering many frames of an engine on a `createHeadlessCanvas` canvas. It gives the same pixels as
30
+ * `readPixels` after each frame, `depth - 1` frames later, and reuses `depth` readback buffers instead of waiting for
31
+ * the GPU after each frame. Dispose it when done, before the engine.
32
+ */
33
+ export declare function createFrameReader(engine: Engine, { depth, signal }?: FrameReaderOptions): FrameReader;