This repository has been archived by the owner on Dec 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
buffer.go
227 lines (191 loc) · 4.82 KB
/
buffer.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package dicom
import (
"bytes"
"encoding/binary"
"fmt"
)
type dicomBuffer struct {
*bytes.Buffer
bo binary.ByteOrder
implicit bool
p uint32 // element start position
}
// The default DicomBuffer reads a buffer with Little Endian byteorder
// and explicit VR
func newDicomBuffer(b []byte) *dicomBuffer {
return &dicomBuffer{
bytes.NewBuffer(b),
binary.LittleEndian,
false,
0,
}
}
// Read the VR from the DICOM ditionary
// The VL is a 32-bit unsigned integer
func (buffer *dicomBuffer) readImplicit(elem *DicomElement, p *Parser) (string, uint32) {
var vr string
entry, err := p.getDictEntry(elem.Group, elem.Element)
if err != nil {
vr = "UN"
} else {
vr = entry.vr
}
vl, ulen, err := decodeValueLength(buffer, vr, false)
elem.undefLen = ulen
if err == ErrOddLength {
fmt.Printf("WARN (implicit): attempted to read odd length VL for %+v\n", elem)
panic(ErrOddLength)
}
return vr, vl
}
// The VR is represented by the next two consecutive bytes
// The VL depends on the VR value
func (buffer *dicomBuffer) readExplicit(elem *DicomElement) (string, uint32) {
vr := string(buffer.Next(2))
buffer.p += 2
vl, ulen, err := decodeValueLength(buffer, vr, true)
elem.undefLen = ulen
if err == ErrOddLength {
fmt.Printf("WARN (explicit): attempted to read odd length VL for %+v\n", elem)
panic(ErrOddLength)
}
return vr, vl
}
func decodeValueLength(buffer *dicomBuffer, vr string, explicit bool) (uint32, bool, error) {
var vl uint32
ulen := false
if explicit {
if vr == "US" {
vl = 2
}
// long value representations
switch vr {
case "NA", "OB", "OD", "OF", "OL", "OW", "SQ", "UN", "UC", "UR", "UT":
buffer.Next(2) // ignore two bytes for "future use" (0000H)
buffer.p += 2
vl = buffer.readUInt32()
// Rectify Undefined Length VL
if vl == 0xffffffff {
switch vr {
case "UC", "UR", "UT":
return 0, ulen, ErrUndefLengthNotAllowed
default:
ulen = true
vl = 0
}
}
default:
vl = uint32(buffer.readUInt16())
// Rectify Undefined Length VL
if vl == 0xffff {
ulen = true
vl = 0
}
}
} else {
vl = buffer.readUInt32()
// Rectify Undefined Length VL
if vl == 0xffffffff {
ulen = true
vl = 0
}
}
// Error when encountering odd length
if vl > 0 && vl%2 != 0 {
return 0, ulen, ErrOddLength
}
return vl, ulen, nil
}
// Read a DICOM data element's tag value
// ie. (0002,0000)
// added Value Multiplicity PS 3.5 6.4
func (buffer *dicomBuffer) readTag(p *Parser) *DicomElement {
group := buffer.readHex() // group
element := buffer.readHex() // element
var name string
//var name, vm, vr string
entry, err := p.getDictEntry(group, element)
if err != nil {
if group%2 == 0 {
name = unknown_group_name
} else {
name = private_group_name
}
} else {
name = entry.name
}
return &DicomElement{
Group: group,
Element: element,
Name: name,
}
}
// Read x consecutive bytes as a string
func (buffer *dicomBuffer) readString(vl uint32) string {
chunk := buffer.Next(int(vl))
chunk = bytes.Trim(chunk, "\x00") // trim those pesky null bytes
chunk = bytes.Trim(chunk, "\u200B") // trim zero-width characters
buffer.p += vl
return string(chunk)
}
// Read 4 consecutive bytes as a float32
func (buffer *dicomBuffer) readFloat() (val float32) {
buf := bytes.NewBuffer(buffer.Next(4))
binary.Read(buf, buffer.bo, &val)
buffer.p += 4
return
}
// Read 8 consecutive bytes as a float64
func (buffer *dicomBuffer) readFloat64() (val float64) {
buf := bytes.NewBuffer(buffer.Next(8))
binary.Read(buf, buffer.bo, &val)
buffer.p += 8
return
}
// Read 2 bytes as a hexadecimal value
func (buffer *dicomBuffer) readHex() uint16 {
return buffer.readUInt16()
}
// Read 4 bytes as an UInt32
func (buffer *dicomBuffer) readUInt32() (val uint32) {
buf := bytes.NewBuffer(buffer.Next(4))
binary.Read(buf, buffer.bo, &val)
buffer.p += 4
return
}
// Read 4 bytes as an int32
func (buffer *dicomBuffer) readInt32() (val int32) {
buf := bytes.NewBuffer(buffer.Next(4))
binary.Read(buf, buffer.bo, &val)
buffer.p += 4
return
}
// Read 2 bytes as an UInt16
func (buffer *dicomBuffer) readUInt16() (val uint16) {
buf := bytes.NewBuffer(buffer.Next(2))
binary.Read(buf, buffer.bo, &val)
buffer.p += 2
return
}
// Read 2 bytes as an int16
func (buffer *dicomBuffer) readInt16() (val int16) {
buf := bytes.NewBuffer(buffer.Next(2))
binary.Read(buf, buffer.bo, &val)
buffer.p += 2
return
}
// Read x number of bytes as an array of UInt16 values
func (buffer *dicomBuffer) readUInt16Array(vl uint32) []uint16 {
slice := make([]uint16, int(vl)/2)
for i := 0; i < len(slice); i++ {
slice[i] = buffer.readUInt16()
}
buffer.p += vl
return slice
}
// Read x number of bytes as an array of UInt8 values
func (buffer *dicomBuffer) readUInt8Array(vl uint32) []byte {
chunk := buffer.Next(int(vl))
buffer.p += vl
return chunk
}