Skip to Content
go-dicom(3)
Packages

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.US and 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

NameWhat it is for
DataElementOne element: VR, Value, Keyword, and the tag behind Tag().
NewDataElementMake one. The tag may be a tag.Tag or a uint32.
DataElement.BytesThe bytes the writer would write, whatever the value is held as.
ValueBytesEncode a Go value for a VR; a value that does not fit is an error.
DataElement.MustTag, TagThe element's tag.
DataElement.GetVR, GetValue, SetValueRead and change the VR and value.
DataElement.GetTextValue, GetPersonNameValueDecode text under a character set.
VR, AllVRs, GetVRInfoThe VR constants and what each is.
ValueInFileA 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 form ValueBytes accepts. Asserting GetValue().([]byte) reads every other form as absent, which is why Bytes exists.
  • A sequence's value is a *sequence.Sequence of *dataset.Dataset; Bytes reports false for it, as for a ValueInFile.
  • 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

dataset · tag · valuerep · charset

On this page