-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
137 lines (112 loc) · 4.5 KB
/
main.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
131
132
133
134
135
136
137
import { Niivue, NVImage } from '@niivue/niivue'
import FlirtWorker from "./worker?worker"
import flirtWasm from "./flirt.wasm?url"
let referenceVolumeBuffer = null
let inputVolumeBuffer = null
let processing = false
let volumePositions = {}
function fixWasmUrl(flirtWasmUrl) {
let idDoubleSlash = flirtWasmUrl.lastIndexOf("//");
let idLastSlash = flirtWasmUrl.lastIndexOf("/");
return flirtWasmUrl.substring(0, idDoubleSlash) + "/" + flirtWasmUrl.substring(idLastSlash+1, flirtWasmUrl.length);
}
function processImage(worker, input, reference) {
processing = true;
document.getElementById("options").innerHTML = "Processing...";
let args = ["-in", "vol.nii", "-ref", "vol_ref.nii", "-out", "out/vol_reg.nii"].concat(getCLIArgumentsFromModal());
console.log(args);
worker.postMessage({files: [{name: "vol.nii", data: new Uint8Array(input)}, {name: "vol_ref.nii", data: new Uint8Array(reference)}], args: args, wasmPath: fixWasmUrl(flirtWasm)});
}
function initFlirtWorker(nv, worker) {
worker.addEventListener("message", async function(e) {
nv.setVolume(new NVImage(e.data[0].data), volumePositions["in"]);
processing = false;
document.getElementById("options").innerHTML = "Options";
});
worker.addEventListener("onerror", function(error) {
console.log(error.message);
processing = false;
document.getElementById("options").innerHTML = "Options";
});
}
async function handleFileSelection(volId, event, nv) {
const file = event.target.files[0];
if (file) {
let volume = await NVImage.loadFromFile(file);
if (volumePositions[volId] === undefined) {
volumePositions[volId] = nv.volumes.length;
nv.addVolume(volume);
} else {
nv.removeVolume(nv.volumes[volumePositions[volId]]);
nv.addVolume(volume);
nv.setVolume(volume, volumePositions[volId]);
}
if (nv.volumes.length == 2) {
document.getElementById("sliderContainer").classList.remove("hidden");
}
const reader = new FileReader();
reader.onload = function(fileEvent) {
if (volId === "ref") {
referenceVolumeBuffer = fileEvent.target.result;
} else {
inputVolumeBuffer = fileEvent.target.result;
}
};
reader.readAsArrayBuffer(file);
}
}
function getCLIArgumentsFromModal() {
const form = document.getElementById('flirtForm');
const selects = form.querySelectorAll('select');
const params = [];
for(let select of selects) {
const value = select.options[select.selectedIndex].value;
params.push("-" + select.name);
params.push(value);
}
return params;
}
window.onload = async () => {
const canvas = document.getElementById('gl');
const worker = new FlirtWorker();
const nv = new Niivue();
document.getElementById("fileSelectionInput").addEventListener("click", (e) => {
e.preventDefault();
if (processing) { return; }
fileInput.value = null;
fileInput.click();
});
document.getElementById("fileSelectionReference").addEventListener("click", (e) => {
e.preventDefault();
if (processing) { return; }
fileReference.vaue = null;
fileReference.click();
});
document.getElementById("runFlirt").addEventListener("click", (e) => {
e.preventDefault();
processImage(worker, inputVolumeBuffer);
modal.style.display = 'none';
});
fileReference.addEventListener("change", (event) => handleFileSelection("ref", event, nv));
fileInput.addEventListener("change", (event) => handleFileSelection("in", event, nv));
const modal = document.getElementById('flirtModal');
const closeModalButton = document.getElementById('closeModal');
document.getElementById("options").addEventListener("click", function(e) {
e.preventDefault();
if (processing) { return; }
modal.style.display = 'block';
});
closeModalButton.addEventListener("click", function() {
modal.style.display = 'none';
});
initFlirtWorker(nv, worker);
nv.attachToCanvas(canvas);
volumeSelector.oninput = function () {
let opacity = this.value / 100;
nv.setOpacity(volumePositions["in"], opacity);
nv.setOpacity(volumePositions["ref"], 1.0-opacity);
};
runFlirt.addEventListener("click", function(e) {
processImage(worker, inputVolumeBuffer, referenceVolumeBuffer);
});
}