]> git.tdb.fi Git - libs/gl.git/blobdiff - scripts/maketex.py
Fix a bug in makefont.py if no ranges are given
[libs/gl.git] / scripts / maketex.py
index 04de1d38a57d51cc2e25c2ea16f8e5896481283a..916ce747c404f8b26210078a0e07b7d53f8b5fbf 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 def escape(str):
        result = ""
@@ -8,13 +8,13 @@ def escape(str):
                elif c=='\\':
                        result += '\\\\'
                elif ord(c)<0x20:
-                       result += "\\%03o"%ord(c)
+                       result += "\\{:03o}".format(ord(c))
                else:
                        result += c
-       return result;
+       return result
 
-def make_tex(fn, filter="LINEAR", anisotropy=0, wrap=None):
-       import Image
+def make_tex(fn, filter="LINEAR", anisotropy=0, wrap=None, srgb=False):
+       from PIL import Image
 
        img = Image.open(fn)
 
@@ -24,19 +24,25 @@ def make_tex(fn, filter="LINEAR", anisotropy=0, wrap=None):
        elif fmt=="L":
                fmt = "LUMINANCE"
 
-       result = "storage %s %d %d;\n"%(fmt, img.size[0], img.size[1])
-       result += "filter %s;\n"%filter
+       if srgb:
+               fmt = "S"+fmt
+
+       result = "storage {} {} {};\n".format(fmt, img.size[0], img.size[1])
+       result += "filter {};\n".format(filter)
        if "MIPMAP" in filter:
                result += "generate_mipmap true;\n"
        if anisotropy:
-               result += "max_anisotropy %d;\n"%anisotropy
+               result += "max_anisotropy {};\n".format(anisotropy)
        if wrap:
-               result += "wrap %s;\n"%wrap
+               result += "wrap {};\n".format(wrap)
        result += "raw_data \""
        data = list(img.getdata())
        for y in range(img.size[1]):
                i = (img.size[1]-1-y)*img.size[0]
-               result += escape("".join(["".join([chr(v) for v in p]) for p in data[i:i+img.size[0]]]))
+               if fmt=="LUMINANCE" or fmt=="SLUMINANCE":
+                       result += escape("".join([chr(v) for v in data[i:i+img.size[0]]]))
+               else:
+                       result += escape("".join(["".join([chr(v) for v in p]) for p in data[i:i+img.size[0]]]))
        result += "\";\n"
 
        return result
@@ -50,6 +56,7 @@ if __name__=="__main__":
        parser.add_argument("-f", "--filter", choices=["NEAREST", "LINEAR", "MIPMAP"], default="LINEAR", help="Filtering mode")
        parser.add_argument("-a", "--anisotropy", metavar="NUMBER", help="Maximum anisotropy, 0 = disable")
        parser.add_argument("-w", "--wrap", choices=["REPEAT", "CLAMP_TO_EDGE", "MIRRORED_REPEAT"], help="Wrapping mode")
+       parser.add_argument("--srgb", action="store_const", const=True, help="Use sRGB color space")
        parser.add_argument("image")
 
        args = parser.parse_args()
@@ -61,5 +68,5 @@ if __name__=="__main__":
        out_fn = args.output
        if not out_fn:
                out_fn = os.path.splitext(args.image)[0]+".tex2d"
-       out = file(out_fn, "w")
-       out.write(make_tex(args.image, filter, args.anisotropy, args.wrap))
+       out = open(out_fn, "w")
+       out.write(make_tex(args.image, filter, args.anisotropy, args.wrap, args.srgb))