159 lines
6.0 KiB
Python
159 lines
6.0 KiB
Python
"""Make the tree pin icons: the game's log icons with their black backdrop cut away.
|
|
|
|
The wood icons in the client (patch-4.mpq) are logs painted on a black square. This
|
|
reads them, clears the dark pixels that connect to the icon's edge (so dark parts
|
|
inside the logs stay), softens the cut edge, and writes 64x64 32-bit TGA files that
|
|
the 1.12 client loads from the addon folder.
|
|
|
|
usage: py -I make_icons.py <out folder> [preview.png]
|
|
Needs mpq.py: put it next to this script, or point MPQ_PY_DIR at the folder that holds it.
|
|
"""
|
|
import os
|
|
import struct
|
|
import sys
|
|
import zlib
|
|
|
|
sys.path.insert(0, os.environ.get("MPQ_PY_DIR", os.path.dirname(os.path.abspath(__file__))))
|
|
from mpq import MpqArchive # noqa: E402
|
|
|
|
DATA = os.path.expanduser(r"~\Documents\Turtle Wow\Data")
|
|
ICONS = { # output name -> client icon
|
|
"simple": "simple_wood_1",
|
|
"bright": "oak_wood_1",
|
|
"shade": "pine_wood_1",
|
|
"tropical": "tropical_logs_1",
|
|
"star": "star_log_2",
|
|
}
|
|
DARK = 18 # brightest channel at or below this counts as backdrop when it touches the edge
|
|
SOFT = 52 # edge pixels up to this brightness fade out instead of a hard cut
|
|
|
|
out_dir = sys.argv[1]
|
|
preview = sys.argv[2] if len(sys.argv) > 2 else None
|
|
|
|
archives = []
|
|
for n in sorted(os.listdir(DATA)):
|
|
if n.lower().endswith(".mpq") and os.path.getsize(os.path.join(DATA, n)) > 0:
|
|
try:
|
|
archives.append(MpqArchive(os.path.join(DATA, n)))
|
|
except Exception: # noqa: BLE001
|
|
pass
|
|
|
|
|
|
def read(name):
|
|
hit = None
|
|
for a in archives: # later archives win
|
|
if a.has(name):
|
|
hit = a
|
|
return hit.read_raw(name)
|
|
|
|
|
|
def c565(c):
|
|
return ((c >> 11) & 31) * 255 // 31, ((c >> 5) & 63) * 255 // 63, (c & 31) * 255 // 31
|
|
|
|
|
|
def decode_dxt1(data, w, h):
|
|
px = [[(0, 0, 0, 0)] * w for _ in range(h)]
|
|
o = 0
|
|
for by in range(0, h, 4):
|
|
for bx in range(0, w, 4):
|
|
a, b, bits = struct.unpack_from("<HHI", data, o)
|
|
o += 8
|
|
ca, cb = c565(a), c565(b)
|
|
if a > b:
|
|
cols = [ca + (255,), cb + (255,),
|
|
tuple((2 * x + y) // 3 for x, y in zip(ca, cb)) + (255,),
|
|
tuple((x + 2 * y) // 3 for x, y in zip(ca, cb)) + (255,)]
|
|
else:
|
|
cols = [ca + (255,), cb + (255,), tuple((x + y) // 2 for x, y in zip(ca, cb)) + (255,), (0, 0, 0, 0)]
|
|
for i in range(16):
|
|
px[by + i // 4][bx + i % 4] = cols[(bits >> (2 * i)) & 3]
|
|
return px
|
|
|
|
|
|
def load_icon(name):
|
|
d = read("Interface\\Icons\\%s.blp" % name)
|
|
magic, _, comp, adepth = struct.unpack_from("<4sIBB", d)
|
|
assert magic == b"BLP2" and comp == 2 and adepth <= 1, name # DXT1
|
|
w, h = struct.unpack_from("<II", d, 12)
|
|
off = struct.unpack_from("<I", d, 20)[0]
|
|
return decode_dxt1(d[off:], w, h)
|
|
|
|
|
|
FRAME = 5 # the icon's grey frame ring; the addon crops it off anyway (texcoords 0.08-0.92)
|
|
|
|
|
|
def cut(px):
|
|
h, w = len(px), len(px[0])
|
|
bright = [[max(p[:3]) if p[3] else 0 for p in row] for row in px]
|
|
back = [[not (FRAME <= x < w - FRAME and FRAME <= y < h - FRAME) for x in range(w)] for y in range(h)]
|
|
lo, hi = FRAME, w - FRAME - 1
|
|
todo = [(x, y) for x in range(lo, hi + 1) for y in (lo, hi)] + [(x, y) for y in range(lo, hi + 1) for x in (lo, hi)]
|
|
while todo: # flood fill the dark backdrop inwards from just inside the frame
|
|
x, y = todo.pop()
|
|
if back[y][x] or bright[y][x] > DARK:
|
|
continue
|
|
back[y][x] = True
|
|
for nx, ny in ((x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)):
|
|
if 0 <= nx < w and 0 <= ny < h and not back[ny][nx]:
|
|
todo.append((nx, ny))
|
|
out = []
|
|
for y in range(h):
|
|
row = []
|
|
for x in range(w):
|
|
r, g, b, a = px[y][x]
|
|
if back[y][x] or not a:
|
|
row.append((0, 0, 0, 0))
|
|
continue
|
|
near = any(0 <= x + dx < w and 0 <= y + dy < h and back[y + dy][x + dx]
|
|
for dx in (-1, 0, 1) for dy in (-1, 0, 1))
|
|
if near and bright[y][x] < SOFT:
|
|
a = int(255 * (bright[y][x] - DARK) / float(SOFT - DARK))
|
|
a = max(60, min(255, a))
|
|
row.append((r, g, b, a))
|
|
out.append(row)
|
|
return out
|
|
|
|
|
|
def write_tga(path, px):
|
|
h, w = len(px), len(px[0])
|
|
head = struct.pack("<BBBHHBHHHHBB", 0, 0, 2, 0, 0, 0, 0, 0, w, h, 32, 8) # bottom-left origin
|
|
body = bytearray()
|
|
for row in reversed(px):
|
|
for r, g, b, a in row:
|
|
body += bytes((b, g, r, a))
|
|
open(path, "wb").write(head + bytes(body))
|
|
|
|
|
|
def write_png(path, rows_of_icons, backgrounds, scale=2):
|
|
# preview: each icon over each background colour, shown at 2x
|
|
size = 64 * scale
|
|
W = len(rows_of_icons) * (size + 8)
|
|
H = len(backgrounds) * (size + 8)
|
|
img = [[(40, 40, 40)] * W for _ in range(H)]
|
|
for j, bg in enumerate(backgrounds):
|
|
for i, px in enumerate(rows_of_icons):
|
|
for y in range(size):
|
|
for x in range(size):
|
|
r, g, b, a = px[y // scale][x // scale]
|
|
k = a / 255.0
|
|
img[j * (size + 8) + y][i * (size + 8) + x] = tuple(int(c * k + bc * (1 - k)) for c, bc in zip((r, g, b), bg))
|
|
raw = b"".join(b"\0" + bytes(v for p in row for v in p) for row in img)
|
|
|
|
def chunk(t, d):
|
|
return struct.pack(">I", len(d)) + t + d + struct.pack(">I", zlib.crc32(t + d) & 0xffffffff)
|
|
open(path, "wb").write(b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", W, H, 8, 2, 0, 0, 0))
|
|
+ chunk(b"IDAT", zlib.compress(raw)) + chunk(b"IEND", b""))
|
|
|
|
|
|
os.makedirs(out_dir, exist_ok=True)
|
|
done = []
|
|
for key, icon in ICONS.items():
|
|
px = cut(load_icon(icon))
|
|
write_tga(os.path.join(out_dir, key + ".tga"), px)
|
|
clear = sum(1 for row in px for p in row if p[3] == 0)
|
|
print("%-9s from %-16s %4.1f%% cleared" % (key, icon, 100.0 * clear / (64 * 64)))
|
|
done.append(px)
|
|
if preview:
|
|
# parchment map, dark map, grass
|
|
write_png(preview, done, [(214, 190, 140), (70, 60, 45), (60, 110, 50)])
|