From 15d48703fc04625acdee7ddbac59e53b5a29bcaf Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 12 May 2016 02:00:22 +0900 Subject: [PATCH] graphics: Bug fix: NewImageFromImage with ebiten.Image froze (#212) --- image.go | 5 +++ image_test.go | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 image_test.go diff --git a/image.go b/image.go index 2a1ab04c8..2a1a5c874 100644 --- a/image.go +++ b/image.go @@ -338,6 +338,11 @@ func NewImage(width, height int, filter Filter) (*Image, error) { // // This function is concurrent-safe. func NewImageFromImage(img image.Image, filter Filter) (*Image, error) { + // Can't call (*ebiten.Image).At here because of the lock. + if _, ok := img.(*Image); ok { + return nil, errors.New("ebiten: NewImageFromImage can't take *ebiten.Image") + } + imageM.Lock() defer imageM.Unlock() size := img.Bounds().Size() diff --git a/image_test.go b/image_test.go new file mode 100644 index 000000000..918ab8ab9 --- /dev/null +++ b/image_test.go @@ -0,0 +1,84 @@ +// Copyright 2016 Hajime Hoshi +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ebiten_test + +import ( + "image" + _ "image/png" + "testing" + + . "github.com/hajimehoshi/ebiten" +) + +var ebitenImageBin = "" + +func openImage(path string) (image.Image, error) { + file, err := readFile(path) + if err != nil { + return nil, err + } + + img, _, err := image.Decode(file) + if err != nil { + return nil, err + } + return img, nil +} + +func openEbitenImage(path string) (*Image, image.Image, error) { + img, err := openImage(path) + if err != nil { + return nil, nil, err + } + + eimg, err := NewImageFromImage(img, FilterNearest) + if err != nil { + return nil, nil, err + } + return eimg, img, nil +} + +func TestImageSelf(t *testing.T) { + img, _, err := openEbitenImage("testdata/ebiten.png") + if err != nil { + t.Fatal(err) + return + } + if err := img.DrawImage(img, nil); err == nil { + t.Fatalf("img.DrawImage(img, nil) doesn't return error; an error should be returned") + } +} + +func TestImageDispose(t *testing.T) { + img, err := NewImage(16, 16, FilterNearest) + if err != nil { + t.Fatal(err) + return + } + if err := img.Dispose(); err != nil { + t.Errorf("img.Dipose() returns error: %v", err) + } +} + +func TestNewImageFromEbitenImage(t *testing.T) { + img, _, err := openEbitenImage("testdata/ebiten.png") + if err != nil { + t.Fatal(err) + return + } + if _, err := NewImageFromImage(img, FilterNearest); err == nil { + t.Errorf("NewImageFromImage with an *ebiten.Image must return an error") + } +}