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 31
/
dicom.go
executable file
·236 lines (178 loc) · 4.99 KB
/
dicom.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
228
229
230
231
232
233
234
235
236
package dicom
import (
"encoding/binary"
"errors"
)
type DicomFile struct {
Elements []DicomElement
}
// Errors
var (
ErrIllegalTag = errors.New("Illegal tag found in PixelData")
ErrTagNotFound = errors.New("Could not find tag in dicom dictionary")
ErrBrokenFile = errors.New("Invalid DICOM file")
ErrOddLength = errors.New("Encountered odd length Value Length")
ErrUndefLengthNotAllowed = errors.New("UC, UR and UT may not have an Undefined Length, i.e.,a Value Length of FFFFFFFFH.")
)
const (
magic_word = "DICM"
implicit_vr_little_endian = "1.2.840.10008.1.2"
explicit_vr_little_endian = "1.2.840.10008.1.2.1"
explicit_vr_big_endian = "1.2.840.10008.1.2.2"
)
// Parse a byte array, returns a DICOM file struct
func (p *Parser) Parse(buff []byte) (*DicomFile, <-chan DicomMessage) {
c := make(chan DicomMessage)
waitMsg := make(chan bool)
buffer := newDicomBuffer(buff) //*di.Bytes)
buffer.Next(128) // skip preamble
buffer.p = +128
// check for magic word
if magicWord := string(buffer.Next(4)); magicWord != magic_word {
panic(ErrBrokenFile)
}
file := &DicomFile{}
go func() {
// (0002,0000) MetaElementGroupLength
metaElem := buffer.readDataElement(p)
metaLength := int(metaElem.Value[0].(uint32))
p.appendDataElement(file, metaElem)
// Read meta tags
start := buffer.Len()
for start-buffer.Len() < metaLength {
elem := buffer.readDataElement(p)
p.appendDataElement(file, elem)
c <- DicomMessage{elem, waitMsg}
<-waitMsg
}
// read endianness and explicit VR
endianess, implicit, err := file.getTransferSyntax()
if err != nil {
panic(ErrBrokenFile)
}
// modify buffer according to new TransferSyntaxUID
buffer.bo = endianess
buffer.implicit = implicit
// Start with image meta data
for buffer.Len() != 0 {
elem := buffer.readDataElement(p)
p.appendDataElement(file, elem)
c <- DicomMessage{elem, waitMsg}
<-waitMsg
if elem.Vr == "SQ" {
p.readItems(file, buffer, elem, c)
}
if elem.Name == "PixelData" {
p.readPixelItems(file, buffer, elem, c)
break
}
}
close(c)
}()
return file, c
}
func (p *Parser) readItems(file *DicomFile, buffer *dicomBuffer, sq *DicomElement, c chan DicomMessage) (uint32, error) {
waitMsg := make(chan bool)
sq.IndentLevel++
sqLength := sq.Vl
if sqLength == 0 {
return 0, nil
}
elem := buffer.readDataElement(p)
elem.IndentLevel = sq.IndentLevel
sqAcum := elem.elemLen
itemLength := elem.Vl
itemAcum := uint32(0)
if elem.Name == "Item" {
if elem.Vl > 0 {
for buffer.Len() != 0 {
p.appendDataElement(file, elem)
c <- DicomMessage{elem, waitMsg}
<-waitMsg
if elem.Vr == "SQ" {
l, _ := p.readItems(file, buffer, elem, c)
sqAcum += l
}
if itemAcum == itemLength {
break
}
if sqAcum == sqLength {
break
}
elem = buffer.readDataElement(p)
elem.IndentLevel = sq.IndentLevel
if elem.Name == "Item" {
itemLength = elem.Vl
}
itemAcum += elem.elemLen
sqAcum += elem.elemLen
}
} else if elem.undefLen == true {
//log.Println("____ ITEM UNDEF LEN ____")
for buffer.Len() != 0 {
if elem.Vr == "SQ" {
p.readItems(file, buffer, elem, c)
}
if elem.Name == "SequenceDelimitationItem" {
break
}
p.appendDataElement(file, elem)
c <- DicomMessage{elem, waitMsg}
<-waitMsg
elem = buffer.readDataElement(p)
elem.IndentLevel = sq.IndentLevel
}
} else {
// ITEM 0 LEN
}
}
return sqAcum, nil
}
func (p *Parser) readPixelItems(file *DicomFile, buffer *dicomBuffer, sq *DicomElement, c chan DicomMessage) {
waitMsg := make(chan bool)
elem := buffer.readDataElement(p)
for buffer.Len() != 0 {
if elem.Name == "Item" {
elem.Value = append(elem.Value, buffer.readUInt8Array(elem.Vl))
}
p.appendDataElement(file, elem)
c <- DicomMessage{elem, waitMsg}
<-waitMsg
elem = buffer.readDataElement(p)
}
p.appendDataElement(file, elem)
c <- DicomMessage{elem, waitMsg}
<-waitMsg
}
// Append a dataElement to the DicomFile
func (p *Parser) appendDataElement(file *DicomFile, elem *DicomElement) {
file.Elements = append(file.Elements, *elem)
}
// Finds the SyntaxTrasnferUID and returns the endianess and implicit VR for the file
func (file *DicomFile) getTransferSyntax() (binary.ByteOrder, bool, error) {
var err error
elem, err := file.LookupElement("TransferSyntaxUID")
if err != nil {
return nil, true, err
}
ts := elem.Value[0].(string)
// defaults are explicit VR, little endian
switch ts {
case implicit_vr_little_endian:
return binary.LittleEndian, true, nil
case explicit_vr_little_endian:
return binary.LittleEndian, false, nil
case explicit_vr_big_endian:
return binary.BigEndian, false, nil
}
return binary.LittleEndian, false, nil
}
// Lookup a tag by name
func (file *DicomFile) LookupElement(name string) (*DicomElement, error) {
for _, elem := range file.Elements {
if elem.Name == name {
return &elem, nil
}
}
return nil, ErrTagNotFound
}