Skip to Content
go-dicom(3)
Packages

filereader/attributes

The file parser alone: reads the File Meta group and the data set of a DICOM file, and writes their DICOM JSON, without linking the packages that build a data set or decode pixels.

import "github.com/amrshadid/go-dicom/filereader/attributes" · pkg.go.dev

When to use it

  • An indexer, a header dump or a gateway that reads attributes and never touches pixels, and should stay small: its dependencies are charset, config, errors, filebase, tag and uid, and a test fails if a codec package joins them.
  • The DICOM JSON of a file's header (DICOMJSON), with your own choice of which values become a BulkDataURI.

Use filereader when you need a dataset.Dataset, frames or waveforms. The two return the same types: File has the fields of filereader.DICOMFile, and ReadOptions, DataElementValue and the rest are filereader's under aliases.

Key types and functions

NameWhat it is for
ReadRead a file; the same parser and options as filereader.ReadDICOMFileWithOptions.
ReadOptionsSkipBulkData, MaxValueLength, MaxHeaderBytes, Visit, Logger.
FileThe result: FileMetaInfo, DataElements, Warnings and the flags.
DataElementValueOne element as read: tag, VR, raw bytes, position, nested items.
TextEncodings, DecodeTextThe character sets in force, and a value decoded to UTF-8.
TextEncodingsWithContext, DecodeTextWithContextThe same, logging to the context's logger.
DICOMJSON, JSONOptionsThe DICOM JSON Model (PS3.18 Annex F) of the elements, with a bulk data hook.
ErrValueTooLong, ErrHeaderTooLargeRefusals by policy, for errors.Is.
ErrorKindThe kind of a read error.

Example

// Print every top-level attribute of a file, text decoded to UTF-8,
// without linking the pixel codecs.
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/amrshadid/go-dicom/filebase"
	"github.com/amrshadid/go-dicom/filereader/attributes"
)

func main() {
	f, err := os.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	file, err := attributes.Read(filebase.NewFileReader(f), attributes.ReadOptions{SkipBulkData: true})
	if err != nil {
		log.Fatal(err)
	}
	encodings := attributes.TextEncodings(file.DataElements, nil)
	for _, e := range file.DataElements {
		if e.ValueSkipped {
			fmt.Println(e.Tag, e.VR, "(left in the file)")
			continue
		}
		if e.VR == "PN" || e.VR == "LO" || e.VR == "CS" || e.VR == "DA" {
			fmt.Println(e.Tag, e.VR, attributes.DecodeText(e.VR, e.Value, encodings))
		}
	}
}

Notes

  • Values are the file's bytes. Text is in the file's character set until you pass it through DecodeText, which applies the rules GetDataset applies.
  • DICOMJSON is the writer behind dataset.Dataset.ToDICOMJSON, not a copy of it, so both give the same document for the same file.
  • Limits and their errors are those of filereader; see CONFORMANCE.md §7.

Tip

A value from one package is a value of the other. A program can start with this package and move to filereader later without changing its types.

See also

filereader · charset · dataset · Reading and writing · Character sets

On this page