# C API: Item previews

One item worn alone on a grey stand-in body, framed by its slot, for wardrobes and item pickers. The guide, with the framing rules and the measured cost, is [Item previews](../../guides/item-preview.md).

A preview is an avatar of the ctx with a made-up manifest. It builds through the same queue, shares decoded items and GPU buffers with every other avatar, counts toward `max_avatars` and is freed by `sona_avatar_remove`. `sona_draw` and `sona_snapshot` never draw it, `sona_avatar_set_visible` does nothing to it and `sona_camera_fit_crowd` ignores it. It is a still picture: `sona_avatar_play`, `_crossfade` and `_queue` return `SONA_E_ARGUMENT` for it. Leave pose overrides off a preview; they are not part of the measured frame.

`sona_preview_options`, 40 bytes:

| Field | Meaning | Default |
|---|---|---|
| `gender` | 1 male, 2 female; 0: the body the item was made for (male when either) | 0 |
| `slot` | The category mask to frame by (`0x40` hat, `0x38` costume, ...); 0: from the item | 0 |
| `flags` | `SONA_PREVIEW_PLAY` (1): a prop plays its clip, framed for all of it. `SONA_PREVIEW_COLOURS` (2): wear the item in `colours` | 0 |
| `skin` | 0xAARRGGBB; 0: the stand-in grey `0xff9a9a9a` | 0 |
| `colours[3]` | 0xAARRGGBB primary, secondary, tertiary, with `SONA_PREVIEW_COLOURS` | 0 |
| `priority` | Build order, as `sona_add_options.priority` | 0 |
| `time` | Props: seconds into the clip to hold (or start at); negative: its fullest frame | -1 |
| `margin` | Room around the item: 1 touches the edges; 0 means 1.36, as the web's item renders | 1.36 |

## sona_preview_defaults

```c
void sona_preview_defaults(sona_preview_options *o);
```

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

**Returns** Nothing.

**Example**:

```c
sona_preview_options x_preview_options;
memset(&x_preview_options, 0, sizeof x_preview_options);
sona_preview_defaults(&x_preview_options);
```

Fills the defaults above. NULL does nothing.

## sona_item_preview_add

```c
sona_result sona_item_preview_add(sona_ctx *ctx, const char *item_id, const sona_preview_options *opts,
                                  sona_avatar **out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `item_id` | `const char *` | An item id as text | required |
| `opts` | `const sona_preview_options *` | The options, a [`sona_preview_options`](../fields.md#sona_preview_options) | `NULL`: the defaults |
| `out` | `sona_avatar **` | Receives the new avatar | required |

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

Queues a preview of one item id (lowercase or uppercase) and returns at once. NULL `opts` takes the defaults. It reaches READY or FAILED through `sona_update` with the usual events. A missing item file does not fail it: it reaches READY without the item, and `sona_item_preview_bounds` says `SONA_E_NOT_FOUND`.

- `SONA_E_ARGUMENT`: NULL `ctx` or `out`, an id that does not parse, an item that is not a worn item, a face item or a head shape (a clip, for example), `gender` above 2, a negative or non-finite `margin`, a non-finite `time`.
- `SONA_E_LIMIT`: the ctx already holds `max_avatars` avatars.
- `SONA_E_MEMORY`.

`*out` is NULL on any error.

## sona_item_preview_bounds

```c
sona_result sona_item_preview_bounds(sona_avatar *a, float lo[3], float hi[3]);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `lo` | `float [3]` | Receives the box's lower corner | required |
| `hi` | `float [3]` | Receives the box's upper corner | required |

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

**Example**:

```c
float x_lo[3];
memset(x_lo, 0, sizeof x_lo);
float x_hi2[3];
memset(x_hi2, 0, sizeof x_hi2);
sona_result result = sona_item_preview_bounds(avatar, x_lo, x_hi2);
if (result != SONA_OK) printf("sona_item_preview_bounds: %s\n", sona_result_name(result));
```

The item's box in world metres, over the whole clip for a playing prop. Face and head-shape items give the head; a pair framed from one side (earrings, wristbands) gives the near one; a held prop leaves out a stray part set apart from the rest. Measured once, on the first call to any of `_bounds`, `_view`, `_draw` or `_snapshot` after READY.

`SONA_E_NOT_READY` before READY. `SONA_E_ARGUMENT` for NULL arguments or an avatar that is not a preview. `SONA_E_NOT_FOUND` when no triangle of the item was drawn (its file is missing).

## sona_item_preview_view

```c
sona_result sona_item_preview_view(sona_avatar *a, uint32_t width, uint32_t height, sona_view *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `width` | `uint32_t` | The picture's width in pixels | - |
| `height` | `uint32_t` | The picture's height in pixels | - |
| `out` | `sona_view *` | Receives the [`sona_view`](../fields.md#sona_view) | required |

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

**Example**:

```c
sona_view x_view;
sona_view_defaults(&x_view);
sona_camera_preset(avatar, SONA_PRESET_FULL, &x_view);
sona_result result = sona_item_preview_view(avatar, 16, 16, &x_view);
if (result != SONA_OK) printf("sona_item_preview_view: %s\n", sona_result_name(result));
```

The framed camera for a `width` x `height` picture: the slot's view preset gives the angle, the item's box the target and the distance. Every field of `*out` is set. Errors as `sona_item_preview_bounds`, except that an item that drew nothing still gets its slot's preset camera and `SONA_OK`.

## sona_item_preview_draw

```c
sona_result sona_item_preview_draw(sona_renderer *r, sona_avatar *a, const sona_view *view,
                                   const sona_target *target, const sona_draw_options *opts);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `r` | `sona_renderer *` | The renderer | required |
| `a` | `sona_avatar *` | The avatar | required |
| `view` | `const sona_view *` | The camera, a [`sona_view`](../fields.md#sona_view) | required |
| `target` | `const sona_target *` | Where the frame lands, a [`sona_target`](../fields.md#sona_target) | required |
| `opts` | `const sona_draw_options *` | The options, a [`sona_draw_options`](../fields.md#sona_draw_options) | required |

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

`sona_draw` with only this preview in the frame. NULL `view`: `sona_item_preview_view` for the target's size. One frame, with the frames-in-flight and host-state rules of `sona_draw`. The renderer must belong to the preview's ctx (`SONA_E_ARGUMENT` otherwise). It also returns what `sona_draw` returns.

## sona_item_preview_snapshot

```c
sona_result sona_item_preview_snapshot(sona_renderer *r, sona_avatar *a, uint32_t width, uint32_t height,
                                       const sona_draw_options *opts, uint8_t *rgba_out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `r` | `sona_renderer *` | The renderer | required |
| `a` | `sona_avatar *` | The avatar | required |
| `width` | `uint32_t` | The picture's width in pixels | - |
| `height` | `uint32_t` | The picture's height in pixels | - |
| `opts` | `const sona_draw_options *` | The options, a [`sona_draw_options`](../fields.md#sona_draw_options) | required |
| `rgba_out` | `uint8_t *` | Receives straight RGBA8, row 0 at the top: `width * height * 4` bytes | required |

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

`sona_snapshot` of this preview alone, framed for `width` x `height`. Blocks until the GPU finishes and writes `width * height * 4` bytes of straight RGBA8, row 0 at the top. The credit is drawn unless `SONA_DRAW_NO_CREDIT`; a picker usually passes the flag and shows the credit once elsewhere, as the licence still requires.
