11 result += "\\{:03o}".format(ord(c))
16 def values(data, size):
17 for y in range(size[1]-1, -1, -1):
18 for x in range(size[0]):
19 yield data[x+y*size[0]]
21 def values_multi(data, size):
22 for y in range(size[1]-1, -1, -1):
23 for x in range(size[0]):
24 for c in data[x+y*size[0]]:
27 def make_tex(fn, mipmap=False, srgb=False):
34 fmt = "".join(img.getbands())
36 fmt = "LUMINANCE8_ALPHA8"
43 result = "storage {} {} {};\n".format(fmt, img.size[0], img.size[1])
45 result += "generate_mipmap true;\n"
47 if len(img.getbands())==1:
48 pixels = values(img.getdata(), img.size)
50 pixels = values_multi(img.getdata(), img.size)
51 result += "raw_data ={}=;\n".format(codecs.decode(base64.b64encode(bytes(pixels)), "ascii"))
55 if __name__=="__main__":
59 parser = argparse.ArgumentParser()
60 parser.add_argument("-o", "--output", metavar="FILE", help="Output filename")
61 parser.add_argument("-m", "--mipmap", action="store_true", help="Enable mipmaps")
62 parser.add_argument("--srgb", action="store_const", const=True, help="Use sRGB color space")
63 parser.add_argument("image")
65 args = parser.parse_args()
69 out_fn = os.path.splitext(args.image)[0]+".tex2d"
70 out = open(out_fn, "w")
71 out.write(make_tex(args.image, args.mipmap, args.srgb))