| 1 |
<!DOCTYPE html> |
| 2 |
<html lang="en"> |
| 3 |
|
| 4 |
<head> |
| 5 |
<meta charset="UTF-8"> |
| 6 |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 7 |
<title>dcm2niix WASM Demo</title> |
| 8 |
</head> |
| 9 |
|
| 10 |
<body> |
| 11 |
<h1>dcm2niix WASM Demo</h1> |
| 12 |
<input type="file" id="fileInput" webkitdirectory multiple> |
| 13 |
<button id="processButton">Process Image</button> |
| 14 |
<div id="dropTarget" style="margin: 2rem; color: white; width: 100px; height: 100px; background-color: red;"> |
| 15 |
Drop files here |
| 16 |
</div> |
| 17 |
<p id="status">Please select a dicom folder to process.</p> |
| 18 |
<a id="downloadLink" style="display: none;">Download Processed Image(s)</a> |
| 19 |
|
| 20 |
<script type="module"> |
| 21 |
// import { Dcm2niix } from './dist/index.js'; |
| 22 |
// use below line for jpegls version |
| 23 |
import { Dcm2niix } from './dist/index.jpeg.js'; |
| 24 |
|
| 25 |
const fileInput = document.getElementById('fileInput'); |
| 26 |
const processButton = document.getElementById('processButton'); |
| 27 |
const status = document.getElementById('status'); |
| 28 |
const downloadLink = document.getElementById('downloadLink'); |
| 29 |
|
| 30 |
const dropTarget = document.getElementById('dropTarget'); |
| 31 |
dropTarget.addEventListener('dragover', (event) => { |
| 32 |
event.preventDefault(); |
| 33 |
dropTarget.style.backgroundColor = 'green'; |
| 34 |
}); |
| 35 |
|
| 36 |
dropTarget.addEventListener('dragleave', (event) => { |
| 37 |
event.preventDefault(); |
| 38 |
dropTarget.style.backgroundColor = 'red'; |
| 39 |
}); |
| 40 |
|
| 41 |
dropTarget.ondrop = handleDrop; |
| 42 |
|
| 43 |
async function handleDrop(e) { |
| 44 |
e.preventDefault(); // prevent navigation to open file |
| 45 |
const items = e.dataTransfer.items; |
| 46 |
const files = []; |
| 47 |
for (let i = 0; i < items.length; i++) { |
| 48 |
const item = items[i].webkitGetAsEntry(); |
| 49 |
if (item) { |
| 50 |
await traverseFileTree(item, '', files); |
| 51 |
} |
| 52 |
} |
| 53 |
const dcm2niix = new Dcm2niix(); |
| 54 |
await dcm2niix.init() |
| 55 |
const resultFileList = await dcm2niix.inputFromDropItems(files).v().run() |
| 56 |
|
| 57 |
resultFileList.forEach((resultFile, index) => { |
| 58 |
let url = URL.createObjectURL(resultFile); |
| 59 |
const downloadLink = document.createElement('a'); |
| 60 |
downloadLink.href = url; |
| 61 |
downloadLink.download = resultFile.name; |
| 62 |
downloadLink.textContent = `Download ${resultFile.name}`; |
| 63 |
downloadLink.style.display = 'block'; |
| 64 |
document.body.appendChild(downloadLink); |
| 65 |
}); |
| 66 |
status.textContent = 'Processing complete!'; |
| 67 |
} |
| 68 |
|
| 69 |
async function traverseFileTree(item, path = '', fileArray) { |
| 70 |
return new Promise((resolve) => { |
| 71 |
if (item.isFile) { |
| 72 |
item.file(file => { |
| 73 |
file.fullPath = path + file.name; |
| 74 |
// IMPORTANT: _webkitRelativePath is required for dcm2niix to work. |
| 75 |
// We need to add this property so we can parse multiple directories correctly. |
| 76 |
// the "webkitRelativePath" property on File objects is read-only, so we can't set it directly, hence the underscore. |
| 77 |
file._webkitRelativePath = path + file.name; |
| 78 |
fileArray.push(file); |
| 79 |
resolve(); |
| 80 |
}); |
| 81 |
} else if (item.isDirectory) { |
| 82 |
const dirReader = item.createReader(); |
| 83 |
const readAllEntries = () => { |
| 84 |
dirReader.readEntries(entries => { |
| 85 |
if (entries.length > 0) { |
| 86 |
const promises = []; |
| 87 |
for (const entry of entries) { |
| 88 |
promises.push(traverseFileTree(entry, path + item.name + '/', fileArray)); |
| 89 |
} |
| 90 |
Promise.all(promises).then(readAllEntries); |
| 91 |
} else { |
| 92 |
resolve(); |
| 93 |
} |
| 94 |
}); |
| 95 |
}; |
| 96 |
readAllEntries(); |
| 97 |
} |
| 98 |
}); |
| 99 |
} |
| 100 |
|
| 101 |
let selectedFiles = null; |
| 102 |
|
| 103 |
fileInput.addEventListener('change', async (event) => { |
| 104 |
selectedFiles = event.target.files; |
| 105 |
console.log(selectedFiles); |
| 106 |
}); |
| 107 |
|
| 108 |
processButton.addEventListener('click', async () => { |
| 109 |
status.textContent = 'Processing...'; |
| 110 |
try { |
| 111 |
console.log('Initializing dcm2niix wasm...'); |
| 112 |
const dcm2niix = new Dcm2niix(); |
| 113 |
await dcm2niix.init(); |
| 114 |
console.log('dcm2niix wasm initialized.'); |
| 115 |
|
| 116 |
const t0 = performance.now(); |
| 117 |
|
| 118 |
const inputFileList = selectedFiles |
| 119 |
const resultFileList = await dcm2niix.input(inputFileList).z('y').run() |
| 120 |
console.log(resultFileList); |
| 121 |
|
| 122 |
const t1 = performance.now(); |
| 123 |
console.log("dcm2niix wasm took " + (t1 - t0) + " milliseconds.") |
| 124 |
|
| 125 |
resultFileList.forEach((resultFile, index) => { |
| 126 |
let url = URL.createObjectURL(resultFile); |
| 127 |
const downloadLink = document.createElement('a'); |
| 128 |
downloadLink.href = url; |
| 129 |
downloadLink.download = resultFile.name; |
| 130 |
downloadLink.textContent = `Download ${resultFile.name}`; |
| 131 |
downloadLink.style.display = 'block'; |
| 132 |
document.body.appendChild(downloadLink); |
| 133 |
}); |
| 134 |
|
| 135 |
|
| 136 |
status.textContent = 'Processing complete!'; |
| 137 |
} catch (error) { |
| 138 |
console.error('Processing failed:', error); |
| 139 |
status.textContent = 'Processing failed. Please check the console for details.'; |
| 140 |
} finally { |
| 141 |
processButton.disabled = false; |
| 142 |
} |
| 143 |
}); |
| 144 |
</script> |
| 145 |
</body> |
| 146 |
|
| 147 |
</html> |