graphics: Use ID for dumped internal images

Updates #900
This commit is contained in:
Hajime Hoshi 2019-07-19 23:29:19 +09:00
parent 0793d35c40
commit 94852b07b2
4 changed files with 35 additions and 45 deletions

View File

@ -20,10 +20,8 @@ package ebiten
import ( import (
"fmt" "fmt"
"image"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
"time" "time"
"github.com/hajimehoshi/ebiten/internal/png" "github.com/hajimehoshi/ebiten/internal/png"
@ -94,24 +92,8 @@ func dumpInternalImages() error {
return err return err
} }
dump := func(img image.Image, index int) error { if err := shareable.DumpImages(dir); err != nil {
filename := filepath.Join(dir, fmt.Sprintf("%d.png", index)) return err
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 := fmt.Fprintf(os.Stderr, "Dumped the internal images at: %s\n", dir); err != nil { if _, err := fmt.Fprintf(os.Stderr, "Dumped the internal images at: %s\n", dir); err != nil {

View File

@ -15,9 +15,15 @@
package graphicscommand package graphicscommand
import ( import (
"fmt"
"image"
"os"
"path/filepath"
"github.com/hajimehoshi/ebiten/internal/affine" "github.com/hajimehoshi/ebiten/internal/affine"
"github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/graphics" "github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/png"
) )
type lastCommand int type lastCommand int
@ -170,3 +176,23 @@ func (i *Image) IsInvalidated() bool {
} }
return i.image.IsInvalidated() 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
}

View File

@ -15,8 +15,6 @@
package restorable package restorable
import ( import (
"image"
"github.com/hajimehoshi/ebiten/internal/graphicscommand" "github.com/hajimehoshi/ebiten/internal/graphicscommand"
) )
@ -90,11 +88,10 @@ func RestoreIfNeeded() error {
return theImages.restore() return theImages.restore()
} }
// Images returns all the current images. // DumpImages dumps all the current images to the specified directory.
// //
// This is for testing usage. // This is for testing usage.
func Images() []image.Image { func DumpImages(dir string) error {
var imgs []image.Image
for img := range theImages.images { for img := range theImages.images {
if img.volatile { if img.volatile {
continue continue
@ -102,25 +99,11 @@ func Images() []image.Image {
if img.screen { if img.screen {
continue continue
} }
if err := img.image.DumpAt(dir); err != nil {
w, h := img.Size() return err
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
}
} }
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. // add adds img to the images.

View File

@ -16,7 +16,6 @@ package shareable
import ( import (
"fmt" "fmt"
"image"
"runtime" "runtime"
"sync" "sync"
@ -528,10 +527,10 @@ func RestoreIfNeeded() error {
return restorable.RestoreIfNeeded() return restorable.RestoreIfNeeded()
} }
func Images() []image.Image { func DumpImages(dir string) error {
backendsM.Lock() backendsM.Lock()
defer backendsM.Unlock() defer backendsM.Unlock()
return restorable.Images() return restorable.DumpImages(dir)
} }
func Error() error { func Error() error {