#!/usr/bin/python

########################
# COPYRIGHT 2004       #
# Alexander Kozik      #
# http://www.atgc.org/ #
# akozik@atgc.org      #
########################

def Image_Drobilka(in_name, out_name, x_size, y_size):

	print in_name
	print out_name
	print x_size
	print y_size
	# print im_filter

	# in_file  = open(in_name,  "rb")
	# out_file1 = open(out_name, "wb")
	out_file2 = open(out_name + '.64.tklife' ,  "wb")
	out_file3 = open(out_name + '.256.tklife' , "wb")

	im = Image.open(in_name)
	width, height = im.size
	print in_name, width, height

	# im = im.resize((x_size, y_size), im_filter)
	# im = im.resize((x_size, y_size))
	im = im.resize((x_size, y_size),Image.ANTIALIAS)

	im.save(out_name, im.format)

	# im_data = list(im.getdata())
	# print im_data
	# data_ln = len(im_data)
	# print data_ln

	n = 0
	while n < y_size:
		m = 0
		while m < x_size: 
			current_pixel = im.getpixel((m, n))
			data_ln = len(current_pixel)
			# print data_ln
			r = current_pixel[0]
			g = current_pixel[1]
			b = current_pixel[2]
			print `n` + '\t' + `m` + '\t' + `r` + ' ' + `g` + ' ' + `b`
			R = TranslateColor256(r)
			G = TranslateColor256(g)
			B = TranslateColor256(b)
			r = TranslateColor64(r)
			g = TranslateColor64(g)
			b = TranslateColor64(b)
			print r + ' ' + g + ' ' + b + '  ==  ' + `R` + ' ' + `G` + ' ' + `B`
			out_file2.write('#' + r + g + b)
			out_file3.write('#' + R + G + B)
			if m < x_size - 1:
				out_file2.write('\t')
				out_file3.write('\t')
			if m == x_size - 1:
				out_file2.write('\n')
				out_file3.write('\n')
			m = m + 1
		n = n + 1 

	# in_file.close()
	# out_file1.close()
	out_file2.close()
	out_file3.close()

def TranslateColor64(c):

	if c <  64:
		ch = '0'
	if c < 128 and c >= 64:
		ch = '5'
	if c < 192 and c >= 128:
		ch = 'a'
	if c >= 192:
		ch = 'f'
	return ch

def TranslateColor256(c):

	if c < 16:
		ch = '0'
	if c < 32 and c >= 16:
		ch = '1'
	if c < 48 and c >= 32:
		ch = '2'
	if c < 64 and c >= 48:
		ch = '3'
	if c < 80 and c >= 64:
		ch = '4'
	if c < 96 and c >= 80:
		ch = '5'
	if c < 112 and c >= 96:
		ch = '6'
	if c < 128 and c >= 112:
		ch = '7'
	if c < 144 and c >= 128:
		ch = '8'
	if c < 160 and c >= 144:
		ch = '9'
	if c < 176 and c >= 160:
		ch = 'a'
	if c < 192 and c >= 176:
		ch = 'b'
	if c < 208 and c >= 192:
		ch = 'c'
	if c < 224 and c >= 208:
		ch = 'd'
	if c < 240 and c >= 224:
		ch = 'e'
	if c >= 240:
		ch = 'f'
	return ch

# import math
# import re
import sys
import string
import Image
if __name__ == "__main__":
	if len(sys.argv) <= 4 or len(sys.argv) > 5:
		print "Program usage: "
		print "[input_file] [output_file] [X_Size] [Y_Size]"
		print "Script resizes image and generates input file for TkLife"
		exit
	if len(sys.argv) == 5:
		in_name   = sys.argv[1]
		out_name  = sys.argv[2]
		x_size    = int(sys.argv[3])
		y_size    = int(sys.argv[4])
		# im_filter = sys.argv[5]
		
		if in_name != out_name:
			Image_Drobilka(in_name, out_name, x_size, y_size)
		else:
			print "Output should have different name than Input"
			exit
