From caf4e9504f2a247d6af941b451428d1bd904718c Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 3 Mar 2018 20:35:44 +0900 Subject: [PATCH] graphics: ReplacePixels should work on shared textures --- image.go | 45 ++++++++++++++++++++++++--------------------- image_test.go | 5 ++++- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/image.go b/image.go index 81b30ae51..dc95b8a60 100644 --- a/image.go +++ b/image.go @@ -147,16 +147,6 @@ func (i *Image) isDisposed() bool { return i.restorable == nil && i.sharedImagePart == nil } -func (i *Image) restorableImage() *restorable.Image { - if i.restorable != nil { - return i.restorable - } - if i.sharedImagePart != nil { - return i.sharedImagePart.image() - } - return nil -} - // DrawImage draws the given image on the image i. // // DrawImage accepts the options. For details, see the document of DrawImageOptions. @@ -355,6 +345,27 @@ func (i *Image) Dispose() error { return nil } +func (i *Image) region() (x, y, width, height int) { + if i.restorable != nil { + w, h := i.restorable.Size() + return 0, 0, w, h + } + if i.sharedImagePart != nil { + return i.sharedImagePart.region() + } + panic("not reached") +} + +func (i *Image) restorableImage() *restorable.Image { + if i.restorable != nil { + return i.restorable + } + if i.sharedImagePart != nil { + return i.sharedImagePart.image() + } + return nil +} + // ReplacePixels replaces the pixels of the image with p. // // The given p must represent RGBA pre-multiplied alpha values. len(p) must equal to 4 * (image width) * (image height). @@ -368,15 +379,14 @@ func (i *Image) Dispose() error { // ReplacePixels always returns nil as of 1.5.0-alpha. func (i *Image) ReplacePixels(p []byte) error { i.copyCheck() - i.ensureNotShared() if i.isDisposed() { return nil } - w, h := i.Size() + x, y, w, h := i.region() if l := 4 * w * h; len(p) != l { panic(fmt.Sprintf("ebiten: len(p) was %d but must be %d", len(p), l)) } - i.restorable.ReplacePixels(p, 0, 0, w, h) + i.restorableImage().ReplacePixels(p, x, y, w, h) return nil } @@ -515,14 +525,7 @@ func NewImageFromImage(source image.Image, filter Filter) (*Image, error) { for j := 0; j < height; j++ { copy(p[j*width*4:(j+1)*width*4], rgbaImg.Pix[j*rgbaImg.Stride:]) } - - if s == nil { - _ = i.ReplacePixels(p) - } else { - x, y, width, height := s.region() - s.image().ReplacePixels(p, x, y, width, height) - } - + _ = i.ReplacePixels(p) return i, nil } diff --git a/image_test.go b/image_test.go index b32c1ced8..7a579bf9f 100644 --- a/image_test.go +++ b/image_test.go @@ -318,6 +318,10 @@ func TestImageDotByDotInversion(t *testing.T) { } func TestReplacePixels(t *testing.T) { + // Create a dummy image so that the shared texture is use and origImg's position is shfited. + dummyImg, _ := NewImageFromImage(image.NewRGBA(image.Rect(0, 0, 16, 16)), FilterDefault) + defer dummyImg.Dispose() + origImg, err := openImage("testdata/ebiten.png") if err != nil { t.Fatal(err) @@ -369,7 +373,6 @@ func TestReplacePixels(t *testing.T) { } } } - } func TestImageDispose(t *testing.T) {