gd3asset 6.0 KB

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