gd3asset 5.6 KB

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