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, thenDICOMFileWriter. - Write back a data set read with
SkipBulkDatawithout 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
| Name | What it is for |
|---|---|
NewDICOMFileWriter, DICOMFileWriter | The writer for a whole file: meta group, elements, Write. |
DICOMFileWriter.SetFileMetaInfo, FileMetaInfo | The SOP Class and Instance UID and the transfer syntax to write. |
ElementsFromDataset | Turn a data set into elements to write, sequences included, each with a resolved VR. |
DICOMFileWriter.AddDataElements, Write | Queue the elements, then write the file. |
StorageTransferSyntax | The syntax to store a data set in: Explicit VR Little Endian for native data, the arrival syntax for compressed. |
DICOMFileWriter.SetValueSource | The file the values left in it are copied from. |
EncodeFileHeader | Preamble, DICM and meta group as bytes, for a data set already encoded. |
DataElement | An element to write: bytes, nested Items, an InFile reference or a Stream. |
SetValidationMode, ValidationMode | How 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
ElementsFromDatasetis the conversion to use. A hand-written one that copiesValueand ignoresItemswrites 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.TransferSyntaxUIDnames. 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. FileMetaInformationVersiondefaults to00 01. How sequences, items and encapsulated pixel data are written is in CONFORMANCE.md §8.3.NewDICOMFileWritertakes afilebase.Writer; wrap an*os.Filewithfilebase.NewFileWriter. Close the file yourself afterWrite.
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