graphics: Fix TestImagePixels to check out-of-range pixels

This commit is contained in:
Hajime Hoshi 2017-02-05 04:24:07 +09:00
parent 91681bdc25
commit 0967df7f5e
2 changed files with 12 additions and 4 deletions

View File

@ -25,6 +25,7 @@ import (
"testing" "testing"
. "github.com/hajimehoshi/ebiten" . "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/graphics"
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
@ -87,8 +88,11 @@ func TestImagePixels(t *testing.T) {
t.Fatalf("img size: got %d; want %d", got, img.Bounds().Size()) t.Fatalf("img size: got %d; want %d", got, img.Bounds().Size())
} }
for j := 0; j < img0.Bounds().Size().Y; j++ { w, h := img0.Bounds().Size().X, img0.Bounds().Size().Y
for i := 0; i < img0.Bounds().Size().X; i++ { // Check out of range part
w2, h2 := graphics.NextPowerOf2Int(w), graphics.NextPowerOf2Int(h)
for j := -100; j < h2+100; j++ {
for i := -100; i < w2+100; i++ {
got := img0.At(i, j) got := img0.At(i, j)
want := color.RGBAModel.Convert(img.At(i, j)) want := color.RGBAModel.Convert(img.At(i, j))
if got != want { if got != want {

View File

@ -170,13 +170,17 @@ func (p *Image) appendDrawImageHistory(image *Image, vertices []float32, colorm
// Note that this must not be called until context is available. // Note that this must not be called until context is available.
// This means Pixels members must match with acutal state in VRAM. // This means Pixels members must match with acutal state in VRAM.
func (p *Image) At(x, y int, context *opengl.Context) (color.RGBA, error) { func (p *Image) At(x, y int, context *opengl.Context) (color.RGBA, error) {
w, _ := p.image.Size() w, h := p.image.Size()
w2, h2 := graphics.NextPowerOf2Int(w), graphics.NextPowerOf2Int(h)
if x < 0 || y < 0 || w2 <= x || h2 <= y {
return color.RGBA{}, nil
}
if p.basePixels == nil || p.drawImageHistory != nil || p.stale { if p.basePixels == nil || p.drawImageHistory != nil || p.stale {
if err := p.readPixelsFromVRAM(p.image, context); err != nil { if err := p.readPixelsFromVRAM(p.image, context); err != nil {
return color.RGBA{}, err return color.RGBA{}, err
} }
} }
idx := 4*x + 4*y*graphics.NextPowerOf2Int(w) idx := 4*x + 4*y*w2
r, g, b, a := p.basePixels[idx], p.basePixels[idx+1], p.basePixels[idx+2], p.basePixels[idx+3] r, g, b, a := p.basePixels[idx], p.basePixels[idx+1], p.basePixels[idx+2], p.basePixels[idx+3]
return color.RGBA{r, g, b, a}, nil return color.RGBA{r, g, b, a}, nil
} }