internal/atlas: Bug fix: Unexpected padding in screenshots

Closes #1736
This commit is contained in:
Hajime Hoshi 2021-07-29 16:09:25 +09:00
parent 6c54fac018
commit 18903db1c6
7 changed files with 16 additions and 13 deletions

View File

@ -52,7 +52,7 @@ func takeScreenshot(screen *Image) error {
} }
blackbg := !IsScreenTransparent() blackbg := !IsScreenTransparent()
if err := screen.mipmap.Dump(newname, blackbg); err != nil { if err := screen.mipmap.DumpScreenshot(newname, blackbg); err != nil {
return err return err
} }

View File

@ -16,6 +16,7 @@ package atlas
import ( import (
"fmt" "fmt"
"image"
"runtime" "runtime"
"sync" "sync"
@ -725,11 +726,11 @@ func (i *Image) allocate(putOnAtlas bool) {
i.node = n i.node = n
} }
func (i *Image) Dump(path string, blackbg bool) error { func (i *Image) DumpScreenshot(path string, blackbg bool) error {
backendsM.Lock() backendsM.Lock()
defer backendsM.Unlock() 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 { func NewScreenFramebufferImage(width, height int) *Image {

View File

@ -149,9 +149,9 @@ func (img *Image) Pixels(x, y, width, height int) (pix []byte, err error) {
return pix, nil return pix, nil
} }
func (i *Image) Dump(name string, blackbg bool) error { func (i *Image) DumpScreenshot(name string, blackbg bool) error {
checkDelayedCommandsFlushed("Dump") 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 { func (i *Image) ReplacePixels(pix []byte, x, y, width, height int) error {

View File

@ -207,7 +207,7 @@ func (i *Image) IsInvalidated() bool {
// If blackbg is true, any alpha values in the dumped image will be 255. // If blackbg is true, any alpha values in the dumped image will be 255.
// //
// This is for testing usage. // 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. // Screen image cannot be dumped.
if i.screen { if i.screen {
return nil 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, Pix: pix,
Stride: 4 * i.width, Stride: 4 * i.width,
Rect: image.Rect(0, 0, i.width, i.height), Rect: image.Rect(0, 0, i.width, i.height),
}); err != nil { }).SubImage(rect)); err != nil {
return err return err
} }
return nil return nil

View File

@ -69,8 +69,8 @@ func (m *Mipmap) SetVolatile(volatile bool) {
m.orig.SetVolatile(volatile) m.orig.SetVolatile(volatile)
} }
func (m *Mipmap) Dump(name string, blackbg bool) error { func (m *Mipmap) DumpScreenshot(name string, blackbg bool) error {
return m.orig.Dump(name, blackbg) return m.orig.DumpScreenshot(name, blackbg)
} }
func (m *Mipmap) ReplacePixels(pix []byte, x, y, width, height int) error { func (m *Mipmap) ReplacePixels(pix []byte, x, y, width, height int) error {

View File

@ -16,6 +16,7 @@ package restorable
import ( import (
"fmt" "fmt"
"image"
"github.com/hajimehoshi/ebiten/v2/internal/affine" "github.com/hajimehoshi/ebiten/v2/internal/affine"
"github.com/hajimehoshi/ebiten/v2/internal/driver" "github.com/hajimehoshi/ebiten/v2/internal/driver"
@ -648,6 +649,6 @@ func (i *Image) isInvalidated() (bool, error) {
return i.image.IsInvalidated(), nil return i.image.IsInvalidated(), nil
} }
func (i *Image) Dump(path string, blackbg bool) error { func (i *Image) Dump(path string, blackbg bool, rect image.Rectangle) error {
return i.image.Dump(path, blackbg) return i.image.Dump(path, blackbg, rect)
} }

View File

@ -15,6 +15,7 @@
package restorable package restorable
import ( import (
"image"
"path/filepath" "path/filepath"
"github.com/hajimehoshi/ebiten/v2/internal/driver" "github.com/hajimehoshi/ebiten/v2/internal/driver"
@ -115,7 +116,7 @@ func RestoreIfNeeded() error {
// This is for testing usage. // This is for testing usage.
func DumpImages(dir string) error { func DumpImages(dir string) error {
for img := range theImages.images { 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 return err
} }
} }