forked from cytec/screengen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotate.go
86 lines (80 loc) · 2.01 KB
/
rotate.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
79
80
81
82
83
84
85
86
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program. If not, see <http://www.gnu.org/licenses/>.
package screengen
import (
"image"
"unsafe"
)
func rotate90(m *image.RGBA) *image.RGBA {
w := m.Rect.Max.X - m.Rect.Min.X
h := m.Rect.Max.Y - m.Rect.Min.Y
d := image.NewRGBA(image.Rect(0, 0, h, w))
dst := d.Pix
src := m.Pix
s1 := m.Stride
s2 := h * 4
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
i := x*s2 + (h-y-1)*4
j := y*s1 + x*4
*(*uint32)(unsafe.Pointer(&dst[i])) = *(*uint32)(unsafe.Pointer(&src[j]))
}
}
return d
}
func rotate270(m *image.RGBA) *image.RGBA {
w := m.Rect.Max.X - m.Rect.Min.X
h := m.Rect.Max.Y - m.Rect.Min.Y
d := image.NewRGBA(image.Rect(0, 0, h, w))
dst := d.Pix
src := m.Pix
s1 := m.Stride
s2 := h * 4
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
i := (w-x-1)*s2 + y*4
j := y*s1 + x*4
*(*uint32)(unsafe.Pointer(&dst[i])) = *(*uint32)(unsafe.Pointer(&src[j]))
}
}
return d
}
func rotate180(m *image.RGBA) *image.RGBA {
buf := make([]uint8, m.Stride)
i := 0
j := len(m.Pix) - m.Stride
for i < j {
flip(m.Pix[i : i+m.Stride])
copy(buf, m.Pix[i:])
flip(m.Pix[j : j+m.Stride])
copy(m.Pix[i:], m.Pix[j:j+m.Stride])
copy(m.Pix[j:], buf)
i += m.Stride
j -= m.Stride
}
if i == j {
flip(m.Pix[i : i+m.Stride])
}
return m
}
func flip(pix []uint8) {
i := 0
j := len(pix) - 4
for i < j {
p := (*uint32)(unsafe.Pointer(&pix[i]))
q := (*uint32)(unsafe.Pointer(&pix[j]))
*p, *q = *q, *p
i += 4
j -= 4
}
}