Files
IMX6U-Game/tools/png_to_header.py
T
basil 5ac61dca0e 统一 2D 渲染链路以消除格式分叉和热路径中转
将图片主路径收口为 RGBA5551 输入、RGB565 backbuffer 输出,并删除 USE_RGB565_BACKBUFFER 双路径。sprite/tilemap 主绘制改为直接 RGBA5551 -> RGB565,透明语义固定为 1-bit alpha test。同步收口 SDL/FB present 路径、sprite 资源加载/导出规范,以及相关测试与文档。

Constraint: 运行时图片主路径需统一到 RGBA5551,不能继续保留主要 RGBA8888 分支
Constraint: 透明仅支持 1-bit 语义,不引入 alpha blending
Rejected: 保留 USE_RGB565_BACKBUFFER 宏双路径 | 持续增加维护成本并掩盖真实主链路
Rejected: 主绘制继续走 RGBA5551 -> RGBA8888 -> RGB565 | 热路径存在不必要中转
Confidence: high
Scope-risk: moderate
Directive: 后续图片资源默认按 RGBA5551 接入;不要重新引入通用 RGBA8888 主运行时路径
Directive: 字体 atlas 目前仍属兼容输入;若继续收口,优先改成 1-bit mask 而不是新的图片分支
Tested: render_pipeline_tests;SDL Demo Release 构建;搜索确认无 USE_RGB565_BACKBUFFER 残留
Not-tested: Linux framebuffer 目标实机构建与显示验证
2026-06-08 18:32:04 +08:00

66 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
将 PNG 图片转换为 C 头文件(RGBA5551 / uint16_t 像素数组)。
用法:
python tools/png_to_header.py input.png output.h [var_name]
像素格式: RGBA5551(5-bit R, 5-bit G, 5-bit B, 1-bit A)
运行时 sprite 主路径只保留 1-bit 透明:A=0 跳过,A=1 覆写。
"""
import sys
import os
from PIL import Image
def convert(input_path: str, output_path: str, var_name: str):
img = Image.open(input_path).convert("RGBA")
w, h = img.size
pixels = list(img.getdata()) if not hasattr(img, "get_flattened_data") else list(img.get_flattened_data())
packed = []
for r, g, b, a in pixels:
packed.append(
((r >> 3) << 11)
| ((g >> 3) << 6)
| ((b >> 3) << 1)
| (1 if a else 0)
)
with open(output_path, "w", encoding="utf-8") as f:
f.write("// Auto-generated by tools/png_to_header.py\n")
f.write(f"#pragma once\n#include <cstdint>\n\n")
f.write(f"static const int32_t {var_name}_width = {w};\n")
f.write(f"static const int32_t {var_name}_height = {h};\n\n")
f.write(f"static const uint16_t {var_name}_pixels[] = {{\n")
for i in range(0, len(packed), 16):
chunk = packed[i : i + 16]
line = ", ".join(f"0x{p:04X}" for p in chunk)
f.write(f" {line},\n")
f.write("};\n")
def main():
if len(sys.argv) < 3:
print(f"用法: {sys.argv[0]} input.png output.h [var_name]")
sys.exit(1)
input_path = sys.argv[1]
output_path = sys.argv[2]
if len(sys.argv) >= 4:
var_name = sys.argv[3]
else:
# 从文件名生成变量名: player_idle.png -> player_idle
var_name = os.path.splitext(os.path.basename(input_path))[0]
convert(input_path, output_path, var_name)
print(f"已生成: {output_path} (var: {var_name}, 像素数: {var_name}_width * {var_name}_height)")
if __name__ == "__main__":
main()