color_convert: importable

This commit is contained in:
Zack Buhman 2024-09-13 07:20:00 -05:00
parent 9b7f0e8bf4
commit 5097e0e5ac

View File

@ -31,62 +31,64 @@ class color_format:
b5 = (b >> 3) & 31 b5 = (b >> 3) & 31
return (r5 << 11) | (g5 << 5) | (b5 << 0) return (r5 << 11) | (g5 << 5) | (b5 << 0)
def axxx4444(r, g, b, a):
a4 = (a >> 4) & 15
return (a4 << 12)
def from_string(s): def from_string(s):
return dict([ return dict([
("gbgr1555", color_format.gbgr1555), ("gbgr1555", color_format.gbgr1555),
("argb4444", color_format.argb4444), ("argb4444", color_format.argb4444),
("argb1555", color_format.argb1555), ("argb1555", color_format.argb1555),
("rgb565", color_format.rgb565), ("rgb565", color_format.rgb565),
("axxx4444", color_format.axxx4444),
])[s] ])[s]
in_file = sys.argv[1] def convert_colors(f, convert, colors):
format = sys.argv[2]
out_file = sys.argv[3]
palette = None
with Image.open(in_file) as im:
width, height = im.size
if not im.palette:
pixels = list(im.convert("RGBA").getdata())
else:
pixels = list(im.convert("P").getdata())
palette = list(im.palette.colors)
has_alpha = False
convert = color_format.from_string(format)
def convert_colors(f, colors):
for color in colors: for color in colors:
value = convert(*color) value = convert(*color)
f.write(struct.pack("<H", value)) f.write(struct.pack("<H", value))
if palette is None: def convert_indices(f, palette, pixels):
with open(out_file, 'wb') as f: if len(palette) <= 4:
convert_colors(f, pixels) for i in range(len(pixels) // 4):
else: a = pixels[i * 4 + 0]
with open(out_file, 'wb') as f: b = pixels[i * 4 + 1]
if len(palette) <= 4: c = pixels[i * 4 + 2]
for i in range(len(pixels) // 4): d = pixels[i * 4 + 3]
a = pixels[i * 4 + 0] assert a <= 3 and b <= 3 and c <= 3 and d <= 3, (a, b, c, d)
b = pixels[i * 4 + 1] pixel = (d << 6) | (c << 4) | (b << 2) | (a << 0)
c = pixels[i * 4 + 2] f.write(struct.pack("<B", pixel))
d = pixels[i * 4 + 3] elif len(palette) <= 16:
assert a <= 3 and b <= 3 and c <= 3 and d <= 3, (a, b, c, d) for i in range(len(pixels) // 2):
pixel = (d << 6) | (c << 4) | (b << 2) | (a << 0) a = pixels[i * 2 + 0]
f.write(struct.pack("<B", pixel)) b = pixels[i * 2 + 1]
elif len(palette) <= 16: assert a <= 15 and b <= 15, (a, b)
for i in range(len(pixels) // 2): pixel = (b << 4) | (a << 0)
a = pixels[i * 2 + 0] f.write(struct.pack("<B", pixel))
b = pixels[i * 2 + 1] elif len(palette) <= 256:
assert a <= 15 and b <= 15, (a, b) for pixel in pixels:
pixel = (b << 4) | (a << 0) assert pixel <= 255
f.write(struct.pack("<B", pixel)) f.write(struct.pack("<B", pixel))
elif len(palette) <= 256: else:
for pixel in pixels: assert False, len(palette)
assert pixel <= 255
f.write(struct.pack("<B", pixel)) if __name__ == "__main__":
in_file = sys.argv[1]
format = sys.argv[2]
out_file = sys.argv[3]
convert = color_format.from_string(format)
with Image.open(in_file) as im:
if not im.palette:
pixels = list(im.convert("RGBA").getdata())
with open(out_file, 'wb') as f:
convert_colors(f, convert, pixels)
else: else:
assert False, len(palette) pixels = list(im.convert("P").getdata())
with open(out_file + '.pal', 'wb') as f: palette = list(im.palette.colors)
convert_colors(f, [(*c, 255) for c in palette]) with open(out_file, 'wb') as f:
convert_indices(f, palette, pixels)
with open(out_file + '.pal', 'wb') as f:
convert_colors(f, convert, [(*c, 255) for c in palette])