# `Dicom.Codec.RLE`
[🔗](https://github.com/Balneario-de-Cofrentes/dicom/blob/v0.9.2/lib/dicom/codec/rle.ex#L1)

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.

# `decode`

```elixir
@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`

```elixir
@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.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
