-
Notifications
You must be signed in to change notification settings - Fork 7
/
draw.go
100 lines (90 loc) · 2.77 KB
/
draw.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package bar
import (
"image"
"github.com/BurntSushi/xgbutil/xgraphics"
)
const m = 1<<16 - 1
func DrawCopySrcRGBAToBGRA(dst *xgraphics.Image, r image.Rectangle, src *image.RGBA, sp image.Point) {
r = r.Intersect(dst.Rect)
dx, dy := r.Dx(), r.Dy()
d0 := dst.PixOffset(r.Min.X, r.Min.Y)
s0 := src.PixOffset(sp.X, sp.Y)
var (
ddelta, sdelta int
i0, i1, idelta int
)
if r.Min.Y < sp.Y || r.Min.Y == sp.Y && r.Min.X <= sp.X {
ddelta = dst.Stride
sdelta = src.Stride
i0, i1, idelta = 0, dx*4, +4
} else {
// If the source start point is higher than the destination start point, or equal height but to the left,
// then we compose the rows in right-to-left, bottom-up order instead of left-to-right, top-down.
d0 += (dy - 1) * dst.Stride
s0 += (dy - 1) * src.Stride
ddelta = -dst.Stride
sdelta = -src.Stride
i0, i1, idelta = (dx-1)*4, -4, -4
}
for ; dy > 0; dy-- {
dpix := dst.Pix[d0:]
spix := src.Pix[s0:]
for i := i0; i != i1; i += idelta {
s := spix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857
sr := s[0]
sg := s[1]
sb := s[2]
sa := s[3]
d := dpix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857
d[0] = sb
d[1] = sg
d[2] = sr
d[3] = sa
}
d0 += ddelta
s0 += sdelta
}
}
func DrawCopyOverRGBAToBGRA(dst *xgraphics.Image, r image.Rectangle, src *image.RGBA, sp image.Point) {
r = r.Intersect(dst.Rect)
dx, dy := r.Dx(), r.Dy()
d0 := dst.PixOffset(r.Min.X, r.Min.Y)
s0 := src.PixOffset(sp.X, sp.Y)
var (
ddelta, sdelta int
i0, i1, idelta int
)
if r.Min.Y < sp.Y || r.Min.Y == sp.Y && r.Min.X <= sp.X {
ddelta = dst.Stride
sdelta = src.Stride
i0, i1, idelta = 0, dx*4, +4
} else {
// If the source start point is higher than the destination start point, or equal height but to the left,
// then we compose the rows in right-to-left, bottom-up order instead of left-to-right, top-down.
d0 += (dy - 1) * dst.Stride
s0 += (dy - 1) * src.Stride
ddelta = -dst.Stride
sdelta = -src.Stride
i0, i1, idelta = (dx-1)*4, -4, -4
}
for ; dy > 0; dy-- {
dpix := dst.Pix[d0:]
spix := src.Pix[s0:]
for i := i0; i != i1; i += idelta {
s := spix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857
sr := uint32(s[0]) * 0x101
sg := uint32(s[1]) * 0x101
sb := uint32(s[2]) * 0x101
sa := uint32(s[3]) * 0x101
// The 0x101 is here for the same reason as in drawRGBA.
a := (m - sa) * 0x101
d := dpix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857
d[0] = uint8((uint32(d[0])*a/m + sb) >> 8)
d[1] = uint8((uint32(d[1])*a/m + sg) >> 8)
d[2] = uint8((uint32(d[2])*a/m + sr) >> 8)
d[3] = uint8((uint32(d[3])*a/m + sa) >> 8)
}
d0 += ddelta
s0 += sdelta
}
}