ó
È4ec        	   @  sª   d  d l  m Z d  d l Z d d l m Z m Z d Z d d d d	 d
 d d d d g	 Z d d d d d
 d d d	 d g	 Z d e	 f d „  ƒ  YZ
 d e	 f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   print_functionNi   (   t   Imaget   _imagingmorphi	   i   i   i    i   i   i   i   i   t
   LutBuilderc           B  sS   e  Z d  Z d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   s~  A class for building a MorphLut from a descriptive language

      The input patterns is a list of a strings sequences like these::

          4:(...
             .1.
             111)->1

      (whitespaces including linebreaks are ignored). The option 4
      describes a series of symmetry operations (in this case a
      4-rotation), the pattern is described by:

      - . or X - Ignore
      - 1 - Pixel is on
      - 0 - Pixel is off

      The result of the operation is described after "->" string.

      The default is to return the current pixel value, which is
      returned if no other match is found.

      Operations:

      - 4 - 4 way rotation
      - N - Negate
      - 1 - Dummy op for no other operation (an op must always be given)
      - M - Mirroring

      Example::

          lb = LutBuilder(patterns = ["4:(... .1. 111)->1"])
          lut = lb.build_lut()

    c         C  s¾   | d  k	 r | |  _ n	 g  |  _ d  |  _ | d  k	 rº i d d g d 6d g d 6d d g d 6d g d	 6d d
 g d 6d d d g d 6} | | k rª t d | d ƒ ‚ n  | | |  _ n  d  S(   Ns   1:(... ... ...)->0s   4:(00. 01. ...)->1t   corners   4:(... .0. .1.)->1t	   dilation4s   4:(... .0. ..1)->1t	   dilation8s   4:(... .1. .0.)->0t   erosion4s   4:(... .1. ..0)->0t   erosion8s   4:(.0. .1. ...)->1s   4:(01. .1. ...)->1t   edges   Unknown pattern t   !(   t   Nonet   patternst   lutt	   Exception(   t   selfR   t   op_namet   known_patterns(    (    s8   /usr/local/lib/python2.7/dist-packages/PIL/ImageMorph.pyt   __init__B   s"    		

c         C  s   |  j  | 7_  d  S(   N(   R   (   R   R   (    (    s8   /usr/local/lib/python2.7/dist-packages/PIL/ImageMorph.pyt   add_patternsZ   s    c           s>   d d g ‰ d ‰  t  ‡  ‡ f d †  t t ƒ Dƒ ƒ |  _ d  S(   Ni    i   i   c         3  s#   |  ] } ˆ | ˆ  @d  k Vq d S(   i    N(    (   t   .0t   i(   t   mt   symbols(    s8   /usr/local/lib/python2.7/dist-packages/PIL/ImageMorph.pys	   <genexpr>`   s    i   (   t	   bytearrayt   ranget   LUT_SIZER   (   R   (    (   R   R   s8   /usr/local/lib/python2.7/dist-packages/PIL/ImageMorph.pyt   build_default_lut]   s    c         C  s   |  j  S(   N(   R   (   R   (    (    s8   /usr/local/lib/python2.7/dist-packages/PIL/ImageMorph.pyt   get_lutb   s    c           s5   t  | ƒ d k s t ‚ d j ‡  f d †  | Dƒ ƒ S(   s„   string_permute takes a pattern and a permutation and returns the
        string permuted according to the permutation list.
        i	   t    c         3  s   |  ] } ˆ  | Vq d  S(   N(    (   R   t   p(   t   pattern(    s8   /usr/local/lib/python2.7/dist-packages/PIL/ImageMorph.pys	   <genexpr>j   s    (   t   lent   AssertionErrort   join(   R   R   t   permutation(    (   R   s8   /usr/local/lib/python2.7/dist-packages/PIL/ImageMorph.pyt   _string_permutee   s    c   	      C  sL  | | f g } d | k rj | d d } x> t  d ƒ D]- } | j |  j | d d t ƒ | f ƒ q6 Wn  d | k rÂ t | ƒ } x= | d | !D]+ \ } } | j |  j | t ƒ | f ƒ q Wn  d | k rHt | ƒ } xk | d | !D]Y \ } } | j d d	 ƒ j d
 d ƒ j d	 d
 ƒ } d t | ƒ } | j | | f ƒ qè Wn  | S(   sÉ   pattern_permute takes a basic pattern and its result and clones
        the pattern according to the modifications described in the $options
        parameter. It returns a list of all cloned patterns.t   4iÿÿÿÿi   i   i    t   Mt   Nt   0t   Zt   1(   R   t   appendR$   t   ROTATION_MATRIXR    t   MIRROR_MATRIXt   replacet   int(	   R   t   basic_patternt   optionst   basic_resultR   t   resR   t   nR   (    (    s8   /usr/local/lib/python2.7/dist-packages/PIL/ImageMorph.pyt   _pattern_permutel   s"    (&*c   
      C  s¾  |  j  ƒ  g  } x¶ |  j D]« } t j d | j d d ƒ ƒ } | s[ t d | d ƒ ‚ n  | j d ƒ } | j d ƒ } t | j d ƒ ƒ } | j d	 d ƒ j d d ƒ } | |  j | | | ƒ 7} q Wx_ t	 | ƒ D]Q \ } } | d
 j d d ƒ j d d ƒ } t j
 | ƒ } | | d f | | <qÖ Wx‰ t t ƒ D]{ } t | ƒ d } d d t | ƒ | d d d … } x= | D]5 \ } }	 | j | ƒ rzd
 d g |	 |  j | <qzqzWq8W|  j S(   sl   Compile all patterns into a morphology lut.

        TBD :Build based on (file) morphlut:modify_lut
        s   (\w*):?\s*\((.+?)\)\s*->\s*(\d)s   
R   s   Syntax error in pattern "t   "i   i   i   t    i    t   .t   Xs   [01]R(   i	   Niÿÿÿÿ(   R   R   t   ret   searchR.   R   t   groupR/   R5   t	   enumeratet   compileR   R   t   binR    t   matchR   (
   R   R   R   R   R1   R   t   resultR   t
   bitpatternt   r(    (    s8   /usr/local/lib/python2.7/dist-packages/PIL/ImageMorph.pyt	   build_lutŠ   s,    
"%"N(   t   __name__t
   __module__t   __doc__R   R   R   R   R   R$   R5   RD   (    (    (    s8   /usr/local/lib/python2.7/dist-packages/PIL/ImageMorph.pyR      s   "					t   MorphOpc           B  sV   e  Z d  Z d d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   s*   A class for binary morphological operatorsc         C  s[   | |  _  | d k	 r0 t d | ƒ j ƒ  |  _  n' | d k	 rW t d | ƒ j ƒ  |  _  n  d S(   s&   Create a binary morphological operatorR   R   N(   R   R   R   RD   (   R   R   R   R   (    (    s8   /usr/local/lib/python2.7/dist-packages/PIL/ImageMorph.pyR   ¸   s
    	c         C  s‹   |  j  d k r t d ƒ ‚ n  | j d k r< t d ƒ ‚ n  t j | j | j d ƒ } t j t	 |  j  ƒ | j
 j | j
 j ƒ } | | f S(   s‡   Run a single morphological operation on an image

        Returns a tuple of the number of changed pixels and the
        morphed images   No operator loadedt   Ls0   Image must be binary, meaning it must use mode LN(   R   R   R   t   modeR   t   newt   sizeR   t   applyt   bytest   imt   id(   R   t   imaget   outimaget   count(    (    s8   /usr/local/lib/python2.7/dist-packages/PIL/ImageMorph.pyRM   À   s    *c         C  s[   |  j  d k r t d ƒ ‚ n  | j d k r< t d ƒ ‚ n  t j t |  j  ƒ | j j ƒ S(   sÈ   Get a list of coordinates matching the morphological operation on
        an image.

        Returns a list of tuples of (x,y) coordinates
        of all matching pixels. See :ref:`coordinate-system`.s   No operator loadedRI   s0   Image must be binary, meaning it must use mode LN(	   R   R   R   RJ   R   R@   RN   RO   RP   (   R   RQ   (    (    s8   /usr/local/lib/python2.7/dist-packages/PIL/ImageMorph.pyR@   Î   s
    c         C  s1   | j  d k r t d ƒ ‚ n  t j | j j ƒ S(   s©   Get a list of all turned on pixels in a binary image

        Returns a list of tuples of (x,y) coordinates
        of all matching pixels. See :ref:`coordinate-system`.RI   s0   Image must be binary, meaning it must use mode L(   RJ   R   R   t   get_on_pixelsRO   RP   (   R   RQ   (    (    s8   /usr/local/lib/python2.7/dist-packages/PIL/ImageMorph.pyRT   Û   s    c         C  s^   t  | d ƒ  } t | j ƒ  ƒ |  _ Wd QXt |  j ƒ t k rZ d |  _ t d ƒ ‚ n  d S(   s!   Load an operator from an mrl filet   rbNs   Wrong size operator file!(   t   openR   t   readR   R    R   R   R   (   R   t   filenamet   f(    (    s8   /usr/local/lib/python2.7/dist-packages/PIL/ImageMorph.pyt   load_lutå   s
    	c         C  sJ   |  j  d k r t d ƒ ‚ n  t | d ƒ  } | j |  j  ƒ Wd QXd S(   s   Save an operator to an mrl files   No operator loadedt   wbN(   R   R   R   RV   t   write(   R   RX   RY   (    (    s8   /usr/local/lib/python2.7/dist-packages/PIL/ImageMorph.pyt   save_lutî   s    c         C  s   | |  _  d S(   s#   Set the lut from an external sourceN(   R   (   R   R   (    (    s8   /usr/local/lib/python2.7/dist-packages/PIL/ImageMorph.pyt   set_lutõ   s    N(   RE   RF   RG   R   R   RM   R@   RT   RZ   R]   R^   (    (    (    s8   /usr/local/lib/python2.7/dist-packages/PIL/ImageMorph.pyRH   µ   s   			
			i   (   t
   __future__R    R:   R   R   R   R   R,   R-   t   objectR   RH   (    (    (    s8   /usr/local/lib/python2.7/dist-packages/PIL/ImageMorph.pyt   <module>   s   				—