prep.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. import os
  2. import sys
  3. import array
  4. import struct
  5. import zlib
  6. import textwrap
  7. import wave
  8. import audioop
  9. from PIL import Image, ImageFont, ImageDraw, ImageChops
  10. import gameduino2 as gd2
  11. import gameduino2.convert
  12. import gameduino2.tmxreader
  13. from gameduino2.imbytes import imbytes
  14. def stretch(im):
  15. d = imbytes(im)
  16. # print min(d), max(d)
  17. r = max(d) - min(d)
  18. return im.point(lambda x: (x - min(d)) * 255 / r)
  19. def getalpha(im):
  20. return im.split()[3]
  21. def tile(tw, th, im):
  22. tiles = []
  23. for y in range(0, im.size[1], th):
  24. for x in range(0, im.size[0], tw):
  25. tiles.append(im.crop((x, y, x + tw, y + th)))
  26. o = Image.new(im.mode, (tw, th * len(tiles)))
  27. for i,t in enumerate(tiles):
  28. o.paste(t, (0, i * th))
  29. return o
  30. def split(tw, th, im):
  31. tiles = []
  32. for y in range(0, im.size[1], th):
  33. for x in range(0, im.size[0], tw):
  34. tiles.append(im.crop((x, y, x + tw, y + th)))
  35. return tiles
  36. def join(tiles):
  37. (tw, th) = tiles[0].size
  38. o = Image.new(tiles[0].mode, (tw, th * len(tiles)))
  39. for i,t in enumerate(tiles):
  40. o.paste(t, (0, i * th))
  41. return o
  42. def setwidth(im, w):
  43. e = Image.new(im.mode, (w, im.size[1]))
  44. e.paste(im, (0, 0))
  45. return e
  46. def even(im):
  47. w = im.size[0]
  48. if (w % 2) == 0:
  49. return im
  50. else:
  51. return setwidth(im, w + 1)
  52. def extents(im):
  53. """ find pixel extents of im, as a box """
  54. w,h = im.size
  55. cols = [set(imbytes(im.crop((i, 0, i + 1, im.size[1])))) != set([0]) for i in range(w)]
  56. rows = [set(imbytes(im.crop((0, i, im.size[0], i + 1)))) != set([0]) for i in range(h)]
  57. if not True in cols:
  58. return (0, 0, 0, 0)
  59. else:
  60. x0 = cols.index(True)
  61. y0 = rows.index(True)
  62. while not cols[-1]:
  63. cols.pop()
  64. while not rows[-1]:
  65. rows.pop()
  66. return (x0, y0, len(cols), len(rows))
  67. def ul(x):
  68. return str(x) + "UL"
  69. def preview(np, fmt, size, data):
  70. def chan(x):
  71. return Image.fromstring("L", size, (255 * x).astype(np.uint8))
  72. if fmt == gd2.L1:
  73. r = Image.fromstring("1", size, data)
  74. elif fmt == gd2.L8:
  75. r = Image.fromstring("L", size, data)
  76. else:
  77. d8 = np.array(data)
  78. a = np.ones(size[0] * size[1])
  79. (r, g, b) = (a, a, a)
  80. if fmt == gd2.ARGB4:
  81. d16 = np.array(array.array('H', data.tostring()))
  82. a = (15 & (d16 >> 12)) / 15.
  83. r = (15 & (d16 >> 8)) / 15.
  84. g = (15 & (d16 >> 4)) / 15.
  85. b = (15 & (d16 >> 0)) / 15.
  86. elif fmt == gd2.RGB565:
  87. d16 = np.array(array.array('H', data.tostring()))
  88. r = (31 & (d16 >> 11)) / 31.
  89. g = (63 & (d16 >> 5)) / 63.
  90. b = (31 & (d16 >> 0)) / 31.
  91. elif fmt == gd2.ARGB1555:
  92. d16 = np.array(array.array('H', data.tostring()))
  93. a = (1 & (d16 >> 15))
  94. r = (31 & (d16 >> 10)) / 31.
  95. g = (31 & (d16 >> 5)) / 31.
  96. b = (31 & (d16 >> 0)) / 31.
  97. elif fmt == gd2.ARGB2:
  98. a = (3 & (d8 >> 6)) / 3.
  99. r = (3 & (d8 >> 4)) / 3.
  100. g = (3 & (d8 >> 2)) / 3.
  101. b = (3 & (d8 >> 0)) / 3.
  102. elif fmt == gd2.RGB332:
  103. r = (7 & (d8 >> 5)) / 7.
  104. g = (7 & (d8 >> 2)) / 7.
  105. b = (3 & (d8 >> 0)) / 3.
  106. elif fmt == gd2.L4:
  107. hi = d8 >> 4
  108. lo = d8 & 15
  109. d4 = np.column_stack((hi, lo)).flatten()
  110. r = d4 / 15.
  111. g = r
  112. b = r
  113. o = Image.merge("RGB", [chan(c) for c in (r, g, b)])
  114. bg = Image.new("RGB", size, (128, 128, 128))
  115. r = Image.composite(o, bg, chan(a))
  116. r = r.resize((r.size[0] * 5, r.size[1] * 5), Image.NEAREST)
  117. return r
  118. import gameduino2.base
  119. def pma(im):
  120. im = im.convert("RGBA")
  121. (r,g,b,a) = im.split()
  122. (r,g,b) = [ImageChops.multiply(a, c) for c in (r,g,b)]
  123. return Image.merge("RGBA", (r, g, b, a))
  124. class EVE(gameduino2.base.GD2):
  125. def __init__(self):
  126. self.d = ""
  127. def c(self, s):
  128. self.d += s
  129. def chunker(seq, size):
  130. return (seq[pos:pos + size] for pos in xrange(0, len(seq), size))
  131. class AssetBin(gameduino2.base.GD2):
  132. asset_file = None
  133. prefix = ""
  134. previews = False
  135. def __init__(self):
  136. self.alldata = ""
  137. self.commands = ""
  138. self.defines = []
  139. self.inits = []
  140. self.handle = 0
  141. self.np = None
  142. self.bitmaps = []
  143. # Set defaults for FT800. target_810() modifies these
  144. self.device = 'GD2'
  145. self.maxram = 256 * 1024
  146. self.maxhandles = 15
  147. def target_810(self):
  148. self.device = 'GD3'
  149. self.maxram = 1024 * 1024
  150. self.maxhandles = 32
  151. def define(self, n, v):
  152. self.defines.append((self.prefix + n, v))
  153. def add(self, name, s):
  154. if name:
  155. self.defines.append((name, ul(len(self.alldata))))
  156. self.alldata += s
  157. def c(self, s):
  158. self.commands += s
  159. def addim(self, name, im, fmt, dither = False):
  160. (_, imgdata) = gameduino2.convert.convert(im, dither, fmt = fmt)
  161. self.add(name, imgdata.tostring())
  162. (w, h) = im.size
  163. if name:
  164. self.defines.append(("%s_WIDTH" % name, w))
  165. self.defines.append(("%s_HEIGHT" % name, h))
  166. def align(self, n):
  167. while (len(self.alldata) % n) != 0:
  168. self.alldata += chr(0)
  169. def load_handle(self, name, images, fmt,
  170. dither = False,
  171. filter = gd2.NEAREST,
  172. scale = 1,
  173. rotating = False):
  174. if self.maxhandles <= self.handle:
  175. print "Error: too many bitmap handles used, limit is %d" % self.maxhandles
  176. sys.exit(1)
  177. (w, h) = images[0].size
  178. self.align(2)
  179. if name is not None:
  180. self.define("%s_HANDLE" % name, self.handle)
  181. name = self.prefix + name
  182. self.defines.append(("%s_WIDTH" % name, w))
  183. self.defines.append(("%s_HEIGHT" % name, h))
  184. self.defines.append(("%s_CELLS" % name, len(images)))
  185. self.bitmaps.append((name.lower(), w, h, w / 2, h / 2, len(self.alldata), fmt, self.handle))
  186. self.BitmapHandle(self.handle);
  187. self.BitmapSource(len(self.alldata));
  188. if not rotating:
  189. (vw, vh) = (scale * w, scale * h)
  190. vsz = 0
  191. else:
  192. vsz = int(scale * max(w, h))
  193. self.define("%s_SIZE" % name, vsz)
  194. (vw, vh) = (vsz, vsz)
  195. self.BitmapSize(filter, gd2.BORDER, gd2.BORDER, vw, vh);
  196. self.inits.append("static const shape_t %s_SHAPE = {%d, %d, %d, %d};" % (name, self.handle, w, h, vsz))
  197. # aw is aligned width
  198. # For L1, L2, L4 formats the width must be a whole number of bytes
  199. if fmt == gd2.L1:
  200. aw = (w + 7) & ~7
  201. elif fmt == gd2.L2:
  202. aw = (w + 3) & ~3
  203. elif fmt == gd2.L4:
  204. aw = (w + 1) & ~1
  205. else:
  206. aw = w
  207. bpl = {
  208. gd2.ARGB1555 : 2 * aw,
  209. gd2.L1 : aw / 8,
  210. gd2.L2 : aw / 4,
  211. gd2.L4 : aw / 2,
  212. gd2.L8 : aw,
  213. gd2.RGB332 : aw,
  214. gd2.ARGB2 : aw,
  215. gd2.ARGB4 : 2 * aw,
  216. gd2.RGB565 : 2 * aw,
  217. gd2.PALETTED : aw}[fmt]
  218. self.BitmapLayout(fmt, bpl, h);
  219. for i,im in enumerate(images):
  220. if aw != w:
  221. im = setwidth(im, aw)
  222. if hasattr(im, "imgdata"):
  223. imgdata = im.imgdata
  224. else:
  225. (_, imgdata) = gameduino2.convert.convert(im, dither, fmt = fmt)
  226. """
  227. if self.previews:
  228. if not self.np:
  229. import numpy
  230. self.np = numpy
  231. preview(self.np, fmt, im.size, imgdata).save("previews/%s-%s-%02d.png" % (self.name, name, i))
  232. """
  233. self.alldata += imgdata.tostring()
  234. self.handle += 1
  235. def load_font(self, name, ims, widths, fmt, **args):
  236. trim0 = 0
  237. while ims[trim0] is None:
  238. trim0 += 1
  239. p0 = len(self.alldata)
  240. h = self.handle
  241. tims = ims[trim0:]
  242. self.load_handle(name, tims, fmt, **args)
  243. self.align(4)
  244. p1 = len(self.alldata)
  245. # Compute memory required by one char
  246. onechar = (p1 - p0) / len(tims)
  247. # print name, 'font requires', (p1 - p0), 'bytes'
  248. sz = ims[trim0].size
  249. self.BitmapSource(p0 - (onechar * trim0));
  250. widths = [max(0, w) for w in widths]
  251. dblock = array.array('B', widths).tostring() + struct.pack("<5i", fmt, 1, sz[0], sz[1], p0 - (onechar * trim0))
  252. self.alldata += dblock
  253. self.cmd_setfont(h, p1);
  254. def load_ttf(self, name, ttfname, size, format):
  255. font = ImageFont.truetype(ttfname, size)
  256. sizes = [font.getsize(chr(c)) for c in range(32, 128)]
  257. fw = max([w for (w, _) in sizes])
  258. fh = max([h for (_, h) in sizes])
  259. # print fw, fh
  260. alle = {}
  261. for i in range(1, 96):
  262. im = Image.new("L", (fw+8, fh))
  263. dr = ImageDraw.Draw(im)
  264. dr.text((8,0), chr(32 + i), font=font, fill=255)
  265. alle[i] = gd2.prep.extents(im)
  266. fw = max([(x1 - x0) for (x0, y0, x1, y1) in alle.values()])
  267. ims = ([None] * 32) + [Image.new("L", (fw, fh)) for i in range(32, 128)]
  268. for i in range(33, 127):
  269. dr = ImageDraw.Draw(ims[i])
  270. (x0, y0, x1, y1) = alle[i - 32]
  271. x = max(0, 8 - x0)
  272. if x > 0:
  273. sizes[i - 32] = (sizes[i - 32][0] - x, sizes[i - 32][1])
  274. dr.text((x, 0), chr(i), font=font, fill=255)
  275. # imgtools.view(im)
  276. widths = ([0] * 32) + [w for (w, _) in sizes]
  277. self.load_font(name, ims, widths, format)
  278. def load_tiles(self, name, file_name, scale = 1.0):
  279. world_map = gameduino2.tmxreader.TileMapParser().parse_decode(file_name)
  280. # print("loaded map:", world_map.map_file_name)
  281. x_pixels = world_map.pixel_width
  282. y_pixels = world_map.pixel_height
  283. # print("map size in pixels:", x_pixels, y_pixels)
  284. # print("tile size used:", world_map.tilewidth, world_map.tileheight)
  285. # print("tiles used:", world_map.width, world_map.height)
  286. # print("found '", len(world_map.layers), "' layers on this map")
  287. layers = [l for l in world_map.layers if hasattr(l, 'decoded_content')]
  288. # print layers
  289. w,h = (world_map.width, world_map.height)
  290. ts = world_map.tile_sets[0]
  291. tw = int(ts.tilewidth)
  292. th = int(ts.tileheight)
  293. if scale is not None:
  294. stw,sth = (int(tw * scale), int(th * scale))
  295. else:
  296. stw,sth = tw,th
  297. used = set()
  298. for layer in layers:
  299. used |= set(layer.decoded_content)
  300. used = sorted(used - set([0]))
  301. def reindex(i):
  302. if i == 0:
  303. return None
  304. else:
  305. return used.index(i)
  306. def fetchtile(l, i, j):
  307. if (i < world_map.width) and (j < world_map.height):
  308. return reindex(l.decoded_content[i + (j * world_map.width)])
  309. else:
  310. return None
  311. eve = EVE()
  312. # print world_map.width * world_map.height * 4
  313. for j in range(0, world_map.height, 4):
  314. for i in range(0, world_map.width, 4):
  315. for layer in layers:
  316. for y in range(4):
  317. for x in range(4):
  318. t = fetchtile(layer, i + x, j + y)
  319. if t is not None:
  320. eve.Vertex2ii(stw * x, sth * y, t / 128, t % 128)
  321. else:
  322. eve.Nop()
  323. stride = ((w + 3) / 4)
  324. self.add(name, struct.pack("6H", w * stw, h * sth, stw * 4, sth * 4, stride, len(layers)) + eve.d)
  325. self.tile_files = world_map.tile_sets[0].images[0].source
  326. # print 'Size of tiles: %d (compressed %d)' % (len(eve.d), len(zlib.compress(eve.d)))
  327. # print 'Tile size', (tw, th)
  328. im = pma(Image.open(self.tile_files))
  329. def extract(i):
  330. w = im.size[0] / 72
  331. x = 72 * (i % w)
  332. y = 72 * (i / w)
  333. return im.crop((x + 0, y + 0, x + 70, y + 70)).resize((32, 32))
  334. def extract(i):
  335. if hasattr(ts, 'columns'):
  336. w = int(ts.columns)
  337. else:
  338. w = im.size[0] / tw
  339. x = ts.margin + (tw + ts.spacing) * (i % w)
  340. y = ts.margin + (th + ts.spacing) * (i / w)
  341. r = im.crop((x + 0, y + 0, x + tw, y + th))
  342. if scale:
  343. r = r.resize((stw, sth), Image.ANTIALIAS)
  344. return r
  345. for i,g128 in enumerate(chunker(used, 128)):
  346. # print 'g128', len(g128), g128
  347. for j,t in enumerate(g128):
  348. extract(t - 1).save("xx_%d_%d.png" % (i, j))
  349. self.load_handle(None, [extract(t - 1) for t in g128], gd2.ARGB4, dither=0)
  350. """
  351. def dxt1(self, imagefile):
  352. import numpy
  353. im = Image.open(imagefile).resize((480,272), Image.ANTIALIAS)
  354. dxt = "%s.dxt" % imagefile
  355. if not os.access(dxt, os.R_OK):
  356. im.save("tmp.png")
  357. assert os.system("squishpng tmp.png %s" % dxt) == 0
  358. sz = (480 / 4, 272 / 4)
  359. def rgb(cs):
  360. r = (cs >> 11) << 3
  361. g = 0xff & ((cs >> 5) << 2)
  362. b = 0xff & (cs << 3)
  363. return (r,g,b)
  364. def rgbim(cs):
  365. return Image.merge("RGB", [Image.fromarray(c.astype(numpy.uint8).reshape(*sz)) for c in rgb(cs)])
  366. def morton1(x):
  367. v = x & 0x55555555
  368. v = (v | (v >> 1)) & 0x33333333;
  369. v = (v | (v >> 2)) & 0x0F0F0F0F;
  370. v = (v | (v >> 4)) & 0x00FF00FF;
  371. v = (v | (v >> 8)) & 0x0000FFFF;
  372. return v.astype(numpy.uint16)
  373. h = open(dxt)
  374. h.read(8)
  375. c0s = []
  376. c1s = []
  377. bits = []
  378. for i in range(sz[0] * sz[1]):
  379. tile = h.read(8)
  380. c0,c1,bit = struct.unpack("2HI", tile)
  381. b0 = bit & 0x55555555
  382. b1 = (bit >> 1) & 0x55555555
  383. is0 = ~b1 & ~b0
  384. is1 = ~b1 & b0
  385. is2 = b1 & ~b0
  386. is3 = b1 & b0
  387. if c0<=c1:
  388. if bit == 0xaaaaaaaa:
  389. # print c0<c1,hex(bit)
  390. r0,g0,b0 = rgb(c0)
  391. r1,g1,b1 = rgb(c1)
  392. r = (r0 + r1) / 2
  393. g = (g0 + g1) / 2
  394. b = (b0 + b1) / 2
  395. # r,g,b = (255,0,255)
  396. c0 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3)
  397. c1 = c0
  398. bit = 0
  399. else:
  400. if 0:
  401. for i in range(0, 32, 2):
  402. fld = (3 & (bit >> i))
  403. if 3 == fld:
  404. print c0, c1, hex(bit), i
  405. assert 0
  406. # Map as follows:
  407. # 0 -> 0
  408. # 1 -> 3
  409. # 2 -> 1
  410. bit = (is1 * 3) + (is2 * 1)
  411. assert is3 == 0
  412. else:
  413. # 0 -> 0
  414. # 1 -> 3
  415. # 2 -> 1
  416. # 3 -> 2
  417. bit = (is1 * 3) + (is2 * 1) + (is3 * 2)
  418. if 0:
  419. c0 = 63 << 5 # green
  420. c1 = 31 << 11 # red
  421. bit = 0x0f0f0f0f
  422. c0s.append(c0)
  423. c1s.append(c1)
  424. bits.append(bit)
  425. c0s = numpy.array(c0s, numpy.uint16)
  426. c1s = numpy.array(c1s, numpy.uint16)
  427. bits = numpy.array(bits, numpy.uint32)
  428. bits0 = morton1(bits)
  429. bits1 = morton1(bits >> 1)
  430. if 1:
  431. def im44(v):
  432. im = Image.new("1", (4,4))
  433. pix = im.load()
  434. for i in range(4):
  435. for j in range(4):
  436. if v & (1 << (4 * i + j)):
  437. pix[j,i] = 255
  438. return im
  439. ims = [im44(i) for i in range(65536)]
  440. b0im = Image.new("1", (480, 272))
  441. b1im = Image.new("1", (480, 272))
  442. xys = [(x,y) for y in range(0, 272, 4) for x in range(0, 480, 4)]
  443. for i,(x,y) in enumerate(xys):
  444. b0im.paste(ims[bits0[i]], (x, y))
  445. b1im.paste(ims[bits1[i]], (x, y))
  446. class MockImage:
  447. def __init__(self, s, size):
  448. self.imgdata = s
  449. self.size = size
  450. self.load_handle("BACKGROUND_COLOR", [MockImage(c0s, sz), MockImage(c1s, sz)], gd2.RGB565, scale = 4)
  451. self.load_handle("BACKGROUND_BITS", [b0im, b1im], gd2.L1)
  452. """
  453. def load_sample(self, name, filename):
  454. f = wave.open(filename, "rb")
  455. if f.getnchannels() != 1:
  456. print "Sorry - .wav file must be mono"
  457. sys.exit(1)
  458. if f.getsampwidth() != 2:
  459. print "Sorry - .wav file must be 16-bit"
  460. sys.exit(1)
  461. freq = f.getframerate()
  462. pcm16 = f.readframes(f.getnframes())
  463. (adpcm, _) = audioop.lin2adpcm(pcm16, f.getsampwidth(), (0,0))
  464. adpcm = adpcm[:len(adpcm) & ~7]
  465. da = array.array('B', [((ord(c) >> 4) | ((15 & ord(c)) << 4)) for c in adpcm])
  466. self.align(8)
  467. self.add(name, da.tostring())
  468. self.define(name + "_LENGTH", len(da))
  469. self.define(name + "_FREQ", freq)
  470. header = None
  471. header_intro = ""
  472. def make(self):
  473. if self.header is None:
  474. name = self.__class__.__name__.lower() + "_assets.h"
  475. else:
  476. name = self.header
  477. self.name = name
  478. self.addall()
  479. if len(self.alldata) > self.maxram:
  480. print "Error: The data (%d bytes) is larger the the %s RAM (%d)" % (len(self.alldata), self.device, self.maxram)
  481. sys.exit(1)
  482. self.defines.append((self.prefix + "ASSETS_END", ul(len(self.alldata))))
  483. self.cmd_inflate(0)
  484. calldata = zlib.compress(self.alldata, 9)
  485. print 'Assets report'
  486. print '-------------'
  487. print 'Header file: %s' % self.header
  488. print '%s RAM used: %d' % (self.device, len(self.alldata))
  489. if not self.asset_file:
  490. print 'Flash used: %d' % len(calldata)
  491. else:
  492. print 'Output file: %s' % self.asset_file
  493. print 'File size: %d' % len(calldata)
  494. commandblock = self.commands + calldata
  495. hh = open(name, "w")
  496. hh.write(self.header_intro)
  497. for (nm,v) in self.defines:
  498. print >>hh, "#define %s %s" % (nm, v)
  499. p = self.prefix
  500. if self.asset_file is None:
  501. print >>hh, "static const PROGMEM uint8_t %s__assets[%d] = {" % (p, len(commandblock))
  502. print >>hh, textwrap.fill(", ".join(["%d" % ord(c) for c in commandblock]))
  503. print >>hh, "};"
  504. print >>hh, "#define %sLOAD_ASSETS() (GD.copy(%s__assets, sizeof(%s__assets)), GD.loadptr = %sASSETS_END)" % (p, p, p, p)
  505. else:
  506. open(self.asset_file, "wb").write(commandblock)
  507. print >>hh, '#define %sLOAD_ASSETS() (GD.safeload("%s"), GD.loadptr = %sASSETS_END)' % (p, self.asset_file, p)
  508. print >>hh
  509. for i in self.inits:
  510. print >>hh, i
  511. self.dump_bitmaps(hh)
  512. self.extras(hh)
  513. def dump_bitmaps(self, hh):
  514. hh.write("struct {\n")
  515. hh.write("".join([" Bitmap %s;\n" % bm[0] for bm in self.bitmaps]))
  516. hh.write("} bitmaps = {\n")
  517. fmt = " /* %16s */ {{%3d, %3d}, {%3d, %3d}, %#8xUL, %2d, %2d}"
  518. hh.write(",\n".join([fmt % bm for bm in self.bitmaps]))
  519. hh.write("\n};\n")
  520. def addall(self):
  521. pass
  522. def extras(self, hh):
  523. pass
  524. class ForthAssetBin(AssetBin):
  525. def make(self):
  526. if self.header is None:
  527. name = self.__class__.__name__.lower() + "_assets.fs"
  528. else:
  529. name = self.header
  530. self.name = name
  531. self.addall()
  532. if len(self.alldata) > self.maxram:
  533. print "Error: The data (%d bytes) is larger the the GD2 RAM" % len(self.alldata)
  534. sys.exit(1)
  535. self.defines.append((self.prefix + "ASSETS_END", ul(len(self.alldata))))
  536. self.cmd_inflate(0)
  537. calldata = zlib.compress(self.alldata)
  538. print 'Assets report'
  539. print '-------------'
  540. print 'Header file: %s' % self.header
  541. print 'GD2 RAM used: %d' % len(self.alldata)
  542. if not self.asset_file:
  543. print 'Flash used: %d' % len(calldata)
  544. else:
  545. print 'Output file: %s' % self.asset_file
  546. print 'File size: %d' % len(calldata)
  547. commandblock = self.commands + calldata
  548. commandblock += chr(0) * ((-len(commandblock)) & 3)
  549. commandblock32 = array.array('I', commandblock)
  550. hh = open(name, "w")
  551. print >>hh, "base @"
  552. print >>hh, "hex"
  553. if self.asset_file is None:
  554. print >>hh, textwrap.fill(" ".join(["%08x GD.c" % c for c in commandblock32]))
  555. else:
  556. open(self.asset_file, "wb").write(commandblock)
  557. print >>hh, "decimal"
  558. for (nm,v) in self.defines:
  559. print >>hh, "%-8s constant %s" % (str(v).replace('UL', ''), nm)
  560. print >>hh, "base !"
  561. self.extras(hh)