restorable: Refactoring: Disallow nil at ReplacePixels

This commit is contained in:
Hajime Hoshi 2019-07-16 01:44:56 +09:00
parent 052697e305
commit f3fa535afb
2 changed files with 5 additions and 6 deletions

View File

@ -306,6 +306,10 @@ func (i *Image) ClearPixels(x, y, width, height int) {
//
// If pixels is nil, ReplacePixels clears the specified reagion.
func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {
if pixels == nil {
panic("restorable: pixels must not be nil")
}
w, h := i.image.Size()
if width <= 0 || height <= 0 {
panic("restorable: width/height must be positive")
@ -318,11 +322,6 @@ func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {
// For this purpuse, images should remember which part of that is used for DrawTriangles.
theImages.makeStaleIfDependingOn(i)
if pixels == nil {
// TODO: Allocating bytes for all pixels are wasteful. Allocate memory only for required regions
// (#897).
pixels = make([]byte, 4*width*height)
}
i.image.ReplacePixels(pixels, x, y, width, height)
if !needsRestoring() {

View File

@ -540,7 +540,7 @@ func TestClear(t *testing.T) {
img := NewImage(4, 4)
img.ReplacePixels(pix, 0, 0, 4, 4)
// This doesn't make the image stale. Its base pixels are available.
img.ReplacePixels(nil, 1, 1, 2, 2)
img.ReplacePixels(make([]byte, 4*4*4), 1, 1, 2, 2)
cases := []struct {
Index int