Files
cnmcdee 6e9e7b5fe3 Fix malformed 16px ICOs so all 547 palette icons load
Pillow writes 16px ICOs with unpadded 2-byte AND-mask rows (840 B vs spec
872 B, biSizeImage 768 vs 832); Ubuntu 26.04 glycin rejects them, so 0
icons attached to the palette. New widgets/tools/fix_icons.py pads each
mask row to 4 bytes and corrects the size fields (XOR pixels identical
547/547); gen_icons.py now calls it after every save; stale 32bpp
comments corrected to 24bpp. Glycin loads 547/547, ico.h harness 547/547,
palette self-test 583/583.
2026-11-29 04:49:52 +00:00

76 lines
2.8 KiB
Python

#!/usr/bin/env python3
"""Rewrite 16x16 BMP-format .ico files to the spec glycin enforces.
Pillow's ICO writer emits the 1-bit AND mask with 2 bytes per row for a
16px icon; the BMP format requires each mask row padded to a 4-byte
boundary (4 bytes/row), and biSizeImage must cover XOR+AND. Glycin
(Ubuntu 26.04's gdk-pixbuf backend) rejects the short files outright
("ICO image data size did not match expected size"), so the designer's
palette showed no icons. The XOR pixel bytes are copied verbatim, so
the artwork is unchanged; only the mask padding and the size fields
change. The palette's own decoder (widgets/common/ico.h) ignores the
AND mask, so it reads fixed files identically.
Usage: python3 widgets/tools/fix_icons.py (run from the repo root)
"""
import os, struct, sys
def fix_ico_bytes(data):
if len(data) < 22 or data[:4] != b"\x00\x00\x01\x00":
raise ValueError("not a single-image ICO")
count = struct.unpack("<H", data[4:6])[0]
if count != 1:
raise ValueError("expected 1 image, got %d" % count)
w, h, colors, res, planes, bpp, size, off = struct.unpack("<BBBBHHII", data[6:22])
if (w or 256) != 16 or (h or 256) != 16 or bpp != 24:
raise ValueError("expected 16x16 24bpp, got %sx%s %dbpp" % (w or 256, h or 256, bpp))
img = data[off:off + size]
if len(img) < 40 + 768 + 32 or img[:4] != struct.pack("<I", 40):
raise ValueError("unexpected BMP payload")
xor = img[40:40 + 768]
andmask = img[40 + 768:40 + 768 + 32]
padded = b"".join(andmask[i:i + 2] + b"\x00\x00" for i in range(0, 32, 2))
assert len(padded) == 64
hdr = bytearray(img[:40])
struct.pack_into("<I", hdr, 20, 768 + 64) # biSizeImage
newimg = bytes(hdr) + xor + padded
assert len(newimg) == 872
entry = bytearray(data[6:22])
struct.pack_into("<I", entry, 8, 872) # dwBytesInRes
return data[:6] + bytes(entry) + newimg
def fix_ico_file(path):
with open(path, "rb") as f:
data = f.read()
fixed = fix_ico_bytes(data)
if fixed == data:
return False
with open(path, "wb") as f:
f.write(fixed)
return True
def main():
root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
outdir = os.path.join(root, "icons")
fixed = unchanged = errors = 0
for name in sorted(os.listdir(outdir)):
if not name.endswith(".ico"):
continue
try:
if fix_ico_file(os.path.join(outdir, name)):
fixed += 1
else:
unchanged += 1
except ValueError as e:
print("ERROR %s: %s" % (name, e), file=sys.stderr)
errors += 1
print("icons fixed: %d, already valid: %d, errors: %d" % (fixed, unchanged, errors))
return 1 if errors else 0
if __name__ == "__main__":
sys.exit(main())