forked from gillesdemey/go-dicom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
executable file
·170 lines (144 loc) · 3.29 KB
/
parser.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package dicom
import (
"bytes"
"fmt"
"strings"
)
// Constants
const (
pixeldata_group = 0xFFFE
unknown_group_name = "Unknown Group"
private_group_name = "Private Data"
)
// Value Multiplicity PS 3.5 6.4
type dcmVM struct {
s string
Min uint8
Max uint8
N bool
}
// A DICOM element
type DicomElement struct {
Group uint16
Element uint16
Name string
Vr string
Vl uint32
Value []interface{} // Value Multiplicity PS 3.5 6.4
IndentLevel uint8
elemLen uint32
undefLen bool
P uint32
}
type Parser struct {
dictionary [][]*dictEntry
}
// Stringer
func (e *DicomElement) String() string {
s := strings.Repeat(" ", int(e.IndentLevel)*2)
sv := fmt.Sprintf("%v", e.Value)
if len(sv) > 50 {
sv = sv[1:50] + "(...)"
}
sVl := fmt.Sprintf("%d", e.Vl)
if e.undefLen == true {
sVl = "UNDEF"
}
return fmt.Sprintf("%08d %s (%04X, %04X) %s %s %d %s %s", e.P, s, e.Group, e.Element, e.Vr, sVl, e.elemLen, e.Name, sv)
}
// Return the tag as a string to use in the Dicom dictionary
func (e *DicomElement) getTag() string {
return fmt.Sprintf("(%04X,%04X)", e.Group, e.Element)
}
// Create a new parser, with functional options for configuration
// http://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis
func NewParser(options ...func(*Parser) error) (*Parser, error) {
p := Parser{}
// apply defaults
dict := bytes.NewReader([]byte(dicomDictData))
err := Dictionary(dict)(&p)
if err != nil {
panic(err)
}
// override defaults
for _, option := range options {
err := option(&p)
if err != nil {
panic(err)
}
}
return &p, nil
}
// Read a DICOM data element
func (buffer *dicomBuffer) readDataElement(p *Parser) *DicomElement {
implicit := buffer.implicit
inip := buffer.p
elem := buffer.readTag(p)
var vr string // Value Representation
var vl uint32 = 0 // Value Length
// The elements for group 0xFFFE should be Encoded as Implicit VR.
// DICOM Standard 09. PS 3.6 - Section 7.5: "Nesting of Data Sets"
if elem.Group == pixeldata_group {
implicit = true
}
if implicit {
vr, vl = buffer.readImplicit(elem, p)
} else {
vr, vl = buffer.readExplicit(elem)
}
elem.Vr = vr
elem.Vl = vl
// data
var data []interface{}
uvl := vl
valLen := uint32(vl)
for uvl > 0 {
switch vr {
case "AT":
valLen = 2
data = append(data, buffer.readHex())
case "UL":
valLen = 4
data = append(data, buffer.readUInt32())
case "SL":
valLen = 4
data = append(data, buffer.readInt32())
case "US":
valLen = 2
data = append(data, buffer.readUInt16())
case "SS":
valLen = 2
data = append(data, buffer.readInt16())
case "FL":
valLen = 4
data = append(data, buffer.readFloat())
case "FD":
valLen = 8
data = append(data, buffer.readFloat64())
case "OW":
valLen = vl
data = append(data, buffer.readUInt16Array(vl))
case "OB":
valLen = vl
data = append(data, buffer.readUInt8Array(vl))
case "NA":
valLen = vl
//case "XS": ??
case "SQ":
valLen = vl
data = append(data, "")
default:
valLen = vl
str := strings.TrimRight(buffer.readString(vl), " ")
strs := strings.Split(str, "\\")
for _, s := range strs {
data = append(data, s)
}
}
uvl -= valLen
}
elem.P = inip
elem.Value = data
elem.elemLen = buffer.p - inip
return elem
}