Debian Codegraph @

CorsixTH/Bitmap/lib_spritesheet.lua

raw
1 --[[ Copyright (c) 2010 Peter "Corsix" Cawley
2
3 Permission is hereby granted, free of charge, to any person obtaining a copy of
4 this software and associated documentation files (the "Software"), to deal in
5 the Software without restriction, including without limitation the rights to
6 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7 of the Software, and to permit persons to whom the Software is furnished to do
8 so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 SOFTWARE. --]]
20
21 -- This is a custom written spritesheet library for CorsixTH. It currently only supports
22 -- complex encoding
23
24 local io_open, assert, setmetatable, string_char, table_concat
25 = io.open, assert, setmetatable, string.char, table.concat
26
27 local Spr = {}
28 local mt = {__index = _M}
29
30 function Spr.open(filename_tab, filename_dat, is_complex)
31 return setmetatable({
32 tab = assert(io_open(filename_tab, "wb")),
33 dat = assert(io_open(filename_dat, "wb")),
34 encode = is_complex and Spr.encodeComplex or Spr.encodeSimple,
35 }, mt)
36 end
37
38 function Spr.close(ss)
39 ss.tab:close()
40 ss.dat:close()
41 return ss
42 end
43
44 function Spr.writeDummy(ss)
45 ss.tab:write"\0\0\0\0\0\0"
46 return ss
47 end
48
49 -- Convert an integer into a little endian byte string
50 local function uint4(value)
51 local b0, b1, b2, b3
52 b0 = value % 0x100
53 value = (value - b0) / 0x100
54 b1 = value % 0x100
55 value = (value - b1) / 0x100
56 b2 = value % 0x100
57 value = (value - b2) / 0x100
58 return string_char(b0, b1, b2, value)
59 end
60
61 function Spr.write(ss, width, height, pixels)
62 ss.tab:write(uint4(ss.dat:seek()))
63 ss.tab:write(string_char(width, height))
64 ss.dat:write(ss.encode(width, height, pixels))
65 return ss
66 end
67
68 function Spr.encodeSimple(width, height, data)
69 error "TODO"
70 end
71
72 function Spr.encodeComplex(width, height, data)
73 local result = {}
74 local run_start = 1
75 local run_byte = false
76 local prev = false
77 local function flush_run(i)
78 if run_byte == false then
79 while i - run_start > 63 do
80 result[#result + 1] = "\63"
81 result[#result + 1] = data:sub(run_start, run_start + 62)
82 run_start = run_start + 63
83 end
84 if i ~= run_start then
85 result[#result + 1] = string_char(i - run_start)
86 result[#result + 1] = data:sub(run_start, i - 1)
87 end
88 else
89 if run_byte == 0xFF then
90 while i - run_start >= 63 do
91 result[#result + 1] = "\191"
92 run_start = run_start + 63
93 end
94 if i ~= run_start then
95 result[#result + 1] = string_char(i - run_start + 128)
96 end
97 else
98 while i - run_start >= 255 do
99 result[#result + 1] = "\255\255"
100 result[#result + 1] = string_char(run_byte)
101 run_start = run_start + 255
102 end
103 if i ~= run_start then
104 local d = i - run_start
105 if 4 <= d and d <= 67 then
106 result[#result + 1] = string_char(d + 60)
107 result[#result + 1] = string_char(run_byte)
108 elseif 68 <= d and d <= 130 then
109 result[#result + 1] = string_char(d + 124)
110 result[#result + 1] = string_char(run_byte)
111 else
112 result[#result + 1] = "\255"
113 result[#result + 1] = string_char(d)
114 result[#result + 1] = string_char(run_byte)
115 end
116 end
117 end
118 run_byte = false
119 end
120 run_start = i
121 end
122 for i = 2, #data, 1 do
123 local byte = data:byte(i)
124 if run_byte then
125 if byte ~= run_byte then
126 flush_run(i)
127 end
128 elseif byte == prev and i - run_start >= 4 and data:byte(i - 3) == byte and data:byte(i - 2) == byte then
129 flush_run(i - 3)
130 run_byte = byte
131 end
132 prev = byte
133 end
134 flush_run(#data + 1)
135 return table_concat(result)
136 end
137
138 return Spr