# Module /image/png

Lossless PNG image decoder and encoder for files and byte slabs.

```tangu
(use /image/png)
```

## Functions & symbols

### Coder

`(Coder width height encoding writable [options]) -> Coder`

Creates a streaming PNG encoder writing rows directly to `writable`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `width` | `Int` | no | Output width in pixels |
| `height` | `Int` | no | Output height in pixels |
| `encoding` | `KeySymbol | String` | no | Input pixel encoding (:rgb8, :rgba8, :luma8) |
| `writable` | `Stream | File` | no | Destination writable stream or file handle |
| `options` | `Map` | yes | Optional PNG encoder settings (e.g. :quality) |

```tangu
(let coder (png/Coder 800 600 :rgba8 out-file))
```

### decode-from-file

`(decode-from-file path) -> Image`

Decodes a PNG file from disk into an in-memory `Image`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `path` | `String` | no | Path to input .png file |

```tangu
(let img (png/decode-from-file "photo.png"))
```

### decode-from-slab

`(decode-from-slab data) -> Image`

Decodes PNG bytes from a `Slab` into an `Image`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `data` | `Slab` | no | Encoded PNG byte Slab |

```tangu
(let img (png/decode-from-slab bytes))
```

### encode-to-file

`(encode-to-file path image [options]) -> None`

Encodes `image` as PNG and writes the result directly to `path`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `path` | `String` | no | Destination file path |
| `image` | `Image` | no | Image map or Image value to encode |
| `options` | `Map` | yes | Optional PNG encoder options |

```tangu
(png/encode-to-file "out.png" img)
```

### encode-to-slab

`(encode-to-slab image [options]) -> Slab`

Encodes `image` into an in-memory PNG byte `Slab`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `image` | `Image` | no | Image map or Image value to encode |
| `options` | `Map` | yes | Optional PNG encoder options |

```tangu
(let encoded (png/encode-to-slab img))
```

### write

`(write coder slab) -> None`

Writes encoded pixel bytes through an open PNG streaming `Coder`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `coder` | `Coder` | no | Active streaming Coder instance |
| `slab` | `Slab` | no | Packed pixel buffer Slab to encode |

```tangu
(png/write coder pixels)
```
