gd2asset 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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,
  42. [Image.open(f) for f in ff],
  43. self.parse_format(format),
  44. dither = '-d' in self.opts)
  45. def ttf(self, suffix, f, size = '12', format = 'L4'):
  46. name = cname(os.path.basename(f[0])[:-1 - len(suffix)])
  47. self.load_ttf(name, f[0], int(size), self.parse_format(format))
  48. def sample(self, suffix, f):
  49. name = os.path.basename(f[0])[:-1 - len(suffix)].upper()
  50. f = wave.open(f[0], "rb")
  51. if f.getnchannels() != 1:
  52. print "Sorry - .wav file must be mono"
  53. sys.exit(1)
  54. if f.getsampwidth() != 2:
  55. print "Sorry - .wav file must be 16-bit"
  56. sys.exit(1)
  57. freq = f.getframerate()
  58. pcm16 = f.readframes(f.getnframes())
  59. (adpcm, _) = audioop.lin2adpcm(pcm16, f.getsampwidth(), (0,0))
  60. adpcm = adpcm[:len(adpcm) & ~7]
  61. da = array.array('B', [((ord(c) >> 4) | ((15 & ord(c)) << 4)) for c in adpcm])
  62. self.align(8)
  63. self.add(name, da.tostring())
  64. self.define(name + "_LENGTH", len(da))
  65. self.define(name + "_FREQ", freq)
  66. def error(self, suffix, f, **_):
  67. print 'ERROR: cannot identify type of file "%s"' % f
  68. print
  69. print 'recognized file types are:'
  70. for suffix,(_, doc) in sorted(self.handlers.items()):
  71. print ' %s %s' % (suffix, doc)
  72. sys.exit(1)
  73. def addall(self):
  74. for a in self.args:
  75. a = a.split(',')
  76. f = []
  77. vars = {}
  78. for part in a:
  79. if '=' in part:
  80. varval = part.split('=')
  81. if len(varval) != 2:
  82. print 'ERROR: syntax error in asset specification "%s"' % setting
  83. sys.exit(1)
  84. (var, val) = varval
  85. vars[var] = val
  86. else:
  87. f.append(part)
  88. suffix = f[0].split('.')[-1].lower()
  89. (handler, _) = self.handlers.get(suffix, (self.error, ''))
  90. handler(suffix, f, **vars)
  91. if __name__ == '__main__':
  92. import sys, getopt
  93. try:
  94. optlist, args = getopt.getopt(sys.argv[1:], "3do:f:")
  95. except getopt.GetoptError:
  96. print 'usage: gd2asset <options> <assets>'
  97. print
  98. print ' -3 target GD3 (FT810 series)'
  99. print ' -d dither all pixel conversions'
  100. print ' -f <name> output asset file (default is header file)'
  101. print ' -o <name> output header file'
  102. print
  103. print 'If no output header file is given, then "default_assets.h" is used'
  104. print
  105. print 'Each asset is a filename, optionally followed by some var=val'
  106. print 'assignments. For example:'
  107. print ' pic1.png image, format ARGB4'
  108. print ' pic2.jpg,format=L8 image, format L8'
  109. print ' serif.ttf,size=16 font, 16 pixels high'
  110. print
  111. print 'The assets are compiled into flash, or if the "-f" option is given'
  112. print 'into a file. In this case the file should be copied to the'
  113. print 'microSD card.'
  114. print 'In either case, calling LOAD_ASSETS() from the program loads all'
  115. print 'assets.'
  116. sys.exit(1)
  117. optdict = dict(optlist)
  118. if 'gd3' in sys.argv[0].lower():
  119. optdict['-3'] = ''
  120. GD2Assets(optdict, args).make()