HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/env/lib/python3.10/site-packages/PIL/__pycache__/ImageMorph.cpython-310.pyc
o

we)�@sLddlZddlmZmZdZgd�Zgd�ZGdd�d�ZGd	d
�d
�ZdS)�N�)�Image�
_imagingmorphi)	��r��r���)	rrrr
rrr	rrc@�JeZdZdZddd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dS)�
LutBuilderaTA 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()

    NcCsx|dur||_ng|_d|_|dur:ddgdgddgdgddggd�d�}||vr3d	|d
}t|��|||_dSdS)N�1:(... ... ...)->0z4:(00. 01. ...)->1z4:(... .0. .1.)->1z4:(... .0. ..1)->1z4:(... .1. .0.)->0z4:(... .1. ..0)->0)rz4:(.0. .1. ...)->1z4:(01. .1. ...)->1)�corner�	dilation4�	dilation8�erosion4�erosion8�edgezUnknown pattern �!)�patterns�lut�	Exception)�selfr�op_name�known_patterns�msg�r�G/home/arjun/projects/env/lib/python3.10/site-packages/PIL/ImageMorph.py�__init__@s"��zLutBuilder.__init__cCs|j|7_dS�N�r)rrrrr�add_patternsYszLutBuilder.add_patternscs.ddg�d�t��fdd�tt�D��|_dS)Nrr�c3s �|]}�|�@dkVqdS)rNr)�.0�i��m�symbolsrr�	<genexpr>_s�z/LutBuilder.build_default_lut.<locals>.<genexpr>)�	bytearray�range�LUT_SIZEr�rrr&r�build_default_lut\s"zLutBuilder.build_default_lutcCs|jSr �rr-rrr�get_lutaszLutBuilder.get_lutcs(t|�dksJ�d��fdd�|D��S)z�string_permute takes a pattern and a permutation and returns the
        string permuted according to the permutation list.
        �	�c3s�|]}�|VqdSr r)r$�p��patternrrr)is�z-LutBuilder._string_permute.<locals>.<genexpr>)�len�join)rr5�permutationrr4r�_string_permutedszLutBuilder._string_permutec	Cs�||fg}d|vr%|dd}td�D]}|�|�|ddt�|f�qd|vrCt|�}|d|�D]\}}|�|�|t�|f�q3d|vrqt|�}|d|�D]\}}|�d	d
��dd	��d
d�}dt|�}|�||f�qQ|S)z�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.�4���rrr�MN�N�0�Z�1)r+�appendr9�ROTATION_MATRIXr6�
MIRROR_MATRIX�replace�int)	r�
basic_pattern�options�basic_resultr�resr%�nr5rrr�_pattern_permuteks$
�zLutBuilder._pattern_permutecCs<|��g}|jD]<}t�d|�dd��}|s!d|d}t|��|�d�}|�d�}t|�d��}|�d	d��dd�}||�|||�7}q	t	|�D]\}}|d
�dd��dd
�}t�
|�}||df||<qJtt�D].}t
|�dd�}	ddt|	�|	ddd�}	|D]\}}
|�|	�r�d
dg|
|j|<q�ql|jS)zlCompile all patterns into a morphology lut.

        TBD :Build based on (file) morphlut:modify_lut
        z(\w*):?\s*\((.+?)\)\s*->\s*(\d)�
r2zSyntax error in pattern "�"rrr� r�.�Xz[01]Nr>r1r;)r.r�re�searchrDr�grouprErK�	enumerate�compiler+r,�binr6�matchr)rrr3r'rrGr5�resultr%�
bitpattern�rrrr�	build_lut�s2




��zLutBuilder.build_lut)NN)�__name__�
__module__�__qualname__�__doc__rr"r.r0r9rKr[rrrrr
s
#r
c@r)�MorphOpz*A class for binary morphological operatorsNcCsB||_|durt|d���|_dS|durt|d���|_dSdS)z&Create a binary morphological operatorN)rr!)rr
r[)rrrrrrrr�s�zMorphOp.__init__cCsb|jdurd}t|��|jdkrd}t|��t�|j|jd�}t�t	|j�|j
j|j
j�}||fS)z�Run 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)rr�mode�
ValueErrorr�new�sizer�apply�bytes�im�id)r�imager�outimage�countrrrrh�s

z
MorphOp.applycCsB|jdurd}t|��|jdkrd}t|��t�t|j�|jj�S)z�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`.Nrarbrc)	rrrdrerrWrirjrk�rrlrrrrrW�s

z
MorphOp.matchcCs$|jdkrd}t|��t�|jj�S)z�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`.rbrc)rdrer�
get_on_pixelsrjrkrorrrrp�s
zMorphOp.get_on_pixelscCs\t|d��}t|���|_Wd�n1swYt|j�tkr,d|_d}t|��dS)z!Load an operator from an mrl file�rbNzWrong size operator file!)�openr*�readrr6r,r)r�filename�frrrr�load_lut�s��zMorphOp.load_lutcCsR|jdurd}t|��t|d��}|�|j�Wd�dS1s"wYdS)zSave an operator to an mrl fileNra�wb)rrrr�write)rrtrrurrr�save_lut�s
"�zMorphOp.save_lutcCs
||_dS)z#Set the lut from an external sourceNr/)rrrrr�set_lut�s
zMorphOp.set_lut)NNN)r\r]r^r_rrhrWrprvryrzrrrrr`�s

r`)	rQr2rrr,rBrCr
r`rrrr�<module>s