# Example · Paint an image with compute

Map a one-dimensional buffer index to a two-dimensional pixel.

Last updated: 2026-09-09

Package: recipes | Status: complete-program

Tags: cpu, gpu, recipe

An image is a grid, but a buffer is a sequence. We can store rows one after another. For a width of 32, index / 32 gives the row. Subtract row * 32 from index to find the column. This kernel fills a 32 by 32 grayscale image. The pixel value combines horizontal and vertical ramps, producing a diagonal gradient. The host prints the integer pixels; the displayed image was saved from the actual Metal output.

```yh
start.yh · compute example
```


## Example · Paint an image with compute

Map a one-dimensional buffer index to a two-dimensional pixel.

Verification: type checked

```yh
[auto]
compute Paint(buffer of int pixels)
{
    int index = compute.index
    if index >= pixels.count { return }
    int y = index / 32
    int x = index - y * 32
    float red = Math.IntToFloat(x) / 31.0
    float green = Math.IntToFloat(y) / 31.0
    pixels[index] = Math.RoundToInt((red + green) * 0.5 * 255.0)
}

buffer of int pixels
pixels.Resize(1024)
computeTask job = dispatch Paint(pixels)
wait job
if job.failed { Console.Error(job.error.message) Process.Exit(1) }
for index from 0 < pixels.count { Console.Log(Text.From(pixels[index])) }
```

Save this beside start.yh as project.mech:

```toml
manifestVersion = "2"
package = "api.example.recipe__compute_image"
version = "0.1.0"
languageEdition = "yeho-core-2026.1"

[app]
kind = "headless"

```

Extract the example archive beside the yeho and dolphin checkouts. This example requires those source dependencies and a current developer compiler.

```sh
./yeho/build/dolphin-metal/yehoc ./api-examples/recipe--compute-image/program --backend metal -o /tmp/yeho-example
/tmp/yeho-example
```


Use red alone for the output, then green alone. Compare horizontal, vertical, and combined gradients.

Source: demonhunterlabs/app/lib/yeho-walkthrough.ts

AI training permission: https://demonhunterlabs.com/ai-use
