suyashkumar / dicom

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

dicom.Write fails on 'SmallestImagePixelValue' with VR of 'US' #190

Closed bpeake-illuscio closed 1 month ago

bpeake-illuscio commented 3 years ago

Hello!

I've run into an issue when writing DICOMs which I have modified. In my source DICOM the SmallestImagePixelValue has a VR of 'US'. When I try to write it to a buffer, I get the following erro:

"ValueType does not match the specified type in the VR"

While debugging, I found an internal error with some more contex:

"ERROR dicomio.veryifyElement: VR mismatch for tag (0028,0106)[SmallestImagePixelValue]. Element.VR=US, but DICOM standard defines VR to be SS"

However, this is not correct, the DICOM spec defines the VR of 'SmallestImagePixelValue' as 'US or SS'.

Here is a basic reproduction of the issue:

package main

import (
    "bytes"
    "fmt"
    "github.com/suyashkumar/dicom"
    "github.com/suyashkumar/dicom/pkg/tag"
)

func MustNewValue(data interface{}) dicom.Value {
    value, err := dicom.NewValue(data)
    if err != nil {
        panic(fmt.Errorf("error writing value: %w", err))
    }

    return value
}

func main() {
    dataset := dicom.Dataset{
        Elements: []*dicom.Element{
            &dicom.Element{
                Tag:                    tag.SmallestImagePixelValue,
                ValueRepresentation:    tag.VRUInt16List,
                RawValueRepresentation: "US",
                ValueLength:            0,
                Value:                  MustNewValue([]int{1}),
            },
        },
    }

    buffer := new(bytes.Buffer)
    err := dicom.Write(buffer, dataset, dicom.DefaultMissingTransferSyntax())
    if err != nil {
        fmt.Println("ERROR:", err)
    }
}

I think I mentioned solving for this in #169 . For now I can disable write verification, but it would be nice to be able to turn it on.

CoderTAu commented 1 month ago

hey,have you solved it?

suyashkumar commented 1 month ago

This should be fixed with #315.

I put together a quick test here: https://github.com/suyashkumar/dicom/compare/s/190?expand=1 and it passes: https://github.com/suyashkumar/dicom/actions/runs/11096078190/job/30825488188#step:6:379