-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
107 lines (91 loc) · 3.46 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Caesium WASM Demo</title>
<style>
body {
font-family: 'sans-serif';
}
.container {
width: 100%;
max-width: 1200px;
margin: auto;
}
.image-previews {
display: flex;
justify-content: space-between;
}
.image-container {
width: 49%;
}
.image {
width: 100%;
}
</style>
</head>
<body>
<div class="container">
<div style="margin-bottom: 10px">
<input type="file" id="fileInput">
<label for="quality">Quality</label> <input type="number" id="quality" value="80">
<span id="status" style="margin-left: 10px"></span>
</div>
<div class="image-previews">
<div class="image-container">
<p style="font-weight: bold">Original Image</p> <span id="originalSize"></span>
<img class="image" alt="original" src="" id="original">
</div>
<div class="image-container">
<p style="font-weight: bold">Compressed Image</p> <span id="compressedSize"></span>
<img class="image" alt="compressed" src="" id="result">
</div>
</div>
</div>
</body>
<script type="module">
import CaesiumWASM from "./libcaesium-wasm.js";
CaesiumWASM().then(CaesiumWASM => {
const input = document.getElementById("fileInput");
input.addEventListener("change", async () => {
document.getElementById('status').innerText = 'Compressing...';
const ab = await input.files[0].arrayBuffer();
const ui8a = new Uint8Array(ab);
document.getElementById('original').src = URL.createObjectURL(
new Blob([ui8a])
);
document.getElementById('originalSize').innerText = `${ui8a.length} bytes`;
// console.log("Uint8Array", ui8a, ui8a.length);
let ptr = CaesiumWASM._malloc(ui8a.length);
CaesiumWASM.HEAP8.set(ui8a, ptr);
const js_wrapped_compress = CaesiumWASM.cwrap("w_compress", "number", ["number", "number", "number", "number"]);
const dataPtr = js_wrapped_compress(ptr, ui8a.length, document.getElementById('quality').value, 1);
let status = CaesiumWASM.getValue(dataPtr, 'i32')
let errorCode = CaesiumWASM.getValue(dataPtr + 4, 'i32')
let vecPtr = CaesiumWASM.getValue(dataPtr + 8, 'i32');
let len = CaesiumWASM.getValue(dataPtr + 12, 'i32');
let res = {
status,
errorCode,
vecPtr,
len
}
console.log(res)
if (status === 1) {
document.getElementById('status').innerText = 'Done!';
let myArray = new Uint8Array(CaesiumWASM.HEAPU8.buffer, vecPtr, len);
document.getElementById('result').src = URL.createObjectURL(
new Blob([myArray])
);
document.getElementById('compressedSize').innerText = `${myArray.length} bytes`;
} else {
document.getElementById('status').innerText = 'ERROR :(';
}
let drop_vector_struct = CaesiumWASM.cwrap('drop_vector_struct', null, ['number']);
drop_vector_struct(dataPtr);
CaesiumWASM._free(ptr);
// CaesiumWASM._free(vecPtr);
});
})
</script>
</html>