From 0967df7f5e3b7dca264f326d90bae3e675347e23 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 5 Feb 2017 04:24:07 +0900 Subject: [PATCH] graphics: Fix TestImagePixels to check out-of-range pixels --- image_test.go | 8 ++++++-- internal/restorable/image.go | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/image_test.go b/image_test.go index 825a7ac22..d5022761e 100644 --- a/image_test.go +++ b/image_test.go @@ -25,6 +25,7 @@ import ( "testing" . "github.com/hajimehoshi/ebiten" + "github.com/hajimehoshi/ebiten/internal/graphics" ) 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()) } - for j := 0; j < img0.Bounds().Size().Y; j++ { - for i := 0; i < img0.Bounds().Size().X; i++ { + w, h := img0.Bounds().Size().X, img0.Bounds().Size().Y + // 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) want := color.RGBAModel.Convert(img.At(i, j)) if got != want { diff --git a/internal/restorable/image.go b/internal/restorable/image.go index 4dc6ab19b..3d4bf6af2 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -170,13 +170,17 @@ func (p *Image) appendDrawImageHistory(image *Image, vertices []float32, colorm // Note that this must not be called until context is available. // This means Pixels members must match with acutal state in VRAM. 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 err := p.readPixelsFromVRAM(p.image, context); err != nil { 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] return color.RGBA{r, g, b, a}, nil }