suyashkumar / dicom

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

Feature/add ValueCount() and GetValueIndex() method to Value #195

Open bpeake-illuscio opened 3 years ago

bpeake-illuscio commented 3 years ago

Hello again!

In writing some code, I realized it would be useful to get the number of inner values a Value contains without having to do a type assert (or really knowing anything about the inner type).

This is a small PR that adds a ValueCount()int and GetValueIndex(i int)interface{} methods to Value.

Example:

package main

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

func main() {
    element, err := dicom.NewElement(tag.ModalitiesInStudy, []string{"MR", "CT"})
    if err != nil {
        panic(err)
    }

    tagInfo, err := tag.Find(tag.ModalitiesInStudy)
    if err != nil {
        panic(err)
    }

    fmt.Println("TAG        :", element.Tag)
    fmt.Println("VM         :", tagInfo.VM)
    fmt.Println("VALUE COUNT:", element.Value.ValueCount())

    for i := 0 ; i < element.Value.ValueCount() ; i++ {
        fmt.Printf("VALUE %v    : %v\n", i, element.Value.GetValueIndex(i))
    }

    // Output:
    // TAG        : (0008,0061)
    // VM         : 1-n
    // VALUE COUNT: 2
    // VALUE 0    : MR
    // VALUE 1    : CT
}

I've opened this as a draft PR in case you feel it doesn't fit the lib. Let me know what you think!

bpeake-illuscio commented 3 years ago

I just updated this PR to also include a GetValueIndex() method. I am working on a problem where it would be useful in testing to be able to build a []interface{} slice of the inner Element values.

bpeake-illuscio commented 3 years ago

Although, looking at this I think it might make more sense to handle PixelData like []byte, always return a count of 1, and return the entire data, instead of frames. I'm going to update that real quick.

bpeake-illuscio commented 3 years ago

alright, that change is up.