From 98532d8983ffa27ed25449289c98b43d9fd79557 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 14 Dec 2017 00:25:35 +0900 Subject: [PATCH] graphics: Add TestImageOutside --- image.go | 11 +++++++++-- image_test.go | 39 +++++++++++++++++++++++++++++++++++++ internal/graphics/shader.go | 3 +++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/image.go b/image.go index abaa7c0f2..5e44c85c0 100644 --- a/image.go +++ b/image.go @@ -98,11 +98,13 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error { if options == nil { options = &DrawImageOptions{} } + parts := options.ImageParts // Parts is deprecated. This implementations is for backward compatibility. if parts == nil && options.Parts != nil { parts = imageParts(options.Parts) } + // ImageParts is deprecated. This implementations is for backward compatibility. if parts != nil { l := parts.Len() @@ -124,13 +126,18 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error { } return nil } + w, h := img.restorable.Size() sx0, sy0, sx1, sy1 := 0, 0, w, h if r := options.SourceRect; r != nil { sx0 = r.Min.X sy0 = r.Min.Y - sx1 = r.Max.X - sy1 = r.Max.Y + if sx1 > r.Max.X { + sx1 = r.Max.X + } + if sy1 > r.Max.Y { + sy1 = r.Max.Y + } } vs := vertices(sx0, sy0, sx1, sy1, w, h, &options.GeoM.impl) mode := opengl.CompositeMode(options.CompositeMode) diff --git a/image_test.go b/image_test.go index 9a0d317e5..9ccc2ae80 100644 --- a/image_test.go +++ b/image_test.go @@ -621,3 +621,42 @@ func TestImageLinear(t *testing.T) { } } } + +func TestImageOutside(t *testing.T) { + src, _ := NewImage(5, 10, FilterNearest) // internal texture size is 16x16. + dst, _ := NewImage(4, 4, FilterNearest) + src.Fill(color.RGBA{0xff, 0, 0, 0xff}) + + cases := []struct { + X, Y int + }{ + {-4, -4}, + {5, 0}, + {0, 10}, + {5, 10}, + {16, 0}, + {0, 16}, + {16, 16}, + {16, -4}, + {-4, 16}, + } + for _, c := range cases { + dst.Clear() + + op := &DrawImageOptions{} + op.GeoM.Translate(0, 0) + r := image.Rect(c.X, c.Y, c.X+4, c.Y+4) + op.SourceRect = &r + dst.DrawImage(src, op) + + for j := 0; j < 4; j++ { + for i := 0; i < 4; i++ { + got := color.RGBAModel.Convert(dst.At(i, j)).(color.RGBA) + want := color.RGBA{0, 0, 0, 0} + if got != want { + t.Errorf("src(%d, %d), dst At(%d, %d): got %#v, want: %#v", c.X, c.Y, i, j, got, want) + } + } + } + } +} diff --git a/internal/graphics/shader.go b/internal/graphics/shader.go index f98206ada..18cd9a5b6 100644 --- a/internal/graphics/shader.go +++ b/internal/graphics/shader.go @@ -83,6 +83,9 @@ highp vec2 roundTexel(highp vec2 p) { } vec4 getColorAt(highp vec2 pos) { + if (pos.x < 0.0 || pos.y < 0.0) { + return vec4(0, 0, 0, 0); + } if (pos.x < varying_tex_coord_min.x || pos.y < varying_tex_coord_min.y || varying_tex_coord_max.x <= pos.x ||