§
    ¢”jt(  ã                  ór   — 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	¦  «        Z	 G d
„ d¦  «        Z
dS )é    )ÚannotationsNé   )ÚImageÚ_imagingmorphi   )	é   é   r   é   é   r   é   é   é   )	r   r   r   r   r
   r   r   r	   r   c                  óN   — 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()

    NÚpatternsúlist[str] | NoneÚop_nameú
str | NoneÚreturnÚNonec                ó²   — d| _         |�;ddgdgddgdgddgg d¢d	œ}||vrd
|› d�}t          |¦  «        ‚||         | _        dS |�	|| _        dS g | _        dS )a  
        :param patterns: A list of input patterns, or None.
        :param op_name: The name of a known pattern. One of "corner", "dilation4",
           "dilation8", "erosion4", "erosion8" or "edge".
        :exception Exception: If the op_name is not recognized.
        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)ÚcornerÚ	dilation4Ú	dilation8Úerosion4Úerosion8ÚedgezUnknown pattern ú!)ÚlutÚ	Exceptionr   )Úselfr   r   Úknown_patternsÚmsgs        ú]/var/www/finuniver-perm.ru/html/portfolio/venv/lib/python3.11/site-packages/PIL/ImageMorph.pyÚ__init__zLutBuilder.__init__A   s¨   € ð &*ˆŒØÐà/Ð1EÐFØ2Ð3Ø2Ð4HÐIØ1Ð2Ø1Ð3GÐHðð ð ðð ˆNð ˜nÐ,Ð,Ø3¨Ð3Ð3Ð3�Ý ‘n”nÐ$à*¨7Ô3ˆDŒMˆMˆMØÐ!Ø$ˆDŒMˆMˆMàˆDŒMˆMˆMó    ú	list[str]c                ó&   — | xj         |z  c_         dS )z\
        Append to list of patterns.

        :param patterns: Additional patterns.
        N)r   )r!   r   s     r$   Úadd_patternszLutBuilder.add_patternsb   s   € ð 	ˆŒ˜Ñ!ˆŒˆˆr&   Ú	bytearrayc                ó†   ‡‡— ddgŠdŠt          ˆˆfd„t          t          ¦  «        D ¦   «         ¦  «        | _        | j        S )z‹
        Set the current LUT, and return it.

        This is the default LUT that patterns will be applied against when building.
        r   r   é   c              3  ó6   •K  — | ]}‰|‰z  d k             V — ŒdS )r   N© )Ú.0ÚiÚmÚsymbolss     €€r$   ú	<genexpr>z/LutBuilder.build_default_lut.<locals>.<genexpr>r   s0   øè è € ÐKÐK°a˜W a¨!¡e¨q¢[Ô1ÐKÐKÐKÐKÐKÐKr&   )r*   ÚrangeÚLUT_SIZEr   )r!   r1   r2   s    @@r$   Úbuild_default_lutzLutBuilder.build_default_lutj   sH   øø€ ð �a�&ˆØˆÝÐKÐKÐKÐKÐK½5Å¹?¼?ÐKÑKÔKÑKÔKˆŒØŒxˆr&   úbytearray | Nonec                ó   — | j         S )z)
        Returns the current LUT
        ©r   )r!   s    r$   Úget_lutzLutBuilder.get_lutu   s   € ð Œxˆr&   ÚpatternÚstrÚpermutationú	list[int]c                óp   ‡— t          |¦  «        dk    sJ ‚d                     ˆfd„|D ¦   «         ¦  «        S )zuTakes a pattern and a permutation and returns the
        string permuted according to the permutation list.
        é	   Ú c              3  ó(   •K  — | ]}‰|         V — Œd S )Nr.   )r/   Úpr;   s     €r$   r3   z-LutBuilder._string_permute.<locals>.<genexpr>€   s'   øè è € Ð7Ð7 a�w˜q”zÐ7Ð7Ð7Ð7Ð7Ð7r&   )ÚlenÚjoin)r!   r;   r=   s    ` r$   Ú_string_permutezLutBuilder._string_permute{   sE   ø€ õ �;ÑÔ 1Ò$Ð$Ð$Ð$Ø�wŠwÐ7Ð7Ð7Ð7¨;Ð7Ñ7Ô7Ñ7Ô7Ð7r&   Úbasic_patternÚoptionsÚbasic_resultÚintúlist[tuple[str, int]]c                óŠ  — ||fg}d|v r\|d         d         }t          d¦  «        D ]>}|                     |                      |d         d         t          ¦  «        |f¦  «         Œ?d|v rOt	          |¦  «        }|d|…         D ]5\  }}|                     |                      |t
          ¦  «        |f¦  «         Œ6d|v r†t	          |¦  «        }|d|…         D ]l\  }}|                     d	d
¦  «                             dd	¦  «                             d
d¦  «        }dt          |¦  «        z
  }|                     ||f¦  «         Œm|S )z¹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éÿÿÿÿr   r
   r   ÚMNÚNÚ0ÚZÚ1)r4   ÚappendrF   ÚROTATION_MATRIXrD   ÚMIRROR_MATRIXÚreplacerJ   )	r!   rG   rH   rI   r   Úresr0   Únr;   s	            r$   Ú_pattern_permutezLutBuilder._pattern_permute‚   sh  € ð # LÐ1Ð2ˆð �'ˆ>ˆ>Ø˜2”,˜q”/ˆCÝ˜1‘X”Xð ð �Ø—’Ø×)Ò)¨(°2¬,°q¬/½?ÑKÔKÈSÐQñô ð ð ð �'ˆ>ˆ>Ý�H‘”ˆAØ (¨¨!¨¤ð Uð U‘�˜Ø—’ ×!5Ò!5°g½}Ñ!MÔ!MÈsÐ SÑTÔTÐTÐTð �'ˆ>ˆ>Ý�H‘”ˆAØ (¨¨!¨¤ð 0ð 0‘�˜à!Ÿ/š/¨#¨sÑ3Ô3×;Ò;¸CÀÑEÔE×MÒMÈcÐSVÑWÔW�Ø�#˜c™(œ(‘l�Ø—’ ¨# Ñ/Ô/Ð/Ð/àˆr&   c                óÎ  — |                       ¦   «          | j        €J ‚g }| j        D ]Ô}t          j        d|                     dd¦  «        ¦  «        }|sd|z   dz   }t          |¦  «        ‚|                     d¦  «        }|                     d¦  «        }t          |                     d	¦  «        ¦  «        }|                     d
d¦  «                             dd¦  «        }||  	                    |||¦  «        z  }ŒÕg }|D ]a}|d                              dd¦  «                             dd¦  «        }| 
                    t          j        |¦  «        |d         f¦  «         Œbt          t          ¦  «        D ]i}	t          |	¦  «        dd…         }
ddt          |
¦  «        z
  z  |
z   ddd…         }
|D ],\  }}|                     |
¦  «        rddg|         | j        |	<   Œ-Œj| j        S )zoCompile all patterns into a morphology LUT, and return it.

        This is the data to be passed into MorphOp.Nz(\w):?\s*\((.+?)\)\s*->\s*(\d)ú
rA   zSyntax error in pattern "ú"r   r   r   ú r   ú.ÚXz[01]rQ   r@   rN   )r6   r   r   ÚreÚsearchrW   r    ÚgrouprJ   rZ   rT   Úcompiler4   r5   ÚbinrD   Úmatch)r!   r   rC   r1   r#   rH   r;   ÚresultÚcompiled_patternsr0   Ú
bitpatternÚrs               r$   Ú	build_lutzLutBuilder.build_lut¢   sù  € ð 	×ÒÑ Ô Ð ØŒxÐ#Ð#Ð#Øˆð ”ð 	Hð 	HˆAÝ”	Ð;¸Q¿YºYÀtÈRÑ=PÔ=PÑQÔQˆAØð %Ø1°AÑ5¸Ñ;�Ý ‘n”nÐ$Ø—g’g˜a‘j”jˆGØ—g’g˜a‘j”jˆGÝ˜Ÿš ™œ‘_”_ˆFð —o’o c¨2Ñ.Ô.×6Ò6°t¸RÑ@Ô@ˆGà˜×-Ò-¨g°wÀÑGÔGÑGˆHˆHð ÐØð 	Bð 	BˆGØ˜”
×"Ò" 3¨Ñ,Ô,×4Ò4°S¸&ÑAÔAˆAØ×$Ò$¥b¤j°¡m¤m°W¸Q´ZÐ%@ÑAÔAÐAÐAõ •x‘”ð 	,ð 	,ˆAå˜Q™œ   œˆJØ ¥S¨¡_¤_Ñ!4Ñ5¸
ÑBÀDÀDÀbÀDÔIˆJà/ð ,ð ,‘
�˜Ø—=’= Ñ,Ô,ð ,Ø#$ a &¨¤)�D”H˜Q‘Køð,ð Œxˆr&   )NN)r   r   r   r   r   r   )r   r'   r   r   )r   r*   )r   r7   )r;   r<   r=   r>   r   r<   )rG   r<   rH   r<   rI   rJ   r   rK   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r%   r)   r6   r:   rF   rZ   rk   r.   r&   r$   r   r      s¹   € € € € € ð!ð !ðH HLðð ð ð ð ðB"ð "ð "ð "ð	ð 	ð 	ð 	ðð ð ð ð8ð 8ð 8ð 8ðð ð ð ð@(ð (ð (ð (ð (ð (r&   r   c                  óR   — 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 )ÚMorphOpz*A class for binary morphological operatorsNr   r7   r   r   r   r   r   r   c                ón   — |€|€	|| _         dS t          ||¦  «                             ¦   «         | _         dS )aÈ  Create a binary morphological operator.

        If the LUT is not provided, then it is built using LutBuilder from the op_name
        or the patterns.

        :param lut: The LUT data.
        :param patterns: A list of input patterns, or None.
        :param op_name: The name of a known pattern. One of "corner", "dilation4",
        "dilation8", "erosion4", "erosion8", "edge".
        :exception Exception: If the op_name is not recognized.
        N)r   r   rk   )r!   r   r   r   s       r$   r%   zMorphOp.__init__Ð   s:   € ð" Ð  ØˆDŒHˆHˆHå! (¨GÑ4Ô4×>Ò>Ñ@Ô@ˆDŒHˆHˆHr&   ÚimageúImage.Imageútuple[int, Image.Image]c                óD  — | j         €d}t          |¦  «        ‚|j        dvrd}t          |¦  «        ‚t	          j        |j        |j        ¦  «        }t          j        t          | j         ¦  «        | 
                    ¦   «         | 
                    ¦   «         ¦  «        }||fS )a9  Run a single morphological operation on an image.

        Returns a tuple of the number of changed pixels and the
        morphed image.

        :param image: A 1-mode or L-mode image.
        :exception Exception: If the current operator is None.
        :exception ValueError: If the image is not 1 or L mode.NúNo operator loaded©rS   ÚLúImage mode must be 1 or L)r   r    ÚmodeÚ
ValueErrorr   ÚnewÚsizer   ÚapplyÚbytesÚgetim)r!   rs   r#   ÚoutimageÚcounts        r$   r   zMorphOp.applyæ   sˆ   € ð Œ8ÐØ&ˆCÝ˜C‘.”.Ð àŒ:˜ZÐ'Ð'Ø-ˆCÝ˜S‘/”/Ð!Ý”9˜UœZ¨¬Ñ4Ô4ˆÝÔ#¥E¨$¬(¡O¤O°U·[²[±]´]ÀHÇNÂNÑDTÔDTÑUÔUˆØ�hˆÐr&   úlist[tuple[int, int]]c                óØ   — | j         €d}t          |¦  «        ‚|j        dvrd}t          |¦  «        ‚t	          j        t          | j         ¦  «        |                     ¦   «         ¦  «        S )ax  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`.

        :param image: A 1-mode or L-mode image.
        :exception Exception: If the current operator is None.
        :exception ValueError: If the image is not 1 or L mode.Nrw   rx   rz   )r   r    r{   r|   r   rf   r€   r�   ©r!   rs   r#   s      r$   rf   zMorphOp.matchú   s^   € ð Œ8ÐØ&ˆCÝ˜C‘.”.Ð àŒ:˜ZÐ'Ð'Ø-ˆCÝ˜S‘/”/Ð!ÝÔ"¥5¨¬¡?¤?°E·K²K±M´MÑBÔBÐBr&   c                ó‚   — |j         dvrd}t          |¦  «        ‚t          j        |                     ¦   «         ¦  «        S )a!  Get a list of all turned on pixels in a 1 or L mode image.

        Returns a list of tuples of (x,y) coordinates of all non-empty pixels. See
        :ref:`coordinate-system`.

        :param image: A 1-mode or L-mode image.
        :exception ValueError: If the image is not 1 or L mode.rx   rz   )r{   r|   r   Úget_on_pixelsr�   r†   s      r$   rˆ   zMorphOp.get_on_pixels  s;   € ð Œ:˜ZÐ'Ð'Ø-ˆCÝ˜S‘/”/Ð!ÝÔ*¨5¯;ª;©=¬=Ñ9Ô9Ð9r&   Úfilenamer<   c                ó  — t          |d¦  «        5 }t          |                     ¦   «         ¦  «        | _        ddd¦  «         n# 1 swxY w Y   t	          | j        ¦  «        t
          k    rd| _        d}t          |¦  «        ‚dS )z­
        Load an operator from an mrl file

        :param filename: The file to read from.
        :exception Exception: If the length of the file data is not 512.
        ÚrbNzWrong size operator file!)Úopenr*   Úreadr   rD   r5   r    )r!   r‰   Úfr#   s       r$   Úload_lutzMorphOp.load_lut  s²   € õ �(˜DÑ!Ô!ð 	+ QÝ  §¢¡¤Ñ*Ô*ˆDŒHð	+ð 	+ð 	+ñ 	+ô 	+ð 	+ð 	+ð 	+ð 	+ð 	+ð 	+øøøð 	+ð 	+ð 	+ð 	+õ ˆtŒx‰=Œ=�HÒ$Ð$ØˆDŒHØ-ˆCÝ˜C‘.”.Ð ð %Ð$s   ‘'AÁAÁAc                ó¼   — | j         €d}t          |¦  «        ‚t          |d¦  «        5 }|                     | j         ¦  «         ddd¦  «         dS # 1 swxY w Y   dS )z¡
        Save an operator to an mrl file.

        :param filename: The destination file.
        :exception Exception: If the current operator is None.
        Nrw   Úwb)r   r    rŒ   Úwrite)r!   r‰   r#   rŽ   s       r$   Úsave_lutzMorphOp.save_lut*  s¢   € ð Œ8ÐØ&ˆCÝ˜C‘.”.Ð Ý�(˜DÑ!Ô!ð 	 QØ�GŠG�D”HÑÔÐð	ð 	ð 	ñ 	ô 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	øøøð 	ð 	ð 	ð 	ð 	ð 	s   ©AÁAÁAc                ó   — || _         dS )zU
        Set the LUT from an external source

        :param lut: A new LUT.
        Nr9   )r!   r   s     r$   Úset_lutzMorphOp.set_lut7  s   € ð ˆŒˆˆr&   )NNN)r   r7   r   r   r   r   r   r   )rs   rt   r   ru   )rs   rt   r   r„   )r‰   r<   r   r   )r   r7   r   r   )rl   rm   rn   ro   r%   r   rf   rˆ   r�   r“   r•   r.   r&   r$   rq   rq   Í   s¿   € € € € € Ø4Ð4ð !%Ø"Ø%)ð	Að Að Að Að Að,ð ð ð ð(Cð Cð Cð Cð&:ð :ð :ð :ð!ð !ð !ð !ðð ð ð ðð ð ð ð ð r&   rq   )Ú
__future__r   ra   rA   r   r   r5   rU   rV   r   rq   r.   r&   r$   ú<module>r—      sÎ   ðð #Ð "Ð "Ð "Ð "Ð "à 	€	€	€	à "Ð "Ð "Ð "Ð "Ð "Ð "Ð "à€ðð ð €ð
ð ð €ðmð mð mð mð mñ mô mð mð`pð pð pð pð pñ pô pð pð pð pr&   