Dicom.Codec.RLE (Dicom v0.9.2)

Copy Markdown View Source

DICOM RLE Lossless codec per PS3.5 Annex G.

Transfer Syntax UID: 1.2.840.10008.1.2.5

RLE Format

Each RLE-compressed frame consists of:

  1. RLE Header (64 bytes):

    • uint32 number of segments (1..15)
    • 15 uint32 segment offsets (unused offsets are zero)
  2. Segments: One per byte plane. For single-byte pixels (8-bit), there is one segment. For 16-bit pixels, segment 1 contains all high bytes and segment 2 all low bytes (byte-plane separation).

  3. RLE Encoding (per segment):

    • n in 0..127: copy the next n + 1 bytes literally
    • n = -128 (0x80): no operation (skip)
    • n in -127..-1: repeat the next byte 1 - n times

All multi-byte integers in the header are little-endian unsigned 32-bit.

Reference: DICOM PS3.5 Annex G.

Summary

Functions

Decodes an RLE-compressed frame to raw pixel data.

Encodes raw pixel data to RLE-compressed form.

Functions

decode(data, metadata)

@spec decode(binary(), Dicom.Codec.metadata()) :: {:ok, binary()} | {:error, term()}

Decodes an RLE-compressed frame to raw pixel data.

Requires :bits_allocated and :samples_per_pixel in metadata to determine the expected number of segments and output size. Requires :rows and :columns to compute total pixel count.

encode(data, metadata)

@spec encode(binary(), Dicom.Codec.metadata()) :: {:ok, binary()} | {:error, term()}

Encodes raw pixel data to RLE-compressed form.

Requires :bits_allocated and :samples_per_pixel in metadata to determine byte-plane segmentation.