1
+ import readImageDICOMFileSeries from 'itk/readImageDICOMFileSeries'
2
+ import PromiseFileReader from 'promise-file-reader'
1
3
import curry from 'curry'
2
4
import dicomParser from 'dicom-parser'
5
+ import "regenerator-runtime/runtime" ;
3
6
4
- class DICOMStore {
5
- constructor ( ) {
6
- this . patientDict = new Map ( )
7
- this . fileLoaded = this . fileLoaded . bind ( this )
8
- }
7
+ const parseDICOMFiles = async ( fileList ) => {
8
+ var patientDict = new Map ( )
9
+
10
+ const parseFile = async ( file ) => {
11
+ // Read
12
+ const arrayBuffer = await PromiseFileReader . readAsArrayBuffer ( file )
13
+
14
+ // Parse
15
+ const byteArray = new Uint8Array ( arrayBuffer )
16
+ const dicomMetaData = dicomParser . parseDicom ( byteArray )
9
17
10
- fileLoaded ( event ) {
11
- var arrayBuffer = event . target . result
12
- var byteArray = new Uint8Array ( arrayBuffer )
13
- var dicomMetaData = dicomParser . parseDicom ( byteArray )
14
- var patientId = dicomMetaData . string ( 'x00100020' )
15
- var patient = this . patientDict . get ( patientId )
18
+ // Add to patientDict
19
+ const patientId = dicomMetaData . string ( 'x00100020' )
20
+ var patient = patientDict . get ( patientId )
16
21
if ( patient === undefined ) {
17
22
console . log ( `New patient ${ patientId } ` )
18
23
patient = new DICOMPatient ( )
19
- this . patientDict . set ( patientId , patient )
24
+ patientDict . set ( patientId , patient )
20
25
}
21
- patient . parseMetaData ( dicomMetaData )
26
+ patient . parseMetaData ( dicomMetaData , file )
22
27
}
23
28
24
- readFile ( file ) {
25
- var reader = new FileReader ( )
26
- reader . onload = this . fileLoaded
27
- reader . readAsArrayBuffer ( file )
28
- }
29
+ // Parse all files and populate patientDict
30
+ const parseFiles = [ ...fileList ] . map ( parseFile )
31
+ await Promise . all ( parseFiles )
32
+ return patientDict
29
33
}
30
34
31
35
class DICOMPatient {
@@ -37,7 +41,7 @@ class DICOMPatient {
37
41
this . studyDict = new Map ( )
38
42
}
39
43
40
- parseMetaData ( dicomMetaData ) {
44
+ parseMetaData ( dicomMetaData , file ) {
41
45
const id = dicomMetaData . string ( 'x00100020' )
42
46
if ( this . id === undefined ) {
43
47
this . id = id
@@ -73,7 +77,7 @@ class DICOMPatient {
73
77
study = new DICOMStudy ( )
74
78
this . studyDict . set ( studyId , study )
75
79
}
76
- study . parseMetaData ( dicomMetaData )
80
+ study . parseMetaData ( dicomMetaData , file )
77
81
}
78
82
}
79
83
@@ -88,7 +92,7 @@ class DICOMStudy {
88
92
this . serieDict = new Map ( )
89
93
}
90
94
91
- parseMetaData ( dicomMetaData ) {
95
+ parseMetaData ( dicomMetaData , file ) {
92
96
const id = dicomMetaData . string ( 'x00200010' )
93
97
if ( this . id === undefined ) {
94
98
this . id = id
@@ -138,7 +142,7 @@ class DICOMStudy {
138
142
serie = new DICOMSerie ( )
139
143
this . serieDict . set ( serieNumber , serie )
140
144
}
141
- serie . parseMetaData ( dicomMetaData )
145
+ serie . parseMetaData ( dicomMetaData , file )
142
146
}
143
147
}
144
148
@@ -152,10 +156,10 @@ class DICOMSerie {
152
156
this . description = undefined // x0008103e
153
157
this . protocolName = undefined // x00181030
154
158
this . bodyPart = undefined // x00180015
155
- this . imageDict = new Map ( )
159
+ this . files = [ ]
156
160
}
157
161
158
- parseMetaData ( dicomMetaData ) {
162
+ parseMetaData ( dicomMetaData , file ) {
159
163
const number = dicomMetaData . string ( 'x00200011' )
160
164
if ( this . number === undefined ) {
161
165
this . number = number
@@ -212,28 +216,38 @@ class DICOMSerie {
212
216
console . assert ( this . protocolName === protocolName , "Inconsistent protocol name" )
213
217
}
214
218
215
- var imageNumber = dicomMetaData . string ( 'x00200013' )
216
- if ( this . imageDict . has ( imageNumber ) ) {
217
- console . warn ( `Instance #${ imageNumber } was already added to the serie` )
218
- } else {
219
- // TODO
220
- }
219
+ this . files . push ( file )
221
220
}
222
221
}
223
222
224
-
225
- var store = new DICOMStore ( )
226
-
227
- const outputFileInformation = curry ( function outputFileInformation ( outputTextArea , event ) {
223
+ const outputFileInformation = curry ( async function outputFileInformation ( outputTextArea , event ) {
228
224
outputTextArea . textContent = "Loading..."
229
225
226
+ // Get files
230
227
const dataTransfer = event . dataTransfer
231
228
const files = event . target . files || dataTransfer . files
232
229
233
- Array . from ( files ) . forEach ( file => {
234
- store . readFile ( file )
235
- } )
236
- console . log ( store )
230
+ // Parse DICOM metadata
231
+ const patientDict = await parseDICOMFiles ( files )
232
+
233
+ // Select DICOM serie
234
+ const patient = patientDict . values ( ) . next ( ) . value
235
+ const study = patient . studyDict . values ( ) . next ( ) . value
236
+ const serie = study . serieDict . values ( ) . next ( ) . value
237
+
238
+ // Read DICOM serie
239
+ const { image, webWorker } = await readImageDICOMFileSeries ( null , serie . files )
240
+ console . log ( image )
241
+ webWorker . terminate ( )
242
+
243
+ // Display
244
+ function replacer ( key , value ) {
245
+ if ( ! ! value && value . byteLength !== undefined ) {
246
+ return String ( value . slice ( 0 , 6 ) ) + '...'
247
+ }
248
+ return value
249
+ }
250
+ outputTextArea . textContent = JSON . stringify ( image , replacer , 4 )
237
251
} )
238
252
239
253
export { outputFileInformation }
0 commit comments