-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotation.go
78 lines (70 loc) · 1.9 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package snip
import (
"image"
"math"
)
// Rotate Method responsible for free image rotation
func Rotate(img image.Image, angle float64) image.Image {
// Final image set on retunr
var newIMG *image.RGBA
// Get img bounds
bounds := img.Bounds()
// Calculate sin and cos using angle parameter
sen, cos := math.Sincos(math.Pi * angle / 180)
if angle == 90 || angle == 270 ||
angle == -90 || angle == -270 {
newIMG = image.NewRGBA(image.Rectangle{
Min: image.Point{
X: 0,
Y: 0,
},
Max: image.Point{
X: bounds.Max.Y,
Y: bounds.Max.X,
},
})
for y := 0; y < bounds.Max.Y; y++ {
for x := 0; x < bounds.Max.X; x++ {
calculateFix(bounds, x, y, sen, cos, newIMG, img)
}
}
} else {
newIMG = image.NewRGBA(bounds)
for y := 0; y < bounds.Max.Y; y++ {
for x := 0; x < bounds.Max.X; x++ {
calculate(bounds, x, y, sen, cos, newIMG, img)
}
}
}
return newIMG
}
// Method responsible for distributing pixels in new positions
func calculate(bounds image.Rectangle, x, y int, sen, cos float64, newIMG *image.RGBA, img image.Image) {
actualPixel := img.At(x, y)
newIMG.Set(
int(cos*(delta(x, bounds.Max.X))-
sen*(delta(y, bounds.Max.Y))+
float64(bounds.Max.X/2)),
int(sen*(delta(x, bounds.Max.X))+
cos*(delta(y, bounds.Max.Y))+
float64(bounds.Max.Y/2)),
actualPixel)
}
// Method responsible for distributing pixels in new fix position
func calculateFix(bounds image.Rectangle, x, y int, sen, cos float64, newIMG *image.RGBA, img image.Image) {
actualPixel := img.At(x, y)
newIMG.Set(
int(cos*(delta(x, bounds.Max.X))-
sen*(delta(y, bounds.Max.Y))+
float64(bounds.Max.X/2)-
float64(bounds.Max.X/2-bounds.Max.Y/2)),
int(sen*(delta(x, bounds.Max.X))+
cos*(delta(y, bounds.Max.Y))+
float64(bounds.Max.Y/2)+
float64(bounds.Max.X/2-bounds.Max.Y/2)),
actualPixel)
}
// Calculate delta value
func delta(x, y int) float64 {
return float64(x - y/2)
}