Skip to Content
go-dicom(3)
Packages

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

NameWhat it is for
ConvertEncodingsThe values of (0008,0005) as encodings the other calls take.
DecodeBytesBytes to UTF-8, following escape sequences, reset at the delimiters given.
EncodeStringUTF-8 to bytes, with the escape sequences the encodings need.
DefaultTextDelimiters, PersonNameDelimitersWhere ISO 2022 switches back to the default set.
PersonName, NewPersonNameA PN value: Alphabetic, Ideographic, Phonetic, and FamilyName, GivenName…
DecodePersonName, EncodePersonNameA PN value from bytes and back.
CharacterSet, NewCharacterSetThe encodings of one data set, as the data set and element methods take them.
ConvertEncodingsWithContext, DecodeBytesWithContextThe same, with the validation mode and logger of a context.
GetSupportedEncodingsThe 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

On this page