DICOM value decoding and encoding.
Converts between raw binary data element values and native Elixir types based on the Value Representation (VR).
Reference: DICOM PS3.5 Section 6.2.
Summary
Functions
Decodes a raw binary value to a native Elixir type based on VR.
Decodes a raw binary value using the given endianness.
Encodes a native Elixir value to binary for a given VR.
Encodes a native Elixir value using the given endianness.
Converts an Elixir Date to a DICOM DA string ("YYYYMMDD").
Converts a DateTime or NaiveDateTime to a DICOM DT string.
Converts an Elixir Time to a DICOM TM string.
Converts a DICOM DA string ("YYYYMMDD") to an Elixir Date.
Converts a DICOM DT string to DateTime (with TZ offset) or NaiveDateTime (without).
Converts a DICOM TM string to an Elixir Time.
Functions
@spec decode(binary(), Dicom.VR.t()) :: term()
Decodes a raw binary value to a native Elixir type based on VR.
Returns nil for empty binaries.
@spec decode(binary(), Dicom.VR.t(), :little | :big) :: term()
Decodes a raw binary value using the given endianness.
@spec encode(term(), Dicom.VR.t()) :: binary()
Encodes a native Elixir value to binary for a given VR.
@spec encode(term(), Dicom.VR.t(), :little | :big) :: binary()
Encodes a native Elixir value using the given endianness.
Converts an Elixir Date to a DICOM DA string ("YYYYMMDD").
Examples
iex> Dicom.Value.from_date(~D[2024-03-15])
"20240315"
@spec from_datetime(DateTime.t() | NaiveDateTime.t()) :: String.t()
Converts a DateTime or NaiveDateTime to a DICOM DT string.
Examples
iex> Dicom.Value.from_datetime(~N[2024-03-15 14:30:22])
"20240315143022"
Converts an Elixir Time to a DICOM TM string.
Includes fractional seconds if microsecond precision > 0.
Examples
iex> Dicom.Value.from_time(~T[14:30:22])
"143022"
Converts a DICOM DA string ("YYYYMMDD") to an Elixir Date.
Examples
iex> Dicom.Value.to_date("20240315")
{:ok, ~D[2024-03-15]}
iex> Dicom.Value.to_date("invalid")
{:error, :invalid_date}
@spec to_datetime(String.t()) :: {:ok, DateTime.t() | NaiveDateTime.t()} | {:error, :invalid_datetime}
Converts a DICOM DT string to DateTime (with TZ offset) or NaiveDateTime (without).
Examples
iex> Dicom.Value.to_datetime("20240315143022")
{:ok, ~N[2024-03-15 14:30:22]}
Converts a DICOM TM string to an Elixir Time.
Supports full ("HHMMSS.FFFFFF") and partial ("HHMM", "HH") formats.
Examples
iex> Dicom.Value.to_time("143022")
{:ok, ~T[14:30:22]}
iex> Dicom.Value.to_time("1430")
{:ok, ~T[14:30:00]}