-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitmap.go
144 lines (138 loc) · 2.44 KB
/
bitmap.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Code generated by cxgo. DO NOT EDIT.
package gotrace
import (
"unsafe"
"github.com/gotranspile/cxgo/runtime/libc"
)
func getsize(dy int, h int) int64 {
var size int64
if dy < 0 {
dy = -dy
}
size = int64(dy) * int64(h) * int64(int(sizeofWord))
if size < 0 || h != 0 && dy != 0 && size/int64(h)/int64(dy) != int64(int(sizeofWord)) {
return -1
}
return size
}
func bm_size(bm *Bitmap) int64 {
return getsize(bm.Dy, bm.H)
}
func bm_base(bm *Bitmap) *Word {
var dy int = bm.Dy
if dy >= 0 || bm.H == 0 {
return &bm.Map[0]
} else {
return &bm.Map[int64(bm.H-1)*int64(bm.Dy)]
}
}
func bm_free(bm *Bitmap) {
if bm != nil && bm.Map != nil {
libc.Free(unsafe.Pointer(bm_base(bm)))
}
}
func NewBitmap(w int, h int) *Bitmap {
var (
bm *Bitmap
dy int
)
if w == 0 {
dy = 0
} else {
dy = (w-1)/(8*(int(sizeofWord))) + 1
}
var size int64
size = getsize(dy, h)
if size < 0 {
panic("out of memory")
return nil
}
if size == 0 {
size = int64(int(sizeofWord))
}
bm = new(Bitmap)
if bm == nil {
return nil
}
bm.W = w
bm.H = h
bm.Dy = dy
bm.Map = make([]Word, uintptr(size)/sizeofWord)
if bm.Map == nil {
return nil
}
return bm
}
func bm_clear(bm *Bitmap, c int) {
var size int64 = bm_size(bm)
libc.MemSet(unsafe.Pointer(bm_base(bm)), byte(int8(func() int {
if c != 0 {
return -1
}
return 0
}())), int(size))
}
func bm_dup(bm *Bitmap) *Bitmap {
var (
bm1 *Bitmap = NewBitmap(bm.W, bm.H)
y int
)
if bm1 == nil {
return nil
}
for y = 0; y < bm.H; y++ {
libc.MemCpy(unsafe.Pointer(&bm1.Map[int64(y)*int64(bm1.Dy)]), unsafe.Pointer(&bm.Map[int64(y)*int64(bm.Dy)]), int(uint64(bm1.Dy)*uint64(int(sizeofWord))))
}
return bm1
}
func bm_invert(bm *Bitmap) {
var (
dy int = bm.Dy
y int
i int
p *Word
)
if dy < 0 {
dy = -dy
}
for y = 0; y < bm.H; y++ {
p = &bm.Map[int64(y)*int64(bm.Dy)]
for i = 0; i < dy; i++ {
*(*Word)(unsafe.Add(unsafe.Pointer(p), sizeofWord*uintptr(i))) ^= ^Word(0)
}
}
}
func bm_resize(bm *Bitmap, h int) int {
var (
dy int = bm.Dy
newsize int64
newmap []Word
)
if dy < 0 {
bm_flip(bm)
}
newsize = getsize(dy, h)
if newsize < 0 {
panic("out of memory")
goto error
}
if newsize == 0 {
newsize = int64(int(sizeofWord))
}
newmap = make([]Word, uintptr(newsize)/sizeofWord)
copy(newmap, bm.Map)
if newmap == nil {
goto error
}
bm.Map = []Word(newmap)
bm.H = h
if dy < 0 {
bm_flip(bm)
}
return 0
error:
if dy < 0 {
bm_flip(bm)
}
return 1
}