-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpx9.lua
291 lines (238 loc) · 5.5 KB
/
px9.lua
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
-->8
-- px9 decompression and compression - credit to zep.
-- px9 decompress
-- x0,y0 where to draw to
-- src compressed data address
-- vget read function (x,y)
-- vset write function (x,y,v)
function
px9_decomp(x0,y0,src,vget,vset)
local function vlist_val(l, val)
-- find position and move
-- to head of the list
--[ 2-3x faster than block below
local v,i=l[1],1
while v!=val do
i+=1
v,l[i]=l[i],v
end
l[1]=val
--]]
--[[ 7 tokens smaller than above
for i,v in ipairs(l) do
if v==val then
add(l,deli(l,i),1)
return
end
end
--]]
end
-- bit cache is between 16 and
-- 31 bits long with the next
-- bit always aligned to the
-- lsb of the fractional part
local cache,cache_bits=0,0
function getval(bits)
if cache_bits<16 then
-- cache next 16 bits
cache+=%src>>>16-cache_bits
cache_bits+=16
src+=2
end
-- clip out the bits we want
-- and shift to integer bits
local val=cache<<32-bits>>>16-bits
-- now shift those bits out
-- of the cache
cache=cache>>>bits
cache_bits-=bits
return val
end
-- get number plus n
function gnp(n)
local bits=0
repeat
bits+=1
local vv=getval(bits)
n+=vv
until vv<(1<<bits)-1
return n
end
-- header
local
w,h_1, -- w,h-1
eb,el,pr,
x,y,
splen,
predict
=
gnp"1",gnp"0",
gnp"1",{},{},
0,0,
0
--,nil
for i=1,gnp"1" do
add(el,getval(eb))
end
for y=y0,y0+h_1 do
for x=x0,x0+w-1 do
splen-=1
if(splen<1) then
splen,predict=gnp"1",not predict
end
local a=y>y0 and vget(x,y-1) or 0
-- create vlist if needed
local l=pr[a]
if not l then
l={}
for e in all(el) do
add(l,e)
end
pr[a]=l
end
-- grab index from stream
-- iff predicted, always 1
local v=l[predict and 1 or gnp"2"]
-- update predictions
vlist_val(l, v)
vlist_val(el, v)
-- set
vset(x,y,v)
end
end
end
-->8
-- px9 compress
-- x0,y0 where to read from
-- w,h image width,height
-- dest address to store
-- vget read function (x,y)
function
px9_comp(x0,y0,w,h,dest,vget)
local dest0=dest
local bit=1
local byte=0
local function vlist_val(l, val)
-- find position and move
-- to head of the list
--[ 2-3x faster than block below
local v,i=l[1],1
while v!=val do
i+=1
v,l[i]=l[i],v
end
l[1]=val
return i
--]]
--[[ 8 tokens smaller than above
for i,v in ipairs(l) do
if v==val then
add(l,deli(l,i),1)
return i
end
end
--]]
end
function putbit(bval)
if (bval) byte+=bit
poke(dest, byte) bit<<=1
if (bit==256) then
bit=1 byte=0
dest += 1
end
end
function putval(val, bits)
for i=0,bits-1 do
putbit(val&1<<i > 0)
end
end
function putnum(val)
local bits = 0
repeat
bits += 1
local mx=(1<<bits)-1
local vv=min(val,mx)
putval(vv,bits)
val -= vv
until vv<mx
end
-- first_used
local el={}
local found={}
local highest=0
for y=y0,y0+h-1 do
for x=x0,x0+w-1 do
c=vget(x,y)
if not found[c] then
found[c]=true
add(el,c)
highest=max(highest,c)
end
end
end
-- header
local bits=1
while highest >= 1<<bits do
bits+=1
end
putnum(w-1)
putnum(h-1)
putnum(bits-1)
putnum(#el-1)
for i=1,#el do
putval(el[i],bits)
end
-- data
local pr={} -- predictions
local dat={}
for y=y0,y0+h-1 do
for x=x0,x0+w-1 do
local v=vget(x,y)
local a=0
if (y>y0) a+=vget(x,y-1)
-- create vlist if needed
local l=pr[a]
if not l then
l={}
for i=1,#el do
l[i]=el[i]
end
pr[a]=l
end
-- add to vlist
add(dat,vlist_val(l,v))
-- and to running list
vlist_val(el, v)
end
end
-- write
-- store bit-0 as runtime len
-- start of each run
local nopredict
local pos=1
while pos <= #dat do
-- count length
local pos0=pos
if nopredict then
while dat[pos]!=1 and pos<=#dat do
pos+=1
end
else
while dat[pos]==1 and pos<=#dat do
pos+=1
end
end
local splen = pos-pos0
putnum(splen-1)
if nopredict then
-- values will all be >= 2
while pos0 < pos do
putnum(dat[pos0]-2)
pos0+=1
end
end
nopredict=not nopredict
end
if (bit!=1) dest+=1 -- flush
return dest-dest0
end