]> git.tdb.fi Git - libs/gl.git/blob - scripts/maketex.py
Perform ambient occlusion PRNG multiplication with 64-bit integers
[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                 result += escape("".join(["".join([chr(v) for v in p]) for p in data[i:i+img.size[0]]]))
43         result += "\";\n"
44
45         return result
46
47 if __name__=="__main__":
48         import os
49         import argparse
50
51         parser = argparse.ArgumentParser()
52         parser.add_argument("-o", "--output", metavar="FILE", help="Output filename")
53         parser.add_argument("-f", "--filter", choices=["NEAREST", "LINEAR", "MIPMAP"], default="LINEAR", help="Filtering mode")
54         parser.add_argument("-a", "--anisotropy", metavar="NUMBER", help="Maximum anisotropy, 0 = disable")
55         parser.add_argument("-w", "--wrap", choices=["REPEAT", "CLAMP_TO_EDGE", "MIRRORED_REPEAT"], help="Wrapping mode")
56         parser.add_argument("--srgb", action="store_const", const=True, help="Use sRGB color space")
57         parser.add_argument("image")
58
59         args = parser.parse_args()
60
61         filter = args.filter
62         if filter=="MIPMAP":
63                 filter = "LINEAR_MIPMAP_LINEAR"
64
65         out_fn = args.output
66         if not out_fn:
67                 out_fn = os.path.splitext(args.image)[0]+".tex2d"
68         out = file(out_fn, "w")
69         out.write(make_tex(args.image, filter, args.anisotropy, args.wrap, args.srgb))