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).
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).

View File

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

View File

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

View File

@ -35,6 +35,7 @@ const (
type ColorM interface {
IsIdentity() bool
At(i, j int) 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() {
var body [16]float32
var translate [4]float32
colorm.UnsafeElements(&body, &translate)
cr := body[0]
cg := body[5]
cb := body[10]
ca := body[15]
cr := colorm.At(0, 0)
cg := colorm.At(1, 1)
cb := colorm.At(2, 2)
ca := colorm.At(3, 3)
colorm = affine.ColorMIdentity{}
const n = graphics.VertexFloatNum
for i := 0; i < len(vertices)/n; i++ {