graphics: Add TestImageOutside

This commit is contained in:
Hajime Hoshi 2017-12-14 00:25:35 +09:00
parent 704d4cf464
commit 98532d8983
3 changed files with 51 additions and 2 deletions

View File

@ -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,14 +126,19 @@ 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
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)
i.restorable.DrawImage(img.restorable, vs, &options.ColorM.impl, mode)

View File

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

View File

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