Skip to Content
go-dicom(3)
Packages

filewriter

Writes DICOM Part 10 files: the preamble, the File Meta group and the data set, encoded in the transfer syntax the meta group names.

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

When to use it

  • Save a dataset.Dataset: ElementsFromDataset, then DICOMFileWriter.
  • Write back a data set read with SkipBulkData without loading its pixels: the skipped values are copied from the source file (SetValueSource).
  • Put a Part 10 header in front of a data set that is already encoded, such as one received over the network (EncodeFileHeader).

Reading is filereader. Writing a DICOMDIR is fileset. Changing pixel encoding before writing is dataset.Dataset.Transcode or DecompressInPlace.

Key types and functions

NameWhat it is for
NewDICOMFileWriter, DICOMFileWriterThe writer for a whole file: meta group, elements, Write.
DICOMFileWriter.SetFileMetaInfo, FileMetaInfoThe SOP Class and Instance UID and the transfer syntax to write.
ElementsFromDatasetTurn a data set into elements to write, sequences included, each with a resolved VR.
DICOMFileWriter.AddDataElements, WriteQueue the elements, then write the file.
StorageTransferSyntaxThe syntax to store a data set in: Explicit VR Little Endian for native data, the arrival syntax for compressed.
DICOMFileWriter.SetValueSourceThe file the values left in it are copied from.
EncodeFileHeaderPreamble, DICM and meta group as bytes, for a data set already encoded.
DataElementAn element to write: bytes, nested Items, an InFile reference or a Stream.
SetValidationMode, ValidationModeHow much the writer checks: none, warn (default) or strict.

Example

// Read a file, change one attribute and write it back as a new Part 10 file.
package main

import (
	"log"
	"os"

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

func main() {
	in, err := os.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}
	defer in.Close()
	df, err := filereader.ReadDICOMFile(filebase.NewFileReader(in))
	if err != nil {
		log.Fatal(err)
	}
	ds := df.GetDataset()
	if err := ds.SetStringByKeyword("PatientID", "ANON-0001"); err != nil {
		log.Fatal(err)
	}

	out, err := os.Create(os.Args[2])
	if err != nil {
		log.Fatal(err)
	}
	w := filewriter.NewDICOMFileWriter(filebase.NewFileWriter(out))
	w.SetFileMetaInfo(&filewriter.FileMetaInfo{
		MediaStorageSOPClassUID:    ds.GetStringByKeyword("SOPClassUID"),
		MediaStorageSOPInstanceUID: ds.GetStringByKeyword("SOPInstanceUID"),
		TransferSyntaxUID:          filewriter.StorageTransferSyntax(ds),
	})
	if err := w.AddDataElements(filewriter.ElementsFromDataset(ds)); err != nil {
		log.Fatal(err)
	}
	if err := w.Write(); err != nil {
		log.Fatal(err)
	}
	if err := out.Close(); err != nil {
		log.Fatal(err)
	}
}

Run as go run . CT_small.dcm out.dcm; reading out.dcm back gives the same data set with the new Patient ID.

Notes

  • ElementsFromDataset is the conversion to use. A hand-written one that copies Value and ignores Items writes sequences with no items and no error.
  • The values of a data set are held little endian whatever the source; the writer encodes them in the syntax FileMetaInfo.TransferSyntaxUID names. Explicit VR Little Endian, Implicit VR Little Endian, Explicit VR Big Endian and Deflated are written; compressed pixel data is written as it is held.
  • FileMetaInformationVersion defaults to 00 01. How sequences, items and encapsulated pixel data are written is in CONFORMANCE.md §8.3.
  • NewDICOMFileWriter takes a filebase.Writer; wrap an *os.File with filebase.NewFileWriter. Close the file yourself after Write.

Note

DCMFileWriter (preamble, prefix, meta and elements written step by step) and the …WithCharset, …WithWaveforms and …WithValidation wrappers are older layers. DICOMFileWriter with ElementsFromDataset covers what they do for a data set.

See also

filereader · dataset · filebase · fileset · Reading and writing · Examples

On this page