From 9228a55482658dfc23b7ae2f59ff6122c8debfc2 Mon Sep 17 00:00:00 2001 From: faiface Date: Sun, 22 Jan 2017 13:45:43 +0100 Subject: [PATCH] add Picture.Image method --- picture.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/picture.go b/picture.go index b1daa77..bf0334d 100644 --- a/picture.go +++ b/picture.go @@ -51,6 +51,38 @@ func NewPicture(img image.Image, smooth bool) *Picture { } } +// Image returns the content of the Picture as an image.NRGBA. +func (p *Picture) Image() *image.NRGBA { + bounds := p.Bounds() + nrgba := image.NewNRGBA(image.Rect( + int(bounds.X()), + int(bounds.Y()), + int(bounds.X()+bounds.W()), + int(bounds.Y()+bounds.H()), + )) + + mainthread.Call(func() { + nrgba.Pix = p.texture.Pixels( + int(bounds.X()), + int(bounds.Y()), + int(bounds.W()), + int(bounds.H()), + ) + }) + + // flip the image vertically + tmp := make([]byte, nrgba.Stride) + for i, j := 0, nrgba.Bounds().Dy()-1; i < j; i, j = i+1, j-1 { + iSlice := nrgba.Pix[i*nrgba.Stride : (i+1)*nrgba.Stride] + jSlice := nrgba.Pix[j*nrgba.Stride : (j+1)*nrgba.Stride] + copy(tmp, iSlice) + copy(iSlice, jSlice) + copy(jSlice, tmp) + } + + return nrgba +} + // Texture returns a pointer to the underlying OpenGL texture of a picture. func (p *Picture) Texture() *pixelgl.Texture { return p.texture