-
Notifications
You must be signed in to change notification settings - Fork 0
/
wofl.py
executable file
·149 lines (107 loc) · 3.77 KB
/
wofl.py
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
#!/usr/bin/env python
import sys
import subprocess
import multiprocessing
WOFL_VERSION = "1.0"
MIPMAP_NUM = 4
def error_exit(msg):
print(msg.strip())
exit(1)
def int_b(n, l=4):
try:
return n.to_bytes(l, "little")
except BaseException:
error_exit("Overflow exception.")
def run_cmd(cmd, **kwargs):
proc = subprocess.run(cmd, capture_output=True, **kwargs)
if proc.stderr:
error_exit(proc.stderr.decode())
return proc
def image_info(files):
proc = run_cmd(["magick", "identify", "-format", "%w %h\n"] + files)
names, dims = [], []
div = 2 ** (MIPMAP_NUM - 1)
for line, cur_file in zip(proc.stdout.decode().splitlines(), files):
w, h = [int(i) for i in line.split()]
if w % div or h % div:
error_exit(
cur_file +
" has invalid resolution " +
str(w) +
"x" +
str(h) +
".")
dims.append([w, h])
try:
idx = cur_file.rindex('.')
name = cur_file[:idx]
except BaseException:
name = cur_file
name = bytes(name, encoding="ascii")
name = name[:min(len(name), 16)]
names.append(name)
name_set = set()
for name in names:
if name in name_set:
error_exit("Duplicate texture name '" + name.decode() + "'.")
name_set.add(name)
return names, dims
def texture_file(f, name, dim):
mip_cmd = (["magick", "convert", f, "-depth", "8", "-write", "RGB:-"]
+ ["-resize", "50%", "-write", "RGB:-"] * (MIPMAP_NUM - 2)
+ ["-resize", "50%", "RGB:-"])
rgb_img = run_cmd(mip_cmd).stdout
rgb_size = len(rgb_img) // 3
if rgb_size != sum([dim[0]*dim[1] // 4**i for i in range(MIPMAP_NUM)]):
error_exit("Internal error.")
h_res = dim[0]
while rgb_size % h_res:
h_res //= 2
v_res = rgb_size // h_res
map_img = run_cmd(["magick", "convert", "-size", f"{h_res}x{v_res}",
"-depth", "8", "RGB:-", "-colors", "256", "+dither",
"MAP:-"], input=rgb_img).stdout
num_colors = (len(map_img) - rgb_size) // 3
mip_offset = [40 + 2]
for i in range(0, MIPMAP_NUM - 1):
mip_offset.append(mip_offset[-1] + dim[0] * dim[1] // 4**i)
header = name.ljust(16, b'\0') + \
b''.join([int_b(n) for n in dim + mip_offset])
return b''.join([header,
b'\0' * 2,
map_img[3 * num_colors:],
b'\0' * 2,
map_img[: 3 * num_colors].ljust(256 * 3, bytes([0xff])),
b'\0' * 2])
def wad_file(textures):
texture_offset = [12]
for tex in textures:
texture_offset.append(texture_offset[-1] + len(tex))
header = b"WAD3" + int_b(len(textures)) + int_b(texture_offset[-1])
entries = []
for offset, tex in zip(texture_offset, textures):
entry = [int_b(offset),
2 * int_b(len(tex)),
bytes([0x43]),
3 * b'\0',
tex[: 16]]
entries.append(b''.join(entry))
return b''.join([header] + textures + entries)
def texture_helper(x):
return texture_file(*x)
def main():
if len(sys.argv) < 3:
print("WADs Out For [The] Ladies v" + WOFL_VERSION)
print("Usage: wofl input1 [input2 ...] output")
exit(1)
output_file = sys.argv[-1]
input_files = sys.argv[1: -1]
input_names, input_dims = image_info(input_files)
pool = multiprocessing.Pool()
texture_files = pool.map(
texture_helper, zip(
input_files, input_names, input_dims))
with open(output_file, "wb") as f:
f.write(wad_file(texture_files))
if __name__ == "__main__":
main()