-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzine-compression-server.js
130 lines (117 loc) · 4.39 KB
/
zine-compression-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import {
compressImage,
compressPointCloud,
decompressPointCloud,
compressDepth,
compressDepthQuantized,
decompressDepth,
decompressDepthQuantized,
compressByteAttribute,
decompressByteAttribute,
compressGeneric,
decompressGeneric,
} from './zine-compression-utils.js';
//
const maxDepth = 10000;
const quantization = 16;
//
export class ZineCompressionServer {
constructor() {
(async () => {
// wait for global init message to get the port
const port = await new Promise((accept, reject) => {
self.addEventListener('message', e => {
const {data} = e;
const {method, args} = data;
if (method === 'init') {
accept(args.port);
args.port.start();
}
});
});
// listen for messages on the port
port.addEventListener('message', async e => {
const {data} = e;
const {id, method, args} = data;
const _respondOk = (result, transfers) => {
port.postMessage({
id,
result,
}, transfers);
};
const _respondError = (error, transfers) => {
port.postMessage({
id,
error: error.stack,
}, transfers);
};
try {
switch (method) {
case 'compress': {
const {type, value} = args;
let compressedValue;
let transfers;
if (type === 'image') {
compressedValue = await compressImage(value);
transfers = [compressedValue];
} else if (type === 'pointCloud') {
compressedValue = await compressPointCloud(new Float32Array(value));
transfers = [compressedValue.buffer];
} else if (type === 'depthQuantized') {
compressedValue = await compressDepthQuantized(new Float32Array(value, maxDepth));
transfers = [compressedValue.buffer];
} else if (type === 'depth') {
compressedValue = await compressDepth(new Float32Array(value), quantization);
transfers = [compressedValue.buffer];
} else if (type === 'byteAttribute') {
compressedValue = await compressByteAttribute(value);
transfers = [compressedValue.buffer];
} else if (type === 'generic') {
compressedValue = await compressGeneric(value);
transfers = [compressedValue.buffer];
} else {
new Error('unknown compression type: ' + type);
}
_respondOk(compressedValue, transfers);
break;
}
case 'decompress': {
const {type, value} = args;
let decompressedValue;
let transfers = [];
if (type === 'pointCloud') {
decompressedValue = await decompressPointCloud(value);
decompressedValue = decompressedValue.buffer;
transfers = [decompressedValue];
} else if (type === 'depthQuantized') {
decompressedValue = await decompressDepthQuantized(value);
transfers = [decompressedValue.buffer];
} else if (type === 'depth') {
decompressedValue = await decompressDepth(value);
transfers = [decompressedValue.buffer];
} else if (type === 'byteAttribute') {
decompressedValue = await decompressByteAttribute(value);
transfers = [decompressedValue.buffer];
} else if (type === 'generic') {
decompressedValue = await decompressGeneric(value);
transfers = [decompressedValue.buffer];
} else {
throw new Error('unknown compression type: ' + type);
}
_respondOk(decompressedValue, transfers);
break;
}
default: {
throw new Error('unknown method: ' + method);
}
}
} catch(err) {
_respondError(err);
}
});
})();
}
}
if (typeof self !== 'undefined' && self.constructor.name === 'DedicatedWorkerGlobalScope') {
new ZineCompressionServer();
}