mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
graphics: Remove NewImageFromImage command
This commit is contained in:
parent
9adc1ac6a0
commit
85b133dad0
12
image.go
12
image.go
@ -398,12 +398,22 @@ func newVolatileImage(width, height int, filter Filter) *Image {
|
||||
func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
|
||||
size := source.Bounds().Size()
|
||||
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{
|
||||
restorable: r,
|
||||
filter: filter,
|
||||
}
|
||||
runtime.SetFinalizer(i, (*Image).Dispose)
|
||||
|
||||
_ = i.ReplacePixels(p)
|
||||
|
||||
return i, nil
|
||||
}
|
||||
|
||||
|
@ -17,7 +17,6 @@ package graphics
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/internal/affine"
|
||||
emath "github.com/hajimehoshi/ebiten/internal/math"
|
||||
@ -313,35 +312,6 @@ func (c *disposeCommand) Exec(indexOffsetInBytes int) error {
|
||||
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.
|
||||
type newImageCommand struct {
|
||||
result *Image
|
||||
|
@ -15,8 +15,6 @@
|
||||
package graphics
|
||||
|
||||
import (
|
||||
"image"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/internal/affine"
|
||||
"github.com/hajimehoshi/ebiten/internal/math"
|
||||
"github.com/hajimehoshi/ebiten/internal/opengl"
|
||||
@ -47,19 +45,6 @@ func NewImage(width, height int) *Image {
|
||||
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 {
|
||||
i := &Image{
|
||||
width: width,
|
||||
|
@ -16,7 +16,6 @@ package restorable
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"image"
|
||||
"image/color"
|
||||
"runtime"
|
||||
|
||||
@ -97,25 +96,6 @@ func NewImage(width, height int, volatile bool) *Image {
|
||||
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.
|
||||
func NewScreenFramebufferImage(width, height int, paddingX0, paddingY0, paddingX1, paddingY1 float64) *Image {
|
||||
i := &Image{
|
||||
@ -339,14 +319,14 @@ func (i *Image) restore() error {
|
||||
// TODO: panic here?
|
||||
return errors.New("restorable: pixels must not be stale when restoring")
|
||||
}
|
||||
w2, h2 := math.NextPowerOf2Int(w), math.NextPowerOf2Int(h)
|
||||
img := image.NewRGBA(image.Rect(0, 0, w2, h2))
|
||||
gimg := graphics.NewImage(w, h)
|
||||
if i.basePixels != nil {
|
||||
for j := 0; j < h; j++ {
|
||||
copy(img.Pix[j*img.Stride:], i.basePixels[j*w2*4:(j+1)*w2*4])
|
||||
gimg.ReplacePixels(i.basePixels)
|
||||
} 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 {
|
||||
// All dependencies must be already resolved.
|
||||
if c.image.hasDependency() {
|
||||
|
@ -225,9 +225,9 @@ func TestRestoreComplexGraph(t *testing.T) {
|
||||
base.Pix[1] = 0xff
|
||||
base.Pix[2] = 0xff
|
||||
base.Pix[3] = 0xff
|
||||
img0 := NewImageFromImage(base)
|
||||
img1 := NewImageFromImage(base)
|
||||
img2 := NewImageFromImage(base)
|
||||
img0 := newImageFromImage(base)
|
||||
img1 := newImageFromImage(base)
|
||||
img2 := newImageFromImage(base)
|
||||
img3 := NewImage(4, 1, false)
|
||||
fill(img3, 0, 0, 0, 0)
|
||||
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) {
|
||||
base := image.NewRGBA(image.Rect(0, 0, 4, 1))
|
||||
base.Pix[0] = 0xff
|
||||
base.Pix[1] = 0xff
|
||||
base.Pix[2] = 0xff
|
||||
base.Pix[3] = 0xff
|
||||
img0 := NewImageFromImage(base)
|
||||
img0 := newImageFromImage(base)
|
||||
img1 := NewImage(4, 1, false)
|
||||
fill(img1, 0, 0, 0, 0)
|
||||
defer func() {
|
||||
|
Loading…
Reference in New Issue
Block a user