]> git.tdb.fi Git - libs/gl.git/blob - scripts/maketex.py
Add expression inlining
[libs/gl.git] / scripts / maketex.py
1 #!/usr/bin/python
2
3 def escape(str):
4         result = ""
5         for c in str:
6                 if c=='"':
7                         result += '\\"'
8                 elif c=='\\':
9                         result += '\\\\'
10                 elif ord(c)<0x20:
11                         result += "\\%03o"%ord(c)
12                 else:
13                         result += c
14         return result
15
16 def make_tex(fn, filter="LINEAR", anisotropy=0, wrap=None, srgb=False):
17         from PIL import Image
18
19         img = Image.open(fn)
20
21         fmt = "".join(img.getbands())
22         if fmt=="LA":
23                 fmt = "LUMINANCE_ALPHA"
24         elif fmt=="L":
25                 fmt = "LUMINANCE"
26
27         if srgb:
28                 fmt = "S"+fmt
29
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"
34         if anisotropy:
35                 result += "max_anisotropy %d;\n"%anisotropy
36         if wrap:
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]]]))
44                 else:
45                         result += escape("".join(["".join([chr(v) for v in p]) for p in data[i:i+img.size[0]]]))
46         result += "\";\n"
47
48         return result
49
50 if __name__=="__main__":
51         import os
52         import argparse
53
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")
61
62         args = parser.parse_args()
63
64         filter = args.filter
65         if filter=="MIPMAP":
66                 filter = "LINEAR_MIPMAP_LINEAR"
67
68         out_fn = args.output
69         if not out_fn:
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))