Debian Codegraph @

prboom2/data/convert-icon

raw
1 #!/usr/bin/python
2 #
3 # $Id: convert-icon 704 2006-10-18 00:51:11Z fraggle $
4 #
5 # Copyright(C) 2005 Simon Howard
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 # 02111-1307, USA.
21 #
22 # Converts images into C structures to be inserted in programs
23 #
24
25 import sys
26 import os
27 import re
28
29 try:
30 import Image
31 except ImportError:
32 print "WARNING: Could not update %s. Please install the Python Imaging library." % sys.argv[2]
33 sys.exit(0)
34
35
36 def convert_image(filename, output_filename):
37
38 im = Image.open(filename).convert("RGB")
39
40 outfile = open(output_filename, "w")
41
42 size = im.size
43
44 struct_name = os.path.basename(output_filename)
45 struct_name = re.sub(re.compile("\\..*$"), "", struct_name)
46 struct_name = re.sub(re.compile("\W"), "_", struct_name)
47
48 outfile.write("static int %s_w = %i;\n" % (struct_name, size[0]))
49 outfile.write("static int %s_h = %i;\n" % (struct_name, size[1]))
50
51 outfile.write("\n")
52 outfile.write("static unsigned char %s_data[] = {\n" % (struct_name))
53
54 elements_on_line = 0
55
56 outfile.write(" ")
57
58 for y in range(size[1]):
59 for x in range(size[0]):
60 val = im.getpixel((x, y))
61 outfile.write("0x%02x,0x%02x,0x%02x, " % val)
62 elements_on_line += 1
63
64 if elements_on_line >= 4:
65 elements_on_line = 0
66 outfile.write("\n")
67 outfile.write(" ")
68
69 outfile.write("\n")
70 outfile.write("};\n")
71
72 convert_image(sys.argv[1], sys.argv[2])
73
74