From 85ba21a2f42f3785ffd4be17f92dc454121fcaaa Mon Sep 17 00:00:00 2001 From: faiface Date: Fri, 28 Apr 2017 13:24:30 +0200 Subject: [PATCH] add Canvas.SetPixels and Canvas.Pixels methods --- pixelgl/canvas.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pixelgl/canvas.go b/pixelgl/canvas.go index e50e5ed..723b544 100644 --- a/pixelgl/canvas.go +++ b/pixelgl/canvas.go @@ -218,6 +218,33 @@ func (c *Canvas) Texture() *glhf.Texture { return c.gf.Texture() } +// SetPixels replaces the content of the Canvas with the provided pixels. The provided slice must be +// an alpha-premultiplied RGBA sequence of correct length (4 * width * height). +func (c *Canvas) SetPixels(pixels []uint8) { + c.gf.Dirty() + + mainthread.Call(func() { + tex := c.Texture() + tex.Begin() + tex.SetPixels(0, 0, tex.Width(), tex.Height(), pixels) + tex.End() + }) +} + +// Pixels returns an alpha-premultiplied RGBA sequence of the content of the Canvas. +func (c *Canvas) Pixels() []uint8 { + var pixels []uint8 + + mainthread.Call(func() { + tex := c.Texture() + tex.Begin() + pixels = tex.Pixels(0, 0, tex.Width(), tex.Height()) + tex.End() + }) + + return pixels +} + // Draw draws a rectangle equal to Canvas's Bounds containing the Canvas's content to another // Target. //