restorable: Refactoring: Remove arguments from NewImageFromImage

This commit is contained in:
Hajime Hoshi 2017-09-15 00:48:44 +09:00
parent ca4a2d8209
commit ccddf3c4ac
4 changed files with 13 additions and 15 deletions

View File

@ -266,10 +266,8 @@ func newVolatileImage(width, height int, filter Filter) *Image {
// Error returned by NewImageFromImage is always nil as of 1.5.0-alpha.
func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
size := source.Bounds().Size()
w, h := size.X, size.Y
checkSize(w, h)
rgbaImg := restorable.CopyImage(source)
r := restorable.NewImageFromImage(rgbaImg, w, h, glFilter(filter))
checkSize(size.X, size.Y)
r := restorable.NewImageFromImage(source, glFilter(filter))
i := &Image{r}
runtime.SetFinalizer(i, (*Image).Dispose)
return i, nil

View File

@ -28,8 +28,7 @@ import (
// Basically CopyImage just calls draw.Draw.
// If origImg is a paletted image, an optimized copying method is used.
//
// CopyImage is used only from ebiten package but defined in restorable package
// because this function needs to be tested but cannot be exposed to Ebiten users.
// CopyImage is used only internally but it is exposed for testing.
func CopyImage(origImg image.Image) *image.RGBA {
size := origImg.Bounds().Size()
w, h := size.X, size.Y

View File

@ -93,16 +93,17 @@ func NewImage(width, height int, filter opengl.Filter, volatile bool) *Image {
}
// NewImageFromImage creates an image with source image.
//
// width/height might be different from source's size.
func NewImageFromImage(source *image.RGBA, width, height int, filter opengl.Filter) *Image {
func NewImageFromImage(source image.Image, filter opengl.Filter) *Image {
size := source.Bounds().Size()
width, height := size.X, size.Y
rgbaImg := CopyImage(source)
w2, h2 := math.NextPowerOf2Int(width), math.NextPowerOf2Int(height)
p := make([]uint8, 4*w2*h2)
for j := 0; j < height; j++ {
copy(p[j*w2*4:(j+1)*w2*4], source.Pix[j*source.Stride:])
copy(p[j*w2*4:(j+1)*w2*4], rgbaImg.Pix[j*rgbaImg.Stride:])
}
i := &Image{
image: graphics.NewImageFromImage(source, width, height, filter),
image: graphics.NewImageFromImage(rgbaImg, width, height, filter),
basePixels: p,
filter: filter,
}

View File

@ -192,9 +192,9 @@ func TestRestoreComplexGraph(t *testing.T) {
base.Pix[1] = 0xff
base.Pix[2] = 0xff
base.Pix[3] = 0xff
img0 := NewImageFromImage(base, 4, 1, opengl.Nearest)
img1 := NewImageFromImage(base, 4, 1, opengl.Nearest)
img2 := NewImageFromImage(base, 4, 1, opengl.Nearest)
img0 := NewImageFromImage(base, opengl.Nearest)
img1 := NewImageFromImage(base, opengl.Nearest)
img2 := NewImageFromImage(base, opengl.Nearest)
img3 := NewImage(4, 1, opengl.Nearest, false)
img3.Fill(0, 0, 0, 0)
img4 := NewImage(4, 1, opengl.Nearest, false)
@ -296,7 +296,7 @@ func TestRestoreRecursive(t *testing.T) {
base.Pix[1] = 0xff
base.Pix[2] = 0xff
base.Pix[3] = 0xff
img0 := NewImageFromImage(base, 4, 1, opengl.Nearest)
img0 := NewImageFromImage(base, opengl.Nearest)
img1 := NewImage(4, 1, opengl.Nearest, false)
img1.Fill(0, 0, 0, 0)
defer func() {