-
Notifications
You must be signed in to change notification settings - Fork 0
/
zfp.js
62 lines (44 loc) · 1.92 KB
/
zfp.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
function compress2d(module, input, nx, ny, tolerance) {
if (input.length != nx * ny) return null;
const inputPtr = module._malloc(input.length * Float32Array.BYTES_PER_ELEMENT);
module.HEAPF32.set(input, inputPtr / Float32Array.BYTES_PER_ELEMENT);
const sizePtr = module._malloc(Uint32Array.BYTES_PER_ELEMENT);
const statusPtr = module._malloc(Uint32Array.BYTES_PER_ELEMENT);
const outputPtr = module.ccall(
'compress_2d',
'number',
['number', 'number', 'number', 'number', 'number', 'number'],
[inputPtr, nx, ny, tolerance, sizePtr, statusPtr]
);
module._free(inputPtr);
const status = module.HEAPU32[statusPtr / Uint32Array.BYTES_PER_ELEMENT];
module._free(statusPtr);
const size = module.HEAPU32[sizePtr / Uint32Array.BYTES_PER_ELEMENT];
module._free(sizePtr);
if (status != 0) return null;
const output = new Uint8Array(size);
const outputOffset = outputPtr / Uint8Array.BYTES_PER_ELEMENT;
for (let i = 0; i < size; i += 1) output[i] = module.HEAPU8[outputOffset + i];
module._free(outputPtr);
return output;
}
function decompress2d(module, input, nx, ny, tolerance) {
const inputPtr = module._malloc(input.length * Uint8Array.BYTES_PER_ELEMENT);
module.HEAPU8.set(input, inputPtr / Uint8Array.BYTES_PER_ELEMENT);
const statusPtr = module._malloc(Uint32Array.BYTES_PER_ELEMENT);
const outputPtr = module.ccall(
'decompress_2d',
'number',
['number', 'number', 'number', 'number', 'number', 'number'],
[inputPtr, nx, ny, tolerance, input.length, statusPtr]
);
module._free(inputPtr);
const status = module.HEAPU32[statusPtr / Uint32Array.BYTES_PER_ELEMENT];
module._free(statusPtr);
if (status != 0) return null;
const output = new Float32Array(nx * ny);
const outputOffset = outputPtr / Float32Array.BYTES_PER_ELEMENT;
for (let i = 0, n = nx * ny; i < n; i += 1) output[i] = module.HEAPF32[outputOffset + i];
module._free(outputPtr);
return output;
}