dataelem
The data element: a tag, a VR and a value, and the VR constants. A
dataset.Dataset is a set of these.
import "github.com/amrshadid/go-dicom/dataelem" · pkg.go.dev
When to use it
- Build an element to add to a data set:
NewDataElement(tag, vr, value), where the value may be bytes, a string, a Go number or a slice of numbers. - Read the bytes of an element whatever Go type it holds (
Bytes), or encode a Go value for a VR (ValueBytes). - Name a VR:
dataelem.PN,dataelem.USand the rest.
To get and set attributes by keyword, use the methods of dataset. Tags and the dictionary are in tag.
Key types and functions
| Name | What it is for |
|---|---|
DataElement | One element: VR, Value, Keyword, and the tag behind Tag(). |
NewDataElement | Make one. The tag may be a tag.Tag or a uint32. |
DataElement.Bytes | The bytes the writer would write, whatever the value is held as. |
ValueBytes | Encode a Go value for a VR; a value that does not fit is an error. |
DataElement.MustTag, Tag | The element's tag. |
DataElement.GetVR, GetValue, SetValue | Read and change the VR and value. |
DataElement.GetTextValue, GetPersonNameValue | Decode text under a character set. |
VR, AllVRs, GetVRInfo | The VR constants and what each is. |
ValueInFile | A value left in the file it was read from: offset and length. |
Example
// Build elements from Go values and read back the bytes a writer would write.
package main
import (
"fmt"
"log"
"github.com/amrshadid/go-dicom/dataelem"
"github.com/amrshadid/go-dicom/dataset"
"github.com/amrshadid/go-dicom/tag"
)
func main() {
rows := dataelem.NewDataElement(tag.New(0x0028, 0x0010), dataelem.US, uint16(512))
spacing := dataelem.NewDataElement(tag.New(0x0028, 0x0030), dataelem.DS, []float64{0.5, 0.5})
name := dataelem.NewDataElement(tag.New(0x0010, 0x0010), dataelem.PN, "Doe^Jane")
for _, e := range []*dataelem.DataElement{rows, spacing, name} {
b, ok := e.Bytes()
fmt.Printf("%s %s %q %v\n", e.MustTag(), e.GetVR(), b, ok)
}
// A value its VR cannot hold is refused when it is encoded.
if _, err := dataelem.ValueBytes(dataelem.US, int32(-1)); err != nil {
fmt.Println("refused:", err)
}
ds := dataset.NewDataset()
for _, e := range []*dataelem.DataElement{rows, spacing, name} {
if err := ds.Add(e); err != nil {
log.Fatal(err)
}
}
fmt.Println("Elements:", ds.Length())
}It prints (0028,0010) US "\x00\x02" true, (0028,0030) DS "0.5\\0.5" true,
(0010,0010) PN "Doe^Jane" true, the refusal of -1 for US, and
Elements: 3.
Notes
- A file read holds
[]byte; a data set built in code may hold any formValueBytesaccepts. AssertingGetValue().([]byte)reads every other form as absent, which is whyBytesexists. - A sequence's value is a
*sequence.Sequenceof*dataset.Dataset;Bytesreports false for it, as for aValueInFile. - Element methods take a lock for each call.
Note
Besides the element, the package holds older helpers that nothing in the
reading or writing path needs: BatchOperation, BufferedDataElement,
CacheableDataElement, ValueCache, RawDataElement, SequenceItem. Its
ParsePersonName goes through valuerep, which splits names
wrongly; use GetPersonNameValue instead.
See also
config
Library-wide settings: where the library logs, and how strictly values are validated when read and written, either for the whole program or for one operation through a context.Context.
dataset
A data set in memory: the elements of one DICOM object, keyed by tag, with sequences of nested data sets, and the operations that work on the whole object: reading and changing attributes, decoding, rendering and compressing its pixels, transcoding, and the DICOM JSON and XML forms.