@manycore/aholo-splat-transform 1.2.8 → 1.2.10
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/CHANGELOG.md +124 -113
- package/README.md +39 -39
- package/THIRD_PARTY_LICENSES.txt +1373 -1373
- package/bin/cli.js +125 -118
- package/dist/SplatData.d.ts +67 -67
- package/dist/SplatData.js +167 -150
- package/dist/constant.d.ts +3 -3
- package/dist/constant.js +13 -13
- package/dist/file/IFile.d.ts +5 -5
- package/dist/file/IFile.js +1 -1
- package/dist/file/esz.d.ts +11 -11
- package/dist/file/esz.js +337 -322
- package/dist/file/index.d.ts +8 -8
- package/dist/file/index.js +7 -7
- package/dist/file/ksplat.d.ts +12 -12
- package/dist/file/ksplat.js +293 -231
- package/dist/file/lcc.d.ts +11 -11
- package/dist/file/lcc.js +161 -158
- package/dist/file/ply.d.ts +13 -13
- package/dist/file/ply.js +439 -390
- package/dist/file/sog.d.ts +80 -80
- package/dist/file/sog.js +525 -494
- package/dist/file/splat.d.ts +6 -6
- package/dist/file/splat.js +119 -99
- package/dist/file/spz.d.ts +11 -11
- package/dist/file/spz.js +597 -583
- package/dist/file/voxel.d.ts +43 -37
- package/dist/file/voxel.js +411 -280
- package/dist/index.d.ts +33 -33
- package/dist/index.js +54 -54
- package/dist/native/index.d.ts +54 -54
- package/dist/native/index.js +122 -129
- package/dist/native/utils.d.ts +1 -0
- package/dist/native/utils.js +54 -0
- package/dist/tasks/AutoChunkLodTask.d.ts +13 -13
- package/dist/tasks/AutoChunkLodTask.js +117 -117
- package/dist/tasks/AutoLodTask.d.ts +10 -10
- package/dist/tasks/AutoLodTask.js +20 -20
- package/dist/tasks/BaseTask.d.ts +15 -15
- package/dist/tasks/BaseTask.js +5 -5
- package/dist/tasks/FlexLodTask.d.ts +12 -12
- package/dist/tasks/FlexLodTask.js +54 -44
- package/dist/tasks/ModifyTask.d.ts +9 -9
- package/dist/tasks/ModifyTask.js +166 -156
- package/dist/tasks/ReadTask.d.ts +9 -9
- package/dist/tasks/ReadTask.js +29 -29
- package/dist/tasks/SkeletonLodTask.d.ts +10 -10
- package/dist/tasks/SkeletonLodTask.js +176 -156
- package/dist/tasks/VoxelTask.d.ts +35 -30
- package/dist/tasks/VoxelTask.js +40 -37
- package/dist/tasks/WriteTask.d.ts +12 -12
- package/dist/tasks/WriteTask.js +70 -70
- package/dist/utils/BufferReader.d.ts +12 -12
- package/dist/utils/BufferReader.js +45 -45
- package/dist/utils/Logger.d.ts +11 -11
- package/dist/utils/Logger.js +40 -40
- package/dist/utils/StreamChunkDecoder.d.ts +16 -16
- package/dist/utils/StreamChunkDecoder.js +31 -31
- package/dist/utils/index.d.ts +27 -27
- package/dist/utils/index.js +101 -101
- package/dist/utils/k-means.d.ts +4 -4
- package/dist/utils/k-means.js +340 -341
- package/dist/utils/math.d.ts +46 -46
- package/dist/utils/math.js +350 -346
- package/dist/utils/quantize-1d.d.ts +4 -4
- package/dist/utils/quantize-1d.js +164 -164
- package/dist/utils/sh-rotate.d.ts +2 -2
- package/dist/utils/sh-rotate.js +236 -175
- package/dist/utils/splat.d.ts +21 -21
- package/dist/utils/splat.js +397 -387
- package/dist/utils/voxel/binary.d.ts +8 -0
- package/dist/utils/voxel/binary.js +176 -0
- package/dist/utils/voxel/common.d.ts +178 -162
- package/dist/utils/voxel/common.js +1752 -1682
- package/dist/utils/voxel/coplanar-merge.d.ts +63 -63
- package/dist/utils/voxel/coplanar-merge.js +818 -819
- package/dist/utils/voxel/filter-cluster.d.ts +20 -0
- package/dist/utils/voxel/filter-cluster.js +628 -0
- package/dist/utils/voxel/gpu-dilation.d.ts +2 -2
- package/dist/utils/voxel/gpu-dilation.js +677 -656
- package/dist/utils/voxel/marching-cubes.d.ts +42 -42
- package/dist/utils/voxel/marching-cubes.js +1645 -1657
- package/dist/utils/voxel/mesh.d.ts +3 -3
- package/dist/utils/voxel/mesh.js +130 -130
- package/dist/utils/voxel/nav.d.ts +29 -29
- package/dist/utils/voxel/nav.js +1068 -1043
- package/dist/utils/voxel/postprocess.d.ts +23 -23
- package/dist/utils/voxel/postprocess.js +408 -375
- package/dist/utils/voxel/voxel-faces.d.ts +18 -18
- package/dist/utils/voxel/voxel-faces.js +662 -663
- package/dist/utils/voxel/voxelize.d.ts +34 -33
- package/dist/utils/voxel/voxelize.js +1208 -1193
- package/dist/utils/webgpu.d.ts +8 -8
- package/dist/utils/webgpu.js +122 -122
- package/package.json +38 -39
- package/dist/native/cpp/bin/linux/binding.node +0 -0
- package/dist/native/cpp/bin/windows/binding.node +0 -0
package/dist/file/sog.js
CHANGED
|
@@ -1,494 +1,525 @@
|
|
|
1
|
-
import { unzipSync, zipSync } from 'fflate';
|
|
2
|
-
import { Buffer } from 'node:buffer';
|
|
3
|
-
import { decodeWebP, encodeWebP, WebPLosslessProfile } from '../native/index.js';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
[
|
|
11
|
-
[1, 2,
|
|
12
|
-
]
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
data[i * channels +
|
|
25
|
-
data[i * channels +
|
|
26
|
-
data[i * channels +
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
this.
|
|
43
|
-
this.
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
offset
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
this.
|
|
78
|
-
this.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
this.
|
|
87
|
-
this.
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const
|
|
93
|
-
const
|
|
94
|
-
const
|
|
95
|
-
const
|
|
96
|
-
const
|
|
97
|
-
const
|
|
98
|
-
const
|
|
99
|
-
const
|
|
100
|
-
const
|
|
101
|
-
const
|
|
102
|
-
const
|
|
103
|
-
const
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const
|
|
112
|
-
const
|
|
113
|
-
const
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
const
|
|
141
|
-
single.
|
|
142
|
-
single.
|
|
143
|
-
single.
|
|
144
|
-
single.
|
|
145
|
-
single.
|
|
146
|
-
single.
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
const
|
|
184
|
-
const
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
const
|
|
278
|
-
const
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
const
|
|
362
|
-
const
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
const
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
const
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
};
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
1
|
+
import { unzipSync, zipSync } from 'fflate';
|
|
2
|
+
import { Buffer } from 'node:buffer';
|
|
3
|
+
import { decodeWebP, encodeWebP, WebPLosslessProfile } from '../native/index.js';
|
|
4
|
+
import { ColIdx } from '../SplatData.js';
|
|
5
|
+
import { SH_C0, SH_MAPS, NUM_F_REST_TO_SH_DEGREE } from '../constant.js';
|
|
6
|
+
import { getOrCreateDevice, kmeans, logger, mortonSort, quantize1d, isUrl, extractFromRootDir, clamp, } from '../utils/index.js';
|
|
7
|
+
const ZIP_MAGIC = 0x04034b50;
|
|
8
|
+
const PERM_TABLE = [
|
|
9
|
+
// original quat idx ---> actual storage idx
|
|
10
|
+
[0, 1, 2, 3],
|
|
11
|
+
[3, 1, 2, 0],
|
|
12
|
+
[1, 3, 2, 0],
|
|
13
|
+
[1, 2, 3, 0],
|
|
14
|
+
];
|
|
15
|
+
const TEMP_ROT = new Float32Array(4);
|
|
16
|
+
function logTransform(value) {
|
|
17
|
+
return Math.sign(value) * Math.log(Math.abs(value) + 1);
|
|
18
|
+
}
|
|
19
|
+
function writeTableData(table, indices, width, height, channels = 4) {
|
|
20
|
+
const data = new Uint8Array(width * height * channels);
|
|
21
|
+
const numColumns = table.length;
|
|
22
|
+
for (let i = 0; i < indices.length; ++i) {
|
|
23
|
+
const idx = indices[i];
|
|
24
|
+
data[i * channels + 0] = table[0][idx];
|
|
25
|
+
data[i * channels + 1] = numColumns > 1 ? table[1][idx] : 0;
|
|
26
|
+
data[i * channels + 2] = numColumns > 2 ? table[2][idx] : 0;
|
|
27
|
+
data[i * channels + 3] = numColumns > 3 ? table[3][idx] : 255;
|
|
28
|
+
}
|
|
29
|
+
return data;
|
|
30
|
+
}
|
|
31
|
+
function buildSHTableMap(shCoeffs) {
|
|
32
|
+
const result = [];
|
|
33
|
+
for (let i = 0; i < 3; i++) {
|
|
34
|
+
for (let j = 0; j < shCoeffs; j++) {
|
|
35
|
+
result.push(j * 3 + i);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
export class SogFile {
|
|
41
|
+
constructor(iterations = 10) {
|
|
42
|
+
this.iterations = iterations;
|
|
43
|
+
this.counts = 0;
|
|
44
|
+
this.shDegree = 0;
|
|
45
|
+
/**
|
|
46
|
+
* @internal
|
|
47
|
+
*/
|
|
48
|
+
this.refs = {};
|
|
49
|
+
}
|
|
50
|
+
async load(stream, contentLength) {
|
|
51
|
+
const buffer = new Uint8Array(contentLength);
|
|
52
|
+
const reader = stream.getReader();
|
|
53
|
+
let offset = 0;
|
|
54
|
+
while (true) {
|
|
55
|
+
const { done, value } = await reader.read();
|
|
56
|
+
if (done) {
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
buffer.set(value, offset);
|
|
60
|
+
offset += value.length;
|
|
61
|
+
}
|
|
62
|
+
let metaBuffer = buffer;
|
|
63
|
+
const view = new DataView(buffer.buffer);
|
|
64
|
+
if (view.getUint32(0, true) === ZIP_MAGIC) {
|
|
65
|
+
this.refs = extractFromRootDir(unzipSync(buffer));
|
|
66
|
+
metaBuffer = this.refs['meta.json'];
|
|
67
|
+
if (!metaBuffer) {
|
|
68
|
+
throw new Error('SOG meta.json not found in the zip archive.');
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
this.meta = JSON.parse(new TextDecoder().decode(metaBuffer));
|
|
72
|
+
if (this.meta.version === undefined) {
|
|
73
|
+
const { means, quats, shN } = this.meta;
|
|
74
|
+
if (quats.encoding !== 'quaternion_packed') {
|
|
75
|
+
throw new Error('Unsupported quaternion encoding');
|
|
76
|
+
}
|
|
77
|
+
this.counts = means.shape[0];
|
|
78
|
+
this.shDegree = shN ? NUM_F_REST_TO_SH_DEGREE[shN.shape[1]] : 0;
|
|
79
|
+
this.version = 1;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
const { version, count, shN } = this.meta;
|
|
83
|
+
if (version !== 2) {
|
|
84
|
+
throw new Error(`Unsupported SOGS version: ${version}`);
|
|
85
|
+
}
|
|
86
|
+
this.counts = count;
|
|
87
|
+
this.shDegree = shN?.bands ?? 0;
|
|
88
|
+
this.version = version;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
parse_v1(data, offset) {
|
|
92
|
+
const setFn = data.set.bind(data);
|
|
93
|
+
const setShFn = data.setShN.bind(data);
|
|
94
|
+
const { meta, counts, shDegree, cached } = this;
|
|
95
|
+
const [mean0, mean1, scale0, quat0, color0, centroids, labels] = cached.map(v => v.data);
|
|
96
|
+
const { means: { mins: [centerMinX, centerMinY, centerMinZ], maxs: [centerMaxX, centerMaxY, centerMaxZ], }, scales: { mins: [scaleMinX, scaleMinY, scaleMinZ], maxs: [scaleMaxX, scaleMaxY, scaleMaxZ], }, sh0: { mins: [colorMinR, colorMinG, colorMinB, colorMinA], maxs: [colorMaxR, colorMaxG, colorMaxB, colorMaxA], }, shN, } = meta;
|
|
97
|
+
const rangeX = (centerMaxX - centerMinX) / 65535;
|
|
98
|
+
const rangeY = (centerMaxY - centerMinY) / 65535;
|
|
99
|
+
const rangeZ = (centerMaxZ - centerMinZ) / 65535;
|
|
100
|
+
const SX_LUT = new Float32Array(256);
|
|
101
|
+
const SY_LUT = new Float32Array(256);
|
|
102
|
+
const SZ_LUT = new Float32Array(256);
|
|
103
|
+
const scaleRangeX = (scaleMaxX - scaleMinX) / 255;
|
|
104
|
+
const scaleRangeY = (scaleMaxY - scaleMinY) / 255;
|
|
105
|
+
const scaleRangeZ = (scaleMaxZ - scaleMinZ) / 255;
|
|
106
|
+
for (let i = 0; i < 256; i++) {
|
|
107
|
+
SX_LUT[i] = Math.exp(scaleMinX + scaleRangeX * i);
|
|
108
|
+
SY_LUT[i] = Math.exp(scaleMinY + scaleRangeY * i);
|
|
109
|
+
SZ_LUT[i] = Math.exp(scaleMinZ + scaleRangeZ * i);
|
|
110
|
+
}
|
|
111
|
+
const A_LUT = new Float32Array(256);
|
|
112
|
+
const colorRangeR = (colorMaxR - colorMinR) / 255;
|
|
113
|
+
const colorRangeG = (colorMaxG - colorMinG) / 255;
|
|
114
|
+
const colorRangeB = (colorMaxB - colorMinB) / 255;
|
|
115
|
+
const colorRangeA = (colorMaxA - colorMinA) / 255;
|
|
116
|
+
for (let i = 0; i < 256; i++) {
|
|
117
|
+
A_LUT[i] = 1.0 / (1.0 + Math.exp(-(colorMinA + colorRangeA * i)));
|
|
118
|
+
}
|
|
119
|
+
const single = {
|
|
120
|
+
x: 0,
|
|
121
|
+
y: 0,
|
|
122
|
+
z: 0,
|
|
123
|
+
sx: 0,
|
|
124
|
+
sy: 0,
|
|
125
|
+
sz: 0,
|
|
126
|
+
qx: 0,
|
|
127
|
+
qy: 0,
|
|
128
|
+
qz: 0,
|
|
129
|
+
qw: 0,
|
|
130
|
+
r: 0,
|
|
131
|
+
g: 0,
|
|
132
|
+
b: 0,
|
|
133
|
+
a: 0,
|
|
134
|
+
shN: [],
|
|
135
|
+
};
|
|
136
|
+
for (let i = 0; i < counts; i++) {
|
|
137
|
+
const i4 = i * 4;
|
|
138
|
+
const x = centerMinX + rangeX * (mean0[i4 + 0] + (mean1[i4 + 0] << 8));
|
|
139
|
+
const y = centerMinY + rangeY * (mean0[i4 + 1] + (mean1[i4 + 1] << 8));
|
|
140
|
+
const z = centerMinZ + rangeZ * (mean0[i4 + 2] + (mean1[i4 + 2] << 8));
|
|
141
|
+
single.x = Math.sign(x) * (Math.exp(Math.abs(x)) - 1);
|
|
142
|
+
single.y = Math.sign(y) * (Math.exp(Math.abs(y)) - 1);
|
|
143
|
+
single.z = Math.sign(z) * (Math.exp(Math.abs(z)) - 1);
|
|
144
|
+
single.sx = SX_LUT[scale0[i4 + 0]];
|
|
145
|
+
single.sy = SY_LUT[scale0[i4 + 1]];
|
|
146
|
+
single.sz = SZ_LUT[scale0[i4 + 2]];
|
|
147
|
+
TEMP_ROT[0] = (quat0[i4 + 0] / 255 - 0.5) * Math.SQRT2;
|
|
148
|
+
TEMP_ROT[1] = (quat0[i4 + 1] / 255 - 0.5) * Math.SQRT2;
|
|
149
|
+
TEMP_ROT[2] = (quat0[i4 + 2] / 255 - 0.5) * Math.SQRT2;
|
|
150
|
+
TEMP_ROT[3] = Math.sqrt(Math.max(0, 1.0 - TEMP_ROT[0] * TEMP_ROT[0] - TEMP_ROT[1] * TEMP_ROT[1] - TEMP_ROT[2] * TEMP_ROT[2]));
|
|
151
|
+
const PERM = PERM_TABLE[quat0[i4 + 3] - 252];
|
|
152
|
+
single.qx = TEMP_ROT[PERM[0]];
|
|
153
|
+
single.qy = TEMP_ROT[PERM[1]];
|
|
154
|
+
single.qz = TEMP_ROT[PERM[2]];
|
|
155
|
+
single.qw = TEMP_ROT[PERM[3]];
|
|
156
|
+
single.r = SH_C0 * (colorMinR + colorRangeR * color0[i4 + 0]) + 0.5;
|
|
157
|
+
single.g = SH_C0 * (colorMinG + colorRangeG * color0[i4 + 1]) + 0.5;
|
|
158
|
+
single.b = SH_C0 * (colorMinB + colorRangeB * color0[i4 + 2]) + 0.5;
|
|
159
|
+
single.a = A_LUT[color0[i4 + 3]];
|
|
160
|
+
setFn(offset + i, single);
|
|
161
|
+
}
|
|
162
|
+
if (shN) {
|
|
163
|
+
const centroidTexWidth = cached[5].width;
|
|
164
|
+
const { mins: min, maxs: max } = shN;
|
|
165
|
+
const range = (max - min) / 255;
|
|
166
|
+
const shCounts = SH_MAPS[shDegree];
|
|
167
|
+
const sh = new Array(shCounts);
|
|
168
|
+
const shCoeffs = shCounts / 3;
|
|
169
|
+
for (let i = 0; i < counts; i++) {
|
|
170
|
+
const i4 = i * 4;
|
|
171
|
+
const label = labels[i4] + (labels[i4 + 1] << 8);
|
|
172
|
+
const o = ((label >>> 6) * centroidTexWidth + (label & 63) * 15) * 4;
|
|
173
|
+
for (let j = 0; j < shCoeffs; j++) {
|
|
174
|
+
sh[j * 3 + 0] = min + range * centroids[o + j * 4 + 0];
|
|
175
|
+
sh[j * 3 + 1] = min + range * centroids[o + j * 4 + 1];
|
|
176
|
+
sh[j * 3 + 2] = min + range * centroids[o + j * 4 + 2];
|
|
177
|
+
}
|
|
178
|
+
setShFn(offset + i, sh);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
parse_v2(data, offset) {
|
|
183
|
+
const setFn = data.set.bind(data);
|
|
184
|
+
const setShFn = data.setShN.bind(data);
|
|
185
|
+
const { meta, counts, shDegree, cached } = this;
|
|
186
|
+
const { means, scales, sh0, shN } = meta;
|
|
187
|
+
const { mins: [centerMinX, centerMinY, centerMinZ], maxs: [centerMaxX, centerMaxY, centerMaxZ], } = means;
|
|
188
|
+
const { codebook: scaleCodebook } = scales;
|
|
189
|
+
const { codebook: sh0Codebook } = sh0;
|
|
190
|
+
const [mean0, mean1, scale0, quat0, color0, centroids, labels] = cached.map(img => img.data);
|
|
191
|
+
const rangeX = (centerMaxX - centerMinX) / 65535;
|
|
192
|
+
const rangeY = (centerMaxY - centerMinY) / 65535;
|
|
193
|
+
const rangeZ = (centerMaxZ - centerMinZ) / 65535;
|
|
194
|
+
const SCALE_LUT = scaleCodebook.map(v => Math.exp(v));
|
|
195
|
+
const single = {
|
|
196
|
+
x: 0,
|
|
197
|
+
y: 0,
|
|
198
|
+
z: 0,
|
|
199
|
+
sx: 0,
|
|
200
|
+
sy: 0,
|
|
201
|
+
sz: 0,
|
|
202
|
+
qx: 0,
|
|
203
|
+
qy: 0,
|
|
204
|
+
qz: 0,
|
|
205
|
+
qw: 0,
|
|
206
|
+
r: 0,
|
|
207
|
+
g: 0,
|
|
208
|
+
b: 0,
|
|
209
|
+
a: 0,
|
|
210
|
+
shN: [],
|
|
211
|
+
};
|
|
212
|
+
for (let i = 0; i < counts; i++) {
|
|
213
|
+
const i4 = i * 4;
|
|
214
|
+
const x = centerMinX + rangeX * (mean0[i4 + 0] + (mean1[i4 + 0] << 8));
|
|
215
|
+
const y = centerMinY + rangeY * (mean0[i4 + 1] + (mean1[i4 + 1] << 8));
|
|
216
|
+
const z = centerMinZ + rangeZ * (mean0[i4 + 2] + (mean1[i4 + 2] << 8));
|
|
217
|
+
single.x = Math.sign(x) * (Math.exp(Math.abs(x)) - 1);
|
|
218
|
+
single.y = Math.sign(y) * (Math.exp(Math.abs(y)) - 1);
|
|
219
|
+
single.z = Math.sign(z) * (Math.exp(Math.abs(z)) - 1);
|
|
220
|
+
single.sx = SCALE_LUT[scale0[i4 + 0]];
|
|
221
|
+
single.sy = SCALE_LUT[scale0[i4 + 1]];
|
|
222
|
+
single.sz = SCALE_LUT[scale0[i4 + 2]];
|
|
223
|
+
TEMP_ROT[0] = (quat0[i4 + 0] / 255 - 0.5) * Math.SQRT2;
|
|
224
|
+
TEMP_ROT[1] = (quat0[i4 + 1] / 255 - 0.5) * Math.SQRT2;
|
|
225
|
+
TEMP_ROT[2] = (quat0[i4 + 2] / 255 - 0.5) * Math.SQRT2;
|
|
226
|
+
TEMP_ROT[3] = Math.sqrt(Math.max(0, 1.0 - TEMP_ROT[0] * TEMP_ROT[0] - TEMP_ROT[1] * TEMP_ROT[1] - TEMP_ROT[2] * TEMP_ROT[2]));
|
|
227
|
+
const PERM = PERM_TABLE[quat0[i4 + 3] - 252];
|
|
228
|
+
single.qx = TEMP_ROT[PERM[0]];
|
|
229
|
+
single.qy = TEMP_ROT[PERM[1]];
|
|
230
|
+
single.qz = TEMP_ROT[PERM[2]];
|
|
231
|
+
single.qw = TEMP_ROT[PERM[3]];
|
|
232
|
+
single.r = SH_C0 * sh0Codebook[color0[i4 + 0]] + 0.5;
|
|
233
|
+
single.g = SH_C0 * sh0Codebook[color0[i4 + 1]] + 0.5;
|
|
234
|
+
single.b = SH_C0 * sh0Codebook[color0[i4 + 2]] + 0.5;
|
|
235
|
+
single.a = color0[i4 + 3] / 255;
|
|
236
|
+
setFn(offset + i, single);
|
|
237
|
+
}
|
|
238
|
+
if (shN) {
|
|
239
|
+
const { codebook } = shN;
|
|
240
|
+
const shCounts = SH_MAPS[shDegree];
|
|
241
|
+
const shCoeffs = shCounts / 3;
|
|
242
|
+
const offsetItemSize = shCoeffs * 4;
|
|
243
|
+
const sh = new Array(shCounts);
|
|
244
|
+
for (let i = 0; i < counts; i++) {
|
|
245
|
+
const i4 = i * 4;
|
|
246
|
+
const o = (labels[i4 + 0] + (labels[i4 + 1] << 8)) * offsetItemSize;
|
|
247
|
+
for (let j = 0; j < shCoeffs; j++) {
|
|
248
|
+
sh[j * 3] = codebook[centroids[o + j * 4 + 0]];
|
|
249
|
+
sh[j * 3 + 1] = codebook[centroids[o + j * 4 + 1]];
|
|
250
|
+
sh[j * 3 + 2] = codebook[centroids[o + j * 4 + 2]];
|
|
251
|
+
}
|
|
252
|
+
setShFn(offset + i, sh);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
async loadTexture(path) {
|
|
257
|
+
let buffer = this.refs[path];
|
|
258
|
+
if (!buffer) {
|
|
259
|
+
if (isUrl(path)) {
|
|
260
|
+
buffer = await fetch(path)
|
|
261
|
+
.then(res => res.arrayBuffer())
|
|
262
|
+
.then(buf => new Uint8Array(buf));
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (!buffer) {
|
|
266
|
+
throw new Error(`Cannot load texture: ${path}`);
|
|
267
|
+
}
|
|
268
|
+
const { data, width, height } = decodeWebP(buffer);
|
|
269
|
+
return {
|
|
270
|
+
data: new Uint8Array(data),
|
|
271
|
+
width,
|
|
272
|
+
height,
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
async read(stream, contentLength, data) {
|
|
276
|
+
await this.load(stream, contentLength);
|
|
277
|
+
const BlockOffset = await data.initBlock(this.counts, this.shDegree);
|
|
278
|
+
const { means, scales, quats, sh0, shN } = this.meta;
|
|
279
|
+
this.cached = await Promise.all([
|
|
280
|
+
means.files[0],
|
|
281
|
+
means.files[1],
|
|
282
|
+
scales.files[0],
|
|
283
|
+
quats.files[0],
|
|
284
|
+
sh0.files[0],
|
|
285
|
+
shN?.files[0],
|
|
286
|
+
shN?.files[1],
|
|
287
|
+
]
|
|
288
|
+
.filter(path => !!path)
|
|
289
|
+
.map(path => this.loadTexture(path)));
|
|
290
|
+
if (this.version === 1) {
|
|
291
|
+
this.parse_v1(data, BlockOffset);
|
|
292
|
+
}
|
|
293
|
+
else if (this.version === 2) {
|
|
294
|
+
this.parse_v2(data, BlockOffset);
|
|
295
|
+
}
|
|
296
|
+
else {
|
|
297
|
+
throw new Error(`Unsupported SOG version: ${this.version}`);
|
|
298
|
+
}
|
|
299
|
+
data.finishBlock();
|
|
300
|
+
}
|
|
301
|
+
async write(stream, data, indices = mortonSort(data)) {
|
|
302
|
+
const { counts, shDegree, shCounts, table } = data;
|
|
303
|
+
const width = Math.ceil(Math.sqrt(counts) / 4) * 4;
|
|
304
|
+
const height = Math.ceil(counts / width / 4) * 4;
|
|
305
|
+
const channels = 4;
|
|
306
|
+
const single = {
|
|
307
|
+
x: 0,
|
|
308
|
+
y: 0,
|
|
309
|
+
z: 0,
|
|
310
|
+
sx: 0,
|
|
311
|
+
sy: 0,
|
|
312
|
+
sz: 0,
|
|
313
|
+
qx: 0,
|
|
314
|
+
qy: 0,
|
|
315
|
+
qz: 0,
|
|
316
|
+
qw: 0,
|
|
317
|
+
r: 0,
|
|
318
|
+
g: 0,
|
|
319
|
+
b: 0,
|
|
320
|
+
a: 0,
|
|
321
|
+
shN: new Array(shCounts),
|
|
322
|
+
};
|
|
323
|
+
const webPProfile = new WebPLosslessProfile();
|
|
324
|
+
const output = {};
|
|
325
|
+
const meta = {
|
|
326
|
+
version: 2,
|
|
327
|
+
count: counts,
|
|
328
|
+
means: {
|
|
329
|
+
mins: [],
|
|
330
|
+
maxs: [],
|
|
331
|
+
files: ['means_l.webp', 'means_u.webp'],
|
|
332
|
+
},
|
|
333
|
+
scales: {
|
|
334
|
+
codebook: [],
|
|
335
|
+
files: ['scales.webp'],
|
|
336
|
+
},
|
|
337
|
+
quats: {
|
|
338
|
+
files: ['quats.webp'],
|
|
339
|
+
},
|
|
340
|
+
sh0: {
|
|
341
|
+
codebook: [],
|
|
342
|
+
files: ['sh0.webp'],
|
|
343
|
+
},
|
|
344
|
+
};
|
|
345
|
+
// means
|
|
346
|
+
{
|
|
347
|
+
logger.time('SOG encoding means');
|
|
348
|
+
const xCol = table[ColIdx.x];
|
|
349
|
+
const yCol = table[ColIdx.y];
|
|
350
|
+
const zCol = table[ColIdx.z];
|
|
351
|
+
// calculate minmax & transform
|
|
352
|
+
let minX = Infinity;
|
|
353
|
+
let minY = Infinity;
|
|
354
|
+
let minZ = Infinity;
|
|
355
|
+
let maxX = -Infinity;
|
|
356
|
+
let maxY = -Infinity;
|
|
357
|
+
let maxZ = -Infinity;
|
|
358
|
+
for (let i = 0; i < counts; i++) {
|
|
359
|
+
const idx = indices[i];
|
|
360
|
+
const x = xCol[idx];
|
|
361
|
+
const y = yCol[idx];
|
|
362
|
+
const z = zCol[idx];
|
|
363
|
+
if (x < minX) {
|
|
364
|
+
minX = x;
|
|
365
|
+
}
|
|
366
|
+
if (x > maxX) {
|
|
367
|
+
maxX = x;
|
|
368
|
+
}
|
|
369
|
+
if (y < minY) {
|
|
370
|
+
minY = y;
|
|
371
|
+
}
|
|
372
|
+
if (y > maxY) {
|
|
373
|
+
maxY = y;
|
|
374
|
+
}
|
|
375
|
+
if (z < minZ) {
|
|
376
|
+
minZ = z;
|
|
377
|
+
}
|
|
378
|
+
if (z > maxZ) {
|
|
379
|
+
maxZ = z;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
minX = logTransform(minX);
|
|
383
|
+
minY = logTransform(minY);
|
|
384
|
+
minZ = logTransform(minZ);
|
|
385
|
+
maxX = logTransform(maxX);
|
|
386
|
+
maxY = logTransform(maxY);
|
|
387
|
+
maxZ = logTransform(maxZ);
|
|
388
|
+
const scaleX = 65535 / Math.max(maxX - minX, 1e-9);
|
|
389
|
+
const scaleY = 65535 / Math.max(maxY - minY, 1e-9);
|
|
390
|
+
const scaleZ = 65535 / Math.max(maxZ - minZ, 1e-9);
|
|
391
|
+
// encode means
|
|
392
|
+
const meansL = new Uint8Array(width * height * channels).fill(0xff);
|
|
393
|
+
const meansU = new Uint8Array(width * height * channels).fill(0xff);
|
|
394
|
+
for (let i = 0; i < indices.length; i++) {
|
|
395
|
+
const idx = indices[i];
|
|
396
|
+
const x = (logTransform(xCol[idx]) - minX) * scaleX;
|
|
397
|
+
const y = (logTransform(yCol[idx]) - minY) * scaleY;
|
|
398
|
+
const z = (logTransform(zCol[idx]) - minZ) * scaleZ;
|
|
399
|
+
meansL[i * 4 + 0] = x & 0xff;
|
|
400
|
+
meansL[i * 4 + 1] = y & 0xff;
|
|
401
|
+
meansL[i * 4 + 2] = z & 0xff;
|
|
402
|
+
meansU[i * 4 + 0] = (x >> 8) & 0xff;
|
|
403
|
+
meansU[i * 4 + 1] = (y >> 8) & 0xff;
|
|
404
|
+
meansU[i * 4 + 2] = (z >> 8) & 0xff;
|
|
405
|
+
}
|
|
406
|
+
output['means_l.webp'] = encodeWebP(meansL, width, height, webPProfile);
|
|
407
|
+
output['means_u.webp'] = encodeWebP(meansU, width, height, webPProfile);
|
|
408
|
+
meta.means.mins = [minX, minY, minZ];
|
|
409
|
+
meta.means.maxs = [maxX, maxY, maxZ];
|
|
410
|
+
logger.timeEnd('SOG encoding means');
|
|
411
|
+
}
|
|
412
|
+
// quaternions
|
|
413
|
+
{
|
|
414
|
+
logger.time('SOG encoding quaternions');
|
|
415
|
+
const quats = new Uint8Array(width * height * channels);
|
|
416
|
+
const q = [0, 0, 0, 0];
|
|
417
|
+
for (let i = 0; i < indices.length; ++i) {
|
|
418
|
+
data.getQuat(indices[i], single);
|
|
419
|
+
q[0] = single.qw;
|
|
420
|
+
q[1] = single.qx;
|
|
421
|
+
q[2] = single.qy;
|
|
422
|
+
q[3] = single.qz;
|
|
423
|
+
const l = Math.sqrt(q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]);
|
|
424
|
+
// normalize
|
|
425
|
+
q.forEach((v, j) => {
|
|
426
|
+
q[j] = v / l;
|
|
427
|
+
});
|
|
428
|
+
// find max component
|
|
429
|
+
const maxComp = q.reduce((v, _, i) => (Math.abs(q[i]) > Math.abs(q[v]) ? i : v), 0);
|
|
430
|
+
// invert if max component is negative
|
|
431
|
+
if (q[maxComp] < 0) {
|
|
432
|
+
q.forEach((_, j) => {
|
|
433
|
+
q[j] *= -1;
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
// scale by sqrt(2) to fit in [-1, 1] range
|
|
437
|
+
q.forEach((_, j) => (q[j] *= Math.SQRT2));
|
|
438
|
+
const idx = [
|
|
439
|
+
[1, 2, 3],
|
|
440
|
+
[0, 2, 3],
|
|
441
|
+
[0, 1, 3],
|
|
442
|
+
[0, 1, 2],
|
|
443
|
+
][maxComp];
|
|
444
|
+
quats[i * 4] = (q[idx[0]] * 0.5 + 0.5) * 255;
|
|
445
|
+
quats[i * 4 + 1] = (q[idx[1]] * 0.5 + 0.5) * 255;
|
|
446
|
+
quats[i * 4 + 2] = (q[idx[2]] * 0.5 + 0.5) * 255;
|
|
447
|
+
quats[i * 4 + 3] = 252 + maxComp;
|
|
448
|
+
}
|
|
449
|
+
output['quats.webp'] = encodeWebP(quats, width, height, webPProfile);
|
|
450
|
+
logger.timeEnd('SOG encoding quaternions');
|
|
451
|
+
}
|
|
452
|
+
// scales
|
|
453
|
+
{
|
|
454
|
+
logger.time('SOG encoding scales');
|
|
455
|
+
const scaleData = quantize1d([table[ColIdx.sx], table[ColIdx.sy], table[ColIdx.sz]], undefined, undefined, Math.log);
|
|
456
|
+
const tableData = writeTableData(scaleData.labels, indices, width, height, channels);
|
|
457
|
+
output['scales.webp'] = encodeWebP(tableData, width, height, webPProfile);
|
|
458
|
+
meta.scales.codebook = Array.from(scaleData.centroids);
|
|
459
|
+
logger.timeEnd('SOG encoding scales');
|
|
460
|
+
}
|
|
461
|
+
// colors
|
|
462
|
+
{
|
|
463
|
+
logger.time('SOG encoding colors');
|
|
464
|
+
const colorData = quantize1d([table[ColIdx.r], table[ColIdx.g], table[ColIdx.b]], undefined, undefined, v => (v - 0.5) / SH_C0);
|
|
465
|
+
const aCol = table[ColIdx.a];
|
|
466
|
+
const opacityData = new Uint8Array(aCol.length);
|
|
467
|
+
for (let i = 0; i < counts; ++i) {
|
|
468
|
+
opacityData[i] = clamp(aCol[i] * 255, 0, 255);
|
|
469
|
+
}
|
|
470
|
+
colorData.labels.push(opacityData);
|
|
471
|
+
const tableData = writeTableData(colorData.labels, indices, width, height, channels);
|
|
472
|
+
output['sh0.webp'] = encodeWebP(tableData, width, height, webPProfile);
|
|
473
|
+
meta.sh0.codebook = Array.from(colorData.centroids);
|
|
474
|
+
logger.timeEnd('SOG encoding colors');
|
|
475
|
+
}
|
|
476
|
+
// SH
|
|
477
|
+
if (shDegree > 0) {
|
|
478
|
+
logger.time(`SOG encoding SH${shDegree}`);
|
|
479
|
+
const shCoeffs = shCounts / 3;
|
|
480
|
+
const shDataTable = [];
|
|
481
|
+
for (const i of buildSHTableMap(shCoeffs)) {
|
|
482
|
+
shDataTable.push(table[ColIdx.shOffset + i]);
|
|
483
|
+
}
|
|
484
|
+
const paletteSize = Math.min(64, 2 ** Math.floor(Math.log2(indices.length / 1024))) * 1024;
|
|
485
|
+
const device = await getOrCreateDevice();
|
|
486
|
+
logger.info(`SOG SH${shDegree} k-means with clusters=${paletteSize} iterations=${this.iterations}`);
|
|
487
|
+
logger.time(`SOG SH${shDegree} k-means`);
|
|
488
|
+
const { centroids, labels } = await kmeans(shDataTable, paletteSize, this.iterations, device);
|
|
489
|
+
logger.timeEnd(`SOG SH${shDegree} k-means`);
|
|
490
|
+
const codebook = quantize1d(centroids);
|
|
491
|
+
// write centroids
|
|
492
|
+
const centroidsBuf = new Uint8Array(64 * shCoeffs * Math.ceil(centroids[0].length / 64) * channels).fill(0xff);
|
|
493
|
+
const centroidsRow = [];
|
|
494
|
+
for (let i = 0; i < centroids[0].length; ++i) {
|
|
495
|
+
codebook.labels.forEach((column, index) => {
|
|
496
|
+
centroidsRow[index] = column[i];
|
|
497
|
+
});
|
|
498
|
+
for (let j = 0; j < shCoeffs; ++j) {
|
|
499
|
+
centroidsBuf[i * shCoeffs * 4 + j * 4 + 0] = centroidsRow[shCoeffs * 0 + j];
|
|
500
|
+
centroidsBuf[i * shCoeffs * 4 + j * 4 + 1] = centroidsRow[shCoeffs * 1 + j];
|
|
501
|
+
centroidsBuf[i * shCoeffs * 4 + j * 4 + 2] = centroidsRow[shCoeffs * 2 + j];
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
output['shN_centroids.webp'] = encodeWebP(centroidsBuf, 64 * shCoeffs, Math.ceil(centroids[0].length / 64), webPProfile);
|
|
505
|
+
// write labels
|
|
506
|
+
const labelsBuf = new Uint8Array(width * height * channels).fill(0xff);
|
|
507
|
+
for (let i = 0; i < indices.length; ++i) {
|
|
508
|
+
const label = labels[indices[i]];
|
|
509
|
+
labelsBuf[i * 4 + 0] = label & 0xff;
|
|
510
|
+
labelsBuf[i * 4 + 1] = (label >> 8) & 0xff;
|
|
511
|
+
}
|
|
512
|
+
output['shN_labels.webp'] = encodeWebP(labelsBuf, width, height, webPProfile);
|
|
513
|
+
meta.shN = {
|
|
514
|
+
count: paletteSize,
|
|
515
|
+
bands: shDegree,
|
|
516
|
+
codebook: Array.from(codebook.centroids),
|
|
517
|
+
files: ['shN_centroids.webp', 'shN_labels.webp'],
|
|
518
|
+
};
|
|
519
|
+
logger.timeEnd(`SOG encoding SH${shDegree}`);
|
|
520
|
+
}
|
|
521
|
+
output['meta.json'] = Buffer.from(JSON.stringify(meta), 'utf-8');
|
|
522
|
+
const result = zipSync(output);
|
|
523
|
+
await stream.getWriter().write(result);
|
|
524
|
+
}
|
|
525
|
+
}
|