ó
î Xc           @   s\   d  d l  m Z d  d l  m Z d  d l Z d
 Z d d d „  ƒ  YZ d d d	 „  ƒ  YZ d S(   iÿÿÿÿ(   t   Image(   t   _imagingmorphNi   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(    (    s2   /usr/lib/python2.7/dist-packages/PIL/ImageMorph.pyt   __init__2   s(    		




c         C   s   |  j  | 7_  d  S(   N(   R   (   R   R   (    (    s2   /usr/lib/python2.7/dist-packages/PIL/ImageMorph.pyt   add_patternsK   s    c         C   sL   d d g } d } t  g  t t ƒ D] } | | | @d k ^ q" ƒ |  _ d  S(   Ni    i   i   i   (   t	   bytearrayt   ranget   LUT_SIZER   (   R   t   symbolst   mt   i(    (    s2   /usr/lib/python2.7/dist-packages/PIL/ImageMorph.pyt   build_default_lutN   s    c         C   s   |  j  S(   N(   R   (   R   (    (    s2   /usr/lib/python2.7/dist-packages/PIL/ImageMorph.pyt   get_lutS   s    c         C   s<   t  | ƒ d k s t ‚ d j g  | D] } | | ^ q% ƒ S(   s„   string_permute takes a pattern and a permutation and returns the
        string permuted according to the permutation list.
        i	   t    (   t   lent   AssertionErrort   join(   R   t   patternt   permutationt   p(    (    s2   /usr/lib/python2.7/dist-packages/PIL/ImageMorph.pyt   _string_permuteV   s    c   	      C   s†  | | f g } d | k r… | d d } xY t  d ƒ D]H } | j |  j | d d d d d d d d d	 d
 d g	 ƒ | f ƒ q6 Wn  d | k rø t | ƒ } xX | d | !D]F \ } } | j |  j | d d d d
 d d d	 d d g	 ƒ | f ƒ q« Wn  d | k r‚t | ƒ } xo | d | !D]] \ } } | j d d ƒ j d d ƒ j d 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    i   i   i   i   i   i   t   Mt   Nt   0t   Zt   1s   %d(   R   t   appendR"   R   t   replacet   int(	   R   t   basic_patternt   optionst   basic_resultR   t   resR   t   nR   (    (    s2   /usr/lib/python2.7/dist-packages/PIL/ImageMorph.pyt   _pattern_permute]   s2    	 	 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 Wxg t	 t
 | ƒ ƒ D]S } | | 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 | ƒ r‚d
 d g |	 |  j | <q‚q‚Wq@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+   R1   R   R   t   compileR   t   bint   matchR   (
   R   R   R!   R   R-   R   t   resultR   t
   bitpatternt   r(    (    s2   /usr/lib/python2.7/dist-packages/PIL/ImageMorph.pyt	   build_lut‚   s.    
&%"N(   t   __name__t
   __module__t   __doc__R
   R   R   R   R   R"   R1   R?   (    (    (    s2   /usr/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   R?   (   R   R   R   R   (    (    s2   /usr/lib/python2.7/dist-packages/PIL/ImageMorph.pyR   ¶   s
    	c         C   sm   |  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 loadedN(   R   R
   R   R    t   newt   modet   sizeR   t   applyt   bytest   imt   id(   R   t   imaget   outimaget   count(    (    s2   /usr/lib/python2.7/dist-packages/PIL/ImageMorph.pyRG   Á   s    $c         C   s=   |  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.s   No operator loadedN(   R   R
   R   R   R;   RH   RI   RJ   (   R   RK   (    (    s2   /usr/lib/python2.7/dist-packages/PIL/ImageMorph.pyR;   Î   s    c         C   s   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.(   R   t   get_on_pixelsRI   RJ   (   R   RK   (    (    s2   /usr/lib/python2.7/dist-packages/PIL/ImageMorph.pyRN   Ù   s    c         C   s^   t  | d ƒ  } t | j ƒ  ƒ |  _ Wd QXt |  j ƒ d k rZ d |  _ t d ƒ ‚ n  d S(   s!   Load an operator from an mrl filet   rbNi    s   Wrong size operator file!(   t   openR   t   readR   R   R
   R   (   R   t   filenamet   f(    (    s2   /usr/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   RP   t   write(   R   RR   RS   (    (    s2   /usr/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   (    (    s2   /usr/lib/python2.7/dist-packages/PIL/ImageMorph.pyt   set_lutñ   s    N(   R@   RA   RB   R
   R   RG   R;   RN   RT   RW   RX   (    (    (    s2   /usr/lib/python2.7/dist-packages/PIL/ImageMorph.pyRC   ³   s   						i   (    (    (   t   PILR    R   R6   R   R   RC   (    (    (    s2   /usr/lib/python2.7/dist-packages/PIL/ImageMorph.pyt   <module>   s
   ¤