util
Debugging helpers: hex dumps, a printed data set, a small summary of common attributes, and a fix for values that use the wrong separator.
import "github.com/amrshadid/go-dicom/util" · pkg.go.dev
When to use it
When you are looking at bytes or a data set by hand: a hex dump of the start of a file, or a quick print of patient, modality and image size.
For a full listing of a file, the command line's show is more complete. For
image attributes in code, use dataset.Dataset.GetPixelDataInfo.
Key types and functions
| Name | What it is for |
|---|---|
HexDump, HexDumpReader | Offset, hex and ASCII columns, from bytes or a reader. |
Bytes2Hex, Hex2Bytes | Bytes as hex text and back. |
GetDatasetInfo, DatasetInfo | Patient, study date, modality, numbers, rows, columns, bits and UIDs. |
PrintDatasetInfo | Print that summary. |
DumpDataset, PrettyPrint | A data set as text. |
FixSeparator, FixSeparatorConfig | Replace a wrong separator with a backslash in a copy of a data set. |
Example
// Dump the first bytes of a file the way hexdump -C does, and print a summary.
package main
import (
"bytes"
"fmt"
"log"
"os"
"github.com/amrshadid/go-dicom/filebase"
"github.com/amrshadid/go-dicom/filereader"
"github.com/amrshadid/go-dicom/util"
)
func main() {
raw, err := os.ReadFile(os.Args[1])
if err != nil {
log.Fatal(err)
}
fmt.Println(util.HexDump(raw[128:176], 128, 0)) // after the preamble
df, err := filereader.ReadDICOMFile(filebase.NewFileReader(bytes.NewReader(raw)))
if err != nil {
log.Fatal(err)
}
info, err := util.GetDatasetInfo(df.GetDataset())
if err != nil {
log.Fatal(err)
}
fmt.Println(info.Modality, info.Rows, "x", info.Columns)
}On CT_small.dcm it prints three lines of hex starting
00000080 44 49 43 4D (DICM) and then CT 128 x 128.
Notes
GetDatasetInforeturns values without trimming their padding: a PN may end in a space and a UI in a NUL. Usestrings.TrimRightbefore comparing.- No other package in the module uses
util.
Note
This is a thin helper package. Prefer GetStringByKeyword and
GetPixelDataInfo on the dataset for values you act on.
See also
uid
UIDs: generating new ones, the registry of well-known UIDs (transfer syntaxes, SOP classes and the rest) with their names and keywords, and a few questions about transfer syntaxes.
validate
Checks a data set against the IOD its SOP class requires, with module and attribute tables generated from PS3.3 and PS3.4.