Blender V2.61 - r43446

MakeGLStipple.py

Go to the documentation of this file.
00001 # ##### BEGIN GPL LICENSE BLOCK #####
00002 #
00003 #  This program is free software; you can redistribute it and/or
00004 #  modify it under the terms of the GNU General Public License
00005 #  as published by the Free Software Foundation; either version 2
00006 #  of the License, or (at your option) any later version.
00007 #
00008 #  This program is distributed in the hope that it will be useful,
00009 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 #  GNU General Public License for more details.
00012 #
00013 #  You should have received a copy of the GNU General Public License
00014 #  along with this program; if not, write to the Free Software Foundation,
00015 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00016 #
00017 # ##### END GPL LICENSE BLOCK #####
00018 
00019 # <pep8-80 compliant>
00020 
00021 # Converts 32x32 XPM images written be the gimp to GL stipples
00022 # takes xpm files as arguments, prints out C style definitions.
00023 
00024 import sys
00025 import os
00026 
00027 
00028 def main():
00029     xpm_ls = [f for f in sys.argv[1:] if f.lower().endswith(".xpm")]
00030 
00031     print("Converting: " + " ".join(xpm_ls))
00032 
00033     for xpm in xpm_ls:
00034         f = open(xpm, "r")
00035         data = f.read()
00036         f.close()
00037 
00038         # all after first {
00039         data = data.split("{", 1)[1]
00040 
00041         # all before first }
00042         data = data.rsplit("}", 1)[0]
00043 
00044         data = data.replace("\n", "")
00045 
00046         data = data.split(",")
00047 
00048         w, h, c, dummy = map(int, data[0].strip("\"").split())
00049 
00050         if w != 32 or h != 32 or c != 2:
00051             print("Skipping %r, expected 32x32, monochrome, got %s" %
00052                   (xpm, data[0]))
00053             continue
00054 
00055         # col_1 = data[1][1]
00056         col_2 = data[2][1]
00057 
00058         data = [d[1:-1] for d in data[3:]]
00059 
00060         bits = []
00061 
00062         for d in data:
00063             for i, c in enumerate(d):
00064                 bits.append('01'[(c == col_2)])
00065 
00066         if len(bits) != 1024:
00067             print("Skipping %r, expected 1024 bits, got %d" %
00068                   (xpm, len(bits)))
00069             continue
00070 
00071         bits = "".join(bits)
00072 
00073         chars = []
00074 
00075         for i in range(0, len(bits), 8):
00076             chars.append("0x%.2x" % int(bits[i:i + 8], 2))
00077 
00078         fout = sys.stdout
00079 
00080         var = os.path.basename(xpm)
00081         var = os.path.splitext(var)[0]
00082 
00083         fout.write("GLubyte stipple_%s[128] {\n\t" % var)
00084         for i, c in enumerate(chars):
00085             if i != 127:
00086                 fout.write("%s, " % c)
00087             else:
00088                 fout.write("%s" % c)
00089 
00090             if not ((i + 1) % 8):
00091                 fout.write("\n\t")
00092         fout.write("};\n")
00093 
00094 
00095 if __name__ == "__main__":
00096     main()