-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.go
240 lines (209 loc) · 6.17 KB
/
io.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
237
238
239
240
package go2com
import (
"bufio"
"encoding/binary"
"fmt"
"github.com/okieraised/go2com/internal/utils"
"github.com/okieraised/go2com/pkg/dicom/tag"
"github.com/okieraised/go2com/pkg/dicom/vr"
"github.com/okieraised/go2com/pkg/plugins/orthanc"
"io"
"strings"
)
const (
MagicString = "DICM"
PrivateTag = "PrivateTag"
)
type dcmReader struct {
reader *bufio.Reader
binaryOrder binary.ByteOrder
dataset Dataset
metadata Dataset
allowNonCompliantDcm bool
isImplicit bool
keepTrackImplicit bool
skipPixelData bool
skipDataset bool
fileSize int64
}
// NewDICOMReader returns a new reader
func NewDICOMReader(reader *bufio.Reader, options ...func(*dcmReader)) *dcmReader {
parser := &dcmReader{
reader: reader,
binaryOrder: binary.LittleEndian,
isImplicit: false,
skipPixelData: false,
skipDataset: false,
}
for _, opt := range options {
opt(parser)
}
return parser
}
// WithAllowNonCompliantDcm provides option to keep trying to parse the file even if it's not DICOM compliant
// e.g.: Missing header, missing FileMetaInformationGroupLength,...
func WithAllowNonCompliantDcm(allowNonCompliantDcm bool) func(*dcmReader) {
return func(s *dcmReader) {
s.allowNonCompliantDcm = allowNonCompliantDcm
}
}
// WithSkipPixelData provides option to skip reading pixel data (7FE0,0010).
// If true, pixel data is skipped. If false, pixel data will be read
func WithSkipPixelData(skipPixelData bool) func(*dcmReader) {
return func(s *dcmReader) {
s.skipPixelData = skipPixelData
}
}
// WithSkipDataset provides option to read only the metadata header.
// If true, only the meta header is read, else, the dataset will be read
func WithSkipDataset(skipDataset bool) func(*dcmReader) {
return func(s *dcmReader) {
s.skipDataset = skipDataset
}
}
// WithSetFileSize provides option to set the file size to the reader
func WithSetFileSize(fileSize int64) func(*dcmReader) {
return func(s *dcmReader) {
s.fileSize = fileSize
}
}
// GetMetadata returns the file meta header
func (r *dcmReader) GetMetadata() Dataset {
return r.metadata
}
// GetDataset returns the dataset
func (r *dcmReader) GetDataset() Dataset {
return r.dataset
}
func (r *dcmReader) RetrieveFileUID() (*DicomUID, error) {
return r.dataset.RetrieveFileUID()
}
// Parse reads the DICOM file and parses it into array of elements
func (r *dcmReader) Parse() error {
err := r.parse()
if err != nil {
return err
}
return nil
}
func (r *dcmReader) SkipPixelData() bool {
return r.skipPixelData
}
func (r *dcmReader) ByteOrder() binary.ByteOrder {
return r.binaryOrder
}
// IsValidDICOM checks if the dicom file follows the standard by having 128 bytes preamble followed by the magic string 'DICM'
func (r *dcmReader) IsValidDICOM() error {
preamble, err := r.peek(132)
if err != nil {
return fmt.Errorf("cannot read the first 132 bytes: %v", err)
}
if string(preamble[128:]) != MagicString {
return fmt.Errorf("file is not in valid dicom format")
}
return nil
}
// GetElementByTagString returns the element value of the input tag
// Tag should be in (gggg,eeee) or ggggeeee format
func (r *dcmReader) GetElementByTagString(tagStr string) (interface{}, error) {
tagStr = utils.FormatTag(tagStr)
if strings.HasPrefix(tagStr, "0002") {
for _, elem := range r.metadata.Elements {
if tagStr == elem.Tag.StringWithoutParentheses() {
return elem.Value, nil
}
}
return nil, fmt.Errorf("cannot find tag %s", tagStr)
} else {
for _, elem := range r.dataset.Elements {
if tagStr == elem.Tag.StringWithoutParentheses() {
return elem.Value, nil
}
}
return nil, fmt.Errorf("cannot find tag %s", tagStr)
}
}
// ExportDatasetTags returns the mapped tag/(vr,value) dictionary
func (r *dcmReader) ExportDatasetTags(exportMeta bool) MappedTag {
res := make(MappedTag, len(r.metadata.Elements)+len(r.dataset.Elements))
if exportMeta {
mt := r.metadata
for _, elem := range mt.Elements {
res.mapElement(elem)
}
}
ds := r.dataset
//colorImage := false
for _, elem := range ds.Elements {
//if elem.Tag == tag.RedPaletteColorLookupTableData || elem.Tag == tag.BluePaletteColorLookupTableData || elem.Tag == tag.GreenPaletteColorLookupTableData {
// colorImage = true
//}
vrStr := elem.ValueRepresentationStr
if vrStr == "OB" || vrStr == "OW" || vrStr == "UN" || strings.ToLower(vrStr) == "ox" {
continue
}
res.mapElement(elem)
}
//if colorImage {
// bulkURIMap := createOrthancURI(ds)
// for k, v := range bulkURIMap {
// res[k] = v
// }
//}
return res
}
func (r *dcmReader) ExportSeriesTags() MappedTag {
res := make(MappedTag, len(r.dataset.Elements))
var value interface{}
for _, elem := range r.dataset.Elements {
if _, ok := orthanc.OrthancGetSeriesOfStudyTags[elem.Tag]; ok {
tagStr := elem.Tag.StringWithoutParentheses()
if elem.ValueRepresentationStr == vr.PersonName {
value = utils.AppendToSlice(map[string]interface{}{
"Alphabetic": elem.Value.RawValue,
})
} else {
value = utils.AppendToSlice(elem.Value.RawValue)
}
res[tagStr] = tag.TagBrowser{
VR: elem.ValueRepresentationStr,
Value: value,
}
}
}
prefix := "http://127.0.0.1:8042"
firstSeriesURI := fmt.Sprintf("%s/dicom-web/studies/%s/series/%s", prefix,
res[tag.StudyInstanceUID.StringWithoutParentheses()].Value.([]interface{})[0].(string),
res[tag.SeriesInstanceUID.StringWithoutParentheses()].Value.([]interface{})[0].(string),
)
res[tag.RetrieveURL.StringWithoutParentheses()] = tag.TagBrowser{
Value: []interface{}{firstSeriesURI},
VR: "UR",
}
return res
}
func (r *dcmReader) Read(p []byte) (int, error) {
return r.reader.Read(p)
}
func (r *dcmReader) IsImplicit() bool {
return r.isImplicit
}
func (r *dcmReader) SetTransferSyntax(binaryOrder binary.ByteOrder, isImplicit bool) {
r.binaryOrder = binaryOrder
r.isImplicit = isImplicit
}
func (r *dcmReader) SetFileSize(fileSize int64) error {
r.fileSize = fileSize
return nil
}
func (r *dcmReader) GetFileSize() int64 {
return r.fileSize
}
func (r *dcmReader) readString(n uint32) (string, error) {
data := make([]byte, n)
_, err := io.ReadFull(r, data)
if err != nil {
return "", err
}
return string(data), nil
}