From a88ceaa1e1d7a20e262d01a3f31b388d414be458 Mon Sep 17 00:00:00 2001 From: Helloyunho Date: Thu, 3 Jul 2025 20:02:54 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9=20fix:=20use=20add=5Fcustom=5Fcomm?= =?UTF-8?q?and=20in=20cmake,=20use=20argparse=20in=20png2h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 32 +++++++++++++++++++++++++------- tools/png2h.py | 21 ++++++++++----------- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d5384a2c..b21c5781 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -473,6 +473,9 @@ if (ISLE_BUILD_APP) ISLE/res/isle.rc ISLE/isleapp.cpp ISLE/islefiles.cpp + ${CMAKE_SOURCE_DIR}/ISLE/res/arrow_bmp.h + ${CMAKE_SOURCE_DIR}/ISLE/res/busy_bmp.h + ${CMAKE_SOURCE_DIR}/ISLE/res/no_bmp.h ) list(APPEND isle_targets isle) if (WIN32) @@ -530,13 +533,28 @@ if (ISLE_BUILD_APP) ) endif() if(Python3_FOUND) - execute_process( - COMMAND ${Python3_EXECUTABLE} tools/png2h.py ISLE/res/arrow.png ISLE/res/busy.png ISLE/res/no.png - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} - RESULT_VARIABLE PNG2H_RESULT - OUTPUT_QUIET - ERROR_QUIET - ) + if(NOT DEFINED PYTHON_PIL_AVAILABLE) + execute_process( + COMMAND ${Python3_EXECUTABLE} -c "import PIL" + RESULT_VARIABLE PIL_RESULT + ERROR_QUIET + ) + set(PYTHON_PIL_AVAILABLE ${PIL_RESULT} EQUAL 0 CACHE BOOL "Is Python PIL available?") + endif() + if(PYTHON_PIL_AVAILABLE) + add_custom_command( + OUTPUT + ${CMAKE_SOURCE_DIR}/ISLE/res/arrow_bmp.h + ${CMAKE_SOURCE_DIR}/ISLE/res/busy_bmp.h + ${CMAKE_SOURCE_DIR}/ISLE/res/no_bmp.h + COMMAND ${Python3_EXECUTABLE} tools/png2h.py ISLE/res/arrow.png ISLE/res/busy.png ISLE/res/no.png + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + DEPENDS + ${CMAKE_SOURCE_DIR}/ISLE/res/arrow.png + ${CMAKE_SOURCE_DIR}/ISLE/res/busy.png + ${CMAKE_SOURCE_DIR}/ISLE/res/no.png + ) + endif() endif() endif() diff --git a/tools/png2h.py b/tools/png2h.py index a26e0540..c6820b73 100755 --- a/tools/png2h.py +++ b/tools/png2h.py @@ -1,10 +1,10 @@ #!/usr/bin/env python3 -import sys +import argparse from PIL import Image from pathlib import Path -def encode_cursor(image_path): +def encode_cursor(image_path: Path): img = Image.open(image_path).convert("RGBA") width, height = img.size pixels = img.load() @@ -47,20 +47,19 @@ def to_c_array(name, data): def main(): - if len(sys.argv) == 1: - print(f"Usage: {sys.argv[0]} [...input.png]") - sys.exit(1) + parser = argparse.ArgumentParser(allow_abbrev=False) + parser.add_argument("inputs", nargs="+", help="PNG images", type=Path) + args = parser.parse_args() - input_files = sys.argv[1:] + input_files: list[Path] = args.inputs for input_file in input_files: data, mask, width, height = encode_cursor(input_file) - input_file_path = Path(input_file) - input_file_name = input_file_path.stem - output_file = input_file_path.with_name(f"{input_file_name}_bmp.h") + input_file_name = input_file.stem + output_file = input_file.with_name(f"{input_file_name}_bmp.h") - with open(output_file, "w") as f: + with output_file.open("w") as f: f.write(f"// Generated from {input_file}\n") f.write(f"// Dimensions: {width}x{height}\n\n") f.write(f"#pragma once\n") @@ -77,4 +76,4 @@ def main(): if __name__ == "__main__": - main() + raise SystemExit(main())