
    h5                       S r SSKJr  SSKrSSKJr  SSKJr  SSKJ	r	  SSK
Jr  SSKJr  SSKrSSKrSS	KJrJrJrJrJr  SS
KJr  SSKJr  SSKJr  SSKJr  / SQr\SS j5       r \\SS j5       5       r!\              SS j5       r"SS jr#S S jr$\\S!S j5       5       r%S"S jr&S#S$S jjr'S%S&S jjr(            S'S jr)S(S jr*S%S)S jjr+S%S)S jjr,S*S jr-g)+aO  Functional implementations of various blur operations for image processing.

This module provides a collection of low-level functions for applying different blur effects
to images, including standard blur, median blur, glass blur, defocus, and zoom effects.
These functions form the foundation for the corresponding transform classes.
    )annotationsN)Sequence)product)ceil)Literal)warn)clipped
float32_iomaybe_process_in_chunkspreserve_channel_dimuint8_io)ValidationInfo)scale)convolve)EIGHT)box_blurcentral_zoomdefocus
glass_blurmedian_blur	zoom_blurc                D    [        [        R                  X4S9nU" U 5      $ )zBlur an image.

This function applies a blur to an image.

Args:
    img (np.ndarray): Input image.
    ksize (int): Kernel size.

Returns:
    np.ndarray: Blurred image.

ksize)r   cv2blurimgr   blur_fns      f/var/www/fran/franai/venv/lib/python3.13/site-packages/albumentations/augmentations/blur/functional.pyr   r      s      &chhunEG3<    c                B    [        [        R                  US9nU" U 5      $ )zMedian blur an image.

This function applies a median blur to an image.

Args:
    img (np.ndarray): Input image.
    ksize (int): Kernel size.

Returns:
    np.ndarray: Median blurred image.

r   )r   r   
medianBlurr   s      r    r   r   /   s     &cnnEBG3<r!   c                   [         R                  " [        R                  " U 5      USS9nUS:X  a  [        R                  " U R
                  S   U-
  US5      n[        R                  " U R
                  S   U-
  US5      n[        R                  " XxR
                  S   5      n	[        R                  " XR
                  S   5      n
[        U5       H6  nUSS2US4   nUSS2US4   nXiU-   X-   4   XiU
4   sXiU
4'   XiU-   X-   4'   M8     OUS:X  a  [        [        [        U5      [        U R
                  S   U-
  US5      [        U R
                  S   U-
  US5      5      5       HU  u  nu  pn
U[        U5      :  a  UOU[        U5      -  nXOUS4   nXOUS4   nXiU-   X-   4   XiU
4   sXiU
4'   XiU-   X-   4'   MW     O[        S	U S
35      e[         R                  " XaSS9$ )aP  Glass blur an image.

This function applies a glass blur to an image.

Args:
    img (np.ndarray): Input image.
    sigma (float): Sigma.
    max_delta (int): Maximum delta.
    iterations (int): Number of iterations.
    dxy (np.ndarray): Dxy.
    mode (Literal["fast", "exact"]): Mode.

Returns:
    np.ndarray: Glass blurred image.

)r   r   )sigmaXr   fastr      NexactzUnsupported mode `z$`. Supports only `fast` and `exact`.)r   GaussianBlurnparrayarangeshapetilerepeatrange	enumerater   len
ValueError)r   sigma	max_delta
iterationsdxymodexhswshwidydxindidxs                   r    r   r   B   s   2 	#uFCAv~YYsyy|i/B?YYsyy|i/B? ggb((1+6 iiHHQK8z"AQ1WBQ1WB)*r616>):AdG&AdGQ2vqv~& #
 
'j!ciilY.	2>ciilY.	2>
NC! s3x#S3s8^C!QYB!QYB)*r616>):AdG&AdGQ2vqv~&
 -dV3WXYYA6::r!   c                   [         R                  " [        SU5      * [        SU5      S-   5      nU[        ::  a  SOSn[         R                  " X35      u  pV[         R
                  " US-  US-  -   US-  :*  [         R                  S9nU[         R                  " U5      -  n[        R                  " XtU4US9n[        XS9$ )	zDefocus an image.

This function defocuses an image.

Args:
    img (np.ndarray): Input image.
    radius (int): Radius.
    alias_blur (float): Alias blur.

Returns:
    np.ndarray: Defocused image.

   r(            dtype)r%   )kernel)r+   r-   maxr   meshgridr,   float32sumr   r*   r   )	r   radius
alias_blurlengthr   r:   yaliased_diskrK   s	            r    r   r   z   s     YYAvAv(:;F5AaE;;v&DA88QTAqD[VQY6bjjILBFF<((LlEN:NFC''r!   c                ,   U R                   SS u  p#[        X!-  5      [        X1-  5      pTX$-
  S-  X5-
  S-  pv[        XXd-   2XwU-   24   U[        R                  5      n U R                   S   U-
  S-  U R                   S   U-
  S-  pXX-   2XU-   24   $ )zCentral zoom an image.

This function zooms an image.

Args:
    img (np.ndarray): Input image.
    zoom_factor (int): Zoom factor.

Returns:
    np.ndarray: Zoomed image.

NrH   r   r(   )r.   r   r   r   INTER_LINEAR)
r   zoom_factorheightwidthh_chw_chh_topw_top
h_trim_top
w_trim_tops
             r    r   r      s     IIbqMMFf*+T%2E-F$Ma'%,1)<5
EL(%$,*>>?cN^N^
_C!iilV39CIIaL5<PUV;V
J//5>P1PPQQr!   c                    [         R                  " U [         R                  S9nU H  nU[        X5      -  nM     X-   [	        U5      S-   -  $ )zZoom blur an image.

This function zooms and blurs an image.

Args:
    img (np.ndarray): Input image.
    zoom_factors (np.ndarray | Sequence[int]): Zoom factors.

Returns:
    np.ndarray: Zoomed and blurred image.

rI   r(   )r+   
zeros_likerN   r   r3   )r   zoom_factorsoutrW   s       r    r   r      sJ     --2::
.C#|C-- $ I#l+a/00r!   c           
         U S   U:  d	  U S   U:  a9  [        XS   5      [        XS   5      4n[        U SU  SU SU S3[        SS9  U$ U $ )	Nr   r(   z: Invalid kernel size range z. Values less than z2 are not allowed. Range automatically adjusted to .rH   
stacklevel)rL   r   UserWarning)result	min_value
field_name
new_results       r    _ensure_min_valuerm      sy    ay9q	I 5)AY/Yq	1JK
l6vh ?  ){ +//9l!= 	
 Mr!   c                    U S   S:X  d  U S   S-  S:X  a  U S   OU S   S-   U S   S:X  d  U S   S-  S:X  a  U S   OU S   S-   4nX :w  a  [        U SU  SU S3[        SS9  U$ )Nr   rH   r(   z+: Non-zero kernel sizes must be odd. Range z automatically adjusted to re   rf   )r   rh   )ri   rk   rl   s      r    _ensure_odd_valuesro      s    AY!^vay1}'9q	vay1}AY!^vay1}'9q	vay1}J lEfXMhishttuv	

 r!   c                   [        U [        5      (       a7  [        U 5      S:w  a  [        S5      e[	        U S   5      [	        U S   5      4nOU[	        U 5      4n[        X2UR                  5      n[        X1R                  5      nUS   US   :  a.  US   US   4n[        UR                   SU SU S3[        SS9  U$ U$ )	z0Process blur limit to ensure valid kernel sizes.rH   z(Sequence must contain exactly 2 elementsr   r(   z: Invalid range z. (min > max). Range automatically adjusted to re   rf   )

isinstancer   r3   r4   intrm   rk   ro   r   rh   )valueinforj   ri   final_results        r    process_blur_limitrv      s     %""u:?GHHeAh-U1X/SZ(v$//BF8Fay6!9q	6!9-/x7efresstu	

 Mr!   c                T   [         R                  " USS5      n[         R                  " X 4[         R                  S9nU S-  n[         R                  " U5      n[         R
                  " U5      n[         R                  " U5      n	U S-  n
US:  a   [        U5      n[        U
* 5      nU
SU-
  -  nO4US:  a  UnU
* SU-
  -  n[        U
5      nO[        U
* 5      n[        U
5      n[         R                  " XU 5      nXhU-  -   nXiU-  -   nU(       a:  UR                  SS5      U
-  S-  nUR                  SS5      U
-  S-  nUU-  nUU-  n[         R                  " [         R                  " U5      SU S-
  5      R                  [        5      n[         R                  " [         R                  " U5      SU S-
  5      R                  [        5      n[         R                  " [         R                  " UU/5      SS9nSUUS	S	2S4   US	S	2S4   4'   UR!                  5       (       d  SXVU4'   U$ )
a  Create a motion blur kernel.

Args:
    kernel_size (int): Size of the kernel (must be odd)
    angle (float): Angle in degrees (counter-clockwise)
    direction (float): Blur direction (-1.0 to 1.0)
    allow_shifted (bool): Allow kernel to be randomly shifted from center
    random_state (random.Random): Python's random.Random instance

Returns:
    np.ndarray: Motion blur kernel

g      g      ?rI   rH   r   r(   r'   )axisN)r+   clipzerosrN   deg2radcossinabsfloatlinspaceuniformroundastyperr   uniquecolumn_stackany)kernel_sizeangle	directionallow_shiftedrandom_staterK   center	angle_radrA   r@   line_lengthbias_factort_startt_endtr:   rS   shift_xshift_ypointss                       r    create_motion_kernelr      s   * 	4-IXX{0

CFAF 

5!I 
		B			B "K 1} )n%q;/	Q  ,!k/2k" %k" 	GK0A 	aAaA &&r1-;a?&&r1-;a?	W	W 	Qa077<A
Qa077<A YYr1v.Q7F)*F6!Q$<1%& ::<<!"v~Mr!   c                    [        SXS-  S:H  -   5      n[        SX"S-  S:H  -   5      n[        X!5      nX:X  a  U$ X!-
  S-  S-   nU R                  SUS-
  5      nUSU-  -   $ )a2  Sample an odd number from the range [low, high] (inclusive).

Args:
    random_state (random.Random): instance of random.Random
    low (int): lower bound (will be converted to nearest valid odd number)
    high (int): upper bound (will be converted to nearest valid odd number)

Returns:
    int: Randomly sampled odd number from the range

Note:
    - Input values will be converted to nearest valid odd numbers:
      * Values less than 3 will become 3
      * Even values will be rounded up to next odd number
    - After normalization, high must be >= low

rF   rH   r   r(   )rL   randint)r   lowhighnum_odd_valuesrand_idxs        r    sample_odd_from_ranger   I  s    & aa1%
&Cq$(a-()D t>D
{
 jQ&*N##A~'9:H!h,r!   c                V   US:X  a  [        U S-  5      S-  S-   OUnUS-  S:X  a  US-   OUn[        R                  " US-  * US-  U5      n[        R                  " SX0-  S-  -  5      nXDR	                  5       -  nUSS2[        R
                  4   U[        R
                  SS24   -  $ )aO  Create a Gaussian kernel following PIL's approach.

Args:
    sigma (float): Standard deviation for Gaussian kernel.
    ksize (int): Kernel size. If 0, size is computed as int(sigma * 3.5) * 2 + 1
           to match PIL's implementation. Otherwise, must be positive and odd.

Returns:
    np.ndarray: 2D normalized Gaussian kernel.

r         @rH   r(         N)rr   r+   r   exprO   newaxisr5   r   sizer:   	kernel_1ds        r    create_gaussian_kernelr   m  s     (-z3us{a!#uD ax1}4!8$D 	daiL$!)T2A tqyQ../IMMO+I Q

]#i

A&>>>r!   c                    US:X  a  [        U S-  5      S-  S-   OUnUS-  S:X  a  US-   OUn[        US9n[        R                  " SX0-  S-  -  5      nXDR	                  5       -  $ )aR  Create a 1D Gaussian kernel following PIL's approach.

Args:
    sigma (float): Standard deviation for Gaussian kernel.
    ksize (int): Kernel size. If 0, size is computed as int(sigma * 3.5) * 2 + 1
           to match PIL's implementation. Otherwise, must be positive and odd.

Returns:
    np.ndarray: 1D normalized Gaussian kernel.

r   r   rH   r(   r   r   )rr   "create_gaussian_kernel_input_arrayr+   r   rO   r   s        r    create_gaussian_kernel_1dr     sr     (-z3us{a!#uD ax1}4!8$D 	+5A tqyQ../I}}&&r!   c           	         U S:  a4  [         R                  " [        [        U S-  * U S-  S-   S5      5      5      $ [         R                  " U S-  * U S-  U 5      $ )a  Creates a 1-D array which will create an array of x-coordinates which will be input for the
gaussian function (values from -size/2 to size/2 with step size of 1)

Piecewise function is needed as equivalent python list comprehension is faster than np.linspace
for values of size < 100

Args:
    size (int): kernel size

Returns:
    np.ndarray: x-coordinate array which will be input for gaussian function that will be used for
    separable gaussian blur

d   rH   r(   )r+   r,   listr1   r   r   s    r    r   r     sS     czxxUTQY<$!)q!DEFF;;|TQY55r!   )r   
np.ndarrayr   rr   returnr   )r   r   r5   r   r6   rr   r7   rr   r8   r   r9   zLiteral['fast', 'exact']r   r   )r   r   rP   rr   rQ   r   r   r   )r   r   rW   rr   r   r   )r   r   rb   znp.ndarray | Sequence[int]r   r   )ri   tuple[int, int]rj   rr   rk   
str | Noner   r   )N)ri   r   rk   r   r   r   )r   )rs   zint | tuple[int, int]rt   r   rj   rr   r   r   )r   rr   r   r   r   r   r   boolr   random.Randomr   r   )r   r   r   rr   r   rr   r   rr   )r5   r   r   rr   r   r   )r   rr   r   r   ).__doc__
__future__r   randomcollections.abcr   	itertoolsr   mathr   typingr   warningsr   r   numpyr+   albucorer	   r
   r   r   r   pydanticr   1albumentations.augmentations.geometric.functionalr   -albumentations.augmentations.pixel.functionalr   $albumentations.core.type_definitionsr   __all__r   r   r   r   r   r   rm   ro   rv   r   r   r   r   r    r!   r    <module>r      sU   #  $     
  a a # C B 6
[  " 	 
 " 4;	4;4; 4; 	4;
 
4; #4; 4; 4;n(4R, 1 	 1*2PPP P 	P
  P Pf! H?:'46r!   