+
    JV-jt(  ã                  óf   € ^ RI Ht ^ RIt^RIHtHt Rt. R	Ot. R
Ot ! R R4      t	 ! R R4      t
R# )é    )ÚannotationsN)ÚImageÚ_imagingmorphc                  ór   € ] tR t^tRtRR R lltR R ltR R ltR	 R
 ltR R lt	R R lt
R R ltRtR# )Ú
LutBuilderaü  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               ó$   € V ^8„  d   QhRRRRRR/# )é   Úpatternsúlist[str] | NoneÚop_nameú
str | NoneÚreturnÚNone© )Úformats   "Ú_/Volumes/fast/ai/experiments/ui-tars-smoke/.venv/lib/python3.14/site-packages/PIL/ImageMorph.pyÚ__annotate__ÚLutBuilder.__annotate__A   s$   € ÷ ñ Ø(ðØ:Dðà	ñó    c                ó¾   € RV n         Ve?   RRR.RR.RRR.R	R
.RR
R.R. RO/pW#9  d   RV R2p\        V4      hW2,          V n        R# Ve	   Wn        R# . V n        R# )zê
: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Úcornerú1:(... ... ...)->0z4:(00. 01. ...)->1Ú	dilation4z4:(... .0. .1.)->1Ú	dilation8z4:(... .0. ..1)->1Úerosion4z4:(... .1. .0.)->0Úerosion8z4:(... .1. ..0)->0ÚedgezUnknown pattern Ú!)r   z4:(.0. .1. ...)->1z4:(01. .1. ...)->1)ÚlutÚ	Exceptionr
   )Úselfr
   r   Úknown_patternsÚmsgs   &&&  r   Ú__init__ÚLutBuilder.__init__A   s’   € ð &*ˆŒØÒàÐ/Ð1EÐFØÐ2Ð3ØÐ2Ð4HÐIØÐ1Ð2ØÐ1Ð3GÐHØò ðˆNð Ô,Ø(¨¨	°Ð3�Ü “nÐ$à*Õ3ˆDŽMØÒ!Ø$ŽMàˆDŽMr   c               ó    € V ^8„  d   QhRRRR/# )r	   r
   z	list[str]r   r   r   )r   s   "r   r   r   b   s   € ÷ "ñ " Yð "°4ñ "r   c                ó8   € V ;P                   V,          un         R# )zD
Append to list of patterns.

:param patterns: Additional patterns.
N)r
   )r!   r
   s   &&r   Úadd_patternsÚLutBuilder.add_patternsb   s   € ð 	�Š˜Õ!�r   c               ó   € V ^8„  d   QhRR/# ©r	   r   Ú	bytearrayr   )r   s   "r   r   r   j   s   € ÷ 	ñ 	 9ñ 	r   c                óz   aa€ ^ ^.o^o\        VV3R l\        \        4       4       4      V n        V P                  # )zs
Set the current LUT, and return it.

This is the default LUT that patterns will be applied against when building.
c              3  óJ   <"  € T F  pSVS,          ^ 8„  ,          x € K  	  R# 5i)r   Nr   )Ú.0ÚiÚmÚsymbolss   & €€r   Ú	<genexpr>Ú/LutBuilder.build_default_lut.<locals>.<genexpr>r   s!   øé € ÐK¹?°a˜W a¨!¥e¨q¡[×1Ò1»?ùs   ƒ #)r,   ÚrangeÚLUT_SIZEr   )r!   r1   r2   s   &@@r   Úbuild_default_lutÚLutBuilder.build_default_lutj   s2   ù€ ð �a�&ˆØˆÜÕK¼5Ä¼?ÓKÓKˆŒØ�x‰xˆr   c               ó   € V ^8„  d   QhRR/# )r	   r   úbytearray | Noner   )r   s   "r   r   r   u   s   € ÷ ñ Ð)ñ r   c                ó   € V P                   # )z
Returns the current LUT
©r   )r!   s   &r   Úget_lutÚLutBuilder.get_lutu   s   € ð �x‰xˆr   c               ó$   € V ^8„  d   QhRRRRRR/# )r	   ÚpatternÚstrÚpermutationz	list[int]r   r   )r   s   "r   r   r   {   s!   € ÷ 8ñ 8 sð 8¸ð 8Àsñ 8r   c                ó^   a€ \        V4      ^	8X  g   Q hRP                  V3R lV 4       4      # )zeTakes a pattern and a permutation and returns the
string permuted according to the permutation list.
Ú c              3  ó6   <"  € T F  pSV,          x € K  	  R # 5i)Nr   )r/   Úpr@   s   & €r   r3   Ú-LutBuilder._string_permute.<locals>.<genexpr>€   s   øé € Ð7©; a�w˜q—z’z«;ùs   ƒ)ÚlenÚjoin)r!   r@   rB   s   &f&r   Ú_string_permuteÚLutBuilder._string_permute{   s-   ø€ ô �;Ó 1Ô$Ð$Ð$Ø�w‰wÔ7©;Ó7Ó7Ð7r   c               ó(   € V ^8„  d   QhRRRRRRRR/# )r	   Úbasic_patternrA   ÚoptionsÚbasic_resultÚintr   zlist[tuple[str, int]]r   )r   s   "r   r   r   ‚   s,   € ÷ ñ Ø ðØ+.ðØ>Aðà	ñr   c                óD  € W3.pRV9   dX   VR,          ^,          p\        ^4       F8  pVP                  V P                  VR,          ^ ,          \        4      V34       K:  	  RV9   dA   \	        V4      pVRV  F,  w  r…VP                  V P                  V\
        4      V34       K.  	  RV9   dp   \	        V4      pVRV  F[  w  r…VP                  RR4      P                  RR4      P                  RR4      p^\        V4      ,
          pVP                  W…34       K]  	  V# )	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ÚMNÚNÚ0ÚZÚ1éÿÿÿÿ)r5   ÚappendrJ   ÚROTATION_MATRIXrH   ÚMIRROR_MATRIXÚreplacerP   )	r!   rM   rN   rO   r
   Úresr0   Únr@   s	   &&&&     r   Ú_pattern_permuteÚLutBuilder._pattern_permute‚   s  € ð #Ð1Ð2ˆð �'Œ>Ø˜2•,˜q•/ˆCÜ˜1–X�Ø—‘Ø×)Ñ)¨(°2­,°q­/¼?ÓKÈSÐQöñ ð
 �'Œ>Ü�H“ˆAØ (¨¨!£‘�Ø—‘ ×!5Ñ!5°g¼}Ó!MÈsÐ SÖTñ !-ð �'Œ>Ü�H“ˆAØ (¨¨!£‘�à!Ÿ/™/¨#¨sÓ3×;Ñ;¸CÀÓE×MÑMÈcÐSVÓW�Øœ#˜c›(•l�Ø—‘  Ö/ñ	 !-ð ˆr   c               ó   € V ^8„  d   QhRR/# r+   r   )r   s   "r   r   r   ¢   s   € ÷ (ñ (˜9ñ (r   c                óä  € V P                  4        V P                  f   Q h. pV P                   FÃ  p\        P                  ! RVP                  RR4      4      pV'       g   RV,           R,           p\        V4      hVP                  ^4      pVP                  ^4      p\        VP                  ^4      4      pVP                  RR4      P                  RR4      pWP                  WeV4      ,          pKÅ  	  . pV FZ  pV^ ,          P                  RR	4      P                  R	R
4      pVP                  \        P                  ! V4      V^,          34       K\  	  \        \        4       F{  p	\        V	4      R,          p
R^	\        V
4      ,
          ,          V
,           RRR1,          p
V F6  w  rkVP!                  V
4      '       g   K  ^ ^.V,          V P                  V	&   K8  	  K}  	  V P                  # )zgCompile all patterns into a morphology LUT, and return it.

This is the data to be passed into MorphOp.Nz(\w):?\s*\((.+?)\)\s*->\s*(\d)Ú
rD   zSyntax error in pattern "Ú"Ú Ú.ÚXz[01]:r	   NNrU   rX   )r7   r   r
   ÚreÚsearchr\   r    ÚgrouprP   r_   rY   Úcompiler5   r6   ÚbinrH   Úmatch)r!   r
   rF   r1   r#   rN   r@   ÚresultÚcompiled_patternsr0   Ú
bitpatternÚrs   &           r   Ú	build_lutÚLutBuilder.build_lut¢   s™  € ð 	×ÑÔ Ø�x‰xÒ#Ð#Ð#Øˆð —”ˆAÜ—	’	Ð;¸Q¿Y¹YÀtÈRÓ=PÓQˆAßØ1°AÕ5¸Õ;�Ü “nÐ$Ø—g‘g˜a“jˆGØ—g‘g˜a“jˆGÜ˜Ÿ™ ›“_ˆFð —o‘o c¨2Ó.×6Ñ6°t¸RÓ@ˆGà×-Ñ-¨gÀÓGÕGŠHñ ð ÐÛˆGØ˜•
×"Ñ" 3¨Ó,×4Ñ4°S¸&ÓAˆAØ×$Ñ$¤b§j¢j°£m°W¸QµZÐ%@ÖAñ  ô ”x–ˆAä˜Q› �ˆJØ ¤S¨£_Õ!4Õ5¸
ÕBÁDÀbÀDÕIˆJã/‘
�Ø—=‘= ×,Ô,Ø#$ a &¨¥)�D—H‘H˜Q“Kó 0ñ !ð �x‰xˆr   )r   r
   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r$   r(   r7   r=   rJ   r_   rr   Ú__static_attributes__r   r   r   r   r      s2   † ñ!÷FõB"õ	õõ8õ÷@(ñ (r   r   c                  ór   € ] tR t^ÍtRtRR R lltR R ltR R ltR	 R
 ltR R lt	R R lt
R R ltRtR# )ÚMorphOpz*A class for binary morphological operatorsNc               ó(   € V ^8„  d   QhRRRRRRRR/# )	r	   r   r:   r   r   r
   r   r   r   r   )r   s   "r   r   ÚMorphOp.__annotate__Ð   s8   € ÷ Añ AàðAð ðAð #ð	Að
 
ñAr   c                ób   € Vf   Vf	   Wn         R# \        W24      P                  4       V n         R# )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   rr   )r!   r   r   r
   s   &&&&r   r$   ÚMorphOp.__init__Ð   s(   € ð" Ò ¢ØŽHä! (Ó4×>Ñ>Ó@ˆDŽHr   c               ó    € V ^8„  d   QhRRRR/# )r	   ÚimageúImage.Imager   ztuple[int, Image.Image]r   )r   s   "r   r   r}   æ   s   € ÷ ñ ˜;ð Ð+Bñ r   c                ó^  € V P                   f   Rp\        V4      hVP                  R9  d   Rp\        V4      h\        P
                  ! VP                  VP                  4      p\        P                  ! \        V P                   4      VP                  4       VP                  4       4      pWC3# )a  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.úNo operator loadedúImage mode must be 1 or L©rW   ÚL)r   r    ÚmodeÚ
ValueErrorr   ÚnewÚsizer   ÚapplyÚbytesÚgetim)r!   r�   r#   ÚoutimageÚcounts   &&   r   rŒ   ÚMorphOp.applyæ   s�   € ð �8‰8ÒØ&ˆCÜ˜C“.Ð à�:‰:˜ZÔ'Ø-ˆCÜ˜S“/Ð!Ü—9’9˜UŸZ™Z¨¯©Ó4ˆÜ×#Ò#¤E¨$¯(©(£O°U·[±[³]ÀHÇNÁNÓDTÓUˆØˆÐr   c               ó    € V ^8„  d   QhRRRR/# ©r	   r�   r‚   r   zlist[tuple[int, int]]r   )r   s   "r   r   r}   ú   s   € ÷ Cñ C˜;ð CÐ+@ñ Cr   c                óä   € V P                   f   Rp\        V4      hVP                  R9  d   Rp\        V4      h\        P
                  ! \        V P                   4      VP                  4       4      # )aH  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.r„   r…   r†   )r   r    rˆ   r‰   r   rm   r�   rŽ   ©r!   r�   r#   s   && r   rm   ÚMorphOp.matchú   sY   € ð �8‰8ÒØ&ˆCÜ˜C“.Ð à�:‰:˜ZÔ'Ø-ˆCÜ˜S“/Ð!Ü×"Ò"¤5¨¯©£?°E·K±K³MÓBÐBr   c               ó    € V ^8„  d   QhRRRR/# r“   r   )r   s   "r   r   r}     s   € ÷ :ñ : ;ð :Ð3Hñ :r   c                ó†   € VP                   R9  d   Rp\        V4      h\        P                  ! VP	                  4       4      # )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.r…   r†   )rˆ   r‰   r   Úget_on_pixelsrŽ   r•   s   && r   r™   ÚMorphOp.get_on_pixels  s6   € ð �:‰:˜ZÔ'Ø-ˆCÜ˜S“/Ð!Ü×*Ò*¨5¯;©;«=Ó9Ð9r   c               ó    € V ^8„  d   QhRRRR/# ©r	   ÚfilenamerA   r   r   r   )r   s   "r   r   r}     s   € ÷ !ñ ! ð !¨ñ !r   c                ó  € \        VR4      ;_uu_ 4       p\        VP                  4       4      V n        RRR4       \	        V P                  4      \
        8w  d   RV n        Rp\        V4      hR#   + '       g   i     LD; i)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   rH   r6   r    )r!   r�   Úfr#   s   &&  r   Úload_lutÚMorphOp.load_lut  s`   € ô �(˜D×!Ô! QÜ  §¡£Ó*ˆDŒH÷ "ô ˆt�x‰x‹=œHÔ$ØˆDŒHØ-ˆCÜ˜C“.Ð ñ %÷ "×!ús   •A0Á0B 	c               ó    € V ^8„  d   QhRRRR/# rœ   r   )r   s   "r   r   r}   *  s   € ÷ ñ  ð ¨ñ r   c                óÔ   € V P                   f   Rp\        V4      h\        VR4      ;_uu_ 4       pVP                  V P                   4       RRR4       R#   + '       g   i     R# ; i)z�
Save an operator to an mrl file.

:param filename: The destination file.
:exception Exception: If the current operator is None.
Nr„   Úwb)r   r    r    Úwrite)r!   r�   r#   r¢   s   &&  r   Úsave_lutÚMorphOp.save_lut*  sK   € ð �8‰8ÒØ&ˆCÜ˜C“.Ð Ü�(˜D×!Ô! QØ�G‰G�D—H‘HÔ÷ "×!×!Ò!ús   °AÁA'	c               ó    € V ^8„  d   QhRRRR/# )r	   r   r:   r   r   r   )r   s   "r   r   r}   7  s   € ÷ ñ Ð+ð °ñ r   c                ó   € Wn         R# )z=
Set the LUT from an external source

:param lut: A new LUT.
Nr<   )r!   r   s   &&r   Úset_lutÚMorphOp.set_lut7  s	   € ð Žr   r<   )NNN)rt   ru   rv   rw   rx   r$   rŒ   rm   r™   r£   r©   r­   ry   r   r   r   r{   r{   Í   s/   † Ù4÷Aõ,õ(Cõ&:õ!õ÷ñ r   r{   i   )	é   é   r   é   é   é   é   é   r	   )	r	   r³   r   rµ   r²   r°   r´   r±   r¯   )Ú
__future__r   rh   rD   r   r   r6   rZ   r[   r   r{   r   r   r   Ú<module>r·      s<   ðõ #ã 	ç "à€ò€ò
€÷mñ m÷`pó pr   