charset
Text in the character sets of DICOM (PS3.5 6.1, PS3.3 C.12.1.1.2): turning Specific Character Set (0008,0005) into encodings, decoding bytes to UTF-8 and encoding UTF-8 back, with ISO 2022 escape sequences, and person names with their three component groups.
import "github.com/amrshadid/go-dicom/charset" · pkg.go.dev
When to use it
Usually indirectly: GetDataset in filereader decodes every
text value to UTF-8 by these rules, and matching and
dcmstore compare text through them. Call it directly to:
- decode raw element bytes yourself (or use
attributes.DecodeText); - write text in a character set other than UTF-8;
- take a person name apart into its alphabetic, ideographic and phonetic groups and their components.
Key types and functions
| Name | What it is for |
|---|---|
ConvertEncodings | The values of (0008,0005) as encodings the other calls take. |
DecodeBytes | Bytes to UTF-8, following escape sequences, reset at the delimiters given. |
EncodeString | UTF-8 to bytes, with the escape sequences the encodings need. |
DefaultTextDelimiters, PersonNameDelimiters | Where ISO 2022 switches back to the default set. |
PersonName, NewPersonName | A PN value: Alphabetic, Ideographic, Phonetic, and FamilyName, GivenName… |
DecodePersonName, EncodePersonName | A PN value from bytes and back. |
CharacterSet, NewCharacterSet | The encodings of one data set, as the data set and element methods take them. |
ConvertEncodingsWithContext, DecodeBytesWithContext | The same, with the validation mode and logger of a context. |
GetSupportedEncodings | The defined terms understood. |
Example
// Encode text in a declared character set and decode it back.
package main
import (
"fmt"
"log"
"github.com/amrshadid/go-dicom/charset"
)
func main() {
// The values of Specific Character Set (0008,0005), as a file declares them.
encodings, err := charset.ConvertEncodings([]string{"ISO_IR 144"}) // Cyrillic
if err != nil {
log.Fatal(err)
}
raw, err := charset.EncodeString("Люксембург", encodings)
if err != nil {
log.Fatal(err)
}
fmt.Printf("% X\n", raw)
text, err := charset.DecodeBytes(raw, encodings, charset.DefaultTextDelimiters)
if err != nil {
log.Fatal(err)
}
fmt.Println(text)
// A person name with an ideographic group, in ISO 2022 Japanese.
jp, err := charset.ConvertEncodings([]string{"", "ISO 2022 IR 87"})
if err != nil {
log.Fatal(err)
}
pnBytes, err := charset.EncodePersonName(charset.NewPersonName("Yamada^Tarou", "山田^太郎", ""), jp)
if err != nil {
log.Fatal(err)
}
pn, err := charset.DecodePersonName(pnBytes, jp)
if err != nil {
log.Fatal(err)
}
fmt.Println(pn.Alphabetic, "|", pn.Ideographic)
}It prints BB EE DA E1 D5 DC D1 E3 E0 D3, Люксембург and
Yamada^Tarou | 山田^太郎.
Notes
- An unknown or empty defined term is, by default, logged and read as the
default repertoire. Under
config.RAISE(config.WithReadingValidationMode) it is an error, a*errors.DicomUnicodeDecodeError. - The functions keep no shared state except an optional cache
(
EnableCache); they are safe for concurrent use. - What is supported, and how text that cannot be encoded is handled, is in CONFORMANCE.md §6.
Tip
To change the text of an element, prefer the data set: GetDataset has
already decoded it to UTF-8 and declared ISO_IR 192. Encode to another set
only when a receiver needs it.
See also
filereader · filereader/attributes · config · errors · Character sets
anonymize
Removes identifying information from data sets by the Basic Application Level Confidentiality Profile of PS3.15 Annex E and its options, remaps UIDs consistently, and finds and fills text burned into the pixels.
compress
The pixel codecs and the encapsulation format. Every compressed transfer syntax but the video ones is decoded here in pure Go, with no CGO: RLE, JPEG Baseline and Extended, JPEG Lossless, JPEG-LS, JPEG 2000 and HTJ2K, JPEG XL and Deflated Image Frame Compression. Sixteen syntaxes are also encoded.