-
Notifications
You must be signed in to change notification settings - Fork 1
/
sixel.fs
247 lines (208 loc) · 7.8 KB
/
sixel.fs
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
242
243
244
245
246
247
\ Copyright (c) 2019, Travis Bemann
\ All rights reserved.
\
\ Redistribution and use in source and binary forms, with or without
\ modification, are permitted provided that the following conditions are met:
\
\ 1. Redistributions of source code must retain the above copyright notice,
\ this list of conditions and the following disclaimer.
\
\ 2. Redistributions in binary form must reproduce the above copyright notice,
\ this list of conditions and the following disclaimer in the documentation
\ and/or other materials provided with the distribution.
\
\ 3. Neither the name of the copyright holder nor the names of its
\ contributors may be used to endorse or promote products derived from
\ this software without specific prior written permission.
\
\ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
\ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
\ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
\ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
\ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
\ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
\ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
\ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
\ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\ POSSIBILITY OF SUCH DAMAGE.
get-order get-current base @
decimal
forth-wordlist 1 set-order
forth-wordlist set-current
wordlist constant sixel-wordlist
forth-wordlist sixel-wordlist 2 set-order
sixel-wordlist set-current
1024 constant sixel-output-size
$1b constant escape
begin-structure sixel-fb-size
field: sixel-fb-width
field: sixel-fb-height
field: sixel-fb-colors
field: sixel-fb-color-count
field: sixel-fb-row-length
field: sixel-fb-color-length
field: sixel-fb-data
field: sixel-fb-output
field: sixel-fb-output-index
end-structure
1024 constant max-color-count
create color-sizes max-color-count allot
create color-sums max-color-count 2 * allot
: init-color-sums ( -- )
0 0 begin dup max-color-count < while
dup dup 10 < if
drop 2
else dup 100 < if
drop 3
else 1000 < if
4
else
5
then then then
2dup swap color-sizes + c! rot + 2dup swap 2 * color-sums + h! swap 1 +
repeat
2drop ;
init-color-sums
: buffer-char ( c fb -- )
dup sixel-fb-output-index @ sixel-output-size >= if
dup sixel-fb-output @ sixel-output-size type
1 over sixel-fb-output-index ! sixel-fb-output @ c!
else
dup sixel-fb-output-index @ >r tuck sixel-fb-output @ r@ + c!
r> 1 + swap sixel-fb-output-index !
then ;
: buffer-string ( c-addr u fb -- )
>r begin dup 0 > while
1 - swap dup c@ r@ buffer-char 1 + swap
repeat
r> drop 2drop ;
: buffer-decimal ( n fb -- )
swap ['] format-number 10 base-execute rot buffer-string ;
: flush-buffer ( fb -- )
dup sixel-fb-output @ over sixel-fb-output-index @ type
0 swap sixel-fb-output-index ! ;
: begin-frame ( fb -- )
escape over buffer-char [char] P over buffer-char [char] q swap buffer-char ;
: end-frame ( -- ) escape emit [char] \ emit ;
: clear-colors ( fb -- )
>r 0 begin dup r@ sixel-fb-color-count @ < while
dup cells r@ sixel-fb-colors @ + 0 swap ! 1 +
repeat
drop r> drop ;
: set-color ( r g b color fb -- )
sixel-fb-colors @ swap cells +
swap $ff and rot $ff and 8 lshift or rot $ff and 16 lshift or swap ! ;
: get-color ( color fb -- r g b )
sixel-fb-colors @ swap cells + @ >r
r@ 16 rshift r@ 8 rshift $ff and r> $ff and ;
: generate-palette-entry ( r g b color fb --)
[char] # over buffer-char tuck buffer-decimal
[char] ; over buffer-char [char] 2 over buffer-char
[char] ; over buffer-char 3 roll 100 * 255 / over buffer-decimal
[char] ; over buffer-char rot 100 * 255 / over buffer-decimal
[char] ; over buffer-char swap 100 * 255 / swap buffer-decimal ;
: generate-palette ( fb -- )
>r 0 begin dup r@ sixel-fb-color-count @ < while
dup r@ get-color 3 pick r@ generate-palette-entry 1 +
repeat
drop r> drop ;
: get-data-size ( fb -- bytes )
>r r@ sixel-fb-color-count @ r@ sixel-fb-width @ 1 + *
r@ sixel-fb-height @ 6 / *
r@ sixel-fb-height @ 6 /
r@ sixel-fb-color-count @ 1 - 2 * color-sums + h@ * +
r> sixel-fb-height @ 6 / + ;
: get-row-length ( fb -- bytes )
>r r@ sixel-fb-color-count @ r@ sixel-fb-width @ 1 + * 1 +
r> sixel-fb-color-count @ 1 - 2 * color-sums + h@ + ;
: get-color-length ( fb -- bytes ) sixel-fb-width @ 1 + ;
: get-address ( x y color fb -- addr )
>r r@ sixel-fb-row-length @ rot 6 / *
over 2 * color-sums + h@ +
swap r@ sixel-fb-color-length @ * + + r> sixel-fb-data @ + ;
: clear-pixel ( x y fb -- )
>r r@ sixel-fb-row-length @ over 6 / * r@ sixel-fb-data @ + rot +
swap 6 mod swap
0 begin dup r@ sixel-fb-color-count @ < while
swap over color-sizes + c@ + swap
over c@ 63 - 1 3 pick lshift not and 63 + 2 pick c!
1 + swap r@ sixel-fb-color-length @ + swap
repeat
2drop drop r> drop ;
: pixel! ( x y color fb -- )
3 pick 3 pick 2 pick clear-pixel
2 pick >r get-address dup c@ 63 - 1 r> 6 mod lshift or 63 + swap c! ;
: pixel-no-clear! ( x y color fb -- )
2 pick >r get-address dup c@ 63 - 1 r> 6 mod lshift or 63 + swap c! ;
: populate-lf ( y/6 fb -- )
>r r@ sixel-fb-row-length @ swap 1 + * 1 - r> sixel-fb-data @ +
[char] - swap c! ;
: populate-cr ( y/6 color fb -- )
>r dup 2 * color-sums + h@
r@ sixel-fb-row-length @ 3 pick * + over r@ sixel-fb-color-length @ * +
r@ sixel-fb-width @ + r> sixel-fb-data @ + [char] $ swap c! 2drop ;
: populate-color ( y/6 color fb -- )
>r dup 2 * color-sums + h@ over color-sizes + c@ -
r@ sixel-fb-row-length @ 3 pick * + over r@ sixel-fb-color-length @ * +
r> sixel-fb-data @ + [char] # over c! 1 +
over ['] format-number 10 base-execute rot swap move 2drop ;
: clear-line-color ( y/6 color fb -- )
>r dup 2 * color-sums + h@
r@ sixel-fb-row-length @ 3 pick * + over r@ sixel-fb-color-length @ * +
r@ sixel-fb-data @ +
r> sixel-fb-width @ 63 fill 2drop ;
: fill-line-color ( y/6 color fb -- )
>r dup 2 * color-sums + h@
r@ sixel-fb-row-length @ 3 pick * + over r@ sixel-fb-color-length @ * +
r@ sixel-fb-data @ +
r> sixel-fb-width @ 126 fill 2drop ;
: clear-pixels ( fb -- )
>r 0 begin dup r@ sixel-fb-height @ 6 / < while
0 begin dup r@ sixel-fb-color-count @ < while
2dup r@ clear-line-color 1 +
repeat
drop 1 +
repeat
drop r> drop ;
: fill-pixels ( color fb -- )
>r 0 begin dup r@ sixel-fb-height @ 6 / < while
0 begin dup r@ sixel-fb-color-count @ < while
dup 3 pick = if
2dup r@ fill-line-color
else
2dup r@ clear-line-color
then
1 +
repeat
drop 1 +
repeat
2drop r> drop ;
: init-pixels ( fb -- )
>r 0 begin dup r@ sixel-fb-height @ 6 / < while
dup r@ populate-lf
0 begin dup r@ sixel-fb-color-count @ < while
2dup r@ populate-cr
2dup r@ populate-color
2dup r@ clear-line-color
1 +
repeat
drop 1 +
repeat
drop r> drop ;
: draw ( fb -- )
dup begin-frame dup generate-palette dup flush-buffer
dup sixel-fb-data @ swap get-data-size type end-frame ;
: new-sixel-fb ( width height colors -- fb )
>r dup 6 mod dup 0 > if 6 swap - + else drop then
here sixel-fb-size allot
r> over sixel-fb-color-count ! tuck sixel-fb-height ! tuck sixel-fb-width !
dup get-row-length over sixel-fb-row-length !
dup get-color-length over sixel-fb-color-length !
here >r dup sixel-fb-color-count @ cells allot r> over sixel-fb-colors !
dup clear-colors
here >r dup get-data-size allot r> over sixel-fb-data !
dup init-pixels
here >r sixel-output-size allot r> over sixel-fb-output !
0 over sixel-fb-output-index ! ;
base ! set-current set-order