forked from CelesteClassic/celeste-maker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gif.lua
193 lines (182 loc) · 3.91 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
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
-- GIF encoder specialized for PICO-8
-- by gamax92.
local palmap={}
for i=1, 16 do
local palette=pico8.palette[i]
local value=bit.lshift(palette[1], 16)+bit.lshift(palette[2], 8)+palette[3]
palmap[i-1]=value
palmap[value]=i-1
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, nil, pico8.resolution[1]*2-1, pico8.resolution[2]*2-1
if self.first then
y0=0
self.first=nil
else
for y=0, y1 do
local kill=false
for x=x0, x1 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=y
kill=true
break
end
end
if kill then
break
end
end
if y0==nil then
-- TODO: Output longer delay instead of bogus frame
x0, y0, x1, y1=0, 0, 0, 0
end
for x=x0, x1 do
local kill=false
for y=y0, y1 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
x0=x
kill=true
break
end
end
if kill then
break
end
end
for y=y1, y0, -1 do
local kill=false
for x=x0, x1 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
y1=y
kill=true
break
end
end
if kill then
break
end
end
for x=x1, x0, -1 do
local kill=false
for y=y0, y1 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
x1=x
kill=true
break
end
end
if kill then
break
end
end
end
self.file:write("\44"..num2str(x0)..num2str(y0)..num2str(x1-x0+1)..num2str(y1-y0+1).."\0\4")
local codetbl={}
for i=0, 15 do
codetbl[string.char(i)]=i
end
local last=17
local buffer=""
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=string.char(palmap[bit.lshift(r, 16)+bit.lshift(g, 8)+b])
local temp=buffer..index
if codetbl[temp] then
buffer=temp
else
stream[#stream+1]=codetbl[buffer]
last=last+1
if last<4095 then
codetbl[temp]=last
else
stream[#stream+1]=16
codetbl={}
for i=0, 15 do
codetbl[string.char(i)]=i
end
last=17
end
buffer=tostring(index)
end
end
end
stream[#stream+1]=codetbl[buffer]
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=1, 16 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