Dicom.P10.Stream (Dicom v0.5.1)

Copy Markdown View Source

Streaming DICOM P10 parser.

Provides lazy, event-based parsing of DICOM P10 data as Elixir streams. This enables processing DICOM files without loading the entire content into memory, and composing with standard Stream and Enum functions.

Binary Streaming

events = Dicom.P10.Stream.parse(binary)
Enum.each(events, fn
  {:element, elem} -> IO.inspect(elem.tag)
  _ -> :ok
end)

File Streaming

events = Dicom.P10.Stream.parse_file("/path/to/image.dcm")
Enum.each(events, fn event -> process(event) end)

Materialization

{:ok, data_set} = Dicom.P10.Stream.to_data_set(events)

Reference: DICOM PS3.5, PS3.10.

Summary

Functions

Parses a DICOM P10 binary into a lazy stream of events.

Parses a DICOM P10 file into a lazy stream of events.

Materializes a stream of events into a Dicom.DataSet.

Functions

parse(binary)

@spec parse(binary()) :: Enumerable.t()

Parses a DICOM P10 binary into a lazy stream of events.

Uses Stream.unfold/2 to emit events one at a time.

Examples

events = Dicom.P10.Stream.parse(binary)
tags = events
       |> Stream.filter(&match?({:element, _}, &1))
       |> Enum.map(fn {:element, elem} -> elem.tag end)

parse_file(path, opts \\ [])

@spec parse_file(
  Path.t(),
  keyword()
) :: Enumerable.t()

Parses a DICOM P10 file into a lazy stream of events.

Opens the file with :raw, :binary, :read mode and uses Stream.resource/3 for proper resource management (the file handle is closed when the stream is consumed or halted).

Options

  • :read_ahead -- read-ahead buffer size in bytes (default: 65536)

Examples

events = Dicom.P10.Stream.parse_file("/path/to/image.dcm")
{:ok, data_set} = Dicom.P10.Stream.to_data_set(events)

to_data_set(events)

@spec to_data_set(Enumerable.t()) :: {:ok, Dicom.DataSet.t()} | {:error, term()}

Materializes a stream of events into a Dicom.DataSet.

Collects all events from the stream and reconstructs the data set, including file meta information, data elements, sequences, and encapsulated pixel data.

Examples

events = Dicom.P10.Stream.parse(binary)
{:ok, data_set} = Dicom.P10.Stream.to_data_set(events)