+
    ÿäûjÒ5  ã                  ó    € ^ RI Ht ^ RIt^ RIt^ RIHt ^ RIHtHtHt R R lt	R R lt
R	 R
 ltRR R llt ! R R]P                  4      tR# )é    )ÚannotationsN)Únn)ÚKORNIA_CHECKÚKORNIA_CHECK_IS_TENSORÚKORNIA_CHECK_TYPEc               ó    € V ^8„  d   QhRRRR/# ©é   Útensorútorch.TensorÚreturnÚbool© )Úformats   "Ú]C:\Ai_Coding\apps\studio\ComfyUI\.venv\Lib\site-packages\kornia\contrib\distance_transform.pyÚ__annotate__r      s   € ÷ ñ ˜|ð °ñ ó    c                óä  € \        V 4      \        P                  J ;'       dÏ    V P                  P                   R9   ;'       d®    \        P                  P                  4       '       * ;'       dƒ    \        P                  P                  4       '       * ;'       dX    \        P                  P                  P                  V 4      '       * ;'       d"    \        P                  P                  4       ^ 8H  # )Úcpu)r   ÚcudaÚmps)ÚtypeÚtorchÚTensorÚdeviceÚjitÚ
is_tracingÚcompilerÚis_compilingÚ_CÚ
_functorchÚis_functorch_wrapped_tensorÚ_len_torch_dispatch_stack©r   s   &r   Ú_use_eager_scratchr%      s¸   € äˆV‹œŸ™Ð$÷ 	6ð 	6Ø�M‰M×ÑÐ"8Ñ8÷	6ð 	6ä—	‘	×$Ñ$Ó&Ô&÷	6ð 	6ô —‘×+Ñ+Ó-Ô-÷	6ð 	6ô —‘×#Ñ#×?Ñ?ÀÓGÔG÷		6ð 	6ô
 �H‰H×.Ñ.Ó0°AÑ5ðr   c               ó    € V ^8„  d   QhRRRR/# r	   r   )r   s   "r   r   r   '   s   € ÷ ñ ˜ð ¨ñ r   c                ó2  € V P                   ;'       g…    \        P                  P                  P	                  V 4      P
                  R J;'       gH    \        P                  P                  4       ;'       g"    \        P                  P                  4       ^ 8„  # ©N)
Úrequires_gradr   ÚautogradÚ
forward_adÚunpack_dualÚtangentr   r   r    r#   r$   s   &r   Ú_needs_gradr.   '   sx   € à×Ñ÷ 	4ð 	4Ü�>‰>×$Ñ$×0Ñ0°Ó8×@Ñ@ÈÐL÷	4ð 	4ä�9‰9×ÑÓ!÷	4ð 	4ô �8‰8×-Ñ-Ó/°!Ñ3ð	r   c               ó    € V ^8„  d   QhRRRR/# )r
   Úcostr   r   r   )r   s   "r   r   r   0   s   € ÷ ^ñ ^ lð ^°|ñ ^r   c                ód  € \        V 4      p\        V 4      pV P                  P                  p\        P
                  P                  4       '       d   R
pRpMVR8X  d   Rp^@pMVR8X  d   RpRpMR
pRpRp\        ^V P                  4       EF—  pV P                  VR4      P                  4       pVP                  R,          p	\        P                  ! W�P                  V P                  R7      p
VP                  RV	4      pVP                  ^ ,          p\        WY\!        ^WI,          4      4      p\!        ^WMV	,          ,          4      p\        WÎ4      pV'       Eg\   V'       EdS   Wý,          V	,          pVe   VP#                  4       V8  d   V P%                  V4      p. p\        ^ W�4       EF  pV
VVV,           1R3,          V
R,          ,
          P'                  4       pVP                  ^ ,          pVP%                  VV34      p\        ^ WÎ4       FŠ  p\        VV,           V4      pVV,
          pVRVV,          V	,           P)                  VVV	4      p\        P*                  ! VVV P-                  R4      VVR7       \        P.                  ! VRVVV R7       KŒ  	  VP1                  V4       EK  	  MÍ. p\        ^ W�4       F»  pV
VVV,           1R3,          V
R,          ,
          P'                  4       pVP3                  V^ R7       Uu. uF+  pVP-                  R4      V,           P/                  RR7      NK-  	  ppTP1                  \5        V4      ^8X  d
   V^ ,          M\        P6                  ! V^ R7      4       K½  	  \5        V4      ^8X  d
   V^ ,          M\        P6                  ! VRR7      pEM@\        P8                  P:                  P=                  V4      P>                  PA                  4       pV'       Ed¨   Wý,          V	,          pVe   VP#                  4       V8  d   V P%                  V4      pVP%                  Wý,          4      p. p\        ^ W�4       EFC  pV
VVV,           1R3,          V
R,          ,
          P'                  4       pVP                  ^ ,          p\        PB                  ! VV3\        PD                  V P                  R	7      p \        ^ WÎ4       F¨  p\        VV,           V4      pVV,
          pVRVV,          V	,           P)                  VVV	4      p\        P*                  ! VVV P-                  R4      VVR7       VRVV,           P)                  VV4      p!\        P                  ! VRV!V VV 3R7       Kª  	  VP1                  V 4       EKF  	  M×. p\        ^ W�4       FÅ  pV
VVV,           1R3,          V
R,          ,
          P'                  4       pVP3                  V^ R7       Uu. uF5  pVP-                  R4      V,           P                  RR7      PF                  NK7  	  ppTP1                  \5        V4      ^8X  d
   V^ ,          M\        P6                  ! V^ R7      4       KÇ  	  \5        V4      ^8X  d
   V^ ,          M\        P6                  ! VRR7      p"VPI                  RV"4      V
V"PK                  V P                  4      ,
          P'                  4       ,           pVP                  VP                  4      P                  RV4      p EKš  	  V # u upi u upi )r
   i   r   r   N)r   Údtype)Úout)Údimr3   )r4   )r2   r   i   i    i  € éÿÿÿÿ)N:NNNéþÿÿÿ)&r%   r.   r   r   r   r   r   ÚrangeÚndimÚmovedimÚ
contiguousÚshapeÚaranger2   ÚreshapeÚminÚmaxÚnumelÚ	new_emptyÚsquareÚviewÚaddÚ	unsqueezeÚaminÚappendÚsplitÚlenÚcatr*   r+   r,   ÚprimalÚdetachÚemptyÚint64ÚindicesÚgatherÚto)#r0   Úuse_scratchÚ	need_gradÚdev_typeÚbudgetÚmax_queriesÚbufÚaxisÚvaluesÚlengthÚ	positionsÚlinesÚ	num_linesÚ
query_tileÚ	line_tileÚb_capÚneed_bufÚtransformedÚstartÚoffsetsÚq_lenÚcol_outÚb_startÚb_endÚb_lenÚcur_bufÚblockÚchunksÚ	out_linesÚ	lines_detÚval_discardÚ	idx_tilesÚcol_idxÚcur_valsÚidxs#   &                                  r   Ú_squared_distance_transformrt   0   s  € ô % TÓ*€KÜ˜DÓ!€IØ�{‰{×Ñ€HÜ‡~�~×"Ñ"×$Ò$ØˆØ‰Ø	�UÔ	ØˆØ‰Ø	�VÔ	ØˆØ‰àˆØˆà#€CÜ�a˜Ÿ™×#ˆà—‘˜d BÓ'×2Ñ2Ó4ˆØ—‘˜bÕ!ˆÜ—L’L ·±À4Ç:Á:ÔNˆ	Ø—‘˜r 6Ó*ˆØ—K‘K •Nˆ	Ü˜¬c°!°VÕ5EÓ.FÓGˆ
Ü˜˜6°6Õ&9Õ:Ó;ˆ	Ü�IÓ)ˆßˆyßˆ{Ø Õ-°Õ6�Ø’; #§)¡)£+°Ô"8ØŸ.™.¨Ó2�CØ �Ü" 1 f×9�EØ(¨°¸Õ1CÐ)CÀTÐ)IÕJÈYÐW^ÕM_Õ_×gÑgÓi�GØ#ŸM™M¨!Õ,�EØ#Ÿo™o¨y¸%Ð.@ÓA�GÜ#(¨¨IÖ#A˜Ü # G¨iÕ$7¸Ó C˜Ø %¨¥˜Ø"%Ð&>¨°­¸Õ(>Ð"?×"DÑ"DÀUÈEÐSYÓ"Z˜ÜŸ	š	 %¨°Ð"6×"@Ñ"@ÀÓ"DÀgÐSZÕ[ÜŸ
š
 7°¸ÀÈÐ8N×Oñ $Bð  ×&Ñ& w×/ò :ð !�Ü" 1 fÖ9�EØ(¨°¸Õ1CÐ)CÀTÐ)IÕJÈYÐW^ÕM_Õ_×gÑgÓi�GØX]×XcÑXcÐdmÐstÐXcÔXuÓvÑXuÈu˜uŸ™¨rÓ2°WÕ<×BÑBÀrÐBÖJÑXu�FÐvØ×&Ñ&´C¸³KÀ1Ô4D v¨a¦yÌ%Ï)Ê)ÐTZÐ`aÔJbÖcñ :ô +.¨kÓ*:¸aÔ*?˜ AžÄUÇYÂYÈ{Ð`bÔEcŠIäŸ™×1Ñ1×=Ñ=¸eÓD×KÑK×RÑRÓTˆIßˆ{Ø Õ-°Õ6�Ø’; #§)¡)£+°Ô"8ØŸ.™.¨Ó2�CØ'×1Ñ1°%Õ2DÓE�Ø�	Ü" 1 f×9�EØ(¨°¸Õ1CÐ)CÀTÐ)IÕJÈYÐW^ÕM_Õ_×gÑgÓi�GØ#ŸM™M¨!Õ,�EÜ#Ÿkšk¨9°eÐ*<ÄEÇKÁKÐX\×XcÑXcÔd�GÜ#(¨¨IÖ#A˜Ü # G¨iÕ$7¸Ó C˜Ø %¨¥˜Ø"%Ð&>¨°­¸Õ(>Ð"?×"DÑ"DÀUÈEÐSYÓ"Z˜ÜŸ	š	 )¨G°EÐ":×"DÑ"DÀRÓ"HÈ'ÐW^Õ_ð $/¨°¸µÐ#?×#DÑ#DÀUÈEÓ#R˜ÜŸ	š	 '¨r¸À'È'ÐRWÐBXÐ7Y×Zñ $Bð ×$Ñ$ W×-ò :ð  �	Ü" 1 fÖ9�EØ(¨°¸Õ1CÐ)CÀTÐ)IÕJÈYÐW^ÕM_Õ_×gÑgÓi�Gð &/§_¡_°YÀA _Ô%Fóá%F˜Eð Ÿ™¨Ó,¨wÕ6×;Ñ;ÀÐ;ÓC×KÔKÙ%Fð ð ð ×$Ñ$´#°f³+ÀÔ2B V¨A¦YÌÏ	Ê	ÐRXÐ^_ÔH`Öañ :ô #& i£.°AÔ"5�)˜A–,¼5¿9º9ÀYÐTVÔ;WˆCØŸ™ R¨Ó-°¸S¿V¹VÀDÇJÁJÓ=OÕ1O×0WÑ0WÓ0YÕYˆIØ× Ñ  §¡Ó.×6Ñ6°r¸4Ó@‹ñI $ðJ €KùòM wùò<s   Ì1\(×=;\-c               ó(   € V ^8„  d   QhRRRRRRRR/# )r
   Úimager   Úkernel_sizeÚintÚhÚfloatr   r   )r   s   "r   r   r   ‘   s5   € ÷ YIñ YI˜lð YI¸ð YIÀUð YIÐVbñ YIr   c                óè  € \        V 4       \        V P                  4       R4       \        V P                  R9   R4       \        \        ;QJ d-    R V P
                  R,           4       F  '       d   K   RM"	  RM! R V P
                  R,           4       4      R4       \        V\        R4       \        V^,          ^ 8g  ;'       d    V^8¬  R	4       \        \        P                  ! V4      ;'       d    V^ 8„  R
4       V P                  \        P                  \        P                  39   d   V P                  4       MT p\        V4      '       d”   \!        V4      '       gƒ   VP#                  RR4      P%                  4       P'                  V) 4      p\)        V4      pVP+                  V\        R4      8H  R4      P-                  4        VP/                  V P                  4      # VP1                  R4      pVP3                  R4      pVP5                  R\        R4      4      pV^ 8„  p	\        P6                  ! W“^8*  ,          W64      p
\        P6                  ! W’) V
P9                  4       ,          V4      p\)        V4      p\        P                  ! V4      V^ 8„  ,          p\        P6                  ! WµV4      p\        P6                  ! W¼P;                  4       V4      P/                  V P                  4      # )a‚  Compute the distance to the nearest positive pixel per channel, weighted for values below 1, in 2D or 3D.

``dist = distance_transform(image)`` takes a floating ``(B, C, H, W)`` image or ``(B, C, D, H, W)`` volume and
returns, per channel, the distance to the nearest positive pixel. For a binary mask it is the Euclidean distance
in pixels, exact up to floating-point rounding. A value :math:`0 < v < 1` is a soft source that adds
:math:`-h \log v` to the squared distance, and the result is piecewise differentiable in the image:

.. math::
    D(p) = \sqrt{\min_{q: v(q)>0} \left(\|p-q\|_2^2 - h\log v(q)\right)},
    \qquad v = \operatorname{clip}(\mathrm{image}, 0, 1).

The separable minimum-convolution formulation follows section 2.2 of
`Distance Transforms of Sampled Functions (Felzenszwalb and Huttenlocher, 2012)
<https://cs.brown.edu/people/pfelzens/papers/dt-final.pdf>`_. The implementation uses
tiled tensor reductions, not the paper's linear-time envelope algorithm: its work
scales as the number of input elements times the sum of the spatial dimensions.

Convention:
    - Distances are to positive pixels, whereas SciPy and OpenCV measure distances to
      zero pixels. On a nonempty binary mask this matches
      ``scipy.ndimage.distance_transform_edt(image == 0)`` per batch/channel.
    - A channel without positive pixels returns zeros, preserving the empty-mask convention.
    - A fractional source adds ``-h * log(v)`` to the **squared** distance, including at
      the source itself. Its influence extends throughout the image. Multiple sources
      compete by their minimum cost; they do not lower distances by being counted repeatedly.
    - The operation is piecewise differentiable. For a unique minimizing source and
      ``D(p) > 0``, its derivative is ``-h / (2 * v(q) * D(p))``. At ties, one minimizing
      source receives the gradient (the first selected by the axis-wise reductions).
      At zero distance the derivative is defined as zero to avoid the square-root singularity.
      Nonpositive inputs and values above 1 have zero gradient; the derivative of clipping
      at exactly 1 passes through. There is no straight-through estimator.
    - For dense fractional masks, a pixel with ``-h * log(v) < 1`` selects itself: every
      other pixel is at least one squared pixel away. Increase ``h`` to let stronger
      neighbours compete over longer distances.
    - ``float16`` and ``bfloat16`` inputs are computed in ``float32`` and cast back.

Args:
    image: Floating-point image or volume of shape ``(B, C, H, W)`` or ``(B, C, D, H, W)``.
    kernel_size: Odd integer of at least 3, retained for call compatibility. It does not
        change the result: the transform considers the entire spatial domain.
    h: Positive finite weight of the fractional-source cost in squared pixel units.
        It does not affect binary distances. ``h``, source costs and gradients must remain
        representable in the working dtype.

Returns:
    Distances with the input's shape, dtype and device.

Raises:
    BaseError: if ``image`` is not a floating tensor of 4 or 5 dimensions with nonempty spatial sizes,
        ``kernel_size`` is not an odd int of at least 3, or ``h`` is not positive and finite.

See Also:
    - :class:`DistanceTransform`: the module form.

Example:
    >>> image = torch.zeros(1, 1, 5, 5)
    >>> image[:, :, 0, 0] = 1
    >>> distance_transform(image)[0, 0, 3, 4]
    tensor(5.)

z%image must be a floating point tensorz2Invalid image shape, expected BxCxHxW or BxCxDxHxWc              3  ó*   "  € T F	  q^ 8„  x € K  	  R# 5i)r   Nr   )Ú.0Úsizes   & r   Ú	<genexpr>Ú%distance_transform.<locals>.<genexpr>Ò   s   é € Ð:©/ $˜A–X«/ùs   ‚:r
   NNFTz#Spatial dimensions must be nonemptyzkernel_size must be an intz'kernel_size must be an odd integer >= 3zh must be positive and finiteg        g      ð?Úinf)é   é   r   )r   r   Úis_floating_pointr8   Úallr;   r   rx   ÚmathÚisfiniter2   r   Úfloat16Úbfloat16rz   r%   r.   ÚclampÚlog_Úmul_rt   Úmasked_fill_Úsqrt_rQ   Únew_onesÚ	new_zerosÚnew_fullÚwhereÚlogÚsqrt)rv   rw   ry   rY   r0   ÚsquaredÚoneÚzeror�   ÚpositiveÚsafe_valuesÚvalidÚsafe_squareds   &&&          r   Údistance_transformrœ   ‘   s%  € ô| ˜5Ô!Ü�×(Ñ(Ó*Ð,SÔTÜ�—‘˜vÑ%Ð'[Ô\Ü—“Ñ:¨%¯+©+°b®/Ó:——’Ñ:¨%¯+©+°b®/Ó:Ó:Ð<aÔbÜ�k¤3Ð(DÔEÜ�˜q• AÑ%×:Ð:¨+¸Ñ*:Ð<eÔfÜ”—’˜qÓ!×+Ð+ a¨!¡eÐ-LÔMà#Ÿk™k¬e¯m©m¼U¿^¹^Ð-LÔLˆU�[‰[Œ]ÐRW€FÜ˜&×!Ò!¬+°f×*=Ò*=Ø�|‰|˜C Ó%×*Ñ*Ó,×1Ñ1°1°"Ó5ˆÜ-¨dÓ3ˆØ×Ñ˜W¬¨e«Ñ4°cÓ:×@Ñ@ÔBØ�z‰z˜%Ÿ+™+Ó&Ð&ð �/‰/˜"Ó
€CØ×Ñ˜BÓ€DØ
�/‰/˜"œe E›lÓ
+€CØ˜‰z€HÜ—+’+˜h°A©+Õ6¸ÓD€KÜ�;Š;�x  k§o¡oÓ&7Õ!7¸Ó=€DÜ)¨$Ó/€GÜ�NŠN˜7Ó# w°¡{Õ3€Eä—;’;˜u¨sÓ3€LÜ�;Š;�u×/Ñ/Ó1°4Ó8×;Ñ;¸E¿K¹KÓHÐHr   c                  óB   a € ] tR t^ítRtRR V 3R llltR R ltRtV ;t# )ÚDistanceTransforma©  Compute the distance to the nearest positive pixel per channel (module form).

``dt = DistanceTransform(h=0.35)`` stores its arguments, and ``dist = dt(image)`` is :func:`distance_transform` on a
``(B, C, H, W)`` image or ``(B, C, D, H, W)`` volume, returning distances of the same shape, dtype and device.

Convention:
    See the Convention block on :func:`distance_transform`.

Args:
    kernel_size: Odd integer of at least 3, retained for compatibility; does not affect distances.
    h: Positive finite weight of the squared-distance source cost. See :func:`distance_transform`.

See Also:
    - :func:`distance_transform`: the one-call form, with the gradient conventions.

c               ó$   € V ^8„  d   QhRRRRRR/# )r
   rw   rx   ry   rz   r   ÚNoner   )r   s   "r   r   ÚDistanceTransform.__annotate__ÿ   s!   € ÷ ñ  Cð °ð Àñ r   c                	ó<   <€ \         SV `  4        Wn        W n        R # r(   )ÚsuperÚ__init__rw   ry   )Úselfrw   ry   Ú	__class__s   &&&€r   r¤   ÚDistanceTransform.__init__ÿ   s   ø€ Ü‰ÑÔØ&ÔØŽr   c               ó    € V ^8„  d   QhRRRR/# )r
   rv   r   r   r   )r   s   "r   r   r¡     s   € ÷ Cñ C˜\ð C¨lñ Cr   c                óB   € \        WP                  V P                  4      # )zNCompute distances for a batch of images or volumes, independently per channel.)rœ   rw   ry   )r¥   rv   s   &&r   ÚforwardÚDistanceTransform.forward  s   € ä! %×)9Ñ)9¸4¿6¹6ÓBÐBr   )ry   rw   ©é   gffffffÖ?)	Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r¤   rª   Ú__static_attributes__Ú__classcell__)r¦   s   @r   rž   rž   í   s   ø† ñ÷"ò ÷
Có Cr   rž   r¬   )Ú
__future__r   r†   r   r   Úkornia.core.checkr   r   r   r%   r.   rt   rœ   ÚModulerž   r   r   r   Ú<module>r¸      sD   ðõ$ #ã ã Ý ç UÑ Uõõõ^÷BYIôxC˜Ÿ	™	ö Cr   