-
Notifications
You must be signed in to change notification settings - Fork 115
/
ViewImage.tsx
99 lines (90 loc) · 3.27 KB
/
ViewImage.tsx
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
import React from 'react'
import { useDispatch } from 'react-redux'
import { setFilesForGallery } from '../../store/explorer/actions'
import GalleryDicomView from '../../components/dicomViewer/GalleryDicomView'
import DicomLoader from '../../components/dicomViewer/DcmLoader'
import { useTypedSelector } from '../../store/hooks'
import * as cornerstone from 'cornerstone-core'
import * as cornerstoneWADOImageLoader from 'cornerstone-wado-image-loader'
import * as cornerstoneWebImageLoader from 'cornerstone-web-image-loader'
import * as cornerstoneNIFTIImageLoader from 'cornerstone-nifti-image-loader'
import * as cornerstoneFileImageLoader from 'cornerstone-file-image-loader'
import * as dicomParser from 'dicom-parser'
import { isNifti, isDicom } from '../../components/dicomViewer/utils'
import './ViewImage.scss'
cornerstoneNIFTIImageLoader.nifti.configure({
headers: {
'Content-Type': 'application/vnd.collection+json',
Authorization: 'Token ' + window.sessionStorage.getItem('CHRIS_TOKEN'),
},
method: 'get',
responseType: 'arrayBuffer',
})
const ImageId = cornerstoneNIFTIImageLoader.nifti.ImageId
cornerstoneNIFTIImageLoader.external.cornerstone = cornerstone
cornerstoneFileImageLoader.external.cornerstone = cornerstone
cornerstoneWebImageLoader.external.cornerstone = cornerstone
cornerstoneWADOImageLoader.external.cornerstone = cornerstone
cornerstoneWADOImageLoader.external.dicomParser = dicomParser
const GalleryPage = () => {
const dispatch = useDispatch()
const files = useTypedSelector((state) => state.explorer.externalFiles)
const [loader, setLoader] = React.useState({
totalFiles: 0,
filesParsed: 0,
})
const loadImagesIntoCornerstone = React.useCallback(() => {
if (files) {
const imageIds: string[] = []
for (let i = 0; i < files.length; i++) {
const file = files[i]
if (isNifti(file.name)) {
const url = URL.createObjectURL(file).split('blob:')[1]
const imageIdObject = ImageId.fromURL(`nifti:${url}${file.name}`)
const numberOfSlices = cornerstone.metaData.get(
'multiFrameModule',
imageIdObject.url,
).numberOfFrames
imageIds.push(
...Array.from(
Array(numberOfSlices),
(_, i) =>
`nifti:${imageIdObject.filePath}#${imageIdObject.slice.dimension}-${i},t-0`,
),
)
} else if (isDicom(file.name)) {
imageIds.push(
cornerstoneWADOImageLoader.wadouri.fileManager.add(file),
)
} else {
imageIds.push(cornerstoneFileImageLoader.fileManager.add(file))
}
setLoader((state) => {
return {
...state,
filesParsed: i + 1,
totalFiles: files.length,
}
})
}
dispatch(setFilesForGallery(imageIds))
close()
}
}, [files, dispatch])
React.useEffect(() => {
loadImagesIntoCornerstone()
}, [loadImagesIntoCornerstone])
return (
<div className="gallery">
{loader.filesParsed === loader.totalFiles ? (
<GalleryDicomView type="visualization" />
) : (
<DicomLoader
totalFiles={loader.totalFiles}
filesParsed={loader.filesParsed}
/>
)}
</div>
)
}
export default GalleryPage