@mce/bigesj 0.11.0 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,312 @@
1
+ const $ = ["87a", "89a"];
2
+ function j(r) {
3
+ const o = new Uint8Array(
4
+ r.reduce((e, l) => e + l.byteLength, 0)
5
+ );
6
+ return r.reduce((e, l) => (o.set(l, e), e + l.byteLength), 0), o;
7
+ }
8
+ function M(r, o, e) {
9
+ let l;
10
+ if (ArrayBuffer.isView(r))
11
+ l = r.buffer;
12
+ else if (r instanceof ArrayBuffer)
13
+ l = r;
14
+ else {
15
+ const c = document.createElement("canvas"), { width: u, height: i } = {}, d = c.getContext("2d");
16
+ if (!d)
17
+ throw new Error("Failed to create canvas context2d");
18
+ c.width = u ?? ("width" in r ? typeof r.width == "number" ? r.width : r.width.baseVal.value : 0), c.height = i ?? ("height" in r ? typeof r.height == "number" ? r.height : r.height.baseVal.value : 0), d.drawImage(r, 0, 0, c.width, c.height), l = d.getImageData(0, 0, c.width, c.height).data.buffer;
19
+ }
20
+ switch (o) {
21
+ case "uint8Array":
22
+ return new Uint8Array(l);
23
+ case "uint8ClampedArray":
24
+ return new Uint8ClampedArray(l);
25
+ case "dataView":
26
+ return new DataView(l);
27
+ default:
28
+ throw new Error("Unsupported output format");
29
+ }
30
+ }
31
+ class V {
32
+ _view;
33
+ offset = 0;
34
+ constructor(o) {
35
+ this._view = M(o, "dataView");
36
+ }
37
+ readByte() {
38
+ return this._view.getUint8(this.offset++);
39
+ }
40
+ readBytes(o) {
41
+ return Array.from({ length: o }).map(() => this.readByte());
42
+ }
43
+ readString(o) {
44
+ return String.fromCharCode(...this.readBytes(o));
45
+ }
46
+ readUnsigned() {
47
+ return [this._view.getUint16(this.offset, !0), this.offset += 2][0];
48
+ }
49
+ readBits() {
50
+ return this._view.getUint8(this.offset++).toString(2).padStart(8, "0").split("").map(Number);
51
+ }
52
+ readColorTable(o) {
53
+ return Array.from({ length: o }, () => Array.from(this.readBytes(3)));
54
+ }
55
+ readSubBlock() {
56
+ const o = [];
57
+ for (; ; ) {
58
+ const e = this.readByte();
59
+ if (e === 0 && this._view.getUint8(this.offset) !== 0)
60
+ break;
61
+ o.push(e);
62
+ }
63
+ return o;
64
+ }
65
+ }
66
+ function H(r) {
67
+ const o = {}, e = new V(r), l = () => ({ index: 0, delay: 100, disposal: 0 }), c = e.readString(3), u = e.readString(3);
68
+ if (c !== "GIF" || !$.includes(u))
69
+ throw new Error("This is not a 87a/89a GIF data.");
70
+ o.version = u, o.width = e.readUnsigned(), o.height = e.readUnsigned();
71
+ const i = e.readBits();
72
+ o.globalColorTable = !!i[0], o.colorResoluTion = Number.parseInt(i.slice(1, 4).join(""), 2) + 1, o.colorTableSorted = !!i[4];
73
+ const d = Number.parseInt(i.slice(5, 8).join(""), 2);
74
+ o.colorTableSize = 2 ** (d + 1), o.backgroundColorIndex = e.readByte(), o.pixelAspectRatio = e.readByte(), o.globalColorTable && (o.colorTableSize ? o.colorTable = e.readColorTable(o.colorTableSize) : e.readSubBlock()), o.frames = [];
75
+ let t = l();
76
+ const g = [], f = [];
77
+ for (; ; ) {
78
+ const n = e.readByte();
79
+ if (g.push(n), n === 44) {
80
+ t.left = e.readUnsigned(), t.top = e.readUnsigned(), t.width = e.readUnsigned(), t.height = e.readUnsigned();
81
+ const a = e.readBits();
82
+ t.localColorTable = !!a[0], t.interlaced = !!a[1], t.colorTableSorted = !!a[2], t.reserved = Number.parseInt(a.slice(3, 5).join(""), 2);
83
+ const s = Number.parseInt(a.slice(5, 8).join(""), 2);
84
+ for (t.colorTableSize = 2 ** (s + 1), t.localColorTable && (t.colorTable = e.readColorTable(t.colorTableSize)), t.lzwMinCodeSize = e.readByte(), t.dataPositions = []; ; ) {
85
+ const h = e.readByte();
86
+ if (h === 0)
87
+ break;
88
+ const p = e.offset;
89
+ t.dataPositions.push([p, h]), e.offset = p + h;
90
+ }
91
+ o.frames.push(t), t = l(), t.index = o.frames.length;
92
+ continue;
93
+ }
94
+ if (n === 33) {
95
+ const a = e.readByte();
96
+ if (f.push(a), a === 255) {
97
+ if (e.readByte() !== 11)
98
+ continue;
99
+ const s = {
100
+ identifier: e.readString(8),
101
+ code: e.readString(3),
102
+ data: []
103
+ };
104
+ `${s.identifier}${s.code}` == "NETSCAPE2.0" && e.readByte() === 3 && (o.looped = !!e.readByte(), o.loopCount = e.readUnsigned()), s.data = e.readSubBlock(), t.application = s;
105
+ continue;
106
+ }
107
+ if (a === 254) {
108
+ t.comment = e.readSubBlock().map((s) => String.fromCharCode(s)).join("");
109
+ continue;
110
+ }
111
+ if (a === 249) {
112
+ if (e.readByte() !== 4)
113
+ continue;
114
+ const s = e.readBits(), h = {
115
+ // ↓ <Packed Fields>
116
+ reserved: Number.parseInt(s.slice(0, 3).join(""), 2),
117
+ disposal: Number.parseInt(s.slice(3, 6).join(""), 2),
118
+ userInput: !!s[6],
119
+ transparent: !!s[7],
120
+ // ↑ <Packed Fields>
121
+ delayTime: e.readUnsigned(),
122
+ transparentIndex: e.readByte()
123
+ };
124
+ e.readSubBlock(), t.graphicControl = h, t.disposal = h.disposal, t.delay = (h.delayTime || 10) * 10;
125
+ continue;
126
+ }
127
+ if (a === 1) {
128
+ if (e.readByte() !== 1)
129
+ continue;
130
+ t.plainText = {
131
+ left: e.readUnsigned(),
132
+ top: e.readUnsigned(),
133
+ width: e.readUnsigned(),
134
+ height: e.readUnsigned(),
135
+ cellWidth: e.readByte(),
136
+ cellHeight: e.readByte(),
137
+ colorIndex: e.readByte(),
138
+ backgroundColorIndex: e.readByte(),
139
+ data: e.readSubBlock()
140
+ };
141
+ continue;
142
+ }
143
+ console.warn(
144
+ `Unknown extension block: 0x${a.toString(16)}`,
145
+ g.slice(0, g.length - 1).map((s) => `0x${s.toString(16)}`),
146
+ f.slice(0, f.length - 1).map((s) => `0x${s.toString(16)}`)
147
+ );
148
+ continue;
149
+ }
150
+ if (n === 59)
151
+ break;
152
+ console.warn(
153
+ `Unknown block: 0x${n.toString(16)}`,
154
+ g.slice(0, g.length - 1).map((a) => `0x${a.toString(16)}`),
155
+ f.slice(0, f.length - 1).map((a) => `0x${a.toString(16)}`)
156
+ );
157
+ }
158
+ return o;
159
+ }
160
+ function W(r) {
161
+ const o = /* @__PURE__ */ new Map(), { workerUrl: e } = r;
162
+ let { workerNumber: l = 1 } = r;
163
+ const c = [...Array.from({ length: e ? l : 0 })].map(() => {
164
+ try {
165
+ const t = new Worker(e);
166
+ return t.onmessage = u, t;
167
+ } catch (t) {
168
+ return console.warn(t), null;
169
+ }
170
+ }).filter(Boolean);
171
+ l = c.length;
172
+ function u(t) {
173
+ const { id: g, data: f } = t.data;
174
+ o.get(g)?.(f), o.delete(g);
175
+ }
176
+ const i = /* @__PURE__ */ (function() {
177
+ let t = 0;
178
+ return (g) => c[(g ?? t++) % l];
179
+ })();
180
+ return {
181
+ call: /* @__PURE__ */ (function() {
182
+ let t = 0;
183
+ return (g, f, n, a) => new Promise((s) => {
184
+ const h = i(a);
185
+ if (!h)
186
+ return s(void 0);
187
+ o.set(t, s), h.postMessage({ id: t++, type: g, data: f }, { transfer: n });
188
+ });
189
+ })()
190
+ };
191
+ }
192
+ function q(r, o) {
193
+ const e = Array.from({ length: r.length }), l = r.length / o, c = [0, 4, 2, 1], u = [8, 8, 4, 2];
194
+ let i = 0;
195
+ for (let d = 0; d < 4; d++)
196
+ for (let t = c[d]; t < l; t += u[d])
197
+ e.splice.apply(
198
+ e,
199
+ [t * o, o].concat(
200
+ r.slice(i * o, (i + 1) * o)
201
+ )
202
+ ), i++;
203
+ return e;
204
+ }
205
+ function J(r, o, e) {
206
+ const u = e;
207
+ let i, d, t, g, f, n, a;
208
+ const s = Array.from({ length: e }), h = Array.from({ length: 4096 }), p = Array.from({ length: 4096 }), T = Array.from({ length: 4097 }), E = r, I = 1 << E, U = I + 1;
209
+ for (i = I + 2, f = -1, t = E + 1, d = (1 << t) - 1, n = 0; n < I; n++)
210
+ h[n] = 0, p[n] = n;
211
+ let N, C, m, S, B, O;
212
+ for (N = C = m = S = B = O = 0, a = 0; a < u; ) {
213
+ if (S === 0) {
214
+ if (C < t) {
215
+ N += o[O] << C, C += 8, O++;
216
+ continue;
217
+ }
218
+ if (n = N & d, N >>= t, C -= t, n > i || n === U)
219
+ break;
220
+ if (n === I) {
221
+ t = E + 1, d = (1 << t) - 1, i = I + 2, f = -1;
222
+ continue;
223
+ }
224
+ if (f === -1) {
225
+ T[S++] = p[n], f = n, m = n;
226
+ continue;
227
+ }
228
+ for (g = n, n === i && (T[S++] = m, n = f); n > I; )
229
+ T[S++] = p[n], n = h[n];
230
+ m = p[n] & 255, T[S++] = m, i < 4096 && (h[i] = f, p[i] = m, i++, (i & d) === 0 && i < 4096 && (t++, d += i)), f = g;
231
+ }
232
+ S--, s[B++] = T[S], a++;
233
+ }
234
+ for (a = B; a < u; a++)
235
+ s[a] = 0;
236
+ return s;
237
+ }
238
+ function Q(r, o) {
239
+ const e = M(r, "uint8Array");
240
+ if (o?.workerUrl)
241
+ return W({ workerUrl: o.workerUrl }).call(
242
+ "frames:decode",
243
+ e,
244
+ [e.buffer]
245
+ );
246
+ const {
247
+ gif: l = H(r),
248
+ range: c
249
+ } = o ?? {}, {
250
+ width: u,
251
+ height: i,
252
+ colorTable: d,
253
+ frames: t
254
+ } = l, g = c ? t.slice(c[0], c[1] + 1) : t, f = g.some((h) => h.disposal === 3);
255
+ let n = new Uint8ClampedArray(u * i * 4), a, s = n.slice();
256
+ return g.map((h) => {
257
+ const {
258
+ left: p,
259
+ top: T,
260
+ width: E,
261
+ height: I,
262
+ interlaced: U,
263
+ localColorTable: N,
264
+ colorTable: C,
265
+ lzwMinCodeSize: m,
266
+ dataPositions: S,
267
+ graphicControl: B,
268
+ delay: O,
269
+ disposal: L
270
+ } = h, v = a?.disposal, z = T + I, {
271
+ transparent: K,
272
+ transparentIndex: Z
273
+ } = B ?? {}, G = N ? C : d, F = K ? Z : -1, D = j(
274
+ S.map(
275
+ ([b, y]) => e.subarray(b, b + y)
276
+ )
277
+ );
278
+ let P = J(m, D, E * I);
279
+ if (U && (P = q(P, E)), v === 3)
280
+ n = s.slice();
281
+ else if (v === 2) {
282
+ const { left: b, top: y, width: X, height: A } = a, k = y + A;
283
+ for (let w = y; w < k; w++) {
284
+ const R = w * u + b;
285
+ for (let x = 0; x < X; x++) {
286
+ const _ = (R + x) * 4;
287
+ n[_] = n[_ + 1] = n[_ + 2] = n[_ + 3] = 0;
288
+ }
289
+ }
290
+ }
291
+ for (let b = T; b < z; b++) {
292
+ const y = b * u + p, X = (b - T) * E;
293
+ for (let A = 0; A < E; A++) {
294
+ const k = P[X + A], w = (y + A) * 4;
295
+ if (k !== F) {
296
+ const [R, x, _] = G?.[k] ?? [0, 0, 0];
297
+ n[w] = R, n[w + 1] = x, n[w + 2] = _, n[w + 3] = 255;
298
+ }
299
+ }
300
+ }
301
+ return f && L !== 1 && L !== 3 && (s = n.slice()), a = h, {
302
+ width: u,
303
+ height: i,
304
+ delay: O,
305
+ data: n.slice()
306
+ };
307
+ });
308
+ }
309
+ export {
310
+ H as decode,
311
+ Q as decodeFrames
312
+ };