mirror of
https://github.com/eried/portapack-mayhem.git
synced 2024-10-01 01:26:06 -04:00
add bitmap arr to file (or reverse) converter (#2248)
* add covert tool * fix for generator * fix for generator
This commit is contained in:
parent
bfa7f3ca6b
commit
582bb02a75
49
firmware/tools/bmp_to_hex_cpp_arr.py
Normal file
49
firmware/tools/bmp_to_hex_cpp_arr.py
Normal file
@ -0,0 +1,49 @@
|
||||
# copyleft zxkmm 2024
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def bmp_to_hex_cpp_arr(input_file, output_file):
|
||||
|
||||
with open(input_file, 'rb') as f:
|
||||
data = f.read()
|
||||
|
||||
name_without_extension = os.path.splitext(os.path.basename(input_file))[0]
|
||||
|
||||
with open(output_file, 'w') as f:
|
||||
|
||||
f.write(f"// converted by bmp_to_hex_cpp_arr.py at the firmware/tools dir\n")
|
||||
f.write(f"// fake transparent px should be the exact color rgb(255,0,255), then use true for the last arg "
|
||||
f"when drawing bmp with the draw func\n")
|
||||
f.write(f"\n")
|
||||
f.write(f"const unsigned char {name_without_extension}[] = {{\n")
|
||||
|
||||
# idk about the clang-format rule, but the splash arr is 12 item per line. but other bmp arr is 2 item per line
|
||||
for i in range(0, len(data), 12):
|
||||
line = data[i:i + 12]
|
||||
hex_values = [f"0x{byte:02x}" for byte in line]
|
||||
|
||||
f.write(f" {', '.join(hex_values)},\n")
|
||||
|
||||
# this is for remove last the extra comma
|
||||
f.seek(f.tell() - 2, os.SEEK_SET)
|
||||
f.truncate()
|
||||
|
||||
f.write("};\n")
|
||||
|
||||
|
||||
|
||||
f.write(f"unsigned int {name_without_extension}_len = {len(data)};\n")
|
||||
|
||||
print(f"done, outputted, pls clang-format yourself before commit (if necessary):{output_file}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) != 3:
|
||||
print("usage: python bmp_to_hex_cpp_arr.py INPUTFILE OUTPUTFILE")
|
||||
sys.exit(1)
|
||||
|
||||
input_file = sys.argv[1]
|
||||
output_file = sys.argv[2]
|
||||
|
||||
bmp_to_hex_cpp_arr(input_file, output_file)
|
51
firmware/tools/hex_cpp_arr_to_bmp.py
Normal file
51
firmware/tools/hex_cpp_arr_to_bmp.py
Normal file
@ -0,0 +1,51 @@
|
||||
# copyleft zxkmm 2024
|
||||
|
||||
import sys
|
||||
import re
|
||||
|
||||
|
||||
def hex_cpp_arr_to_bmp(input_file, output_file):
|
||||
with open(input_file, 'r') as f:
|
||||
content = f.read()
|
||||
|
||||
# in the mayhem code some of them not const
|
||||
array_name_const = re.search(r'const unsigned char (\w+)\[\]', content)
|
||||
array_name_no_const = re.search(r'unsigned char (\w+)\[]', content)
|
||||
if array_name_const or array_name_no_const:
|
||||
|
||||
if array_name_const:
|
||||
array_name = array_name_const.group(1)
|
||||
else:
|
||||
array_name = array_name_no_const.group(1)
|
||||
|
||||
print(f"Array name: {array_name}")
|
||||
|
||||
else:
|
||||
print("the file provided seems doesn't contains needed arr.")
|
||||
return
|
||||
|
||||
hex_data = re.findall(r'0x[0-9A-Fa-f]{2}', content)
|
||||
|
||||
if not hex_data:
|
||||
print("Error: No hex data found.")
|
||||
return
|
||||
|
||||
binary_data = bytes([int(x, 16) for x in hex_data])
|
||||
|
||||
with open(output_file, 'wb') as f:
|
||||
f.write(binary_data)
|
||||
|
||||
print(f"Done. Output file: {output_file}")
|
||||
print(f"Array name: {array_name}")
|
||||
print(f"Data length: {len(binary_data)} bytes")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) != 3:
|
||||
print("usage: python hex_cpp_arr_to_bmp.py INPUTFILE OUTPUTFILE")
|
||||
sys.exit(1)
|
||||
|
||||
input_file = sys.argv[1]
|
||||
output_file = sys.argv[2]
|
||||
|
||||
hex_cpp_arr_to_bmp(input_file, output_file)
|
Loading…
Reference in New Issue
Block a user