from gamera.core import *
from gamera.core import Point, ONEBIT

class Morph():
    """Morfologicke a rle operace
    """

    structure1 = Image(Point(0, 0), Point(2, 0), ONEBIT)
    structure2 = Image(Point(0, 0), Point(4, 4), ONEBIT)
    structure3 = Image(Point(0, 0), Point(0, 2), ONEBIT)
    structure4 = Image(Point(0, 0), Point(12, 0), ONEBIT)
    structure5 = Image(Point(0, 0), Point(0, 12), ONEBIT)

    def __init__(self):
        """Inicializuje zakladni strukturalni segmenty
        """
        self.structure1.fill(1)
        self.structure2.fill(1)
        self.structure3.fill(1)
        self.structure4.fill(1)
        self.structure5.fill(1)

    def filterhor1(self, img):
        """Filtrace horizontalnich linek  moorf otevrenim
        """
        temp = img.erode_with_structure(self.structure5, Point(0, 6))
        temp = temp.dilate_with_structure(self.structure5, Point(0, 6))
        return temp.image_copy()

    def filterline2(self, img, sf):
        """Filtrace linek pomoci RLE
        """
        mostfrequentrun = img.most_frequent_run(u'black', u'horizontal') + 2
        imgnolines1 = img.image_copy()
        imgnolines2 = img.image_copy()
        #imgnolines1.despeckle(3)
        imgnolines1.filter_wide_runs(mostfrequentrun, u'black')
        #imgnolines1 = self.morphology(imgnolines2, 'dilate', 2, 0)
        #imgnolines1.filter_wide_runs(mostfrequentrun+6, u'black')
        #imgnolines1 = self.morphology(imgnolines2, 'erode', 2, 0)
        #imgnolines1 = self.morphology(imgnolines1, 'dilate', 0, 2)
        imgnolines2.filter_short_runs(15, u'black')
        imgnolines2.filter_wide_runs(mostfrequentrun, u'black')
        imgnolines2 = self.morphology(imgnolines2, 'dilate', 0, 2)
        imgnolines = imgnolines1.or_image(imgnolines2)
        #imgnolines = imgnolines1
        #imgnolines = self.morphology(imgnolines, 'dilate', 0, 6)
        #imgnolines = self.morphology(imgnolines, 'erode', 0, 5)
        #imgnolines.filter_wide_runs(mostfrequentrun + 2, u'black')
        #imgnolines.filter_short_runs(15, u'black')
        #imgdel = self.filterhorver1(img)
        #imgdel = self.morphology(imgdel, 'dilate', 2, 0)
        #imgdel = self.morphology(imgdel, 'erode', 0, 3)
        #imgdel.invert()
        #imgnolines = imgnolines.and_image(imgdel)
        #imgnolines.filter_short_runs(15, u'black')
        #imgnolines = self.morphology(imgnolines, 'erode', 1, 0)
        imgnolines.despeckle(8)
        return imgnolines.image_copy()


    def filterhorver1(self, img):
        """Filtrace linek pomoci RLE
        """
        imgnolines1 = img.image_copy()
        imgnolines2 = img.image_copy()
        imgnolines1.filter_short_runs(10, u'black')
        imgnolines2.filter_wide_runs(10, u'black')
        imgnolines = imgnolines1.or_image(imgnolines2)
        imgnolines = self.morphology(imgnolines, 'open', 1, 1)
        imgnolines = self.morphology(imgnolines, 'close', 1, 1)
        imgnolines.filter_narrow_runs(10, u'black')
        imgnolines.despeckle(8)
        imgnolines = img.and_image(imgnolines)
        return imgnolines.image_copy()

    def filterlines(self, img):
        """Filtrace linek not morf. operacemi
        """
        temp = img.erode_with_structure(self.structure4, Point(6, 0))
        temp = temp.erode_with_structure(self.structure1, Point(1, 0))
        temp = temp.erode_with_structure(self.structure1, Point(1, 0))
        temp = temp.erode_with_structure(self.structure1, Point(1, 0))
        temp = temp.dilate_with_structure(self.structure1, Point(1, 0))
        temp = temp.dilate_with_structure(self.structure1, Point(1, 0))
        temp = temp.dilate_with_structure(self.structure1, Point(1, 0))
        temp = temp.dilate_with_structure(self.structure4, Point(6,0))
        return temp.image_copy()

    def getheads(self, img):
        """Filtrace hlavicek not morf. operacemi
        """
        temp = img.erode_with_structure(self.structure5, Point(0, 6))
        temp = temp.erode_with_structure(self.structure4, Point(6, 0))
        temp = temp.erode_with_structure(self.structure1, Point(1, 0))
        temp = temp.erode_with_structure(self.structure1, Point(1, 0))
        temp = temp.erode_with_structure(self.structure1, Point(1, 0))
        temp = temp.dilate_with_structure(self.structure1, Point(1, 0))
        temp = temp.dilate_with_structure(self.structure1, Point(1, 0))
        temp = temp.dilate_with_structure(self.structure1, Point(1, 0))
        temp = temp.dilate_with_structure(self.structure4, Point(6,0))
        temp = temp.dilate_with_structure(self.structure3, Point(0, 1))
        temp = temp.dilate_with_structure(self.structure3, Point(0, 1))
        temp = temp.dilate_with_structure(self.structure3, Point(0, 1))
        return temp.image_copy()

    def morphology(self, img, func, i, j):
        """Univerzalni morfologicka funkce
        """
        structurex = Image(Point(0, 0), Point(i*2, j*2), ONEBIT)
        pointx = Point(i,j)
        structurex.fill(1)
        if(func == 'dilate'):
            temp = img.dilate_with_structure(structurex, pointx)
        elif(func == 'erode'):
            temp = img.erode_with_structure(structurex, pointx)
        elif(func == 'open'):
            temp = img.dilate_with_structure(structurex, pointx)
            temp = temp.erode_with_structure(structurex, pointx)
        elif (func == 'close'):
            temp = img.erode_with_structure(structurex, pointx)
            temp = temp.dilate_with_structure(structurex, pointx)
        else:
            temp = img
        return temp.image_copy()


    def getlines(self, img, imgerase):
        """Ziskani linek s odstranenim malych komponent
        """
        temp = self.eraser(img, imgerase)
        temp.despeckle(10)
        return temp.image_copy()

    def eraser(self, img, imgerase):
        """Odecteni dvou obrazu
        """
        temp = imgerase.image_copy()
        temp.invert()
        temp = img.and_image(temp)
        return temp.image_copy()


    def filterhor2(self, img):
        """Filtrace malych useku horizontalnich linek pomoci RLE
        """
        imgnolines1 = img.image_copy()
        imgnolines2 = img.image_copy()
        imgnolines1.filter_short_runs(10, u'black')
        imgnolines2.filter_wide_runs(10, u'black')
        imgnolines = imgnolines1.or_image(imgnolines2)
        return imgnolines.image_copy()

    def deletelines(self, img):
        """Odstraneni linek pomoci morfologie s ctvercovym s.e.
        """
        temp = img.erode_with_structure(self.structure2, Point(2, 2))
        temp = temp.erode_with_structure(self.structure2, Point(2, 2))
        temp = temp.erode_with_structure(self.structure2, Point(2, 2))
        temp = temp.dilate_with_structure(self.structure2, Point(2, 2))
        return temp.image_copy()