# TGA fixtures (tools/fixtures.py): files written by Pillow (RGB / RGBA / L / P / LA, raw and RLE, both origins) and # ImageMagick (raw / RLE, palette, grey), plus hand-written ones (15 / 16-bit pixels and colour maps, 8 / 16-bit # indexes with a first index, right-to-left, RLE packets crossing rows, an image ID, 16-bit grey + alpha); # references = Pillow's RGBA decodes, ImageMagick's for the files Pillow # refuses (right-to-left, 16-bit grey + alpha) and for 15 / 16-bit colour, where Pillow differs (5-bit scaling, alpha). # tests/gen/tga.luau { { name, data, width, height, rgba (bytes) } } import struct CODE = r''' import io, os, random, struct, subprocess, sys from PIL import Image magick = sys.argv[1] rng = random.Random(9) out = [] def emit(name, data, magick_ref=False): try: if magick_ref: raise ValueError("15 / 16-bit: Pillow scales 5-bit values by x * 255 // 31 and inverts the alpha bit") im = Image.open(io.BytesIO(data)) rgba = im.convert("RGBA").tobytes() size = im.size except Exception: # files Pillow refuses (right-to-left, 16-bit grey + alpha): ImageMagick's decode info = subprocess.run([magick, "tga:-", "-format", "%w %h", "info:"], input=data, capture_output=True, check=True).stdout.split() size = (int(info[0]), int(info[1])) rgba = subprocess.run([magick, "tga:-", "-depth", "8", "rgba:-"], input=data, capture_output=True, check=True).stdout out.append((name, data, size, rgba)) w, h = 29, 17 px = bytearray() for y in range(h): for x in range(w): a = 255 if (x + y) % 4 else (x * 37) % 256 px += bytes(((x * 9) % 256, (y * 15) % 256, rng.randrange(256) if x % 3 == 0 else 100, a)) rgba = Image.frombytes("RGBA", (w, h), bytes(px)) for mode in ("RGB", "RGBA", "L", "LA", "P"): im = rgba.convert(mode) if mode != "P" else rgba.convert("RGB").quantize(37) for comp in (None, "tga_rle"): for orient in (1, -1): b = io.BytesIO() kw = {"orientation": orient} if comp: kw["compression"] = comp im.save(b, "TGA", **kw) emit("pil-%s-%s-%s" % (mode, comp or "raw", "top" if orient == 1 else "bottom"), b.getvalue()) # runs that cross rows (RLE) flat = Image.new("RGB", (13, 9), (10, 20, 30)) for x in range(5): flat.putpixel((x, 4), (200, 0, 0)) b = io.BytesIO(); flat.save(b, "TGA", compression="tga_rle"); emit("pil-runs", b.getvalue()) # ImageMagick writers src = io.BytesIO(); rgba.save(src, "PNG") for name, args in (("im-rgba", []), ("im-rle", ["-compress", "RLE"]), ("im-gray", ["-colorspace", "gray"]), ("im-palette", ["-colors", "20", "-type", "Palette"]), ("im-rgb", ["-alpha", "off"])): r = subprocess.run([magick, "png:-"] + args + ["tga:-"], input=src.getvalue(), capture_output=True, check=True) emit(name, r.stdout) # hand-written def hdr(idlen, cmt, it, cfirst, clen, cbits, w, h, depth, desc): return struct.pack("> 3) << 10) | ((g >> 3) << 5) | (b >> 3)) cols = [((i * 40) % 256, (i * 90) % 256, (255 - i * 20) % 256) for i in range(12)] w, h = 6, 4 idx = [(x + y * 2) % 12 for y in range(h) for x in range(w)] emit("hand-16bit", hdr(0, 0, 2, 0, 0, 0, w, h, 16, 0) + b"".join(p15(*cols[i]) for i in idx), magick_ref=True) emit("hand-15bit-rtl", hdr(0, 0, 2, 0, 0, 0, w, h, 15, 0x10) + b"".join(p15(*cols[i], a=0) for i in idx), magick_ref=True) emit("hand-cmap16", hdr(0, 1, 1, 0, 12, 16, w, h, 8, 0x20) + b"".join(p15(*c) for c in cols) + bytes(idx), magick_ref=True) emit("hand-cmap32-first", hdr(0, 1, 1, 4, 12, 32, w, h, 8, 0x28) + b"".join(bytes((c[2], c[1], c[0], 77)) for c in cols) + bytes(i + 4 for i in idx)) emit("hand-cmap24-rle", hdr(0, 1, 9, 0, 12, 24, w, h, 8, 0) + b"".join(bytes((c[2], c[1], c[0])) for c in cols) + bytes([0x80 + 9, 3, 0x05, 1, 2, 3, 4, 5, 6, 0x80 + 7, 11])) emit("hand-id-top", hdr(5, 0, 3, 0, 0, 0, w, h, 8, 0x20) + b"hello" + bytes((i * 21) % 256 for i in idx)) emit("hand-graya16", hdr(0, 0, 3, 0, 0, 0, w, h, 16, 0x28) + b"".join(bytes(((i * 21) % 256, (i * 50) % 256)) for i in idx)) emit("hand-rle32-cross", hdr(0, 0, 10, 0, 0, 0, 5, 3, 32, 0x28) + bytes([0x80 + 6, 1, 2, 3, 200, 0x08]) + bytes(range(9 * 4))) for name, data, size, rgba in out: sys.stdout.buffer.write(struct.pack("