+
    UBir!                    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   "3/usr/lib/python3.14/site-packages/PIL/ImageMorph.py__annotate__LutBuilder.__annotate__A   s$     4 4(4:D4	4    c                	    Ve   Wn         M. V n         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 # R # )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)r
   lut	Exception)selfr
   r   known_patternsmsgs   &&&  r   __init__LutBuilder.__init__A   s     $MDM%)/1EF2324HI1213GH N ,(	3n$*3DM# r   c                    V ^8  d   QhRRRR/# )r	   r
   z	list[str]r   r   r   )r   s   "r   r   r   \   s     " "Y "4 "r   c                	8    V ;P                   V,          un         R # Nr
   )r!   r
   s   &&r   add_patternsLutBuilder.add_patterns\   s    !r   c                   V ^8  d   QhRR/# )r	   r   r   r   )r   s   "r   r   r   _   s     L L4 Lr   c                	f   aa ^ ^.o^o\        VV3R l\        \        4       4       4      V n        R# )r   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>b   s!     K?aWa!eq[11?s    #N)	bytearrayrangeLUT_SIZEr   )r!   r1   r2   s   &@@r   build_default_lutLutBuilder.build_default_lut_   s'    a&K5?KKr   c                   V ^8  d   QhRR/# )r	   r   bytearray | Noner   )r   s   "r   r   r   d   s      ) r   c                	    V P                   # r(   r   )r!   s   &r   get_lutLutBuilder.get_lutd   s    xx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   g   s!     8 8s 8 8s 8r   c                ^   a \        V4      ^	8X  g   Q hRP                  V3R lV 4       4      # )ztstring_permute takes 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r(   r   )r/   prA   s   & r   r3   -LutBuilder._string_permute.<locals>.<genexpr>l   s     7;awqzz;s   )lenjoin)r!   rA   rC   s   &f&r   _string_permuteLutBuilder._string_permuteg   s-     ;1$$$ww7;777r   c               (    V ^8  d   QhRRRRRRRR/# )r	   basic_patternrB   optionsbasic_resultintr   zlist[tuple[str, int]]r   )r   s   "r   r   r   n   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  rVP                  V P                  V\
        4      V34       K.  	  RV9   dp   \	        V4      pVRV  F[  w  rVP                  RR4      P                  RR4      P                  RR4      p^\        V4      ,
          pVP                  W34       K]  	  V# )	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.4MNN0Z1)r6   appendrK   ROTATION_MATRIXrI   MIRROR_MATRIXreplacerQ   )	r!   rN   rO   rP   r
   resr0   nrA   s	   &&&&     r   _pattern_permuteLutBuilder._pattern_permuten   s    #12 '>2,q/C1X))(2,q/?KSQ 
 '>HA (!!5!5g}!Ms ST !- '>HA (!!//#s3;;CEMMcSVW#c(l/	 !- r   c                   V ^8  d   QhRR/# )r	   r   r5   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                  # )z\Compile all patterns into a morphology lut.

TBD :Build based on (file) morphlut:modify_lut
Nz(\w):?\s*\((.+?)\)\s*->\s*(\d)
rE   zSyntax error in pattern "" .Xz[01]:r	   NNrV   rY   )r8   r   r
   researchr]   r    grouprQ   r`   rZ   compiler6   r7   binrI   match)r!   r
   rG   r1   r#   rO   rA   resultcompiled_patternsr0   
bitpatternrs   &           r   	build_lutLutBuilder.build_lut   s   
 	 xx### A		;QYYtR=PQA1A5;n$ggajGggajG_F ooc2.66tR@G--gGGH  G
""3,44S&AA$$bjjmWQZ%@A   xAQJS_!45
BDbDIJ/
==,,#$a&)DHHQK 0 ! xxr   )r   r
   )NN)__name__
__module____qualname____firstlineno____doc__r$   r*   r8   r>   rK   r`   rs   __static_attributes__r   r   r   r   r      s2    !F46"L
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                    Wn         Ve"   \        VR7      P                  4       V n         R# Ve"   \        VR7      P                  4       V n         R# R# )z&Create a binary morphological operatorN)r   r)   )r   r   rs   )r!   r   r   r
   s   &&&&r   r$   MorphOp.__init__   sD     !'2<<>DH!!84>>@DH "r   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8w  d   Rp\        V4      h\        P
                  ! VP                  VP                  R4      p\        P                  ! \        V P                   4      VP                  4       VP                  4       4      pWC3# )zwRun 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getim)r!   r   r#   outimagecounts   &&   r   r   MorphOp.apply   s    
 88&CC. ::(CS/!99UZZT:##E$((OU[[]HNNDT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8w  d   Rp\        V4      h\        P
                  ! \        V P                   4      VP                  4       4      # )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`.r   r   r   )r   r    r   r   r   rn   r   r   r!   r   r#   s   && r   rn   MorphOp.match   sY     88&CC. ::(CS/!""5?EKKM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8w  d   Rp\        V4      h\        P                  ! VP	                  4       4      # )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`.r   r   )r   r   r   get_on_pixelsr   r   s   && r   r   MorphOp.get_on_pixels   s6     ::(CS/!**5;;=99r   c                    V ^8  d   QhRRRR/# r	   filenamerB   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rbNzWrong size operator file!)openr5   readr   rI   r7   r    )r!   r   fr#   s   &&  r   load_lutMorphOp.load_lut   s^    (D!!Q *DH " txx=H$DH-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 fileNr   wb)r   r    r   write)r!   r   r#   r   s   &&  r   save_lutMorphOp.save_lut   sI    88&CC. (D!!QGGDHH "!!!s   AA'	c                    V ^8  d   QhRRRR/# )r	   r   r;   r   r   r   )r   s   "r   r   r~     s      +  r   c                    Wn         R# )z#Set the lut from an external sourceNr=   )r!   r   s   &&r   set_lutMorphOp.set_lut  s    r   r=   )NNN)ru   rv   rw   rx   ry   r$   r   rn   r   r   r   r   rz   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   ri   rE   r   r   r7   r[   r\   r   r|   r   r   r   <module>r      s<    # 	 "
[ [|N Nr   