forked from gonengazit/Celia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gif.lua
155 lines (141 loc) · 3.54 KB
/
gif.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
-- GIF encoder specialized for PICO-8
-- by gamax92.
local bit=require("numberlua").bit
local palmap={}
for i=0, 15 do
local palette=pico8.palette[i]
local value=bit.lshift(palette[1], 16)+bit.lshift(palette[2], 8)+palette[3]
palmap[i]=value
palmap[value]=i
end
local function num2str(data)
return string.char(bit.band(data, 0xFF), bit.rshift(data, 8))
end
local gif={}
function gif:frame(data)
self.file:write("\33\249\4\4\3\0\0\0")
local last=self.last
local x0, y0, x1, y1=0, 0, pico8.resolution[1]*2-1, pico8.resolution[1]*2-1
-- disabled bounding box for now, because discord's gif decoder doesn't seem to respect it
--[[ if self.first then
x0, y0, x1, y1=0, 0, pico8.resolution[1]*2-1, pico8.resolution[1]*2-1
self.first=nil
else
local t = love.timer.getTime()
--get bounding box for changes
x0, y0, x1, y1=pico8.resolution[1]*2-1, pico8.resolution[1]*2-1, 0, 0
local changed = false
for y=0, pico8.resolution[1]*2-1 do
for x=0, pico8.resolution[1]*2-1 do
local r1, g1, b1=last:getPixel(x, y)
local r2, g2, b2=data:getPixel(x, y)
if r1~=r2 or g1~=g2 or b1~=b2 then
y0=math.min(y0, y)
x0=math.min(x0, x)
y1=math.max(y1, y)
x1=math.max(x1, x)
changed=true
end
end
end
if not changed then
-- TODO: Output longer delay instead of bogus frame
x0, y0, x1, y1=0, 0, 0, 0
end
print("bbox " .. love.timer.getTime() -t)
end ]]
self.file:write("\44"..num2str(x0)..num2str(y0)..num2str(x1-x0+1)..num2str(y1-y0+1).."\0\4")
local trie={}
for i=0, 15 do
trie[i]={[-1]=i}
end
local last=17
local trie_ptr=trie
local stream={16}
for y=y0, y1 do
for x=x0, x1 do
local r, g, b=data:getPixel(x, y)
r, g, b=r*255, g*255, b*255
local index=palmap[bit.lshift(r, 16)+bit.lshift(g, 8)+b]
if trie_ptr[index] then
trie_ptr = trie_ptr[index]
else
stream[#stream+1]=trie_ptr[-1]
last=last+1
if last<4095 then
trie_ptr[index]={[-1]=last}
else
stream[#stream+1]=16
trie={}
for i=0, 15 do
trie[i]={[-1]=i}
end
last=17
end
trie_ptr=trie[index]
end
end
end
stream[#stream+1]=trie_ptr[-1]
stream[#stream+1]=17
local output={}
local size=5
local bits=0
local pack=0
local base=-16
for i=1, #stream do
pack=pack+bit.lshift(stream[i], bits)
bits=bits+size
while bits>=8 do
bits=bits-8
output[#output+1]=string.char(bit.band(pack, 0xFF))
pack=bit.rshift(pack, 8)
end
if i-base>=2^size then
size=size+1
end
if stream[i]==16 then
base=i-17
size=5
end
end
while bits>0 do
bits=bits-8
output[#output+1]=string.char(bit.band(pack, 0xFF))
pack=bit.rshift(pack, 8)
end
output=table.concat(output)
while #output>0 do
self.file:write(string.char(math.min(#output, 255))..output:sub(1, 255))
output=output:sub(256)
end
self.file:write("\0")
self.last=data
end
function gif:close()
self.file:write("\59")
self.file:close()
self.file=nil
self.last=nil
end
local gifmt={
__index=function(t, k)
return gif[k]
end
}
local giflib={}
function giflib.new(filename)
local file, err=love.filesystem.newFile(filename, "w")
if not file then
return nil, err
end
file:write("GIF89a"..num2str(pico8.resolution[1]*2)..num2str(pico8.resolution[2]*2).."\243\0\0")
for i=0, 15 do
local palette=pico8.palette[i]
file:write(string.char(palette[1], palette[2], palette[3]))
end
file:write("\33\255\11NETSCAPE2.0\3\1\0\0\0")
local last=love.image.newImageData(pico8.resolution[1]*2, pico8.resolution[2]*2)
return setmetatable({filename=filename, file=file, last=last, first=true}, gifmt)
end
return giflib