#!/usr/bin/python

import xml.etree.ElementTree as ET
import sys
import zlib
import base64

print sys.argv[1] + "->" + sys.argv[2]
tree = ET.parse(sys.argv[1])
root = tree.getroot()
width = root.attrib["width"]
height = root.attrib["height"]
fh = open(sys.argv[2],'wb')
fh.write(";;----\n")
fh.write("defb "+width+" ;; width\n")
fh.write("defb "+height+" ;; height\n")
layerindex = 0
for layer in root.iter('layer'):
	dataindex = 0
	for data in layer.iter('data'):
		datab64 = data.text
		datazlib = base64.b64decode(datab64)
		uncompressed = zlib.decompress(datazlib)
		uncompresseddatabytes = bytearray(uncompressed)
		uncompressedintlength = len(uncompresseddatabytes)/4
		c = 0
		for i in range(0,uncompressedintlength):
			offs = i*4
			v1 = uncompresseddatabytes[offs+0]
			v2 = uncompresseddatabytes[offs+1]
			v3 = uncompresseddatabytes[offs+2]
			v4 = uncompresseddatabytes[offs+3]
			value = v1 + (v2<<8) + (v3<<16) + (v4<<24) 
			if c==0:
				fh.write("defb ")
			else:
				fh.write(",")
			fh.write(str(value))
			c = c + 1
			if c>=16:
				c = 0
				fh.write("\n")
fh.write(";;----")
fh.close()