ipx 4.0.0-alpha.2 → 4.0.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.
@@ -1,58 +1,79 @@
1
1
  const e = new TextDecoder(), t = (t, n = 0, r = t.length) => e.decode(t.slice(n, r)), n = (e, t = 0, n = e.length) => e.slice(t, n).reduce((e, t) => e + (`0` + t.toString(16)).slice(-2), ``), r = (e, t = 0) => {
2
- let n = e[t] + e[t + 1] * 2 ** 8;
3
- return n | (n & 2 ** 15) * 131070;
4
- }, i = (e, t = 0) => e[t] * 2 ** 8 + e[t + 1], a = (e, t = 0) => e[t] + e[t + 1] * 2 ** 8, o = (e, t = 0) => e[t] + e[t + 1] * 2 ** 8 + e[t + 2] * 2 ** 16, s = (e, t = 0) => e[t] + e[t + 1] * 2 ** 8 + e[t + 2] * 2 ** 16 + (e[t + 3] << 24), c = (e, t = 0) => e[t] * 2 ** 24 + e[t + 1] * 2 ** 16 + e[t + 2] * 2 ** 8 + e[t + 3], l = (e, t = 0) => e[t] + e[t + 1] * 2 ** 8 + e[t + 2] * 2 ** 16 + e[t + 3] * 2 ** 24, u = {
2
+ let n = e[t] + e[t + 1] * 256;
3
+ return n >= 2 ** 15 ? n - 2 ** 16 : n;
4
+ }, i = (e, t = 0) => e[t] * 256 + e[t + 1], a = (e, t = 0) => e[t] + e[t + 1] * 256, o = (e, t = 0) => e[t] + e[t + 1] * 256 + e[t + 2] * 2 ** 16, s = (e, t = 0) => {
5
+ let n = l(e, t);
6
+ return n >= 2 ** 31 ? n - 2 ** 32 : n;
7
+ }, c = (e, t = 0) => e[t] * 2 ** 24 + e[t + 1] * 2 ** 16 + e[t + 2] * 256 + e[t + 3], l = (e, t = 0) => e[t] + e[t + 1] * 256 + e[t + 2] * 2 ** 16 + e[t + 3] * 2 ** 24, ee = {
5
8
  readUInt16BE: i,
6
9
  readUInt16LE: a,
7
10
  readUInt32BE: c,
8
11
  readUInt32LE: l
9
12
  };
10
- function d(e, t, n, r) {
13
+ function u(e, t, n, r) {
11
14
  n ||= 0;
12
- let i = r ? `BE` : `LE`;
13
- return u[`readUInt` + t + i](e, n);
15
+ let i = r ? `BE` : `LE`, a = `readUInt` + t + i;
16
+ return ee[a](e, n);
14
17
  }
15
- const ee = {
18
+ const te = new TextEncoder(), d = (e) => c(te.encode(e));
19
+ function f(e, t, n = 0, r = e.length) {
20
+ let i = d(t), a = n;
21
+ for (; a + 8 <= r;) {
22
+ let t = c(e, a);
23
+ if (t < 8 || a + t > r) return;
24
+ if (c(e, a + 4) === i) return {
25
+ offset: a,
26
+ size: t
27
+ };
28
+ a += t;
29
+ }
30
+ }
31
+ const ne = {
16
32
  validate: (e) => t(e, 0, 2) === `BM`,
17
- calculate: (e) => ({
33
+ calculate: (e) => l(e, 14) === 12 ? {
34
+ height: a(e, 20),
35
+ width: a(e, 18)
36
+ } : {
18
37
  height: Math.abs(s(e, 22)),
19
- width: l(e, 18)
20
- })
38
+ width: s(e, 18)
39
+ }
21
40
  };
22
- function f(e, t) {
41
+ function p(e, t) {
23
42
  let n = e[t];
24
43
  return n === 0 ? 256 : n;
25
44
  }
26
- function p(e, t) {
45
+ function m(e, t) {
27
46
  let n = 6 + t * 16;
28
47
  return {
29
- height: f(e, n + 1),
30
- width: f(e, n)
48
+ height: p(e, n + 1),
49
+ width: p(e, n)
31
50
  };
32
51
  }
33
- const m = {
52
+ const h = {
34
53
  validate(e) {
35
54
  let t = a(e, 0), n = a(e, 4);
36
55
  return t !== 0 || n === 0 ? !1 : a(e, 2) === 1;
37
56
  },
38
57
  calculate(e) {
39
- let t = a(e, 4), n = p(e, 0);
58
+ let t = a(e, 4);
59
+ if (6 + t * 16 > e.length) throw TypeError(`Invalid ICO`);
60
+ let n = m(e, 0);
40
61
  if (t === 1) return n;
41
62
  let r = [n];
42
- for (let n = 1; n < t; n += 1) r.push(p(e, n));
63
+ for (let n = 1; n < t; n += 1) r.push(m(e, n));
43
64
  return {
44
65
  height: n.height,
45
66
  images: r,
46
67
  width: n.width
47
68
  };
48
69
  }
49
- }, te = {
70
+ }, re = {
50
71
  validate(e) {
51
72
  let t = a(e, 0), n = a(e, 4);
52
73
  return t !== 0 || n === 0 ? !1 : a(e, 2) === 2;
53
74
  },
54
- calculate: (e) => m.calculate(e)
55
- }, h = {
75
+ calculate: (e) => h.calculate(e)
76
+ }, ie = {
56
77
  validate: (e) => l(e, 0) === 542327876,
57
78
  calculate: (e) => ({
58
79
  height: l(e, 12),
@@ -64,65 +85,60 @@ const m = {
64
85
  height: a(e, 8),
65
86
  width: a(e, 6)
66
87
  })
67
- }, v = {
68
- validate: (e) => {
69
- let n = y(e, `ftyp`);
70
- if (!n) return !1;
71
- let r = t(e, n.offset + 8, n.offset + 12);
72
- return [
73
- `heic`,
74
- `heix`,
75
- `hevc`,
76
- `hevx`,
77
- `mif1`,
78
- `msf1`
79
- ].includes(r);
80
- },
81
- calculate: (e) => {
82
- let t = y(e, `meta`);
83
- if (!t) throw TypeError(`heic: meta box not found`);
84
- let n = y(e, `iprp`, t.offset + 12, t.offset + t.size);
85
- if (!n) throw TypeError(`heic: iprp box not found`);
86
- let r = y(e, `ipco`, n.offset + 8, n.offset + n.size);
87
- if (!r) throw TypeError(`heic: ipco box not found`);
88
- let i = b(e, `ispe`, r.offset + 8, r.offset + r.size).map((t) => ({
89
- width: c(e, t.offset + 12),
90
- height: c(e, t.offset + 16)
91
- }));
92
- if (i.length === 0) throw TypeError(`heic: ispe box not found`);
93
- let a = i[0];
94
- for (let e = 1; e < i.length; e++) {
95
- let t = i[e];
96
- t.width * t.height > a.width * a.height && (a = t);
88
+ }, v = d(`ftyp`), y = d(`ispe`), ae = d(`clap`);
89
+ function b(e, t) {
90
+ return !e || t.width * t.height > e.width * e.height ? t : e;
91
+ }
92
+ function oe(e) {
93
+ let t = f(e, `meta`), n = t && f(e, `iprp`, t.offset + 12, t.offset + t.size), r = n && f(e, `ipco`, n.offset + 8, n.offset + n.size);
94
+ if (!r) throw TypeError(`Invalid HEIF, no ipco box found`);
95
+ let i, a, o = r.offset + r.size, s = r.offset + 8;
96
+ for (; s + 8 <= o;) {
97
+ let t = c(e, s);
98
+ if (t < 8 || s + t > o) throw TypeError(`Invalid HEIF, corrupt ipco box`);
99
+ let n = c(e, s + 4);
100
+ if (n === y) {
101
+ if (t < 20) throw TypeError(`Invalid HEIF, corrupt ispe box`);
102
+ a && (i = b(i, a)), a = {
103
+ width: c(e, s + 12),
104
+ height: c(e, s + 16)
105
+ };
97
106
  }
98
- return a;
99
- }
100
- };
101
- function y(e, n, r = 0, i = e.length) {
102
- let a = r;
103
- for (; a < i;) {
104
- let r = c(e, a);
105
- if (t(e, a + 4, a + 8) === n) return {
106
- offset: a,
107
- size: r
108
- };
109
- if (r <= 0 || a + r > i) break;
110
- a += r;
107
+ if (n === ae && t >= 24 && a) {
108
+ let t = c(e, s + 12), n = c(e, s + 20);
109
+ t > 0 && n > 0 && (a.width = Math.round(c(e, s + 8) / t), a.height = Math.round(c(e, s + 16) / n));
110
+ }
111
+ s += t;
111
112
  }
113
+ if (!a) throw TypeError(`Invalid HEIF, no ispe box found`);
114
+ return b(i, a);
112
115
  }
113
- function b(e, n, r = 0, i = e.length) {
114
- let a = r, o = [];
115
- for (; a < i;) {
116
- let r = c(e, a);
117
- if (t(e, a + 4, a + 8) === n && o.push({
118
- offset: a,
119
- size: r
120
- }), r <= 0 || a + r > i) break;
121
- a += r;
116
+ const x = new Set([`avif`, `avis`].map((e) => d(e)));
117
+ function S(e) {
118
+ let n = c(e, 0);
119
+ if (c(e, 4) !== v || n < 12 || n > e.length) return;
120
+ if (x.has(c(e, 8))) return `avif`;
121
+ let r = t(e, 8, 12);
122
+ if ([
123
+ `mif1`,
124
+ `msf1`,
125
+ `miaf`
126
+ ].includes(r)) {
127
+ for (let t = 16; t + 4 <= n; t += 4) if (x.has(c(e, t))) return `avif`;
122
128
  }
123
- return o;
129
+ return [
130
+ `heic`,
131
+ `heix`,
132
+ `hevc`,
133
+ `hevx`,
134
+ `mif1`,
135
+ `msf1`
136
+ ].includes(r) ? `heic` : void 0;
124
137
  }
125
- const x = {
138
+ const C = {
139
+ validate: (e) => S(e) === `heic`,
140
+ calculate: (e) => oe(e)
141
+ }, w = {
126
142
  ICON: 32,
127
143
  "ICN#": 32,
128
144
  "icm#": 16,
@@ -134,18 +150,24 @@ const x = {
134
150
  is32: 16,
135
151
  s8mk: 16,
136
152
  icp4: 16,
153
+ ic04: 16,
154
+ icsb: 18,
155
+ sb24: 24,
137
156
  icl4: 32,
138
157
  icl8: 32,
139
158
  il32: 32,
140
159
  l8mk: 32,
141
160
  icp5: 32,
161
+ ic05: 32,
142
162
  ic11: 32,
163
+ icsB: 36,
143
164
  ich4: 48,
144
165
  ich8: 48,
145
166
  ih32: 48,
146
167
  h8mk: 48,
168
+ SB24: 48,
147
169
  icp6: 64,
148
- ic12: 32,
170
+ ic12: 64,
149
171
  it32: 128,
150
172
  t8mk: 128,
151
173
  ic07: 128,
@@ -154,131 +176,219 @@ const x = {
154
176
  ic09: 512,
155
177
  ic14: 512,
156
178
  ic10: 1024
157
- };
158
- function S(e, n) {
159
- let r = n + 4;
160
- return [t(e, n, r), c(e, r)];
179
+ }, se = new Map(Object.keys(w).map((e) => [d(e), e]));
180
+ function ce(e, t) {
181
+ let n = t + 4;
182
+ if (n + 4 > e.length) throw TypeError(`Invalid ICNS`);
183
+ let r = c(e, n);
184
+ if (r < 8) throw TypeError(`Invalid ICNS`);
185
+ return [se.get(c(e, t)), r];
161
186
  }
162
- function C(e) {
163
- let t = x[e];
187
+ function le(e) {
188
+ let t = w[e];
164
189
  return {
165
190
  width: t,
166
191
  height: t,
167
192
  type: e
168
193
  };
169
194
  }
170
- const ne = {
195
+ const ue = {
171
196
  validate: (e) => t(e, 0, 4) === `icns`,
172
197
  calculate(e) {
173
- let t = e.length, n = c(e, 4), r = 8, i = S(e, r), a = C(i[0]);
174
- if (r += i[1], r === n) return a;
175
- let o = {
176
- height: a.height,
177
- images: [a],
178
- width: a.width
198
+ let t = e.length, n = c(e, 4), r = 8, i = [], a = /* @__PURE__ */ new Set();
199
+ for (; r < n && r < t;) {
200
+ let [t, n] = ce(e, r);
201
+ r += n, t !== void 0 && !a.has(t) && (a.add(t), i.push(le(t)));
202
+ }
203
+ if (i.length === 0) throw TypeError(`Invalid ICNS, no icons found`);
204
+ return i.length === 1 ? i[0] : {
205
+ height: i[0].height,
206
+ images: i,
207
+ width: i[0].width
179
208
  };
180
- for (; r < n && r < t;) i = S(e, r), a = C(i[0]), r += i[1], o.images.push(a);
181
- return o;
182
209
  }
183
- }, re = {
184
- validate: (e) => n(e, 0, 4) === `ff4fff51`,
210
+ }, de = {
211
+ validate: (e) => c(e, 0) === 4283432785,
185
212
  calculate: (e) => ({
186
- height: c(e, 12),
187
- width: c(e, 8)
213
+ height: c(e, 12) - c(e, 20),
214
+ width: c(e, 8) - c(e, 16)
188
215
  })
189
- }, w = {
190
- ftyp: `66747970`,
191
- jp2h: `6a703268`,
192
- jp__: `6a502020`,
193
- rreq: `72726571`
194
- }, ie = (e) => {
195
- let t = e[0], n = 1 + 2 * t, r = i(e, n) * (2 + t);
196
- n = n + 2 + r;
197
- let a = i(e, n) * (16 + t);
198
- return n + 2 + a;
199
- }, T = (e) => ({
200
- height: c(e, 4),
201
- width: c(e, 8)
202
- }), E = {
216
+ }, fe = {
203
217
  validate(e) {
204
- let t = n(e, 4, 8), r = c(e, 0);
205
- if (t !== w.jp__ || r < 1) return !1;
206
- let i = r + 4, a = c(e, r);
207
- return n(e.slice(i, i + a), 0, 4) === w.ftyp;
218
+ return t(e, 4, 8) === `jP ` && f(e, `ftyp`) !== void 0;
208
219
  },
209
220
  calculate(e) {
210
- let r = c(e, 0), a = i(e, r + 2), o = r + 4 + a;
211
- switch (n(e, o, o + 4)) {
212
- case w.rreq: return o = o + 4 + 4 + ie(e.slice(o + 4)), T(e.slice(o + 8, o + 24));
213
- case w.jp2h: return T(e.slice(o + 8, o + 24));
214
- default: throw TypeError(`Unsupported header found: ` + t(e, o, o + 4));
215
- }
221
+ let t = f(e, `jp2h`), n = t && f(e, `ihdr`, t.offset + 8, t.offset + t.size);
222
+ if (!n || n.size < 16) throw TypeError(`Invalid JPEG 2000, no ihdr box found`);
223
+ return {
224
+ height: c(e, n.offset + 8),
225
+ width: c(e, n.offset + 12)
226
+ };
216
227
  }
217
- };
218
- function D(e) {
219
- return n(e, 2, 6) === `45786966`;
228
+ }, pe = d(`Exif`);
229
+ function me(e) {
230
+ return c(e, 2) === pe;
220
231
  }
221
- function O(e, t) {
232
+ function he(e, t) {
222
233
  return {
223
234
  height: i(e, t),
224
235
  width: i(e, t + 2)
225
236
  };
226
237
  }
227
- function k(e, t) {
228
- let n = d(e, 16, 14, t);
229
- for (let r = 0; r < n; r++) {
230
- let n = 16 + r * 12, i = n + 12;
231
- if (n > e.length) return;
232
- let a = e.slice(n, i);
233
- if (d(a, 16, 0, t) === 274) return d(a, 16, 2, t) !== 3 || d(a, 32, 4, t) !== 1 ? void 0 : d(a, 16, 8, t);
238
+ function ge(e, t) {
239
+ if (u(e, 16, 8, t) !== 42) return;
240
+ let n = u(e, 32, 10, t), r = 6 + n;
241
+ if (!(n >= 8 && r + 2 <= e.length)) return;
242
+ let i = u(e, 16, r, t);
243
+ for (let n = 0; n < i; n++) {
244
+ let i = r + 2 + n * 12, a = i + 12;
245
+ if (i > e.length) return;
246
+ let o = e.slice(i, a);
247
+ if (u(o, 16, 0, t) === 274) return u(o, 16, 2, t) !== 3 || u(o, 32, 4, t) !== 1 ? void 0 : u(o, 16, 8, t);
234
248
  }
235
249
  }
236
- function A(e, t) {
237
- let r = e.slice(2, t), i = n(r, 6, 8), a = i === `4d4d`;
238
- if (a || i === `4949`) return k(r, a);
250
+ function _e(e, t) {
251
+ let n = e.slice(2, t), r = i(n, 6), a = r === 19789;
252
+ if (a || r === 18761) return ge(n, a);
239
253
  }
240
- function j(e, t) {
254
+ function ve(e, t) {
241
255
  if (t > e.length) throw TypeError(`Corrupt JPG, exceeded buffer limits`);
242
- if (e[t] !== 255) throw TypeError(`Invalid JPG, marker table corrupted`);
243
256
  }
244
- const M = {
257
+ function T(e, t) {
258
+ let n = e.indexOf(255, t);
259
+ for (; n !== -1;) {
260
+ for (; e[n + 1] === 255;) n++;
261
+ if (e[n + 1] !== 0) return n;
262
+ n = e.indexOf(255, n + 2);
263
+ }
264
+ throw TypeError(`Invalid JPG, marker table corrupted`);
265
+ }
266
+ const E = {
245
267
  validate: (e) => n(e, 0, 2) === `ffd8`,
246
268
  calculate(e) {
247
- e = e.slice(4);
269
+ e = e.subarray(2);
248
270
  let t, n;
249
271
  for (; e.length > 0;) {
250
- let r = i(e, 0);
251
- if (D(e) && (t = A(e, r)), j(e, r), n = e[r + 1], n === 192 || n === 193 || n === 194) {
252
- let n = O(e, r + 5);
272
+ let r = T(e, 0);
273
+ if (n = e[r + 1], n >= 192 && n <= 195 || n >= 201 && n <= 203) {
274
+ let n = he(e, r + 5);
253
275
  return t ? {
254
276
  height: n.height,
255
277
  orientation: t,
256
278
  width: n.width
257
279
  } : n;
258
280
  }
259
- e = e.slice(r + 2);
281
+ if (n === 218) break;
282
+ e = e.subarray(r + 2);
283
+ let a = i(e, 0);
284
+ ve(e, a), me(e) && (t = _e(e, a)), e = e.subarray(a);
260
285
  }
261
286
  throw TypeError(`Invalid JPG, no size found`);
262
287
  }
263
- }, N = {
264
- validate: (e) => t(e, 1, 7) === `KTX 11`,
265
- calculate: (e) => ({
266
- height: l(e, 40),
267
- width: l(e, 36)
268
- })
269
- }, P = `CgBI`, F = {
288
+ };
289
+ function D(e) {
290
+ let t = 16;
291
+ return (n) => {
292
+ let r = 0;
293
+ for (let i = 0; i < n; i++, t++) {
294
+ let n = e[t >> 3];
295
+ if (n === void 0) throw TypeError(`Invalid JXL, truncated codestream`);
296
+ r += (n >> (t & 7) & 1) * 2 ** i;
297
+ }
298
+ return r;
299
+ };
300
+ }
301
+ const O = [
302
+ [1, 1],
303
+ [12, 10],
304
+ [4, 3],
305
+ [3, 2],
306
+ [16, 9],
307
+ [5, 4],
308
+ [2, 1]
309
+ ];
310
+ function k(e) {
311
+ let t = D(e), n = (e) => e ? 8 * (1 + t(5)) : 1 + t([
312
+ 9,
313
+ 13,
314
+ 18,
315
+ 30
316
+ ][t(2)]), r = t(1) === 1, i = n(r), a = t(3);
317
+ if (a === 0) return {
318
+ height: i,
319
+ width: n(r)
320
+ };
321
+ let [o, s] = O[a - 1];
322
+ return {
323
+ height: i,
324
+ width: Math.floor(i * o / s)
325
+ };
326
+ }
327
+ const A = d(`jxlc`), j = d(`jxlp`);
328
+ function M(e) {
329
+ let t = [], n = 0, r = 0;
330
+ for (; r + 8 <= e.length && n < 32;) {
331
+ let i = c(e, r), a = c(e, r + 4), o = 8;
332
+ if (i === 1) {
333
+ if (r + 16 > e.length) break;
334
+ i = c(e, r + 8) * 2 ** 32 + c(e, r + 12), o = 16;
335
+ } else i === 0 && (i = e.length - r);
336
+ if (a === A || a === j) {
337
+ let s = r + o + (a === j ? 4 : 0);
338
+ if (i < s - r) throw TypeError(`Invalid JXL, corrupt ${a === j ? `jxlp` : `jxlc`} box`);
339
+ let c = Math.min(r + i, e.length, s + 32 - n);
340
+ if (c > s && (t.push(e.subarray(s, c)), n += c - s), a === A) break;
341
+ }
342
+ if (i < o) throw TypeError(`Invalid JXL, corrupt box`);
343
+ r += i;
344
+ }
345
+ let i = new Uint8Array(n), a = 0;
346
+ for (let e of t) i.set(e, a), a += e.length;
347
+ return i;
348
+ }
349
+ const N = (e) => n(e, 0, 2) === `ff0a`, P = {
350
+ validate: (e) => {
351
+ if (N(e)) return !0;
352
+ if (t(e, 4, 8) !== `JXL `) return !1;
353
+ let n = f(e, `ftyp`);
354
+ return n !== void 0 && t(e, n.offset + 8, n.offset + 12) === `jxl `;
355
+ },
356
+ calculate: (e) => {
357
+ let t = N(e) ? e : M(e);
358
+ if (!N(t)) throw TypeError(`Invalid JXL, no codestream found`);
359
+ return k(t);
360
+ }
361
+ }, F = 16909060, ye = {
362
+ validate: (e) => {
363
+ let n = t(e, 1, 7);
364
+ return n === `KTX 11` || n === `KTX 20`;
365
+ },
366
+ calculate: (e) => {
367
+ if (e[5] === 50) return {
368
+ height: l(e, 24),
369
+ width: l(e, 20)
370
+ };
371
+ let t = l(e, 12);
372
+ if (t !== 67305985 && t !== F) throw TypeError(`Invalid KTX, unknown endianness`);
373
+ let n = t === F;
374
+ return {
375
+ height: u(e, 32, 40, n),
376
+ width: u(e, 32, 36, n)
377
+ };
378
+ }
379
+ }, I = `CgBI`, be = {
270
380
  validate(e) {
271
381
  if (t(e, 1, 8) === `PNG\r
272
382
  
273
383
  `) {
274
384
  let n = t(e, 12, 16);
275
- if (n === P && (n = t(e, 28, 32)), n !== `IHDR`) throw TypeError(`Invalid PNG`);
385
+ if (n === I && (n = t(e, 28, 32)), n !== `IHDR`) throw TypeError(`Invalid PNG`);
276
386
  return !0;
277
387
  }
278
388
  return !1;
279
389
  },
280
390
  calculate(e) {
281
- return t(e, 12, 16) === P ? {
391
+ return t(e, 12, 16) === I ? {
282
392
  height: c(e, 36),
283
393
  width: c(e, 32)
284
394
  } : {
@@ -286,7 +396,7 @@ const M = {
286
396
  width: c(e, 16)
287
397
  };
288
398
  }
289
- }, I = {
399
+ }, L = {
290
400
  P1: `pbm/ascii`,
291
401
  P2: `pgm/ascii`,
292
402
  P3: `ppm/ascii`,
@@ -294,115 +404,161 @@ const M = {
294
404
  P5: `pgm`,
295
405
  P6: `ppm`,
296
406
  P7: `pam`,
297
- PF: `pfm`
298
- }, L = {
407
+ PF: `pfm`,
408
+ Pf: `pfm`
409
+ }, R = (e) => e === 10 || e === 13, xe = (e) => e === 32 || e >= 9 && e <= 13, z = (e = ``) => /^\d+$/.test(e) ? Number(e) : NaN, Se = /^\s*(width|height|endhdr)(?!\S)\s*(\S*)/i, B = {
299
410
  default: (e) => {
300
- let t = [];
301
- for (; e.length > 0;) {
302
- let n = e.shift();
303
- if (n[0] !== `#`) {
304
- t = n.split(` `);
305
- break;
306
- }
307
- }
308
- if (t.length === 2) return {
309
- height: Number.parseInt(t[1], 10),
310
- width: Number.parseInt(t[0], 10)
411
+ let [t, n] = V(e, `tokens`);
412
+ return {
413
+ height: z(n),
414
+ width: z(t)
311
415
  };
312
- throw TypeError(`Invalid PNM`);
313
416
  },
314
417
  pam: (e) => {
315
418
  let t = {};
316
- for (; e.length > 0;) {
317
- let n = e.shift();
318
- if (n.length > 16 || (n.codePointAt(0) || 0) > 128) continue;
319
- let [r, i] = n.split(` `);
320
- if (r && i && (t[r.toLowerCase()] = Number.parseInt(i, 10)), t.height && t.width) break;
419
+ for (let n of V(e, `lines`)) {
420
+ let [, e = ``, r] = Se.exec(n) || [], i = e.toLowerCase();
421
+ if ((i === `width` || i === `height`) && (t[i] = z(r)), i === `endhdr` || `width` in t && `height` in t) break;
321
422
  }
322
- if (t.height && t.width) return {
423
+ return {
323
424
  height: t.height,
324
425
  width: t.width
325
426
  };
326
- throw TypeError(`Invalid PAM`);
327
427
  }
328
- }, R = {
329
- validate: (e) => t(e, 0, 2) in I,
428
+ };
429
+ function* V(e, n) {
430
+ let r = n === `lines` ? R : xe, i = n === `lines` ? /[^\n\r]+/g : /[^\t-\r ]+/g, a = 2;
431
+ for (; a < e.length;) {
432
+ let n = a, o = a, s = !1;
433
+ for (; n < e.length && (n - a < 1024 || o === a);) {
434
+ let t = e[n++];
435
+ s = t === 35 || s && !R(t), !s && r(t) && (o = n);
436
+ }
437
+ n === e.length && (o = n), yield* t(e, a, o).replace(/#[^\n\r]*/g, `
438
+ `).match(i) || [], a = o;
439
+ }
440
+ }
441
+ const Ce = {
442
+ validate: (e) => t(e, 0, 2) in L,
330
443
  calculate(e) {
331
- let n = I[t(e, 0, 2)], r = t(e, 3).split(/[\n\r]+/);
332
- return (L[n] || L.default)(r);
444
+ let n = t(e, 0, 2), r = L[n];
445
+ return (B[r] || B.default)(e);
333
446
  }
334
- }, z = {
447
+ }, we = {
335
448
  validate: (e) => t(e, 0, 4) === `8BPS`,
336
449
  calculate: (e) => ({
337
450
  height: c(e, 14),
338
451
  width: c(e, 18)
339
452
  })
340
- }, B = /<svg\s([^"'>]|"[^"]*"|'[^']*')*>/, V = {
453
+ }, Te = /<svg\s([^"'>]|"[^"]*"|'[^']*')*>/, Ee = /<!--[\s\S]*?(?:-->|$)/g, H = {
341
454
  height: /\sheight=(["'])([^%]+?)\1/,
342
- root: B,
343
- viewbox: /\sviewbox=(["'])(.+?)\1/i,
455
+ viewbox: /\sviewbox=(["'])([\s\S]+?)\1/i,
344
456
  width: /\swidth=(["'])([^%]+?)\1/
345
- }, H = 2.54, U = {
457
+ }, U = [
458
+ 60,
459
+ 115,
460
+ 118,
461
+ 103
462
+ ], De = /^(?:\u00C2\u00A0|\u00E1\u009A\u0080|\u00E2\u0080[\u0080-\u008A\u00A8\u00A9\u00AF]|\u00E2\u0081\u009F|\u00E3\u0080\u0080|\u00EF\u00BB\u00BF)/;
463
+ function Oe(e, t) {
464
+ let n = e[t];
465
+ if (n < 128) return n === 32 || n >= 9 && n <= 13;
466
+ let r = String.fromCharCode(n, e[t + 1], e[t + 2]);
467
+ return De.test(r);
468
+ }
469
+ const W = (e, t) => e[t] === 60 && t + 3 < e.length && e[t + 1] === 33 && e[t + 2] === 45 && e[t + 3] === 45;
470
+ function G(e, t) {
471
+ for (let n = t + 4; n + 2 < e.length; n++) if (e[n] === 45 && e[n + 1] === 45 && e[n + 2] === 62) return n + 3;
472
+ return e.length;
473
+ }
474
+ function ke(e) {
475
+ let n = e.length, r = 0, i = 0, a = 0;
476
+ for (; i < n;) {
477
+ let t = e[i];
478
+ if (t === 60 && W(e, i)) i = G(e, i);
479
+ else {
480
+ if (a === U.length && Oe(e, i)) break;
481
+ t === 60 ? (r = i, a = 1) : a < U.length && t === U[a] ? a++ : a = 0, i++;
482
+ }
483
+ }
484
+ let o = 0;
485
+ for (i++; i < n; i++) {
486
+ let t = e[i];
487
+ if (t === 60 && W(e, i)) i = G(e, i) - 1;
488
+ else if (o) t === o && (o = 0);
489
+ else if (t === 34 || t === 39) o = t;
490
+ else if (t === 62) break;
491
+ }
492
+ if (!(i >= n)) try {
493
+ return t(e, r, i + 1).replace(Ee, ``);
494
+ } catch {
495
+ return;
496
+ }
497
+ }
498
+ const K = 2.54, q = {
346
499
  in: 96,
347
- cm: 96 / H,
500
+ cm: 96 / K,
348
501
  em: 16,
349
502
  ex: 8,
350
- m: 96 / H * 100,
351
- mm: 96 / H / 10,
352
- pc: 96 / 72 / 12,
503
+ m: 96 / K * 100,
504
+ mm: 96 / K / 10,
505
+ pc: 16,
353
506
  pt: 96 / 72,
354
507
  px: 1
355
- }, W = RegExp(`^([0-9.]+(?:e\\d+)?)(${Object.keys(U).join(`|`)})?$`);
356
- function G(e) {
357
- let t = W.exec(e);
358
- if (t) return Math.round(Number(t[1]) * (U[t[2]] || 1));
508
+ }, Ae = RegExp(`^([0-9.]+(?:e\\d+)?)(${Object.keys(q).join(`|`)})?$`);
509
+ function J(e) {
510
+ let t = Ae.exec(e);
511
+ if (t) return Number(t[1]) * (q[t[2]] || 1);
359
512
  }
360
- function K(e) {
361
- let t = e.split(` `);
513
+ function je(e) {
514
+ let t = e.trim().split(/[\s,]+/, 4);
362
515
  return {
363
- height: G(t[3]),
364
- width: G(t[2])
516
+ height: J(t[3]),
517
+ width: J(t[2])
365
518
  };
366
519
  }
367
- function q(e) {
368
- let t = e.match(V.width), n = e.match(V.height), r = e.match(V.viewbox);
520
+ function Me(e) {
521
+ let t = e.match(H.width), n = e.match(H.height), r = e.match(H.viewbox);
369
522
  return {
370
- height: n && G(n[2]),
371
- viewbox: r && K(r[2]),
372
- width: t && G(t[2])
523
+ height: n && Math.round(J(n[2])),
524
+ viewbox: r && je(r[2]),
525
+ width: t && Math.round(J(t[2]))
373
526
  };
374
527
  }
375
- function J(e) {
528
+ function Ne(e) {
376
529
  return {
377
530
  height: e.height,
378
531
  width: e.width
379
532
  };
380
533
  }
381
- function Y(e, t) {
382
- let n = t.width / t.height;
534
+ function Y(e) {
535
+ return Math.max(1, Math.round(e));
536
+ }
537
+ function Pe(e, t) {
538
+ let n = t.width, r = t.height;
383
539
  return e.width ? {
384
- height: Math.floor(e.width / n),
540
+ height: Y(e.width * r / n),
385
541
  width: e.width
386
542
  } : e.height ? {
387
543
  height: e.height,
388
- width: Math.floor(e.height * n)
544
+ width: Y(e.height * n / r)
389
545
  } : {
390
- height: t.height,
391
- width: t.width
546
+ height: Y(r),
547
+ width: Y(n)
392
548
  };
393
549
  }
394
- const ae = {
395
- validate: (e) => B.test(t(e, 0, 1e3)),
550
+ const Fe = {
551
+ validate: (e) => Te.test(t(e, 0, 1e3)),
396
552
  calculate(e) {
397
- let n = t(e).match(V.root);
398
- if (n) {
399
- let e = q(n[0]);
400
- if (e.width && e.height) return J(e);
401
- if (e.viewbox) return Y(e, e.viewbox);
553
+ let t = ke(e);
554
+ if (t) {
555
+ let e = Me(t);
556
+ if (e.width && e.height) return Ne(e);
557
+ if (e.viewbox?.width && e.viewbox.height) return Pe(e, e.viewbox);
402
558
  }
403
559
  throw TypeError(`Invalid SVG`);
404
560
  }
405
- }, oe = {
561
+ }, Ie = {
406
562
  validate(e) {
407
563
  return a(e, 0) === 0 && a(e, 4) === 0;
408
564
  },
@@ -413,120 +569,118 @@ const ae = {
413
569
  };
414
570
  }
415
571
  };
416
- function se(e, t) {
417
- let n = d(e, 32, 4, t), r = 1024, i = e.length;
418
- return n + r > i && (r = i - n - 10), e.slice(n + 2, n + 2 + r);
419
- }
420
- function ce(e, t) {
421
- let n = d(e, 16, 8, t);
422
- return (d(e, 16, 10, t) << 16) + n;
572
+ function X(e, t, n) {
573
+ let r = u(e, 32, t + (n ? 0 : 4), n), i = u(e, 32, t + (n ? 4 : 0), n);
574
+ return r * 2 ** 32 + i;
423
575
  }
424
- function le(e) {
425
- if (e.length > 24) return e.slice(12);
426
- }
427
- function ue(e, t) {
428
- let n = {}, r = e;
429
- for (; r && r.length > 0;) {
430
- let e = d(r, 16, 0, t), i = d(r, 16, 2, t), a = d(r, 32, 4, t);
431
- if (e === 0) break;
432
- a === 1 && (i === 3 || i === 4) && (n[e] = ce(r, t)), r = le(r);
576
+ function Z(e, t, n, r, i) {
577
+ switch (t) {
578
+ case 3: return u(e, 16, n, r);
579
+ case 4: return u(e, 32, n, r);
580
+ case 16: {
581
+ if (!i) return;
582
+ let t = X(e, n, r);
583
+ if (!Number.isSafeInteger(t)) throw TypeError(`Invalid Tiff. Value too large`);
584
+ return t;
585
+ }
433
586
  }
434
- return n;
435
- }
436
- function de(e) {
437
- let n = t(e, 0, 2);
438
- if (n === `II`) return `LE`;
439
- if (n === `MM`) return `BE`;
440
587
  }
441
- const fe = /* @__PURE__ */ new Set([`49492a00`, `4d4d002a`]), pe = {
442
- validate: (e) => fe.has(n(e, 0, 4)),
588
+ const Le = /* @__PURE__ */ new Set([
589
+ `49492a00`,
590
+ `4d4d002a`,
591
+ `49492b00`,
592
+ `4d4d002b`
593
+ ]), Re = {
594
+ validate: (e) => Le.has(n(e, 0, 4)),
443
595
  calculate(e) {
444
- let t = de(e) === `BE`, n = ue(se(e, t), t), r = n[256], i = n[257];
445
- if (!r || !i) throw TypeError(`Invalid Tiff. Missing tags`);
596
+ let n = t(e, 0, 2) === `MM`, r = u(e, 16, 2, n) === 43, i, a, o;
597
+ if (r) {
598
+ let t = u(e, 16, 4, n), r = u(e, 16, 6, n);
599
+ if (t !== 8 || r !== 0) throw TypeError(`Invalid BigTIFF header`);
600
+ let s = X(e, 8, n);
601
+ if (!(s + 8 <= e.length)) throw TypeError(`Invalid Tiff. IFD offset out of bounds`);
602
+ i = X(e, s, n), a = s + 8, o = 20;
603
+ } else {
604
+ let t = u(e, 32, 4, n);
605
+ if (!(t + 2 <= e.length)) throw TypeError(`Invalid Tiff. IFD offset out of bounds`);
606
+ i = u(e, 16, t, n), a = t + 2, o = 12;
607
+ }
608
+ let s = {};
609
+ for (let t = 0; t < Math.min(i, 4096); t++) {
610
+ let i = a + t * o;
611
+ if (i + o > e.length) break;
612
+ let c = u(e, 16, i, n), l = u(e, 16, i + 2, n);
613
+ if ((r ? X(e, i + 4, n) : u(e, 32, i + 4, n)) === 1 && (s[c] = Z(e, l, i + (r ? 12 : 8), n, r)), s[256] && s[257]) break;
614
+ }
615
+ let c = s[256], l = s[257];
616
+ if (!c || !l) throw TypeError(`Invalid Tiff. Missing tags`);
446
617
  return {
447
- height: i,
448
- width: r
618
+ height: l,
619
+ width: c
449
620
  };
450
621
  }
451
622
  };
452
- function me(e) {
623
+ function ze(e) {
453
624
  return {
454
625
  height: 1 + o(e, 7),
455
626
  width: 1 + o(e, 4)
456
627
  };
457
628
  }
458
- function he(e) {
629
+ function Be(e) {
459
630
  return {
460
631
  height: 1 + ((e[4] & 15) << 10 | e[3] << 2 | (e[2] & 192) >> 6),
461
632
  width: 1 + ((e[2] & 63) << 8 | e[1])
462
633
  };
463
634
  }
464
- function ge(e) {
635
+ function Ve(e) {
465
636
  return {
466
637
  height: r(e, 8) & 16383,
467
638
  width: r(e, 6) & 16383
468
639
  };
469
640
  }
470
- const X = {
471
- validate(e) {
472
- let n = t(e, 0, 4) === `RIFF`, r = t(e, 8, 12) === `WEBP`, i = t(e, 12, 15) === `VP8`;
473
- return n && r && i;
474
- },
475
- calculate(e) {
476
- let r = t(e, 12, 16);
477
- if (e = e.slice(20, 30), r === `VP8X`) {
478
- let t = e[0], n = (t & 192) == 0, r = (t & 1) == 0;
479
- if (n && r) return me(e);
641
+ const Q = {
642
+ bmp: ne,
643
+ cur: re,
644
+ dds: ie,
645
+ gif: _,
646
+ heic: C,
647
+ icns: ue,
648
+ ico: h,
649
+ j2c: de,
650
+ jp2: fe,
651
+ jpg: E,
652
+ jxl: P,
653
+ ktx: ye,
654
+ png: be,
655
+ pnm: Ce,
656
+ psd: we,
657
+ svg: Fe,
658
+ tga: Ie,
659
+ tiff: Re,
660
+ webp: {
661
+ validate(e) {
662
+ let n = t(e, 0, 4) === `RIFF`, r = t(e, 8, 12) === `WEBP`, i = t(e, 12, 15) === `VP8`;
663
+ return n && r && i;
664
+ },
665
+ calculate(e) {
666
+ if (e.length < 30) throw TypeError(`Invalid WebP, truncated input`);
667
+ let r = t(e, 12, 16);
668
+ if (e = e.slice(20, 30), r === `VP8X`) {
669
+ let t = e[0], n = !(t & 192), r = !(t & 1);
670
+ if (n && r) return ze(e);
671
+ throw TypeError(`Invalid WebP`);
672
+ }
673
+ if (r === `VP8 ` && e[0] !== 47) return Ve(e);
674
+ let i = n(e, 3, 6);
675
+ if (r === `VP8L` && i !== `9d012a`) return Be(e);
480
676
  throw TypeError(`Invalid WebP`);
481
677
  }
482
- if (r === `VP8 ` && e[0] !== 47) return ge(e);
483
- let i = n(e, 3, 6);
484
- if (r === `VP8L` && i !== `9d012a`) return he(e);
485
- throw TypeError(`Invalid WebP`);
486
- }
487
- }, _e = {
488
- validate: (e) => t(e, 8, 12) === `avif`,
489
- calculate: (e) => {
490
- let t = Z(e, `meta`), n = Z(e, `iprp`, t.offset + 12, t.offset + t.size), r = Z(e, `ipco`, n.offset + 8, n.offset + n.size), i = Z(e, `ispe`, r.offset + 8, r.offset + r.size);
491
- return {
492
- width: c(e, i.offset + 12),
493
- height: c(e, i.offset + 16)
494
- };
495
- }
496
- };
497
- function Z(e, n, r = 0, i = e.length) {
498
- for (let a = r; a < i;) {
499
- let r = c(e, a);
500
- if (t(e, a + 4, a + 8) === n) return {
501
- offset: a,
502
- size: r
503
- };
504
- if (r <= 0 || a + r > i) break;
505
- a += r;
678
+ },
679
+ avif: {
680
+ validate: (e) => S(e) === `avif`,
681
+ calculate: (e) => C.calculate(e)
506
682
  }
507
- throw Error(`${n} box not found`);
508
- }
509
- const Q = {
510
- bmp: ee,
511
- cur: te,
512
- dds: h,
513
- gif: _,
514
- heic: v,
515
- icns: ne,
516
- ico: m,
517
- j2c: re,
518
- jp2: E,
519
- jpg: M,
520
- ktx: N,
521
- png: F,
522
- pnm: R,
523
- psd: z,
524
- svg: ae,
525
- tga: oe,
526
- tiff: pe,
527
- webp: X,
528
- avif: _e
529
- }, ve = Object.keys(Q), $ = {
683
+ }, He = Object.keys(Q), $ = {
530
684
  56: `psd`,
531
685
  66: `bmp`,
532
686
  68: `dds`,
@@ -538,21 +692,26 @@ const Q = {
538
692
  137: `png`,
539
693
  255: `jpg`
540
694
  };
541
- function ye(e) {
695
+ function Ue(e) {
542
696
  let t = e[0];
543
697
  if (t in $) {
544
698
  let n = $[t];
545
699
  if (n && Q[n].validate(e)) return n;
546
700
  }
547
- return ve.find((t) => Q[t].validate(e));
701
+ return He.find((t) => Q[t].validate(e));
548
702
  }
549
- function be(e) {
703
+ function We(e) {
550
704
  if (!(e instanceof Uint8Array)) throw TypeError(`Input should be a Uint8Array`);
551
- let t = ye(e);
705
+ let t = Ue(e);
552
706
  if (t !== void 0 && t in Q) {
553
707
  let n = Q[t].calculate(e);
554
- if (n !== void 0) return n.type = t, n;
708
+ if (n !== void 0) {
709
+ if (n.images && n.images.length > 1) for (let e of n.images) e.width * e.height > n.width * n.height && (n.width = e.width, n.height = e.height);
710
+ if (!Number.isSafeInteger(n.width) || !Number.isSafeInteger(n.height)) throw TypeError(`Invalid ${t}, truncated or corrupt input`);
711
+ if (n.width <= 0 || n.height < 0) throw TypeError(`Invalid ${t}, zero or negative size`);
712
+ return n.type = t, n;
713
+ }
555
714
  }
556
715
  throw TypeError(`Unsupported file type: ${t}`);
557
716
  }
558
- export { be as imageMeta };
717
+ export { We as imageMeta };