> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nika.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# Image effects

> Deterministic artistic styling — dither, palettes, halftone, retro, glitch, ascii — and contact sheets. Same input, same args, same bytes forever: the styled image becomes a receipt in the run's proof chain.

`nika:image_fx` is the deterministic sibling of
[`nika:image_generate`](https://docs.nika.sh/guides/image-generation): a pure pixel transform —
no provider, no network, no clock. Style a PNG through an ordered pipeline of
effects and get an artifact that is **byte-identical** for identical
`(input, args)` on the **same pinned engine** — not a promise across
engine versions.

That determinism is the point, not a detail. The artifact's sha256 joins the
run's hash-chained trace, re-rendering the recipe is the tamper check, and the
full recipe (contract tag `image_fx/v1` · input sha256 · seed · ops) travels
*inside* the PNG as a `nika` tEXt chunk — no timestamp, ever. Your workflow
draws its own receipts.

## The 30-second pipeline

```yaml theme={"system"}
nika: styled-hero
permits:
  tools: ["nika:image_generate", "nika:image_fx"]
  fs:
    read: ["out/**"]
    write: ["out/**"]
tasks:
  generate:
    invoke:
      tool: "nika:image_generate"
      args:
        provider: mock            # offline dry-run · flip to local/openai/gemini/xai
        prompt: "a monarch butterfly over a nebula"
        output_dir: out
        format: png
  style:
    with:
      src: ${{ tasks.generate.output.images[0].path }}
    invoke:
      tool: "nika:image_fx"
      args:
        input: "${{ with.src }}"
        out: out/hero-gameboy.png
        seed: 42                  # the seed IS the style — same seed, same bytes
        ops:
          - resize: { width: 320 }
          - dither: { mode: floyd_steinberg, palette: gameboy }
          - grain: { intensity: 32 }
          - scanlines: { strength: 110, period: 4 }
          - vignette: { strength: 140 }
```

Run it twice: the second run reports `skipped_existing: true` and the artifact
hash never moves. Mutate one byte of the PNG and a re-render no longer matches
— that mismatch *is* the tamper detection.

## The op vocabulary (closed · v1)

`ops:` is an **ordered list of single-key maps** — a linear pipeline by design.
Branching and fan-out belong to the workflow (`with:`/`after:` edges ·
`for_each`), never inside the builtin.

| Op | What it does | Key knobs |
| - | - | - |
| `resize` | Linear-light resample | `width`/`height` (one may be omitted) · `filter: auto\|nearest\|bilinear` |
| `crop` | Exact rectangle | `x` · `y` · `width` · `height` |
| `levels` | Brightness / contrast | `brightness` −255..255 · `contrast` −128..128 |
| `grayscale` | Linear-light luma | — |
| `palette_map` | Nearest palette color (Oklab distance) | `palette` |
| `dither` | Quantize through a dither | `mode: bayer2\|bayer4\|bayer8\|blue_noise\|ign\|floyd_steinberg\|atkinson\|jjn` · `palette` |
| `duotone` | Two-stop gradient map (Oklab ramp) | `dark` · `light` (`#rrggbb` or `[r,g,b]`) |
| `pixelate` | Block mosaic | `block` 2..256 |
| `halftone` | Rotated clustered-dot screen (print look) | `cell` 3..64 · `angle: 0\|15\|45\|75` |
| `grain` | Luminance-shaped film grain (seeded) | `intensity` 0..128 |
| `vignette` | Natural cos⁴ falloff | `strength` 0..255 |
| `chromatic_aberration` | Per-channel radial split | `shift` 1..16 |
| `scanlines` | CRT row darkening | `strength` 0..255 · `period` 2..64 |
| `glitch` | Seeded databending | `line_shift` ≤64 · `channel_shift` ≤16 · `blocks` ≤64 |
| `ascii` | Character-grid render — **must be the last op** | `cols` 2..1024 · `emit: png\|text\|ansi` |

Palettes: presets `bw · gray4 · gameboy · cga · okabe_ito`, or an inline list
of 2–256 colors. Unknown op parameters are **rejected loudly**
(`NIKA-BUILTIN-IMAGE_FX-001`) — a typo'd knob never silently lies about the
style.

> [!WARNING]
>
> Input is **PNG in v1** (depth 8 · no interlace). Non-PNG fails with a typed
> hint: produce PNG upstream (`image_generate` with `format: png`) or convert
> once via `exec:`. Decode is budget-gated from the header **before** any
> decompression — hostile dimension claims are rejected, never allocated.

## Why deterministic styling matters

Every incumbent styling path is nondeterministic by default — image tools
timestamp their outputs, and generative models can't reproduce bytes across
hardware at all. That makes styled assets un-verifiable: you can sign them,
but you can't *re-derive* them.

`image_fx` inverts that: integer/fixed-point pixel math, a seeded noise
stream, zero wall-clock. So the artifact can be:

* **chained** — its sha256 lands in the trace (`nika trace verify` covers it);
* **re-rendered** — the same input + the recipe reproduces the exact bytes;
* **carried** — the recipe rides the artifact itself, so any holder of the
  input can verify the styling.

One recipe, one hash, forever. Batch-style a hundred campaign images with
`for_each` and every one of them is a receipt.

```yaml fragment · paste into the file above theme={"system"}
  variants:
    with:
      src: ${{ tasks.generate.output.images[0].path }}
    for_each:
      items: ["bw", "gameboy", "cga"]
    invoke:
      tool: "nika:image_fx"
      args:
        input: "${{ with.src }}"
        out: "out/variant-${{ item }}.png"
        ops:
          - dither: { mode: bayer4, palette: "${{ item }}" }
```

## Output contract

```json theme={"system"}
{
  "input": "out/hero.png",
  "input_sha256": "…",
  "path": "out/hero-gameboy.png",
  "sha256": "…",
  "size_bytes": 323953,
  "width": 320,
  "height": 200,
  "format": "png",
  "ops_applied": 5,
  "seed": 42,
  "skipped_existing": false
}
```

Artifact bytes never ride outputs (the disk law). Text artifacts (`ascii` with
`emit: text|ansi`) land as `.txt`/`.ans`, report `width`/`height` `0`, and are
sha256'd like any other artifact.

Errors are typed `NIKA-BUILTIN-IMAGE_FX-001..006` — invalid args · input read
· unsupported format · decode · pixel budget · save — plus the boundary
`NIKA-SEC-004` when a path resolves outside `permits.fs`. An input over 16384
pixels on a side is the pixel budget (`-005`), and a PNG whose `IHDR` is not
its first chunk, or that carries a second one, is refused as corrupt (`-004`):
the size the budget judged from the header is always the size decoded.

## Compose several images into one

`nika:image_compose` is `image_fx`'s sibling for layout: it lays 1 to 64 PNG
files out as one image, a contact sheet (`grid`), a side-by-side (`row`) or a
stack (`column`). Like `image_fx` it is pure pixel work, with no provider, no
network and no clock, so identical input bytes and args give identical PNG
bytes, and the `image_compose/v1` recipe (each input's sha256 and size, the
resolved layout) rides the artifact. A contact sheet no longer needs an
image program run through `exec:`.

```yaml theme={"system"}
nika: hero-sheet
permits:
  tools: ["nika:image_generate", "nika:image_fx", "nika:image_compose"]
  fs:
    read: ["out/**"]
    write: ["out/**"]
tasks:
  generate:
    invoke:
      tool: "nika:image_generate"
      args:
        provider: mock            # offline dry-run · flip to local/openai/gemini/xai
        prompt: "a monarch butterfly over a nebula"
        output_dir: out
        format: png
  gameboy:
    with:
      src: ${{ tasks.generate.output.images[0].path }}
    invoke:
      tool: "nika:image_fx"
      args:
        input: "${{ with.src }}"
        out: out/hero-gameboy.png
        ops:
          - resize: { width: 320 }
          - dither: { mode: floyd_steinberg, palette: gameboy }
  halftone:
    with:
      src: ${{ tasks.generate.output.images[0].path }}
    invoke:
      tool: "nika:image_fx"
      args:
        input: "${{ with.src }}"
        out: out/hero-halftone.png
        ops:
          - resize: { width: 320 }
          - halftone: { cell: 6, angle: 45 }
  sheet:
    after: { gameboy: success, halftone: success }   # it reads both files
    invoke:
      tool: "nika:image_compose"
      args:
        inputs:                   # ordered · 1 to 64 PNG files, each a read
          - out/hero-gameboy.png
          - out/hero-halftone.png
        out: out/hero-sheet.png
        layout: row               # grid (default) · row · column
        scale: fit                # none (default) · fit · fill
        cell: { width: 320, height: 200 }
        gap: 8
        padding: 16
        background: "#10141c"
outputs:
  sheet: ${{ tasks.sheet.output.path }}
```

| Arg | What it does |
| - | - |
| `inputs` | The ordered PNG paths, placed row-major. Each is a read `nika check` judges against `permits.fs.read` (a literal path at check, a templated one at run) |
| `out` | The `.png` artifact, a write inside `permits.fs.write` |
| `layout` | `grid` (the default: columns and rows derived from the count, or set with `columns` and `rows`), `row` or `column` |
| `scale` | `none` (each image at its own size), `fit` (scaled into its cell, aspect kept, letterboxed on `background`) or `fill` (scaled to cover its cell, then center-cropped) |
| `cell` | `{ width, height }` for `fit` and `fill`; the default is the largest input box |
| `gap` · `padding` · `background` | Pixels between cells, around the sheet, and the fill color (`#rrggbb` or `[r, g, b]`) |

Every limit is judged from the PNG headers before any decompression: each
input's side, the summed input pixels, each scaled cell and the canvas
(`NIKA-BUILTIN-IMAGE_COMPOSE-005`). An input count outside 1 to 64 is an
argument error (`-001`); the codes follow `image_fx`'s ladder, and
`nika explain` teaches each one. Inputs are PNG only, as for `image_fx`. The
output carries `path`, `sha256`, `width` and `height` (and each input's
digest), never the bytes, and identical bytes already at `out:` are an
idempotent skip (`skipped_existing`). The full contract is
[`nika:image_compose`](https://docs.nika.sh/reference/tools/image_compose).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.