From b77a28b2085616670c5ef70596e1101465fa2529 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 27 Oct 2016 02:47:10 +0900 Subject: [PATCH] graphics: Use shift instead of div --- vertices.go | 15 ++++++++++++--- vertices_js.go | 18 ++++++++++++------ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/vertices.go b/vertices.go index 5e494de33..45a77b5fd 100644 --- a/vertices.go +++ b/vertices.go @@ -52,8 +52,14 @@ func vertices(parts ImageParts, width, height int, geo *GeoM) []int16 { oneSize := totalSize / 4 l := parts.Len() vs := make([]int16, l*totalSize) - width2p := graphics.NextPowerOf2Int(width) - height2p := graphics.NextPowerOf2Int(height) + w := uint(0) + h := uint(0) + for (1 << w) < width { + w++ + } + for (1 << h) < height { + h++ + } geo16 := floatsToInt16s(geo.Element(0, 0), geo.Element(0, 1), geo.Element(1, 0), @@ -71,7 +77,10 @@ func vertices(parts ImageParts, width, height int, geo *GeoM) []int16 { if sx0 == sx1 || sy0 == sy1 { continue } - u0, v0, u1, v1 := u(sx0, width2p), v(sy0, height2p), u(sx1, width2p), v(sy1, height2p) + u0 := int16((math.MaxInt16 * sx0) >> w) + v0 := int16((math.MaxInt16 * sy0) >> h) + u1 := int16((math.MaxInt16 * sx1) >> w) + v1 := int16((math.MaxInt16 * sy1) >> h) offset := n * totalSize vs[offset] = x0 vs[offset+1] = y0 diff --git a/vertices_js.go b/vertices_js.go index 19263ab02..4d316487a 100644 --- a/vertices_js.go +++ b/vertices_js.go @@ -31,8 +31,14 @@ func vertices(parts ImageParts, width, height int, geo *GeoM) []int16 { a := js.Global.Get("ArrayBuffer").New(l * totalSize * 2) af32 := js.Global.Get("Float32Array").New(a) a16 := js.Global.Get("Int16Array").New(a) - w2p := graphics.NextPowerOf2Int(width) - h2p := graphics.NextPowerOf2Int(height) + w := uint(0) + h := uint(0) + for (1 << w) < width { + w++ + } + for (1 << h) < height { + h++ + } gs := []float64{geo.Element(0, 0), geo.Element(0, 1), geo.Element(1, 0), @@ -49,10 +55,10 @@ func vertices(parts ImageParts, width, height int, geo *GeoM) []int16 { if sx0 == sx1 || sy0 == sy1 { continue } - u0 := math.MaxInt16 * sx0 / w2p - v0 := math.MaxInt16 * sy0 / h2p - u1 := math.MaxInt16 * sx1 / w2p - v1 := math.MaxInt16 * sy1 / h2p + u0 := (math.MaxInt16 * sx0) >> w + v0 := (math.MaxInt16 * sy0) >> h + u1 := (math.MaxInt16 * sx1) >> w + v1 := (math.MaxInt16 * sy1) >> h offset := n * totalSize a16.SetIndex(offset, dx0) a16.SetIndex(offset+1, dy0)