Add 'internal/math' package

This commit is contained in:
Hajime Hoshi 2017-08-06 20:05:14 +09:00
parent 14737df60f
commit 631264fce1
8 changed files with 22 additions and 17 deletions

View File

@ -21,6 +21,7 @@ import (
"runtime"
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/math"
"github.com/hajimehoshi/ebiten/internal/opengl"
"github.com/hajimehoshi/ebiten/internal/restorable"
)
@ -198,7 +199,7 @@ func (i *Image) ReplacePixels(p []uint8) error {
if l := 4 * w * h; len(p) != l {
panic(fmt.Sprintf("ebiten: len(p) was %d but must be %d", len(p), l))
}
w2, h2 := graphics.NextPowerOf2Int(w), graphics.NextPowerOf2Int(h)
w2, h2 := math.NextPowerOf2Int(w), math.NextPowerOf2Int(h)
pix := make([]uint8, 4*w2*h2)
for j := 0; j < h; j++ {
copy(pix[j*w2*4:], p[j*w*4:(j+1)*w*4])

View File

@ -25,7 +25,7 @@ import (
"testing"
. "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/graphics"
emath "github.com/hajimehoshi/ebiten/internal/math"
)
func TestMain(m *testing.M) {
@ -90,7 +90,7 @@ func TestImagePixels(t *testing.T) {
w, h := img0.Bounds().Size().X, img0.Bounds().Size().Y
// Check out of range part
w2, h2 := graphics.NextPowerOf2Int(w), graphics.NextPowerOf2Int(h)
w2, h2 := emath.NextPowerOf2Int(w), emath.NextPowerOf2Int(h)
for j := -100; j < h2+100; j++ {
for i := -100; i < w2+100; i++ {
got := img0.At(i, j)

View File

@ -22,6 +22,7 @@ import (
"math"
"github.com/hajimehoshi/ebiten/internal/affine"
emath "github.com/hajimehoshi/ebiten/internal/math"
"github.com/hajimehoshi/ebiten/internal/opengl"
"github.com/hajimehoshi/ebiten/internal/sync"
)
@ -284,7 +285,7 @@ func (c *replacePixelsCommand) Exec(indexOffsetInBytes int) error {
if err := opengl.GetContext().BindTexture(c.dst.texture.native); err != nil {
return err
}
opengl.GetContext().TexSubImage2D(c.pixels, NextPowerOf2Int(c.dst.width), NextPowerOf2Int(c.dst.height))
opengl.GetContext().TexSubImage2D(c.pixels, emath.NextPowerOf2Int(c.dst.width), emath.NextPowerOf2Int(c.dst.height))
return nil
}
@ -317,7 +318,7 @@ func (c *newImageFromImageCommand) Exec(indexOffsetInBytes int) error {
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, NextPowerOf2Int(w), NextPowerOf2Int(h)) {
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, c.filter)
@ -338,8 +339,8 @@ type newImageCommand struct {
}
func (c *newImageCommand) Exec(indexOffsetInBytes int) error {
w := NextPowerOf2Int(c.width)
h := NextPowerOf2Int(c.height)
w := emath.NextPowerOf2Int(c.width)
h := emath.NextPowerOf2Int(c.height)
if w < 1 {
return errors.New("graphics: width must be equal or more than 1.")
}

View File

@ -21,13 +21,14 @@ import (
"runtime"
"github.com/hajimehoshi/ebiten/internal/affine"
"github.com/hajimehoshi/ebiten/internal/math"
"github.com/hajimehoshi/ebiten/internal/opengl"
)
func CopyImage(origImg image.Image) *image.RGBA {
size := origImg.Bounds().Size()
w, h := size.X, size.Y
newImg := image.NewRGBA(image.Rect(0, 0, NextPowerOf2Int(w), NextPowerOf2Int(h)))
newImg := image.NewRGBA(image.Rect(0, 0, math.NextPowerOf2Int(w), math.NextPowerOf2Int(h)))
switch origImg := origImg.(type) {
case *image.Paletted:
b := origImg.Bounds()
@ -158,7 +159,7 @@ func (i *Image) Pixels() ([]uint8, error) {
if err != nil {
return nil, err
}
return opengl.GetContext().FramebufferPixels(f.native, NextPowerOf2Int(i.width), NextPowerOf2Int(i.height))
return opengl.GetContext().FramebufferPixels(f.native, math.NextPowerOf2Int(i.width), math.NextPowerOf2Int(i.height))
}
func (i *Image) ReplacePixels(p []uint8) {

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package graphics
package math
func NextPowerOf2Int(x int) int {
if x <= 0 {

View File

@ -12,11 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package graphics_test
package math_test
import (
. "github.com/hajimehoshi/ebiten/internal/graphics"
"testing"
. "github.com/hajimehoshi/ebiten/internal/math"
)
func TestNextPowerOf2(t *testing.T) {

View File

@ -22,6 +22,7 @@ import (
"github.com/hajimehoshi/ebiten/internal/affine"
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/math"
"github.com/hajimehoshi/ebiten/internal/opengl"
)
@ -80,7 +81,7 @@ func NewImage(width, height int, filter opengl.Filter, volatile bool) *Image {
}
func NewImageFromImage(source *image.RGBA, width, height int, filter opengl.Filter) *Image {
w2, h2 := graphics.NextPowerOf2Int(width), graphics.NextPowerOf2Int(height)
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:])
@ -197,7 +198,7 @@ func (p *Image) appendDrawImageHistory(image *Image, vertices []float32, colorm
// Note that this must not be called until context is available.
func (p *Image) At(x, y int) (color.RGBA, error) {
w, h := p.image.Size()
w2, h2 := graphics.NextPowerOf2Int(w), graphics.NextPowerOf2Int(h)
w2, h2 := math.NextPowerOf2Int(w), math.NextPowerOf2Int(h)
if x < 0 || y < 0 || w2 <= x || h2 <= y {
return color.RGBA{}, nil
}
@ -294,7 +295,7 @@ func (p *Image) restore() error {
// TODO: panic here?
return errors.New("restorable: pixels must not be stale when restoring")
}
w2, h2 := graphics.NextPowerOf2Int(w), graphics.NextPowerOf2Int(h)
w2, h2 := math.NextPowerOf2Int(w), math.NextPowerOf2Int(h)
img := image.NewRGBA(image.Rect(0, 0, w2, h2))
if p.basePixels != nil {
for j := 0; j < h; j++ {

View File

@ -26,7 +26,7 @@ import (
"golang.org/x/image/math/fixed"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/graphics" // TODO: Move NextPowerOf2Int to a new different package
emath "github.com/hajimehoshi/ebiten/internal/math"
"github.com/hajimehoshi/ebiten/internal/sync"
)
@ -80,7 +80,7 @@ func (c *char) atlasGroup() int {
if t < 32 {
return 32
}
return graphics.NextPowerOf2Int(t)
return emath.NextPowerOf2Int(t)
}
type glyph struct {