
    rbit(                    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/html/mdtn/previsions/meteo_cartes/venv/lib/python3.11/site-packages/PIL/ImageMorph.py__init__zLutBuilder.__init__A   s     &*/1EF2324HI1213GH   N n,,3333nn$*73DMMM!$DMMMDMMM    	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KaWa!eq[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K5??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7awqz777777r&   )lenjoin)r!   r;   r=   s    ` r$   _string_permutezLutBuilder._string_permute{   sE     ;1$$$$ww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 rt	          |          }|d|         D ]l\  }}|                    d	d
                              dd	                              d
d          }dt          |          z
  }|                    ||f           m|S )zTakes 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)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1XX  ))(2,q/?KKSQ    '>>HA (! U U!5!5g}!M!Ms STTTT '>>HA (! 0 0!//#s33;;CEEMMcSV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	;QYYtR=P=PQQA %1A5;nn$ggajjGggajjG__F ooc2..66tR@@G--gwGGGHH  	B 	BG
""3,,44S&AAA$$bjmmWQZ%@AAAA x 	, 	,AQJS__!45
BDDbDIJ/ , ,
==,, ,#$a&)DHQK, 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:    " DHHH!(G44>>@@DHHHr&   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//!9UZ44#E$(OOU[[]]HNNDTDT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??EKKMM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 **DH	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ tx==H$$DH-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GGDH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	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&   