suyashkumar / dicom

⚡High Performance DICOM Medical Image Parser in Go.
MIT License
950 stars 138 forks source link

Bug: Write() replaces all VRs for unknown tags with 'UN' #193

Closed bpeake-illuscio closed 3 years ago

bpeake-illuscio commented 3 years ago

Hello,

I came across another Write() bug today. Currently with the way VRs are checked, if an element's tag is not known (i.e., it isn't contained in our tag dict), Write() falls back to using 'UN' as the VR for the element:

func verifyVROrDefault(t tag.Tag, vr string, opts writeOptSet) (string, error) {
    tagInfo, err := tag.Find(t)
    if err != nil {
        return vrraw.Unknown, nil
    }
    // logic continues

This causes issues with Private Element encoded with an Explicit Transfer Syntax. Since the Private element's VR's are known, Write() should keep them in-place when re-writing a file. Instead, all Private elements are encoded with 'UN'. Which can also trigger type errors elsewhere. I originally discovered this when I got the following error when encoding Private elements:

error writing DICOM: ValueType does not match the VR required

I'm working for a PR on this that I'll post shortly.