-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
232 lines (195 loc) · 4.87 KB
/
main.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
package main
import (
"fmt"
"math"
"strings"
"syscall/js"
"time"
)
func main() {
c := make(chan bool)
js.Global().Set("renderFIF", js.FuncOf(callRenderFIF))
<-c
}
type ByteReadr struct {
offset int
bytes []byte
}
func (r *ByteReadr) readByte() byte {
a := r.bytes[r.offset]
r.offset = r.offset + 1
return a
}
func (r *ByteReadr) readString(len int) string {
a := r.bytes[r.offset : r.offset+len]
r.offset = r.offset + len
return string(a)
}
func (r *ByteReadr) readBytes(len int) []byte {
a := r.bytes[r.offset : r.offset+len]
r.offset = r.offset + len
return a
}
func itob(b byte, fg int, bg int) int {
if b > 0 {
return fg
}
return bg
}
func drawBraille(c js.Value, braille byte, x int, y int, fg int, bg int) {
var r1 int = itob(braille&0x08, fg, bg)
var r2 int = itob(braille&0x10, fg, bg)
var r3 int = itob(braille&0x20, fg, bg)
var r4 int = itob(braille&0x80, fg, bg)
var l1 int = itob(braille&0x01, fg, bg)
var l2 int = itob(braille&0x02, fg, bg)
var l3 int = itob(braille&0x04, fg, bg)
var l4 int = itob(braille&0x40, fg, bg)
draw(c, x, y-3, l1)
draw(c, x+1, y-3, r1)
draw(c, x, y-2, l2)
draw(c, x+1, y-2, r2)
draw(c, x, y-1, l3)
draw(c, x+1, y-1, r3)
draw(c, x, y, l4)
draw(c, x+1, y, r4)
}
func draw(c js.Value, x int, y int, color int) {
/*r := color & 0x0000FF
g := color & 0x00FF
b := color & 0xFF
c.Set("fillStyle", "rgb(" + strconv.Itoa(r) + "," + strconv.Itoa(g) + "," + strconv.Itoa(b) +")")*/
c.Set("fillStyle", "#"+fmt.Sprintf("%06x", color))
c.Call("fillRect", x, y, 1, 1)
}
func callRenderFIF(this js.Value, inputs []js.Value) interface{} {
go renderFIF(this, inputs)
return nil
}
func renderFIF(this js.Value, inputs []js.Value) interface{} {
if len(inputs) >= 4 {
js.Global().Get("document").Call("getElementById", inputs[3].String()).Set("innerText", "RENDERING...")
}
delayEnabled := false
delay := time.Millisecond
var sleptTimeNS int64 = 0
if len(inputs) >= 5 {
delayEnabled = true
delay = time.Duration(inputs[4].Int()) * time.Millisecond
}
canvasV := inputs[0].String()
var cv js.Value = js.
Global().
Get("document").
Call("getElementById", canvasV)
length := inputs[2].Int()
var aa []byte = make([]byte, length)
nowe := time.Now()
js.CopyBytesToGo(aa, inputs[1])
nowg := time.Now().UnixNano() - nowe.UnixNano()
now := time.Now()
br := ByteReadr{
offset: 0,
bytes: aa,
}
var fastIF = br.readString(6)
println(fastIF)
println(strings.Compare(fastIF, "FastIF"))
if strings.Compare(fastIF, "FastIF") != 0 {
panic("Not FIF")
}
var w int = int(br.readByte()) * 2
var h int = int(br.readByte()) * 4
if w > 320 {
println("Width exceeds RPH-set standars")
}
if h > 200 {
println("Width exceeds RPH-set standars")
}
cv.Set("width", w)
cv.Set("height", h)
var canvas js.Value = cv.Call("getContext", "2d")
run := true
__len := len(br.bytes)
background := 0
foreground := 0
for run {
cmd := br.readByte()
switch cmd {
case 0x01:
{
r := br.readByte()
g := br.readByte()
b := br.readByte()
background = int(b) + (int(g) << 8) + (int(r) << 16)
}
case 0x02:
{
r := br.readByte()
g := br.readByte()
b := br.readByte()
foreground = int(b) + (int(g) << 8) + (int(r) << 16)
}
case 0x10:
{
x := br.readByte()
y := br.readByte()
size := br.readByte()
data := br.readBytes(int(size))
for i := 0; i < int(size); i++ {
b := data[i]
drawBraille(canvas, b, int(x)*2+(i*2), int(y)*4, foreground, background)
}
}
case 0x11:
{
x := br.readByte()
y := br.readByte()
w := br.readByte()
h := br.readByte()
lower := br.readByte()
for xr := 0; xr < int(w); xr++ {
for yr := 0; yr < int(h); yr++ {
drawBraille(canvas, lower, int(x)*2+xr*2, int(y)*4+yr*4, foreground, background)
}
}
}
case 0x12:
{
we := br.readByte()
time.Sleep(time.Duration(we * 10) * time.Millisecond)
sleptTimeNS += (int64(we) * 10) * 1000 * 1000
}
case 0x13:
{
x := br.readByte()
y := br.readByte()
size := br.readByte()
data := br.readBytes(int(size))
for yr := 0; yr < int(size); yr++ {
a := data[yr]
drawBraille(canvas, a, int(x)*2, int(y)*4 + yr*4, foreground, background)
}
}
case 0x20:
{
run = false
}
}
if br.offset > __len+1 {
println("Non-compliant encoder was used. No 0x20 at the end.")
break
}
if delayEnabled {
time.Sleep(delay)
}
}
taken := now.UnixNano() - time.Now().UnixNano()
if len(inputs) >= 4 {
js.Global().Get("document").Call("getElementById", inputs[3].String()).Set("innerText", fmt.Sprintf("Render time: %.2fms, Copy time: %.2fms, Slept time: %.2fms", math.Abs(float64(taken)-float64(sleptTimeNS))/1000000, math.Abs(float64(nowg))/1000000, math.Abs(float64(sleptTimeNS))/1000000)) // DONT QUESTION THE A B S
}
if !js.Global().Get("finished").IsNull() {
js.Global().Call("finished")
}
return nil
}