internal/atlas: refactoring: remove arguments from Pixels

This commit is contained in:
Hajime Hoshi 2022-03-20 18:16:57 +09:00
parent 367a9ec5bf
commit 11ff0ab48c
6 changed files with 25 additions and 32 deletions

View File

@ -601,17 +601,17 @@ func (i *Image) replacePixels(pix []byte) {
i.backend.restorable.ReplacePixels(pixb, px, py, pw, ph)
}
func (img *Image) Pixels(graphicsDriver graphicsdriver.Graphics, x, y, width, height int) ([]byte, error) {
func (img *Image) Pixels(graphicsDriver graphicsdriver.Graphics) ([]byte, error) {
backendsM.Lock()
defer backendsM.Unlock()
x += paddingSize
y += paddingSize
x := paddingSize
y := paddingSize
bs := make([]byte, 4*width*height)
bs := make([]byte, 4*img.width*img.height)
idx := 0
for j := y; j < y+height; j++ {
for i := x; i < x+width; i++ {
for j := y; j < y+img.height; j++ {
for i := x; i < x+img.width; i++ {
r, g, b, a, err := img.at(graphicsDriver, i, j)
if err != nil {
return nil, err

View File

@ -110,7 +110,7 @@ func TestEnsureIsolated(t *testing.T) {
t.Errorf("got: %v, want: %v", got, want)
}
pix, err := img4.Pixels(ui.GraphicsDriverForTesting(), 0, 0, size, size)
pix, err := img4.Pixels(ui.GraphicsDriverForTesting())
if err != nil {
t.Fatal(err)
}
@ -202,7 +202,7 @@ func TestReputOnAtlas(t *testing.T) {
t.Fatal(err)
}
pix, err := img1.Pixels(ui.GraphicsDriverForTesting(), 0, 0, size, size)
pix, err := img1.Pixels(ui.GraphicsDriverForTesting())
if err != nil {
t.Fatal(err)
}
@ -226,7 +226,7 @@ func TestReputOnAtlas(t *testing.T) {
t.Errorf("got: %v, want: %v", got, want)
}
pix, err = img1.Pixels(ui.GraphicsDriverForTesting(), 0, 0, size, size)
pix, err = img1.Pixels(ui.GraphicsDriverForTesting())
if err != nil {
t.Fatal(err)
}
@ -314,7 +314,7 @@ func TestExtend(t *testing.T) {
// Ensure to allocate
img1.ReplacePixels(p1)
pix0, err := img0.Pixels(ui.GraphicsDriverForTesting(), 0, 0, w0, h0)
pix0, err := img0.Pixels(ui.GraphicsDriverForTesting())
if err != nil {
t.Fatal(err)
}
@ -333,7 +333,7 @@ func TestExtend(t *testing.T) {
}
}
pix1, err := img1.Pixels(ui.GraphicsDriverForTesting(), 0, 0, w1, h1)
pix1, err := img1.Pixels(ui.GraphicsDriverForTesting())
if err != nil {
t.Fatal(err)
}
@ -380,7 +380,7 @@ func TestReplacePixelsAfterDrawTriangles(t *testing.T) {
dst.DrawTriangles([graphics.ShaderImageNum]*atlas.Image{src}, vs, is, affine.ColorMIdentity{}, graphicsdriver.CompositeModeCopy, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dr, graphicsdriver.Region{}, [graphics.ShaderImageNum - 1][2]float32{}, nil, nil, false)
dst.ReplacePixels(pix)
pix, err := dst.Pixels(ui.GraphicsDriverForTesting(), 0, 0, w, h)
pix, err := dst.Pixels(ui.GraphicsDriverForTesting())
if err != nil {
t.Fatal(err)
}
@ -427,7 +427,7 @@ func TestSmallImages(t *testing.T) {
}
dst.DrawTriangles([graphics.ShaderImageNum]*atlas.Image{src}, vs, is, affine.ColorMIdentity{}, graphicsdriver.CompositeModeSourceOver, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dr, graphicsdriver.Region{}, [graphics.ShaderImageNum - 1][2]float32{}, nil, nil, false)
pix, err := dst.Pixels(ui.GraphicsDriverForTesting(), 0, 0, w, h)
pix, err := dst.Pixels(ui.GraphicsDriverForTesting())
if err != nil {
t.Fatal(err)
}
@ -475,7 +475,7 @@ func TestLongImages(t *testing.T) {
}
dst.DrawTriangles([graphics.ShaderImageNum]*atlas.Image{src}, vs, is, affine.ColorMIdentity{}, graphicsdriver.CompositeModeSourceOver, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dr, graphicsdriver.Region{}, [graphics.ShaderImageNum - 1][2]float32{}, nil, nil, false)
pix, err := dst.Pixels(ui.GraphicsDriverForTesting(), 0, 0, dstW, dstH)
pix, err := dst.Pixels(ui.GraphicsDriverForTesting())
if err != nil {
t.Fatal(err)
}

View File

@ -51,7 +51,7 @@ func TestShaderFillTwice(t *testing.T) {
s1 := atlas.NewShader(&p1)
dst.DrawTriangles([graphics.ShaderImageNum]*atlas.Image{}, vs, is, affine.ColorMIdentity{}, graphicsdriver.CompositeModeCopy, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dr, graphicsdriver.Region{}, [graphics.ShaderImageNum - 1][2]float32{}, s1, nil, false)
pix, err := dst.Pixels(g, 0, 0, w, h)
pix, err := dst.Pixels(g)
if err != nil {
t.Error(err)
}
@ -83,7 +83,7 @@ func TestImageDrawTwice(t *testing.T) {
vs = quadVertices(w, h, 0, 0, 1)
dst.DrawTriangles([graphics.ShaderImageNum]*atlas.Image{src1}, vs, is, affine.ColorMIdentity{}, graphicsdriver.CompositeModeCopy, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dr, graphicsdriver.Region{}, [graphics.ShaderImageNum - 1][2]float32{}, nil, nil, false)
pix, err := dst.Pixels(ui.GraphicsDriverForTesting(), 0, 0, w, h)
pix, err := dst.Pixels(ui.GraphicsDriverForTesting())
if err != nil {
t.Error(err)
}

View File

@ -16,7 +16,6 @@ package buffered
import (
"fmt"
"image"
"github.com/hajimehoshi/ebiten/v2/internal/affine"
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
@ -139,19 +138,13 @@ func (i *Image) MarkDisposed() {
i.img.MarkDisposed()
}
func (img *Image) Pixels(graphicsDriver graphicsdriver.Graphics, x, y, width, height int) (pix []byte, err error) {
func (img *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pix []byte, x, y, width, height int) (err error) {
checkDelayedCommandsFlushed("Pixels")
if !image.Rect(x, y, x+width, y+height).In(image.Rect(0, 0, img.width, img.height)) {
return nil, fmt.Errorf("buffered: out of range")
}
pix = make([]byte, 4*width*height)
if img.pixels == nil {
pix, err := img.img.Pixels(graphicsDriver, 0, 0, img.width, img.height)
pix, err := img.img.Pixels(graphicsDriver)
if err != nil {
return nil, err
return err
}
img.pixels = pix
}
@ -159,7 +152,7 @@ func (img *Image) Pixels(graphicsDriver graphicsdriver.Graphics, x, y, width, he
for j := 0; j < height; j++ {
copy(pix[4*j*width:4*(j+1)*width], img.pixels[4*((j+y)*img.width+x):])
}
return pix, nil
return nil
}
func (i *Image) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, name string, blackbg bool) error {
@ -209,7 +202,7 @@ func (i *Image) ReplacePartialPixels(graphicsDriver graphicsdriver.Graphics, pix
}
if i.pixels == nil {
pix, err := i.img.Pixels(graphicsDriver, 0, 0, i.width, i.height)
pix, err := i.img.Pixels(graphicsDriver)
if err != nil {
return err
}

View File

@ -84,8 +84,8 @@ func (m *Mipmap) ReplacePartialPixels(graphicsDriver graphicsdriver.Graphics, pi
return nil
}
func (m *Mipmap) Pixels(graphicsDriver graphicsdriver.Graphics, x, y, width, height int) ([]byte, error) {
return m.orig.Pixels(graphicsDriver, x, y, width, height)
func (m *Mipmap) ReadPixels(graphicsDriver graphicsdriver.Graphics, pix []byte, x, y, width, height int) error {
return m.orig.ReadPixels(graphicsDriver, pix, x, y, width, height)
}
func (m *Mipmap) DrawTriangles(srcs [graphics.ShaderImageNum]*Mipmap, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageNum - 1][2]float32, shader *Shader, uniforms [][]float32, evenOdd bool, canSkipMipmap bool) {

View File

@ -87,8 +87,8 @@ func (i *Image) Pixels(x, y, width, height int) []byte {
return nil
}
pix, err := i.mipmap.Pixels(graphicsDriver(), x, y, width, height)
if err != nil {
pix := make([]byte, 4*width*height)
if err := i.mipmap.ReadPixels(graphicsDriver(), pix, x, y, width, height); err != nil {
if panicOnErrorOnReadingPixels {
panic(err)
}