gd2asset 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/python
  2. import gameduino2 as gd2
  3. import os
  4. import array
  5. import Image
  6. import wave
  7. import audioop
  8. def cname(s):
  9. """ make name s C-friendly """
  10. for c in "-+.":
  11. s = s.replace(c, "_")
  12. return s.upper()
  13. class GD2Assets(gd2.prep.AssetBin):
  14. def __init__(self, opts, args):
  15. self.opts = opts
  16. self.args = args
  17. gd2.prep.AssetBin.__init__(self)
  18. self.header = opts.get('-o', 'default_assets.h')
  19. if '-f' in opts:
  20. self.asset_file = opts['-f']
  21. if '-3' in opts:
  22. self.target_810()
  23. self.handlers = {
  24. 'png' : (self.image, "PNG image file (options: format)"),
  25. 'jpg' : (self.image, "JPEG image file (options: format)"),
  26. 'bmp' : (self.image, "BMP image file (options: format)"),
  27. 'gif' : (self.image, "GIF image file (options: format)"),
  28. 'ttf' : (self.ttf, "TrueType font file (options: format, size)"),
  29. 'wav' : (self.sample, "Audio sample, mono 16-bit (no options)"),
  30. }
  31. def parse_format(self, format):
  32. formats = ('ARGB1555', 'L1', 'L2', 'L4', 'L8', 'RGB332', 'ARGB2', 'ARGB4', 'RGB565')
  33. if format not in formats:
  34. print 'ERROR: unknown format "%s"' % format
  35. print
  36. print 'Formats are: %s' % " ".join(formats)
  37. sys.exit(1)
  38. return eval("gd2." + format)
  39. def image(self, suffix, ff, format = 'ARGB4'):
  40. name = cname(os.path.basename(ff[0])[:-1 - len(suffix)])
  41. self.load_handle(name, [Image.open(f) for f in ff], self.parse_format(format))
  42. def ttf(self, suffix, f, size = '12', format = 'L4'):
  43. name = cname(os.path.basename(f[0])[:-1 - len(suffix)])
  44. self.load_ttf(name, f[0], int(size), self.parse_format(format))
  45. def sample(self, suffix, f):
  46. name = os.path.basename(f[0])[:-1 - len(suffix)].upper()
  47. f = wave.open(f[0], "rb")
  48. if f.getnchannels() != 1:
  49. print "Sorry - .wav file must be mono"
  50. sys.exit(1)
  51. if f.getsampwidth() != 2:
  52. print "Sorry - .wav file must be 16-bit"
  53. sys.exit(1)
  54. freq = f.getframerate()
  55. pcm16 = f.readframes(f.getnframes())
  56. (adpcm, _) = audioop.lin2adpcm(pcm16, f.getsampwidth(), (0,0))
  57. adpcm = adpcm[:len(adpcm) & ~7]
  58. da = array.array('B', [((ord(c) >> 4) | ((15 & ord(c)) << 4)) for c in adpcm])
  59. self.align(8)
  60. self.add(name, da.tostring())
  61. self.define(name + "_LENGTH", len(da))
  62. self.define(name + "_FREQ", freq)
  63. def error(self, suffix, f, **_):
  64. print 'ERROR: cannot identify type of file "%s"' % f
  65. print
  66. print 'recognized file types are:'
  67. for suffix,(_, doc) in sorted(self.handlers.items()):
  68. print ' %s %s' % (suffix, doc)
  69. sys.exit(1)
  70. def addall(self):
  71. for a in self.args:
  72. a = a.split(',')
  73. f = []
  74. vars = {}
  75. for part in a:
  76. if '=' in part:
  77. varval = part.split('=')
  78. if len(varval) != 2:
  79. print 'ERROR: syntax error in asset specification "%s"' % setting
  80. sys.exit(1)
  81. (var, val) = varval
  82. vars[var] = val
  83. else:
  84. f.append(part)
  85. suffix = f[0].split('.')[-1].lower()
  86. (handler, _) = self.handlers.get(suffix, (self.error, ''))
  87. handler(suffix, f, **vars)
  88. if __name__ == '__main__':
  89. import sys, getopt
  90. try:
  91. optlist, args = getopt.getopt(sys.argv[1:], "o:f:3")
  92. except getopt.GetoptError:
  93. print 'usage: gd2asset <options> <assets>'
  94. print
  95. print ' -o <name> output header file'
  96. print ' -f <name> output asset file (default is header file)'
  97. print ' -3 target GD3 (FT810 series)'
  98. print
  99. print 'If no output header file is given, then "default_assets.h" is used'
  100. print
  101. print 'Each asset is a filename, optionally followed by some var=val'
  102. print 'assignments. For example:'
  103. print ' pic1.png image, format ARGB4'
  104. print ' pic2.jpg,format=L8 image, format L8'
  105. print ' serif.ttf,size=16 font, 16 pixels high'
  106. print
  107. print 'The assets are compiled into flash, or if the "-f" option is given'
  108. print 'into a file. In this case the file should be copied to the'
  109. print 'microSD card.'
  110. print 'In either case, calling LOAD_ASSETS() from the program loads all'
  111. print 'assets.'
  112. sys.exit(1)
  113. optdict = dict(optlist)
  114. if 'gd3' in sys.argv[0].lower():
  115. optdict['-3'] = ''
  116. GD2Assets(optdict, args).make()