Last active
March 12, 2024 01:05
-
-
Save 0xD34D/25cbddfd70ae9cf5ab27abdde544fa87 to your computer and use it in GitHub Desktop.
Python script to convert TJC TFT RLE encoded images to raw RGB565
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
import argparse | |
import os | |
from pathlib import Path | |
import struct | |
if __name__ == "__main__": | |
desc = """TFT RLE to RGB5656 image converter | |
Developped by Clark Scheff, licensed under GPLv3""" | |
parser = argparse.ArgumentParser(description=desc) | |
parser.add_argument("-i", "--input", metavar="IN_FILE", type=str, required=True, | |
help="Path to the RLE image file") | |
parser.add_argument("-o", "--output", metavar="OUT_FILE", type=str, required=True, | |
help="Path to the RGB565 output image file") | |
args = parser.parse_args() | |
inPath = Path(args.input) | |
if not inPath.exists(): | |
parser.error("Invalid source file!") | |
outPath = Path(args.output) | |
with open(inPath, 'rb') as in_file: | |
with open(outPath, 'wb') as out_file: | |
header = in_file.read(20) | |
compressed = header[0] != 0 | |
if not compressed: | |
data = in_file.read() | |
out_file.write(data) | |
else: | |
data_end = struct.unpack("I", header[4:8])[0] | |
while in_file.tell() < data_end: | |
counts = struct.unpack("I", in_file.read(4))[0] | |
for i in range(8): | |
shift = 4 * i | |
count = (counts >> shift) & 0x0f | |
# if the first nibble is 0x0f we just write | |
# the colors as is to the output | |
if i == 0 and count == 0x0f: | |
for x in range(4): | |
colors = in_file.read(4) | |
out_file.write(colors) | |
break | |
color = bytearray(in_file.read(2)) | |
for x in range(count): | |
out_file.write(color) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment