-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathambiguous_vr.go
More file actions
305 lines (283 loc) · 6.77 KB
/
Copy pathambiguous_vr.go
File metadata and controls
305 lines (283 loc) · 6.77 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package godicom
import (
"fmt"
)
// Ambiguous VR correction follows pydicom.filewriter.correct_ambiguous_vr.
var ambiguousUsSsTags = map[Tag]bool{
MustTag(0x00189810): true,
MustTag(0x00221452): true,
MustTag(0x00280104): true,
MustTag(0x00280105): true,
MustTag(0x00280106): true,
MustTag(0x00280107): true,
MustTag(0x00280108): true,
MustTag(0x00280109): true,
MustTag(0x00280110): true,
MustTag(0x00280111): true,
MustTag(0x00280120): true,
MustTag(0x00280121): true,
MustTag(0x00281101): true,
MustTag(0x00281102): true,
MustTag(0x00281103): true,
MustTag(0x00283002): true,
MustTag(0x00409211): true,
MustTag(0x00409216): true,
MustTag(0x00603004): true,
MustTag(0x00603006): true,
}
var ambiguousObOwTags = map[Tag]bool{
MustTag(0x54000110): true,
MustTag(0x54000112): true,
MustTag(0x5400100A): true,
MustTag(0x54001010): true,
}
func isOverlayDataTag(tag Tag) bool {
group := tag.Group()
return group >= 0x6000 && group <= 0x601E && group%2 == 0 && tag.Element() == 0x3000
}
func pixelRepresentationFromAncestors(ancestors []*Dataset) (int, bool) {
for _, ds := range ancestors {
if v, ok := ds.GetInt(MustTag(0x00280103)); ok {
return v, true
}
}
return 0, false
}
func elementRawBytes(elem *Element) []byte {
if elem.RawValue != nil {
return elem.RawValue
}
if b, ok := elem.Value.([]byte); ok {
return b
}
return nil
}
func valueIsInt(v interface{}) bool {
switch v.(type) {
case int, int16, int32, int64, uint16, uint32, uint64:
return true
case *MultiValue[int]:
return true
case *MultiValue[int64]:
return true
default:
return false
}
}
func correctAmbiguousVRElement(elem *Element, ds *Dataset, isLittleEndian bool, ancestors []*Dataset) error {
if len(ancestors) == 0 {
ancestors = []*Dataset{ds}
}
switch {
case elem.Tag == MustTag(0x7FE00010):
if elem.IsUndefinedLength {
elem.VR = VROB
return nil
}
if ds.originalEnc.IsImplicitVR {
elem.VR = VROW
return nil
}
bits, ok := ds.GetInt(MustTag(0x00280100))
if !ok {
return fmt.Errorf("failed to resolve ambiguous VR for tag %s: missing 'BitsAllocated'", elem.Tag)
}
if bits > 8 {
elem.VR = VROW
} else {
elem.VR = VROB
}
case ambiguousUsSsTags[elem.Tag]:
pixelRep, hasRep := pixelRepresentationFromAncestors(ancestors)
if !hasRep {
if ds.Has(MustTag(0x7FE00010)) {
return fmt.Errorf("failed to resolve ambiguous VR for tag %s: missing 'PixelRepresentation'", elem.Tag)
}
if repElem, ok := ds.Get(MustTag(0x00280103)); ok {
if v, ok := repElem.Value.(int); ok && v == 0 {
pixelRep = 0
} else {
pixelRep = 1
}
} else {
pixelRep = 0
}
}
if pixelRep == 0 {
elem.VR = VRUS
} else {
elem.VR = VRSS
}
raw := elementRawBytes(elem)
if elem.VM() == 0 && len(raw) == 0 {
return nil
}
if !valueIsInt(elem.Value) {
if len(raw) == 0 {
return nil
}
converted, err := convertInts(raw, isLittleEndian, 2, pixelRep != 0)
if err != nil {
return fmt.Errorf("failed to resolve ambiguous VR for tag %s: %w", elem.Tag, err)
}
elem.Value = converted
elem.RawValue = nil
}
case ambiguousObOwTags[elem.Tag]:
if ds.originalEnc.IsImplicitVR {
elem.VR = VROW
return nil
}
bits, ok := ds.GetInt(MustTag("WaveformBitsAllocated"))
if !ok {
return fmt.Errorf("failed to resolve ambiguous VR for tag %s: missing 'WaveformBitsAllocated'", elem.Tag)
}
if bits > 8 {
elem.VR = VROW
} else {
elem.VR = VROB
}
case elem.Tag == MustTag(0x00283006):
lutDescriptor, ok := ds.Get(MustTag(0x00283002))
if !ok {
return fmt.Errorf("failed to resolve ambiguous VR for tag %s: missing 'LUTDescriptor'", elem.Tag)
}
first := lutDescriptorFirstValue(lutDescriptor)
if first == 1 {
elem.VR = VRUS
raw := elementRawBytes(elem)
if elem.VM() == 0 && len(raw) == 0 {
return nil
}
if !valueIsInt(elem.Value) {
if len(raw) == 0 {
return nil
}
converted, err := convertInts(raw, isLittleEndian, 2, false)
if err != nil {
return fmt.Errorf("failed to resolve ambiguous VR for tag %s: %w", elem.Tag, err)
}
elem.Value = converted
elem.RawValue = nil
}
} else {
elem.VR = VROW
}
case isOverlayDataTag(elem.Tag):
if IsAmbiguousVR(elem.VR) {
elem.VR = VROW
}
}
return nil
}
func lutDescriptorFirstValue(elem *Element) int {
switch v := elem.Value.(type) {
case int:
return v
case []int:
if len(v) > 0 {
return v[0]
}
case *MultiValue[int]:
vals := v.Values()
if len(vals) > 0 {
return vals[0]
}
case *MultiValue[int64]:
vals := v.Values()
if len(vals) > 0 {
return int(vals[0])
}
case *MultiValue[uint64]:
vals := v.Values()
if len(vals) > 0 {
return int(vals[0])
}
case *MultiValue[interface{}]:
vals := v.Values()
if len(vals) > 0 {
if i, ok := vals[0].(int); ok {
return i
}
}
case []byte:
if len(v) >= 2 {
return int(uint16(v[0]) | uint16(v[1])<<8)
}
}
return 0
}
// CorrectAmbiguousVRPreservingRaw corrects ambiguous VR elements that are not
// stored as raw bytes. Used before implicit writes with in-memory values while
// keeping raw element roundtrips intact.
func CorrectAmbiguousVRPreservingRaw(ds *Dataset, isLittleEndian bool, ancestors []*Dataset) error {
if ancestors == nil {
ancestors = []*Dataset{ds}
}
for _, tag := range ds.SortedTags() {
elem, ok := ds.Get(tag)
if !ok {
continue
}
if elem.Deferred {
continue
}
if elem.VR == VRSQ {
seq, ok := elem.Value.(*Sequence)
if !ok || seq == nil {
continue
}
for _, item := range seq.Items() {
childAncestors := append([]*Dataset{item}, ancestors...)
if err := CorrectAmbiguousVRPreservingRaw(item, isLittleEndian, childAncestors); err != nil {
return err
}
}
continue
}
if IsAmbiguousVR(elem.VR) {
if len(elem.RawValue) > 0 {
continue
}
if err := correctAmbiguousVRElement(elem, ds, isLittleEndian, ancestors); err != nil {
return err
}
}
}
return nil
}
// CorrectAmbiguousVR walks ds correcting ambiguous VR elements when possible.
// Mirrors pydicom.filewriter.correct_ambiguous_vr.
func CorrectAmbiguousVR(ds *Dataset, isLittleEndian bool, ancestors []*Dataset) error {
if ancestors == nil {
ancestors = []*Dataset{ds}
}
for _, tag := range ds.SortedTags() {
elem, ok := ds.Get(tag)
if !ok {
continue
}
if elem.Deferred {
continue
}
if elem.VR == VRSQ {
seq, ok := elem.Value.(*Sequence)
if !ok || seq == nil {
continue
}
for _, item := range seq.Items() {
childAncestors := append([]*Dataset{item}, ancestors...)
if err := CorrectAmbiguousVR(item, isLittleEndian, childAncestors); err != nil {
return err
}
}
continue
}
if IsAmbiguousVR(elem.VR) {
if err := correctAmbiguousVRElement(elem, ds, isLittleEndian, ancestors); err != nil {
return err
}
}
}
return nil
}