interna/affine: Avoid UnsafeElements at At

Updates #1796
This commit is contained in:
Hajime Hoshi 2021-09-04 17:55:47 +09:00
parent df710a5c63
commit 8c9ec8fc9f
5 changed files with 27 additions and 18 deletions

View File

@ -99,7 +99,7 @@ func (c *ColorM) ChangeHSV(hueTheta float64, saturationScale float64, valueScale
// Element returns a value of a matrix at (i, j). // Element returns a value of a matrix at (i, j).
func (c *ColorM) Element(i, j int) float64 { func (c *ColorM) Element(i, j int) float64 {
return float64(affine.ColorMElement(c.affineColorM(), i, j)) return float64(c.affineColorM().At(i, j))
} }
// SetElement sets an element at (i, j). // SetElement sets an element at (i, j).

View File

@ -44,6 +44,7 @@ var (
type ColorM interface { type ColorM interface {
IsIdentity() bool IsIdentity() bool
ScaleOnly() bool ScaleOnly() bool
At(i, j int) float32
UnsafeElements(body *[16]float32, translate *[4]float32) UnsafeElements(body *[16]float32, translate *[4]float32)
Apply(clr color.Color) color.Color Apply(clr color.Color) color.Color
@ -454,15 +455,25 @@ func (c *colorMImplBodyTranslate) Invert() ColorM {
return m return m
} }
// ColorMElement returns a value of a matrix at (i, j). func (c ColorMIdentity) At(i, j int) float32 {
func ColorMElement(c ColorM, i, j int) float32 { if i == j {
var b [16]float32 return 1
var t [4]float32
c.UnsafeElements(&b, &t)
if j < ColorMDim-1 {
return b[i+j*(ColorMDim-1)]
} }
return t[i] return 0
}
func (c colorMImplScale) At(i, j int) float32 {
if i == j {
return c.scale[i]
}
return 0
}
func (c *colorMImplBodyTranslate) At(i, j int) float32 {
if j < ColorMDim-1 {
return c.body[i+j*(ColorMDim-1)]
}
return c.translate[i]
} }
// ColorMSetElement sets an element at (i, j). // ColorMSetElement sets an element at (i, j).

View File

@ -169,8 +169,8 @@ func abs(x float32) float32 {
func equalWithDelta(a, b ColorM, delta float32) bool { func equalWithDelta(a, b ColorM, delta float32) bool {
for j := 0; j < 5; j++ { for j := 0; j < 5; j++ {
for i := 0; i < 4; i++ { for i := 0; i < 4; i++ {
ea := ColorMElement(a, i, j) ea := a.At(i, j)
eb := ColorMElement(b, i, j) eb := b.At(i, j)
if abs(ea-eb) > delta { if abs(ea-eb) > delta {
return false return false
} }

View File

@ -35,6 +35,7 @@ const (
type ColorM interface { type ColorM interface {
IsIdentity() bool IsIdentity() bool
At(i, j int) float32
UnsafeElements(body *[16]float32, translate *[4]float32) UnsafeElements(body *[16]float32, translate *[4]float32)
} }

View File

@ -124,13 +124,10 @@ func (m *Mipmap) DrawTriangles(srcs [graphics.ShaderImageNum]*Mipmap, vertices [
} }
if colorm.ScaleOnly() { if colorm.ScaleOnly() {
var body [16]float32 cr := colorm.At(0, 0)
var translate [4]float32 cg := colorm.At(1, 1)
colorm.UnsafeElements(&body, &translate) cb := colorm.At(2, 2)
cr := body[0] ca := colorm.At(3, 3)
cg := body[5]
cb := body[10]
ca := body[15]
colorm = affine.ColorMIdentity{} colorm = affine.ColorMIdentity{}
const n = graphics.VertexFloatNum const n = graphics.VertexFloatNum
for i := 0; i < len(vertices)/n; i++ { for i := 0; i < len(vertices)/n; i++ {