-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
read.go
585 lines (515 loc) · 15.8 KB
/
read.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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
package dicom
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"log"
"strconv"
"strings"
"unicode"
"github.com/suyashkumar/dicom/pkg/vrraw"
"github.com/suyashkumar/dicom/pkg/dicomio"
"github.com/suyashkumar/dicom/pkg/frame"
"github.com/suyashkumar/dicom/pkg/tag"
)
var (
// ErrorOWRequiresEvenVL indicates that an element with VR=OW had a not even
// value length which is not allowed.
ErrorOWRequiresEvenVL = errors.New("vr of OW requires even value length")
// ErrorUnsupportedVR indicates that this VR is not supported.
ErrorUnsupportedVR = errors.New("unsupported VR")
errorUnableToParseFloat = errors.New("unable to parse float type")
)
func readTag(r dicomio.Reader) (*tag.Tag, error) {
group, gerr := r.ReadUInt16()
element, eerr := r.ReadUInt16()
if gerr == nil && eerr == nil {
return &tag.Tag{Group: group, Element: element}, nil
}
return nil, fmt.Errorf("error reading tag: %v %v", gerr, eerr)
}
// TODO: Parsed VR should be an enum. Will require refactors of tag pkg.
func readVR(r dicomio.Reader, isImplicit bool, t tag.Tag) (string, error) {
if isImplicit {
if entry, err := tag.Find(t); err == nil {
return entry.VR, nil
}
return tag.UnknownVR, nil
}
// Explicit Transfer Syntax, read 2 byte VR:
return r.ReadString(2)
}
func readVL(r dicomio.Reader, isImplicit bool, t tag.Tag, vr string) (uint32, error) {
if isImplicit {
return r.ReadUInt32()
}
// Explicit Transfer Syntax
// More details here: http://dicom.nema.org/medical/dicom/current/output/html/part05.html#sect_7.1.2
switch vr {
// TODO: Parsed VR should be an enum. Will require refactors of tag pkg.
case "NA", vrraw.OtherByte, vrraw.OtherDouble, vrraw.OtherFloat,
vrraw.OtherLong, vrraw.OtherWord, vrraw.Sequence, vrraw.Unknown,
vrraw.UnlimitedCharacters, vrraw.UniversalResourceIdentifier,
vrraw.UnlimitedText:
_ = r.Skip(2) // ignore two reserved bytes (0000H)
vl, err := r.ReadUInt32()
if err != nil {
return 0, err
}
if vl == tag.VLUndefinedLength &&
(vr == vrraw.UnlimitedCharacters ||
vr == vrraw.UniversalResourceIdentifier ||
vr == vrraw.UnlimitedText) {
return 0, errors.New("UC, UR and UT may not have an Undefined Length, i.e.,a Value Length of FFFFFFFFH")
}
return vl, nil
default:
vl16, err := r.ReadUInt16()
if err != nil {
return 0, err
}
vl := uint32(vl16)
// Rectify Undefined Length VL
if vl == 0xffff {
vl = tag.VLUndefinedLength
}
return vl, nil
}
}
func readValue(r dicomio.Reader, t tag.Tag, vr string, vl uint32, isImplicit bool, d *Dataset, fc chan<- *frame.Frame) (Value, error) {
vrkind := tag.GetVRKind(t, vr)
// TODO: if we keep consistent function signature, consider a static map of VR to func?
switch vrkind {
case tag.VRBytes:
return readBytes(r, t, vr, vl)
case tag.VRString:
return readString(r, t, vr, vl)
case tag.VRDate:
return readDate(r, t, vr, vl)
case tag.VRUInt16List, tag.VRUInt32List, tag.VRInt16List, tag.VRInt32List, tag.VRTagList:
return readInt(r, t, vr, vl)
case tag.VRSequence:
return readSequence(r, t, vr, vl)
case tag.VRItem:
return readSequenceItem(r, t, vr, vl)
case tag.VRPixelData:
return readPixelData(r, t, vr, vl, d, fc)
case tag.VRFloat32List, tag.VRFloat64List:
return readFloat(r, t, vr, vl)
default:
return readString(r, t, vr, vl)
}
}
func readPixelData(r dicomio.Reader, t tag.Tag, vr string, vl uint32, d *Dataset, fc chan<- *frame.Frame) (Value,
error) {
if vl == tag.VLUndefinedLength {
var image PixelDataInfo
image.IsEncapsulated = true
// The first Item in PixelData is the basic offset table. Skip this for now.
// TODO: use basic offset table
_, _, err := readRawItem(r)
if err != nil {
return nil, err
}
for !r.IsLimitExhausted() {
data, endOfItems, err := readRawItem(r)
if err != nil {
break
}
if endOfItems {
break
}
f := frame.Frame{
Encapsulated: true,
EncapsulatedData: frame.EncapsulatedFrame{
Data: data,
},
}
if fc != nil {
fc <- &f
}
image.Frames = append(image.Frames, f)
}
return &pixelDataValue{PixelDataInfo: image}, nil
}
// Assume we're reading NativeData data since we have a defined value length as per Part 5 Sec A.4 of DICOM spec.
// We need Elements that have been already parsed (rows, cols, etc) to parse frames out of NativeData Pixel data
if d == nil {
return nil, errors.New("the Dataset context cannot be nil in order to read Native PixelData")
}
i, _, err := readNativeFrames(r, d, fc)
if err != nil {
return nil, err
}
// TODO: avoid this copy
return &pixelDataValue{PixelDataInfo: *i}, nil
}
// readNativeFrames reads NativeData frames from a Decoder based on already parsed pixel information
// that should be available in parsedData (elements like NumberOfFrames, rows, columns, etc)
func readNativeFrames(d dicomio.Reader, parsedData *Dataset, fc chan<- *frame.Frame) (pixelData *PixelDataInfo,
bytesRead int, err error) {
image := PixelDataInfo{
IsEncapsulated: false,
}
// Parse information from previously parsed attributes that are needed to parse NativeData Frames:
rows, err := parsedData.FindElementByTag(tag.Rows)
if err != nil {
return nil, 0, err
}
cols, err := parsedData.FindElementByTag(tag.Columns)
if err != nil {
return nil, 0, err
}
nof, err := parsedData.FindElementByTag(tag.NumberOfFrames)
nFrames := 0
if err == nil {
// No error, so parse number of frames
nFrames, err = strconv.Atoi(MustGetStrings(nof.Value)[0]) // odd that number of frames is encoded as a string...
if err != nil {
return nil, 0, err
}
} else {
// error fetching NumberOfFrames, so default to 1. TODO: revisit
nFrames = 1
}
b, err := parsedData.FindElementByTag(tag.BitsAllocated)
if err != nil {
return nil, 0, err
}
bitsAllocated := MustGetInts(b.Value)[0]
s, err := parsedData.FindElementByTag(tag.SamplesPerPixel)
if err != nil {
return nil, 0, err
}
samplesPerPixel := MustGetInts(s.Value)[0]
pixelsPerFrame := MustGetInts(rows.Value)[0] * MustGetInts(cols.Value)[0]
// Parse the pixels:
image.Frames = make([]frame.Frame, nFrames)
bo := d.ByteOrder()
bytesAllocated := bitsAllocated / 8
pixelBuf := make([]byte, bytesAllocated)
for frameIdx := 0; frameIdx < nFrames; frameIdx++ {
// Init current frame
currentFrame := frame.Frame{
Encapsulated: false,
NativeData: frame.NativeFrame{
BitsPerSample: bitsAllocated,
Rows: MustGetInts(rows.Value)[0],
Cols: MustGetInts(cols.Value)[0],
Data: make([][]int, int(pixelsPerFrame)),
},
}
buf := make([]int, int(pixelsPerFrame)*samplesPerPixel)
for pixel := 0; pixel < int(pixelsPerFrame); pixel++ {
for value := 0; value < samplesPerPixel; value++ {
_, err := io.ReadFull(d, pixelBuf)
if err != nil {
return nil, bytesRead,
fmt.Errorf("could not read uint%d from input: %w", bitsAllocated, err)
}
if bitsAllocated == 8 {
buf[(pixel*samplesPerPixel)+value] = int(pixelBuf[0])
} else if bitsAllocated == 16 {
buf[(pixel*samplesPerPixel)+value] = int(bo.Uint16(pixelBuf))
} else if bitsAllocated == 32 {
buf[(pixel*samplesPerPixel)+value] = int(bo.Uint32(pixelBuf))
}
}
currentFrame.NativeData.Data[pixel] = buf[pixel*samplesPerPixel : (pixel+1)*samplesPerPixel]
}
image.Frames[frameIdx] = currentFrame
if fc != nil {
fc <- ¤tFrame // write the current frame to the frame channel
}
}
bytesRead = bytesAllocated * samplesPerPixel * pixelsPerFrame * nFrames
return &image, bytesRead, nil
}
// readSequence reads a sequence element (VR = SQ) that contains a subset of Items. Each item contains
// a set of Elements.
// See http://dicom.nema.org/medical/dicom/current/output/chtml/part05/sect_7.5.2.html#table_7.5-1
func readSequence(r dicomio.Reader, t tag.Tag, vr string, vl uint32) (Value, error) {
var sequences sequencesValue
if vl == tag.VLUndefinedLength {
for {
subElement, err := readElement(r, nil, nil)
if err != nil {
// Stop reading due to error
log.Println("error reading subitem, ", err)
return nil, err
}
if subElement.Tag == tag.SequenceDelimitationItem {
// Stop reading
break
}
if subElement.Tag != tag.Item || subElement.Value.ValueType() != SequenceItem {
// This is an error, should be an Item!
// TODO: use error var
log.Println("Tag is ", subElement.Tag)
return nil, fmt.Errorf("non item found in sequence")
}
// Append the Item element's dataset of elements to this Sequence's sequencesValue.
sequences.value = append(sequences.value, subElement.Value.(*SequenceItemValue))
}
} else {
// Sequence of elements for a total of VL bytes
err := r.PushLimit(int64(vl))
if err != nil {
return nil, err
}
for !r.IsLimitExhausted() {
subElement, err := readElement(r, nil, nil)
if err != nil {
// TODO: option to ignore errors parsing subelements?
return nil, err
}
// Append the Item element's dataset of elements to this Sequence's sequencesValue.
sequences.value = append(sequences.value, subElement.Value.(*SequenceItemValue))
}
r.PopLimit()
}
return &sequences, nil
}
// readSequenceItem reads an item component of a sequence dicom element and returns an Element
// with a SequenceItem value.
func readSequenceItem(r dicomio.Reader, t tag.Tag, vr string, vl uint32) (Value, error) {
var sequenceItem SequenceItemValue
// seqElements holds items read so far.
// TODO: deduplicate with sequenceItem above
var seqElements Dataset
if vl == tag.VLUndefinedLength {
for {
subElem, err := readElement(r, &seqElements, nil)
if err != nil {
return nil, err
}
if subElem.Tag == tag.ItemDelimitationItem {
break
}
sequenceItem.elements = append(sequenceItem.elements, subElem)
seqElements.Elements = append(seqElements.Elements, subElem)
}
} else {
err := r.PushLimit(int64(vl))
if err != nil {
return nil, err
}
for !r.IsLimitExhausted() {
subElem, err := readElement(r, &seqElements, nil)
if err != nil {
return nil, err
}
sequenceItem.elements = append(sequenceItem.elements, subElem)
seqElements.Elements = append(seqElements.Elements, subElem)
}
r.PopLimit()
}
return &sequenceItem, nil
}
func readBytes(r dicomio.Reader, t tag.Tag, vr string, vl uint32) (Value, error) {
// TODO: add special handling of PixelData
if vr == vrraw.OtherByte {
data := make([]byte, vl)
_, err := io.ReadFull(r, data)
return &bytesValue{value: data}, err
} else if vr == vrraw.OtherWord {
// OW -> stream of 16 bit words
if vl%2 != 0 {
return nil, ErrorOWRequiresEvenVL
}
buf := bytes.NewBuffer(make([]byte, 0, vl))
numWords := int(vl / 2)
for i := 0; i < numWords; i++ {
word, err := r.ReadUInt16()
if err != nil {
return nil, err
}
// TODO: support bytes.BigEndian byte ordering
err = binary.Write(buf, binary.LittleEndian, word)
if err != nil {
return nil, err
}
}
return &bytesValue{value: buf.Bytes()}, nil
}
return nil, ErrorUnsupportedVR
}
func readString(r dicomio.Reader, t tag.Tag, vr string, vl uint32) (Value, error) {
str, err := r.ReadString(vl)
onlySpaces := true
for _, char := range str {
if !unicode.IsSpace(char) {
onlySpaces = false
}
}
if !onlySpaces {
// String may have '\0' suffix if its length is odd.
str = strings.Trim(str, " \000")
}
// Split multiple strings
strs := strings.Split(str, "\\")
return &stringsValue{value: strs}, err
}
func readFloat(r dicomio.Reader, t tag.Tag, vr string, vl uint32) (Value, error) {
err := r.PushLimit(int64(vl))
if err != nil {
return nil, err
}
retVal := &floatsValue{value: make([]float64, 0, vl/2)}
for !r.IsLimitExhausted() {
switch vr {
case vrraw.FloatingPointSingle:
val, err := r.ReadFloat32()
if err != nil {
return nil, err
}
// TODO(suyashkumar): revisit this hack to prevent some internal representation issues upconverting from
// float32 to float64. There is no loss of precision, but the value gets some additional significant digits
// when using golang casting. This approach prevents those artifacts, but is less efficient.
pval, err := strconv.ParseFloat(fmt.Sprint(val), 64)
if err != nil {
return nil, err
}
retVal.value = append(retVal.value, pval)
break
case vrraw.FloatingPointDouble:
val, err := r.ReadFloat64()
if err != nil {
return nil, err
}
retVal.value = append(retVal.value, val)
break
default:
return nil, errorUnableToParseFloat
}
}
r.PopLimit()
return retVal, nil
}
func readDate(r dicomio.Reader, t tag.Tag, vr string, vl uint32) (Value, error) {
rawDate, err := r.ReadString(vl)
if err != nil {
return nil, err
}
date := strings.Trim(rawDate, " \000")
return &stringsValue{value: []string{date}}, nil
}
func readInt(r dicomio.Reader, t tag.Tag, vr string, vl uint32) (Value, error) {
// TODO: add other integer types here
err := r.PushLimit(int64(vl))
if err != nil {
return nil, err
}
retVal := &intsValue{value: make([]int, 0, vl/2)}
for !r.IsLimitExhausted() {
switch vr {
case vrraw.UnsignedShort, vrraw.AttributeTag:
val, err := r.ReadUInt16()
if err != nil {
return nil, err
}
retVal.value = append(retVal.value, int(val))
break
case vrraw.UnsignedLong:
val, err := r.ReadUInt32()
if err != nil {
return nil, err
}
retVal.value = append(retVal.value, int(val))
break
case vrraw.SignedLong:
val, err := r.ReadInt32()
if err != nil {
return nil, err
}
retVal.value = append(retVal.value, int(val))
break
case vrraw.SignedShort:
val, err := r.ReadInt16()
if err != nil {
return nil, err
}
retVal.value = append(retVal.value, int(val))
break
default:
return nil, errors.New("unable to parse integer type")
}
}
r.PopLimit()
return retVal, err
}
// readElement reads the next element. If the next element is a sequence element,
// it may result in a collection of Elements. It takes a pointer to the Dataset of
// elements read so far, since previously read elements may be needed to parse
// certain Elements (like native PixelData). If the Dataset is nil, it is
// treated as an empty Dataset.
func readElement(r dicomio.Reader, d *Dataset, fc chan<- *frame.Frame) (*Element, error) {
t, err := readTag(r)
if err != nil {
return nil, err
}
readImplicit := r.IsImplicit()
if *t == tag.Item {
// Always read implicit for item elements
readImplicit = true
}
vr, err := readVR(r, readImplicit, *t)
if err != nil {
return nil, err
}
vl, err := readVL(r, readImplicit, *t, vr)
if err != nil {
return nil, err
}
// log.Println("readElement: vr, vl", vr, vl)
val, err := readValue(r, *t, vr, vl, readImplicit, d, fc)
if err != nil {
log.Println("error reading value ", err)
return nil, err
}
return &Element{Tag: *t, ValueRepresentation: tag.GetVRKind(*t, vr), RawValueRepresentation: vr, ValueLength: vl, Value: val}, nil
}
// Read an Item object as raw bytes, useful when parsing encapsulated PixelData.
// This returns the read raw item, an indication if this is the end of the set
// of items, and a possible error.
func readRawItem(r dicomio.Reader) ([]byte, bool, error) {
t, err := readTag(r)
if err != nil {
return nil, true, err
}
// Item is always encoded implicit. PS3.6 7.5
vr, err := readVR(r, true, *t)
if err != nil {
return nil, true, err
}
vl, err := readVL(r, true, *t, vr)
if err != nil {
return nil, true, err
}
if *t == tag.SequenceDelimitationItem {
if vl != 0 {
log.Printf("SequenceDelimitationItem's VL != 0: %d", vl)
}
return nil, true, nil
}
if *t != tag.Item {
log.Printf("Expect Item in pixeldata but found tag %s", tag.DebugString(*t))
return nil, false, nil
}
if vl == tag.VLUndefinedLength {
log.Println("Expect defined-length item in pixeldata")
return nil, false, nil
}
if vr != "NA" {
return nil, true, fmt.Errorf("readRawItem: expected VR=NA, got VR=%s", vr)
}
data := make([]byte, vl)
_, err = io.ReadFull(r, data)
if err != nil {
log.Println(err)
return nil, false, err
}
return data, false, nil
}