-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgif.lua
More file actions
232 lines (204 loc) · 6.15 KB
/
Copy pathgif.lua
File metadata and controls
232 lines (204 loc) · 6.15 KB
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
local ffi = require "ffi"
local bit = require "bit"
local bio = require "bio"
local surf = require "surf"
local lzw = require "lzw"
local bnot = bit.bnot
local bor, band = bit.bor, bit.band
local lshift, rshift = bit.lshift, bit.rshift
-- == Encoder ==
local write_num = bio.write_lei16
local function write_nums(f, ...)
local nums = {...}
for i, n in pairs(nums) do
write_num(f, n)
end
end
local function get_depth(n)
local m, e = math.frexp(n-1)
return math.max(e, 1)
end
local GIFout = {}
GIFout.__index = GIFout
local function new_gif(f, w, h, colors)
if type(f) == "string" then f = io.open(f, "wb") end
local depth = get_depth(#colors)
local self = setmetatable({f=f, w=w, h=h, d=depth, gct=colors}, GIFout)
if depth == 1 then
self.back = surf.new_bitmap(w, h)
else
self.back = surf.new_bytemap(w, h)
end
f:write("GIF89a")
write_nums(f, w, h)
f:write(string.char(0xF0 + depth - 1, 0, 0)) -- FDSZ, BGINDEX, ASPECT
local i = 1
while i <= #colors do
f:write(string.char(unpack(colors[i])))
i = i + 1
end
while i <= 2^depth do -- GCT size must be a power of two
f:write(string.char(0, 0, 0)) -- fill unused colors as black
i = i + 1
end
self.n = 0 -- # of frames added
return self
end
function GIFout:set_loop(n)
n = n or 0
self.f:write("!")
self.f:write(string.char(0xFF, 0x0B))
self.f:write("NETSCAPE2.0")
self.f:write(string.char(0x03, 0x01))
write_num(self.f, n)
self.f:write(string.char(0))
end
function GIFout:set_delay(d)
self.f:write("!")
self.f:write(string.char(0xF9, 0x04, 0x04))
write_num(self.f, d)
self.f:write(string.char(0, 0))
end
function GIFout:get_bbox(frame)
local w, h = self.w, self.h
if self.n == 0 then return 0, 0, w, h end
local xmin, ymin = w, h
local xmax, ymax = 0, 0
local back = self.back
for y = 0, h-1 do
for x = 0, w-1 do
if frame:pget(x, y) ~= back:pget(x, y) then
if x < xmin then xmin = x end
if y < ymin then ymin = y end
if x > xmax then xmax = x end
if y > ymax then ymax = y end
end
end
end
if xmin == w or ymin == h then return 0, 0, 1, 1 end
return xmin, ymin, xmax-xmin+1, ymax-ymin+1
end
function GIFout:put_image(frame, x, y, w, h)
self.f:write(",")
write_nums(self.f, x, y, w, h)
self.f:write(string.char(0))
lzw.encode(self.f, self.d, frame, x, y, w, h) -- IP (Appendix F)
end
function GIFout:add_frame(frame, delay)
if delay then self:set_delay(delay) end
local x, y, w, h = self:get_bbox(frame)
self:put_image(frame, x, y, w, h)
self.n = self.n + 1
self.back:blit(x, y, frame, x, y, w, h)
end
function GIFout:close()
self.f:write(";")
self.f:close()
end
-- == Decoder ==
local read_num = bio.read_lei16
local GIFin = {}
GIFin.__index = GIFin
local function open_gif(f)
if type(f) == "string" then f = io.open(f, "rb") end
assert(f:read(6) == "GIF89a", "invalid signature")
local w, h = read_num(f), read_num(f)
local fdsz = bio.read_byte(f)
local has_gct = rshift(fdsz, 7) == 1
local d = band(fdsz, 7) + 1
local bg = bio.read_byte(f)
f:seek("cur", 1) -- ASPECT:u8
local self = setmetatable({f=f, w=w, h=h, d=d, bg=bg}, GIFin)
self.gct = has_gct and self:read_color_table(d) or {}
if d == 1 then
self.surf = surf.new_bitmap(w, h)
else
self.surf = surf.new_bytemap(w, h)
end
return self
end
function GIFin:read_color_table(d)
local ct = {}
local ncolors = lshift(1, d)
for i = 1, ncolors do
local r = bio.read_byte(self.f)
local g = bio.read_byte(self.f)
local b = bio.read_byte(self.f)
table.insert(ct, {r, g, b})
end
return ct
end
function GIFin:discard_sub_blocks()
repeat
local size = bio.read_byte(self.f)
self.f:seek("cur", size)
until size == 0
end
function GIFin:read_graphic_control_ext()
assert(bio.read_byte(self.f) == 0x04, "invalid GCE block size")
local rdit = bio.read_byte(self.f)
self.disposal = band(rshift(rdit, 2), 3)
self.input = band(rshift(rdit, 1), 1) == 1
local transp = band(rdit, 1) == 1
self.delay = read_num(self.f)
local tindex = bio.read_byte(self.f)
self.tindex = transp and tindex or -1
self.f:seek("cur", 1) -- end-of-block
end
function GIFin:read_application_ext()
assert(bio.read_byte(self.f) == 0x0B, "invalid APP block size")
local app_ip = self.f:read(8)
local app_auth_code = self.f:read(3)
if app_ip == "NETSCAPE" then
self.f:seek("cur", 2) -- always 0x03, 0x01
self.loop = read_num(self.f)
self.f:seek("cur", 1) -- end-of-block
else
self:discard_sub_blocks()
end
end
function GIFin:read_ext()
local label = bio.read_byte(self.f)
if label == 0xF9 then
self:read_graphic_control_ext()
elseif label == 0xFF then
self:read_application_ext()
else
error(("unknown extension: %02X"):format(label))
end
end
function GIFin:read_image()
local x, y = read_num(self.f), read_num(self.f)
local w, h = read_num(self.f), read_num(self.f)
local fisrz = bio.read_byte(self.f)
local has_lct = band(rshift(fisrz, 7), 1) == 1
--~ assert(not has_lct, "unsupported GIF feature: Local Color Table")
local interlace = band(rshift(fisrz, 6), 1) == 1
assert(not interlace, "unsupported GIF feature: Interlaced Frame")
local d = band(fisrz, 7) + 1
local lct = has_lct and self:read_color_table(d) or {}
d = has_lct and d or self.d
lzw.decode(self.f, d, self.surf, x, y, w, h) -- IP (Appendix F)
end
function GIFin:get_frame()
local sep = self.f:read(1)
while sep ~= "," do
if sep == ";" then
return nil
elseif sep == "!" then
self:read_ext()
else
return nil
end
sep = self.f:read(1)
end
self:read_image()
return self.surf
end
function GIFin:frames()
return function() return self:get_frame() end
end
function GIFin:close()
self.f:close()
end
return {new_gif=new_gif, open_gif=open_gif}