Browse Source

moved matrices insite a subpackage

master
bake 11 months ago
parent
commit
f51db3daeb
2 changed files with 65 additions and 53 deletions
  1. 40
    0
      matrix/matrix.go
  2. 25
    53
      transform.go

+ 40
- 0
matrix/matrix.go View File

@@ -0,0 +1,40 @@
package matrix

import (
"math"

"gonum.org/v1/gonum/mat"
)

func Translate(x, y int) mat.Matrix {
return mat.NewDense(3, 3, []float64{
1, 0, float64(x),
0, 1, float64(y),
0, 0, 1,
})
}

func Rotate(deg float64) mat.Matrix {
deg *= math.Pi / 180
return mat.NewDense(3, 3, []float64{
math.Cos(deg), -math.Sin(deg), 0,
math.Sin(deg), math.Cos(deg), 0,
0, 0, 1,
})
}

func Scale(x, y float64) mat.Matrix {
return mat.NewDense(3, 3, []float64{
x, 0, 0,
0, y, 0,
0, 0, 1,
})
}

func Shear(x, y float64) mat.Matrix {
return mat.NewDense(3, 3, []float64{
1, x, 0,
y, 1, 0,
0, 0, 1,
})
}

+ 25
- 53
transform.go View File

@@ -4,31 +4,24 @@ import (
"image"
"math"

"github.com/bakerolls/transform/matrix"
"gonum.org/v1/gonum/mat"
)

// Translate an image.
func Translate(img image.Image, x, y int) *image.RGBA {
return Matrix(img, translate(x, y))
}

func translate(x, y int) mat.Matrix {
return mat.NewDense(3, 3, []float64{
1, 0, float64(x),
0, 1, float64(y),
0, 0, 1,
})
return Apply(img, matrix.Translate(x, y))
}

// Rotate an image.
func Rotate(img image.Image, deg float64) *image.RGBA {
b := img.Bounds()
w, h := b.Max.X-b.Min.X, b.Max.Y-b.Min.Y
return Matrix(img, mul(
translate(w/2, h/2),
rotate(deg),
translate(-w/2, -h/2),
))
return Apply(img,
matrix.Translate(w/2, h/2),
matrix.Rotate(deg),
matrix.Translate(-w/2, -h/2),
)
}

// RotateInBounds rotates an image and scales the result to fit inside the
@@ -46,40 +39,23 @@ func RotateInBounds(img image.Image, deg float64) *image.RGBA {
w, h = h, w
}
s := math.Min(w/rotW, h/rotH)
return Matrix(img, mul(
translate(int(w/2), int(h/2)),
scale(s, s),
rotate(deg),
translate(-int(w/2), -int(h/2)),
))
}

func rotate(deg float64) mat.Matrix {
deg *= math.Pi / 180
return mat.NewDense(3, 3, []float64{
math.Cos(deg), -math.Sin(deg), 0,
math.Sin(deg), math.Cos(deg), 0,
0, 0, 1,
})
return Apply(img,
matrix.Translate(int(w/2), int(h/2)),
matrix.Scale(s, s),
matrix.Rotate(deg),
matrix.Translate(-int(w/2), -int(h/2)),
)
}

// Scale an image
func Scale(img image.Image, x, y float64) *image.RGBA {
b := img.Bounds()
w, h := b.Max.X-b.Min.X, b.Max.Y-b.Min.Y
return Matrix(img, mul(
translate(int(w/2), int(h/2)),
scale(x, y),
translate(-int(w/2), -int(h/2)),
))
}

func scale(x, y float64) mat.Matrix {
return mat.NewDense(3, 3, []float64{
x, 0, 0,
0, y, 0,
0, 0, 1,
})
return Apply(img,
matrix.Translate(int(w/2), int(h/2)),
matrix.Scale(x, y),
matrix.Translate(-int(w/2), -int(h/2)),
)
}

// FlipH flips an image horizontally.
@@ -94,19 +70,15 @@ func FlipV(img image.Image) *image.RGBA {

// Shear an image.
func Shear(img image.Image, x, y float64) *image.RGBA {
return Matrix(img, shear(x, y))
}

func shear(x, y float64) mat.Matrix {
return mat.NewDense(3, 3, []float64{
1, x, 0,
y, 1, 0,
0, 0, 1,
})
return Apply(img, matrix.Shear(x, y))
}

// Matrix executes a transformation matrix on an image.
func Matrix(img image.Image, m mat.Matrix) *image.RGBA {
// Apply executes one or more transformation matrix on an image. Additional
// matrices will be multiplied. This allows you to combine transformations
// found inside the matrix subpackage or a custom gonum 3x3 mat.Matrix in any
// way you want, which will be faster than applying one step at a time.
func Apply(img image.Image, m mat.Matrix, ms ...mat.Matrix) *image.RGBA {
m = mul(m, ms...)
b := img.Bounds()
dst := image.NewRGBA(b)
for x := b.Min.X; x < b.Max.X; x++ {

Loading…
Cancel
Save