mirror of
https://github.com/portapack-mayhem/mayhem-firmware.git
synced 2025-06-16 18:48:28 +00:00
128 lines
3.8 KiB
Python
128 lines
3.8 KiB
Python
![]() |
#!/usr/bin/env python3
|
||
|
|
||
|
# Copyright (C) 2016 Furrtek
|
||
|
#
|
||
|
# 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 2, 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; see the file COPYING. If not, write to
|
||
|
# the Free Software Foundation, Inc., 51 Franklin Street,
|
||
|
# Boston, MA 02110-1301, USA.
|
||
|
#
|
||
|
|
||
|
import sys
|
||
|
import struct
|
||
|
from PIL import Image
|
||
|
from os import listdir
|
||
|
from os import path
|
||
|
from os import makedirs
|
||
|
|
||
|
usage_message = f"""
|
||
|
1BPP PortaPack bitmap.hpp generator
|
||
|
|
||
|
Usage: {sys.argv[0]} <directory>
|
||
|
"""
|
||
|
|
||
|
if len(sys.argv) < 2:
|
||
|
print(usage_message)
|
||
|
sys.exit(-1)
|
||
|
|
||
|
graphics_path = path.join(sys.argv[1], '')
|
||
|
hex_output_dir = path.join(graphics_path, 'hex_files')
|
||
|
|
||
|
# Create hex output directory if it doesn't exist
|
||
|
if not path.exists(hex_output_dir):
|
||
|
makedirs(hex_output_dir)
|
||
|
|
||
|
def convert_png(file):
|
||
|
c = 0
|
||
|
data = 0
|
||
|
|
||
|
rgb_im = Image.open(file).convert('RGBA')
|
||
|
|
||
|
if rgb_im.size[0] % 8 or rgb_im.size[1] % 8:
|
||
|
print((file + ": Size is not a multiple of 8. Image is not included in bitmap.hpp."))
|
||
|
sys.exit(-1)
|
||
|
|
||
|
name = path.basename(file).split(".")[0].lower();
|
||
|
|
||
|
# Prepare data for both hpp and hex file
|
||
|
hpp_lines = []
|
||
|
hex_data = bytearray()
|
||
|
|
||
|
for i in range(rgb_im.size[1]):
|
||
|
for j in range(rgb_im.size[0]):
|
||
|
r, g, b, a = rgb_im.getpixel((j, i))
|
||
|
|
||
|
data >>= 1
|
||
|
|
||
|
if r > 127 and g > 127 and b > 127 and a > 127:
|
||
|
data += 128
|
||
|
|
||
|
if j % 8 == 7:
|
||
|
hpp_lines.append(" 0x%0.2X,\n" % data)
|
||
|
hex_data.append(data)
|
||
|
data = 0
|
||
|
|
||
|
# Write to hpp file
|
||
|
f.write("static constexpr uint8_t bitmap_" + name + "_data[] = {\n")
|
||
|
f.writelines(hpp_lines)
|
||
|
f.write("};\n")
|
||
|
f.write("static constexpr Bitmap bitmap_" + name + " {\n")
|
||
|
f.write(" {" + str(rgb_im.size[0]) + ", " + str(rgb_im.size[1]) + "},\n bitmap_" + name + "_data};\n\n")
|
||
|
|
||
|
# Write to hex file
|
||
|
hex_filename = path.join(hex_output_dir, name + ".hex")
|
||
|
with open(hex_filename, 'wb') as hex_file:
|
||
|
hex_file.write(hex_data)
|
||
|
|
||
|
return
|
||
|
|
||
|
count = 0
|
||
|
|
||
|
f = open('bitmap.hpp', 'w')
|
||
|
f.write("/*\n"
|
||
|
" * Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.\n"
|
||
|
" * Copyright (C) 2016 Furrtek\n"
|
||
|
" *\n"
|
||
|
" * This file is part of PortaPack.\n"
|
||
|
" *\n"
|
||
|
" * This program is free software; you can redistribute it and/or modify\n"
|
||
|
" * it under the terms of the GNU General Public License as published by\n"
|
||
|
" * the Free Software Foundation; either version 2, or (at your option)\n"
|
||
|
" * any later version.\n"
|
||
|
" *\n"
|
||
|
" * This program is distributed in the hope that it will be useful,\n"
|
||
|
" * but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
|
||
|
" * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
|
||
|
" * GNU General Public License for more details.\n"
|
||
|
" *\n"
|
||
|
" * You should have received a copy of the GNU General Public License\n"
|
||
|
" * along with this program; see the file COPYING. If not, write to\n"
|
||
|
" * the Free Software Foundation, Inc., 51 Franklin Street,\n"
|
||
|
" * Boston, MA 02110-1301, USA.\n"
|
||
|
" */\n\n"
|
||
|
"// This file was generated by make_bitmap.py\n\n"
|
||
|
"#ifndef __BITMAP_HPP__\n"
|
||
|
"#define __BITMAP_HPP__\n\n"
|
||
|
"#include \"ui.hpp\"\n\n"
|
||
|
"namespace ui {\n\n")
|
||
|
|
||
|
for file in listdir(graphics_path):
|
||
|
if file.endswith(".png") and path.isfile(graphics_path + file):
|
||
|
convert_png(graphics_path + file)
|
||
|
count += 1
|
||
|
|
||
|
f.write("} /* namespace ui */\n\n")
|
||
|
f.write("#endif/*__BITMAP_HPP__*/\n")
|
||
|
|
||
|
print(("Converted " + str(count) + " files"))
|