diff --git a/imagedumper_desktop.go b/imagedumper_desktop.go index 655cc9e78..a124458ac 100644 --- a/imagedumper_desktop.go +++ b/imagedumper_desktop.go @@ -20,10 +20,8 @@ package ebiten import ( "fmt" - "image" "io/ioutil" "os" - "path/filepath" "time" "github.com/hajimehoshi/ebiten/internal/png" @@ -94,24 +92,8 @@ func dumpInternalImages() error { return err } - dump := func(img image.Image, index int) error { - filename := filepath.Join(dir, fmt.Sprintf("%d.png", index)) - f, err := os.Create(filename) - if err != nil { - return err - } - defer f.Close() - - if err := png.Encode(f, img); err != nil { - return err - } - return nil - } - - for i, img := range shareable.Images() { - if err := dump(img, i); err != nil { - return err - } + if err := shareable.DumpImages(dir); err != nil { + return err } if _, err := fmt.Fprintf(os.Stderr, "Dumped the internal images at: %s\n", dir); err != nil { diff --git a/internal/graphicscommand/image.go b/internal/graphicscommand/image.go index 725b2d07e..e842cf096 100644 --- a/internal/graphicscommand/image.go +++ b/internal/graphicscommand/image.go @@ -15,9 +15,15 @@ package graphicscommand import ( + "fmt" + "image" + "os" + "path/filepath" + "github.com/hajimehoshi/ebiten/internal/affine" "github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/graphics" + "github.com/hajimehoshi/ebiten/internal/png" ) type lastCommand int @@ -170,3 +176,23 @@ func (i *Image) IsInvalidated() bool { } return i.image.IsInvalidated() } + +// DumpImages dumps the image to the specified directory. +// +// This is for testing usage. +func (i *Image) DumpAt(dir string) error { + f, err := os.Create(filepath.Join(dir, fmt.Sprintf("%d.png", i.id))) + if err != nil { + return err + } + defer f.Close() + + if err := png.Encode(f, &image.RGBA{ + Pix: i.Pixels(), + Stride: 4 * i.width, + Rect: image.Rect(0, 0, i.width, i.height), + }); err != nil { + return err + } + return nil +} diff --git a/internal/restorable/images.go b/internal/restorable/images.go index 4c91bb369..89e637f8e 100644 --- a/internal/restorable/images.go +++ b/internal/restorable/images.go @@ -15,8 +15,6 @@ package restorable import ( - "image" - "github.com/hajimehoshi/ebiten/internal/graphicscommand" ) @@ -90,11 +88,10 @@ func RestoreIfNeeded() error { return theImages.restore() } -// Images returns all the current images. +// DumpImages dumps all the current images to the specified directory. // // This is for testing usage. -func Images() []image.Image { - var imgs []image.Image +func DumpImages(dir string) error { for img := range theImages.images { if img.volatile { continue @@ -102,25 +99,11 @@ func Images() []image.Image { if img.screen { continue } - - w, h := img.Size() - pix := make([]byte, 4*w*h) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { - r, g, b, a := img.At(i, j) - pix[4*(i+j*w)] = r - pix[4*(i+j*w)+1] = g - pix[4*(i+j*w)+2] = b - pix[4*(i+j*w)+3] = a - } + if err := img.image.DumpAt(dir); err != nil { + return err } - imgs = append(imgs, &image.RGBA{ - Pix: pix, - Stride: 4 * w, - Rect: image.Rect(0, 0, w, h), - }) } - return imgs + return nil } // add adds img to the images. diff --git a/internal/shareable/shareable.go b/internal/shareable/shareable.go index 8cb40a0ca..afc622ccc 100644 --- a/internal/shareable/shareable.go +++ b/internal/shareable/shareable.go @@ -16,7 +16,6 @@ package shareable import ( "fmt" - "image" "runtime" "sync" @@ -528,10 +527,10 @@ func RestoreIfNeeded() error { return restorable.RestoreIfNeeded() } -func Images() []image.Image { +func DumpImages(dir string) error { backendsM.Lock() defer backendsM.Unlock() - return restorable.Images() + return restorable.DumpImages(dir) } func Error() error {