"""The start-up screen's logo (gpu/vk/vk_imgui.cpp, drawn by vk_menu.cpp) from assets/hookshot.png: a C header with its pixels as straight RGBA bytes, written by nx2/build.py into the build's own folder (no second copy of the picture in the repository). python nx2/logo_rgba.py [picture.png] [out.h] Standard library only: the PNG is decoded here (8-bit RGBA or RGB, not interlaced: what the asset is). """ import os import struct import sys import zlib ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..") SOURCE = os.path.join(ROOT, "assets", "triforce.png") # (Triforce: its crest; upstream: assets/hookshot.png) def decode(data): """(width, height, RGBA bytes) of an 8-bit, non-interlaced RGBA or RGB PNG""" if data[:8] != b"\x89PNG\r\n\x1a\n": raise ValueError("not a PNG") at, idat, head = 8, b"", None while at < len(data): n, kind = struct.unpack(">I4s", data[at:at + 8]) body = data[at + 8:at + 8 + n] if kind == b"IHDR": head = struct.unpack(">IIBBBBB", body) elif kind == b"IDAT": idat += body elif kind == b"IEND": break at += 12 + n w, h, depth, colour, _, _, interlace = head if depth != 8 or colour not in (2, 6) or interlace: raise ValueError("only 8-bit RGB/RGBA, not interlaced: %r" % (head,)) bpp = 4 if colour == 6 else 3 raw, stride = zlib.decompress(idat), w * bpp out, prev = bytearray(), bytearray(stride) for y in range(h): f = raw[y * (stride + 1)] line = bytearray(raw[y * (stride + 1) + 1:(y + 1) * (stride + 1)]) for x in range(stride): a = line[x - bpp] if x >= bpp else 0 b = prev[x] c = prev[x - bpp] if x >= bpp else 0 if f == 1: line[x] = (line[x] + a) & 255 elif f == 2: line[x] = (line[x] + b) & 255 elif f == 3: line[x] = (line[x] + ((a + b) >> 1)) & 255 elif f == 4: p = a + b - c pa, pb, pc = abs(p - a), abs(p - b), abs(p - c) line[x] = (line[x] + (a if pa <= pb and pa <= pc else b if pb <= pc else c)) & 255 prev = line if bpp == 4: out += line else: for x in range(w): out += line[x * 3:x * 3 + 3] + b"\xff" return w, h, bytes(out) def header(png_path=SOURCE): if not os.path.isfile(png_path): # (a source tree without the picture: the screen shows the wordmark alone) return "// generated by nx2/logo_rgba.py: no assets/hookshot.png\n#pragma once\n" w, h, px = decode(open(png_path, "rb").read()) lines = ["// generated by nx2/logo_rgba.py from assets/hookshot.png: do not edit", "#pragma once", "#define HOOKSHOT_LOGO 1", "static const unsigned hookshot_logo_w = %d, hookshot_logo_h = %d;" % (w, h), "static const unsigned char hookshot_logo_rgba[%d] = {" % len(px)] for i in range(0, len(px), 32): lines.append(",".join(str(b) for b in px[i:i + 32]) + ",") lines.append("};") return "\n".join(lines) + "\n" def write(out_path, png_path=SOURCE): """Writes the header when it differs (an unchanged one keeps its timestamp); returns its text""" text = header(png_path) try: with open(out_path, encoding="utf-8") as f: if f.read() == text: return text except OSError: pass os.makedirs(os.path.dirname(out_path) or ".", exist_ok=True) with open(out_path, "w", encoding="utf-8", newline="\n") as f: f.write(text) return text if __name__ == "__main__": src = sys.argv[1] if len(sys.argv) > 1 else SOURCE out = sys.argv[2] if len(sys.argv) > 2 else "hookshot_logo.h" write(out, src) print("logo_rgba: %s" % out)