-
Notifications
You must be signed in to change notification settings - Fork 4
/
rotation.go
58 lines (48 loc) · 1.27 KB
/
rotation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package geometry
import (
"image"
"image/color"
)
type matrix2d [4]int
func (m2d matrix2d) mul(x, y int) (int, int) {
return m2d[0]*x + m2d[1]*y,
m2d[2]*x + m2d[3]*y
}
// image2DTransform is a 2 by 2 matrix for 2d image manips
// it allows to transform images while accessing coordinates.
type image2DTransformInt struct {
m matrix2d
src image.Image
bounds image.Rectangle
}
// At gives you transformed location of x,y
func (i2dt *image2DTransformInt) At(x, y int) color.Color {
xx, yy := i2dt.m.mul(x, y)
return i2dt.src.At(xx, yy)
}
func (i2dt *image2DTransformInt) Bounds() image.Rectangle { return i2dt.bounds }
func (i2dt *image2DTransformInt) ColorModel() color.Model { return i2dt.src.ColorModel() }
// InPlaceRotation90 returns an image that references src
// and that will output you a 90 degrees rotation of src.
func InPlaceRotation90(src image.Image) image.Image {
min, max := src.Bounds().Min, src.Bounds().Max
// THOUGHTS(azr)
// weird/hard, this works but
// I have a wrong feeling about it
maxY := 0 //max.X - max.X
minY := -min.X - max.X
maxX := max.Y
minX := min.Y
rect := image.Rect(minX, minY, maxX, maxY)
return &image2DTransformInt{
bounds: rect,
src: src,
m: rota90M,
}
}
var (
rota90M = matrix2d{
0, -1,
1, 0,
}
)