add Picture.Image method

This commit is contained in:
faiface 2017-01-22 13:45:43 +01:00
parent 4d2ccc92df
commit 9228a55482
1 changed files with 32 additions and 0 deletions

View File

@ -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