+
     åûj²n  ã                   ó  € R t ^ RIt^ RIHtHt ^ RIt^ RIHu Ht	 ^ RI
Ht ^ RIHtHt ^ RIHt ^RIHtHt ^RIHt RR R	 lltR
 R ltR R ltR R ltRR R lltR R ltR R ltR R lt]RR R ll4       tR R ltR# )zHIn this module several equalization methods are exposed: he, ahe, clahe.N)ÚTupleÚUnion)Ú_check_async)Ú _normalize_to_float32_or_float64Ú_to_device_nonblocking)Úperform_keep_shape_image)Ú_check_floating_imageÚ_level_scale)Ú	histogramc          
      óÈ   € V ^8„  d   QhR\         P                  R\        \        \        3,          R\        R\        \         P                  \         P                  3,          /# )é   ÚimgsÚ	grid_sizeÚeven_tile_sizeÚreturn)ÚtorchÚTensorr   ÚintÚbool)Úformats   "ÚWC:\Ai_Coding\apps\studio\ComfyUI\.venv\Lib\site-packages\kornia\enhance\equalization.pyÚ__annotate__r   "   sP   € ÷ Iñ IÜ
�,‰,ðIÜ#(¬¬c¨¥?ðIÜDHðIä
Œ5�<‰<œŸ™Ð%Õ&ñIó    c                ód  € T pVP                   RR w  rEV'       d[   WA^ ,          8:  g   WQ^,          8:  dA   \        RV RV R\        V4       RV^ ,          ^,            RV^,          ^,            R24      h\        P                  ! WA^ ,          ,          4      p\        P                  ! WQ^,          ,          4      pV'       d3   Yf^,          '       d   ^M^ ,          pYw^,          '       d   ^M^ ,          pWa^ ,          ,          V,
          pWq^,          ,          V,
          p	WƒP                   R,          8¼  g   W“P                   R,          8¼  d‰   V'       d   V^ ,          ^,           MV^ ,          ^,          ^,           p
V'       d   V^,          ^,           MV^,          ^,          ^,           p\        RV RV R\        V4       RV RV	 RV
 RV R24      hV^ 8”  g   V	^ 8”  d   \
        P                  ! V^ V	^ V.R	R
7      pVP                   R,          pVP                  ^WÌ4      P                  ^Wf4      P                  ^Ww4      P                  ^4      P                  4       pVP                   R,          V^ ,          8w  d   \        hVP                   R,          V^,          8w  d   \        hWÓ3# )a\  Compute tiles on an image according to a grid size.

Note that padding can be added to the image in order to crop properly the image.
So, the grid_size (GH, GW) x tile_size (TH, TW) >= image_size (H, W)

Args:
    imgs: batch of 2D images with shape (B, C, H, W) or (C, H, W).
    grid_size: number of tiles to be cropped in each direction (GH, GW)
    even_tile_size: Determine if the width and height of the tiles must be even.

Returns:
    tensor with tiles (B, GH, GW, C, TH, TW). B = 1 in case of a single image is provided.
    tensor with the padded batch of 2D imageswith shape (B, C, H', W').

NúTCannot compute tiles on the image according to the given grid size. Got image size (ú, ú) and grid size ú*. The smallest image this grid admits is (ú).z, which needs (zb) of reflect padding, more than the image has to reflect. The smallest image this grid admits is (Úreflect)Úmodeéþÿÿÿéÿÿÿÿéýÿÿÿéûÿÿÿéüÿÿÿ)ÚshapeÚ
ValueErrorÚtupleÚmathÚceilÚFÚpadÚunfoldÚsqueezeÚ
contiguousÚAssertionError)r   r   r   ÚbatchÚhÚwÚkernel_vertÚkernel_horzÚpad_vertÚpad_horzÚmin_vertÚmin_horzÚcÚtiless   &&&           r   Ú_compute_tilesr<   "   sg  € ð$ €Eð �;‰;�r�sÐ�D€Aß˜1¨!¥Ô,°¸qµ\Ô0AÜðØ ˜c  A 3Ð&6´u¸YÓ7GÐ6Hð I7Ø7@Àµ|ÀaÕ7GÐ6HÈÈ9ÐUVÍ<ÐZ[ÕK[ÐJ\Ð\^ð`ó
ð 	
ô —y’y ¨q¥\Õ!1Ó2€KÜ—y’y ¨q¥\Õ!1Ó2€KçØ¨!ŸOœO‘q°Õ2ˆØ¨!ŸOœO‘q°Õ2ˆð  q�\Õ)¨AÕ-€HØ q�\Õ)¨AÕ-€Hð
 —;‘;˜r•?Ô" h·+±+¸bµ/Ô&A÷ (6�9˜Q•< !Ö#¸9ÀQ½<È1Õ;LÈqÕ;Pˆß'5�9˜Q•< !Ö#¸9ÀQ½<È1Õ;LÈqÕ;PˆÜðØ ˜c  A 3Ð&6´u¸YÓ7GÐ6Hð IØˆz˜˜H˜:ð &7Ø7?°jÀÀ8À*ÈBðPó
ð 	
ð �!„|�x !”|Ü—’�e˜a ¨1¨hÐ7¸iÔHˆð �[‰[˜�_€Aà�‰�Q˜Óß	‰��;Ó	,ß	‰��;Ó	,ß	‰�‹ß�jƒlð 
ð ‡{�{�2…˜) A�,Ô&ÜÐà‡{�{�2…˜) A�,Ô&ÜÐàˆ<Ðr   c                ó†   € V ^8„  d   QhR\         P                  R\        \        \        3,          R\         P                  /# )r   Úpadded_imgsÚ	tile_sizer   ©r   r   r   r   )r   s   "r   r   r   n   s5   € ÷ ,ñ ,¬e¯l©lð ,ÄuÌSÔRUÈXÅð ,Ô[`×[gÑ[gñ ,r   c                óú  € V P                  4       ^8w  d   \        R4      hV P                  R,          V^ ,          ,          ^ 8w  d   \        R4      hV P                  R,          V^,          ,          ^ 8w  d   \        R4      hV^ ,          ^,          pV^,          ^,          pV P                  R,          pV P                  ^WD4      P                  ^W"4      P                  ^W34      P	                  ^4      P                  4       pVP                  R,          V8w  d   \        hVP                  R,          V^ ,          ^,          8w  d   \        hVP                  R,          V^,          ^,          8w  d   \        hV# )aÀ  Compute interpolation tiles on a properly padded set of images.

Note that images must be padded. So, the tile_size (TH, TW) * grid_size (GH, GW) = image_size (H, W)

Args:
    padded_imgs: batch of 2D images with shape (B, C, H, W) already padded to extract tiles
                                of size (TH, TW).
    tile_size: shape of the current tiles (TH, TW).

Returns:
    tensor with the interpolation tiles (B, 2GH, 2GW, C, TH/2, TW/2).

zImages Tensor must be 4D.z Images are not correctly padded.r!   r"   r#   )Údimr0   r&   r-   r.   r/   )r>   r?   Úinterp_kernel_vertÚinterp_kernel_horzr:   Úinterp_tiless   &&    r   Ú_compute_interpolation_tilesrF   n   sC  € ð ‡�Ó˜AÔÜÐ8Ó9Ð9à×Ñ˜Õ˜y¨�|Õ+¨qÔ0ÜÐ?Ó@Ð@à×Ñ˜Õ˜y¨�|Õ+¨qÔ0ÜÐ?Ó@Ð@ð (¨�l¨aÕ/ÐØ'¨�l¨aÕ/Ðà×Ñ˜rÕ"€Aà×Ñ˜1˜aÓ#ß	‰�Ð%Ó	:ß	‰�Ð%Ó	:ß	‰�‹ß�jƒlð ð ×Ñ˜"Õ Ô"ÜÐà×Ñ˜"Õ ¨1¥°Õ!1Ô1ÜÐà×Ñ˜"Õ ¨1¥°Õ!1Ô1ÜÐàÐr   c                ód   € V ^8„  d   QhR\         P                  R\        R\         P                  /# )r   r;   Úbinsr   ©r   r   r   )r   s   "r   r   r   �   s)   € ÷ ñ œŸ™ð ¬Cð ´E·L±Lñ r   c                ó*  € V P                   ^ ,          pV \        V P                  V^,
          4      ,          P                  4       P	                  ^ V^,
          4      pV P                  \        V P                  4      4      pVP                  ^ ^4      V8H  p\        P                  ! WSV4      p\        P                  ! W!^,           VP                  VP                  R7      pVP                  ^V\        P                  ! V4      4       VRRV13,          # )a°  Histogram every tile over ``[0, 1]`` in one pass, on the index its lookup table is read with.

A value ``x`` is counted in bin ``(x * _level_scale(x.dtype, bins - 1)).long()``, computed in the dtype of
``tiles`` as the tile lookups compute ``(x * _level_scale(x.dtype)).long()``, so every pixel is counted in the
entry it reads, as OpenCV counts and reads one 8-bit level; ``torch.histc``'s ``floor(x * bins)`` would put
every value in ``[k/256, k/255)`` one bin above that entry. One ``scatter_add_`` counts all
tiles, instead of one ``histc`` launch per tile, and keeps a static output shape so the function stays
a single dynamo graph. Values outside ``[0, 1]`` and NaN are not counted, which the CPU ``histc`` also
skips; they go to one extra overflow column per tile that is dropped.

Args:
    tiles: flattened tiles. (T, P)
    bins: number of bins.

Returns:
    Per-tile counts in float32, or float64 for float64 ``tiles``. (T, bins)

©ÚdtypeÚdeviceºNNNN)r&   r	   rL   ÚlongÚclamp_Útor   Úclampr   ÚwhereÚzerosrM   Úscatter_add_Ú	ones_like)r;   rH   Ú	num_tilesÚidxÚxÚin_rangeÚcountss   &&     r   Ú_tiles_histcr\   �   sÍ   € ð& —‘˜A•€Ià”< §¡¨T°A­XÓ6Õ6×
<Ñ
<Ó
>×
EÑ
EÀaÈÐPQÍÓ
R€CØ�‰Ô1°%·+±+Ó>Ó?€Aà�w‰w�q˜!‹} Ñ!€Hä
�+Š+�h TÓ
*€CÜ�[Š[˜¨1¥H°A·G±GÀAÇHÁHÔM€FØ
×Ñ˜˜3¤§¢°Ó 2Ô3Ø�!�U�d�U�(ÕÐr   c                ód   € V ^8„  d   QhR\         P                  R\        R\         P                  /# )r   ÚresidualÚnum_binsr   rI   )r   s   "r   r   r   ½   s)   € ÷ "ñ "œuŸ|™|ð "´sð "¼u¿|¹|ñ "r   c                ó˜  € \         P                  ! WP                  R7      p\         P                  ! WP	                  ^R7      RR7      P	                  ^R7      P                  ^4      p\         P                  ! W#4      ^ 8H  \         P                  ! W#RR7      V P                  ^4      8  ,          pVP                  V P                  4      # )aú  Spread the residual of the clipped pixels over the histogram bins the way OpenCV does.

``residual`` is the part of the clipped mass of each tile that is left after every bin took the same whole
share, so it is below ``num_bins``. OpenCV adds one count to every ``step``-th bin starting at bin 0, with
``step = max(num_bins // residual, 1)``, until the residual is spent. Writing it into the first ``residual``
bins instead piles it onto the darkest levels and lifts the whole lookup table, which brightens small tiles
by tens of levels.

Args:
    residual: residual per tile, shape ``(T,)``, below ``num_bins``.
    num_bins: number of histogram bins.

Returns:
    the counts to add, 0 or 1 per bin, shape :math:`(T, num_bins)` in the dtype of ``residual``.

©rM   ©ÚminÚfloor©Úrounding_mode)	r   ÚarangerM   ÚdivrR   Ú	unsqueezeÚ	remainderrQ   rL   )r^   r_   rH   ÚstepÚhits   &&   r   Ú_spread_residualrm   ½   s˜   € ô" Ÿš h·±ÔG€DÜŸš 8¯^©^À¨^Ó-BÐRYÔZ×`Ñ`ÐefÐ`Óg×qÑqÐrsÓt€DÜŸš¨Ó4¸Ñ9Ü�	Š	�$¨GÔ4°x×7IÑ7IÈ!Ó7LÑLõ€Cð �6‰6�(—.‘.Ó!Ð!r   c          
      ó²   € V ^8„  d   QhR\         P                  R\        R\        \        \         P                  3,          R\
        R\         P                  /# )r   Ú
tiles_x_imr_   ÚclipÚdiffr   )r   r   r   r   Úfloatr   )r   s   "r   r   r   Ö   sQ   € ÷ J/ñ J/Ü—‘ðJ/Ü(+ðJ/Ü9>¼uÄeÇlÁlÐ?RÕ9SðJ/ÜbfðJ/ä
‡\�\ñJ/r   c           	     óº  € V P                  4       ^8w  d   \        R4      hV P                  w  rErgr‰W‰,          p
V P                  RV
4      pV'       g   \	        W±4      pM}\
        P                  ! VP                  \
        P                  4      p\
        P                  ! ^ ^WP                  VR7      p\        W¾\
        P                  ! RVR7      4      pWÊ,          p\        V\
        P                  4      '       Edî   VP                  P                  R8X  d   VP!                  4       P#                  4       MVP#                  4       pWú,          P%                  VRR7      P'                  ^R7      p\)        VVP                  VP                  4      P                  V^4      P+                  WEV,          V,          4      P-                  R^4      pVP'                  VR	7      pV'       d-   \
        P.                  ! VV,
          4      P1                  ^4      MV
VP1                  ^4      ,
          p\
        P2                  ! VV4      pVVV,
          V,          P5                  ^4      ,           pV\7        VV4      ,           p\)        V^ 8„  VP                  4      P                  V^4      P+                  WEV,          V,          4      P-                  R^4      p\
        P8                  ! VVV4      pMçVR
8”  dá   \;        W*,          V,          ^4      pV'       d-   \
        P.                  ! VV,
          4      P1                  ^4      pVP=                  VR	7       V'       g   W¬P1                  ^4      ,
          p\
        P2                  ! XV4      pVV,
          P%                  V4      pVVR,          P?                  ^ ^4      ,          pV\7        VV4      ,          pV^,
          V
,          p\
        P@                  ! V^4      V,          pVP'                  ^ V^,
          4      pV'       g*   VPC                  4       PE                  V P                  4      pVP                  WEWgV34      # )aó  Compute luts for a batched set of tiles.

Same approach as in OpenCV (https://github.com/opencv/opencv/blob/master/modules/imgproc/src/clahe.cpp)

Args:
    tiles_x_im: set of tiles per image to apply the lut. (B, GH, GW, C, TH, TW)
    num_bins: number of bins. default: 256
    clip: threshold value for contrast limiting. If it is 0 then the clipping is disabled.
    diff: denote if the differentiable histagram will be used. Default: False

Returns:
    Lut for each tile (B, GH, GW, C, 256).

zTensor must be 6D.©rM   rL   gü©ñÒMbP?©rL   Úmpsrd   re   rb   )Úmaxg        Nr"   )#rB   r0   r&   Úviewr\   r   Úpromote_typesrL   Úfloat32ÚlinspacerM   r
   ÚtensorÚ
isinstancer   ÚtypeÚcpuÚdoublerh   rR   r   ÚexpandÚreshapeÚreluÚsumrj   ri   rm   rS   rw   rP   Ú	transposeÚcumsumÚroundrQ   )ro   r_   rp   rq   ÚbÚghÚgwr:   ÚthÚtwÚpixelsr;   ÚhistosÚcentre_dtyperH   ÚlimitsÚmax_valsÚlimitedÚclipped_tensorÚresidual_tensorÚenabledÚmax_valÚclippedr^   ÚredistÚ	lut_scaleÚlutss   &&&&                       r   Ú_compute_lutsr›   Ö   sL  € ð" ‡~�~Ó˜1ÔÜÐ1Ó2Ð2à%×+Ñ+Ñ€Aˆ2�"Ø•'€FØ$Ÿ/™/¨"¨fÓ5€Eßô ˜eÓ.‰ô ×*Ò*¨5¯;©;¼¿¹ÓFˆÜ"Ÿ^š^¨A¨q°(Ç<Á<ÐWcÔdˆä˜5¬¯ª°UÀ,Ô(OÓPˆØÕˆä�$œŸ™×%Ó%ð
 )-¯©×(8Ñ(8¸EÔ(A�—‘“×"Ñ"Ô$ÀtÇ{Á{Ã}ˆØ•O×(Ñ(¨ÀÐ(ÓI×OÑOÐTUÐOÓVˆä" 8¨V¯]©]¸F¿L¹LÓIß‰T�!�Q‹Zß‰V�A˜B•w •{Ó#ß‰W�R˜‹^ð	 	ð —,‘, 8�,Ó,ˆßAEœŸš F¨XÕ$5Ó6×:Ñ:¸1Ô=È6ÐT[×T_ÑT_Ð`aÓTbÕKbˆÜŸ/š/¨.¸(ÓCˆØ˜n¨Õ>À(ÕJ×UÑUÐVWÓXÕXˆØÔ,¨_¸hÓGÕGˆÜ(¨°©°6·=±=ÓA×FÑFÀqÈ!ÓL×SÑSÐTUÐ\^ÕW^ÐabÕWbÓc×kÑkÐlnÐpqÓrˆÜ—’˜W g¨vÓ6‰Ø	�ŒÜ˜T�]¨hÕ6¸Ó:ˆßÜ$)§J¢J¨v¸Õ/?Ó$@×$DÑ$DÀQÓ$GˆGØ�‰˜'ˆÔ"ßØŸz™z¨!›}Õ,ˆGÜ!&§¢°¸(Ó!CˆØ '¨(Õ 2×7Ñ7¸ÓAˆØ�&˜•,×(Ñ(¨¨AÓ.Õ.ˆØÔ" 8¨XÓ6Õ6ˆà  1�¨Õ.€IÜŸš f¨aÓ0°9Õ<€DØ�:‰:�a˜ A�Ó&€Dßà�z‰z‹|�‰˜z×/Ñ/Ó0ˆØ�9‰9�a˜R HÐ-Ó.Ð.r   c                óx   € V ^8„  d   QhR\         P                  R\         P                  R\         P                  /# ©r   rE   rš   r   ©r   r   )r   s   "r   r   r   #  s-   € ÷ 9ñ 9œEŸL™Lð 9´·±ð 9ÄÇÁñ 9r   c           	     óÐ  € V P                  4       ^8w  d   \        R4      hVP                  4       ^8w  d   \        R4      hV P                  w  r#rE p\        P                  ! ^ ^\        P
                  R7      pV^8”  d€   \        P                  ! ^V^,
          4      P                  V^,
          ^4      P                  ^RR7      p\        P                  ! . R	O. RO.V^,
          ^,          ,          4      pWx,          p\        P                  ! ^ ^\        P
                  R7      p	V^8”  d€   \        P                  ! ^V^,
          4      P                  V^,
          ^4      P                  ^RR7      p
\        P                  ! . RO. RO.V^,
          ^,          ,          4      p	Wš,          p	\        WqP                  4      p\        W‘P                  4      p	\        P                  ! W#V^WQP                  R
,          3R
V P                  V P                  R7      pVR^ R\        V^,          ^,
          ^4      1^ R\        V^,          ^,
          ^4      13,          VR^ RV^,
          1^ RV^,
          1^ 3&   VRVR,          ^ R\        V^,          ^,
          ^4      13,          VR^R
1^ RV^,
          1^ 3&   VRVR,          ^ R\        V^,          ^,
          ^4      13,          VR^R
1^ RV^,
          1^3&   VR^ R\        V^,          ^,
          ^4      1V	R,          3,          VR^ RV^,
          1^R
1^ 3&   VR^ R\        V^,          ^,
          ^4      1V	R,          3,          VR^ RV^,
          1^R
1^3&   VRVP                  \        V^,
          ^4      ^^4      P!                  ^^ ^4      V	P                  \        V^,
          ^4      ^^4      3,          VR^R
1^R
1R3&   V# )zõAssign the required luts to each tile.

Args:
    interp_tiles: set of interpolation tiles. (B, 2GH, 2GW, C, TH/2, TW/2)
    luts: luts for each one of the original tiles. (B, GH, GW, C, 256)

Returns:
     mapped luts (B, 2GH, 2GW, 4, C, 256)

úinterp_tiles tensor must be 6D.úluts tensor must be 5D.ru   Útruncre   rK   rN   N)é    r£   é   r¤   r"   )r"   r"   r£   r£   )r£   r¤   r£   r¤   )r"   r£   r"   r£   )rN   r£   )rN   r   )rN   r¤   )rB   r0   r&   r   ÚemptyrO   rg   rx   rh   r|   r   rM   ÚfullrL   rw   ÚrepeatÚpermute)rE   rš   Únum_imgsr‰   rŠ   r:   Ú_Új_idxsÚj_floorÚi_idxsÚi_floorÚluts_x_interp_tiless   &&          r   Ú	_map_lutsr°   #  s¹  € ð ×ÑÓ˜QÔÜÐ>Ó?Ð?à‡x�xƒz�Q„ÜÐ6Ó7Ð7ð !-× 2Ñ 2Ñ€H�"˜˜Aô �[Š[˜˜A¤U§Z¡ZÔ0€FØ	ˆA„vÜ—,’,˜q " q¥&Ó)×.Ñ.¨r°A­v°qÓ9×=Ñ=¸aÈwÐ=ÓWˆÜ—’š|ª^Ð<ÀÀaÅÈAÅÕNÓOˆØÕˆä�[Š[˜˜A¤U§Z¡ZÔ0€FØ	ˆA„vÜ—,’,˜q " q¥&Ó)×.Ñ.¨r°A­v°qÓ9×=Ñ=¸aÈwÐ=ÓWˆÜ—’š|ª^Ð<ÀÀaÅÈAÅÕNÓOˆØÕˆô $ F¯K©KÓ8€FÜ# F¯K©KÓ8€Fô
 ).¯
ª
Ø	�r˜1˜a§¡¨B¥Ð0°"¸L×<NÑ<NÐWc×WjÑWjô)Ðð ;?¸qÀ!ÐBZÄsÈ2ÐQRÍ7ÐUVÍ;ÐXYÓGZÐBZÐ\]Ð\tÔadÐegÐklÕelÐopÕepÐrsÓatÐ\tÐ?tÕ:uÐ˜˜1˜;  Q¥˜;¨¨¨R°!­V¨°QÐ6Ñ7à37¸¸6À$½<ÈÐIaÌcÐRTÐXYÕRYÐ\]ÕR]Ð_`ÓNaÐIaÐ8aÕ3bÐ˜˜1˜R˜4   b¨1¥f ¨aÐ/Ñ0Ø37¸¸6À$½<ÈÐIaÌcÐRTÐXYÕRYÐ\]ÕR]Ð_`ÓNaÐIaÐ8aÕ3bÐ˜˜1˜R˜4   b¨1¥f ¨aÐ/Ñ0à37¸¸1Ð;SÄÀBÈ!ÅGÈaÅKÐQRÓ@SÐ;SÐU[Ð\`ÕUaÐ8aÕ3bÐ˜˜1˜;  Q¥˜;¨¨"¨¨aÐ/Ñ0Ø37¸¸1Ð;SÄÀBÈ!ÅGÈaÅKÐQRÓ@SÐ;SÐU[Ð\`ÕUaÐ8aÕ3bÐ˜˜1˜;  Q¥˜;¨¨"¨¨aÐ/Ñ0à,0Ø	ˆ6�=‰=œ˜R !�V Q›¨¨AÓ.×6Ñ6°q¸!¸QÓ?ÀÇÁÌsÐSUÐXYÕSYÐ[\Ë~Ð_`ÐbcÓAdÐdõ-Ð˜˜1˜R˜4  2  qÐ(Ñ)ð Ðr   c                óx   € V ^8„  d   QhR\         P                  R\         P                  R\         P                  /# r�   rž   )r   s   "r   r   r   _  s5   € ÷ T&ñ T&¬5¯<©<ð T&¼u¿|¹|ð T&ÔPU×P\ÑP\ñ T&r   c           	     ó¾
  € V P                  4       ^8w  d   \        R4      hVP                  4       ^8w  d   \        R4      h\        W4      pV P                  w  r4rVrxV \	        V P
                  4      ,          P                  4       P                  R	R
4      p	V	P                  R4      P                  W4V^WgV,          4      p	\        P                  ! V^V	4      P                  V 4      P                  W4V^WgV4      p
\        P                  ! V 4      p\        P                  ! ^V,          ^ R
V P
                  V P                   R7      P#                  R4      P%                  RV,          4      R,          P'                  R	R
4      P                  ^V,          V4      pVP)                  ^ Ww4      P)                  ^Wˆ4      p\        P                  ! ^V,          ^ R
V P
                  V P                   R7      P#                  R4      P%                  RV,          4      P                  V^V,          4      pVP)                  ^ Ww4      P)                  ^Wˆ4      pVP                  V^,
          ^,          ^Wx4      P                  V^,
          ^Wx4      P                  ^ 4      pVP+                  V^,
          ^,          ^^^4      P                  ^4      pV
R^R
1^R
13,          P-                  ^4      w  pppp\        P.                  ! VV\        P"                  ! VV4      4      p\        P.                  ! VV\        P"                  ! VV4      4      p\        P.                  ! VV\        P"                  ! VV4      4      VR^R
1^R
13&   V
R^ RV^,
          1^ RV^,
          1^ 3,          VR^ RV^,
          1^ RV^,
          13&   V
R^R
1^ 3,          P-                  ^4      w  pp p\        P.                  ! VVP1                  ^4      \        P"                  ! VV4      4      VR^R
1^ 3&   V
R^R
1V^,
          3,          P-                  ^4      w  pp p\        P.                  ! VVP1                  ^4      \        P"                  ! VV4      4      VR^R
1V^,
          3&   V
R^ ^R
13,          P-                  ^4      w  pp p\        P.                  ! VV\        P"                  ! VV4      4      VR^ ^R
13&   V
RV^,
          ^R
13,          P-                  ^4      w  pp p\        P.                  ! VV\        P"                  ! VV4      4      VRV^,
          ^R
13&   VP%                  R4      # )a  Equalize the tiles.

Args:
    interp_tiles: set of interpolation tiles, values must be in the range [0, 1].
      (B, 2GH, 2GW, C, TH/2, TW/2)
    luts: luts for each one of the original tiles. (B, GH, GW, C, 256)

Returns:
    equalized tiles (B, 2GH, 2GW, C, TH/2, TW/2)

r    r¡   rK   g      à?g       @NrN   ç     ào@r!   r"   r#   )rB   r0   r°   r&   r	   rL   rO   Úflattenri   r�   r   ÚgatherrQ   r‚   Ú
zeros_likerg   rM   Úsubrh   r…   r-   r§   ÚunbindÚaddcmulr.   )rE   rš   Úmapped_lutsr©   r‰   rŠ   r:   r‹   rŒ   Úflatten_interp_tilesÚpreinterp_tiles_equalizedÚtiles_equalizedÚihÚiwÚtiwÚtihÚtlÚtrÚblÚbrÚtrˆ   rª   ÚleftÚrights   &&                       r   Ú_compute_equalized_tilesrÉ   _  sÀ  € ð ×ÑÓ˜QÔÜÐ>Ó?Ð?à‡x�xƒz�Q„ÜÐ6Ó7Ð7ä )¨,Ó =€Kð #/×"4Ñ"4Ñ€H�"˜ð
 +7¼Àl×FXÑFXÓ9YÕ*Y×)_Ñ)_Ó)a×)iÑ)iÐjlÐnpÓ)qÐØ/×9Ñ9¸"Ó=×DÑDÀXÐSUÐWXÐZ[ÐbdÕ]dÓeÐä�Š�[ !Ð%9Ó:ß	‰ˆLÓ	ß	‰�˜r 1 a¨RÓ	0ð ô %*×$4Ò$4°\Ó$B€Oô 	�Š�Q˜•V˜Q ¨,×*<Ñ*<À\×EXÑEXÔYß	‰ˆS‹ß	‰ˆS�2�X‹�tõ	÷ 
‰�2�rÓ	ß	‰��B•˜Ó	ð ð 
�‰�1�bÓ	×	$Ñ	$ Q¨Ó	/€Bä�Š�Q˜•V˜Q ¨,×*<Ñ*<À\×EXÑEXÔYß	‰ˆS‹ß	‰ˆS�2�X‹ß	‰��A˜•FÓ	ð	 ð 
�‰�1�bÓ	×	$Ñ	$ Q¨Ó	/€Bð �)‰)�R˜!•V •M 1 bÓ
-×
5Ñ
5°b¸1µf¸aÀÓ
H×
RÑ
RÐSTÓ
U€CØ
�)‰)�R˜!•V •M 1 a¨Ó
+×
5Ñ
5°aÓ
8€Cð /¨q°!°B°$¸¸"¸¨}Õ=×DÑDÀQÓG�N€BˆˆB�Ü�Š�b˜#œuŸyšy¨¨RÓ0Ó1€AÜ�Š�b˜#œuŸyšy¨¨RÓ0Ó1€AÜ%*§]¢]°1°c¼5¿9º9ÀQÈ»?Ó%K€O�A�q˜�t˜Q˜r˜T�MÑ"ð 4MÈQÐPQÐP[ÐUWÐZ[ÕU[ÐP[Ð]^Ð]hÐbdÐghÕbhÐ]hÐjkÐMkÕ3l€O�A�q�{˜B �F�{ A K¨¨a­ KÐ/Ñ0ð +¨1¨a°¨d°A¨:Õ6×=Ñ=¸aÓ@�J€A€qˆ!ˆQÜ"'§-¢-°°3·;±;¸q³>Ä5Ç9Â9ÈQÐPQÃ?Ó"S€O�A�q˜�t˜Q�JÑØ*¨1¨a°¨d°B¸µF¨?Õ;×BÑBÀ1ÓE�J€A€qˆ!ˆQÜ',§}¢}°Q¸¿¹ÀA»ÌÏ	Ê	ÐRSÐUVËÓ'X€O�A�q˜�t˜R !�V�OÑ$ð 2°!°Q¸¸"¸°*Õ=×DÑDÀQÓGÑ€Dˆ%��AÜ"'§-¢-°°s¼E¿IºIÀdÈEÓ<RÓ"S€O�A�q˜!˜B˜$�JÑØ1°!°R¸!µV¸Q¸r¸T°/ÕB×IÑIÈ!ÓLÑ€Dˆ%��AÜ',§}¢}°U¸CÄÇÂÈ4ÐQVÓAWÓ'X€O�A�r˜A•v˜q ˜t�OÑ$ð ×Ñ˜uÓ%Ð%r   c                ó¦   € V ^8„  d   QhR\         P                  R\         P                  R\        \        \        3,          R\         P                  /# )r   r   rš   r?   r   r@   )r   s   "r   r   r   ¶  sD   € ÷ '/ñ '/¤E§L¡Lð '/¼¿¹ð '/ÔQVÔWZÔ\_ÐW_ÕQ`ð '/Ôej×eqÑeqñ '/r   c                ó6  € V P                   w  r4rVVP                   R,          w  rxVw  ršV P                  \        P                  8X  d   \        P                  M\        P                  p\        P
                  ! WPP                  VR7      V	^,
          ^,          ,
          V	,          p\        P
                  ! W`P                  VR7      V
^,
          ^,          ,
          V
,          pVP                  4       pVP                  4       pVP                  4       P                  ^ V^,
          4      pVP                  4       ^,           P                  ^ V^,
          4      pVP                  4       P                  ^ V^,
          4      pVP                  4       ^,           P                  ^ V^,
          4      pWÎ,
          P                  ^ ^4      pWß,
          P                  ^ ^4      p\        P                  ! V^ 8  WÇ^,
          8„  ,          ^ V4      P                  V P                  4      p\        P                  ! V^ 8  WØ^,
          8„  ,          ^ V4      P                  V P                  4      pVP                  V P                  4      pV \        V P                  4      ,          P                  4       p\        P
                  ! W0P                  R7      P                  V^^^4      p\        P
                  ! W@P                  R7      P                  ^V^^4      pVP                  ^^V^4      VP                  ^^V^4      ppVP                  ^^^V4      VP                  ^^^V4      ppVVVVVV3,          pVVVVVV3,          pVVVVVV3,          pVVVVVV3,          p VP                  ^^V^4      pVP                  ^^^V4      pVVV,
          ^V,
          ,          ,           p!V VV ,
          ^V,
          ,          ,           p"V!V"V!,
          V,          ,           R,          # )zEEqualize odd-sized CLAHE tiles by interpolating LUTs at tile centres.:r¤   é   Nrt   ra   r³   )r&   rL   r   Úfloat64rz   rg   rM   rd   rO   rR   rS   rQ   r	   rx   )#r   rš   r?   Ú
batch_sizeÚchannelsÚheightÚwidthÚgrid_hÚgrid_wÚtile_hÚtile_wÚcoord_dtypeÚyrY   Úy_floorÚx_floorÚy0Úy1Úx0Úx1ÚwyÚwxÚ
pixel_binsÚ	batch_idxÚchannel_idxÚrows0Úrows1Úcols0Úcols1Útop_leftÚ	top_rightÚbottom_leftÚbottom_rightÚtopÚbottoms#   &&&                                r   Ú_equalize_clahe_odd_tilesrí   ¶  s:  € à*.¯*©*Ñ'€J˜&Ø—Z‘Z •_�N€FØ�N€Fà#'§:¡:´·±Ô#>”%—-’-ÄEÇMÁM€KÜ	�Š�f§[¡[¸Ô	DÈÐQRÍ
ÐVWÕGWÕ	WÐ[aÕa€AÜ	�Š�e§K¡K°{Ô	CÀvÐPQÅzÐUVÕFVÕ	VÐZ`Õ`€Aà�g‰g‹i€GØ�g‰g‹i€GØ	�‰‹×	Ñ	˜a ¨!¥Ó	,€BØ
�,‰,‹.˜1Õ
×	#Ñ	# A v°¥zÓ	2€BØ	�‰‹×	Ñ	˜a ¨!¥Ó	,€BØ
�,‰,‹.˜1Õ
×	#Ñ	# A v°¥zÓ	2€Bà
�+×	Ñ	˜Q Ó	"€BØ
�+×	Ñ	˜Q Ó	"€BÜ	�Š�a˜!‘e ¨Q¥J¡Õ/°°BÓ	7×	:Ñ	:¸4¿:¹:Ó	F€BÜ	�Š�a˜!‘e ¨Q¥J¡Õ/°°BÓ	7×	:Ñ	:¸4¿:¹:Ó	F€Bà�7‰7�4—:‘:Ó€DØœ d§j¡jÓ1Õ1×7Ñ7Ó9€JÜ—’˜Z·±Ô<×AÑAÀ*ÈaÐQRÐTUÓV€IÜ—,’,˜x·±Ô<×AÑAÀ!ÀXÈqÐRSÓT€KØ—7‘7˜1˜a ¨Ó+¨R¯W©W°Q¸¸6À1Ó-Eˆ5€EØ—7‘7˜1˜a  EÓ*¨B¯G©G°A°q¸!¸UÓ,Cˆ5€Eà�I˜u e¨[¸*ÐDÕE€HØ�Y  u¨k¸:ÐEÕF€IØ�y %¨°¸ZÐGÕH€KØ˜	 5¨%°¸jÐHÕI€Là	�‰��A�v˜qÓ	!€BØ	�‰��A�q˜%Ó	 €Bà
�x )Õ+°°BµÕ7Õ
7€CØ˜[¨<Õ7¸AÀ½FÕCÕC€FØ�6˜C•< 2Õ%Õ%¨Õ.Ð.r   c          
      óž   € V ^8„  d   QhR\         P                  R\        R\        \        \        3,          R\
        R\         P                  /# ©r   ÚinputÚ
clip_limitr   Úslow_and_differentiabler   )r   r   rr   r   r   r   )r   s   "r   r   r   á  sQ   € ÷ >Rñ >RÜ�<‰<ð>Räð>Rô ”Sœ#�X�ð>Rô "ð	>Rô
 ‡\�\ñ>Rr   c                óŒ   € \        V R4       \        V\        4      '       g   \        R\	        V4       24      h\        WW#4      # )a(  Equalize an image tile by tile with a clipped histogram (CLAHE).

``out = equalize_clahe(input, clip_limit, grid_size)`` equalizes each channel of an image in :math:`[0, 1]` in every
tile of a ``grid_size`` grid and blends the tiles' lookup tables where they meet. Clipping the histogram at
``clip_limit`` limits the local contrast gain.

.. image:: _static/img/equalize_clahe.png

Convention:
    - The tiles are ``ceil(H / grid_size[0])`` by ``ceil(W / grid_size[1])`` pixels, and an axis the grid does not
      divide is reflect-padded at its end. OpenCV uses the same tiles when the grid divides both axes or neither;
      when it divides one, OpenCV also pads that axis by one pixel per tile and Kornia does not.
    - Lookup tables are blended with weights anchored at each tile's centre, ``(p + 0.5) / tile_size - 0.5`` for
      pixel ``p``, which keeps flip symmetry on unpadded grids. This is half a pixel from the phase of OpenCV and
      scikit-image, a blending-weight difference of at most ``0.5 / tile_size`` per axis, so the output differs
      from ``cv2.createCLAHE`` even for the same lookup tables. The output is not rounded to 8-bit levels.
    - The default path rounds the lookup tables to nearest like OpenCV; ``slow_and_differentiable=True`` does not.

Args:
    input: image with values in :math:`[0, 1]` and shape ``(*, C, H, W)``.
    clip_limit: caps each bin at ``max(clip_limit * tile_pixels // 256, 1)`` counts and spreads the excess over all
        bins. ``0.0`` disables clipping. A ``float``; an ``int`` raises ``TypeError``.
    grid_size: number of tiles ``(GH, GW)`` along H and W, a tuple of ``int``. Each image axis must be larger than
        its grid size; the ``ValueError`` otherwise names the smallest image the grid admits.
    slow_and_differentiable: count with a soft histogram, so gradients reach ``input``; the default path has none.
        It is slower and, without the lookup-table rounding, its values differ from the default path's.

Returns:
    Equalized image with the shape of ``input``.

.. note::
   An integer or boolean image raises :exc:`~kornia.core.exceptions.TypeCheckError` (a :exc:`TypeError`), and values
   whose level before truncation lies outside ``(-1, 256)`` raise a ``RuntimeError`` naming the range, as for
   :func:`equalize`. That function also describes the 8-bit level the lookup reads and when the range error
   surfaces on CUDA and MPS.

See Also:
    - :func:`equalize`: equalizes the whole channel with one histogram.
    - :class:`~kornia.augmentation.RandomClahe`: applies it with random parameters per image.

Examples:
    >>> img = torch.rand(1, 10, 20)
    >>> res = equalize_clahe(img)
    >>> res.shape
    torch.Size([1, 10, 20])

    >>> img = torch.rand(2, 3, 10, 20)
    >>> res = equalize_clahe(img)
    >>> res.shape
    torch.Size([2, 3, 10, 20])

Úequalize_clahez(Input clip_limit type is not float. Got )r   r}   rr   Ú	TypeErrorr~   Ú_equalize_clahe)rð   rñ   r   rò   s   &&&&r   rô   rô   à  sC   € ôv ˜%Ð!1Ô2Ü�j¤%×(Ò(ÜÐBÄ4È
ÓCSÐBTÐUÓVÐVä˜5¨iÓQÐQr   c          
      óÔ   € V ^8„  d   QhR\         P                  R\        \        \         P                  3,          R\        \
        \
        3,          R\        R\         P                  /# rï   )r   r   r   rr   r   r   r   )r   s   "r   r   r   "  s_   € ÷ Añ AÜ�<‰<ðAä”eœUŸ\™\Ð)Õ*ðAô ”Sœ#�X�ðAô "ð	Aô
 ‡\�\ñAr   c                ó  € \        V\        4      '       g   \        R\        V4       24      h\	        V4      ^8w  d   \        R\	        V4       24      h\        V^ ,          \
        4      '       g   \        V^,          \
        4      '       d   \        R4      hV^ ,          ^ 8:  g   V^,          ^ 8:  d   \        RV 24      hV \        V P                  4      ,          p\        VR8„  VR8  ,          P                  4       RRR7       T pVP                  RR	 w  rgVw  r‰Wh8:  g   Wy8:  d3   \        R
V RV R\        V4       RV^,            RV	^,            R24      hV) V,          ) p
V) V	,          ) pV
^,          ^8H  ;'       g    V^,          ^8H  p\        WRV'       * 4      w  rÞVP                  R,          VP                  R,          3p\        WÑVR7      pV'       d   \        VVV4      pM<\        Wï4      p\!        VV4      pVP#                  ^ ^^^^^4      P%                  V4      pVP                  RR	 w  ppVRR	V1R	V13,          pV P'                  4       VP'                  4       8w  d   VP)                  ^ 4      pV# )zIEqualize a BCHW tensor using a scalar or one tensor clip limit per image.z'Input grid_size type is not Tuple. Got z4Input grid_size is not a Tuple with 2 elements. Got z=Input grid_size type is not valid, must be a Tuple[int, int].z/Input grid_size elements must be positive. Got g      p@zequalize_clahe expects input values in [0, 1]. Scale the image into that range first, for example image / 255.0 for 8-bit data.T)ÚguardNr   r   r   r   r   )rp   rq   .g      ð¿r!   r"   )r}   r(   rõ   r~   Úlenrr   r'   r	   rL   r   Úallr&   r<   r›   rí   rF   rÉ   r¨   Ú
reshape_asrB   r.   )rð   rñ   r   rò   Úlevelsr   rÐ   rÑ   rÒ   rÓ   Únatural_tile_hÚnatural_tile_wÚuse_odd_tilesÚ
hist_tilesÚ
img_paddedr?   rš   Úeq_imgsrE   Úequalized_tilesr2   r3   s   &&&&                  r   rö   rö   "  sx  € ô �i¤×'Ò'ÜÐAÄ$ÀyÃ/ÐARÐSÓTÐTä
ˆ9ƒ~˜ÔÜÐNÌsÐS\Ë~ÐN^Ð_Ó`Ð`ä�)˜A•,¤×&Ò&¬*°Y¸qµ\Ä5×*IÒ*IÜÐWÓXÐXà�…|�qÔ˜I a�L¨AÔ-ÜÐJÈ9È+ÐVÓWÐWð ”\ %§+¡+Ó.Õ.€FÜØ
�4‰-˜F U™NÕ	+×0Ñ0Ó2ð	4àõ	ð €Dà—J‘J˜r˜s�O�M€FØ�N€FØÔ˜5œ?ÜðØ%˜h b¨¨Ð/?ÄÀiÓ@PÐ?Qð R7Ø7=Àµz°lÀ"ÀVÈaÅZÀLÐPRðTó
ð 	
ð �w &Õ(Ð)€NØ�v Õ'Ð(€NØ" QÕ&¨!Ñ+×FÐF¨~ÀÕ/AÀQÑ/F€Mä+¨DÀÔ=NÓOÑ€JØ",×"2Ñ"2°2Õ"6¸
×8HÑ8HÈÕ8LÐ!M€IÜ& zÐI`Ôa€DçÜ+¨J¸¸iÓH‰ä3°JÓJˆÜ2°<ÀÓFˆØ!×)Ñ)¨!¨Q°°1°a¸Ó;×FÑFÀzÓRˆà�:‰:�b�cˆ?�D€A€qØ�c˜2˜A˜2˜r ˜r�kÕ"€Gð ‡y�yƒ{�g—k‘k“mÔ#Ø—/‘/ !Ó$ˆà€Nr   )F)é   ç      D@F)r  )é   r  F)Ú__doc__r)   Útypingr   r   r   Útorch.nn.functionalÚnnÚ
functionalr+   Úkornia.core.checkr   Úkornia.core.utilsr   r   Úkornia.image.utilsr   Úadjustr   r	   r
   r<   rF   r\   rm   r›   r°   rÉ   rí   rô   rö   © r   r   Ú<module>r     sx   ðñ$ Oã ß ã ß Ð å *ß VÝ 7ç 7Ý  ÷IõX,õ^õ@"÷2J/õZ9õxT&õn'/ðT ö>Ró ð>R÷BAr   