# C API: 2D outputs

Sprite sheets of a clip at N frames in M directions with Aseprite and TexturePacker atlases, outlined stickers, and crisp pixel art with a small palette. Sheets and stickers draw through `sona_snapshot` with the avatar staged alone at the origin, and put it back as it was (placement, clip and its time, pose, events, visibility, crowd step). A clip named in their options is read from the ctx's sources without touching the avatar's clip: `SONA_PENDING` until its file arrives (run `sona_update`, call again). The guide, with every field and the numbers: [2D outputs](../../guides/sprites2d.md).

Every options struct starts with `size`, set by its `_defaults` call; a struct without it is refused with `SONA_E_ARGUMENT`. `sona_picture` holds `rgba`, `width` and `height`: straight RGBA8, row 0 at the top.

## sona_pixel_defaults

```c
void sona_pixel_defaults(sona_pixel_options *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `o` | `sona_pixel_options *` | The [`sona_pixel_options`](../fields.md#sona_pixel_options) to fill with the defaults | required |

**Returns** Nothing.

**Example**:

```c
sona_pixel_options x_pixel_options;
memset(&x_pixel_options, 0, sizeof x_pixel_options);
x_pixel_options.size = sizeof x_pixel_options;
sona_pixel_defaults(&x_pixel_options);
```

`size` set, `height` 32, `colours` 16, `flags` `SONA_PIXEL_OUTLINE`, `outline` 0 (from the palette), `alpha_cut` 0.5, no palette. NULL does nothing.

## sona_pixel_art

```c
sona_result sona_pixel_art(sona_ctx *ctx, const sona_picture *in, const sona_pixel_options *o, sona_picture *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `in` | `const sona_picture *` | A [`sona_picture`](../fields.md#sona_picture) | required |
| `o` | `const sona_pixel_options *` | The options, a [`sona_pixel_options`](../fields.md#sona_pixel_options) | required |
| `out` | `sona_picture *` | Receives the [`sona_picture`](../fields.md#sona_picture) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_picture x_picture;
memset(&x_picture, 0, sizeof x_picture);
sona_pixel_options x_pixel_options2;
memset(&x_pixel_options2, 0, sizeof x_pixel_options2);
x_pixel_options2.size = sizeof x_pixel_options2;
sona_picture x_picture3;
memset(&x_picture3, 0, sizeof x_picture3);
sona_result result = sona_pixel_art(ctx, &x_picture, &x_pixel_options2, &x_picture3);
if (result != SONA_OK) printf("sona_pixel_art: %s\n", sona_result_name(result));
```

The figure's box of `in` (alpha 8 and up; the whole picture with `SONA_PIXEL_KEEP_FRAME`) scaled with an area filter to exactly `o->height` pixels including the outline, every pixel opaque or clear at `alpha_cut`, colours mapped to `o->palette` or to `o->colours` colours made from the picture, optionally dithered (`SONA_PIXEL_DITHER`) and outlined (`SONA_PIXEL_OUTLINE`, `_OUTLINE_THIN`). `out->rgba` is freed with `sona_free`. `SONA_E_NOT_FOUND` when no pixel reaches alpha 8 or none is opaque enough at that size; `SONA_E_ARGUMENT` for a value out of range or a picture too wide for the height (over 4096 pixels).

## sona_pixel_palette

```c
sona_result sona_pixel_palette(sona_ctx *ctx, const sona_picture *images, uint32_t image_count, uint32_t colours,
                               uint32_t palette[256], uint32_t *count);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `images` | `const sona_picture *` | A [`sona_picture`](../fields.md#sona_picture) | required |
| `image_count` | `uint32_t` | How many pictures `images` holds | - |
| `colours` | `uint32_t` | How many colours the palette may hold | - |
| `palette` | `uint32_t [256]` | Receives up to 256 colours | required |
| `count` | `uint32_t *` | Receives how many entries there are; may be NULL | `NULL`: not written |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

One palette of up to `colours` (2..256) entries over every pixel of alpha 128 and up in the pictures: median cut, then k-means, integer maths. Entries are `0xFFRRGGBB`, darkest first; `*count` may be below `colours` when the pictures hold fewer colours. Pass it as `sona_pixel_options.palette` so frames share colours. `SONA_E_NOT_FOUND` when no pixel counts.

## sona_sticker_defaults

```c
void sona_sticker_defaults(sona_sticker_options *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `o` | `sona_sticker_options *` | The [`sona_sticker_options`](../fields.md#sona_sticker_options) to fill with the defaults | required |

**Returns** Nothing.

**Example**:

```c
sona_sticker_options x_sticker_options;
memset(&x_sticker_options, 0, sizeof x_sticker_options);
x_sticker_options.size = sizeof x_sticker_options;
sona_sticker_defaults(&x_sticker_options);
```

`size` set, `pixels` 512, `time` -1, `framing` `SONA_PRESET_FULL`, `outline_px` 10, `outline` `0xffffffff`, `shadow` `0x59000000` (off while `shadow_px` is 0), `padding` 8. NULL does nothing.

## sona_sticker

```c
sona_result sona_sticker(sona_renderer *r, sona_avatar *a, const sona_sticker_options *o, sona_picture *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `r` | `sona_renderer *` | The renderer | required |
| `a` | `sona_avatar *` | The avatar | required |
| `o` | `const sona_sticker_options *` | The options, a [`sona_sticker_options`](../fields.md#sona_sticker_options) | required |
| `out` | `sona_picture *` | Receives the [`sona_picture`](../fields.md#sona_picture) | required |

**Returns** `SONA_OK`, `SONA_PENDING` while it is still working. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_NOT_READY`, `SONA_E_NOT_FOUND`, `SONA_E_MEMORY`, `SONA_E_CONTEXT`, `SONA_E_READBACK` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_sticker_options x_sticker_options;
memset(&x_sticker_options, 0, sizeof x_sticker_options);
x_sticker_options.size = sizeof x_sticker_options;
sona_picture x_picture2;
memset(&x_picture2, 0, sizeof x_picture2);
sona_result result = sona_sticker(r, avatar, &x_sticker_options, &x_picture2);
if (result != SONA_OK) printf("sona_sticker: %s\n", sona_result_name(result));
```

A transparent cut-out of the avatar turned `turn_deg` from facing the camera, at `time` seconds into `clip` (negative: its middle; NULL clip: the pose it has now), framed whole (`SONA_PRESET_FULL`) or by the console's `UPPER`, `BUST` and `FACE` framings, drawn at twice the size and brought down so its longer side is `pixels`, outline included; then the outline, the drop shadow, `padding`, and with `SONA_STICKER_SQUARE` a square canvas. The credit badge is stamped bottom right unless `draw_flags` has `SONA_DRAW_NO_CREDIT`. `out->rgba` is freed with `sona_free`. `SONA_E_NOT_READY` before READY, `SONA_E_ARGUMENT` for an item preview, a renderer of another ctx or an option out of range, `SONA_E_NOT_FOUND` when the avatar drew nothing or the clip's file cannot be read, `SONA_PENDING` while the clip loads.

## sona_sheet_defaults

```c
void sona_sheet_defaults(sona_sheet_options *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `o` | `sona_sheet_options *` | The [`sona_sheet_options`](../fields.md#sona_sheet_options) to fill with the defaults | required |

**Returns** Nothing.

**Example**:

```c
sona_sheet_options x_sheet_options;
memset(&x_sheet_options, 0, sizeof x_sheet_options);
x_sheet_options.size = sizeof x_sheet_options;
sona_sheet_defaults(&x_sheet_options);
```

`size` set, 8 frames, 8 directions, `px_per_metre` 64, `padding` 2, `max_page` 4096, `draw_flags` `SONA_DRAW_NO_BLOOM`; every other field 0 (the whole clip, facing the camera first, side on, orthographic, packed and trimmed). NULL does nothing.

## sona_sheet_render

```c
sona_result sona_sheet_render(sona_renderer *r, sona_avatar *a, const sona_sheet_options *o, sona_sheet **out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `r` | `sona_renderer *` | The renderer | required |
| `a` | `sona_avatar *` | The avatar | required |
| `o` | `const sona_sheet_options *` | The options, a [`sona_sheet_options`](../fields.md#sona_sheet_options) | required |
| `out` | `sona_sheet **` | Receives the new sprite sheet | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_NOT_READY`, `SONA_E_NOT_FOUND`, `SONA_E_LIMIT`, `SONA_E_MEMORY`, `SONA_E_CONTEXT`, `SONA_E_READBACK` ([what each means](../../troubleshooting.md#error-codes)).

Renders `frames` x `directions` pictures of the clip under one fixed camera (looking down by `pitch_deg`, orthographic unless `fov_deg`) at `px_per_metre`, the hips held in place unless `SONA_SHEET_ROOT_MOTION`. Frames are trimmed (`SONA_SHEET_NO_TRIM`: whole cells), identical ones share a rectangle (`SONA_SHEET_NO_DEDUP`), and they are packed on pages of at most `max_page` (`SONA_SHEET_POW2`: power-of-two sides), or with `SONA_SHEET_GRID` laid out a row per direction. With `o->pixel` the frames are pixel art with one palette. The sheet is the caller's until `sona_sheet_free`, which must come before `sona_close`. Errors as `sona_sticker`, plus `SONA_E_LIMIT` when a frame does not fit `max_page`, a cell needs a render over 8192 pixels, or the cells would pass the 1 GB limit. `*out` is NULL on any error.

## sona_sheet_free

```c
void sona_sheet_free(sona_sheet *s);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `s` | `sona_sheet *` | The sprite sheet | required |

**Returns** Nothing.

Frees the sheet, its pages and frames. NULL does nothing.

## sona_sheet_page_count

```c
uint32_t sona_sheet_page_count(const sona_sheet *s);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `s` | `const sona_sheet *` | The sprite sheet | required |

**Returns** `uint32_t`.

The number of pages; 0 for NULL.

## sona_sheet_page

```c
sona_result sona_sheet_page(const sona_sheet *s, uint32_t page, sona_picture *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `s` | `const sona_sheet *` | The sprite sheet | required |
| `page` | `uint32_t` | An atlas page, from 0 | - |
| `out` | `sona_picture *` | Receives the [`sona_picture`](../fields.md#sona_picture) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

One page's pixels, owned by the sheet: do not free them; valid until `sona_sheet_free`. `SONA_E_ARGUMENT` past the last page.

## sona_sheet_frame_count

```c
uint32_t sona_sheet_frame_count(const sona_sheet *s);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `s` | `const sona_sheet *` | The sprite sheet | required |

**Returns** `uint32_t`.

`frames * directions`; 0 for NULL.

## sona_sheet_frame_get

```c
sona_result sona_sheet_frame_get(const sona_sheet *s, uint32_t index, sona_sheet_frame *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `s` | `const sona_sheet *` | The sprite sheet | required |
| `index` | `uint32_t` | Which one, from 0 | - |
| `out` | `sona_sheet_frame *` | Receives the [`sona_sheet_frame`](../fields.md#sona_sheet_frame) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

One frame (index = direction * frames + frame): its page and rectangle, where the rectangle sits in the full cell (`trim_x`, `trim_y`), the cell's size, the pivot (the avatar's feet) in the cell, its direction, facing, time, duration and `alias` (the first frame with the same rectangle). `SONA_E_ARGUMENT` past the last frame.

## sona_sheet_atlas

```c
sona_result sona_sheet_atlas(const sona_sheet *s, uint32_t format, uint32_t page, const char *image, char **json,
                             size_t *len);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `s` | `const sona_sheet *` | The sprite sheet | required |
| `format` | `uint32_t` | The file format to write | - |
| `page` | `uint32_t` | An atlas page, from 0 | - |
| `image` | `const char *` | The picture's file name, as the atlas names it | required |
| `json` | `char **` | Receives the atlas as JSON text; free it with `sona_free` | required |
| `len` | `size_t *` | Receives how many bytes were written | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_NOT_READY`, `SONA_E_NOT_FOUND`, `SONA_E_MEMORY`, `SONA_E_CONTEXT`, `SONA_E_READBACK` ([what each means](../../troubleshooting.md#error-codes)).

The atlas of one page as NUL-terminated JSON, freed with `sona_free` on the sheet's ctx. `SONA_ATLAS_ASEPRITE`: Aseprite's array form, a frame tag per direction and a `pivot` slice. `SONA_ATLAS_TEXTUREPACKER`: TexturePacker's hash form with a pivot per frame and an animation per direction; with `page` `SONA_ATLAS_ALL_PAGES`, every page in one multi-atlas file. Frames are named `<name>_<direction>_<nnn>`. `image` is the page file's name for `meta.image` (`{n}` becomes the page number; NULL: `sheet-<n>.png`). `SONA_E_ARGUMENT` for an unknown format, a page past the last, or ALL_PAGES with Aseprite.
