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, thenGetDataset. - Read files you do not trust, with bounds:
ReadDICOMFileWithOptionsandReadOptions(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 (GetDatasetWithValuesInFilewith filewriter'sSetValueSource). - 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
| Name | What it is for |
|---|---|
ReadDICOMFile | Read a whole file into a DICOMFile. |
ReadDICOMFileWithOptions, ReadOptions | The same, with bulk data skipped, values and header bounded, a logger of its own, or a Visit callback. |
DICOMFile | What was read: FileMetaInfo, DataElements, Warnings, Truncated, Overrun, TagOrderViolations. |
DICOMFile.GetDataset | Build a dataset.Dataset, sequences included, text decoded to UTF-8. |
DICOMFile.GetDatasetWithValuesInFile, GetDatasetWithValuesIn | A data set whose skipped values are references into the file, for writing back without loading them. |
DICOMFile.FrameRanges | Where each frame of Pixel Data lies in the file. |
DICOMFile.FrameReader, FrameReader | Read one frame, or a band of rows, from the file; a dataset.FrameReader. |
FileMetaInfo | The File Meta group: SOP Class and Instance UID, transfer syntax, implementation. |
DataElementValue | One element as read: tag, VR, bytes, position in the file. |
Visit, VisitAction, ErrStopVisit | See each element as it is read and keep or discard it. |
ErrValueTooLong, ErrHeaderTooLarge, ErrTruncated | Errors to test with errors.Is. |
ErrorKind, ReadErrorKind | What went wrong, as one of a fixed set, without parsing an error's text. |
TextEncodings, DecodeText | Decode 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.HasPreambleis false). A deflated data set is inflated, bounded byMaxInflatedDatasetSize(256 MiB). - Problems that do not stop the read are collected, not returned:
Warnings, and the flagsTruncatedandOverrun. Check them when the input matters. ReadOptions.Loggersends everything a read logs, including the validationGetDatasetruns later, to that logger alone;slog.New(slog.DiscardHandler)silences it. Log lines name tags and offsets, never values.- A
DICOMFileis 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