-
-
Notifications
You must be signed in to change notification settings - Fork 568
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add bitmap arr to file (or reverse) converter (#2248)
* add covert tool * fix for generator * fix for generator
- Loading branch information
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |