Skip to Content
go-dicom(3)
Packages

filereader

Reads DICOM files: the preamble, the File Meta group and the data set, in any of the native transfer syntaxes or a compressed one, from a file or a stream. It returns the elements as the file holds them, and builds a dataset.Dataset from them on request.

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

When to use it

  • Open a file and get a data set: ReadDICOMFile, then GetDataset.
  • Read files you do not trust, with bounds: ReadDICOMFileWithOptions and ReadOptions (MaxValueLength, MaxHeaderBytes, SkipBulkData).
  • Read the header of a large image and leave its pixels on disk, then read one frame at a time (FrameRanges, FrameReader), or change attributes and write the file back without loading the pixels (GetDatasetWithValuesInFile with filewriter's SetValueSource).
  • Read a header of a million per-frame items element by element (Visit).

A program that only indexes or lists attributes can use filereader/attributes: the same parser and types, without linking the codecs. Writing is filewriter.

Key types and functions

NameWhat it is for
ReadDICOMFileRead a whole file into a DICOMFile.
ReadDICOMFileWithOptions, ReadOptionsThe same, with bulk data skipped, values and header bounded, a logger of its own, or a Visit callback.
DICOMFileWhat was read: FileMetaInfo, DataElements, Warnings, Truncated, Overrun, TagOrderViolations.
DICOMFile.GetDatasetBuild a dataset.Dataset, sequences included, text decoded to UTF-8.
DICOMFile.GetDatasetWithValuesInFile, GetDatasetWithValuesInA data set whose skipped values are references into the file, for writing back without loading them.
DICOMFile.FrameRangesWhere each frame of Pixel Data lies in the file.
DICOMFile.FrameReader, FrameReaderRead one frame, or a band of rows, from the file; a dataset.FrameReader.
FileMetaInfoThe File Meta group: SOP Class and Instance UID, transfer syntax, implementation.
DataElementValueOne element as read: tag, VR, bytes, position in the file.
Visit, VisitAction, ErrStopVisitSee each element as it is read and keep or discard it.
ErrValueTooLong, ErrHeaderTooLarge, ErrTruncatedErrors to test with errors.Is.
ErrorKind, ReadErrorKindWhat went wrong, as one of a fixed set, without parsing an error's text.
TextEncodings, DecodeTextDecode the text of raw elements, as GetDataset does.

Example

// Read a file's header without its pixel data, then find where each frame lies.
package main

import (
	"fmt"
	"log"
	"os"

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

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

	df, err := filereader.ReadDICOMFileWithOptions(filebase.NewFileReader(f), filereader.ReadOptions{
		SkipBulkData:   true,    // leave Pixel Data in the file
		MaxValueLength: 1 << 20, // refuse any other value over 1 MiB
	})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Transfer syntax:", df.FileMetaInfo.TransferSyntaxUID)
	fmt.Println("Elements:", len(df.DataElements), "warnings:", len(df.Warnings))

	ds := df.GetDataset() // text decoded to UTF-8; skipped values are absent
	fmt.Println("Patient:", ds.GetStringByKeyword("PatientName"))

	frames, err := df.FrameRanges(f) // byte ranges of each frame in the file
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Frames:", len(frames), "first at offset", frames[0][0].Offset)
}

On CT_small.dcm it prints the transfer syntax 1.2.840.10008.1.2.1, 258 elements, the patient name and Frames: 1 first at offset 6300.

Notes

  • A file without the preamble and meta group is read as a raw data set in Implicit VR Little Endian (DICOMFile.HasPreamble is false). A deflated data set is inflated, bounded by MaxInflatedDatasetSize (256 MiB).
  • Problems that do not stop the read are collected, not returned: Warnings, and the flags Truncated and Overrun. Check them when the input matters.
  • ReadOptions.Logger sends everything a read logs, including the validation GetDataset runs later, to that logger alone; slog.New(slog.DiscardHandler) silences it. Log lines name tags and offsets, never values.
  • A DICOMFile is not safe for concurrent use while it is being read; read each file on its own goroutine.
  • The enforced limits are listed in CONFORMANCE.md §7; truncated files in §8.4.

Warning

Without MaxValueLength or MaxHeaderBytes a read holds whatever the file declares, up to the bytes it actually has. Set them when the file comes from a peer or an upload.

Note

DCMFileReader, NewDCMFileReader and its RegisterHook are the older, step-by-step reader. ReadDICOMFile does not go through them; new code has no reason to.

See also

filereader/attributes · dataset · filewriter · filebase · charset · Reading and writing · Pixel data · Examples

On this page