[rendering] Do not write duplicated hashes...
[wikipediardware.git] / host-tools / imagetool / imagetool.py
blobb56077c5775fd0f9214504efe65e66bb8f8d3af3
1 #!/usr/bin/python
2 """
3 Generate an image from a png which can be used by guilib.
5 Copyright (C) 2009 Daniel Mack <daniel@caiaq.de>
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 """
21 import gd
22 import sys
23 import struct
25 def usage():
26 print "guilib image file generator"
27 print "Usage: %s <image> <outfilename> <imagename>" % (sys.argv[0])
28 print "This tool will output a 'static struct guilib_image' to <outfile>"
29 print "containing the pixel information from <imagefile>. The struct will"
30 print "be named <imagename>."
31 print "All paramters are mandatory."
32 sys.exit(1)
34 try:
35 imagefile = sys.argv[1]
36 outfile = sys.argv[2]
37 imagename = sys.argv[3]
38 except:
39 usage()
42 bit = 0
43 outbyte = 0
44 count = 0
46 try:
47 im = gd.image(imagefile)
48 (w, h) = im.size()
50 out = "static struct guilib_image %s = {\n" % (imagename)
51 out += "\t.width = %d,\n" % (w)
52 out += "\t.height = %d,\n" % (h)
53 out += "\t.data = {\n"
54 out += "\t\t"
56 for n in range (0, w * h):
57 pixel = im.getPixel((n % w, n / w))
58 bit = n % 8;
60 (r, g, b) = im.colorComponents(pixel)
61 color = (r + g + b) / 3
63 if (color > 127):
64 outbyte |= 1 << (7 - bit);
66 if bit == 7:
67 out += "0x%02x, " % outbyte
68 outbyte = 0
69 bit = 0
70 count += 1
72 if (count % 16) == 0:
73 out += "\n\t\t"
75 out += "}\n"
76 out += "};\n"
78 if bit > 0:
79 out += struct.pack("B", outbyte)
81 #im.close()
83 except:
84 print "unable to open bitmap file >%s<" % (imagefile)
85 sys.exit(2)
87 f = open(outfile, 'w')
88 print >> f, out
89 f.close()