@margoengine/loaders 0.1.0 → 1.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.
- package/LICENSE +201 -201
- package/NOTICE +4 -4
- package/README.md +1666 -1198
- package/dist/crease.d.ts +7 -0
- package/dist/crease.js +67 -0
- package/dist/dae-common.js +1 -1
- package/dist/dae-kinematics.d.ts +65 -0
- package/dist/dae-kinematics.js +461 -0
- package/dist/dae.d.ts +9 -4
- package/dist/dae.js +16 -5
- package/dist/dxf-hatch.d.ts +8 -0
- package/dist/dxf-hatch.js +346 -0
- package/dist/dxf-leaders.d.ts +59 -0
- package/dist/dxf-leaders.js +258 -0
- package/dist/dxf.d.ts +42 -1
- package/dist/dxf.js +226 -15
- package/dist/engine.d.ts +5 -0
- package/dist/engine.js +3 -0
- package/dist/gcode.d.ts +37 -0
- package/dist/gcode.js +91 -0
- package/dist/geometry.d.ts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/mtl.d.ts +8 -3
- package/dist/mtl.js +91 -10
- package/dist/nrrd.d.ts +12 -5
- package/dist/nrrd.js +68 -26
- package/dist/obj.d.ts +3 -1
- package/dist/obj.js +7 -1
- package/dist/render.d.ts +9 -4
- package/dist/render.js +31 -3
- package/dist/shared/asset-url.js +82 -82
- package/dist/shared/crc32.d.ts +2 -2
- package/dist/shared/crc32.js +47 -47
- package/dist/shared/errors.js +36 -36
- package/dist/shared/inflate.js +284 -284
- package/dist/shared/normals.js +26 -26
- package/dist/shared/scene-scope.js +54 -54
- package/dist/shared/trailing-data.js +45 -42
- package/dist/threemf-beam-reader.d.ts +19 -0
- package/dist/threemf-beam-reader.js +165 -0
- package/dist/threemf-beams.d.ts +52 -0
- package/dist/threemf-beams.js +268 -0
- package/dist/threemf-boolean-read.d.ts +10 -0
- package/dist/threemf-boolean-read.js +113 -0
- package/dist/threemf-boolean.d.ts +59 -0
- package/dist/threemf-boolean.js +20 -0
- package/dist/threemf-csg.d.ts +14 -0
- package/dist/threemf-csg.js +193 -0
- package/dist/threemf-displacement-read.d.ts +8 -0
- package/dist/threemf-displacement-read.js +213 -0
- package/dist/threemf-displacement.d.ts +71 -0
- package/dist/threemf-displacement.js +18 -0
- package/dist/threemf-evaluate.d.ts +36 -0
- package/dist/threemf-evaluate.js +86 -0
- package/dist/threemf-functions.d.ts +33 -0
- package/dist/threemf-functions.js +359 -0
- package/dist/threemf-lattice-mesh.d.ts +9 -0
- package/dist/threemf-lattice-mesh.js +115 -0
- package/dist/threemf-lattice.js +10 -145
- package/dist/threemf-levelset.d.ts +23 -0
- package/dist/threemf-levelset.js +105 -0
- package/dist/threemf-materials.d.ts +89 -2
- package/dist/threemf-materials.js +194 -11
- package/dist/threemf-mesh.d.ts +21 -6
- package/dist/threemf-mesh.js +266 -45
- package/dist/threemf-secure.d.ts +50 -0
- package/dist/threemf-secure.js +153 -0
- package/dist/threemf-tessellate.d.ts +37 -0
- package/dist/threemf-tessellate.js +277 -0
- package/dist/threemf-volume-colour.d.ts +25 -0
- package/dist/threemf-volume-colour.js +233 -0
- package/dist/threemf-volume-sample.d.ts +8 -0
- package/dist/threemf-volume-sample.js +65 -0
- package/dist/threemf-volumetric-read.d.ts +15 -0
- package/dist/threemf-volumetric-read.js +539 -0
- package/dist/threemf-volumetric.d.ts +177 -0
- package/dist/threemf-volumetric.js +28 -0
- package/dist/threemf.d.ts +73 -4
- package/dist/threemf.js +235 -41
- package/dist/types.d.ts +18 -0
- package/dist/workers/usd.js +1 -1
- package/package.json +291 -246
package/dist/shared/inflate.js
CHANGED
|
@@ -1,284 +1,284 @@
|
|
|
1
|
-
// zlib (RFC 1950) and DEFLATE (RFC 1951) decoder, written from the two RFCs: EXR ZIP and ZIPS chunks
|
|
2
|
-
// (@margoengine/environment) and PNG image data (@margoengine/decoders/png), whose decompressed size is known up front
|
|
3
|
-
// and fills one buffer of exactly that size. With a `{ limit }` option the size need not be stored: the buffer grows
|
|
4
|
-
// up to that limit. inflateRaw serves containers that must know where a raw stream ends (gzip members and deflated
|
|
5
|
-
// DICOM data sets in @margoengine/sciviz/formats). Every input read, output write and back reference is bounds checked, and damaged data throws
|
|
6
|
-
// MargoError 'failed'.
|
|
7
|
-
import { fault } from './errors.js';
|
|
8
|
-
|
|
9
|
-
const damaged = (problem) => fault('failed', `Compressed data ${problem}; the file is damaged, export it again`);
|
|
10
|
-
|
|
11
|
-
// Every `groupShift`-sized group of symbols adds one extra bit (after the first two groups, which have none), and each
|
|
12
|
-
// base is the previous base plus the span the previous symbol's extra bits cover. That rule rebuilds both RFC 1951
|
|
13
|
-
// section 3.2.5 tables: lengths 3, 4, ... 227 for symbols 257..284 and distances 1, 2, ... 24577 for 0..29. Length
|
|
14
|
-
// symbol 285 breaks the pattern (exactly 258, no extra bits), passed as `lastBase`.
|
|
15
|
-
function symbolRanges(count, groupShift, first, lastBase) {
|
|
16
|
-
const base = new Uint16Array(count),
|
|
17
|
-
extra = new Uint8Array(count);
|
|
18
|
-
for (let i = 0, value = first; i < count; i++) {
|
|
19
|
-
extra[i] = Math.max(0, (i >> groupShift) - 1);
|
|
20
|
-
base[i] = value;
|
|
21
|
-
value += 1 << extra[i];
|
|
22
|
-
}
|
|
23
|
-
if (lastBase !== undefined) {
|
|
24
|
-
base[count - 1] = lastBase;
|
|
25
|
-
extra[count - 1] = 0;
|
|
26
|
-
}
|
|
27
|
-
return [base, extra];
|
|
28
|
-
}
|
|
29
|
-
const LENGTHS = /* @__PURE__ */ symbolRanges(29, 2, 3, 258);
|
|
30
|
-
const DISTANCES = /* @__PURE__ */ symbolRanges(30, 1, 1);
|
|
31
|
-
const LENGTH_258 = 28;
|
|
32
|
-
const END_OF_BLOCK = 256;
|
|
33
|
-
// RFC 1951 section 3.2.7: order in which the code length alphabet's own code lengths are stored.
|
|
34
|
-
const CODE_LENGTH_ORDER = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
|
|
35
|
-
const MAX_BITS = 15;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* The decoding table for the canonical code RFC 1951 section 3.2.2 assigns to these code lengths: one entry per
|
|
39
|
-
* pattern of the next `width` stream bits (first stream bit lowest), `symbol << 4 | code length`, 0 for patterns no
|
|
40
|
-
* code starts with.
|
|
41
|
-
*/
|
|
42
|
-
function huffmanTable(lengths) {
|
|
43
|
-
const perLength = new Uint16Array(MAX_BITS + 1);
|
|
44
|
-
let width = 1;
|
|
45
|
-
for (const length of lengths)
|
|
46
|
-
if (length) {
|
|
47
|
-
perLength[length]++;
|
|
48
|
-
width = Math.max(width, length);
|
|
49
|
-
}
|
|
50
|
-
// Kraft sum: more codes of some length than the shorter ones leave room for cannot be decoded. An incomplete set is
|
|
51
|
-
// accepted (RFC 1951 allows a lone distance code); its unused patterns stay 0 and throw only if the data uses them.
|
|
52
|
-
for (let length = 1, room = 1; length <= MAX_BITS; length++) {
|
|
53
|
-
room = room * 2 - perLength[length];
|
|
54
|
-
if (room < 0) throw damaged('has an over-subscribed Huffman code');
|
|
55
|
-
}
|
|
56
|
-
const nextCode = new Uint16Array(MAX_BITS + 1);
|
|
57
|
-
for (let length = 1, code = 0; length <= MAX_BITS; length++) {
|
|
58
|
-
code = (code + (length > 1 ? perLength[length - 1] : 0)) << 1;
|
|
59
|
-
nextCode[length] = code;
|
|
60
|
-
}
|
|
61
|
-
const entries = new Int32Array(1 << width);
|
|
62
|
-
for (let symbol = 0; symbol < lengths.length; symbol++) {
|
|
63
|
-
const length = lengths[symbol];
|
|
64
|
-
if (!length) continue;
|
|
65
|
-
// Codes are defined most significant bit first but arrive first bit lowest, so the table is indexed reversed.
|
|
66
|
-
const code = nextCode[length]++;
|
|
67
|
-
let reversed = 0;
|
|
68
|
-
for (let bit = 0; bit < length; bit++) reversed |= ((code >> bit) & 1) << (length - 1 - bit);
|
|
69
|
-
const entry = (symbol << 4) | length;
|
|
70
|
-
for (let index = reversed; index < entries.length; index += 1 << length) entries[index] = entry;
|
|
71
|
-
}
|
|
72
|
-
return [entries, width];
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// The fixed literal/length and distance codes of block type 1 (RFC 1951 section 3.2.6), built on first use.
|
|
76
|
-
let fixedTables;
|
|
77
|
-
|
|
78
|
-
/** One zlib stream being inflated; fields and steps are private so the minifier shortens their names. */
|
|
79
|
-
class Inflater {
|
|
80
|
-
#input;
|
|
81
|
-
#limit;
|
|
82
|
-
#limitHint;
|
|
83
|
-
#output;
|
|
84
|
-
#position;
|
|
85
|
-
#held = 0;
|
|
86
|
-
#count = 0;
|
|
87
|
-
#written = 0;
|
|
88
|
-
constructor(input, size, start = 2) {
|
|
89
|
-
this.#input = input;
|
|
90
|
-
this.#position = start;
|
|
91
|
-
this.#limit = size.limit;
|
|
92
|
-
this.#limitHint = size.limitHint ?? 'raise limit to allow more decompressed bytes';
|
|
93
|
-
// A growable buffer starts no larger than its limit, so a limit under 1 KiB holds as well as a larger one.
|
|
94
|
-
this.#output = new Uint8Array(this.#limit === undefined ? size : Math.min(1024, this.#limit));
|
|
95
|
-
}
|
|
96
|
-
// The stream is read least significant bit first, holding up to 32 bits ahead.
|
|
97
|
-
#refill() {
|
|
98
|
-
while (this.#count <= 24 && this.#position < this.#input.length) {
|
|
99
|
-
this.#held |= this.#input[this.#position++] << this.#count;
|
|
100
|
-
this.#count += 8;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
/** The next `n` bits (0..16) as an unsigned number. */
|
|
104
|
-
#bits(n) {
|
|
105
|
-
if (this.#count < n) {
|
|
106
|
-
this.#refill();
|
|
107
|
-
if (this.#count < n) throw damaged('ends early while reading DEFLATE bits');
|
|
108
|
-
}
|
|
109
|
-
const value = this.#held & ((1 << n) - 1);
|
|
110
|
-
this.#held >>>= n;
|
|
111
|
-
this.#count -= n;
|
|
112
|
-
return value;
|
|
113
|
-
}
|
|
114
|
-
/** The next symbol of a Huffman code. */
|
|
115
|
-
#symbol(table) {
|
|
116
|
-
if (this.#count < table[1]) this.#refill();
|
|
117
|
-
const entry = table[0][this.#held & (table[0].length - 1)],
|
|
118
|
-
length = entry & 15;
|
|
119
|
-
if (entry === 0) throw damaged('uses a Huffman code its block does not define');
|
|
120
|
-
if (length > this.#count) throw damaged('ends early while reading a DEFLATE Huffman code');
|
|
121
|
-
this.#held >>>= length;
|
|
122
|
-
this.#count -= length;
|
|
123
|
-
return entry >> 4;
|
|
124
|
-
}
|
|
125
|
-
/** Drops the bits left in the current byte and returns the position of the next whole byte. */
|
|
126
|
-
#align() {
|
|
127
|
-
this.#position -= this.#count >> 3;
|
|
128
|
-
this.#held = this.#count = 0;
|
|
129
|
-
return this.#position;
|
|
130
|
-
}
|
|
131
|
-
/** Room in the output for `need` bytes: a growable buffer doubles, up to the limit. */
|
|
132
|
-
#room(need) {
|
|
133
|
-
const output = this.#output,
|
|
134
|
-
limit = this.#limit;
|
|
135
|
-
if (need <= output.length) return output;
|
|
136
|
-
if (limit === undefined)
|
|
137
|
-
throw damaged(`inflates to at least ${need} bytes, above the expected ${output.length} bytes`);
|
|
138
|
-
if (need > limit)
|
|
139
|
-
throw fault(
|
|
140
|
-
'failed',
|
|
141
|
-
`Compressed data needs at least ${need} bytes, above the limit ${limit} bytes; ${this.#limitHint}`,
|
|
142
|
-
);
|
|
143
|
-
const bigger = new Uint8Array(Math.min(limit, Math.max(output.length * 2, need)));
|
|
144
|
-
bigger.set(output);
|
|
145
|
-
return (this.#output = bigger);
|
|
146
|
-
}
|
|
147
|
-
/** One Huffman-coded block. */
|
|
148
|
-
#block(literals, distances) {
|
|
149
|
-
let output = this.#output,
|
|
150
|
-
written = this.#written;
|
|
151
|
-
for (;;) {
|
|
152
|
-
const next = this.#symbol(literals);
|
|
153
|
-
if (next < END_OF_BLOCK) {
|
|
154
|
-
if (written >= output.length) output = this.#room(written + 1);
|
|
155
|
-
output[written++] = next;
|
|
156
|
-
continue;
|
|
157
|
-
}
|
|
158
|
-
if (next === END_OF_BLOCK) return (this.#written = written);
|
|
159
|
-
const lengthSymbol = next - 257;
|
|
160
|
-
if (lengthSymbol > LENGTH_258) throw damaged(`uses the reserved length symbol ${next}`);
|
|
161
|
-
const length = LENGTHS[0][lengthSymbol] + this.#bits(LENGTHS[1][lengthSymbol]),
|
|
162
|
-
distanceSymbol = this.#symbol(distances);
|
|
163
|
-
if (distanceSymbol >= 30) throw damaged(`uses the reserved distance symbol ${distanceSymbol}`);
|
|
164
|
-
const distance = DISTANCES[0][distanceSymbol] + this.#bits(DISTANCES[1][distanceSymbol]);
|
|
165
|
-
if (distance > written)
|
|
166
|
-
throw damaged(`refers back ${distance} bytes with only ${written} bytes written, before the start of its data`);
|
|
167
|
-
if (written + length > output.length) output = this.#room(written + length);
|
|
168
|
-
// Front to back, one byte at a time: a distance shorter than the length repeats the bytes just written.
|
|
169
|
-
for (let from = written - distance, stop = written + length; written < stop; ) output[written++] = output[from++];
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
/** Code lengths of a dynamic block's two codes (RFC 1951 section 3.2.7), themselves Huffman coded. */
|
|
173
|
-
#dynamic() {
|
|
174
|
-
const literalCount = this.#bits(5) + 257,
|
|
175
|
-
distanceCount = this.#bits(5) + 1,
|
|
176
|
-
codeLengthCount = this.#bits(4) + 4;
|
|
177
|
-
if (literalCount > 286) throw damaged(`declares ${literalCount} literal/length codes (at most 286)`);
|
|
178
|
-
const codeLengthLengths = new Uint8Array(19);
|
|
179
|
-
for (let i = 0; i < codeLengthCount; i++) codeLengthLengths[CODE_LENGTH_ORDER[i]] = this.#bits(3);
|
|
180
|
-
const codeLengthCode = huffmanTable(codeLengthLengths);
|
|
181
|
-
// One run of lengths covers both codes; repeats may cross from the literal lengths into the distance lengths.
|
|
182
|
-
const lengths = new Uint8Array(literalCount + distanceCount);
|
|
183
|
-
for (let filled = 0; filled < lengths.length; ) {
|
|
184
|
-
const next = this.#symbol(codeLengthCode);
|
|
185
|
-
if (next < 16) {
|
|
186
|
-
lengths[filled++] = next;
|
|
187
|
-
continue;
|
|
188
|
-
}
|
|
189
|
-
let value = 0,
|
|
190
|
-
repeat;
|
|
191
|
-
if (next === 16) {
|
|
192
|
-
if (filled === 0) throw damaged('repeats a code length before the first one');
|
|
193
|
-
value = lengths[filled - 1];
|
|
194
|
-
repeat = 3 + this.#bits(2);
|
|
195
|
-
} else repeat = next === 17 ? 3 + this.#bits(3) : 11 + this.#bits(7);
|
|
196
|
-
if (repeat > lengths.length - filled) throw damaged('repeats code lengths past the declared count');
|
|
197
|
-
lengths.fill(value, filled, filled + repeat);
|
|
198
|
-
filled += repeat;
|
|
199
|
-
}
|
|
200
|
-
if (!lengths[END_OF_BLOCK]) throw damaged('has a block without an end-of-block code');
|
|
201
|
-
this.#block(huffmanTable(lengths.subarray(0, literalCount)), huffmanTable(lengths.subarray(literalCount)));
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Decodes every block and checks the trailer; see {@link inflateZlib}. A `raw` stream (no zlib header or trailer)
|
|
205
|
-
* returns its bytes and the position after its final block instead; see {@link inflateRaw}.
|
|
206
|
-
*/
|
|
207
|
-
run(size, checkAdler, raw) {
|
|
208
|
-
const input = this.#input;
|
|
209
|
-
if (!raw) {
|
|
210
|
-
if (input.length < 2) throw damaged('is too short for a zlib header');
|
|
211
|
-
const method = input[0],
|
|
212
|
-
flags = input[1];
|
|
213
|
-
// RFC 1950: compression method 8 (deflate), window of at most 32 KiB, header check, no preset dictionary.
|
|
214
|
-
if ((method & 15) !== 8) throw damaged(`declares zlib compression method ${method & 15}, not DEFLATE (8)`);
|
|
215
|
-
if (method >> 4 > 7) throw damaged(`declares zlib window size ${method >> 4}, above the maximum 7`);
|
|
216
|
-
if (((method << 8) | flags) % 31 !== 0) throw damaged('fails its zlib header check');
|
|
217
|
-
if (flags & 0x20)
|
|
218
|
-
throw fault(
|
|
219
|
-
'unsupported',
|
|
220
|
-
'Compressed data needs a preset dictionary; recompress it without a preset dictionary',
|
|
221
|
-
);
|
|
222
|
-
}
|
|
223
|
-
let final = 0;
|
|
224
|
-
do {
|
|
225
|
-
final = this.#bits(1);
|
|
226
|
-
const type = this.#bits(2);
|
|
227
|
-
if (type === 0) {
|
|
228
|
-
// Stored block: byte aligned LEN, NLEN (its one's complement), then LEN raw bytes.
|
|
229
|
-
let at = this.#align();
|
|
230
|
-
if (input.length - at < 4) throw damaged('ends early in its stored block length header');
|
|
231
|
-
const length = input[at] | (input[at + 1] << 8),
|
|
232
|
-
check = input[at + 2] | (input[at + 3] << 8);
|
|
233
|
-
if ((length ^ 0xffff) !== check) throw damaged('has a stored block whose length check fails');
|
|
234
|
-
at += 4;
|
|
235
|
-
if (length > input.length - at) throw damaged('ends early in its stored block data');
|
|
236
|
-
this.#room(this.#written + length).set(input.subarray(at, at + length), this.#written);
|
|
237
|
-
this.#written += length;
|
|
238
|
-
this.#position = at + length;
|
|
239
|
-
} else if (type === 1) {
|
|
240
|
-
fixedTables ??= [
|
|
241
|
-
huffmanTable(new Uint8Array(288).fill(8, 0, 144).fill(9, 144, 256).fill(7, 256, 280).fill(8, 280, 288)),
|
|
242
|
-
huffmanTable(new Uint8Array(32).fill(5)),
|
|
243
|
-
];
|
|
244
|
-
this.#block(fixedTables[0], fixedTables[1]);
|
|
245
|
-
} else if (type === 2) this.#dynamic();
|
|
246
|
-
else throw damaged('uses the reserved block type 3');
|
|
247
|
-
} while (!final);
|
|
248
|
-
if (this.#limit === undefined && this.#written !== size)
|
|
249
|
-
throw damaged(`inflates to ${this.#written} bytes, not the expected ${size} bytes`);
|
|
250
|
-
const output = this.#output.subarray(0, this.#written);
|
|
251
|
-
const at = this.#align();
|
|
252
|
-
if (raw) return { output, end: at };
|
|
253
|
-
// RFC 1950 trailer: the Adler-32 of the decompressed bytes, big-endian, after the last block's final byte.
|
|
254
|
-
if (input.length - at < 4) throw damaged('ends before its Adler-32 checksum');
|
|
255
|
-
const stored = ((input[at] << 24) | (input[at + 1] << 16) | (input[at + 2] << 8) | input[at + 3]) >>> 0;
|
|
256
|
-
if (checkAdler && adler32(output) !== stored) throw damaged('fails its Adler-32 checksum');
|
|
257
|
-
return output;
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/** Inflates zlib `input`; see inflate.d.ts. */
|
|
262
|
-
export const inflateZlib = (input, size, checkAdler = true) => new Inflater(input, size).run(size, checkAdler);
|
|
263
|
-
|
|
264
|
-
/** Inflates the raw DEFLATE stream at `start` of `input`; see inflate.d.ts. */
|
|
265
|
-
export const inflateRaw = (input, start, size) => new Inflater(input, size, start).run(size, false, true);
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* Adler-32 (RFC 1950 section 8.2): two sums modulo 65521. 5552 bytes is the longest run after which the second sum
|
|
269
|
-
* still fits in 32 unsigned bits, so the sums stay 32-bit integers (twice as fast as doubles) and are reduced per run.
|
|
270
|
-
*/
|
|
271
|
-
function adler32(bytes) {
|
|
272
|
-
let a = 1,
|
|
273
|
-
b = 0;
|
|
274
|
-
for (let start = 0; start < bytes.length; start += 5552) {
|
|
275
|
-
const end = Math.min(bytes.length, start + 5552);
|
|
276
|
-
for (let i = start; i < end; i++) {
|
|
277
|
-
a = (a + bytes[i]) | 0;
|
|
278
|
-
b = (b + a) | 0;
|
|
279
|
-
}
|
|
280
|
-
a %= 65521;
|
|
281
|
-
b = (b >>> 0) % 65521;
|
|
282
|
-
}
|
|
283
|
-
return ((b << 16) | a) >>> 0;
|
|
284
|
-
}
|
|
1
|
+
// zlib (RFC 1950) and DEFLATE (RFC 1951) decoder, written from the two RFCs: EXR ZIP and ZIPS chunks
|
|
2
|
+
// (@margoengine/environment) and PNG image data (@margoengine/decoders/png), whose decompressed size is known up front
|
|
3
|
+
// and fills one buffer of exactly that size. With a `{ limit }` option the size need not be stored: the buffer grows
|
|
4
|
+
// up to that limit. inflateRaw serves containers that must know where a raw stream ends (gzip members and deflated
|
|
5
|
+
// DICOM data sets in @margoengine/sciviz/formats). Every input read, output write and back reference is bounds checked, and damaged data throws
|
|
6
|
+
// MargoError 'failed'.
|
|
7
|
+
import { fault } from './errors.js';
|
|
8
|
+
|
|
9
|
+
const damaged = (problem) => fault('failed', `Compressed data ${problem}; the file is damaged, export it again`);
|
|
10
|
+
|
|
11
|
+
// Every `groupShift`-sized group of symbols adds one extra bit (after the first two groups, which have none), and each
|
|
12
|
+
// base is the previous base plus the span the previous symbol's extra bits cover. That rule rebuilds both RFC 1951
|
|
13
|
+
// section 3.2.5 tables: lengths 3, 4, ... 227 for symbols 257..284 and distances 1, 2, ... 24577 for 0..29. Length
|
|
14
|
+
// symbol 285 breaks the pattern (exactly 258, no extra bits), passed as `lastBase`.
|
|
15
|
+
function symbolRanges(count, groupShift, first, lastBase) {
|
|
16
|
+
const base = new Uint16Array(count),
|
|
17
|
+
extra = new Uint8Array(count);
|
|
18
|
+
for (let i = 0, value = first; i < count; i++) {
|
|
19
|
+
extra[i] = Math.max(0, (i >> groupShift) - 1);
|
|
20
|
+
base[i] = value;
|
|
21
|
+
value += 1 << extra[i];
|
|
22
|
+
}
|
|
23
|
+
if (lastBase !== undefined) {
|
|
24
|
+
base[count - 1] = lastBase;
|
|
25
|
+
extra[count - 1] = 0;
|
|
26
|
+
}
|
|
27
|
+
return [base, extra];
|
|
28
|
+
}
|
|
29
|
+
const LENGTHS = /* @__PURE__ */ symbolRanges(29, 2, 3, 258);
|
|
30
|
+
const DISTANCES = /* @__PURE__ */ symbolRanges(30, 1, 1);
|
|
31
|
+
const LENGTH_258 = 28;
|
|
32
|
+
const END_OF_BLOCK = 256;
|
|
33
|
+
// RFC 1951 section 3.2.7: order in which the code length alphabet's own code lengths are stored.
|
|
34
|
+
const CODE_LENGTH_ORDER = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
|
|
35
|
+
const MAX_BITS = 15;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The decoding table for the canonical code RFC 1951 section 3.2.2 assigns to these code lengths: one entry per
|
|
39
|
+
* pattern of the next `width` stream bits (first stream bit lowest), `symbol << 4 | code length`, 0 for patterns no
|
|
40
|
+
* code starts with.
|
|
41
|
+
*/
|
|
42
|
+
function huffmanTable(lengths) {
|
|
43
|
+
const perLength = new Uint16Array(MAX_BITS + 1);
|
|
44
|
+
let width = 1;
|
|
45
|
+
for (const length of lengths)
|
|
46
|
+
if (length) {
|
|
47
|
+
perLength[length]++;
|
|
48
|
+
width = Math.max(width, length);
|
|
49
|
+
}
|
|
50
|
+
// Kraft sum: more codes of some length than the shorter ones leave room for cannot be decoded. An incomplete set is
|
|
51
|
+
// accepted (RFC 1951 allows a lone distance code); its unused patterns stay 0 and throw only if the data uses them.
|
|
52
|
+
for (let length = 1, room = 1; length <= MAX_BITS; length++) {
|
|
53
|
+
room = room * 2 - perLength[length];
|
|
54
|
+
if (room < 0) throw damaged('has an over-subscribed Huffman code');
|
|
55
|
+
}
|
|
56
|
+
const nextCode = new Uint16Array(MAX_BITS + 1);
|
|
57
|
+
for (let length = 1, code = 0; length <= MAX_BITS; length++) {
|
|
58
|
+
code = (code + (length > 1 ? perLength[length - 1] : 0)) << 1;
|
|
59
|
+
nextCode[length] = code;
|
|
60
|
+
}
|
|
61
|
+
const entries = new Int32Array(1 << width);
|
|
62
|
+
for (let symbol = 0; symbol < lengths.length; symbol++) {
|
|
63
|
+
const length = lengths[symbol];
|
|
64
|
+
if (!length) continue;
|
|
65
|
+
// Codes are defined most significant bit first but arrive first bit lowest, so the table is indexed reversed.
|
|
66
|
+
const code = nextCode[length]++;
|
|
67
|
+
let reversed = 0;
|
|
68
|
+
for (let bit = 0; bit < length; bit++) reversed |= ((code >> bit) & 1) << (length - 1 - bit);
|
|
69
|
+
const entry = (symbol << 4) | length;
|
|
70
|
+
for (let index = reversed; index < entries.length; index += 1 << length) entries[index] = entry;
|
|
71
|
+
}
|
|
72
|
+
return [entries, width];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// The fixed literal/length and distance codes of block type 1 (RFC 1951 section 3.2.6), built on first use.
|
|
76
|
+
let fixedTables;
|
|
77
|
+
|
|
78
|
+
/** One zlib stream being inflated; fields and steps are private so the minifier shortens their names. */
|
|
79
|
+
class Inflater {
|
|
80
|
+
#input;
|
|
81
|
+
#limit;
|
|
82
|
+
#limitHint;
|
|
83
|
+
#output;
|
|
84
|
+
#position;
|
|
85
|
+
#held = 0;
|
|
86
|
+
#count = 0;
|
|
87
|
+
#written = 0;
|
|
88
|
+
constructor(input, size, start = 2) {
|
|
89
|
+
this.#input = input;
|
|
90
|
+
this.#position = start;
|
|
91
|
+
this.#limit = size.limit;
|
|
92
|
+
this.#limitHint = size.limitHint ?? 'raise limit to allow more decompressed bytes';
|
|
93
|
+
// A growable buffer starts no larger than its limit, so a limit under 1 KiB holds as well as a larger one.
|
|
94
|
+
this.#output = new Uint8Array(this.#limit === undefined ? size : Math.min(1024, this.#limit));
|
|
95
|
+
}
|
|
96
|
+
// The stream is read least significant bit first, holding up to 32 bits ahead.
|
|
97
|
+
#refill() {
|
|
98
|
+
while (this.#count <= 24 && this.#position < this.#input.length) {
|
|
99
|
+
this.#held |= this.#input[this.#position++] << this.#count;
|
|
100
|
+
this.#count += 8;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/** The next `n` bits (0..16) as an unsigned number. */
|
|
104
|
+
#bits(n) {
|
|
105
|
+
if (this.#count < n) {
|
|
106
|
+
this.#refill();
|
|
107
|
+
if (this.#count < n) throw damaged('ends early while reading DEFLATE bits');
|
|
108
|
+
}
|
|
109
|
+
const value = this.#held & ((1 << n) - 1);
|
|
110
|
+
this.#held >>>= n;
|
|
111
|
+
this.#count -= n;
|
|
112
|
+
return value;
|
|
113
|
+
}
|
|
114
|
+
/** The next symbol of a Huffman code. */
|
|
115
|
+
#symbol(table) {
|
|
116
|
+
if (this.#count < table[1]) this.#refill();
|
|
117
|
+
const entry = table[0][this.#held & (table[0].length - 1)],
|
|
118
|
+
length = entry & 15;
|
|
119
|
+
if (entry === 0) throw damaged('uses a Huffman code its block does not define');
|
|
120
|
+
if (length > this.#count) throw damaged('ends early while reading a DEFLATE Huffman code');
|
|
121
|
+
this.#held >>>= length;
|
|
122
|
+
this.#count -= length;
|
|
123
|
+
return entry >> 4;
|
|
124
|
+
}
|
|
125
|
+
/** Drops the bits left in the current byte and returns the position of the next whole byte. */
|
|
126
|
+
#align() {
|
|
127
|
+
this.#position -= this.#count >> 3;
|
|
128
|
+
this.#held = this.#count = 0;
|
|
129
|
+
return this.#position;
|
|
130
|
+
}
|
|
131
|
+
/** Room in the output for `need` bytes: a growable buffer doubles, up to the limit. */
|
|
132
|
+
#room(need) {
|
|
133
|
+
const output = this.#output,
|
|
134
|
+
limit = this.#limit;
|
|
135
|
+
if (need <= output.length) return output;
|
|
136
|
+
if (limit === undefined)
|
|
137
|
+
throw damaged(`inflates to at least ${need} bytes, above the expected ${output.length} bytes`);
|
|
138
|
+
if (need > limit)
|
|
139
|
+
throw fault(
|
|
140
|
+
'failed',
|
|
141
|
+
`Compressed data needs at least ${need} bytes, above the limit ${limit} bytes; ${this.#limitHint}`,
|
|
142
|
+
);
|
|
143
|
+
const bigger = new Uint8Array(Math.min(limit, Math.max(output.length * 2, need)));
|
|
144
|
+
bigger.set(output);
|
|
145
|
+
return (this.#output = bigger);
|
|
146
|
+
}
|
|
147
|
+
/** One Huffman-coded block. */
|
|
148
|
+
#block(literals, distances) {
|
|
149
|
+
let output = this.#output,
|
|
150
|
+
written = this.#written;
|
|
151
|
+
for (;;) {
|
|
152
|
+
const next = this.#symbol(literals);
|
|
153
|
+
if (next < END_OF_BLOCK) {
|
|
154
|
+
if (written >= output.length) output = this.#room(written + 1);
|
|
155
|
+
output[written++] = next;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (next === END_OF_BLOCK) return (this.#written = written);
|
|
159
|
+
const lengthSymbol = next - 257;
|
|
160
|
+
if (lengthSymbol > LENGTH_258) throw damaged(`uses the reserved length symbol ${next}`);
|
|
161
|
+
const length = LENGTHS[0][lengthSymbol] + this.#bits(LENGTHS[1][lengthSymbol]),
|
|
162
|
+
distanceSymbol = this.#symbol(distances);
|
|
163
|
+
if (distanceSymbol >= 30) throw damaged(`uses the reserved distance symbol ${distanceSymbol}`);
|
|
164
|
+
const distance = DISTANCES[0][distanceSymbol] + this.#bits(DISTANCES[1][distanceSymbol]);
|
|
165
|
+
if (distance > written)
|
|
166
|
+
throw damaged(`refers back ${distance} bytes with only ${written} bytes written, before the start of its data`);
|
|
167
|
+
if (written + length > output.length) output = this.#room(written + length);
|
|
168
|
+
// Front to back, one byte at a time: a distance shorter than the length repeats the bytes just written.
|
|
169
|
+
for (let from = written - distance, stop = written + length; written < stop; ) output[written++] = output[from++];
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/** Code lengths of a dynamic block's two codes (RFC 1951 section 3.2.7), themselves Huffman coded. */
|
|
173
|
+
#dynamic() {
|
|
174
|
+
const literalCount = this.#bits(5) + 257,
|
|
175
|
+
distanceCount = this.#bits(5) + 1,
|
|
176
|
+
codeLengthCount = this.#bits(4) + 4;
|
|
177
|
+
if (literalCount > 286) throw damaged(`declares ${literalCount} literal/length codes (at most 286)`);
|
|
178
|
+
const codeLengthLengths = new Uint8Array(19);
|
|
179
|
+
for (let i = 0; i < codeLengthCount; i++) codeLengthLengths[CODE_LENGTH_ORDER[i]] = this.#bits(3);
|
|
180
|
+
const codeLengthCode = huffmanTable(codeLengthLengths);
|
|
181
|
+
// One run of lengths covers both codes; repeats may cross from the literal lengths into the distance lengths.
|
|
182
|
+
const lengths = new Uint8Array(literalCount + distanceCount);
|
|
183
|
+
for (let filled = 0; filled < lengths.length; ) {
|
|
184
|
+
const next = this.#symbol(codeLengthCode);
|
|
185
|
+
if (next < 16) {
|
|
186
|
+
lengths[filled++] = next;
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
let value = 0,
|
|
190
|
+
repeat;
|
|
191
|
+
if (next === 16) {
|
|
192
|
+
if (filled === 0) throw damaged('repeats a code length before the first one');
|
|
193
|
+
value = lengths[filled - 1];
|
|
194
|
+
repeat = 3 + this.#bits(2);
|
|
195
|
+
} else repeat = next === 17 ? 3 + this.#bits(3) : 11 + this.#bits(7);
|
|
196
|
+
if (repeat > lengths.length - filled) throw damaged('repeats code lengths past the declared count');
|
|
197
|
+
lengths.fill(value, filled, filled + repeat);
|
|
198
|
+
filled += repeat;
|
|
199
|
+
}
|
|
200
|
+
if (!lengths[END_OF_BLOCK]) throw damaged('has a block without an end-of-block code');
|
|
201
|
+
this.#block(huffmanTable(lengths.subarray(0, literalCount)), huffmanTable(lengths.subarray(literalCount)));
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Decodes every block and checks the trailer; see {@link inflateZlib}. A `raw` stream (no zlib header or trailer)
|
|
205
|
+
* returns its bytes and the position after its final block instead; see {@link inflateRaw}.
|
|
206
|
+
*/
|
|
207
|
+
run(size, checkAdler, raw) {
|
|
208
|
+
const input = this.#input;
|
|
209
|
+
if (!raw) {
|
|
210
|
+
if (input.length < 2) throw damaged('is too short for a zlib header');
|
|
211
|
+
const method = input[0],
|
|
212
|
+
flags = input[1];
|
|
213
|
+
// RFC 1950: compression method 8 (deflate), window of at most 32 KiB, header check, no preset dictionary.
|
|
214
|
+
if ((method & 15) !== 8) throw damaged(`declares zlib compression method ${method & 15}, not DEFLATE (8)`);
|
|
215
|
+
if (method >> 4 > 7) throw damaged(`declares zlib window size ${method >> 4}, above the maximum 7`);
|
|
216
|
+
if (((method << 8) | flags) % 31 !== 0) throw damaged('fails its zlib header check');
|
|
217
|
+
if (flags & 0x20)
|
|
218
|
+
throw fault(
|
|
219
|
+
'unsupported',
|
|
220
|
+
'Compressed data needs a preset dictionary; recompress it without a preset dictionary',
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
let final = 0;
|
|
224
|
+
do {
|
|
225
|
+
final = this.#bits(1);
|
|
226
|
+
const type = this.#bits(2);
|
|
227
|
+
if (type === 0) {
|
|
228
|
+
// Stored block: byte aligned LEN, NLEN (its one's complement), then LEN raw bytes.
|
|
229
|
+
let at = this.#align();
|
|
230
|
+
if (input.length - at < 4) throw damaged('ends early in its stored block length header');
|
|
231
|
+
const length = input[at] | (input[at + 1] << 8),
|
|
232
|
+
check = input[at + 2] | (input[at + 3] << 8);
|
|
233
|
+
if ((length ^ 0xffff) !== check) throw damaged('has a stored block whose length check fails');
|
|
234
|
+
at += 4;
|
|
235
|
+
if (length > input.length - at) throw damaged('ends early in its stored block data');
|
|
236
|
+
this.#room(this.#written + length).set(input.subarray(at, at + length), this.#written);
|
|
237
|
+
this.#written += length;
|
|
238
|
+
this.#position = at + length;
|
|
239
|
+
} else if (type === 1) {
|
|
240
|
+
fixedTables ??= [
|
|
241
|
+
huffmanTable(new Uint8Array(288).fill(8, 0, 144).fill(9, 144, 256).fill(7, 256, 280).fill(8, 280, 288)),
|
|
242
|
+
huffmanTable(new Uint8Array(32).fill(5)),
|
|
243
|
+
];
|
|
244
|
+
this.#block(fixedTables[0], fixedTables[1]);
|
|
245
|
+
} else if (type === 2) this.#dynamic();
|
|
246
|
+
else throw damaged('uses the reserved block type 3');
|
|
247
|
+
} while (!final);
|
|
248
|
+
if (this.#limit === undefined && this.#written !== size)
|
|
249
|
+
throw damaged(`inflates to ${this.#written} bytes, not the expected ${size} bytes`);
|
|
250
|
+
const output = this.#output.subarray(0, this.#written);
|
|
251
|
+
const at = this.#align();
|
|
252
|
+
if (raw) return { output, end: at };
|
|
253
|
+
// RFC 1950 trailer: the Adler-32 of the decompressed bytes, big-endian, after the last block's final byte.
|
|
254
|
+
if (input.length - at < 4) throw damaged('ends before its Adler-32 checksum');
|
|
255
|
+
const stored = ((input[at] << 24) | (input[at + 1] << 16) | (input[at + 2] << 8) | input[at + 3]) >>> 0;
|
|
256
|
+
if (checkAdler && adler32(output) !== stored) throw damaged('fails its Adler-32 checksum');
|
|
257
|
+
return output;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** Inflates zlib `input`; see inflate.d.ts. */
|
|
262
|
+
export const inflateZlib = (input, size, checkAdler = true) => new Inflater(input, size).run(size, checkAdler);
|
|
263
|
+
|
|
264
|
+
/** Inflates the raw DEFLATE stream at `start` of `input`; see inflate.d.ts. */
|
|
265
|
+
export const inflateRaw = (input, start, size) => new Inflater(input, size, start).run(size, false, true);
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Adler-32 (RFC 1950 section 8.2): two sums modulo 65521. 5552 bytes is the longest run after which the second sum
|
|
269
|
+
* still fits in 32 unsigned bits, so the sums stay 32-bit integers (twice as fast as doubles) and are reduced per run.
|
|
270
|
+
*/
|
|
271
|
+
function adler32(bytes) {
|
|
272
|
+
let a = 1,
|
|
273
|
+
b = 0;
|
|
274
|
+
for (let start = 0; start < bytes.length; start += 5552) {
|
|
275
|
+
const end = Math.min(bytes.length, start + 5552);
|
|
276
|
+
for (let i = start; i < end; i++) {
|
|
277
|
+
a = (a + bytes[i]) | 0;
|
|
278
|
+
b = (b + a) | 0;
|
|
279
|
+
}
|
|
280
|
+
a %= 65521;
|
|
281
|
+
b = (b >>> 0) % 65521;
|
|
282
|
+
}
|
|
283
|
+
return ((b << 16) | a) >>> 0;
|
|
284
|
+
}
|
package/dist/shared/normals.js
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
// Area-weighted vertex normal accumulation for indexed triangles: the loaders' computeVertexNormals and the exporters'
|
|
2
|
-
// generated normals.
|
|
3
|
-
|
|
4
|
-
/** Adds each triangle's unnormalized face normal (length twice its area) to its three vertices in `out` (xyz each). */
|
|
5
|
-
export function accumulateNormals(positions, indices, out) {
|
|
6
|
-
for (let t = 0; t + 2 < indices.length; t += 3) {
|
|
7
|
-
const a = indices[t] * 3,
|
|
8
|
-
b = indices[t + 1] * 3,
|
|
9
|
-
c = indices[t + 2] * 3;
|
|
10
|
-
const ux = positions[b] - positions[a],
|
|
11
|
-
uy = positions[b + 1] - positions[a + 1],
|
|
12
|
-
uz = positions[b + 2] - positions[a + 2];
|
|
13
|
-
const vx = positions[c] - positions[a],
|
|
14
|
-
vy = positions[c + 1] - positions[a + 1],
|
|
15
|
-
vz = positions[c + 2] - positions[a + 2];
|
|
16
|
-
const nx = uy * vz - uz * vy,
|
|
17
|
-
ny = uz * vx - ux * vz,
|
|
18
|
-
nz = ux * vy - uy * vx;
|
|
19
|
-
for (const v of [a, b, c]) {
|
|
20
|
-
out[v] += nx;
|
|
21
|
-
out[v + 1] += ny;
|
|
22
|
-
out[v + 2] += nz;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
return out;
|
|
26
|
-
}
|
|
1
|
+
// Area-weighted vertex normal accumulation for indexed triangles: the loaders' computeVertexNormals and the exporters'
|
|
2
|
+
// generated normals.
|
|
3
|
+
|
|
4
|
+
/** Adds each triangle's unnormalized face normal (length twice its area) to its three vertices in `out` (xyz each). */
|
|
5
|
+
export function accumulateNormals(positions, indices, out) {
|
|
6
|
+
for (let t = 0; t + 2 < indices.length; t += 3) {
|
|
7
|
+
const a = indices[t] * 3,
|
|
8
|
+
b = indices[t + 1] * 3,
|
|
9
|
+
c = indices[t + 2] * 3;
|
|
10
|
+
const ux = positions[b] - positions[a],
|
|
11
|
+
uy = positions[b + 1] - positions[a + 1],
|
|
12
|
+
uz = positions[b + 2] - positions[a + 2];
|
|
13
|
+
const vx = positions[c] - positions[a],
|
|
14
|
+
vy = positions[c + 1] - positions[a + 1],
|
|
15
|
+
vz = positions[c + 2] - positions[a + 2];
|
|
16
|
+
const nx = uy * vz - uz * vy,
|
|
17
|
+
ny = uz * vx - ux * vz,
|
|
18
|
+
nz = ux * vy - uy * vx;
|
|
19
|
+
for (const v of [a, b, c]) {
|
|
20
|
+
out[v] += nx;
|
|
21
|
+
out[v + 1] += ny;
|
|
22
|
+
out[v + 2] += nz;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return out;
|
|
26
|
+
}
|