Pixels

Direct pixel access and per-pixel operations. Reading pixels back (LoadPixels, Get, and everything built on them) requires a CPU-readable Surface — the same requirement Save() has.

On this page · 8 entries

LoadPixels · Pixels · UpdatePixels

  • LoadPixels()
  • Pixels → Color[]
  • UpdatePixels()

LoadPixels() snapshots the canvas into the Pixels array (row-major, index = y * Width + x) for reading or editing. UpdatePixels() writes it back — verbatim, no blending with what was there. Pixels throws if LoadPixels() hasn't been called; it deliberately doesn't auto-load, so it's always clear whether you're looking at a fresh read.

Get

  • Get(x, y) → Color
  • Get(x, y, w, h) → PImage
  • Get() → PImage

Reads pixels straight from the canvas — no LoadPixels() needed. Out-of-bounds coordinates come back transparent rather than throwing.

Set

  • Set(x, y, Color c)
  • Set(x, y, PImage img)

Writes a single pixel or draws an image with pixels copied verbatim — no tint, no blending. Out-of-bounds coordinates are silently ignored.

img.LoadPixels · img.Pixels · img.UpdatePixels · img.Get · img.Set

  • img.LoadPixels()
  • img.Pixels → Color[]
  • img.UpdatePixels()
  • img.Get(x, y)
  • img.Set(x, y, c)

The same for a PImage, like Processing's img.pixels: read a loaded image's colors (a heightmap, a palette, a mask) without drawing it anywhere first. Any format comes out as plain colors — a grayscale PNG reads as gray Colors. A PGraphics can be read (pg.LoadPixels(), pg.Get()) after EndDraw() too.

Copy

  • Copy(sx,sy,sw,sh, dx,dy,dw,dh)
  • Copy(PImage src, sx,sy,sw,sh, dx,dy,dw,dh)

Copies a region (scaling if source/destination sizes differ) — verbatim, no blending. Without a source image, copies from this same canvas onto itself.

Blend

  • Blend(sx,sy,sw,sh, dx,dy,dw,dh, BlendModeKind mode)
  • Blend(PImage src, ...)

Same as Copy(), but composites through the given blend mode instead of overwriting. See BlendModeKind.

Filter

  • Filter(FilterKind kind, float param = 0.5f)

Applies a per-pixel filter to the whole canvas in place. param means different things per kind: the cutoff for Threshold (0–1), the levels per channel for Posterize, the pixel radius for Blur; ignored otherwise. See FilterKind.

Not implemented. Erode/Dilate need neighbor-pixel sampling rather than a per-pixel transform and throw NotSupportedException for now.

Runnable example

A complete sketch using this section. Test in Dana opens it in the IDE, or paste it into a new tab and press Run.

C#
// LoadPixels / Pixels[] / UpdatePixels: read and rewrite every pixel.
public class PixelWave : Sketch
{
    public override void Setup() => Size(320, 240);

    public override void Draw()
    {
        LoadPixels();
        float t = FrameCount * 0.05f;
        for (int y = 0; y < Height; y++)
for (int x = 0; x < Width; x++)
{
    float d = Dist(x, y, MouseX, MouseY);
    byte v = (byte)(128 + 127 * Sin(d * 0.08f - t));
    Pixels[y * Width + x] = new Color(v, (byte)(x * 255 / Width), (byte)(255 - v), 255);
}
        UpdatePixels();

        // Get(x, y) reads one pixel straight from the canvas.
        var c = Get((int)MouseX, (int)MouseY);
        Fill(c); Stroke(255);
        Rect(10, 10, 40, 40);
    }
}