o
    ]oiL                     @  sX   d dl mZ d dlZddlmZmZ dZg dZg dZG dd	 d	Z	G d
d dZ
dS )    )annotationsN   )Image_imagingmorphi   )	      r         r            )	r   r   r   r   r	   r   r
   r   r   c                   @  J   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dS )
LutBuilderaT  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()

    Nc                 C  sx   |d ur|| _ ng | _ d | _|d ur:ddgdgddgdgddgg dd}||vr3d	| d
 }t||| | _ d S d S )N1:(... ... ...)->0z4:(00. 01. ...)->1z4:(... .0. .1.)->1z4:(... .0. ..1)->1z4:(... .1. .0.)->0z4:(... .1. ..0)->0)r   z4:(.0. .1. ...)->1z4:(01. .1. ...)->1)ZcornerZ	dilation4Z	dilation8Zerosion4Zerosion8edgezUnknown pattern !)patternslut	Exception)selfr   op_nameZknown_patternsmsg r   V/var/www/html/kuke-dev/django-kuke/venv/lib/python3.10/site-packages/PIL/ImageMorph.py__init__A   s"   zLutBuilder.__init__c                 C  s   |  j |7  _ d S Nr   )r   r   r   r   r   add_patternsZ   s   zLutBuilder.add_patternsc                   s.   ddgd t  fddttD | _d S )Nr   r      c                 3  s     | ]}| @ d k V  qdS )r   Nr   ).0imsymbolsr   r   	<genexpr>`   s    z/LutBuilder.build_default_lut.<locals>.<genexpr>)	bytearrayrangeLUT_SIZEr   r   r   r!   r   build_default_lut]   s   "zLutBuilder.build_default_lutc                 C  s   | j S r   r   r(   r   r   r   get_lutb   s   zLutBuilder.get_lutc                   s(   t |dksJ d fdd|D S )zstring_permute takes a pattern and a permutation and returns the
        string permuted according to the permutation list.
        	    c                 3  s    | ]} | V  qd S r   r   )r   ppatternr   r   r$   j   s    z-LutBuilder._string_permute.<locals>.<genexpr>)lenjoin)r   r0   Zpermutationr   r/   r   _string_permutee   s   zLutBuilder._string_permutec           	      C  s   ||fg}d|v r%|d d }t dD ]}|| |d d t|f qd|v rCt|}|d| D ]\}}|| |t|f q3d|v rqt|}|d| D ]\}}|d	d
dd	d
d}dt| }|||f qQ|S )zpattern_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.4r   r	   r   MNN0Z1)r&   appendr3   ROTATION_MATRIXr1   MIRROR_MATRIXreplaceint)	r   Zbasic_patternoptionsZbasic_resultr   resr    nr0   r   r   r   _pattern_permutel   s$   
zLutBuilder._pattern_permutec                 C  s<  |    g }| jD ]<}td|dd}|s!d| d }t||d}|d}t|d}|d	ddd}|| |||7 }q	t	|D ]\}}|d
 dddd}t
|}||d f||< qJttD ].}t|dd }	ddt|	  |	 ddd }	|D ]\}}
||	rd
dg|
 | j|< qql| jS )zlCompile all patterns into a morphology lut.

        TBD :Build based on (file) morphlut:modify_lut
        z(\w*):?\s*\((.+?)\)\s*->\s*(\d)
r-   zSyntax error in pattern ""r   r   r    r   .Xz[01]Nr8   r,   r5   )r)   r   researchr>   r   groupr?   rC   	enumeratecompiler&   r'   binr1   matchr   )r   r   r.   r"   r   r@   r0   resultr    Z
bitpatternrr   r   r   	build_lut   s2   




zLutBuilder.build_lut)NN)__name__
__module____qualname____doc__r   r   r)   r+   r3   rC   rR   r   r   r   r   r      s    
#r   c                   @  r   )MorphOpz*A class for binary morphological operatorsNc                 C  sB   || _ |durt|d | _ dS |durt|d | _ dS dS )z&Create a binary morphological operatorN)r   r   )r   r   rR   )r   r   r   r   r   r   r   r      s   zMorphOp.__init__c                 C  sb   | j du rd}t||jdkrd}t|t|j|jd}tt	| j |j
j|j
j}||fS )zRun a single morphological operation on an image

        Returns a tuple of the number of changed pixels and the
        morphed imageNNo operator loadedLImage mode must be L)r   r   mode
ValueErrorr   newsizer   applybytesimid)r   imager   Zoutimagecountr   r   r   r_      s   

zMorphOp.applyc                 C  sB   | j du rd}t||jdkrd}t|tt| j |jjS )zGet 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`.NrX   rY   rZ   )	r   r   r[   r\   r   rO   r`   ra   rb   r   rc   r   r   r   r   rO      s   

zMorphOp.matchc                 C  s$   |j dkrd}t|t|jjS )zGet 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`.rY   rZ   )r[   r\   r   get_on_pixelsra   rb   re   r   r   r   rf      s   
zMorphOp.get_on_pixelsc                 C  s\   t |d}t| | _W d   n1 sw   Y  t| jtkr,d| _d}t|dS )z!Load an operator from an mrl filerbNzWrong size operator file!)openr%   readr   r1   r'   r   )r   filenamefr   r   r   r   load_lut   s   zMorphOp.load_lutc                 C  sR   | j du rd}t|t|d}|| j  W d   dS 1 s"w   Y  dS )zSave an operator to an mrl fileNrX   wb)r   r   rh   write)r   rj   r   rk   r   r   r   save_lut   s   
"zMorphOp.save_lutc                 C  s
   || _ dS )z#Set the lut from an external sourceNr*   )r   r   r   r   r   set_lut   s   
zMorphOp.set_lut)NNN)rS   rT   rU   rV   r   r_   rO   rf   rl   ro   rp   r   r   r   r   rW      s    

rW   )
__future__r   rI   r-   r   r   r'   r<   r=   r   rW   r   r   r   r   <module>   s    