GetImageData bugfix

This commit is contained in:
Thomas Friedel 2019-12-04 12:13:09 +01:00
parent fd1cca7ba9
commit f0367ee72d
4 changed files with 25 additions and 11 deletions

View file

@ -26,14 +26,19 @@ func (b *GoGLBackend) GetImageData(x, y, w, h int) *image.RGBA {
if h > b.h { if h > b.h {
h = b.h h = b.h
} }
if len(b.imageBuf) < w*h*3 {
b.imageBuf = make([]byte, w*h*3)
}
gl.ReadPixels(int32(x), int32(y), int32(w), int32(h), gl.RGB, gl.UNSIGNED_BYTE, gl.Ptr(&b.imageBuf[0])) var vp [4]int32
gl.GetIntegerv(gl.VIEWPORT, &vp[0])
size := int(vp[2] * vp[3] * 3)
if len(b.imageBuf) < size {
b.imageBuf = make([]byte, size)
}
gl.ReadPixels(vp[0], vp[1], vp[2], vp[3], gl.RGB, gl.UNSIGNED_BYTE, gl.Ptr(&b.imageBuf[0]))
rgba := image.NewRGBA(image.Rect(x, y, x+w, y+h)) rgba := image.NewRGBA(image.Rect(x, y, x+w, y+h))
bp := 0
for cy := y; cy < y+h; cy++ { for cy := y; cy < y+h; cy++ {
bp := (int(vp[3])-h+cy)*int(vp[2])*3 + x*3
for cx := x; cx < x+w; cx++ { for cx := x; cx < x+w; cx++ {
rgba.SetRGBA(cx, y+h-1-cy, color.RGBA{R: b.imageBuf[bp], G: b.imageBuf[bp+1], B: b.imageBuf[bp+2], A: 255}) rgba.SetRGBA(cx, y+h-1-cy, color.RGBA{R: b.imageBuf[bp], G: b.imageBuf[bp+1], B: b.imageBuf[bp+2], A: 255})
bp += 3 bp += 3

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
@ -212,6 +213,8 @@ func rewrite(filename, src string) (string, string) {
params[i] = param[6 : len(param)-1] params[i] = param[6 : len(param)-1]
} else if strings.HasPrefix(param, "gl.Ptr(") { } else if strings.HasPrefix(param, "gl.Ptr(") {
params[i] = param[8:len(param)-2] + ":]" params[i] = param[8:len(param)-2] + ":]"
} else if len(param) >= 5 && param[:3] == "vp[" {
params[i] = fmt.Sprintf("int(%s)", param)
} }
} }
return "b.glctx.ReadPixels(" + params[6] + ", " + strings.Join(params[:len(params)-1], ", ") + ")" return "b.glctx.ReadPixels(" + params[6] + ", " + strings.Join(params[:len(params)-1], ", ") + ")"
@ -239,6 +242,7 @@ func rewrite(filename, src string) (string, string) {
src = rewriteCalls(src, "loadShader", func(params []string) string { src = rewriteCalls(src, "loadShader", func(params []string) string {
return "loadShader(b, " + strings.Join(params, ", ") + ")" return "loadShader(b, " + strings.Join(params, ", ") + ")"
}) })
src = strings.ReplaceAll(src, "if tex == 0 {", "if tex.Value == 0 {")
if filename == "gogl.go" { if filename == "gogl.go" {
filename = "xmobile.go" filename = "xmobile.go"

View file

@ -26,14 +26,19 @@ func (b *XMobileBackend) GetImageData(x, y, w, h int) *image.RGBA {
if h > b.h { if h > b.h {
h = b.h h = b.h
} }
if len(b.imageBuf) < w*h*3 {
b.imageBuf = make([]byte, w*h*3)
}
b.glctx.ReadPixels(b.imageBuf[0:], x, y, w, h, gl.RGB, gl.UNSIGNED_BYTE) var vp [4]int32
b.glctx.GetIntegerv(vp[:], gl.VIEWPORT)
size := int(vp[2] * vp[3] * 3)
if len(b.imageBuf) < size {
b.imageBuf = make([]byte, size)
}
b.glctx.ReadPixels(b.imageBuf[0:], int(vp[0]), int(vp[1]), int(vp[2]), int(vp[3]), gl.RGB, gl.UNSIGNED_BYTE)
rgba := image.NewRGBA(image.Rect(x, y, x+w, y+h)) rgba := image.NewRGBA(image.Rect(x, y, x+w, y+h))
bp := 0
for cy := y; cy < y+h; cy++ { for cy := y; cy < y+h; cy++ {
bp := (int(vp[3])-h+cy)*int(vp[2])*3 + x*3
for cx := x; cx < x+w; cx++ { for cx := x; cx < x+w; cx++ {
rgba.SetRGBA(cx, y+h-1-cy, color.RGBA{R: b.imageBuf[bp], G: b.imageBuf[bp+1], B: b.imageBuf[bp+2], A: 255}) rgba.SetRGBA(cx, y+h-1-cy, color.RGBA{R: b.imageBuf[bp], G: b.imageBuf[bp+1], B: b.imageBuf[bp+2], A: 255})
bp += 3 bp += 3

View file

@ -23,7 +23,7 @@ func (b *XMobileBackend) LoadImage(src image.Image) (backendbase.Image, error) {
var tex gl.Texture var tex gl.Texture
tex = b.glctx.CreateTexture() tex = b.glctx.CreateTexture()
if tex == 0 { if tex.Value == 0 {
return nil, errors.New("glGenTextures failed") return nil, errors.New("glGenTextures failed")
} }