Created
June 13, 2019 02:08
-
-
Save LiquidFenrir/086830602b9228124d1212869ac485be to your computer and use it in GitHub Desktop.
This file is a python script (that requires Pillow/PIL) to decode a ctrulib font file to an image, or encode a png to a ctrulib font file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This file is a python script (that requires Pillow/PIL) to decode a ctrulib font file to an image, or encode a png to a ctrulib font file | |
ctrulib: https://github.com/smealum/ctrulib | |
Copyright (C) 2019 LiquidFenrir | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
Additional Terms 7.b and 7.c of GPLv3 apply to this file: | |
* Requiring preservation of specified reasonable legal notices or | |
author attributions in that material or in the Appropriate Legal | |
Notices displayed by works containing it. | |
* Prohibiting misrepresentation of the origin of that material, | |
or requiring that modified versions of such material be marked in | |
reasonable ways as different from the original version. | |
""" | |
from PIL import Image | |
import struct | |
import argparse | |
def decode_font_bin(inpath, outpath): | |
with open(inpath, "rb") as f: | |
data = f.read() | |
img = Image.new("RGBA", (16*8, 16*8)) | |
x = 16*8 - 8 | |
y = 0 | |
for i in range(256): | |
rows = struct.unpack_from("8B", data, i*8) | |
for py, row in enumerate(rows, y): | |
for px in range(8): | |
pixel = bool((row >> px) & 1) | |
img.putpixel((py, px + x), 0xFFFFFFFF if pixel else 0) | |
x -= 8 | |
if x == -8: | |
x = 16*8 - 8 | |
y += 8 | |
img.rotate(-90).save(outpath) | |
def encode_font_bin(inpath, outpath): | |
data = [0] * (2048) | |
pixels = list(Image.open(inpath).rotate(90).convert("RGBA").getdata(3)) | |
data = [0] * 2048 | |
x = 0 | |
y = 16*8 - 1 | |
for character in range(256): | |
for py in range(8): | |
num = 0 | |
for px in range(8): | |
if pixels[(y - px)*128 + py + x]: | |
num |= 1 << (8 - px - 1) | |
data[character * 8 + py] = num | |
y -= 8 | |
if y < 0: | |
x += 8 | |
y = 16*8 - 1 | |
with open(outpath, "wb") as f: | |
f.write(bytearray(data)) | |
if __name__ == "__main__": | |
actions_dict = { | |
"decode": decode_font_bin, | |
"encode": encode_font_bin, | |
} | |
parser = argparse.ArgumentParser(description="") | |
parser.add_argument("to_do", choices=actions_dict.keys(), help="Action to perform") | |
parser.add_argument("-i", "--infile", "--in", help="Input file (either a .bin or an image file)") | |
parser.add_argument("-o", "--outfile", "--out", help="Output file") | |
args = parser.parse_args() | |
actions_dict[args.to_do](args.infile, args.outfile) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment