graphics: Bug fix: SubImage's SubImage's bounds was wrong

This commit is contained in:
Hajime Hoshi 2019-02-21 23:44:57 +09:00
parent 8f133c443e
commit 5ed6565d1d
2 changed files with 36 additions and 1 deletions

View File

@ -507,7 +507,7 @@ func (i *Image) SubImage(r image.Rectangle) image.Image {
}
img.addr = img
r = r.Intersect(img.Bounds())
r = r.Intersect(i.Bounds())
// Need to check Empty explicitly. See the standard image package implementations.
if r.Empty() {
img.bounds = &image.ZR

View File

@ -1617,3 +1617,38 @@ func TestImageAtAfterDisposingSubImage(t *testing.T) {
t.Errorf("got: %v, want: %v", got, want)
}
}
func TestImageSubImageSubImage(t *testing.T) {
img, _ := NewImage(16, 16, FilterDefault)
img.Fill(color.White)
sub0 := img.SubImage(image.Rect(0, 0, 12, 12)).(*Image)
sub1 := sub0.SubImage(image.Rect(4, 4, 16, 16)).(*Image)
cases := []struct {
X int
Y int
Color color.RGBA
}{
{
X: 0,
Y: 0,
Color: color.RGBA{},
},
{
X: 4,
Y: 4,
Color: color.RGBA{0xff, 0xff, 0xff, 0xff},
},
{
X: 15,
Y: 15,
Color: color.RGBA{},
},
}
for _, c := range cases {
got := sub1.At(c.X, c.Y)
want := c.Color
if got != want {
t.Errorf("At(%d, %d): got: %v, want: %v", c.X, c.Y, got, want)
}
}
}