#!/usr/bin/python
from SLPLib import *
from SLPPal import pal

from sys import argv, exit
import Image, ImageDraw, ImageFile

pall = pal[0]


file = argv[1]

xpics = int(argv[2])
ypics = int(argv[3])

print "Opening SLP file %s." % file

slp = SLP(file)
slp.load()

print slp.numShapes

if (xpics * ypics) != slp.numShapes:
	print "Wrong number of shapes."
	exit(1)

# determine width ->
mwid = 0
mhig = 0
for shape in slp.shapes:
	if shape.size[0] > mwid:
		mwid = shape.size[0]
	if shape.size[1] > mhig:
		mhig = shape.size[1]
print mwid, mhig
size = (mwid*xpics,mhig*ypics)
print "Picture size: %d*%d" % size

image = Image.new('RGB', size)
pad = ImageDraw.Draw(image) #Awkward PIL, blah

Xp = 0 # pic counter
S = 0 # shape counter

while Xp < xpics:
	Yp = 0 # Ycounter
	xoff = Xp * mwid
	while Yp < ypics:
		#inlined drawPicture
		data = slp.gimage(S)
		yoff = Yp * mhig
		yc = 0
		for y in data:
			xc = 0
			for x in y:
				if xc > mwid:
					break
				if  x == 'P-C':
					color = (255, 255, 255)
				elif x == 'S-D':
					color = (255, 128, 255)
				elif x == 'SH':
					color = (64, 102, 134)
				elif x == None:
					color = (128, 205, 168)
				else: 
					t = pall[ord(x)]
					color = (t[0],t[1],t[2])
				pad.point((xoff+xc, yoff+yc), fill=color)
				xc += 1
			yc += 1	
		# end inlined
		S += 1
		Yp += 1
	Xp += 1

image.show()
		
		
