diff --git a/internal/restorable/image.go b/internal/restorable/image.go index 93dfcd746..d07e5f680 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -169,10 +169,16 @@ func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) { } if x == 0 && y == 0 && width == w && height == h { - if i.basePixels == nil { - i.basePixels = make([]byte, 4*w*h) + if pixels != nil { + if i.basePixels == nil { + i.basePixels = make([]byte, 4*w*h) + } + copy(i.basePixels, pixels) + } else { + // If basePixels is nil, the restored pixels are cleared. + // See restore() implementation. + i.basePixels = nil } - copy(i.basePixels, pixels) i.drawImageHistory = nil i.stale = false return diff --git a/internal/restorable/images_test.go b/internal/restorable/images_test.go index 2ef0a620c..86b89d4be 100644 --- a/internal/restorable/images_test.go +++ b/internal/restorable/images_test.go @@ -97,6 +97,26 @@ func TestRestore(t *testing.T) { } } +func TestRestoreWithoutDraw(t *testing.T) { + img0 := NewImage(1024, 1024, false) + defer img0.Dispose() + + // If there is no drawing command on img0, img0 is cleared when restored. + + ResolveStaleImages() + if err := Restore(); err != nil { + t.Fatal(err) + } + + for i := 0; i < 1024*1024; i++ { + want := color.RGBA{0x00, 0x00, 0x00, 0x00} + got := byteSliceToColor(img0.BasePixelsForTesting(), i) + if !sameColors(got, want, 0) { + t.Errorf("got %v, want %v", got, want) + } + } +} + func TestRestoreChain(t *testing.T) { const num = 10 imgs := []*Image{}