graphics: Remove NewImageFromImage command

This commit is contained in:
Hajime Hoshi 2018-02-25 23:13:06 +09:00
parent 9adc1ac6a0
commit 85b133dad0
5 changed files with 28 additions and 76 deletions

View File

@ -398,12 +398,22 @@ func newVolatileImage(width, height int, filter Filter) *Image {
func NewImageFromImage(source image.Image, filter Filter) (*Image, error) { func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
size := source.Bounds().Size() size := source.Bounds().Size()
checkSize(size.X, size.Y) checkSize(size.X, size.Y)
r := restorable.NewImageFromImage(source) width, height := size.X, size.Y
rgbaImg := restorable.CopyImage(source)
p := make([]byte, 4*width*height)
for j := 0; j < height; j++ {
copy(p[j*width*4:(j+1)*width*4], rgbaImg.Pix[j*rgbaImg.Stride:])
}
r := restorable.NewImage(width, height, false)
i := &Image{ i := &Image{
restorable: r, restorable: r,
filter: filter, filter: filter,
} }
runtime.SetFinalizer(i, (*Image).Dispose) runtime.SetFinalizer(i, (*Image).Dispose)
_ = i.ReplacePixels(p)
return i, nil return i, nil
} }

View File

@ -17,7 +17,6 @@ package graphics
import ( import (
"errors" "errors"
"fmt" "fmt"
"image"
"github.com/hajimehoshi/ebiten/internal/affine" "github.com/hajimehoshi/ebiten/internal/affine"
emath "github.com/hajimehoshi/ebiten/internal/math" emath "github.com/hajimehoshi/ebiten/internal/math"
@ -313,35 +312,6 @@ func (c *disposeCommand) Exec(indexOffsetInBytes int) error {
return nil return nil
} }
// newImageFromImageCommand represents a command to create an image from an image.RGBA.
type newImageFromImageCommand struct {
result *Image
img *image.RGBA
}
// Exec executes the newImageFromImageCommand.
func (c *newImageFromImageCommand) Exec(indexOffsetInBytes int) error {
origSize := c.img.Bounds().Size()
if origSize.X < 1 {
return errors.New("graphics: width must be equal or more than 1.")
}
if origSize.Y < 1 {
return errors.New("graphics: height must be equal or more than 1.")
}
w, h := c.img.Bounds().Size().X, c.img.Bounds().Size().Y
if c.img.Bounds() != image.Rect(0, 0, emath.NextPowerOf2Int(w), emath.NextPowerOf2Int(h)) {
panic(fmt.Sprintf("graphics: invalid image bounds: %v", c.img.Bounds()))
}
native, err := opengl.GetContext().NewTexture(w, h, c.img.Pix)
if err != nil {
return err
}
c.result.texture = &texture{
native: native,
}
return nil
}
// newImageCommand represents a command to create an empty image with given width and height. // newImageCommand represents a command to create an empty image with given width and height.
type newImageCommand struct { type newImageCommand struct {
result *Image result *Image

View File

@ -15,8 +15,6 @@
package graphics package graphics
import ( import (
"image"
"github.com/hajimehoshi/ebiten/internal/affine" "github.com/hajimehoshi/ebiten/internal/affine"
"github.com/hajimehoshi/ebiten/internal/math" "github.com/hajimehoshi/ebiten/internal/math"
"github.com/hajimehoshi/ebiten/internal/opengl" "github.com/hajimehoshi/ebiten/internal/opengl"
@ -47,19 +45,6 @@ func NewImage(width, height int) *Image {
return i return i
} }
func NewImageFromImage(img *image.RGBA, width, height int) *Image {
i := &Image{
width: width,
height: height,
}
c := &newImageFromImageCommand{
result: i,
img: img,
}
theCommandQueue.Enqueue(c)
return i
}
func NewScreenFramebufferImage(width, height int) *Image { func NewScreenFramebufferImage(width, height int) *Image {
i := &Image{ i := &Image{
width: width, width: width,

View File

@ -16,7 +16,6 @@ package restorable
import ( import (
"errors" "errors"
"image"
"image/color" "image/color"
"runtime" "runtime"
@ -97,25 +96,6 @@ func NewImage(width, height int, volatile bool) *Image {
return i return i
} }
// NewImageFromImage creates an image with source image.
func NewImageFromImage(source image.Image) *Image {
size := source.Bounds().Size()
width, height := size.X, size.Y
rgbaImg := CopyImage(source)
w2, h2 := math.NextPowerOf2Int(width), math.NextPowerOf2Int(height)
p := make([]byte, 4*w2*h2)
for j := 0; j < height; j++ {
copy(p[j*w2*4:(j+1)*w2*4], rgbaImg.Pix[j*rgbaImg.Stride:])
}
i := &Image{
image: graphics.NewImageFromImage(rgbaImg, width, height),
basePixels: p,
}
theImages.add(i)
runtime.SetFinalizer(i, (*Image).Dispose)
return i
}
// NewScreenFramebufferImage creates a special image that framebuffer is one for the screen. // NewScreenFramebufferImage creates a special image that framebuffer is one for the screen.
func NewScreenFramebufferImage(width, height int, paddingX0, paddingY0, paddingX1, paddingY1 float64) *Image { func NewScreenFramebufferImage(width, height int, paddingX0, paddingY0, paddingX1, paddingY1 float64) *Image {
i := &Image{ i := &Image{
@ -339,14 +319,14 @@ func (i *Image) restore() error {
// TODO: panic here? // TODO: panic here?
return errors.New("restorable: pixels must not be stale when restoring") return errors.New("restorable: pixels must not be stale when restoring")
} }
w2, h2 := math.NextPowerOf2Int(w), math.NextPowerOf2Int(h) gimg := graphics.NewImage(w, h)
img := image.NewRGBA(image.Rect(0, 0, w2, h2))
if i.basePixels != nil { if i.basePixels != nil {
for j := 0; j < h; j++ { gimg.ReplacePixels(i.basePixels)
copy(img.Pix[j*img.Stride:], i.basePixels[j*w2*4:(j+1)*w2*4]) } else {
// Clear the image explicitly.
pix := make([]uint8, w*h*4)
gimg.ReplacePixels(pix)
} }
}
gimg := graphics.NewImageFromImage(img, w, h)
for _, c := range i.drawImageHistory { for _, c := range i.drawImageHistory {
// All dependencies must be already resolved. // All dependencies must be already resolved.
if c.image.hasDependency() { if c.image.hasDependency() {

View File

@ -225,9 +225,9 @@ func TestRestoreComplexGraph(t *testing.T) {
base.Pix[1] = 0xff base.Pix[1] = 0xff
base.Pix[2] = 0xff base.Pix[2] = 0xff
base.Pix[3] = 0xff base.Pix[3] = 0xff
img0 := NewImageFromImage(base) img0 := newImageFromImage(base)
img1 := NewImageFromImage(base) img1 := newImageFromImage(base)
img2 := NewImageFromImage(base) img2 := newImageFromImage(base)
img3 := NewImage(4, 1, false) img3 := NewImage(4, 1, false)
fill(img3, 0, 0, 0, 0) fill(img3, 0, 0, 0, 0)
img4 := NewImage(4, 1, false) img4 := NewImage(4, 1, false)
@ -323,13 +323,20 @@ func TestRestoreComplexGraph(t *testing.T) {
} }
} }
func newImageFromImage(rgba *image.RGBA) *Image {
s := rgba.Bounds().Size()
img := NewImage(s.X, s.Y, false)
img.ReplacePixels(rgba.Pix)
return img
}
func TestRestoreRecursive(t *testing.T) { func TestRestoreRecursive(t *testing.T) {
base := image.NewRGBA(image.Rect(0, 0, 4, 1)) base := image.NewRGBA(image.Rect(0, 0, 4, 1))
base.Pix[0] = 0xff base.Pix[0] = 0xff
base.Pix[1] = 0xff base.Pix[1] = 0xff
base.Pix[2] = 0xff base.Pix[2] = 0xff
base.Pix[3] = 0xff base.Pix[3] = 0xff
img0 := NewImageFromImage(base) img0 := newImageFromImage(base)
img1 := NewImage(4, 1, false) img1 := NewImage(4, 1, false)
fill(img1, 0, 0, 0, 0) fill(img1, 0, 0, 0, 0)
defer func() { defer func() {