|
| 1 | +cornerstoneNIFTIImageLoader.external.cornerstone = cornerstone; |
| 2 | +const ImageId = cornerstoneNIFTIImageLoader.nifti.ImageId; |
| 3 | +cornerstoneNIFTIImageLoader.nifti.streamingMode = true; |
| 4 | +const niftiReader = cornerstoneNIFTIImageLoader.external.niftiReader; |
| 5 | + |
| 6 | +let loaded = false; |
| 7 | +const synchronizer = new cornerstoneTools.Synchronizer("cornerstonenewimage", cornerstoneTools.updateImageSynchronizer); |
| 8 | + |
| 9 | +let voxel3dUnits = [0, 0, 0]; |
| 10 | +let dims = [0, 0, 0, 0]; |
| 11 | +let niftiImageBuffer = []; |
| 12 | + |
| 13 | +// Helper to calculate voxel position based on view |
| 14 | +function updateVoxelCoordinates(view, voxelCoords) { |
| 15 | + if (view === 'axial') { |
| 16 | + voxel3dUnits[0] = voxelCoords.x; |
| 17 | + voxel3dUnits[1] = voxelCoords.y; |
| 18 | + } else if (view === 'sagittal') { |
| 19 | + voxel3dUnits[1] = voxelCoords.x; |
| 20 | + voxel3dUnits[2] = voxelCoords.y; |
| 21 | + } else if (view === 'coronal') { |
| 22 | + voxel3dUnits[0] = voxelCoords.x; |
| 23 | + voxel3dUnits[2] = voxelCoords.y; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// Event listener for mouse drag to update voxel coordinates |
| 28 | +function addMouseDragListener(element, view) { |
| 29 | + element.addEventListener('cornerstonetoolsmousedrag', (event) => { |
| 30 | + const voxelCoords = event.detail?.currentPoints?.image; |
| 31 | + if (voxelCoords) { |
| 32 | + updateVoxelCoordinates(view, voxelCoords); |
| 33 | + handleVoxelClick(voxel3dUnits); |
| 34 | + } |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +// Load and display NIfTI image on a specific element |
| 39 | +function loadAndViewImage(element, imageId, view) { |
| 40 | + const imageIdObject = ImageId.fromURL(imageId); |
| 41 | + element.dataset.imageId = imageIdObject.url; |
| 42 | + |
| 43 | + cornerstone.loadAndCacheImage(imageIdObject.url).then(image => { |
| 44 | + setupImageViewport(element, image); |
| 45 | + setupImageTools(element, imageIdObject); |
| 46 | + |
| 47 | + synchronizer.add(element); |
| 48 | + addMouseDragListener(element, view); |
| 49 | + |
| 50 | + element.addEventListener('cornerstonestackscroll', (event) => updateSliceIndex(view, event.detail.newImageIdIndex)); |
| 51 | + }).catch(err => { |
| 52 | + console.error(`Error loading image for ${view} view:`, err); |
| 53 | + }); |
| 54 | + element.addEventListener('click', function (event) { |
| 55 | + //TODO: Update the clicked voxel information. |
| 56 | + console.log(event) |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +// Setup viewport and display the image |
| 61 | +function setupImageViewport(element, image) { |
| 62 | + const viewport = cornerstone.getDefaultViewportForImage(element, image); |
| 63 | + cornerstone.displayImage(element, image, viewport); |
| 64 | + cornerstone.resize(element, true); |
| 65 | +} |
| 66 | + |
| 67 | +// Enable tools and interactions for the displayed image |
| 68 | +function setupImageTools(element, imageIdObject) { |
| 69 | + const numberOfSlices = cornerstone.metaData.get('multiFrameModule', imageIdObject.url).numberOfFrames; |
| 70 | + const stack = { |
| 71 | + currentImageIdIndex: imageIdObject.slice.index, |
| 72 | + imageIds: Array.from({ length: numberOfSlices }, (_, i) => `nifti:${imageIdObject.filePath}#${imageIdObject.slice.dimension}-${i},t-0`) |
| 73 | + }; |
| 74 | + |
| 75 | + cornerstoneTools.addStackStateManager(element, ['stack']); |
| 76 | + cornerstoneTools.addToolState(element, 'stack', stack); |
| 77 | + cornerstoneTools.mouseInput.enable(element); |
| 78 | + cornerstoneTools.mouseWheelInput.enable(element); |
| 79 | + cornerstoneTools.pan.activate(element, 2); |
| 80 | + cornerstoneTools.stackScrollWheel.activate(element); |
| 81 | + cornerstoneTools.orientationMarkers.enable(element); |
| 82 | + cornerstoneTools.stackPrefetch.enable(element); |
| 83 | + cornerstoneTools.referenceLines.tool.enable(element, synchronizer); |
| 84 | + cornerstoneTools.crosshairs.enable(element, 1, synchronizer); |
| 85 | +} |
| 86 | + |
| 87 | +// Handle voxel click event |
| 88 | +function handleVoxelClick(currentVoxel) { |
| 89 | + const [nx, ny, nz, nt] = dims; |
| 90 | + let [voxelX, voxelY, voxelZ] = currentVoxel; |
| 91 | + |
| 92 | + voxelX = Math.min(Math.max(Math.round(voxelX), 1), nx) - 1; |
| 93 | + voxelY = Math.min(Math.max(ny - Math.round(voxelY), 1), ny) - 1; |
| 94 | + voxelZ = Math.min(Math.max(nz - Math.round(voxelZ), 1), nz) - 1; |
| 95 | + |
| 96 | + const voxelValues = getVoxelValuesAcrossTime(voxelX, voxelY, voxelZ, nx, ny, nz, nt); |
| 97 | + updateVoxelCoordinatesDisplay(voxelX + 1, voxelY + 1, voxelZ + 1); |
| 98 | + plotVoxelData(voxelValues); |
| 99 | +} |
| 100 | + |
| 101 | +// Extract voxel values across all time points |
| 102 | +function getVoxelValuesAcrossTime(x, y, z, nx, ny, nz, nt) { |
| 103 | + const sliceSize = nx * ny; |
| 104 | + const volumeSize = sliceSize * nz; |
| 105 | + let voxelValues = []; |
| 106 | + |
| 107 | + for (let t = 0; t < nt; t++) { |
| 108 | + const voxelIndex = x + y * nx + z * nx * ny + t * volumeSize; |
| 109 | + voxelValues.push(niftiImageBuffer[voxelIndex]); |
| 110 | + } |
| 111 | + |
| 112 | + return voxelValues; |
| 113 | +} |
| 114 | + |
| 115 | +// Update voxel coordinates display on the page |
| 116 | +function updateVoxelCoordinatesDisplay(x, y, z) { |
| 117 | + document.getElementById('voxel-coordinates').innerText = `(x, y, z): (${x}, ${y}, ${z})`; |
| 118 | +} |
| 119 | + |
| 120 | +// Update the slice index based on the current view |
| 121 | +function updateSliceIndex(view, newIndex) { |
| 122 | + if (view === 'axial') { |
| 123 | + voxel3dUnits[2] = newIndex; |
| 124 | + } else if (view === 'sagittal') { |
| 125 | + voxel3dUnits[0] = newIndex; |
| 126 | + } else if (view === 'coronal') { |
| 127 | + voxel3dUnits[1] = newIndex; |
| 128 | + } |
| 129 | + handleVoxelClick(voxel3dUnits); |
| 130 | +} |
| 131 | + |
| 132 | +// Load NIfTI file and display the axial, sagittal, and coronal views |
| 133 | +function loadAllFileViews(file) { |
| 134 | + const fileURL = URL.createObjectURL(file); |
| 135 | + const imageId = `nifti:${fileURL}`; |
| 136 | + |
| 137 | + cornerstoneNIFTIImageLoader.nifti.loadHeader(imageId).then((header) => { |
| 138 | + dims = [...header.voxelLength, header.timeSlices]; |
| 139 | + loadAndViewImage(document.getElementById('nifti-image-z'), `${imageId}#z,t-0`, 'axial'); |
| 140 | + loadAndViewImage(document.getElementById('nifti-image-x'), `${imageId}#x,t-0`, 'sagittal'); |
| 141 | + loadAndViewImage(document.getElementById('nifti-image-y'), `${imageId}#y,t-0`, 'coronal'); |
| 142 | + }); |
| 143 | + |
| 144 | +} |
| 145 | + |
| 146 | + |
| 147 | +// Plot voxel data using Plotly |
| 148 | +function plotVoxelData(values) { |
| 149 | + const trace = { |
| 150 | + y: values, |
| 151 | + mode: 'markers', |
| 152 | + type: 'bar', |
| 153 | + }; |
| 154 | + const layout = { |
| 155 | + title: 'Voxel Intensity Under the Cursor', |
| 156 | + xaxis: { title: 'Time Point' }, |
| 157 | + yaxis: { title: 'Intensity' }, |
| 158 | + }; |
| 159 | + Plotly.newPlot('plot', [trace], layout); |
| 160 | +} |
| 161 | + |
| 162 | +// Initialize file upload and view |
| 163 | +document.getElementById('upload-and-view').addEventListener('click', () => { |
| 164 | + const file = document.getElementById('nifti-file').files[0]; |
| 165 | + if (file) { |
| 166 | + getNiftiArrayBuffer(file); |
| 167 | + loadAllFileViews(file); |
| 168 | + } else { |
| 169 | + alert("Please select a NIFTI file to upload."); |
| 170 | + } |
| 171 | +}); |
| 172 | + |
| 173 | +// Enable cornerstone for the viewports |
| 174 | +cornerstone.enable(document.getElementById('nifti-image-z')); |
| 175 | +cornerstone.enable(document.getElementById('nifti-image-x')); |
| 176 | +cornerstone.enable(document.getElementById('nifti-image-y')); |
| 177 | + |
| 178 | +// Fetch NIfTI file data as ArrayBuffer |
| 179 | +async function getNiftiArrayBuffer(file) { |
| 180 | + const data = await loadNiftiFile(file); |
| 181 | + if (!data) return console.error('Failed to load NIfTI file'); |
| 182 | + |
| 183 | + try { |
| 184 | + const header = niftiReader.readHeader(data); |
| 185 | + niftiImageBuffer = createTypedArray(header, niftiReader.readImage(header, data)); |
| 186 | + } catch (error) { |
| 187 | + console.error('Error processing file:', error); |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +// Create typed array based on NIfTI data type |
| 192 | +// Create a mapping between datatype codes and typed array constructors |
| 193 | +const typedArrayConstructorMap = { |
| 194 | + [niftiReader.NIFTI1.TYPE_UINT8]: Uint8Array, |
| 195 | + [niftiReader.NIFTI1.TYPE_UINT16]: Uint16Array, |
| 196 | + [niftiReader.NIFTI1.TYPE_UINT32]: Uint32Array, |
| 197 | + [niftiReader.NIFTI1.TYPE_INT8]: Int8Array, |
| 198 | + [niftiReader.NIFTI1.TYPE_INT16]: Int16Array, |
| 199 | + [niftiReader.NIFTI1.TYPE_INT32]: Int32Array, |
| 200 | + [niftiReader.NIFTI1.TYPE_FLOAT32]: Float32Array, |
| 201 | + [niftiReader.NIFTI1.TYPE_FLOAT64]: Float64Array, |
| 202 | + [niftiReader.NIFTI1.TYPE_RGB]: Uint8Array, |
| 203 | + [niftiReader.NIFTI1.TYPE_RGBA]: Uint8Array |
| 204 | +}; |
| 205 | + |
| 206 | +// Create typed array based on NIfTI data type code |
| 207 | +function createTypedArray(header, imageBuffer) { |
| 208 | + const TypedArrayConstructor = typedArrayConstructorMap[header.datatypeCode]; |
| 209 | + |
| 210 | + if (TypedArrayConstructor) { |
| 211 | + return new TypedArrayConstructor(imageBuffer); |
| 212 | + } else { |
| 213 | + console.error('Unsupported datatype:', header.datatypeCode); |
| 214 | + return null; |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +// Load the NIfTI file as an ArrayBuffer |
| 219 | +async function loadNiftiFile(file) { |
| 220 | + return new Promise((resolve, reject) => { |
| 221 | + const reader = new FileReader(); |
| 222 | + reader.onload = (event) => { |
| 223 | + const arrayBuffer = event.target.result; |
| 224 | + const data = niftiReader.isCompressed(arrayBuffer) |
| 225 | + ? niftiReader.decompress(arrayBuffer) |
| 226 | + : arrayBuffer; |
| 227 | + resolve(data); |
| 228 | + }; |
| 229 | + reader.onerror = (error) => reject(error); |
| 230 | + reader.readAsArrayBuffer(file); |
| 231 | + }); |
| 232 | +} |
0 commit comments