imagetool.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/python
  2. """
  3. Generate an image from a png which can be used by guilib.
  4. Copyright (c) 2009 Openmoko Inc.
  5. Authors Daniel Mack <daniel@caiaq.de>
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. """
  17. import gd
  18. import sys
  19. import struct
  20. def usage():
  21. print "guilib image file generator"
  22. print "Usage: %s <image> <outfilename> <imagename>" % (sys.argv[0])
  23. print "This tool will output a 'static struct guilib_image' to <outfile>"
  24. print "containing the pixel information from <imagefile>. The struct will"
  25. print "be named <imagename>."
  26. print "All paramters are mandatory."
  27. sys.exit(1)
  28. try:
  29. imagefile = sys.argv[1]
  30. outfile = sys.argv[2]
  31. imagename = sys.argv[3]
  32. except:
  33. usage()
  34. bit = 0
  35. outbyte = 0
  36. count = 0
  37. try:
  38. im = gd.image(imagefile)
  39. (w, h) = im.size()
  40. out = "static const struct guilib_image %s = {\n" % (imagename)
  41. out += "\t.width = %d,\n" % (w)
  42. out += "\t.height = %d,\n" % (h)
  43. out += "\t.data = {\n"
  44. out += "\t\t"
  45. for n in range (0, w * h):
  46. pixel = im.getPixel((n % w, n / w))
  47. bit = n % 8;
  48. (r, g, b) = im.colorComponents(pixel)
  49. color = (r + g + b) / 3
  50. if (color > 127):
  51. outbyte |= 1 << (7 - bit);
  52. if bit == 7:
  53. out += "0x%02x, " % outbyte
  54. outbyte = 0
  55. bit = 0
  56. count += 1
  57. if (count % 16) == 0:
  58. out += "\n\t\t"
  59. out += "}\n"
  60. out += "};\n"
  61. if bit > 0:
  62. out += struct.pack("B", outbyte)
  63. #im.close()
  64. except:
  65. print "unable to open bitmap file >%s<" % (imagefile)
  66. sys.exit(2)
  67. f = open(outfile, 'w')
  68. print >> f, out
  69. f.close()