Skip to content

Commit b4314dc

Browse files
author
Alexis Girault
committed
Use promises to return the organized DICOM entities
Then provide files to ITK to read the DICOM.
1 parent 8ac2a66 commit b4314dc

File tree

2 files changed

+54
-39
lines changed

2 files changed

+54
-39
lines changed

examples/Dicom/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
"homepage": "https://github.com/InsightSoftwareConsortium/itk-js#readme",
2424
"dependencies": {
2525
"curry": "^1.2.0",
26+
"dicom-parser": "^1.8.3",
2627
"expose-loader": "^0.7.5",
2728
"itk": "^9.5.0",
28-
"dicom-parser": "^1.8.3"
29+
"regenerator-runtime": "^0.13.3"
2930
},
3031
"devDependencies": {
3132
"@babel/core": "^7.2.0",

examples/Dicom/src/index.js

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
1+
import readImageDICOMFileSeries from 'itk/readImageDICOMFileSeries'
2+
import PromiseFileReader from 'promise-file-reader'
13
import curry from 'curry'
24
import dicomParser from 'dicom-parser'
5+
import "regenerator-runtime/runtime";
36

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)
917

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)
1621
if (patient === undefined) {
1722
console.log(`New patient ${patientId}`)
1823
patient = new DICOMPatient()
19-
this.patientDict.set(patientId, patient)
24+
patientDict.set(patientId, patient)
2025
}
21-
patient.parseMetaData(dicomMetaData)
26+
patient.parseMetaData(dicomMetaData, file)
2227
}
2328

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
2933
}
3034

3135
class DICOMPatient {
@@ -37,7 +41,7 @@ class DICOMPatient {
3741
this.studyDict = new Map()
3842
}
3943

40-
parseMetaData(dicomMetaData) {
44+
parseMetaData(dicomMetaData, file) {
4145
const id = dicomMetaData.string('x00100020')
4246
if (this.id === undefined) {
4347
this.id = id
@@ -73,7 +77,7 @@ class DICOMPatient {
7377
study = new DICOMStudy()
7478
this.studyDict.set(studyId, study)
7579
}
76-
study.parseMetaData(dicomMetaData)
80+
study.parseMetaData(dicomMetaData, file)
7781
}
7882
}
7983

@@ -88,7 +92,7 @@ class DICOMStudy {
8892
this.serieDict = new Map()
8993
}
9094

91-
parseMetaData(dicomMetaData) {
95+
parseMetaData(dicomMetaData, file) {
9296
const id = dicomMetaData.string('x00200010')
9397
if (this.id === undefined) {
9498
this.id = id
@@ -138,7 +142,7 @@ class DICOMStudy {
138142
serie = new DICOMSerie()
139143
this.serieDict.set(serieNumber, serie)
140144
}
141-
serie.parseMetaData(dicomMetaData)
145+
serie.parseMetaData(dicomMetaData, file)
142146
}
143147
}
144148

@@ -152,10 +156,10 @@ class DICOMSerie {
152156
this.description = undefined // x0008103e
153157
this.protocolName = undefined // x00181030
154158
this.bodyPart = undefined // x00180015
155-
this.imageDict = new Map()
159+
this.files = []
156160
}
157161

158-
parseMetaData(dicomMetaData) {
162+
parseMetaData(dicomMetaData, file) {
159163
const number = dicomMetaData.string('x00200011')
160164
if (this.number === undefined) {
161165
this.number = number
@@ -212,28 +216,38 @@ class DICOMSerie {
212216
console.assert(this.protocolName === protocolName, "Inconsistent protocol name")
213217
}
214218

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)
221220
}
222221
}
223222

224-
225-
var store = new DICOMStore()
226-
227-
const outputFileInformation = curry(function outputFileInformation (outputTextArea, event) {
223+
const outputFileInformation = curry(async function outputFileInformation (outputTextArea, event) {
228224
outputTextArea.textContent = "Loading..."
229225

226+
// Get files
230227
const dataTransfer = event.dataTransfer
231228
const files = event.target.files || dataTransfer.files
232229

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)
237251
})
238252

239253
export { outputFileInformation }

0 commit comments

Comments
 (0)