From 0bcfff7935c94685154ab30e0176c8f6f204459e Mon Sep 17 00:00:00 2001 From: Suyash Kumar Date: Mon, 4 Jan 2021 18:03:49 -0500 Subject: [PATCH] Use vrraw consts in read (#172) --- read.go | 28 ++++++++++++++++++---------- read_test.go | 6 ++++-- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/read.go b/read.go index b9958e21..d7367b51 100644 --- a/read.go +++ b/read.go @@ -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" @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/read_test.go b/read_test.go index 34912731..ad9837f4 100644 --- a/read_test.go +++ b/read_test.go @@ -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" @@ -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, }, @@ -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, },