-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.go
241 lines (201 loc) · 4.93 KB
/
buffer.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package tcg
import (
"fmt"
"image"
"image/color"
_ "image/png"
"strings"
)
// Buffer - implement base screen pixel buffer
type Buffer struct {
Width int
Height int
buffer [][]byte
}
// NewBuffer - get new buffer object
func NewBuffer(width, height int) Buffer {
return Buffer{
Width: width,
Height: height,
buffer: allocateBuffer(width, height),
}
}
// NewBufferFromStrings - get new buffer object from list of strings (00110001, 0001001, ...)
// such symbols are also valid: " ** ", "..##.."
func NewBufferFromStrings(in []string) (*Buffer, error) {
if len(in) == 0 {
return nil, fmt.Errorf("got empty string list")
}
width, height := len(in[0]), len(in)
buf := Buffer{
Width: width,
Height: height,
buffer: allocateBuffer(width, height),
}
for y, line := range in {
if len(line) != width {
return nil, fmt.Errorf("got line with different width (%d != %d) on line %d", width, len(line), y)
}
for x, char := range line {
switch char {
case '0', '.', ' ':
// pass
case '1', '*', '#':
buf.Set(x, y, Black)
default:
return nil, fmt.Errorf("got not valid char %v on %d:%d", char, x, y)
}
}
}
return &buf, nil
}
// MustNewBufferFromStrings - get new buffer object from list of strings and die on error
func MustNewBufferFromStrings(in []string) Buffer {
buf, err := NewBufferFromStrings(in)
if err != nil {
panic(err)
}
return *buf
}
// Strings - render as slice of string, ["...**...", ...]
func (b Buffer) Strings() []string {
return b.RenderAsStrings(Mode1x1Simple)
}
// String - render as string with new-line separator: "..*..\n*....*\n..*.."
func (b Buffer) String() string {
return strings.Join(b.Strings(), "\n")
}
func allocateBuffer(w, h int) [][]byte {
buffer := make([][]byte, h)
bytesLen := widthInBytes(w)
for i := range buffer {
buffer[i] = make([]byte, bytesLen)
}
return buffer
}
func widthInBytes(w int) int {
result := w / 8
if w%8 > 0 {
result++
}
return result
}
// NewBufferFromImage - get new buffer from image.Image object
func NewBufferFromImage(img image.Image) Buffer {
buf := NewBuffer(img.Bounds().Size().X, img.Bounds().Size().Y)
for y := 0; y < buf.Height; y++ {
for x := 0; x < buf.Width; x++ {
tc := Black
if color.GrayModel.Convert(img.At(x, y)).(color.Gray).Y >= 128 {
tc = White
}
buf.Set(x, y, tc)
}
}
return buf
}
// ToImage - convert buffer to std Image with Gray colorspace, for example for save buffer to image file like png
func (b Buffer) ToImage() image.Image {
img := image.NewGray(image.Rect(0, 0, b.Width, b.Height))
var c uint8
for y := 0; y < b.Height; y++ {
for x := 0; x < b.Width; x++ {
c = 0
if b.At(x, y) == White {
c = 255
}
img.SetGray(x, y, color.Gray{Y: c})
}
}
return img
}
// Clone to new buffer
func (b Buffer) Clone() *Buffer {
newBuf := NewBuffer(b.Width, b.Height)
for y := 0; y < b.Height; y++ {
copy(newBuf.buffer[y], b.buffer[y])
}
return &newBuf
}
// Cut area to the new buffer, without change current buffer
func (b Buffer) Cut(x, y, width, height int) Buffer {
newBuf := NewBuffer(width, height)
newBuf.BitBlt(0, 0, width, height, b, x, y)
return newBuf
}
// Set - put pixel into buffer
func (b *Buffer) Set(x, y int, color int) {
if x < 0 || x > b.Width-1 || y < 0 || y > b.Height-1 {
return
}
// [0][1][2][3] [4][5][6][7]
i, mask := x/8, byte(0b1000_0000>>byte(x%8))
switch color {
case Black:
b.buffer[y][i] |= mask
case White:
b.buffer[y][i] &^= mask
}
}
// At - get pixel color from buffer
func (b Buffer) At(x, y int) int {
if x < 0 || x > b.Width-1 || y < 0 || y > b.Height-1 {
return White
}
// [0][1][2][3] [4][5][6][7]
i, mask := x/8, byte(0b1000_0000>>byte(x%8))
if b.buffer[y][i]&mask > 0 {
return Black
}
return White
}
// getPixelsBlock - get rectangular block of pixels as linear bits
func (b Buffer) getPixelsBlock(x, y, width, height int) int {
if x < 0 || x > b.Width || y < 0 || y > b.Height {
return 0
}
result, num := 0, width*height-1
for h := 0; h < height; h++ {
for w := 0; w < width; w++ {
result |= b.At(x+w, y+h) << num
num--
}
}
return result
}
// IsEqual - are two buffers equal?
func (b Buffer) IsEqual(a Buffer) bool {
if b.Width != a.Width || b.Height != a.Height {
return false
}
for y := 0; y < b.Height; y++ {
for x := 0; x < b.Width; x++ {
if b.At(x, y) != a.At(x, y) {
return false
}
}
}
return true
}
// RenderAsStrings - render buffer as slice of strings with pixel characters
func (b Buffer) RenderAsStrings(mode PixelMode) []string {
blockW, blockH := mode.Width(), mode.Height()
var result []string
width := b.Width / blockW
if b.Width%blockW != 0 {
width++
}
height := b.Height / blockH
if b.Height%blockH != 0 {
height++
}
for y := 0; y < height; y++ {
line := ""
for x := 0; x < width; x++ {
charIndex := b.getPixelsBlock(x*blockW, y*blockH, blockW, blockH)
line += string(mode.charMapping[charIndex])
}
result = append(result, line)
}
return result
}