From 18903db1c6029ba5b7e00edb5453f97085ae3bbc Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 29 Jul 2021 16:09:25 +0900 Subject: [PATCH] internal/atlas: Bug fix: Unexpected padding in screenshots Closes #1736 --- imagedumper_desktop.go | 2 +- internal/atlas/image.go | 5 +++-- internal/buffered/image.go | 4 ++-- internal/graphicscommand/image.go | 6 +++--- internal/mipmap/mipmap.go | 4 ++-- internal/restorable/image.go | 5 +++-- internal/restorable/images.go | 3 ++- 7 files changed, 16 insertions(+), 13 deletions(-) diff --git a/imagedumper_desktop.go b/imagedumper_desktop.go index 7264f5103..15141edb3 100644 --- a/imagedumper_desktop.go +++ b/imagedumper_desktop.go @@ -52,7 +52,7 @@ func takeScreenshot(screen *Image) error { } blackbg := !IsScreenTransparent() - if err := screen.mipmap.Dump(newname, blackbg); err != nil { + if err := screen.mipmap.DumpScreenshot(newname, blackbg); err != nil { return err } diff --git a/internal/atlas/image.go b/internal/atlas/image.go index edf70f9ed..e5cd447b8 100644 --- a/internal/atlas/image.go +++ b/internal/atlas/image.go @@ -16,6 +16,7 @@ package atlas import ( "fmt" + "image" "runtime" "sync" @@ -725,11 +726,11 @@ func (i *Image) allocate(putOnAtlas bool) { i.node = n } -func (i *Image) Dump(path string, blackbg bool) error { +func (i *Image) DumpScreenshot(path string, blackbg bool) error { backendsM.Lock() defer backendsM.Unlock() - return i.backend.restorable.Dump(path, blackbg) + return i.backend.restorable.Dump(path, blackbg, image.Rect(paddingSize, paddingSize, i.width-2*paddingSize, i.height-2*paddingSize)) } func NewScreenFramebufferImage(width, height int) *Image { diff --git a/internal/buffered/image.go b/internal/buffered/image.go index db35efccd..84bd99ceb 100644 --- a/internal/buffered/image.go +++ b/internal/buffered/image.go @@ -149,9 +149,9 @@ func (img *Image) Pixels(x, y, width, height int) (pix []byte, err error) { return pix, nil } -func (i *Image) Dump(name string, blackbg bool) error { +func (i *Image) DumpScreenshot(name string, blackbg bool) error { checkDelayedCommandsFlushed("Dump") - return i.img.Dump(name, blackbg) + return i.img.DumpScreenshot(name, blackbg) } func (i *Image) ReplacePixels(pix []byte, x, y, width, height int) error { diff --git a/internal/graphicscommand/image.go b/internal/graphicscommand/image.go index 54f56bd86..2f69a1805 100644 --- a/internal/graphicscommand/image.go +++ b/internal/graphicscommand/image.go @@ -207,7 +207,7 @@ func (i *Image) IsInvalidated() bool { // If blackbg is true, any alpha values in the dumped image will be 255. // // This is for testing usage. -func (i *Image) Dump(path string, blackbg bool) error { +func (i *Image) Dump(path string, blackbg bool, rect image.Rectangle) error { // Screen image cannot be dumped. if i.screen { return nil @@ -231,11 +231,11 @@ func (i *Image) Dump(path string, blackbg bool) error { } } - if err := png.Encode(f, &image.RGBA{ + if err := png.Encode(f, (&image.RGBA{ Pix: pix, Stride: 4 * i.width, Rect: image.Rect(0, 0, i.width, i.height), - }); err != nil { + }).SubImage(rect)); err != nil { return err } return nil diff --git a/internal/mipmap/mipmap.go b/internal/mipmap/mipmap.go index e15569bff..90f2b923d 100644 --- a/internal/mipmap/mipmap.go +++ b/internal/mipmap/mipmap.go @@ -69,8 +69,8 @@ func (m *Mipmap) SetVolatile(volatile bool) { m.orig.SetVolatile(volatile) } -func (m *Mipmap) Dump(name string, blackbg bool) error { - return m.orig.Dump(name, blackbg) +func (m *Mipmap) DumpScreenshot(name string, blackbg bool) error { + return m.orig.DumpScreenshot(name, blackbg) } func (m *Mipmap) ReplacePixels(pix []byte, x, y, width, height int) error { diff --git a/internal/restorable/image.go b/internal/restorable/image.go index 5dffbcb5f..7b9fe6aa3 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -16,6 +16,7 @@ package restorable import ( "fmt" + "image" "github.com/hajimehoshi/ebiten/v2/internal/affine" "github.com/hajimehoshi/ebiten/v2/internal/driver" @@ -648,6 +649,6 @@ func (i *Image) isInvalidated() (bool, error) { return i.image.IsInvalidated(), nil } -func (i *Image) Dump(path string, blackbg bool) error { - return i.image.Dump(path, blackbg) +func (i *Image) Dump(path string, blackbg bool, rect image.Rectangle) error { + return i.image.Dump(path, blackbg, rect) } diff --git a/internal/restorable/images.go b/internal/restorable/images.go index f4878f13b..c2ec78847 100644 --- a/internal/restorable/images.go +++ b/internal/restorable/images.go @@ -15,6 +15,7 @@ package restorable import ( + "image" "path/filepath" "github.com/hajimehoshi/ebiten/v2/internal/driver" @@ -115,7 +116,7 @@ func RestoreIfNeeded() error { // This is for testing usage. func DumpImages(dir string) error { for img := range theImages.images { - if err := img.Dump(filepath.Join(dir, "*.png"), false); err != nil { + if err := img.Dump(filepath.Join(dir, "*.png"), false, image.Rect(0, 0, img.width, img.height)); err != nil { return err } }