"""Build the authentic Japanese-styled English title screen (Screen 0). Matches the original 1991 I'MAX Famicom title screen layout, palettes, clouds, and animation 1-for-1: - Left badge (rows 4-5, cols 2-9, pal 2): bold orange-red "FAMICOM(R)" with crisp 1px white outline (matching the original ファミコン® badge). - Main logo (rows 2-6, cols 11-29, pal 0): large calligraphic "GO BASICS" in bright green -> horizontal green/black scanline stripe -> jet black with crisp 1px white outline (matching 囲碁入門). - Stamp (CHR bank 7 sprites 0-13, original $E57E OAM coords, pal 3): crisp 56x16 white stamp "NIHON KIIN / APPROVED" (matching 日本棋院推薦). - Menu items (rows 20-27, pal 3): bold pixel-serif "GO LESSONS", "PLAY GO", and "RANK TEST" in the original white -> cream-gold -> coral-orange sunset gradient, aligned beside the white Go-stone cursor. - Strictly preserves all 53 runtime animation tiles (rows 11-17), the Mount Fuji frame/sprites, all white clouds, and "(c)1991 I'MAX". """ from PIL import Image, ImageDraw, ImageFont import make_book # Every CHR bank 0 tile ever written by the title sunrise/light-ray animation # across frames 31..700 (verified via nametable trace on the JP ROM). ANIM_TILES = { 0x1C, 0x6D, 0x6E, 0x6F, 0x7C, 0x7D, 0x7F, 0x8D, 0x9D, 0xAD, 0xAE, 0xBD, 0xBE, 0xBF, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFE, } FONT_LOGO = '/usr/share/fonts/noto/NotoSerif-BlackItalic.ttf' # Crisp hand-crafted 4x5 uppercase pixel font for the 56x16 white stamp. FONT_4X5 = { 'A': [".##.", "#..#", "####", "#..#", "#..#"], 'B': ["###.", "#..#", "###.", "#..#", "###."], 'C': [".###", "#...", "#...", "#...", ".###"], 'D': ["###.", "#..#", "#..#", "#..#", "###."], 'E': ["####", "#...", "###.", "#...", "####"], 'G': [".###", "#...", "#.##", "#..#", ".###"], 'H': ["#..#", "#..#", "####", "#..#", "#..#"], 'I': ["###", ".#.", ".#.", ".#.", "###"], 'K': ["#..#", "#.#.", "##..", "#.#.", "#..#"], 'N': ["#..#", "##.#", "#.##", "#..#", "#..#"], 'O': [".##.", "#..#", "#..#", "#..#", ".##."], 'P': ["###.", "#..#", "###.", "#...", "#..."], 'R': ["###.", "#..#", "###.", "#.#.", "#..#"], 'S': [".###", "#...", ".##.", "...#", "###."], 'T': ["###", ".#.", ".#.", ".#.", ".#."], 'V': ["#..#", "#..#", "#..#", ".##.", ".##."], 'Y': ["#.#", "#.#", ".#.", ".#.", ".#."], ' ': ["..", "..", "..", "..", ".."], } # Hand-crafted 7x11 bold retro pixel glyphs for "FAMICOM" badge (italicized # 1px to the right on the top half to evoke the classic Famicom wordmark). FAMICOM_GLYPHS = { 'F': [ "######", "######", "##....", "##....", "#####.", "#####.", "##....", "##....", "##....", "##....", "##....", ], 'A': [ "..###..", ".#####.", "##...##", "##...##", "##...##", "#######", "#######", "##...##", "##...##", "##...##", "##...##", ], 'M': [ "##...##", "###.###", "#######", "##.#.##", "##.#.##", "##...##", "##...##", "##...##", "##...##", "##...##", "##...##", ], 'I': [ "####", "####", ".##.", ".##.", ".##.", ".##.", ".##.", ".##.", ".##.", "####", "####", ], 'C': [ ".#####.", "######.", "##...##", "##.....", "##.....", "##.....", "##.....", "##.....", "##...##", "######.", ".#####.", ], 'O': [ ".#####.", "#######", "##...##", "##...##", "##...##", "##...##", "##...##", "##...##", "##...##", "#######", ".#####.", ], } # Crisp hand-crafted 7x13 bold pixel-serif letters for the sunset title menu # (GO LESSONS / PLAY GO / RANK TEST). 2px vertical stems and clean serifs # keep every letter razor-sharp across the white -> cream -> coral gradient. MENU_SERIF = { 'A': [ "..###..", "..###..", ".##.##.", ".##.##.", ".##.##.", "##...##", "##...##", "#######", "#######", "##...##", "##...##", "##...##", "###.###", ], 'E': [ "#######", "#######", ".##...#", ".##....", ".##..#.", ".#####.", ".#####.", ".##..#.", ".##....", ".##....", ".##...#", "#######", "#######", ], 'G': [ ".#####.", "#######", "##...##", "##.....", "##.....", "##.....", "##..###", "##..###", "##...##", "##...##", "##...##", "#######", ".#####.", ], 'K': [ "###..##", ".##.##.", ".##.##.", ".####..", ".###...", ".###...", ".####..", ".##.##.", ".##.##.", ".##..##", ".##..##", ".##..##", "###..##", ], 'L': [ "####...", ".##....", ".##....", ".##....", ".##....", ".##....", ".##....", ".##....", ".##....", ".##...#", ".##...#", "#######", "#######", ], 'N': [ "###..##", "###..##", "####.##", "####.##", "##.####", "##.####", "##..###", "##..###", "##...##", "##...##", "##...##", "##...##", "###..##", ], 'O': [ ".#####.", "#######", "##...##", "##...##", "##...##", "##...##", "##...##", "##...##", "##...##", "##...##", "##...##", "#######", ".#####.", ], 'P': [ "######.", "#######", ".##..##", ".##..##", ".##..##", ".##..##", ".######", ".#####.", ".##....", ".##....", ".##....", ".##....", "####...", ], 'R': [ "######.", "#######", ".##..##", ".##..##", ".##..##", ".##..##", ".######", ".#####.", ".##.##.", ".##.##.", ".##..##", ".##..##", "###..##", ], 'S': [ ".#####.", "#######", "##...##", "##.....", "###....", ".#####.", "..#####", "....###", ".....##", "##...##", "##...##", "#######", ".#####.", ], 'T': [ "#######", "#######", "#..#..#", "..###..", "..###..", "..###..", "..###..", "..###..", "..###..", "..###..", "..###..", "..###..", ".#####.", ], 'Y': [ "###.###", "##...##", "##...##", ".##.##.", ".##.##.", "..###..", "..###..", "..###..", "..###..", "..###..", "..###..", "..###..", ".#####.", ], } def _draw_famicom_badge(): """64x16 orange-red FAMICOM(R) badge with crisp 1px white outline (pal 2).""" W, H = 64, 16 mask = [[False] * W for _ in range(H)] cx = 2 for ch in 'FAMICOM': g = FAMICOM_GLYPHS[ch] gw = len(g[0]) for y, row in enumerate(g): # Subtle 1px forward slant on upper half for retro Famicom energy. slant = 1 if y < 5 else 0 for x, pix in enumerate(row): if pix == '#': xx = cx + x + slant yy = 3 + y if 0 <= xx < W and 0 <= yy < H: mask[yy][xx] = True cx += gw + 1 out = [[0] * W for _ in range(H)] for y in range(H): for x in range(W): if mask[y][x]: out[y][x] = 2 # vibrant orange-red else: for dy in (-1, 0, 1): for dx in (-1, 0, 1): yy, xx = y + dy, x + dx if 0 <= yy < H and 0 <= xx < W and mask[yy][xx]: out[y][x] = 1 # crisp 1px white outline break if out[y][x]: break # Crisp registered-trademark (R) circle on the right (white, color 1) reg = [ "..####..", ".#....#.", "#.#..#.#", "#.##.#.#", "#.#.#..#", "#.#..#.#", ".#....#.", "..####..", ] for ry, row in enumerate(reg): for rx, ch in enumerate(row): if ch == '#': out[3 + ry][56 + rx] = 1 return out def _draw_main_logo(text='GO BASICS'): """152x40 bold calligraphic logo in exact JP 囲碁入門 palette & striping.""" W, H = 152, 40 f = ImageFont.truetype(FONT_LOGO, 136) dr0 = ImageDraw.Draw(Image.new('L', (10, 10))) bb = dr0.textbbox((0, 0), text, font=f) tw, th = bb[2] - bb[0], bb[3] - bb[1] tmp = Image.new('L', (tw + 32, th + 32), 0) ImageDraw.Draw(tmp).text((16 - bb[0], 16 - bb[1]), text, font=f, fill=255) target_w, target_h = W - 6, H - 6 tmp = tmp.resize((target_w, target_h), Image.Resampling.LANCZOS) px = tmp.load() mask = [[False] * W for _ in range(H)] for y in range(target_h): for x in range(target_w): if px[x, y] >= 118: mask[y + 2][x + 3] = True # Remove isolated 1px speckles so serifs stay crisp. clean = [row[:] for row in mask] for y in range(1, H - 1): for x in range(1, W - 1): if mask[y][x]: nb = (mask[y - 1][x] + mask[y + 1][x] + mask[y][x - 1] + mask[y][x + 1]) if nb == 0: clean[y][x] = False mask = clean # Exact 囲碁入門 horizontal stripe gradient: # bright green top -> alternating green/black stripes -> jet black bottom stripe_map = {17: 3, 18: 2, 19: 3, 20: 3, 21: 2, 22: 2, 23: 3} out = [[0] * W for _ in range(H)] for y in range(H): for x in range(W): if mask[y][x]: if y < 17: out[y][x] = 2 # bright green elif y in stripe_map: out[y][x] = stripe_map[y] else: out[y][x] = 3 # jet black # Crisp 1px white outline all around the logo. for y in range(H): for x in range(W): if mask[y][x]: continue for dy in (-1, 0, 1): for dx in (-1, 0, 1): yy, xx = y + dy, x + dx if 0 <= yy < H and 0 <= xx < W and mask[yy][xx]: out[y][x] = 1 # white outline break if out[y][x]: break return out def _draw_menu_letter(ch): """8x16 bold pixel-serif letter in JP sunset gradient (pal 3).""" g = MENU_SERIF[ch] # Exact JP vertical sunset bands (matching jp_menu_2x.png): # top white(1) -> cream-gold(2) -> alternating coral/cream -> coral(3) band = [1, 1, 1, 1, 1, 2, 2, 2, 3, 2, 3, 3, 3, 3, 3, 3] out = [[0] * 8 for _ in range(16)] for y, row in enumerate(g): for x, pix in enumerate(row): if pix == '#': out[2 + y][1 + x] = band[2 + y] return out def _draw_subtitle_stamp(chr_): """Draw crisp 56x16 white stamp 'NIHON KIIN / APPROVED' into CHR bank 7. Uses the exact 14 sprite tiles of the original 日本棋院推薦 stamp at their original $E57E OAM positions (x=152..208, y=55..71, sprite pal 3: color 1 = white box, color 3 = crisp black ink). """ W, H = 56, 16 # Solid white stamp (color 1) with clean 1px transparent padding on top/bot # would shrink height; original JP stamp fills the full 56x16 white box # (1px white border inside the 56x16 rectangle, black text in color 3). canvas = [[1] * W for _ in range(H)] def put_4x5(text, y0): # Measure width (4px per char except I=3, space=2; 1px spacing) total_w = sum(len(FONT_4X5[c][0]) for c in text) + len(text) - 1 x0 = (W - total_w) // 2 cx = x0 for ch in text: g = FONT_4X5[ch] for gy, row in enumerate(g): for gx, p in enumerate(row): if p == '#': canvas[y0 + gy][cx + gx] = 3 # black ink cx += len(g[0]) + 1 put_4x5('NIHON KIIN', 2) put_4x5('APPROVED', 9) top_tiles = [0x95, 0x96, 0x97, 0x98, 0x74, 0x75, 0x76] bot_tiles = [0xA5, 0xA6, 0xA7, 0xA8, 0x84, 0x85, 0x86] for row_idx, tile_row in enumerate((top_tiles, bot_tiles)): for col_idx, t in enumerate(tile_row): base = 7 * 4096 + t * 16 for y in range(8): lo = hi = 0 for x in range(8): v = canvas[row_idx * 8 + y][col_idx * 8 + x] if v & 1: lo |= 1 << (7 - x) if v & 2: hi |= 1 << (7 - x) chr_[base + y] = lo chr_[base + 8 + y] = hi def build(b3, chr_): """Rewrite CHR bank 0, CHR bank 7 stamp, and return compressed Screen 0.""" s0 = bytearray(make_book.decompress(b3, 0)[0]) # Reserve every tile used by the mountain frame, runtime animation, clouds, # and "(c)1991 I'MAX" so nothing animated or decorative is ever touched. keep = set(ANIM_TILES) | {0x00} for r in range(30): for c in range(32): in_fam = (4 <= r <= 5 and 2 <= c <= 9) in_logo = (2 <= r <= 8 and 11 <= c <= 29) in_menu = ((20 <= r <= 21 and 8 <= c <= 18) or (23 <= r <= 24 and 6 <= c <= 17) or (26 <= r <= 27 and 4 <= c <= 16)) if not (in_fam or in_logo or in_menu): keep.add(s0[r * 32 + c]) else: s0[r * 32 + c] = 0 free = sorted(set(range(256)) - keep) pi = 0 tile_cache = {} def get_tile(pix): nonlocal pi if all(v == 0 for row in pix for v in row): return 0 key = tuple(tuple(row) for row in pix) if key in tile_cache: return tile_cache[key] assert pi < len(free), f'ran out of free CHR0 title tiles ({pi}/{len(free)})' code = free[pi] pi += 1 tile_cache[key] = code for y in range(8): lo = hi = 0 for x in range(8): v = pix[y][x] if v & 1: lo |= 1 << (7 - x) if v & 2: hi |= 1 << (7 - x) chr_[code * 16 + y] = lo chr_[code * 16 + 8 + y] = hi return code def blit(canvas, r0, c0): H, W = len(canvas), len(canvas[0]) for ty in range(H // 8): for tx in range(W // 8): tile = [[canvas[ty * 8 + y][tx * 8 + x] for x in range(8)] for y in range(8)] s0[(r0 + ty) * 32 + (c0 + tx)] = get_tile(tile) def set_attr(tr, tc, pal): idx = 960 + (tr // 4) * 8 + (tc // 4) sh = ((tr % 4) // 2) * 4 + ((tc % 4) // 2) * 2 s0[idx] = (s0[idx] & ~(3 << sh)) | ((pal & 3) << sh) # 1. Left badge: FAMICOM(R) in rows 4-5, cols 2-9 (Palette 2). blit(_draw_famicom_badge(), 4, 2) for r in (4, 5): for c in range(2, 10): set_attr(r, c, 2) # 2. Main calligraphic logo: GO BASICS in rows 2-6, cols 11-29 (Palette 0). blit(_draw_main_logo('GO BASICS'), 2, 11) for r in range(2, 7): for c in range(11, 30): set_attr(r, c, 0) # 3. Sunset menu items aligned beside the white Go-stone cursor (Palette 3). menu_items = [ (20, 8, 'GO LESSONS'), (23, 6, 'PLAY GO'), (26, 4, 'RANK TEST'), ] letter_cache = {} for r0, c0, word in menu_items: for i, ch in enumerate(word): if ch == ' ': continue if ch not in letter_cache: g = _draw_menu_letter(ch) letter_cache[ch] = (get_tile(g[:8]), get_tile(g[8:])) t0, t1 = letter_cache[ch] s0[r0 * 32 + c0 + i] = t0 s0[(r0 + 1) * 32 + c0 + i] = t1 for r in (r0, r0 + 1): for c in range(c0, c0 + len(word)): set_attr(r, c, 3) # 4. Subtitle stamp in CHR bank 7 (original $E57E OAM positions untouched). _draw_subtitle_stamp(chr_) comp = make_book.compress(s0) print(f'title screen: {pi}/{len(free)} CHR0 tiles, compressed {len(comp)}/{0x020A} bytes') return comp