ebiten: Add (*Image).RGBA64At (#1773)

Closes #1769.
This commit is contained in:
Trevor Slocum 2021-08-20 23:15:48 -07:00 committed by GitHub
parent c1190c4633
commit 92d8562b1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 3 deletions

View File

@ -708,6 +708,22 @@ func (i *Image) At(x, y int) color.Color {
return color.RGBA{pix[0], pix[1], pix[2], pix[3]}
}
// RGBA64At implements image.RGBA64Image's RGBA64At.
//
// RGBA64At loads pixels from GPU to system memory if necessary, which means
// that RGBA64At can be slow.
//
// RGBA64At always returns a transparent color if the image is disposed.
//
// Note that an important logic should not rely on values returned by RGBA64At,
// since the returned values can include very slight differences between some machines.
//
// RGBA64At can't be called outside the main loop (ebiten.Run's updating function) starts.
func (i *Image) RGBA64At(x, y int) color.RGBA64 {
r, g, b, a := i.At(x, y).(color.RGBA).RGBA()
return color.RGBA64{uint16(r), uint16(g), uint16(b), uint16(a)}
}
// Set sets the color at (x, y).
//
// Set loads pixels from GPU to system memory if necessary, which means that Set can be slow.

View File

@ -1582,18 +1582,29 @@ func TestImageAtAfterDisposingSubImage(t *testing.T) {
img.Set(0, 0, color.White)
img.SubImage(image.Rect(0, 0, 16, 16))
runtime.GC()
got := img.At(0, 0)
want := color.RGBA{0xff, 0xff, 0xff, 0xff}
want64 := color.RGBA64{0xffff, 0xffff, 0xffff, 0xffff}
got := img.At(0, 0)
if got != want {
t.Errorf("got: %v, want: %v", got, want)
t.Errorf("At(0,0) got: %v, want: %v", got, want)
}
got = img.RGBA64At(0, 0)
if got != want64 {
t.Errorf("RGBA64At(0,0) got: %v, want: %v", got, want)
}
img.Set(0, 1, color.White)
sub := img.SubImage(image.Rect(0, 0, 16, 16)).(*Image)
sub.Dispose()
got = img.At(0, 1)
if got != want {
t.Errorf("got: %v, want: %v", got, want)
t.Errorf("At(0,1) got: %v, want: %v", got, want64)
}
got = img.RGBA64At(0, 1)
if got != want64 {
t.Errorf("RGBA64At(0,1) got: %v, want: %v", got, want64)
}
}