Copy3DEngine/oconv/convert.py
2024-03-11 22:38:03 +01:00

72 lines
2 KiB
Python

import subprocess
import sys
import os
from PIL import Image
def rgb_to_rgb565(rgb_tuple):
r = rgb_tuple[0]
g = rgb_tuple[1]
b = rgb_tuple[2]
return (int(r/255*31)<<11)|(int(g/255*63)<<5) | int(b/255*31)
class image:
def __init__(self, path):
self.image = Image.open(path)
def getImage(self):
return self.image
def setImage(self,path):
self.image = Image.open(path)
def getpixels(self):
width, height = self.image.size
pixels = []
for y in range(height):
for x in range(width):
pixel = self.image.getpixel((x, y))
pixels.append(rgb_to_rgb565(pixel))
return pixels
class converter:
def __init__(self, file):
self.file = file
def getFile(self):
return self.file
def setFile(self, file):
self.file = file
def write_word(self, data):
asm = ""
asm += ".word " + hex(data)
return asm
def convert_image(self, symbol, section=".rodata"):
img = image(self.file)
asmc = f".section {section} \n"
asmc += f".global _{symbol}\n"
asmc += f"_{symbol}:\n"
width,height = img.getImage().size
asmc += self.write_word(height) + "\n"
asmc += self.write_word(width) + "\n"
pixels = img.getpixels()
for i in pixels:
asmc += self.write_word(i) + "\n"
f = open(symbol + ".S", "wb")
f.write(asmc.encode('utf-8'))
f.close()
dir = sys.argv[1]
ls = os.listdir(dir)
if not "oconv.txt" in ls:
print(" OConv ----- oconv.txt cannot be found")
exit()
print("\n")
conf = open(f"{dir}oconv.txt", 'r').read().replace(" ","")
files = conf.split("\n")
for file in files:
filename, metadatas = file.split(":")
type, name = metadatas.split(",")
print(f" OConv ----- Converting {filename}")
conv = converter(dir+filename)
if(type == "image"):
conv.convert_image(name)
print("\n")