Skip to content

Instantly share code, notes, and snippets.

@infval
Created February 3, 2019 09:25
Show Gist options
  • Save infval/6ddd65727ec0bf7c64da1a035d889934 to your computer and use it in GitHub Desktop.
Save infval/6ddd65727ec0bf7c64da1a035d889934 to your computer and use it in GitHub Desktop.
Font decoder/encoder - Splinter Cell Chaos Theory (N-Gage)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import math
from PIL import Image, ImageDraw
def decode(filename):
print(filename)
with open(filename, "rb") as f:
b = f.read()
w = 0
h = 0
# Определяем ширину и высоту
for i in range(len(b)//4):
if b[i*4:i*4+4] == b"\x00\xFF\x00\x00":
if w == 0:
w = i
print("width", w)
else:
h = i//w + 1
print("height", h)
im = Image.new("RGBA", (w, h))
pixels = im.load()
x = 0
y = 0
for i in range(len(b)//4):
int32_str = b[i*4:i*4+4]
c0, c1, c2, c3 = int32_str
if int32_str == b"\x00\xFF\x00\x00": # Границы вертикальные (слева) 00FF0000
pixels[x, y] = (0, 0, 255, 255)
elif int32_str[:3] == b"\xFF\x00\xFF": # Границы горизонтальные (сверху) FF00FF00 или FF00FFFF, или FF00FFxx
pixels[x, y] = (0, 255, 0, 255)
elif int32_str == b"\xFF\xFF\x00\x00": # Совмещённая граница горизонтальная (сверху) FFFF0000
pixels[x, y] = (255, 0, 0, 255)
# Лишние пиксели? Залезают на первую строку
elif (int32_str != b"\x00\x00\x00\x00" and int32_str != b"\xFF\xFF\xFF\x00") and (y == 0 or x == 0):
print("x:{},y:{} = ({:x},{:x},{:x},{:x}) ".format(x, y, c0, c1, c2, c3))
pixels[x, y] = (c0, c1, c2, c3)
else:
pixels[x, y] = (c0, c1, c2, c3)
x += 1
if x == w:
x = 0
y += 1
im.save(filename + ".png", "PNG")
print()
def encode(filename):
print(filename)
im = Image.open(filename)
pixels = im.load()
w = im.width
h = im.height
b = []
# Первая строка с границами символов
for i in range(w):
c = pixels[i, 0]
if c == (0, 255, 0, 255): # Границы горизонтальные (сверху) FF00FF00 или FF00FFFF, или FF00FFxx
c = tuple(b"\xFF\x00\xFF\x00")
elif c == (255, 0, 0, 255): # Совмещённая граница горизонтальная (сверху) FFFF0000
c = tuple(b"\xFF\xFF\x00\x00")
b.extend(c)
for y in range(1, h):
for x in range(w):
c = pixels[x, y]
if x == 0 and c == (0, 0, 255, 255): # Границы вертикальные (слева) 00FF0000
c = tuple(b"\x00\xFF\x00\x00")
b.extend(c)
with open(filename + ".raw", "wb") as f:
f.write(bytes(b))
# decode("alarm.raw")
# decode("cardinal.raw")
# decode("conversation.raw")
# decode("hud.raw")
# decode("menu.raw")
# decode("test.raw")
encode("alarm.raw.png")
encode("cardinal.raw.png")
encode("conversation.raw.png")
encode("hud.raw.png")
encode("menu.raw.png")
encode("test.raw.png")
input("Press any key...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment