graphics: Forbid nil at ReplacePixels explicitly

This commit is contained in:
Hajime Hoshi 2018-11-28 22:24:04 +01:00
parent 1f538af440
commit 1a54ff34e6
3 changed files with 23 additions and 3 deletions

View File

@ -15,6 +15,7 @@
package ebiten
import (
"fmt"
"image"
"image/color"
"math"
@ -616,6 +617,10 @@ func (i *Image) ReplacePixels(p []byte) error {
if i.isSubimage() {
panic("render to a subimage is not implemented")
}
s := i.Bounds().Size()
if l := 4 * s.X * s.Y; len(p) != l {
panic(fmt.Sprintf("ebiten: len(p) was %d but must be %d", len(p), l))
}
i.mipmap.original().ReplacePixels(p)
i.disposeMipmaps()
return nil

View File

@ -368,6 +368,22 @@ func TestImageReplacePixels(t *testing.T) {
}
}
func TestImageReplacePixelsNil(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("ReplacePixels(nil) must panic")
}
}()
img, err := NewImage(16, 16, FilterNearest)
if err != nil {
t.Fatal(err)
return
}
img.Fill(color.White)
img.ReplacePixels(nil)
}
func TestImageDispose(t *testing.T) {
img, err := NewImage(16, 16, FilterNearest)
if err != nil {
@ -799,7 +815,7 @@ func TestImageSize4096(t *testing.T) {
func TestImageCopy(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("copying image and using it should panic")
t.Errorf("copying image and using it must panic")
}
}()

View File

@ -15,7 +15,6 @@
package shareable
import (
"fmt"
"image"
"image/color"
"runtime"
@ -259,7 +258,7 @@ func (i *Image) replacePixels(p []byte) {
x, y, w, h := i.region()
if p != nil {
if l := 4 * w * h; len(p) != l {
panic(fmt.Sprintf("shareable: len(p) was %d but must be %d", len(p), l))
panic("not reached")
}
}
i.backend.restorable.ReplacePixels(p, x, y, w, h)