#!/usr/bin/env python

"""Go image recognition"""

import sys
import os
import math
import argparse
from operator import itemgetter

try:
    import Image, ImageDraw
except ImportError, msg:
    print >> sys.stderr, msg
    sys.exit(1)

import im_debug
import filters
from hough import Hough

Saving_dir = ''
Saving_num = 0

def main():
    """Main function of the program."""
    
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('file', metavar='file', nargs=1,
                        help="image to analyse")
    parser.add_argument('-w', type=int, default=640,
                        help="scale image to the specified width before analysis")
    parser.add_argument('-d', '--debug', dest='show_all', action='store_true',
                        help="show every step of the computation")
    parser.add_argument('-s', '--save', dest='do_something', action='store_const',
                        const=image_save, default=im_debug.show,
                        help="save images instead of displaying them")
    parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
                        help="report progress")
    args = parser.parse_args()

    show_all = args.show_all
    do_something = args.do_something
    verbose = args.verbose

    try:
        image = Image.open(args.file[0])
    except IOError, msg:
        print >> sys.stderr, msg
        return 1
    if image.mode == 'P':
        image = image.convert('RGB')
    
    if image.size[0] > args.w:
        image = image.resize((args.w, int((float(args.w)/image.size[0]) *
                              image.size[1])), Image.ANTIALIAS)
    global Saving_dir
    Saving_dir = "saved/" + args.file[0][:-4] + "_" + str(image.size[0]) + "/"
    
    if verbose:
        print >> sys.stderr, "preprocessing"

    if show_all:
        do_something(image, "original image")

    im_l = image.convert('L')
    if show_all:
        do_something(im_l, "ITU-R 601-2 luma transform")

    if verbose:
        print >> sys.stderr, "edge detection"

    im_edges = filters.edge_detection(im_l)
    if show_all:    
        do_something(im_edges, "edge detection")

    im_h = filters.high_pass(im_edges, 100)
    if show_all:
        do_something(im_h, "high pass filters")
    
    if verbose:
        print >> sys.stderr, "hough transform"

    hough1 = Hough(im_h.size)
    im_hough = hough1.transform(im_h)
    if show_all:
        do_something(im_hough, "hough transform")

    im_hough = filters.peaks(im_hough)
    if show_all:
        do_something(im_hough, "peak extraction")
               
    im_h2 = filters.high_pass(im_hough, 120)
    if show_all:
        do_something(im_h2, "second high pass filters")

    im_h2 = filters.components(im_h2)
    if show_all:
        do_something(im_h2, "components centers")

    if verbose:
        print >> sys.stderr, "second hough transform"

    hough2 = Hough(im_h2.size)
    im_hough2 = hough2.transform(im_h2)
    if show_all:
        do_something(im_hough2, "second hough transform")

    im_h3 = filters.high_pass(im_hough2, 120)
    if show_all:
        do_something(im_h3, "third high pass filter")
     
    im_h3 = filters.half_centers(im_h3)
    if show_all:
        do_something(im_h3, "half centers")

    if verbose:
        print >> sys.stderr, "finding the grid"

    lines_m = hough2.all_lines(im_h3)
    lines = []

    for line in lines_m:
        im_line = Image.new('L', im_h2.size)
        draw = ImageDraw.Draw(im_line)
        draw.line(line_from_angl_dist(line, im_h2.size), fill=255, width=5)
        if show_all:
            do_something(im_line, "line")
        im_c = combine(im_h2, im_line)
        if show_all:
            do_something(im_c, "hough x lines")
        lines.append(hough1.all_lines(im_c))

    image_g = image.copy()
    draw = ImageDraw.Draw(image_g)
    for line in [l for s in lines for l in s]:
        draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120))
    if show_all:
        do_something(image_g, "the grid")

    intersections = intersections_from_angl_dist(lines, image.size)
    image_g = image.copy()
    draw = ImageDraw.Draw(image_g)
    for line in intersections:
        for (x, y) in line:
            draw.point((x , y), fill=(120, 255, 120))
    
    for line in intersections:
        print ' '.join([stone_color(image, intersection) for intersection in
                       line])

    if show_all:
        do_something(image_g, "intersections")

    return 0

def stone_color(image, (x, y)):
    suma = 0.
    for i in range(-2, 3):
        for j in range(-2, 3):
            suma += sum(image.getpixel((x + i, y + j)))
    suma /= 3 * 25
    if suma < 55:
        return 'B'
    elif suma < 200: 
        return '.'
    else:
        return 'W'

def image_save(image, title=''):
    global Saving_dir
    global Saving_num
    filename = Saving_dir + "{0:0>2}".format(Saving_num) + '.jpg'
    if not os.path.isdir(Saving_dir):
        os.makedirs(Saving_dir)
    image.save(filename, 'JPEG')
    Saving_num += 1

def combine(image1, image2):
    im_l1 = image1.load()
    im_l2 = image2.load()

    im_n = Image.new('L', image1.size)
    im_nl = im_n.load()

    for x in xrange(image1.size[0]):
        for y in xrange(image1.size[1]):
            if im_l1[x, y] and im_l2[x, y]:
                im_nl[x, y] = 255
    return im_n

def line_from_angl_dist((angle, distance), size):
    x1 = - size[0] / 2
    y1 = int(round((x1 * math.sin(angle) - distance) / math.cos(angle))) + size[1] / 2
    x2 = size[0] / 2 
    y2 = int(round((x2 * math.sin(angle) - distance) / math.cos(angle))) + size[1] / 2
    return [(0, y1), (size[0] - 1, y2)]

def intersections_from_angl_dist(lines, size):
    intersections = []
    for (angl1, dist1) in sorted(lines[1], key=itemgetter(1)):
        line = []
        for (angl2, dist2) in lines[0]:
            if abs(angl1 - angl2) > 0.4:
                x =  - ((dist2 / math.cos(angl2))-(dist1 / math.cos(angl1))) / (math.tan(angl1) - math.tan(angl2))
                y = (math.tan(angl1) * x) - (dist1 / math.cos(angl1))
                line.append((int(x + size[0] / 2), int(y + size[1] / 2)))
        intersections.append(line)
    return intersections

if __name__ == '__main__':
    sys.exit(main())
