#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Breaknes project icon generator. The icon is the DALL-E artwork Breaknes/Breaknes/brkns_dalle.png. This script square-crops it (center), resizes to a 256x256 master with Lanczos and produces: breaknes.png - 256x256 RGBA master breaknes.ico - Windows multi-size icon (16/24/32/48/64/128/256) breaknes_icon.h - C++ header for the SDL2 port with the 64x64 RGBA pixels embedded, so the window icon needs no image loader (works on Windows and Linux, SDL 2.x). Usage: python3 gen_breaknes_icon.py [source.png] """ import os import sys from PIL import Image HERE = os.path.dirname(os.path.abspath(__file__)) DEFAULT_SRC = os.path.normpath( os.path.join(HERE, "..", "Breaknes", "Breaknes", "brkns_dalle.png")) PNG_PATH = os.path.join(HERE, "breaknes.png") ICO_PATH = os.path.join(HERE, "breaknes.ico") HDR_PATH = os.path.join(HERE, "..", "Breaknes", "BreaknesSDL", "breaknes_icon.h") ICO_SIZES = [16, 24, 32, 48, 64, 128, 256] MASTER = 256 def load_master(src_path): """Load the source, center-crop to a square, resize to the 256x256 master.""" img = Image.open(src_path).convert("RGBA") w, h = img.size side = min(w, h) left = (w - side) // 2 top = (h - side) // 2 img = img.crop((left, top, left + side, top + side)) img = img.resize((MASTER, MASTER), Image.LANCZOS) return img def main(): src = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_SRC if not os.path.exists(src): print("Source image not found:", src) return 1 master = load_master(src) master.save(PNG_PATH) print("saved", PNG_PATH, master.size, "from", os.path.basename(src)) # Multi-size Windows icon (Lanczos downscale per frame). frames = {s: master.resize((s, s), Image.LANCZOS) for s in ICO_SIZES} frames[MASTER].save(ICO_PATH, sizes=[(s, s) for s in ICO_SIZES], append_images=[frames[s] for s in ICO_SIZES if s != MASTER]) print("saved", ICO_PATH, ICO_SIZES) # SDL2 port: 64x64 RGBA pixels embedded in a C++ header. icon64 = master.resize((64, 64), Image.LANCZOS) rgba = list(icon64.convert("RGBA").tobytes()) w = h = 64 assert len(rgba) == w * h * 4 lines = [] for i in range(0, len(rgba), 12): chunk = rgba[i:i + 12] lines.append(" " + ", ".join("0x%02X" % b for b in chunk) + ",") body = "\n".join(lines) header = """// Breaknes project icon for the SDL2 port. // // Generated by Artwork/gen_breaknes_icon.py from Breaknes/Breaknes/brkns_dalle.png // (DALL-E artwork) - do not edit by hand. // 64x64 RGBA pixels (8-bit per channel, row-major). The window icon is set // with SDL_SetWindowIcon(), so no image loader (SDL_image) is required and // the icon works the same way on Windows and Linux. #pragma once #ifdef _WIN32 #include "SDL.h" #else #include #endif /// /// The Breaknes icon, 64x64, 32 bits per pixel, RGBA byte order. /// static const int kBreaknesIconWidth = %(w)d; static const int kBreaknesIconHeight = %(h)d; static const unsigned char kBreaknesIconRGBA[%(w)d * %(h)d * 4] = { %(body)s }; /// /// Create a 64x64 icon surface from the embedded pixels. /// Caller must SDL_FreeSurface() the result. /// static inline SDL_Surface* BreaknesCreateIconSurface() { return SDL_CreateRGBSurfaceFrom( (void*)kBreaknesIconRGBA, kBreaknesIconWidth, kBreaknesIconHeight, 32, kBreaknesIconWidth * 4, #if SDL_BYTEORDER == SDL_BIG_ENDIAN 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF); #else 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000); #endif } """ % {"w": w, "h": h, "body": body} with open(HDR_PATH, "w", encoding="utf-8") as f: f.write(header) print("saved", HDR_PATH) return 0 if __name__ == "__main__": sys.exit(main())