11 result += "\\%03o"%ord(c)
16 def make_tex(fn, filter="LINEAR", anisotropy=0, wrap=None, srgb=False):
21 fmt = "".join(img.getbands())
23 fmt = "LUMINANCE_ALPHA"
30 result = "storage %s %d %d;\n"%(fmt, img.size[0], img.size[1])
31 result += "filter %s;\n"%filter
32 if "MIPMAP" in filter:
33 result += "generate_mipmap true;\n"
35 result += "max_anisotropy %d;\n"%anisotropy
37 result += "wrap %s;\n"%wrap
38 result += "raw_data \""
39 data = list(img.getdata())
40 for y in range(img.size[1]):
41 i = (img.size[1]-1-y)*img.size[0]
42 if fmt=="LUMINANCE" or fmt=="SLUMINANCE":
43 result += escape("".join([chr(v) for v in data[i:i+img.size[0]]]))
45 result += escape("".join(["".join([chr(v) for v in p]) for p in data[i:i+img.size[0]]]))
50 if __name__=="__main__":
54 parser = argparse.ArgumentParser()
55 parser.add_argument("-o", "--output", metavar="FILE", help="Output filename")
56 parser.add_argument("-f", "--filter", choices=["NEAREST", "LINEAR", "MIPMAP"], default="LINEAR", help="Filtering mode")
57 parser.add_argument("-a", "--anisotropy", metavar="NUMBER", help="Maximum anisotropy, 0 = disable")
58 parser.add_argument("-w", "--wrap", choices=["REPEAT", "CLAMP_TO_EDGE", "MIRRORED_REPEAT"], help="Wrapping mode")
59 parser.add_argument("--srgb", action="store_const", const=True, help="Use sRGB color space")
60 parser.add_argument("image")
62 args = parser.parse_args()
66 filter = "LINEAR_MIPMAP_LINEAR"
70 out_fn = os.path.splitext(args.image)[0]+".tex2d"
71 out = file(out_fn, "w")
72 out.write(make_tex(args.image, filter, args.anisotropy, args.wrap, args.srgb))