Skip to content

Commit

Permalink
Use vrraw consts in read (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
suyashkumar authored Jan 4, 2021
1 parent 0f5b024 commit 0bcfff7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
28 changes: 18 additions & 10 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"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"
Expand Down Expand Up @@ -58,14 +60,20 @@ func readVL(r dicomio.Reader, isImplicit bool, t tag.Tag, vr string) (uint32, er
// 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", "OB", "OD", "OF", "OL", "OW", "SQ", "UN", "UC", "UR", "UT":
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 == "UC" || vr == "UR" || vr == "UT") {
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
Expand Down Expand Up @@ -351,11 +359,11 @@ func readSequenceItem(r dicomio.Reader, t tag.Tag, vr string, vl uint32) (Value,

func readBytes(r dicomio.Reader, t tag.Tag, vr string, vl uint32) (Value, error) {
// TODO: add special handling of PixelData
if vr == "OB" {
if vr == vrraw.OtherByte {
data := make([]byte, vl)
_, err := io.ReadFull(r, data)
return &bytesValue{value: data}, err
} else if vr == "OW" {
} else if vr == vrraw.OtherWord {
// OW -> stream of 16 bit words
if vl%2 != 0 {
return nil, ErrorOWRequiresEvenVL
Expand Down Expand Up @@ -407,7 +415,7 @@ func readFloat(r dicomio.Reader, t tag.Tag, vr string, vl uint32) (Value, error)
retVal := &floatsValue{value: make([]float64, 0, vl/2)}
for !r.IsLimitExhausted() {
switch vr {
case "FL":
case vrraw.FloatingPointSingle:
val, err := r.ReadFloat32()
if err != nil {
return nil, err
Expand All @@ -421,7 +429,7 @@ func readFloat(r dicomio.Reader, t tag.Tag, vr string, vl uint32) (Value, error)
}
retVal.value = append(retVal.value, pval)
break
case "FD":
case vrraw.FloatingPointDouble:
val, err := r.ReadFloat64()
if err != nil {
return nil, err
Expand Down Expand Up @@ -456,28 +464,28 @@ func readInt(r dicomio.Reader, t tag.Tag, vr string, vl uint32) (Value, error) {
retVal := &intsValue{value: make([]int, 0, vl/2)}
for !r.IsLimitExhausted() {
switch vr {
case "US", "AT":
case vrraw.UnsignedShort, vrraw.AttributeTag:
val, err := r.ReadUInt16()
if err != nil {
return nil, err
}
retVal.value = append(retVal.value, int(val))
break
case "UL":
case vrraw.UnsignedLong:
val, err := r.ReadUInt32()
if err != nil {
return nil, err
}
retVal.value = append(retVal.value, int(val))
break
case "SL":
case vrraw.SignedLong:
val, err := r.ReadInt32()
if err != nil {
return nil, err
}
retVal.value = append(retVal.value, int(val))
break
case "SS":
case vrraw.SignedShort:
val, err := r.ReadInt16()
if err != nil {
return nil, err
Expand Down
6 changes: 4 additions & 2 deletions read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strconv"
"testing"

"github.com/suyashkumar/dicom/pkg/vrraw"

"github.com/suyashkumar/dicom/pkg/dicomio"
"github.com/suyashkumar/dicom/pkg/frame"

Expand Down Expand Up @@ -51,7 +53,7 @@ func TestReadFloat_float64(t *testing.T) {
{
name: "float64",
floats: []float64{20.1, 32.22},
VR: "FD",
VR: vrraw.FloatingPointDouble,
want: &floatsValue{value: []float64{20.1, 32.22}},
expectedErr: nil,
},
Expand Down Expand Up @@ -100,7 +102,7 @@ func TestReadFloat_float32(t *testing.T) {
{
name: "float32",
floats: []float32{20.1001, 32.22},
VR: "FL",
VR: vrraw.FloatingPointSingle,
want: &floatsValue{value: []float64{20.1001, 32.22}},
expectedErr: nil,
},
Expand Down

0 comments on commit 0bcfff7

Please sign in to comment.