-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlovease.lua
418 lines (324 loc) · 9.72 KB
/
lovease.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
--[[
MIT License
Copyright (c) 2021 Pedro Lucas (github.com/elloramir)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]--
-- https://www.lua.org/manual/5.3/manual.html#6.4.2
local BYTE = { "B", 1 }
local WORD = { "H", 2 }
local SHORT = { "h", 2 }
local DWORD = { "I4", 4 }
local LONG = { "i4", 4 }
local FIXED = { "i4", 4 }
-- unpack file data into a number
local function read_num(file, fmt, amount)
amount = amount or 1
return love.data.unpack(fmt[1], file:read(fmt[2] * amount), 1)
end
local function read_string(file)
return file:read(read_num(file, WORD))
end
local function grab_header(file)
local header = {}
header.file_size = read_num(file, DWORD)
header.magic_number = read_num(file, WORD)
if header.magic_number ~= 0xA5E0 then
error("Not a valid aseprite file")
end
header.frames_number = read_num(file, WORD)
header.width = read_num(file, WORD)
header.height = read_num(file, WORD)
header.color_depth = read_num(file, WORD)
header.opacity = read_num(file, DWORD)
header.speed = read_num(file, WORD)
-- skip
read_num(file, DWORD, 2)
header.palette_entry = read_num(file, BYTE)
-- skip
read_num(file, BYTE, 3)
header.number_color = read_num(file, WORD)
header.pixel_width = read_num(file, BYTE)
header.pixel_height = read_num(file, BYTE)
header.grid_x = read_num(file, SHORT)
header.grid_y = read_num(file, SHORT)
header.grid_width = read_num(file, WORD)
header.grid_height = read_num(file, WORD)
-- skip
read_num(file, BYTE, 84)
-- to the future
header.frames = {}
return header
end
local function grab_frame_header(file)
local frame_header = {}
frame_header.bytes_size = read_num(file, DWORD)
frame_header.magic_number = read_num(file, WORD)
if frame_header.magic_number ~= 0xF1FA then
error("Corrupted file")
end
local old_chunks = read_num(file, WORD)
frame_header.frame_duration = read_num(file, WORD)
-- skip
read_num(file, BYTE, 2)
-- if 0, use old chunks as chunks
local new_chunks = read_num(file, DWORD)
if new_chunks == 0 then
frame_header.chunks_number = old_chunks
else
frame_header.chunks_number = new_chunks
end
-- to the future
frame_header.chunks = {}
return frame_header
end
local function grab_color_profile(file)
local color_profile = {}
color_profile.type = read_num(file, WORD)
color_profile.uses_fixed_gama = read_num(file, WORD)
color_profile.fixed_game = read_num(file, FIXED)
-- skip
read_num(file, BYTE, 8)
if color_profile.type ~= 1 then
error("No suported color profile, use sRGB")
end
return color_profile
end
local function grab_palette(file)
local palette = {}
palette.entry_size = read_num(file, DWORD)
palette.first_color = read_num(file, DWORD)
palette.last_color = read_num(file, DWORD)
palette.colors = {}
-- skip
read_num(file, BYTE, 8)
for i = 1, palette.entry_size do
local has_name = read_num(file, WORD)
palette.colors[i] = {
color = {
read_num(file, BYTE),
read_num(file, BYTE),
read_num(file, BYTE),
read_num(file, BYTE)}}
if has_name == 1 then
palette.colors[i].name = read_string(file)
end
end
return palette
end
local function grab_old_palette(file)
local palette = {}
palette.packets = read_num(file, WORD)
palette.colors_packet = {}
for i = 1, palette.packets do
palette.colors_packet[i] = {
entries = read_num(file, BYTE),
number = read_num(file, BYTE),
colors = {}}
-- (0 means 256)
if palette.colors_packet[i].number == 0 then
palette.colors_packet[i].number = 256
end
for j = 1, palette.colors_packet[i].number do
palette.colors_packet[i][j] = {
read_num(file, BYTE),
read_num(file, BYTE),
read_num(file, BYTE)}
end
end
return palette
end
local function grab_layer(file)
local layer = {}
layer.flags = read_num(file, WORD)
layer.type = read_num(file, WORD)
layer.child_level = read_num(file, WORD)
layer.width = read_num(file, WORD)
layer.height = read_num(file, WORD)
layer.blend = read_num(file, WORD)
layer.opacity = read_num(file, BYTE)
-- skip
read_num(file, BYTE, 3)
layer.name = read_string(file)
return layer
end
local function grab_cel(file, size)
local cel = {}
cel.layer_index = read_num(file, WORD)
cel.x = read_num(file, WORD)
cel.y = read_num(file, WORD)
cel.opacity_level = read_num(file, BYTE)
cel.type = read_num(file, WORD)
read_num(file, BYTE, 7) -- skip
-- raw image data
if cel.type == 0 then
cel.width = read_num(file, WORD)
cel.height = read_num(file, WORD)
cel.data = {}
for i = 1, cel.width * cel.height do
cel.data[i] = {
read_num(file, BYTE),
read_num(file, BYTE),
read_num(file, BYTE),
read_num(file, BYTE)
}
end
-- linked cel
elseif cel.type == 1 then
cel.frame_pos_link = read_num(file, WORD)
-- compressed image
elseif cel.type == 2 then
cel.width = read_num(file, WORD)
cel.height = read_num(file, WORD)
cel.data = file:read(size - 26)
-- compressed tilemap
elseif cel.type == 3 then
cel.width = read_num(file, WORD)
cel.height = read_num(file, WORD)
cel.bits_per_tile = read_num(file, WORD)
cel.bitmask_tile_id = read_num(file, DWORD) -- allways 32 bits
cel.bitmask_x_flip = read_num(file, DWORD)
cel.bitmask_y_flip = read_num(file, DWORD)
cel.bitmask_rotation = read_num(file, DWORD)
read_num(file, BYTE, 10) -- skip
cel.data = {}
for i = 1, cel.width * cel.height do
cel.data[i] = { read_num(file, DWORD)}
end
end
return cel
end
local function grab_tags(file)
local tags = {}
tags.number = read_num(file, WORD)
tags.tags = {}
-- skip
read_num(file, BYTE, 8)
for i = 1, tags.number do
tags.tags[i] = {
from = read_num(file, WORD),
to = read_num(file, WORD),
direction = read_num(file, BYTE),
extra_byte = read_num(file, BYTE),
color = read_num(file, BYTE, 3),
skip_holder = read_num(file, BYTE, 8),
name = read_string(file)}
end
return tags
end
local function grab_slice(file)
local slice = {}
slice.key_numbers = read_num(file, DWORD)
slice.keys = {}
slice.flags = read_num(file, DWORD)
-- reserved?
read_num(file, DWORD)
slice.name = read_string(file)
for i = 1, slice.key_numbers do
slice.keys[i] = {
frame = read_num(file, DWORD),
x = read_num(file, DWORD),
y = read_num(file, DWORD),
width = read_num(file, DWORD),
height = read_num(file, DWORD)}
if slice.flags == 1 then
slice.keys[i].center_x = read_num(file, DWORD)
slice.keys[i].center_y = read_num(file, DWORD)
slice.keys[i].center_width = read_num(file, DWORD)
slice.keys[i].center_height = read_num(file, DWORD)
elseif slice.flags == 2 then
slice.keys[i].pivot_x = read_num(file, DWORD)
slice.keys[i].pivot_y = read_num(file, DWORD)
end
end
return slice
end
local function grab_user_data(file)
local user_data = {}
user_data.flags = read_num(file, DWORD)
if user_data.flags == 1 then
user_data.text = read_string(file)
elseif user_data.flags == 2 then
user_data.colors = read_num(file, BYTE, 4)
end
return user_data
end
local function grab_tileset(file)
local tileset = {}
tileset.id = read_num(file, DWORD)
tileset.flags = read_num(file, DWORD)
tileset.num_tiles = read_num(file, DWORD)
tileset.tile_width = read_num(file, WORD)
tileset.tile_height = read_num(file, WORD)
tileset.base_index = read_num(file, SHORT)
-- skip
read_num(file, BYTE, 14)
tileset.name = read_string(file)
if tileset.flags == 1 then
tileset.external_id = read_num(file, DWORD)
tileset.tileset_id_in_external_file = read_num(file, DWORD)
elseif tileset.flags == 2 then
error("Compressed tileset not supported yet")
end
return tileset
end
local function grab_chunk(file)
local chunk = {}
chunk.size = read_num(file, DWORD)
chunk.type = read_num(file, WORD)
if chunk.type == 0x2007 then
chunk.data = grab_color_profile(file)
elseif chunk.type == 0x2019 then
chunk.data = grab_palette(file)
elseif chunk.type == 0x0004 then
chunk.data = grab_old_palette(file)
elseif chunk.type == 0x2004 then
chunk.data = grab_layer(file)
elseif chunk.type == 0x2005 then
chunk.data = grab_cel(file, chunk.size)
elseif chunk.type == 0x2018 then
chunk.data = grab_tags(file)
elseif chunk.type == 0x2022 then
chunk.data = grab_slice(file)
elseif chunk.type == 0x2020 then
chunk.data = grab_user_data(file)
elseif chunk.type == 0x2023 then
chunk.data = grab_tileset(file)
end
return chunk
end
local function ase_loader(src)
local file = love.filesystem.newFile(src)
-- error if file is not found
if not file:open("r") then
error("File not found: " .. src)
end
file:open("r")
local ase = {}
-- parse header
ase.header = grab_header(file)
-- parse frames
for i = 1, ase.header.frames_number do
ase.header.frames[i] = grab_frame_header(file)
-- parse frames chunks
for j = 1, ase.header.frames[i].chunks_number do
ase.header.frames[i].chunks[j] = grab_chunk(file)
end
end
file:close()
return ase
end
return ase_loader