#! /bin/python
"""
Draws a chess board for a bitmap of size (6x8x8) 
where each kind of piece is in a different channel
Author: Ashudeep Singh (17-05-2015)
"""

import numpy as np
import cv2


pieces = ['pawn', 'rook', 'knight', 'bishop', 'queen', 'king']
PIECE_TO_INDEX = {'P' : 0, 'R' : 1, 'N' : 2, 'B' : 3, 'Q' : 4, 'K' : 5}
piece_images=[]
img_size = 392
piece_size = 392/8

board = np.zeros((img_size, img_size), dtype=np.uint8)

for i in xrange(8):
	for j in xrange(8):
		if (i+j)%2 == 0:
			board[i*piece_size:(i+1)*piece_size,j*piece_size:(j+1)*piece_size] = 255*np.ones((piece_size, piece_size), dtype=np.uint8)


wpawnw = np.load("wpawnw.npz.npy")
wpawnb = np.load("wpawnb.npz.npy")
bpawnw = abs(255-wpawnb)
bpawnb = abs(255-wpawnw)
wrookw = np.load("wrookw.npz.npy")
wrookb = np.load("wrookb.npz.npy")
brookw = abs(255-wrookb)
brookb = abs(255-wrookw)
wknightw = np.load("wknightw.npz.npy")
wknightb = np.load("wknightb.npz.npy")
bknightw = abs(255-wknightb)
bknightb = abs(255-wknightw)
wbishopw = np.load("wbishopw.npz.npy")
wbishopb = np.load("wbishopb.npz.npy")
bbishopw = abs(255-wbishopb)
bbishopb = abs(255-wbishopw)
wqueenw = np.load("wqueenw.npz.npy")
wqueenb = np.load("wqueenb.npz.npy")
bqueenw = abs(255-wqueenb)
bqueenb = abs(255-wqueenw)
wkingw = np.load("wkingw.npz.npy")
wkingb = np.load("wkingb.npz.npy")
bkingw = abs(255-wkingb)
bkingb = abs(255-wkingw)




#read the bitmaps
map = np.load("sample_bitmap.npy")
for p in xrange(map.shape[0]):
	for i in xrange(map.shape[1]):
		for j in xrange(map.shape[2]):
			if map[p,i,j] == 1:
				piece = "w"
			elif map[p,i,j] == -1:
				piece = "b"
			else:
				continue
			piece = piece+pieces[p]
			if (i+j)%2 == 0:
				piece = piece+"w"
			else:
				piece = piece+"b"
			board[i*piece_size:(i+1)*piece_size,j*piece_size:(j+1)*piece_size] = eval(piece)

#cv2.startWindowThread()
#cv2.namedWindow("board")
#cv2.imshow("board",board)

