forked from grailbio/go-dicom
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdicomdir.go
50 lines (46 loc) · 1.14 KB
/
dicomdir.go
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
package dicom
import (
"io"
"io/ioutil"
"strings"
"github.com/grailbio/go-dicom/dicomtag"
)
// DirectoryRecord contains info about one DICOM file mentioned in DICOMDIR.
type DirectoryRecord struct {
Path string
// TODO(saito): perhaps extract more fields
}
// ParseDICOMDIR parses contents of a "DICOMDIR" stored in "in".
//
// http://dicom.nema.org/medical/Dicom/2016b/output/chtml/part03/sect_F.2.2.2.html
func ParseDICOMDIR(in io.Reader) (recs []DirectoryRecord, err error) {
bytes, err := ioutil.ReadAll(in)
if err != nil {
return nil, err
}
ds, err := ReadDataSetInBytes(bytes, ReadOptions{})
if err != nil {
return nil, err
}
seq, err := ds.FindElementByTag(dicomtag.DirectoryRecordSequence)
if err != nil {
return nil, err
}
for _, item := range seq.Value {
path := ""
for _, subvalue := range item.(*Element).Value {
subelem := subvalue.(*Element)
if subelem.Tag == dicomtag.ReferencedFileID {
names, err := subelem.GetStrings()
if err != nil {
return nil, err
}
path = strings.Join(names, "/")
}
}
if path != "" {
recs = append(recs, DirectoryRecord{Path: path})
}
}
return recs, nil
}