
    h#                   V   % S r SSKJr  SSKrSSKJr  SSKJr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JrJrJrJrJrJrJrJrJrJrJrJrJ r J!r!J"r"J#r#J$r$  SSK%J&s  J's  J(r)  SSK*J+r+J,r,  SS	K-J.r.J/r/J0r0  \$\"          SS
 j5       5       r1\SS j5       r2\$\SS j5       5       r3SSS jjr4SSS jjr5        SS jr6 S     SS jjr7\$\"   S         SS jj5       5       r8\$        SS j5       r9\      SS j5       r:\$\"        SS j5       5       r;\$\"        SS j5       5       r<\$        SS j5       r=      SS jr>\$            SS j5       r?\$\"                  SS j5       5       r@          SS jrA\$\\"            SS j5       5       5       rB\$\"\            SS j5       5       5       rC\$\            SS j5       5       rD\$\"        SS j5       5       rE\$\\"SS j5       5       5       rFSS  jrGSS! jrHSS" jrISS# jrJSS$ jrK\\          SS% j5       5       rLSS& jrM\$\SS' j5       5       rN\SS( j5       rOSS) jrPSS* jrQ\SS+ j5       rR        SS, jrS S     SS- jjrT\"\$          SS. j5       5       rUSS/ jrV\\\"SS0 j5       5       5       rW\"SS1 j5       rX\"SS2 j5       rY\\" S       SS3 jj5       5       rZSS4 jr[SS5 jr\\$\"            SS6 j5       5       r]\\\"            SS7 j5       5       5       r^\"        SS8 j5       r_\\\"SS9 j5       5       5       r`\\\"SS: j5       5       5       ra\$\              SS; j5       5       rb              SS< jrc0 S=/ S>Q_S?/ S@Q_SA/ SBQ_SC/ SDQ_SE/ SFQ_SG/ SHQ_SI/ SJQ_SK/ SLQ_SM/ SNQ_SO/ SPQ_SQ/ SRQ_SS/ STQ_SU/ SVQ_SW/ SXQ_SY/ SZQ_S[/ S\Q_S]/ S^Q_/ S_Q/ S`Q/ SaQ/ SbQ/ ScQ/ SdQ/ SeQ/ SfQSg.E0 SA/ ShQ_SC/ SiQ_SE/ SjQ_SG/ SkQ_SI/ SlQ_SK/ SmQ_SM/ SnQ_SO/ SoQ_SQ/ SpQ_SS/ SqQ_SU/ SrQ_SW/ SsQ_SY/ StQ_S[/ SuQ_S]/ SvQ_Sw/ SxQ_Sy/ SzQ_/ S{Q/ S|Q/ S}Q/ S~Q/ SQ/ SQS.ES.rdS\eS'   \        SS j5       rf\SS j5       rg  S         SS jjrh\"\        SS j5       5       ri        SS jrj                SS jrk            GS S jrl            GS S jrm            GSS jrn        GSS jro        GSS jrp        GSS jrq        GSS jrr            GS S jrs\\"          GSS j5       5       rt        GSS jru\R                  " / SQ/ SQ/ SQ/\R                  S9rx\R                  " / SQ/ SQ/ SQ/\R                  S9rySrz        GSS jr{\\          GSS j5       5       r|\        GSS j5       r}GS	S jr~\GS
S j5       r\        GSS j5       r\          GSS j5       r\$          GSS j5       r            GSS jrGSS jr          GSS jr        GSS jr        GSS jrGSS jr        GSS jr              GSS jr\R                  " / SQ/ SQ/5      \R                  " / SQ/ SQ/5      \R                  " / SQ/ SQ/5      \R                  " / SQ/ SQ/5      \R                  " / SQ/ SQ/5      \R                  " / SQ/ SQ/5      \R                  " / SQ/ SQ/5      \R                  " / SQ/ SQ/5      S.rGSGSS jjrGSS jrGSS jr " S S5      r " S S5      rGSS jr " S S\5      r " S S\5      rGSSS jjr\\            GSS j5       5       r\\"GSS j5       5       r\\"GSS j5       5       rg(  a  Functional implementations of image augmentation operations.

This module contains low-level functions for various image augmentation techniques including
color transformations, blur effects, tone curve adjustments, noise additions, and other visual
modifications. These functions form the foundation for the transform classes and provide
the core functionality for manipulating image data during the augmentation process.
    )annotationsN)Sequence)AnyLiteral)warn)MAX_VALUES_BY_DTYPEadd	add_arrayadd_constantadd_weightedclipclipped
float32_io
from_floatget_num_channelsis_grayscale_imageis_rgb_imagemaybe_process_in_chunksmultiplymultiply_addmultiply_by_arraymultiply_by_constantnormalize_per_imagepowerpreserve_channel_dimsz_lutuint8_io)PCAnon_rgb_error)MONO_CHANNEL_DIMENSIONSNUM_MULTI_CHANNEL_DIMENSIONSNUM_RGB_CHANNELSc                D   US:X  a  US:X  a  US:X  a  U $ [        U 5      nU(       a?  US:w  d  US:w  a  SnSn[        SSS9  [        R                  " U [        R                  5      n [        R                  " U [        R
                  5      n [        R                  " U 5      u  pVnUS:w  ad  [        R                  " SS[        R                  S9n[        R                  " X-   S5      R                  [        R                  5      n[        XXSS	9nUS:w  a  US:H  n	[        XbS
S	9nSXi'   US:w  a
  [        XsS
S	9n[        R                  " XVU45      n [        R                  " U [        R                   5      n U(       a%  [        R                  " U [        R"                  5      $ U $ )a6  Shift the hue, saturation, and value of an image.

Args:
    img (np.ndarray): The image to shift.
    hue_shift (float): The amount to shift the hue.
    sat_shift (float): The amount to shift the saturation.
    val_shift (float): The amount to shift the value.

Returns:
    np.ndarray: The shifted image.

r   zqHueSaturationValue: hue_shift and sat_shift are not applicable to grayscale image. Set them to 0 or use RGB image   )
stacklevel   dtype   FinplaceT)r   r   cv2cvtColorCOLOR_GRAY2RGBCOLOR_RGB2HSVsplitnparangeint16modastypeuint8r   r   mergeCOLOR_HSV2RGBCOLOR_RGB2GRAY)
img	hue_shift	sat_shift	val_shiftis_grayhuesatvallut_huegrayscale_masks
             g/var/www/fran/franai/venv/lib/python3.13/site-packages/albumentations/augmentations/pixel/functional.py	shift_hsvrE   7   s]   ( A~)q.Y!^
 %G>Y!^II1
 ll3 2 23
,,sC--
.CIIcNMCcA~))As"((3&&,c299"((CS51A~  348  A~348
))SsO
$C
,,sC--
.C4;3<<S//0DD    c                   U R                   n[        U   nU[        R                  :X  a  [        R                  " [        [        U5      S-   5       Vs/ s H  oDX-  :  a  X4-
  OUPM     snUS9nU R                  n[        XSS9n [        U5      U R                  :X  a  U $ [        R                  " U S5      $ [        R                  " X:  X0-
  U 5      $ s  snf )a  Invert all pixel values above a threshold.

Args:
    img (np.ndarray): The image to solarize. Can be uint8 or float32.
    threshold (float): Normalized threshold value in range [0, 1].
        For uint8 images: pixels above threshold * 255 are inverted
        For float32 images: pixels above threshold are inverted

Returns:
    np.ndarray: Solarized image.

Note:
    The threshold is normalized to [0, 1] range for both uint8 and float32 images.
    For uint8 images, the threshold is internally scaled by 255.

   r'   Fr*   )r(   r   r1   r6   arrayrangeintshaper   lenndimexpand_dimswhere)r:   	thresholdr(   max_valilut
prev_shapes          rD   solarizerW   w   s    $ IIE!%(GhhEJ3w<Z[K[E\]E\!44W[!;E\]
 YY
Su-*o1sNr~~c27NN88C$gmS99 ^s   Cc                $   [         R                  " U5      nUR                  (       a  [        U5      S:X  aR  [         R                  " SS[         R                  S9n[         R                  " SSU-
  -  S-
  5      ) nX4-  n[        XSS9$ [         R                  " U 5      n[        U5       Hb  u  pg[         R                  " SS[         R                  S9n[         R                  " SSU-
  -  S-
  5      ) nX4-  n[        U S	U4   US
S9US	U4'   Md     U$ )a  Reduce the number of bits for each color channel by keeping only the highest N bits.

Args:
    img (np.ndarray): Input image. Can be single or multi-channel.
    bits (Literal[1, 2, 3, 4, 5, 6, 7] | list[Literal[1, 2, 3, 4, 5, 6, 7]]): Number of high bits to keep..
        Can be either:
        - A single value to apply the same bit reduction to all channels
        - A list of values to apply different bit reduction per channel.
          Length of list must match number of channels in image.

Returns:
    np.ndarray: Image with reduced bit depth. Has same shape and dtype as input.

Note:
    - The transform keeps the N highest bits and sets all other bits to 0
    - For example, if bits=3:
        - Original value: 11010110 (214)
        - Keep 3 bits:   11000000 (192)
    - The number of unique colors per channel will be 2^bits
    - Higher bits values = more colors = more subtle effect
    - Lower bits values = fewer colors = more dramatic posterization

Examples:
    >>> import numpy as np
    >>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
    >>> # Same posterization for all channels
    >>> result = posterize(image, bits=3)
    >>> # Different posterization per channel
    >>> result = posterize(image, bits=[3, 4, 5])  # RGB channels

rH   r   r&   r'   r$      Fr*   .T)r1   r6   rM   rN   r2   r   
empty_like	enumerate)r:   bits
bits_arrayrU   mask
result_imgrT   channel_bitss           rD   	posterizera      s    D $Js:!3ii3bhh/q:~.233c..s#J$Z0ii3bhh/q</01455#CQKdC
36 1 rF   c                   [         R                  " U /S/US/S5      R                  5       n[        R                  " U Vs/ s H  o3(       d  M  UPM     sn5      n[        U5      S::  a  U R                  5       $ [        R                  " US S 5      S-  nU(       d  U R                  5       $ [        R                  " [        R                  " U5      US-  -   U-  S5      R                  [        R                  5      n[        XSS	9$ s  snf )
Nr   r&   r   r&   rH   rI      r$   Tr*   )r,   calcHistravelr1   rJ   rN   copysumminimumcumsumr5   r6   r   )r:   r^   	histogram_fhsteprU   s          rD   _equalize_pilro      s    cUQCuh?EEGI
y/yB"y/0A
1v{xxz66!CR&>S Dxxz
**bii	*TQY64?
E
L
LRXX
VC#D)) 0s    
DDc                   Uc  [         R                  " U 5      $ [         R                  " U /S/US/S5      R                  5       n[        R
                  " U5      (       a  [        R                  " U5      S   OSn[        R                  " U5      nSXBU   -
  -  n[        R                  " U5      n[        R                  " XfU   -
  U-  R                  5       SS5      R                  [        R                  5      n[        XSS9$ )Nr   r&   rc   rd   g     o@Tr*   )r,   equalizeHistre   rf   r1   anyflatnonzerorh   rj   r   roundr5   r6   r   )r:   r^   rk   rT   totalscalecumsum_histogramrU   s           rD   _equalize_cvrx      s    |$$cUQCuh?EEGI )+y(9(9y!!$sAFF9EUq\)*E yy+
''$'::eCJJLaQT
U
\
\]_]e]e
fC#D))rF   c                    Ubx  [        U5      (       a5  [        U 5      (       a%  [        SU R                   SUR                   35      eU(       d+  [        U5      (       d  SUR                   3n[        U5      eg g g )NzWrong mask shape. Image shape: z. Mask shape: zAWhen by_channels=False only 1-channel mask supports. Mask shape: )r   r   
ValueErrorrM   )r:   r^   by_channelsmsgs       rD   _check_preconditionsr}      s    
 "4S"9"91#))N4::,W  #5d#;#;UVZV`V`UabCS/! $<{ rF   c                ~    U c  g U R                  [        R                  SS9n Ub  [        U 5      (       d  U SU4   n U $ )NF)rg   .)r5   r1   r6   r   )r^   rT   s     rD   _handle_maskr      sM     |;;
  D
 	}/55CF|KrF   c                   [        XU5        US:X  a  [        O[        n[        U 5      (       a  U" U [	        U5      5      $ U(       db  [
        R                  " U [
        R                  5      nU" US   [	        U5      5      US'   [
        R                  " U[
        R                  5      $ [        R                  " U 5      n[        [        5       H!  n[	        X5      nU" U SU4   U5      USU4'   M#     U$ )a  Apply histogram equalization to the input image.

This function enhances the contrast of the input image by equalizing its histogram.
It supports both grayscale and color images, and can operate on individual channels
or on the luminance channel of the image.

Args:
    img (np.ndarray): Input image. Can be grayscale (2D array) or RGB (3D array).
    mask (np.ndarray | None): Optional mask to apply the equalization selectively.
        If provided, must have the same shape as the input image. Default: None.
    mode (ImageMode): The backend to use for equalization. Can be either "cv" for
        OpenCV or "pil" for Pillow-style equalization. Default: "cv".
    by_channels (bool): If True, applies equalization to each channel independently.
        If False, converts the image to YCrCb color space and equalizes only the
        luminance channel. Only applicable to color images. Default: True.

Returns:
    np.ndarray: Equalized image. The output has the same dtype as the input.

Raises:
    ValueError: If the input image or mask have invalid shapes or types.

Note:
    - If the input image is not uint8, it will be temporarily converted to uint8
      for processing and then converted back to its original dtype.
    - For color images, when by_channels=False, the image is converted to YCrCb
      color space, equalized on the Y channel, and then converted back to RGB.
    - The function preserves the original number of channels in the image.

Example:
    >>> import numpy as np
    >>> import albumentations as A
    >>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
    >>> equalized = A.equalize(image, mode="cv", by_channels=True)
    >>> assert equalized.shape == image.shape
    >>> assert equalized.dtype == image.dtype

pil.r   .)r}   ro   rx   r   r   r,   r-   COLOR_RGB2YCrCbCOLOR_YCrCb2RGBr1   rZ   rK   r"   )r:   r^   moder{   functionr_   rT   _masks           rD   equalizer     s    \ K0 $}<H#\$/00\\#s':':;
%j&8,t:LM
6||J(;(;<<s#J#$T%%c#q&k59
36 % rF   c                n   [         R                  " SSS5      n        S
S jn[        U 5      n[         R                  " U5      (       aY  [         R                  " U5      (       a>  [	        [         R
                  " U" X1U5      5      [         R                  SS9n[        XSS9$ [        U[         R                  5      (       a  [        U[         R                  5      (       a  [	        [         R
                  " U" USS2[         R                  4   X5      R                  5      [         R                  SS9n[        R                  " [        U5       Vs/ s H.  n[        U SS2SS2U4   [         R                  " Xx   5      SS9PM0     sn5      $ [!        S[#        U5       S	[#        U5       35      es  snf )a
  Rescales the relationship between bright and dark areas of the image by manipulating its tone curve.

Args:
    img (np.ndarray): Any number of channels
    low_y (float | np.ndarray): per-channel or single y-position of a Bezier control point used
        to adjust the tone curve, must be in range [0, 1]
    high_y (float | np.ndarray): per-channel or single y-position of a Bezier control point used
        to adjust image tone curve, must be in range [0, 1]

Returns:
    np.ndarray: Image with adjusted tone curve

              ?r&   c                X    SU -
  nSUS-  -  U -  U-  SU-  U S-  -  U-  -   U S-  -   S-  $ )NrH      r$   rd    )tlow_yhigh_yone_minus_ts       rD   evaluate_bez%move_tone_curve.<locals>.evaluate_bezg  sO    
 !eKN"Q&.[1a41G&1PPSTVWSWW[^^^rF   Fr*   Nz?low_y and high_y must both be of type float or np.ndarray. Got z and )r   
np.ndarrayr   float | np.ndarrayr   r   returnr   )r1   linspacer   isscalarr   rintr6   r   
isinstancendarraynewaxisTr,   r7   rK   ascontiguousarray	TypeErrortype)	r:   r   r   r   r   num_channelsrU   lutsrT   s	            rD   move_tone_curver   R  sw   & 	Cc"A__!_ #_ 
	_ $C(L	{{5bkk&11277<&9:BHHeTc..%$$FBJJ)G)GGGL1bjj=!15ACCDHH

 yyY^_kYlmYlTUVC1aL""6"6tw"?OYlm
 	
 
I$u+V[\`ag\h[ij  ns   5F2c                .    [         R                  " X5      $ )a  Apply a linear transformation to the RGB channels of an image.

This function applies a linear transformation matrix to the RGB channels of an image.
The transformation matrix is a 3x3 matrix that maps the RGB values to new values.

Args:
    img (np.ndarray): Input image. Can be grayscale (2D array) or RGB (3D array).
    transformation_matrix (np.ndarray): 3x3 transformation matrix.

Returns:
    np.ndarray: Image with the linear transformation applied. The output has the same dtype as the input.

)r,   	transform)r:   transformation_matrixs     rD   linear_transformation_rgbr     s    $ ==44rF   c                J   [         R                  " XS9n[        U 5      (       a  UR                  U 5      $ [         R                  " U [         R
                  5      nUR                  USS2SS2S4   5      USS2SS2S4'   [         R                  " U[         R                  5      $ )a  Apply Contrast Limited Adaptive Histogram Equalization (CLAHE) to the input image.

This function enhances the contrast of the input image using CLAHE. For color images,
it converts the image to the LAB color space, applies CLAHE to the L channel, and then
converts the image back to RGB.

Args:
    img (np.ndarray): Input image. Can be grayscale (2D array) or RGB (3D array).
    clip_limit (float): Threshold for contrast limiting. Higher values give more contrast.
    tile_grid_size (tuple[int, int]): Size of grid for histogram equalization.
        Width and height of the grid.

Returns:
    np.ndarray: Image with CLAHE applied. The output has the same dtype as the input.

Note:
    - If the input image is float32, it's temporarily converted to uint8 for processing
      and then converted back to float32.
    - For color images, CLAHE is applied only to the luminance channel in the LAB color space.

Raises:
    ValueError: If the input image is not 2D or 3D.

Example:
    >>> import numpy as np
    >>> img = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
    >>> result = clahe(img, clip_limit=2.0, tile_grid_size=(8, 8))
    >>> assert result.shape == img.shape
    >>> assert result.dtype == img.dtype

)	clipLimittileGridSizeNr   )r,   createCLAHEr   applyr-   COLOR_RGB2LABCOLOR_LAB2RGB)r:   
clip_limittile_grid_size	clahe_matimg_labs        rD   claher     s~    L *RI#s##ll3 1 12G wq!Qw'78GAq!G<<!2!233rF   c                  ^^^ TS:X  a  [         R                  O[         R                  m[        U 5      nS
UUU4S jjnUS:X  a,  U" U [         R                  5      nUS[
        R                  4   $ US[        4;   a>  US:X  a  [
        R                  " U SSS9OU nU" U[         R                  5      nUSS	U24   $ U SS	[        24   nU" U[         R                  5      n[        [        U5       V	s/ s H1  o" U SU	4   [         R                  5      S[
        R                  4   PM3     n
n	[
        R                  " U/U
Q5      $ s  sn	f )a
  Compress the image using JPEG or WebP compression.

Args:
    img (np.ndarray): Input image
    quality (int): Quality of compression in range [1, 100]
    image_type (Literal[".jpg", ".webp"]): Type of compression to use

Returns:
    np.ndarray: Compressed image

z.jpgc                z   > [         R                  " TU [        T5      T45      u  p#[         R                  " X15      $ N)r,   imencoderL   imdecode)src_img	read_mode_encoded_img
image_typequalityquality_flags       rD   encode_decode(image_compression.<locals>.encode_decode  s0    j'C<Mw;WX||K33rF   rH   .r$   )r   r   r   r   rH   constant)r   N)r   r   r   rL   r   r   )r,   IMWRITE_JPEG_QUALITYIMWRITE_WEBP_QUALITYr   IMREAD_GRAYSCALEr1   r   r"   padIMREAD_UNCHANGEDrK   dstack)r:   r   r   r   r   decoded
padded_imgdecoded_bgrbgrrT   extra_channelsr   s    ``        @rD   image_compressionr     sH   & 0:V/C3++IaIaL#C(L4 4 qS%9%9:sBJJ''+,,O[_`O`RVVC!9
Kfi
#J0D0DE3-.. c$$$$
%CS%9%9:K TYYikwSxSxac#q&k3#7#78bjjISx   99k3N344s   18Ec                   [         [        R                     nX-  S-  US-  -   n[        R                  " U [        R
                  5      nUSS2SS2S4   R                  [        R                  5      nXQ:  nXV==   U-  ss'   [        U[        R                  SS9nXTSS2SS2S4'   [        R                  " U[        R                  5      $ )a#  Adds a simple snow effect to the image by bleaching out pixels.

This function simulates a basic snow effect by increasing the brightness of pixels
that are above a certain threshold (snow_point). It operates in the HLS color space
to modify the lightness channel.

Args:
    img (np.ndarray): Input image. Can be either RGB uint8 or float32.
    snow_point (float): A float in the range [0, 1], scaled and adjusted to determine
        the threshold for pixel modification. Higher values result in less snow effect.
    brightness_coeff (float): Coefficient applied to increase the brightness of pixels
        below the snow_point threshold. Larger values lead to more pronounced snow effects.
        Should be greater than 1.0 for a visible effect.

Returns:
    np.ndarray: Image with simulated snow effect. The output has the same dtype as the input.

Note:
    - This function converts the image to the HLS color space to modify the lightness channel.
    - The snow effect is created by selectively increasing the brightness of pixels.
    - This method tends to create a 'bleached' look, which may not be as realistic as more
      advanced snow simulation techniques.
    - The function automatically handles both uint8 and float32 input images.

The snow effect is created through the following steps:
1. Convert the image from RGB to HLS color space.
2. Adjust the snow_point threshold.
3. Increase the lightness of pixels below the threshold.
4. Convert the image back to RGB.

Mathematical Formulation:
    Let L be the lightness channel in HLS space.
    For each pixel (i, j):
    If L[i, j] < snow_point:
        L[i, j] = L[i, j] * brightness_coeff

Examples:
    >>> import numpy as np
    >>> import albumentations as A
    >>> image = np.random.randint(0, 256, [100, 100, 3], dtype=np.uint8)
    >>> snowy_image = A.functional.add_snow_v1(image, snow_point=0.5, brightness_coeff=1.5)

References:
    - HLS Color Space: https://en.wikipedia.org/wiki/HSL_and_HSV
    - Original implementation: https://github.com/UjjwalSaxena/Automold--Road-Augmentation-Library

r$   r   NrH   Tr*   )
r   r1   r6   r,   r-   COLOR_RGB2HLSr5   float32r   COLOR_HLS2RGB)r:   
snow_pointbrightness_coeff	max_value	image_hlslightness_channelr^   s          rD   add_snow_bleachr     s    j $BHH-I (1,Q?J S#"3"34I!!Q'*11"**= )D// .$G +aAg <<	3#4#455rF   c                    UR                  U SS SSS9n[        R                  " USSSS9nUR                  U SS 5      S	:  nX#4$ )
zGenerate snow texture and sparkle mask.

Args:
    img_shape (tuple[int, int]): Image shape.
    random_generator (np.random.Generator): Random generator to use.

Returns:
    tuple[np.ndarray, np.ndarray]: Tuple of (snow_texture, sparkle_mask) arrays.

Nr$         ?g333333?)sizelocrv   r   rH   sigmaXsigmaYGz?)normalr,   GaussianBlurrandom)	img_shaperandom_generatorsnow_texturesparkle_masks       rD   generate_snow_texturesr   F  s]     $**	"13c*RL##L&1ML $**9Ra=9D@L%%rF   c                l   [         [        R                     n[        R                  " U [        R
                  5      R                  [        R                  5      n[        R                  " USS2SS2S4   SX!-  -   -  SU5      USS2SS2S4'   [        R                  " USSSS9nU R                  S   n[        R                  " SSU5      SS2[        R                  4   nX8-  n[        R                  " U/S-  5      U-  U-  R                  [        R                  5      n	[        R                  " Xi5      n
[        R                  " U
S	5      n[        R                   " U
S
USU-  S5      n
[        R                  " U
R                  [        R                  5      [        R"                  5      n
XUU/X'   U
$ )a  Add a realistic snow effect to the input image.

This function simulates snowfall by applying multiple visual effects to the image,
including brightness adjustment, snow texture overlay, depth simulation, and color tinting.
The result is a more natural-looking snow effect compared to simple pixel bleaching methods.

Args:
    img (np.ndarray): Input image in RGB format.
    snow_point (float): Coefficient that controls the amount and intensity of snow.
        Should be in the range [0, 1], where 0 means no snow and 1 means maximum snow effect.
    brightness_coeff (float): Coefficient for brightness adjustment to simulate the
        reflective nature of snow. Should be in the range [0, 1], where higher values
        result in a brighter image.
    snow_texture (np.ndarray): Snow texture.
    sparkle_mask (np.ndarray): Sparkle mask.

Returns:
    np.ndarray: Image with added snow effect. The output has the same dtype as the input.

Note:
    - The function first converts the image to HSV color space for better control over
      brightness and color adjustments.
    - A snow texture is generated using Gaussian noise and then filtered for a more
      natural appearance.
    - A depth effect is simulated, with more snow at the top of the image and less at the bottom.
    - A slight blue tint is added to simulate the cool color of snow.
    - Random sparkle effects are added to simulate light reflecting off snow crystals.

The snow effect is created through the following steps:
1. Brightness adjustment in HSV space
2. Generation of a snow texture using Gaussian noise
3. Application of a depth effect to the snow texture
4. Blending of the snow texture with the original image
5. Addition of a cool blue tint
6. Addition of sparkle effects

Examples:
    >>> import numpy as np
    >>> import albumentations as A
    >>> image = np.random.randint(0, 256, [100, 100, 3], dtype=np.uint8)
    >>> snowy_image = A.functional.add_snow_v2(image, snow_coeff=0.5, brightness_coeff=0.2)

Note:
    This function works with both uint8 and float32 image types, automatically
    handling the conversion between them.

References:
    - Perlin Noise: https://en.wikipedia.org/wiki/Perlin_noise
    - HSV Color Space: https://en.wikipedia.org/wiki/HSL_and_HSV

Nr$   rH   r   r   r   g?r   )333333?      ?rH   333333?333333?)r   r1   r6   r,   r-   r/   r5   r   r   r   rM   r   r   r   r	   	full_likeaddWeightedr8   )r:   r   r   r   r   r   img_hsvrowsdepth_effect
snow_layerimg_with_snow	blue_tints               rD   add_snow_texturer   ^  sx   v $BHH-I ll3 1 1299"**EG ww1aA 0 ==>	GAq!G ##L&1ML 99Q<D;;q#t,Q

];L L ))\NQ./);jHPP


J
 GGG0M ]N;IOOz	M LL!5!5bhh!?ARARSM $-"CMrF   c           	     P   UR                   (       d  U R                  5       $ U R                  5       n [        R                  " U [        R                  S9nU[        R
                  " X//5      -   n	[        R                  " Xy4SS9n
[        R                  " UU
R                  [        R                  5      SUU[        R                  S9  US:  a  [        R                  " XU4US9  [        R                  " XU S9  US:w  a$  [        R                  " XU [        R                  S9  U $ )	a  Add rain to an image.

This function adds rain to an image by drawing rain drops on the image.
The rain drops are drawn using the OpenCV function cv2.polylines.

Args:
    img (np.ndarray): The image to add rain to.
    slant (float): The slant of the rain drops.
    drop_length (int): The length of the rain drops.
    drop_width (int): The width of the rain drops.
    drop_color (tuple[int, int, int]): The color of the rain drops.
    blur_value (int): The blur value of the rain drops.
    brightness_coefficient (float): The brightness coefficient of the rain drops.
    rain_drops (np.ndarray): The rain drops to draw on the image.

Returns:
    np.ndarray: The image with rain added.

r'   rH   axisF)lineTypedstr   )r   r(   )r   rg   r1   
zeros_liker6   rJ   stackr,   	polylinesr5   int32LINE_4blurr	   r   CV_8U)r:   slantdrop_length
drop_width
drop_color
blur_valuebrightness_coefficient
rain_drops
rain_layer
end_pointsliness              rD   add_rainr    s    > ??xxz
((*C s"((3J bhh(<'=>>J HHj-A6EMMRXX A~*5:FGGC%$ScKJrF   c           	         U SS u  pE[        S[        [        XE5      S-  U-  5      5      n[        SUS-  5      n[        U5       Vs/ s H  oR	                  Xv5      PM     sn$ s  snf )a\  Generate radiuses for fog particles.

Args:
    img_shape (tuple[int, int]): Image shape.
    num_particles (int): Number of fog particles.
    fog_intensity (float): Intensity of the fog effect, between 0 and 1.
    random_generator (np.random.Generator): Random generator to use.

Returns:
    list[int]: List of radiuses for each fog particle.

Nr$   皙?rH   )maxrL   minrK   integers)	r   num_particlesfog_intensityr   heightwidthmax_fog_radius
min_radiusr   s	            rD   get_fog_particle_radiusesr    sk    $ bqMMFCF 2S 8= HIJNQ!+,JKPQ^K_`K_a%%jAK_```s   A%c           
        U R                  5       n[        X45       HP  u  u  pgnUR                  5       n	[        R                  " U	Xg4USSS9  X!-  n
[        R                  " XUSU
-
  SUS9  MR     [        S[        [        U R                  SS	 5      S
-  5      5      nUS	-  S:X  a  US-  n[        R                  " X[U4S5      n[        U[        R                  SS9$ )a<  Add fog to an image.

This function adds fog to an image by drawing fog particles on the image.
The fog particles are drawn using the OpenCV function cv2.circle.

Args:
    img (np.ndarray): The image to add fog to.
    fog_intensity (float): The intensity of the fog effect, between 0 and 1.
    alpha_coef (float): The coefficient for the alpha blending.
    fog_particle_positions (list[tuple[int, int]]): The positions of the fog particles.
    fog_particle_radiuses (list[int]): The radiuses of the fog particles.

Returns:
    np.ndarray: The image with fog added.

)rd   rd   rd   rI   )centerradiuscolor	thicknessrH   r   r   r   Nr$      Tr*   )rg   zipr,   circler   r  rL   r  rM   r   r   r1   r6   )r:   r  
alpha_coeffog_particle_positionsfog_particle_radiusesresultxyr   overlayalpha	blur_sizes               rD   add_fogr/  &  s    4 XXZF 4L++-

6!	
 *E	1&I M As3syy!}-345I1}Q	f)&<a@F$//rF   c           	        U R                  5       nU R                  5       nSnSnU H>  u  n	u  ppXyU-  -  nX-  n[        R                  " XZU4XS5        [        XYUSU	-
  5      nM@     U V
s/ s H  n
[	        U
5      PM     nn
UR                  5       nUS-  nXx-  S-  n[
        R                  " S[        US5      US9n	[
        R                  " SX/S9n[        U5       HY  n[        R                  " X^[	        UU   5      US5        XU-
  S-
     XU-
  S-
     -  XU-
  S-
     -  n[        UUUSU-
  5      nM[     U$ s  sn
f )am	  Add a sun flare effect to an image using a simple overlay technique.

This function creates a basic sun flare effect by overlaying multiple semi-transparent
circles of varying sizes and intensities on the input image. The effect simulates
a simple lens flare caused by bright light sources.

Args:
    img (np.ndarray): The input image.
    flare_center (tuple[float, float]): (x, y) coordinates of the flare center
        in pixel coordinates.
    src_radius (int): The radius of the main sun circle in pixels.
    src_color (tuple[int, ...]): The color of the sun, represented as a tuple of RGB values.
    circles (list[Any]): A list of tuples, each representing a circle that contributes
        to the flare effect. Each tuple contains:
        - alpha (float): The transparency of the circle (0.0 to 1.0).
        - center (tuple[int, int]): (x, y) coordinates of the circle center.
        - radius (int): The radius of the circle.
        - color (tuple[int, int, int]): RGB color of the circle.

Returns:
    np.ndarray: The output image with the sun flare effect added.

Note:
    - This function uses a simple alpha blending technique to overlay flare elements.
    - The main sun is created as a gradient circle, fading from the center outwards.
    - Additional flare circles are added along an imaginary line from the sun's position.
    - This method is computationally efficient but may produce less realistic results
      compared to more advanced techniques.

The flare effect is created through the following steps:
1. Create an overlay image and output image as copies of the input.
2. Add smaller flare circles to the overlay.
3. Blend the overlay with the output image using alpha compositing.
4. Add the main sun circle with a radial gradient.

Examples:
    >>> import numpy as np
    >>> import albumentations as A
    >>> image = np.random.randint(0, 256, [100, 100, 3], dtype=np.uint8)
    >>> flare_center = (50, 50)
    >>> src_radius = 20
    >>> src_color = (255, 255, 200)
    >>> circles = [
    ...     (0.1, (60, 60), 5, (255, 200, 200)),
    ...     (0.2, (70, 70), 3, (200, 255, 200))
    ... ]
    >>> flared_image = A.functional.add_sun_flare_overlay(
    ...     image, flare_center, src_radius, src_color, circles
    ... )

References:
    - Alpha compositing: https://en.wikipedia.org/wiki/Alpha_compositing
    - Lens flare: https://en.wikipedia.org/wiki/Lens_flare

r   rI   rH   
      r   )num)	rg   r,   r%  r   rL   r1   r   r  rK   )r:   flare_center
src_radius	src_colorcirclesr,  outputweighted_brightnesstotal_radius_lengthr-  r*  r+  rad3circle_colorpoint	num_times	max_alpharadrT   alps                       rD   add_sun_flare_overlayrB  [  s[   B hhjGXXZF-4)vtt|+#

7FD;gfa%i@	 .5 **\SV\E*kkmGb I
 $9A=IKKSC0i@E
++a
3C9

73s1v;	2>MA%&1}q/@)AAEVW-Z[J[D\\gsFAG< 
 M% +s   -D?c           
        U R                  5       nU R                  SS u  pg[        R                  " U [        R                  S9n[
        R                  " XX#S5        S H  n	[        US   [        R                  " [        R                  " U	5      5      [        Xv5      -  -   5      [        US   [        R                  " [        R                  " U	5      5      [        Xv5      -  -   5      4n
[
        R                  " XXS5        M     U H+  u  pp[
        R                  " X[        US-  5      US5        M-     [
        R                  " US	S
S
S9n[        R                  SU2SU24   u  nn[        R                  " UUS   -
  S-  XS   -
  S-  -   5      nS[        R                   " U[        Xv5      S-  -  SS5      -
  n[        R"                  " U/S-  5      nUU-  n[%        [
        R&                  " U5      5      n[
        R                  " US   S	SSS9US'   [
        R                  " US   S	SSS9US'   [
        R(                  " U5      nSSU-
  SU-
  -  S-  -
  $ )a1  Add a more realistic sun flare effect to the image.

This function creates a complex sun flare effect by simulating various optical phenomena
that occur in real camera lenses when capturing bright light sources. The result is a
more realistic and physically plausible lens flare effect.

Args:
    img (np.ndarray): Input image.
    flare_center (tuple[int, int]): (x, y) coordinates of the sun's center in pixels.
    src_radius (int): Radius of the main sun circle in pixels.
    src_color (tuple[int, int, int]): Color of the sun in RGB format.
    circles (list[Any]): List of tuples, each representing a flare circle with parameters:
        (alpha, center, size, color)
        - alpha (float): Transparency of the circle (0.0 to 1.0).
        - center (tuple[int, int]): (x, y) coordinates of the circle center.
        - size (float): Size factor for the circle radius.
        - color (tuple[int, int, int]): RGB color of the circle.

Returns:
    np.ndarray: Image with added sun flare effect.

Note:
    This function implements several techniques to create a more realistic flare:
    1. Separate flare layer: Allows for complex manipulations of the flare effect.
    2. Lens diffraction spikes: Simulates light diffraction in camera aperture.
    3. Radial gradient mask: Creates natural fading of the flare from the center.
    4. Gaussian blur: Softens the flare for a more natural glow effect.
    5. Chromatic aberration: Simulates color fringing often seen in real lens flares.
    6. Screen blending: Provides a more realistic blending of the flare with the image.

The flare effect is created through the following steps:
1. Create a separate flare layer.
2. Add the main sun circle and diffraction spikes to the flare layer.
3. Add additional flare circles based on the input parameters.
4. Apply Gaussian blur to soften the flare.
5. Create and apply a radial gradient mask for natural fading.
6. Simulate chromatic aberration by applying different blurs to color channels.
7. Blend the flare with the original image using screen blending mode.

Examples:
    >>> import numpy as np
    >>> import albumentations as A
    >>> image = np.random.randint(0, 256, [1000, 1000, 3], dtype=np.uint8)
    >>> flare_center = (500, 500)
    >>> src_radius = 50
    >>> src_color = (255, 255, 200)
    >>> circles = [
    ...     (0.1, (550, 550), 10, (255, 200, 200)),
    ...     (0.2, (600, 600), 5, (200, 255, 200))
    ... ]
    >>> flared_image = A.functional.add_sun_flare_physics_based(
    ...     image, flare_center, src_radius, src_color, circles
    ... )

References:
    - Lens flare: https://en.wikipedia.org/wiki/Lens_flare
    - Diffraction: https://en.wikipedia.org/wiki/Diffraction
    - Chromatic aberration: https://en.wikipedia.org/wiki/Chromatic_aberration
    - Screen blending: https://en.wikipedia.org/wiki/Blend_modes#Screen

Nr$   r'   rI   )r   -   Z      r   rH   gQ?r      r   ffffff?r   r2  rd   )rg   rM   r1   r   r   r,   r%  rL   cosradiansr  sinliner   ogridsqrtr   r   listr0   r7   )r:   r4  r5  r6  r7  r8  r  r  flare_layerangle	end_pointr   r  r   r!  r+  r*  r^   channelss                      rD   add_sun_flare_physics_basedrT    s-   L XXZFIIbqMMF --2::6K JJ{*D "Q"&&E):";c%>P"PPQQ"&&E):";c%>P"PPQ
	 	I!D " #*4

;D$JC #* "";r"MK 88GVGVeVO$DAq77AQ'A-!_1D0JJKDrwwts51C78!Q??D99dVaZ D 4K CIIk*+H""	HQK ""	HQK ))H%K 3<C+$56<==rF   c                   [        U 5      n[        [        R                     nU R	                  5       n[        X5       H  u  pg[        R                  " U R                  S   U R                  S   S4[        R                  S9n[        R                  " X/U45        [        R                  " XSS9nUSS2SS2S4   U:H  n	SU-
  n
[        XY   U
-  [        R                  SS9XY'   M     U$ )	a  Add shadows to the image by reducing the intensity of the pixel values in specified regions.

Args:
    img (np.ndarray): Input image. Multichannel images are supported.
    vertices_list (list[np.ndarray]): List of vertices for shadow polygons.
    intensities (np.ndarray): Array of shadow intensities. Range is [0, 1].

Returns:
    np.ndarray: Image with shadows added.

References:
    Automold--Road-Augmentation-Library: https://github.com/UjjwalSaxena/Automold--Road-Augmentation-Library

r   rH   r'   r$   r   NTr*   )r   r   r1   r6   rg   r$  zerosrM   r,   fillPolyrepeatr   )r:   vertices_listintensitiesr   r   img_shadowedverticesshadow_intensityr^   shadowed_indicesdarknesss              rD   
add_shadowr`  8  s    * $C(L#BHH-I88:L '*-&E"xx1syy|Q7rxxHT:	|4 yy!4  1a=I5'')-*X5HH*
& 'F$ rF   c                    [        U 5        [        R                  " U [        R                  5      nU H  nUu  pEpgnXXE2Xg2S4'   M     [        R                  " U[        R                  5      $ )ac  Add gravel to an image.

This function adds gravel to an image by drawing gravel particles on the image.
The gravel particles are drawn using the OpenCV function cv2.circle.

Args:
    img (np.ndarray): The image to add gravel to.
    gravels (list[Any]): The gravel particles to draw on the image.

Returns:
    np.ndarray: The image with gravel added.

rH   )r   r,   r-   r   r   )	r:   gravelsr   gravelmin_ymax_ymin_xmax_xr@   s	            rD   
add_gravelrh  h  sd    " #S#"3"34I*0'eC14%+u{A-.  <<	3#4#455rF   c                .    [         U R                     U -
  $ )a  Invert the colors of an image.

This function inverts the colors of an image by subtracting each pixel value from the maximum possible value.
The result is a negative of the original image.

Args:
    img (np.ndarray): The image to invert.

Returns:
    np.ndarray: The inverted image.

)r   r(   r:   s    rD   invertrk    s     syy)C//rF   c                    [         R                  " U 5      n/ n[        U5       H  u  pEUR                  XT/5        M     [        R
                  " U /U/U5        U$ )af  Shuffle the channels of an image.

This function shuffles the channels of an image by using the cv2.mixChannels function.
The channels are shuffled according to the channels_shuffled array.

Args:
    img (np.ndarray): The image to shuffle.
    channels_shuffled (np.ndarray): The array of channels to shuffle.

Returns:
    np.ndarray: The shuffled image.

)r1   rZ   r[   extendr,   mixChannels)r:   channels_shuffledr8  from_torT   js         rD   channel_shufflerr    sP     ]]3FG+,v -OOSEF8W-MrF   c                P    U R                   S:X  a  U R                  5       SU4   $ U $ )zShuffle channels of a single volume (D, H, W, C) or (D, H, W).

Args:
    volume (np.ndarray): Input volume.
    channels_shuffled (Sequence[int]): New channel order.

Returns:
    np.ndarray: Volume with channels shuffled.

   .rO   rg   )volumero  s     rD   volume_channel_shufflerw    s+     5;KK14D6;;=//0P&PrF   c                P    U R                   S:X  a  U R                  5       SU4   $ U $ )zShuffle channels of a batch of volumes (B, D, H, W, C) or (B, D, H, W).

Args:
    volumes (np.ndarray): Input batch of volumes.
    channels_shuffled (Sequence[int]): New channel order.

Returns:
    np.ndarray: Batch of volumes with channels shuffled.

r2  .ru  )volumesro  s     rD   volumes_channel_shufflerz    s+     6=\\Q5F7<<>#001SGSrF   c                    U R                   [        R                  :X  aE  [        R                  " SSS5      U-  S-  n[	        XR                  [        R                  5      SS9$ [        R                  " X5      $ )a  Apply gamma transformation to an image.

This function applies gamma transformation to an image by raising each pixel value to the power of gamma.
The result is a non-linear transformation that can enhance or reduce the contrast of the image.

Args:
    img (np.ndarray): The image to apply gamma transformation to.
    gamma (float): The gamma value to apply.

Returns:
    np.ndarray: The gamma transformed image.

r   g?gp?rd   Fr*   )r(   r1   r6   r2   r   r5   r   )r:   gammatables      rD   gamma_transformr~    sY     yyBHH1k95>#Ec<<15AA88CrF   c                   [         R                  " U [         R                  5      n[         R                  " U5      u  pVUR	                  US   U-  UR
                  SS S9nUR                  SX-  UR
                  SS S9nUS==   U-  ss'   [        US   Xr-  SUS   -
  -  5      US'   [         R                  " U[         R                  5      n	[        R                  " U	SSU	S	9$ )
aR  Apply poisson noise to an image to simulate camera sensor noise.

Args:
    image (np.ndarray): Input image. Currently, only RGB images are supported.
    color_shift (float): The amount of color shift to apply.
    intensity (float): Multiplication factor for noise values. Values of ~0.5 produce a noticeable,
                       yet acceptable level of noise.
    random_generator (np.random.Generator): If specified, this will be random generator used
        for noise generation.

Returns:
    np.ndarray: The noised image.

Image types:
    uint8, float32

Number of channels:
    3

rH   Nr$   r   r   r   .rH   r   out)r,   r-   r   
meanStdDevpoissonrM   r   r
   r   r1   r   )
imagecolor_shift	intensityr   hlsr   stddevluminance_noisecolor_noise
noised_hlss
             rD   	iso_noiser    s    8 ,,uc//
0Cs#IA&..q	IYYr] / O #))	YYr] * K K;KF#sS['89CK
 c3#4#45J77:q!44rF   c                L    [         R                  " U [         R                  5      $ )a  Convert an RGB image to grayscale using the weighted average method.

This function uses OpenCV's cvtColor function with COLOR_RGB2GRAY conversion,
which applies the following formula:
Y = 0.299*R + 0.587*G + 0.114*B

Args:
    img (np.ndarray): Input RGB image as a numpy array.

Returns:
    np.ndarray: Grayscale image as a 2D numpy array.

Image types:
    uint8, float32

Number of channels:
    3

)r,   r-   r9   rj  s    rD   to_gray_weighted_averager    s    ( <<S//00rF   c                R    [         R                  " U [         R                  5      S   $ )a  Convert an RGB image to grayscale using the L channel from the LAB color space.

This function converts the RGB image to the LAB color space and extracts the L channel.
The LAB color space is designed to approximate human vision, where L represents lightness.

Key aspects of this method:
1. The L channel represents the lightness of each pixel, ranging from 0 (black) to 100 (white).
2. It's more perceptually uniform than RGB, meaning equal changes in L values correspond to
   roughly equal changes in perceived lightness.
3. The L channel is independent of the color information (A and B channels), making it
   suitable for grayscale conversion.

This method can be particularly useful when you want a grayscale image that closely
matches human perception of lightness, potentially preserving more perceived contrast
than simple RGB-based methods.

Args:
    img (np.ndarray): Input RGB image as a numpy array.

Returns:
    np.ndarray: Grayscale image as a 2D numpy array, representing the L (lightness) channel.
                Values are scaled to match the input image's data type range.

Image types:
    uint8, float32

Number of channels:
    3

r   )r,   r-   r   rj  s    rD   to_gray_from_labr  &  s!    B <<S../77rF   c                    U R                  [        R                  5      n[        R                  " USS9[        R                  " USS9-   S-  $ )zConvert an image to grayscale using the desaturation method.

Args:
    img (np.ndarray): Input image as a numpy array.

Returns:
    np.ndarray: Grayscale image as a 2D numpy array.

Image types:
    uint8, float32

Number of channels:
    any

rI   r   r$   )r5   r1   r   r  r  )r:   float_images     rD   to_gray_desaturationr  J  s<    " **RZZ(KFF;R(266+B+GG1LLrF   c                ^    [         R                  " U SS9R                  U R                  5      $ )a8  Convert an image to grayscale using the average method.

This function computes the arithmetic mean across all channels for each pixel,
resulting in a grayscale representation of the image.

Key aspects of this method:
1. It treats all channels equally, regardless of their perceptual importance.
2. Works with any number of channels, making it versatile for various image types.
3. Simple and fast to compute, but may not accurately represent perceived brightness.
4. For RGB images, the formula is: Gray = (R + G + B) / 3

Note: This method may produce different results compared to weighted methods
(like RGB weighted average) which account for human perception of color brightness.
It may also produce unexpected results for images with alpha channels or
non-color data in additional channels.

Args:
    img (np.ndarray): Input image as a numpy array. Can be any number of channels.

Returns:
    np.ndarray: Grayscale image as a 2D numpy array. The output data type
                matches the input data type.

Image types:
    uint8, float32

Number of channels:
    any

rI   r   )r1   meanr5   r(   rj  s    rD   to_gray_averager  _  s$    > 773R ''		22rF   c                ,    [         R                  " U SS9$ )a  Convert an image to grayscale using the maximum channel value method.

This function takes the maximum value across all channels for each pixel,
resulting in a grayscale image that preserves the brightest parts of the original image.

Key aspects of this method:
1. Works with any number of channels, making it versatile for various image types.
2. For 3-channel (e.g., RGB) images, this method is equivalent to extracting the V (Value)
   channel from the HSV color space.
3. Preserves the brightest parts of the image but may lose some color contrast information.
4. Simple and fast to compute.

Note:
- This method tends to produce brighter grayscale images compared to other conversion methods,
  as it always selects the highest intensity value from the channels.
- For RGB images, it may not accurately represent perceived brightness as it doesn't
  account for human color perception.

Args:
    img (np.ndarray): Input image as a numpy array. Can be any number of channels.

Returns:
    np.ndarray: Grayscale image as a 2D numpy array. The output data type
                matches the input data type.

Image types:
    uint8, float32

Number of channels:
    any

rI   r   )r1   r  rj  s    rD   to_gray_maxr    s    B 66#BrF   c                   U R                   nU R                  SU R                  S   5      n[        SS9nUR	                  U5      nUR                  U R                  SS 5      n[        US5      nU[        R                  :X  a	  [        XQS9$ U$ )aQ  Convert an image to grayscale using Principal Component Analysis (PCA).

This function applies PCA to reduce a multi-channel image to a single channel,
effectively creating a grayscale representation that captures the maximum variance
in the color data.

Args:
    img (np.ndarray): Input image as a numpy array with shape (height, width, channels).

Returns:
    np.ndarray: Grayscale image as a 2D numpy array with shape (height, width).
                If input is uint8, output is uint8 in range [0, 255].
                If input is float32, output is float32 in range [0, 1].

Note:
    This method can potentially preserve more information from the original image
    compared to standard weighted average methods, as it accounts for the
    correlations between color channels.

Image types:
    uint8, float32

Number of channels:
    any

rI   r$   rH   )n_componentsNmin_max)target_dtype)	r(   reshaperM   r   fit_transformr   r1   r6   r   )r:   r(   pixelspca
pca_result	grayscales         rD   to_gray_pcar    s    8 IIE[[SYYq\*F 1
C""6*J ""399Ra=1I#Iy9I8=8I:i4XyXrF   c                   US:X  a  [        U 5      nOhUS:X  a  [        U 5      nOVUS:X  a  [        U 5      nODUS:X  a  [        U 5      nO2US:X  a  [	        U 5      nO US:X  a  [        U 5      nO[        SU 35      e[        X15      $ )a+  Convert an image to grayscale using a specified method.

This function converts an image to grayscale using a specified method.
The method can be one of the following:
- "weighted_average": Use the weighted average method.
- "from_lab": Use the L channel from the LAB color space.
- "desaturation": Use the desaturation method.
- "average": Use the average method.
- "max": Use the maximum channel value method.
- "pca": Use the Principal Component Analysis method.

Args:
    img (np.ndarray): Input image as a numpy array.
    num_output_channels (int): The number of channels in the output image.
    method (Literal["weighted_average", "from_lab", "desaturation", "average", "max", "pca"]):
        The method to use for grayscale conversion.

Returns:
    np.ndarray: Grayscale image as a 2D numpy array.

weighted_averagefrom_labdesaturationaverager  r  zUnsupported method: )r  r  r  r  r  r  rz   grayscale_to_multichannel)r:   num_output_channelsmethodr)  s       rD   to_grayr    s    B ##)#.	:	!#&	>	!%c*	9	 %	5S!	5S!/x899$VAArF   c                r    US:X  a  U $ [         R                  " U 5      n[        R                  " U/U-  5      $ )aB  Convert a grayscale image to a multi-channel image.

This function takes a 2D grayscale image or a 3D image with a single channel
and converts it to a multi-channel image by repeating the grayscale data
across the specified number of channels.

Args:
    grayscale_image (np.ndarray): Input grayscale image. Can be 2D (height, width)
                                  or 3D (height, width, 1).
    num_output_channels (int, optional): Number of channels in the output image. Defaults to 3.

Returns:
    np.ndarray: Multi-channel image with shape (height, width, num_channels)

rH   )r1   squeezer,   r7   )grayscale_imager  squeezeds      rD   r  r    s9    ( a zz/*H99hZ"5566rF   c                    U R                   SS u  pE[        R                  " U SUUUS9n[        R                  " XeU4US9$ )a  Downscale and upscale an image.

This function downscales and upscales an image using the specified interpolation methods.
The downscaling and upscaling are performed using the cv2.resize function.

Args:
    img (np.ndarray): Input image as a numpy array.
    scale (float): The scale factor for the downscaling and upscaling.
    down_interpolation (int): The interpolation method for the downscaling.
    up_interpolation (int): The interpolation method for the upscaling.

Returns:
    np.ndarray: The downscaled and upscaled image.

Nr$   )fxfyinterpolationr  )rM   r,   resize)r:   rv   down_interpolationup_interpolationr  r  
downscaleds          rD   	downscaler     sJ    . IIbqMMF(J ::j&/AQRRrF   c                    U $ )aC  No-op function.

This function is a no-op and returns the input object unchanged.
It is used to satisfy the type checker requirements for the `noop` function.

Args:
    input_obj (Any): The input object to return unchanged.
    **params (Any): Additional keyword arguments.

Returns:
    Any: The input object unchanged.

r   )	input_objparamss     rD   noopr  C  s
     rF   c           	        U R                   n[        U 5      nU R                  SU5      n[        R                  " USS9nXE-
  nUS:X  a"  [        R
                  " U5      nUS   U-  U-  nO[        R                  " USS9n	[        R                  R                  U	5      u  pU
SSS2   R                  5       nX   n
USS2U4   n[        R                  " [        R                  " U[        R                  " X-  5      5      UR                  5      R                  nXH-   nUR                  U5      n[        R                  " USSUS9$ )	a5  Perform 'Fancy PCA' augmentation on an image with any number of channels.

Args:
    img (np.ndarray): Input image
    alpha_vector (np.ndarray): Vector of scale factors for each principal component.
                               Should have the same length as the number of channels in the image.

Returns:
    np.ndarray: Augmented image of the same shape, type, and range as the input.

Image types:
    uint8, float32

Number of channels:
    Any

Note:
    - This function generalizes the Fancy PCA augmentation to work with any number of channels.
    - It preserves the original range of the image ([0, 255] for uint8, [0, 1] for float32).
    - For single-channel images, the augmentation is applied as a simple scaling of pixel intensity variation.
    - For multi-channel images, PCA is performed on the entire image, treating each pixel
      as a point in N-dimensional space (where N is the number of channels).
    - The augmentation preserves the correlation between channels while adding controlled noise.
    - Computation time may increase significantly for images with a large number of channels.

References:
    ImageNet classification with deep convolutional neural networks: Krizhevsky, A., Sutskever, I.,
        & Hinton, G. E. (2012): In Advances in neural information processing systems (pp. 1097-1105).

rI   r   r   rH   F)rowvarNr  )rM   r   r  r1   r  stdcovlinalgeighargsortdotdiagr   r   )r:   alpha_vector
orig_shaper   img_reshapedimg_meanimg_centeredstd_devnoiseimg_coveig_valseig_vecs	sort_permimg_pcas                 rD   	fancy_pcar  T  s0   D J#C(L ;;r<0L ww|!,H*Lq&&&Q')L8 &&e4  YY^^G4 TrTN**,	&AyL) FF8RWW\%<=>NN
 ! 	 "G ooj)G 777Aqg..rF   c                ^    US:X  a  [         R                  " U 5      $ US:X  a  U $ [        XSS9$ )ap  Adjust the brightness of an image.

This function adjusts the brightness of an image by multiplying each pixel value by a factor.
The brightness is adjusted by multiplying the image by the factor.

Args:
    img (np.ndarray): Input image as a numpy array.
    factor (np.ndarray): The factor to adjust the brightness by.

Returns:
    np.ndarray: The adjusted image.

r   rH   Fr*   )r1   r   r   r:   factors     rD   adjust_brightness_torchvisionr    s3     {}}S!!{
C//rF   c                |   US:X  a  U $ [        U 5      (       a  U R                  5       O2[        R                  " U [        R                  5      R                  5       nUS:X  aK  U R
                  [        R                  :w  a  [        US-   5      n[        R                  " XU R
                  S9$ [        XUSU-
  -  SS9$ )ac  Adjust the contrast of an image.

This function adjusts the contrast of an image by multiplying each pixel value by a factor.
The contrast is adjusted by multiplying the image by the factor.

Args:
    img (np.ndarray): Input image as a numpy array.
    factor (float): The factor to adjust the contrast by.

Returns:
    np.ndarray: The adjusted image.

rH   r   r   r'   Fr*   )r   r  r,   r-   r9   r(   r1   r   rL   r   r   )r:   r  r  s      rD   adjust_contrast_torchvisionr    s     {
+C00388:cll3HZHZ6[6`6`6bD{99

"tcz?D||CSYY77TQZ%8%HHrF   c                
   US:X  d  [        U 5      (       a  U $ [        R                  " U [        R                  5      n[        R                  " U[        R                  5      nUS:X  a  U$ [        R
                  " XUSU-
  US9$ )a  Adjust the saturation of an image.

This function adjusts the saturation of an image by multiplying each pixel value by a factor.
The saturation is adjusted by multiplying the image by the factor.

Args:
    img (np.ndarray): Input image as a numpy array.
    factor (float): The factor to adjust the saturation by.
    gamma (float): The gamma value to use for the adjustment.

Returns:
    np.ndarray: The adjusted image.

rH   r   )r|  )r   r,   r-   r9   r.   r   )r:   r  r|  grays       rD   adjust_saturation_torchvisionr    sl    * {(--
<<S//0D<<c001DQ;4_COOCq6zY^$__rF   c                t   [         R                  " U [         R                  5      n [        R                  " SS[        R
                  S9n[        R                  " USU-  -   S5      R                  [        R                  5      n[        U S   USS9U S'   [         R                  " U [         R                  5      $ )Nr   r&   r'   r)   r   Fr*   )r,   r-   r/   r1   r2   r3   r4   r5   r6   r   r8   )r:   r  rU   s      rD   _adjust_hue_torchvision_uint8r    s    
,,sC--
.C
))As"((
+C
&&sV|#S
)
0
0
:CVc59CK<<S..//rF   c                ^   [        U 5      (       d  US:X  a  U $ U R                  [        R                  :X  a  [	        X5      $ [
        R                  " U [
        R                  5      n [        R                  " U S   US-  -   S5      U S'   [
        R                  " U [
        R                  5      $ )zAdjust the hue of an image.

This function adjusts the hue of an image by adding a factor to the hue value.

Args:
    img (np.ndarray): Input image.
    factor (float): The factor to adjust the hue by.

Returns:
    np.ndarray: The adjusted image.

r   r   ih  )
r   r(   r1   r6   r  r,   r-   r/   r4   r8   r  s     rD   adjust_hue_torchvisionr    s     #&A+

yyBHH,S99
,,sC--
.C&&Vv|3S9CK<<S..//rF   c                
   [         R                  " U5      (       d  U $ U R                  nUbd  [        U R                  SS 5      nXc:  aG  X6-  nU R                  SS u  p[	        X-  5      [	        X-  5      p[
        R                  " X
U4U5      n [        U USS9nSn[        U R                     n[         R                  " U 5      n U R                  [        :X  a  [         R                  " U SS9n [        U 5      n[        U5       H  nU SU4   n[         R                   " U5      nUS   S:X  a  US	S n[#        U5       H  u  nnUU[%        U5      -     (       d  M  UU:H  n[         R&                  " UU   5      nUR                  R(                  S
;   a6  [	        [         R*                  " U5      5      n[-        [        UU5      U5      nOUnUUU'   M     M     XPR                  :w  a  [
        R                  " XSS U5      $ U $ )aS  Apply superpixels to an image.

This function applies superpixels to an image using the SLIC algorithm.
The superpixels are applied by replacing the pixels in the image with the mean intensity of the superpixel.

Args:
    image (np.ndarray): Input image as a numpy array.
    n_segments (int): The number of segments to use for the superpixels.
    replace_samples (Sequence[bool]): The samples to replace.
    max_size (int | None): The maximum size of the superpixels.
    interpolation (int): The interpolation method to use.

Returns:
    np.ndarray: The superpixels applied to the image.

Nr$   r1  )
n_segmentscompactnessr   rI   r   .rH   )rT   ub)r1   rr   rM   r  rL   
fgeometricr  slicr   r(   rg   rO   r    rP   r   rK   uniquer[   rN   r  kindrt   r  )r  r  replace_samplesmax_sizer  r  r   rv   r  r  
new_height	new_widthsegments	min_valuer   r   c
image_sp_cunique_labelsidxlabelr^   mean_intensityvalues                           rD   superpixelsr    s   2 66/""J5;;r?#?OE!KKOMF$'$7U]9K	%%e)-DmTEH I#EKK0IGGENEzz,,u2.#E*L< 36]
		(+q )!"-M $M2JC sS%99::5(!#D)9!:##((O;
   89EE9 5yAE*E#(
4 # 3 !6 GQT_T_F_:UrNMBjejjrF   c                   [        [        R                  X4US9nU R                  [        :X  a$  [        U 5      S:X  a  [        R                  " U SS9n U" U 5      nX-
  n[        R                  " U5      S-  U:  nUR                  [        R                  5      nXU-  -   n	[        R                  " U	SSU	S9n	U" U5      n
[        [        X5      [        U SU
-
  5      SS	9$ )
a  Apply an unsharp mask to an image.
This function applies an unsharp mask to an image using the Gaussian blur function.
The unsharp mask is applied by subtracting the blurred image from the original image and
then adding the result to the original image.

Args:
    image (np.ndarray): Input image as a numpy array.
    ksize (int): The kernel size to use for the Gaussian blur.
    sigma (float): The sigma value to use for the Gaussian blur.
    alpha (float): The alpha value to use for the unsharp mask.
    threshold (int): The threshold value to use for the unsharp mask.

Returns:
    np.ndarray: The unsharp mask applied to the image.

)ksizer   rH   rI   r   rd   r   r  Tr*   )r   r,   r   rO   r!   r   r1   r  absr5   r   r   r
   r   )r  r  sigmar-  rR   blur_fnr  residualr^   sharp	soft_masks              rD   unsharp_maskr  e  s    4 &nG zz116Fu6MQR6R

5r*5>D|H 66(c!I-D;;rzz"DH$$EGGE1aU+EI"I& rF   c                0    [         R                  " XU 5      $ )a  Apply pixel dropout to the image.

Args:
    image (np.ndarray): Input image
    drop_mask (np.ndarray): Boolean mask indicating which pixels to drop
    drop_values (np.ndarray): Values to replace dropped pixels with

Returns:
    np.ndarray: Image with dropped pixels

)r1   rQ   )r  	drop_maskdrop_valuess      rD   pixel_dropoutr    s    " 88IE22rF   c                    [        XSS9$ )a"  Apply spatter rain to an image.

This function applies spatter rain to an image by adding the rain to the image.

Args:
    img (np.ndarray): Input image as a numpy array.
    rain (np.ndarray): Rain image as a numpy array.

Returns:
    np.ndarray: The spatter rain applied to the image.

Fr*   r	   )r:   rains     rD   spatter_rainr    s      s%((rF   c                    [        X-  USS9$ )aV  Apply spatter mud to an image.

This function applies spatter mud to an image by adding the mud to the image.

Args:
    img (np.ndarray): Input image as a numpy array.
    non_mud (np.ndarray): Non-mud image as a numpy array.
    mud (np.ndarray): Mud image as a numpy array.

Returns:
    np.ndarray: The spatter mud applied to the image.

Fr*   r	  )r:   non_mudmuds      rD   spatter_mudr    s    " s}c511rF   c                   U R                   SS u  pg[        R                  " S[        R                  S9nXxS'   XhS'   US-  US'   US-  US	'   [        R                  " XS
S
/[        R                  S9n	[        R                  " X4S
S
/[        R                  S9n
[        U S   UU	UUU5      n[        U S   UU
UUU5      n[        R                  " XS   U/5      $ )a  Apply chromatic aberration to an image.

This function applies chromatic aberration to an image by distorting the red and blue channels.

Args:
    img (np.ndarray): Input image as a numpy array.
    primary_distortion_red (float): The primary distortion of the red channel.
    secondary_distortion_red (float): The secondary distortion of the red channel.
    primary_distortion_blue (float): The primary distortion of the blue channel.
    secondary_distortion_blue (float): The secondary distortion of the blue channel.
    interpolation (int): The interpolation method to use.

Returns:
    np.ndarray: The chromatic aberration applied to the image.

Nr$   r   r'   r   )rH   rH   g       @)r   r$   )rH   r$   r   r   .r$   r  )rM   r1   eyer   rJ   _distort_channelr   )r:   primary_distortion_redsecondary_distortion_redprimary_distortion_bluesecondary_distortion_bluer  r  r  
camera_matdistortion_coeffs_reddistortion_coeffs_bluered_distortedblue_distorteds                rD   chromatic_aberrationr    s    4 IIbqMMF ,Jtts{Jt|Jt HH	1a@jj  XX	 QBjj %FM &FN 99m[.ABBrF   c           	         [         R                  " UUS UXC4[         R                  S9u  pg[         R                  " U UUU[         R                  S9$ )N)cameraMatrix
distCoeffsRnewCameraMatrixr   m1type)r  
borderMode)r,   initUndistortRectifyMapCV_32FC1remapBORDER_REPLICATE)channelr  distortion_coeffsr  r  r  map_xmap_ys           rD   r  r    sV     ..$
"_||LE 99#'' rF   i  )gk+ݓ?gӼ?g_LU?i  )a+e?gMSt$?gZd;O?i  )gD?gs?g_L?i  )g<Nё\?gJY?g:#J{/?i  )gJ4?gL7A`?g9m4?i|  )gݓ?gx&?gcZB?ip  )gj+?g-!lV?gF_?id  )g:pΈ?gŏ1w?g<,Ԛ?iX  )g<,Ԛ?g oŏ?gfj+?iL  )gu?goʡ?g?W[?i@  )gjMS?g1?gSt$?i4!  )gS?g)Ǻ?gvq-?i(#  )gsF?g:H?gl	g?i%  )gX9v?gW2?g^I+?i'  )gs?y?g6<R?i)  )gFx?Ӽ?gI.!?i*  )gd]K?n?gTN?)gW[?\ Ac?gW[?)g[<?r1  g鷯?)g4@?r1  g^)?)gZd;O?r0  gAf?)gz6>?r0  g%䃞?)gOjM?r0  gm?)glV}?r/  g
F%u?)g{Pk?r.  g:#J{/?),  .  0  2  4  6  8  :  )g,Ԛ?gAc]K?gQI?)goʡ?gZӼ?gY ?)gDio?gQI?g46<R?)gJ4?g<R!?gb48?)g,C?g.n?gGz?)r/  gBi?r/  )gU0*?g|гY?g|Pk?)g\C?go_?gF_?)gA`"?gGz?g9#J?)gBi?g&1?g<,Ԛ?)g:H?g9#?g镲q?)gkw#?gy)?g?W[?)g6;Nё?gV-?gz6>W[?)g]Fx?gX2ı.?g:H?)gjM?g|a2U0?r-  r2  )gHP?g-1?g"~j?r3  )g[B>٬?߾3?g?ܵ?)gʡE?r:  g`"?)gۊe?r:  gH}8?)gK7?r:  g58EGr?)gJ4?r:  g'?)g+e?r:  g?)gǘ?r:  g/n?)r4  r5  r6  r7  r8  r9  )	blackbodyciedz!dict[str, dict[int, list[float]]]PLANCKIAN_COEFFSc                   U R                  5       n [        [        U   R                  5       5      n[	        [        U   R                  5       5      n[
        R                  " XU5      nSn[	        X-  U-  U5      n[        X-  S-   U-  U5      nXg:X  a!  [
        R                  " [        U   U   5      nOUX-
  Xv-
  -  n	SU	-
  n
U
[
        R                  " [        U   U   5      -  U	[
        R                  " [        U   U   5      -  -   n[        U SS2SS2S4   US   US   -  SS9U SS2SS2S4'   [        U SS2SS2S4   US   US   -  SS9U SS2SS2S4'   U $ )a  Apply Planckian jitter to an image.

This function applies Planckian jitter to an image by linearly interpolating
between the two closest temperatures in the PLANCKIAN_COEFFS dictionary.

Args:
    img (np.ndarray): Input image as a numpy array.
    temperature (int): The temperature to apply.
    mode (Literal["blackbody", "cied"]): The mode to use.

Returns:
    np.ndarray: The Planckian jitter applied to the image.

i  rH   Nr   Tr*   r$   )	rg   r  r=  keysr  r1   r   rJ   r   )r:   temperaturer   min_tempmax_temprn   t_leftt_rightcoeffsw_rightw_lefts              rD   planckian_jitterrH  j  s   ( ((*C#D)..01H#D)..01H ''+:K D		$F 		q	 D(G *4089'G,<=W"((#3D#9&#ABBWrxxT"7+P
 F
 
 (Aq!Gq	F1IC1aL
 (Aq!Gq	F1IC1aL JrF   c                    [        XSS9$ )a  Add noise to an image.

This function adds noise to an image by adding the noise to the image.

Args:
    img (np.ndarray): Input image as a numpy array.
    noise (np.ndarray): Noise as a numpy array.

Returns:
    np.ndarray: The noise added to the image.

Fr*   r	  )r:   r  s     rD   	add_noiserJ    s     s5))rF   c           	        U R                   [        :X  a  U S[        R                  4   n U R                  SS u  pEXE-  nU R                  [        R                  5      [        R                  " U S-   5      -  n[        Xa-  S-  5      n[        R                  " US-  XX5      n	[        R                  " US-  XH5      n
[        R                  " U
 VVs/ s H  o  H  oU:  d  M
  X:  d  M  X4PM     M     snn5      nS[        R                  " XE4[        R                  S9-  n[        R                  " XE4[        R                  5      n[        U5       GHu  n[!        U5       H  u  nn[        US   5      [        US	   5      p[        S	X-
  5      [#        XKU-   S-   5      nn[        S	X-
  5      [#        X\U-   S-   5      nnUUU2UU24   nUX{U4   -
  n[        R$                  " US-  SS
9n[        R&                  UU2UU24   u  nnUU-
  S-  UU-
  S-  -   US-  -  nUUU-  -   nUUUU2UU24   :  nUU   UUU2UU24   U'   UUUU2UU24   U'   M     [        [)        U5      5       HW  nUU:H  n[        R*                  " U5      (       d  M%  [        R,                  " [        R.                  " U5      S	S
9SSS2   UU'   MY     GMx     U$ s  snnf )a  Simple Linear Iterative Clustering (SLIC) superpixel segmentation using OpenCV and NumPy.

Args:
    image (np.ndarray): Input image (2D or 3D numpy array).
    n_segments (int): Approximate number of superpixels to generate.
    compactness (float): Balance between color proximity and space proximity.
    max_iterations (int): Maximum number of iterations for k-means.

Returns:
    np.ndarray: Segmentation mask where each superpixel has a unique label.

.Nr$   ư>r   rI   r'   rH   r   r   )rO   r    r1   r   rM   r5   r   r  rL   r2   rJ   onesr  fullinfrK   r[   r  rh   rM  rN   rr   r  argwhere)r  r  r  max_iterationsr  r  
num_pixelsimage_normalized	grid_stepx_rangey_ranger+  r*  centerslabels	distancesr   rT   r  y_lowy_highx_lowx_highcrop
color_diffcolor_distanceyyxxspatial_distancedistancer^   s                                  rD   r  r    s   $ zz,,c2::o&KKOMFJ ||BJJ/"&&2FF Z,45Iii	Q9Gii	Q:Ghh NA'QY1:!'NG
 "''6/::F0I>""7+IAvvay>3vay>q  1=13v9}q?P3Q6E1=13u)ma>O3P6E $E&L%,$>?D 0A 66JVVJM;NXXeFlE&L89FB!#aAaA =)Q,O%6F(FFHifeFl(BCCD:B4.IeFlE&L01$778F5<v-.t4' ,, s7|$AQ;Dvvd||WWR[[%6Q?"E
 %/ #8 MG 	Os   K
#K
*K
c           	         [         R                  " U S5      nX1S-  -   U-  n[        UR                  U5      R	                  [
        R                  5      USS9n[        [
        R                  " USSUS9S5      $ )	zApply shot noise to the image.

Args:
    img (np.ndarray): Input image
    scale (float): Scale factor for the noise
    random_generator (np.random.Generator): Random number generator

Returns:
    np.ndarray: Image with shot noise

g@rL  Tr*   r   rH   r  g]tE?)	r,   powr   r  r5   r1   r   r   r   )r:   rv   r   
img_linear
scaled_img	noisy_imgs         rD   
shot_noiserj   	  st    & c"J t|+u4J %  ,33BJJ?I Aqi8'BBrF   c                    U S:  a,  [         R                  " USU5      n[        XU-
  U-  5      nXC4$ [        X5      n[        X* U-  5      nXC4$ )zGet safe brightness and contrast parameters.

Args:
    alpha (float): Contrast factor
    beta (float): Brightness factor
    max_value (float): Maximum pixel value

Returns:
    tuple[float, float]: Safe alpha and beta values

r   )r1   r   r  r  )r-  betar   	safe_beta
safe_alphas        rD   #get_safe_brightness_contrast_paramsro  #	  sd      qy GGD!Y/	Y!6) CD
   	 (	
Y 67
  rF   c                ,   Uc#  [         R                  " U[         R                  S9$ UR                  SS5      n[        R
                  " U5        US:X  a  [        U UUUU5      $ US:X  a$  US:X  a  [        U UUUU5      $ [        U UUUU5      $ USS u  p[        S	[        X-  5      5      n
[        S	[        X-  5      5      nX4USS -   nUS:X  a  [        U UUUU5      nO[        U UUUU5      n[        R                  " XU	4[        R                  S
9$ )a  Generate noise with optional approximation for speed.

This function generates noise with optional approximation for speed.

Args:
    noise_type (Literal["uniform", "gaussian", "laplace", "beta"]): The type of noise to generate.
    spatial_mode (Literal["constant", "per_pixel", "shared"]): The spatial mode to use.
    shape (tuple[int, ...]): The shape of the noise to generate.
    params (dict[str, Any] | None): The parameters of the noise to generate.
    max_value (float): The maximum value of the noise to generate.
    approximation (float): The approximation to use for the noise to generate.
    random_generator (np.random.Generator): The random number generator to use.

Returns:
    np.ndarray: The noise generated.

Nr'   r   i   r   r   sharedr$   rH   r  )r1   rV  r   r  r,   
setRNGSeedgenerate_constant_noisegenerate_shared_noisegenerate_per_pixel_noiser  rL   r  r  INTER_LINEAR)
noise_typespatial_moderM   r  r   approximationr   cv2_seedr  r  reduced_heightreduced_widthreduced_shaper  s                 rD   generate_noiser~  C	  sS   4 ~xxRZZ00((E2HNN8z!&
 	
 8#(   (
 	
 "1IMFC 678N3u456M#3eABi?M x%
 )
 UUO3CSCSTTrF   c                V    [        U5      [        :  a  US   OSn[        U U4UUU5      $ )a'  Generate constant noise.

This function generates constant noise by sampling from the noise distribution.

Args:
    noise_type (Literal["uniform", "gaussian", "laplace", "beta"]): The type of noise to generate.
    shape (tuple[int, ...]): The shape of the noise to generate.
    params (dict[str, Any]): The parameters of the noise to generate.
    max_value (float): The maximum value of the noise to generate.
    random_generator (np.random.Generator): The random number generator to use.

Returns:
    np.ndarray: The constant noise generated.

rI   rH   )rN   r    sample_noise)rw  rM   r  r   r   r   s         rD   rs  rs  	  s9    , !$E
-D D59!L	 rF   c                    [        XX#U5      $ )a*  Generate per-pixel noise.

This function generates per-pixel noise by sampling from the noise distribution.

Args:
    noise_type (Literal["uniform", "gaussian", "laplace", "beta"]): The type of noise to generate.
    shape (tuple[int, ...]): The shape of the noise to generate.
    params (dict[str, Any]): The parameters of the noise to generate.
    max_value (float): The maximum value of the noise to generate.
    random_generator (np.random.Generator): The random number generator to use.

Returns:
    np.ndarray: The per-pixel noise generated.

)r  )rw  rM   r  r   r   s        rD   ru  ru  	  s    , 
6>NOOrF   c                    U S:X  a  [        XU5      U-  $ U S:X  a  [        XU5      U-  $ U S:X  a  [        XU5      U-  $ U S:X  a  [        XU5      U-  $ [	        SU  35      e)a  Sample from specific noise distribution.

This function samples from a specific noise distribution.

Args:
    noise_type (Literal["uniform", "gaussian", "laplace", "beta"]): The type of noise to generate.
    size (tuple[int, ...]): The size of the noise to generate.
    params (dict[str, Any]): The parameters of the noise to generate.
    max_value (float): The maximum value of the noise to generate.
    random_generator (np.random.Generator): The random number generator to use.

Returns:
    np.ndarray: The noise sampled.

uniformgaussianlaplacerl  zUnknown noise type: )sample_uniformsample_gaussiansample_laplacesample_betarz   )rw  r   r  r   r   s        rD   r  r  	  s    , Yd,<=	IIZt-=>JJYd,<=	IIV4)9:YFF
+J<8
99rF   c           
     h   [        U 5      S:X  a  US   nU S   n[        U5      S:X  a  X4-  nO)[        U5      U:  a  [        SU S[        U5       35      e[        R                  " USU  VVs/ s H  u  pVUR	                  XV5      PM     snn5      $ US   S   u  pVUR	                  XVU S9$ s  snnf )a	  Sample from uniform distribution.

Args:
    size (tuple[int, ...]): Size of the output array
    params (dict[str, Any]): Distribution parameters
    random_generator (np.random.Generator): Random number generator

Returns:
    np.ndarray | float: Sampled values

rH   rangesr   z%Not enough ranges provided. Expected z, got Nr  )rN   rz   r1   rJ   r  )r   r  r   r  r   lowhighs          rD   r  r  	  s      4yA~!Awv;!*F[<'7~VCPVK=Y  xxBH,BWXBWYS%%c0BWX
 	

 x #IC##CD#99 Ys   /B.
c                   US   S   US   S   :X  a  US   S   OUR                   " US   6 nUS   S   US   S   :X  a  US   S   OUR                   " US   6 n[        U 5      [        :  a  U S   OSnU[        R                  " U4[        R
                  S9-  nU[        R                  " U4[        R
                  S9-  n[        R                  " U S9n[        R                  " XUS9  UR                  [        R
                  5      $ )	ak  Sample from Gaussian distribution.

This function samples from a Gaussian distribution.

Args:
    size (tuple[int, ...]): The size of the noise to generate.
    params (dict[str, Any]): The parameters of the noise to generate.
    random_generator (np.random.Generator): The random number generator to use.

Returns:
    np.ndarray: The Gaussian noise sampled.


mean_ranger   rH   	std_ranger$   )rM   r(   )rM   )r   r  r  )
r  rN   r    r1   rM  r   rV  r,   randnr5   )	r   r  r   r  r  r   mean_vectorstd_dev_vectorgaussian_sampled_arrs	            rD   r  r  
  s   ( ,"f\&:1&== 	|Q%%vl';< 	 +q!VK%8%;; 	{A%%vk':; 
 "$i*AA47qLbjjIIK277,

KKN88$/II&P&&rzz22rF   c                j    UR                   " US   6 nUR                   " US   6 nUR                  X4U S9$ )ah  Sample from Laplace distribution.

This function samples from a Laplace distribution.

Args:
    size (tuple[int, ...]): The size of the noise to generate.
    params (dict[str, Any]): The parameters of the noise to generate.
    random_generator (np.random.Generator): The random number generator to use.

Returns:
    np.ndarray: The Laplace noise sampled.

r  scale_range)r   rv   r   )r  r  )r   r  r   r   rv   s        rD   r  r  =
  sD    $ 
"
"F<$8
9C$$f]&;<E##t#DDrF   c                    UR                   " US   6 nUR                   " US   6 nUR                   " US   6 nUR                  X4U S9nSU-  S-
  U-  $ )a_  Sample from Beta distribution.

This function samples from a Beta distribution.

Args:
    size (tuple[int, ...]): The size of the noise to generate.
    params (dict[str, Any]): The parameters of the noise to generate.
    random_generator (np.random.Generator): The random number generator to use.

Returns:
    np.ndarray: The Beta noise sampled.

alpha_range
beta_ranger  r  r$   rH   )r  rl  )r   r  r   r-  rl  rv   sampless          rD   r  r  T
  sm    $ $$f]&;<E##VL%9:D$$f]&;<E ##Ed#;GK!Ou$$rF   c                    USS u  pV[        U XV4UUU5      n[        U5      [        :  a  [        R                  " US   U5      $ U$ )a  Generate shared noise.

Args:
    noise_type (Literal["uniform", "gaussian", "laplace", "beta"]): Type of noise to generate
    shape (tuple[int, ...]): Shape of the output array
    params (dict[str, Any]): Distribution parameters
    max_value (float): Maximum value for the noise
    random_generator (np.random.Generator): Random number generator

Returns:
    np.ndarray: Generated noise

Nr$   .N)r  rN   r    r1   broadcast_to)rw  rM   r  r   r   r  r  	noise_maps           rD   rt  rt  o
  sW    * "1IMF	I 5z++y3U;;rF   c                D    [         R                  " U X"4UUS9nXX-
  -  -   $ )a  Sharpen image using Gaussian blur.

This function sharpens an image using a Gaussian blur.

Args:
    img (np.ndarray): The image to sharpen.
    alpha (float): The alpha value to use for the sharpening.
    kernel_size (int): The kernel size to use for the Gaussian blur.
    sigma (float): The sigma value to use for the Gaussian blur.

Returns:
    np.ndarray: The sharpened image.

)r  r   r   )r,   r   )r:   r-  kernel_sizer  blurreds        rD   sharpen_gaussianr  
  s5    , (	G #-(((rF   c           	         U R                   S:X  a
  US   nUS   n[        U R                     n[        R                  " X[        R                  " USU 5      5      $ )a  Apply salt and pepper noise to an image.

This function applies salt and pepper noise to an image using pre-computed masks.

Args:
    img (np.ndarray): The image to apply salt and pepper noise to.
    salt_mask (np.ndarray): The salt mask to use for the salt and pepper noise.
    pepper_mask (np.ndarray): The pepper mask to use for the salt and pepper noise.

Returns:
    np.ndarray: The image with salt and pepper noise applied.

r   r  r   )rO   r   r(   r1   rQ   )r:   	salt_maskpepper_maskr   s       rD   apply_salt_and_pepperr  
  sO    & xx1}i(	!),#CII.I88I"((;3*GHHrF   )      ?r   r  )r   r   r   r'   )r   r  r   r   r   c                ~  ^ S	U4S jjn[        U 5      nS[        R                  " [        R                  " US-
  5      5      -  S-   n[	        [        R                  " US-
  5      S-
  5      n[        R
                  " [        U5       Vs/ s H  oqU-  PM	     sn5      nTR                  SSS5      R                  [        R
                  5      n	U H  n
U" X5      n	M     [        R                  " [        R                  " U	SU S   2SU S   24   SSS[        R                  [        R                  S9SS5      $ s  snf )
a  Generate a plasma pattern.

This function generates a plasma pattern using the diamond-square algorithm.

Args:
    target_shape (tuple[int, int]): The shape of the plasma pattern to generate.
    roughness (float): The roughness of the plasma pattern.
    random_generator (np.random.Generator): The random number generator to use.

Returns:
    np.ndarray: The plasma pattern generated.

c           	       > U R                   S   S-
  S-  S-   nU R                   S   S-
  S-  S-   n[        R                  " X#4[        R                  S9nT
R	                  U* XU45      R                  [        R                  5      nXS S S2S S S24   -   US S S2S S S24'   [        R                  " US[        [        R                  S9nUS:  nXFU-   U-  -  n[        R                  " US[        [        R                  S9nUS:  n	XHU-   U	-  -  n[        R                  " US SS[        R                  [        R                  S9$ )Nr   rH   r$   r'   rI   )
borderType)rM   r1   rV  r   r  r5   r,   filter2DDIAMOND_KERNELBORDER_CONSTANTSQUARE_KERNEL	normalizeNORM_MINMAXCV_32F)current_gridnoise_scalenext_height
next_widthexpanded_grid	all_noisediamond_interpolationdiamond_masksquare_interpolationsquare_maskr   s             rD   one_diamond_square_step8generate_plasma_pattern.<locals>.one_diamond_square_step
  sK   #))!,q0A59"((+a/14q8
 +!:"**M %,,k\;V`Habiijljtjtu	 #/3Q3!81D"Dcc3Q3h !$]B[^[n[n o,q0);|KK  #||M2}Y\YlYlm*Q.:kII }}]D!QszzZZrF   r$   rH   rI   r  Nr   r'   )r  r   r  floatr   r   )r  r1   ceillog2rL   r   rK   r  r5   r   r,   r  r  r  )target_shape	roughnessr   r  max_dimensionpower_of_two_sizetotal_stepsrT   noise_scalesplasma_gridr  s     `        rD   generate_plasma_patternr  
  s   &[6 %MRWWRWW]Q->%?@@1Dbgg/!34q89K::U;5GH5G!|5GHIL #**2q&9@@LK $-kG $ 77k"3LO"35F|A5F"FGqRSUXUdUdlolvlvw		  Is   
D:c                   US:X  a  US:X  a  U $ U R                  5       n U R                  [        :  a:  [        R                  " US[        R
                  4   SSU R                  S   45      nUS:w  a  [        X1SS9n[        XSS9n US:w  a9  U R                  5       n[        X2SS9S-   n[        XSS9n USU-
  -  n[        XSS9$ U $ )	a&  Apply plasma-based brightness and contrast adjustments.

This function applies plasma-based brightness and contrast adjustments to an image.

Args:
    img (np.ndarray): The image to apply the brightness and contrast adjustments to.
    brightness_factor (float): The brightness factor to apply.
    contrast_factor (float): The contrast factor to apply.
    plasma_pattern (np.ndarray): The plasma pattern to use for the brightness and contrast adjustments.

Returns:
    np.ndarray: The image with the brightness and contrast adjustments applied.

r   .rH   rI   Fr*   Tr   )
rg   rO   r    r1   tiler   rM   r   r	   r  )r:   brightness_factorcontrast_factorplasma_patternbrightness_adjustmentr  contrast_weightsmean_factors           rD    apply_plasma_brightness_contrastr  '  s    . A/Q"6

((*C xx))RZZ!@1aSUBWX A (TY Z#d; !xxz#NUSVWWsd;c$4453T22JrF   c                l    X!-  nU R                   [        :  a  US[        R                  4   nU SU-
  -  $ )zApply plasma shadow to the image.

Args:
    img (np.ndarray): Input image
    intensity (float): Shadow intensity
    plasma_pattern (np.ndarray): Plasma pattern to use

Returns:
    np.ndarray: Image with plasma shadow

.rH   )rO   r    r1   r   )r:   r  r  scaled_patterns       rD   apply_plasma_shadowr  Y  s?    $ $/N xx))'RZZ8 !n$%%rF   c           	     
   US:X  aR  [         R                  " SSU[         R                  S9SSS24   [         R                  " U S4[         R                  S9-  $ US:X  aR  [         R                  " SSU[         R                  S9SSS24   [         R                  " U S4[         R                  S9-  $ US:X  aR  [         R                  " SSU [         R                  S9SS2S4   [         R                  " SU4[         R                  S9-  $ US:X  aR  [         R                  " SSU [         R                  S9SS2S4   [         R                  " SU4[         R                  S9-  $ US;   GaU  [         R                  " SSU[         R                  S9SSS24   n[         R                  " SSU [         R                  S9SS2S4   nUS	:X  a7  [        R
                  " X4-   SSS[        R                  [        R                  S9$ US
:X  a;  [        R
                  " SU-
  U-   SSS[        R                  [        R                  S9$ US:X  a>  [        R
                  " SU-
  SU-
  -   SSS[        R                  [        R                  S9$ [        R
                  " USU-
  -   SSS[        R                  [        R                  S9$ [         R                  " SSU [         R                  S9SS2S4   n[         R                  " SSU[         R                  S9SSS24   n[         R                  " U5      n[        R                  " U5      n[        R                  " U5      n[        R                  " X6US9  [        R                  " XGUS9  X4-   $ )a0  Create a directional gradient in [0, 1] range.

This function creates a directional gradient in the [0, 1] range.

Args:
    height (int): The height of the image.
    width (int): The width of the image.
    angle (float): The angle of the gradient.

Returns:
    np.ndarray: The directional gradient.

r   rH   r'   Nr)   rE  i  )rD  rF     i;  rD  rF  r  r   )r1   r   r   rM  r,   r  r  r  deg2radmathrI  rK  r   )r  r  rQ  r*  r+  	angle_radcos_asin_as           rD   create_directional_gradientr  u  s    z{{1abjj9$'BRWWfVW[`b`j`jEkkk|{{1abjj9$'BRWWfVW[`b`j`jEkkk {{{1arzz:1d7CbggqRWj`b`j`jFkkk|{{1arzz:1d7CbggqRWj`b`j`jFkkk ##KK1e2::6tQw?KK1fBJJ74@B;==aCOO3::VVC<==!a%1dAq#//QTQ[Q[\\C<==!a%AE!2D!QWZWaWabb}}Q!a%[$1cooSZZXX 	Aq&

3AtG<A
Aq%rzz247;A

5!IHHYEHHYELLq!LLq!5LrF   c                T   U R                   SS u  p4[        U5      n[        X4U5      nUS:  a  [        R                  " SXfS9  [        R
                  " USU-  US9  [        R                  " USU-
  US9  U R                  [        :X  a  US[        R                  4   n[        X5      $ )zApply linear illumination to the image.

Args:
    img (np.ndarray): Input image
    intensity (float): Illumination intensity
    angle (float): Illumination angle in radians

Returns:
    np.ndarray: Image with linear illumination

Nr$   r   rH   r   .)rM   r  r  r,   subtractr   r	   rO   r!   r1   r   r   )r:   r  rQ  r  r  abs_intensitygradients          rD   apply_linear_illuminationr    s     IIbqMMF	NM +6%@H1}Q/LL1},(;GGHa-'X6 xx//CO,S++rF   c                ~   US:X  a  U R                  5       $ U R                  SS u  p4[        R                  " X3-  XD-  -   5      n[        R
                  " X44S[        R                  S9nSSUS-
  4US-
  US-
  4US-
  S4/nSXgU   '   [        R                  " U[        R                  [        R                  [        R                  S9n[        R                  " X* U-  US	9  [        R                  " USUS	9  U R                  [        :X  a'  [        R                   " U/U R                  S   -  5      n[#        X5      $ )
a  Apply corner illumination to the image.

Args:
    img (np.ndarray): Input image
    intensity (float): Illumination intensity
    corner (Literal[0, 1, 2, 3]): The corner to apply the illumination to.

Returns:
    np.ndarray: Image with corner illumination applied.

r   Nr$   rd   r'   r   rH   )distanceTypemaskSizedstTyper   )rg   rM   r  rN  r1   rN  r6   r,   distanceTransformDIST_L2DIST_MASK_PRECISEr  r   r	   rO   r!   r7   r   )	r:   r  cornerr  r  diagonal_lengthr^   cornerspatterns	            rD   apply_corner_illuminationr    s   " A~xxzIIbqMMF ii%- ?@O 77F?Crxx8D 519~
EAI'>!QPGD ##[[&&

	G LL*6GDGGGQG$
xx//))WI		!45S**rF   c                   US:X  a  U R                  5       $ U R                  SS u  pEXRS   -  nXBS   -  nS[        XE5      U-  S-  -  n[        R                  SU2SU24   u  pU
R                  [        R                  5      n
U	R                  [        R                  5      n	X-  n
X-  n	[        R                  " XU
S9  [        R                  " XU	S9  X-   n
[        R                  " U
SU-  U
S9  [        R                  " XS9  [        R                  " XU
S9  [        R                  " U
SU
S9  U R                  [        :X  a'  [        R                  " U
/U R                  S   -  5      n
[        X
5      $ )zApply gaussian illumination to the image.

Args:
    img (np.ndarray): Input image
    intensity (float): Illumination intensity
    center (tuple[float, float]): The center of the illumination.
    sigma (float): The sigma of the illumination.

r   Nr$   rH   r   rI   )rg   rM   r  r1   rM  r5   r   r,   r   expr	   rO   r!   r7   r   )r:   r  r  r  r  r  center_xcenter_ysigma2r+  r*  s              rD   apply_gaussian_illuminationr    sG     A~xxzIIbqMMF ay Hq	!H#f$u,22F 88GVGVeVO$DA	A	AMAMA LL1LL1	A LLBKQ'GGA LL1%GGAqa
xx//IIqcCIIaL()S$$rF   c           	     >   U R                  5       n[        U 5      n[        U R                     nU R                  [
        :  a  [        R                  " U 5      n/ n[        U5       Hg  u  pUb  X:X  a  UR                  S5        M   Uc  SOX:g  n[        R                  " U
/S/US/SU/5      nUR                  UR                  5       5        Mi     [        U5       H  n	Ub  X:X  a  M  U R                  [
        :  a  WU	   nWU	   n
O8Uc  SOX:g  n[        R                  " U /S/US/SU/5      R                  5       nU n
[        X5      u  pX::  a  Mx  [        XXU5      nUb  X/U'   U R                  [
        :  a  [        X5      USU	4'   M  [        X5      nM     U$ )a<  Apply automatic contrast enhancement.

Args:
    img (np.ndarray): Input image
    cutoff (float): Cutoff percentage for histogram
    ignore (int | None): Value to ignore in histogram
    method (Literal["cdf", "pil"]): Method to use for contrast enhancement

Returns:
    np.ndarray: Image with enhanced contrast

Nr   r&   .)rg   r   r   r(   rO   r    r,   r0   r[   appendre   rf   rK   get_histogram_boundscreate_contrast_lutr   )r:   cutoffignorer  r)  r   r   rS  histsrT   r)  r^   histlohirU   s                   rD   auto_contrastr  3  s   & XXZF#C(L#CII.I xx))99S>"$#H-JA!akT"!>40AD<<	A3sea^LDLL& . < !+88--8DqkG!>4D<<sD3%!YHNNPDG%d38!$B6B K88--#G1F36NG)F/ !2 MrF   c                   X:  a#  [         R                  " S[         R                  S9$ US:X  a  XUS-    nUR                  5       nUS   S:X  a#  [         R                  " S[         R                  S9$ XfS   -
  U-  US   US   -
  -  n[         R                  " S[         R                  S9n[         R
                  " [         R                  " U5      SU5      R                  [         R                  5      XqUS-   & X7US-   S& U$ X2U-
  -  n[         R                  " S[        S9n	[         R
                  " [         R                  " X-
  U-  5      SU5      R                  [         R                  5      nSUSU& X7US-   S& U$ )a  Create lookup table for contrast adjustment.

This function creates a lookup table for contrast adjustment.

Args:
    hist (np.ndarray): Histogram of the image.
    min_intensity (int): Minimum intensity of the histogram.
    max_intensity (int): Maximum intensity of the histogram.
    max_value (int): Maximum value of the lookup table.
    method (Literal["cdf", "pil"]): Method to use for contrast enhancement.

Returns:
    np.ndarray: Lookup table for contrast adjustment.

r&   r'   cdfrH   rI   r   N)	r1   rV  r6   rj   r2   r   rt   r5   r  )
r  min_intensitymax_intensityr   r  
hist_ranger  rU   rv   indicess
             rD   r  r  r  s_   , %xx288,,-!*;<
!r7a<99S11 V|y(CGc!f,<= hhs"((+13#91U1\1\]_]e]e1fMA-.#,MA 
 67Eii5)G ''"((G3u<=q)
L
S
STVT\T\
]CC(JrF   c                   U(       dE  [         R                  " U 5      S   n[        U5      S:X  a  g[        US   5      [        US   5      4$ [	        U R                  5       5      nUS:X  a  gX1-  S-  n[        U 5      S:X  aN  [         R                  " X S   :H  5      (       a.  [        [        U 5      U-  S-  5      n[        U 5      U-
  S-
  nXV4$ SnSn[        [        U 5      5       H  nXpU   -  nXt:  d  M  US-   n  O   [        U[        U 5      S-
  5      nSn[        U 5      S-
  n[        [        U 5      S-
  SS5       H  nXpU   -  nXt:  d  M  Un  O   XV:  a  [        U 5      S-
  S	-  n	X4$ XV4$ )
a  Get the low and high bounds of the histogram.

This function gets the low and high bounds of the histogram.

Args:
    hist (np.ndarray): Histogram of the image.
    cutoff (float): Cutoff percentage for histogram.

Returns:
    tuple[int, int]: Low and high bounds of the histogram.

r   r   rI   g      Y@r&   d   rH   r   r$   )	r1   nonzerorN   rL   r  rh   allrK   r  )
r  r  non_zero_intensitiestotal_pixelspixels_to_cutr  r  rj   rT   	mid_points
             rD   r  r    s    !zz$/2#$)'*+S1Eb1I-JJJ$Lq )E1M 4yCBFF47?33CI.45D	M1A5++ FM3t9q'"EM	 
 s4y1}5M FIMM3t9q="b)q'"M	 * $Y]q(	##''rF   c                    U(       d  [        U 5      S:X  a  UR                  SS/U USU-
  /S9$ UR                  SS/U SS USU-
  /S9n[        U 5      S:X  a  U$ [        R                  " US   U S   SS9$ )	aT  Generate dropout mask.

This function generates a dropout mask.

Args:
    shape (tuple[int, ...]): Shape of the output mask
    per_channel (bool): Whether to apply dropout per channel
    dropout_prob (float): Dropout probability
    random_generator (np.random.Generator): Random number generator

Returns:
    np.ndarray: Dropout mask

r$   TFrH   )pNr  r   )rN   choicer1   rX  )rM   per_channeldropout_probr   mask_2ds        rD   get_drop_maskr    s    ( c%jAo&&5MQ-. ' 
 	
 %%	ubq	\)
* & G 5zQ 99WY'q::rF   c                    U[         R                  :X  a"  UR                  S[        [        U   5      U US9$ U[         R
                  :X  a   UR                  SSU S9R                  U5      $ [        SU 35      e)zGenerate random values.

Args:
    channels (int): Number of channels
    dtype (np.dtype): Data type of the output array
    random_generator (np.random.Generator): Random number generator

Returns:
    np.ndarray: Random values

r   )r   r(   rH   r  zUnsupported dtype: )	r1   r6   r  rL   r   r   r  r5   rz   )rS  r(   r   s      rD   generate_random_valuesr  
  s      ((#E*+	 ) 
 	
 

''18'<CCEJJ
*5'2
33rF   c                   Uc"  [        U 5      n[        X0R                  U5      nOq[        U[        [
        45      (       a)  [        R                  " U R                  XR                  S9$ [        R                  " XR                  S9R                  S5      nU R                  S:X  a-  [        R                  " U R                  US   U R                  S9$ [        R                  " U R                  SS [        U5      4-   X@R                  S9$ )aZ  Prepare values to fill dropped pixels.

Args:
    array (np.ndarray): Input array to determine shape and dtype
    value (float | Sequence[float] | np.ndarray | None): User-specified drop values or None for random
    random_generator (np.random.Generator): Random number generator

Returns:
    np.ndarray: Array of values matching input shape

Nr'   rI   r$   r   )r   r  r(   r   rL   r  r1   rN  rM   rJ   r  rO   rN   )rJ   r  r   rS  valuess        rD   prepare_drop_valuesr  '  s      }#E*'++?OP	EC<	(	(wwu{{E==%{{3;;B? zzQwwu{{F1IU[[AA 775;;r?c&k^3V;;OOrF   c                8    SU ;   a  U S   $ SU ;   a  U S   S   $ S$ )z,Get mask array from input data if it exists.r^   masksr   Nr   )datas    rD   get_mask_arrayr  G  s-    ~F|&$4=8D8rF   c                   [        U S-  [        R                  SS9n S[        R                  " U SS5      -
  n[        R
                  " U[        R                  S5      n[        R                  " USS[        R                  5      u  pC[        R                  " USS	S	[        R                  S
9n[        U[        R                  SS9n[        U5      n[        R                  " / SQ/ SQ/ SQ/[        R                  S9n[        X55      n[        R                  " USSS[        R                  S
9R                  [        R                  5      nU R                  [        R                  5      U-  n[        R                   " USS9nUS:  a	  US	U-  -  nO[        R"                  " U5      nUSS2SS2S4   U-  US-  -  nSU0$ )a5  Generate parameters for rain effect.

This function generates parameters for a rain effect.

Args:
    liquid_layer (np.ndarray): Liquid layer of the image.
    color (np.ndarray): Color of the rain.
    intensity (float): Intensity of the rain.

Returns:
    dict[str, Any]: Parameters for the rain effect.

rd   Fr*   2      r2     r  rH   r  r   r   r  T)rI   r   )rI   rH   rH   )r   rH   r$   r'   )r2  r2  g      ?r   r   r   Ng?drops)r   r1   r6   r,   Cannyr  r  rR   THRESH_TRUNCr   r(  r   rJ   r   convolver5   r  r   )	liquid_layerr!  r  distr   kermm_maxr$  s	            rD   get_rain_paramsr-  N  s   $ s*BHHeDL <S11D  s{{A6DmmD"b#*:*:;GA ''D bhh-D D>D ((	

 jjC DD '' fRZZ 	 	BJJ'$.A FF16"Eqy	QYMM! aDjME!Y_5E 	 rF   c                   U R                   u  pgX:  R                  [        R                  5      n[        R                  " U5      S:X  a^  Xg-  n	[        S[        SU	-  5      5      n
UR                  XSS9n[        R                  " U [        R                  S9nSUR                  U'   US:  a&  [        R                  " USUU[        R                  S	9n[        R
                  " U5      nUS:  a  X-  nOSUS'   X-  n[        R                  " XgS
4[        R                  S9n[        S
5       H  nXU   -  USU4'   M     [        R                  " U5      n[        S
5       HC  nX   S:  a.  [        R                   " X   USU4   -
  X   -  SS5      USU4'   M9  SU-
  USU4'   ME     UR                  [        R                  5      UR                  [        R                  5      S.$ )a  Generate parameters for mud effect.

This function generates parameters for a mud effect.

Args:
    liquid_layer (np.ndarray): Liquid layer of the image.
    color (np.ndarray): Color of the mud.
    cutout_threshold (float): Cutout threshold for the mud.
    sigma (float): Sigma for the Gaussian blur.
    intensity (float): Intensity of the mud.
    random_generator (np.random.Generator): Random number generator.

Returns:
    dict[str, Any]: Parameters for the mud effect.

r   rH   r  F)replacer'   r   r   r"  r   .)r  r  )rM   r5   r1   r   rh   r  rL   r  r   flatr,   r   r(  rV  rK   	ones_liker   )r(  r!  cutout_thresholdr  r  r   r  r  r^   rR  
num_neededflat_indicesmask_maxr  rT   r  s                   rD   get_mud_paramsr6    s   0 !&&MF +33BJJ?D	vvd|q^
Cj 012
'..zu.U}}\<"%		, qy++
 vvd|H!| T
 D ((F1%RZZ
8C 1X1XoCF  ll3G1X8a< ggux#c1f+'=&I1aPGCFO!DjGCFO	  zz"**%>>"**- rF   g&c`?g8?gH?gm?g?g҈}?)gX ?g/'?gH.?)gQkw?g3ı.n?g$?)g?rH  g(\?)gQ?r   )\(?)皙?g)\(?r9  )gQ?gQ?g\(\?)r   gQ?g{Gz?)g{Gz?g(\?g
ףp=
?)r   r   Q?)g(\?gffffff?r  )g(\?r:  r;  )g
ףp=
?g
ףp=
?gzG?)g=
ףp=?gQ?gRQ?)r   g{Gz?gzG?)ruifrokmacenkostandardhigh_contrasth_heavye_heavydarklightc                    [         U R                     nU R                  SS5      R                  [        R
                  5      n[        R                  " X2-  U5      n[        R                  " U5      * $ )zConvert RGB image to optical density.

This function converts an RGB image to optical density.

Args:
    img (np.ndarray): Input image.
    eps (float): Epsilon value.

Returns:
    np.ndarray: Optical density image.

rI   r   )r   r(   r  r5   r1   r   maximumlog)r:   epsr   pixel_matrixs       rD   rgb_to_optical_densityrI    sV     $CII.I;;r1%,,RZZ8L::l6<LFF<   rF   c                d    [         R                  " [         R                  " U S-  SSS95      nX-  $ )zNormalize vectors.

This function normalizes vectors.

Args:
    vectors (np.ndarray): Vectors to normalize.

Returns:
    np.ndarray: Normalized vectors.

r$   rH   Tr   keepdims)r1   rN  rh   )vectorsnormss     rD   normalize_vectorsrO  2  s+     GGBFF7A:A=>E?rF   StainNormalizerc                6    U S:X  a
  [        5       $ [        5       $ )zGet stain normalizer based on method.

This function gets a stain normalizer based on a method.

Args:
    method (Literal["vahadane", "macenko"]): Method to use for stain normalization.

Returns:
    StainNormalizer: Stain normalizer.

vahadane)VahadaneNormalizerMacenkoNormalizer)r  s    rD   get_normalizerrU  B  s     $*Z#7P=N=PPrF   c                  ,    \ rS rSrSrSS jrSS jrSrg)	rP  iQ  z!Base class for stain normalizers.c                    S U l         g r   stain_matrix_target)selfs    rD   __init__StainNormalizer.__init__T  s
    #' rF   c                    [         e)zFit the stain normalizer to an image.

This function fits the stain normalizer to an image.

Args:
    img (np.ndarray): Input image.

)NotImplementedError)rZ  r:   s     rD   fitStainNormalizer.fitW  s
     "!rF   rX  N)r   Noner:   r   r   ra  )__name__
__module____qualname____firstlineno____doc__r[  r_  __static_attributes__r   rF   rD   rP  rP  Q  s    +(	"rF   c                  0    \ rS rSrSrSSS jjrS	S jrSrg)
	SimpleNMFic  a  Simple Non-negative Matrix Factorization (NMF) for histology stain separation.

This class implements a simplified version of the Non-negative Matrix Factorization algorithm
specifically designed for separating Hematoxylin and Eosin (H&E) stains in histopathology images.
It is used as part of the Vahadane stain normalization method.

The algorithm decomposes optical density values of H&E stained images into stain color appearances
(the stain color vectors) and stain concentrations (the density of each stain at each pixel).

The implementation uses an iterative multiplicative update approach that preserves non-negativity
constraints, which are physically meaningful for stain separation as concentrations and
absorption coefficients cannot be negative.

This implementation is optimized for stability by:
1. Initializing with standard H&E reference colors from Ruifrok
2. Using normalized projection for initial concentrations
3. Applying careful normalization to avoid numerical issues

Args:
    n_iter (int): Number of iterations for the NMF algorithm. Default: 100

References:
    - Vahadane, A., et al. (2016): Structure-preserving color normalization and
      sparse stain separation for histological images. IEEE Transactions on
      Medical Imaging, 35(8), 1962-1971.
    - Ruifrok, A. C., & Johnston, D. A. (2001): Quantification of histochemical
      staining by color deconvolution. Analytical and Quantitative Cytology and
      Histology, 23(4), 291-299.

c                l    Xl         [        R                  " / SQ/ SQ/[        R                  S9U l        g )Nr7  r8  r'   )n_iterr1   rJ   r   initial_colors)rZ  rl  s     rD   r[  SimpleNMF.__init__  s,     hh.. **
rF   c                   U R                   R                  5       n[        U5      n[        R                  " XR
                  -  S5      nSn[        U R                  5       H  nXR
                  -  nXBUR
                  -  -  nXGX-   -  -  n[        R                  " US5      nUR
                  U-  nUR
                  U-  U-  nX'X-   -  -  n[        R                  " US5      n[        U5      nM     XB4$ )zFit the NMF model to optical density.

This function fits the NMF model to optical density.

Args:
    optical_density (np.ndarray): Optical density image.

Returns:
    tuple[np.ndarray, np.ndarray]: Stain concentrations and stain colors.

r   rL  )rm  rg   rO  r1   rE  r   rK   rl  )	rZ  optical_densitystain_colorsstain_colors_normalizedstain_concentrationsrG  r   	numeratordenominators	            rD   r  SimpleNMF.fit_transform  s     **//1 #4L"A!zz/<U<U*UWXY t{{#A'..8I.2OPK 1B$CC  $&::.BA#F  -..@I/114HHLXK):;;L ::lA6L,\:L! $$ $11rF   )rm  rl  N)r  )rl  rL   )rp  r   r   tuple[np.ndarray, np.ndarray])rc  rd  re  rf  rg  r[  r  rh  r   rF   rD   rj  rj  c  s    >	
(2rF   rj  c                z   [        U 5      n [        R                  " [        R                  " U SS2S4   U SS2S4   5      [        R                  5      nU SS2S4   [        R
                  " U SS9S-   -  nU SS2S4   [        R
                  " U SS9S-   -  nX-  U-
  n[        R                  " U5      nSU-
  nXV4$ )a  Order stains using a combination of methods.

This combines both angular information and spectral characteristics
for more robust identification.

Args:
    stain_colors (np.ndarray): Stain colors.

Returns:
    tuple[int, int]: Hematoxylin and eosin indices.

NrH   r   r$   r   rL  )rO  r1   r4   arctan2pirh   argmax)rq  angles
blue_ratio	red_ratioscoreshematoxylin_idx	eosin_idxs          rD   order_stains_combinedr    s     %\2L VVBJJ|AqD1<13EFNF ad#rvvl'Cd'JKJQT"bff\&BT&IJI
  9,Fii'OO#I%%rF   c                  "    \ rS rSrSrSS jrSrg)rS  i  a  A stain normalizer implementation based on Vahadane's method for histopathology images.

This class implements the "Structure-Preserving Color Normalization and Sparse Stain Separation
for Histological Images" method proposed by Vahadane et al. The technique uses Non-negative
Matrix Factorization (NMF) to separate Hematoxylin and Eosin (H&E) stains in histopathology
images and then normalizes them to a target standard.

The Vahadane method is particularly effective for histology image normalization because:
1. It maintains tissue structure during color normalization
2. It performs sparse stain separation, reducing color bleeding
3. It adaptively estimates stain vectors from each image
4. It preserves biologically relevant information

This implementation uses SimpleNMF as its core matrix factorization algorithm to extract
stain color vectors (appearance matrix) and concentration matrices from optical
density-transformed images. It identifies the Hematoxylin and Eosin stains by their
characteristic color profiles and spatial distribution.

References:
    Vahadane, et al., 2016: Structure-preserving color normalization
    and sparse stain separation for histological images. IEEE transactions on medical imaging,
    35(8), pp.1962-1971.

Examples:
    >>> import numpy as np
    >>> import albumentations as A
    >>> from albumentations.augmentations.pixel import functional as F
    >>> import cv2
    >>>
    >>> # Load source and target images (H&E stained histopathology)
    >>> source_img = cv2.imread('source_image.png')
    >>> source_img = cv2.cvtColor(source_img, cv2.COLOR_BGR2RGB)
    >>> target_img = cv2.imread('target_image.png')
    >>> target_img = cv2.cvtColor(target_img, cv2.COLOR_BGR2RGB)
    >>>
    >>> # Create and fit the normalizer to the target image
    >>> normalizer = F.VahadaneNormalizer()
    >>> normalizer.fit(target_img)
    >>>
    >>> # Normalize the source image to match the target's stain characteristics
    >>> normalized_img = normalizer.transform(source_img)

c                    [        U5      n[        SS9nUR                  U5      u  pE[        U5      u  pg[        R
                  " XV   XW   /5      U l        g)zFit the Vahadane stain normalizer to an image.

This function fits the Vahadane stain normalizer to an image.

Args:
    img (np.ndarray): Input image.

r  )rl  N)rI  rj  r  r  r1   rJ   rY  )rZ  r:   rp  nmfr   rq  r  r  s           rD   r_  VahadaneNormalizer.fit  s[     15s#++O< &;<%H"#%88-'$
 rF   rX  Nrb  )rc  rd  re  rf  rg  r_  rh  r   rF   rD   rS  rS    s    *X
rF   rS  c                  B   ^  \ rS rSrSrSSU 4S jjjrSSS jjrSrU =r$ )	rT  i!  z5Macenko stain normalizer with optimized computations.c                .   > [         TU ]  5         Xl        g r   )superr[  angular_percentile)rZ  r  	__class__s     rD   r[  MacenkoNormalizer.__init__$  s    "4rF   c           	     P   [        U5      nSnX4:  R                  SS9nX5   n[        U5      S:  a  [        SU S35      e[        R
                  " U[        R                  S9n[        R                  " US[        R                  [        R                  -  [        R                  -  5      S   n[        R                  " U5      SS u  p[        R                  " UR                  5       5      S	S n
[        R
                  " U	SS2U
4   [        R                  S9nS
n[        R                  " [        R                  " U5      U:  5      (       aI  [        R                   " [        R                  " U5      U:  [        R                   " US:  U* U5      U5      nXl-   nX-  n[        R"                  " USS2S4   USS2S4   5      n[        R$                  " USU-
  5      n[        R$                  " X5      n[        R&                  " U5      [        R(                  " U5      nn[        R&                  " U5      [        R(                  " U5      nn[        R*                  " UU/UU//[        R                  S9n[        R
                  " UR,                  [        R                  S9n[        R.                  " UUSSS5      n[        R                  " U5      nU[        R0                  " [        R2                  " US-  SSS9U-   5      -  nUS   US   :  a  UU l        gUSSS2   U l        g)zFit the Macenko stain normalizer to an image.

This function fits the Macenko stain normalizer to an image.

Args:
    img (np.ndarray): Input image.
    angular_percentile (float): Angular percentile.

g?rH   r   z"No tissue pixels found (threshold=)r'   Nr   r#  g:0yE>r  r$   TrK  r   )rH   r   rI   )rI  rr   rN   rz   r1   r   r   r,   calcCovarMatrixCOVAR_NORMAL
COVAR_ROWSCOVAR_SCALEeigenr  rf   r  rQ   ry  
percentilerI  rK  rJ   r   gemmrN  rh   rY  )rZ  r:   r  rp  od_thresholdthreshold_masktissue_densityod_covarianceeigenvalueseigenvectorsr  principal_eigenvectorsepsilonsafe_tissue_densityplane_coordinatespolar_angleshematoxylin_angleeosin_anglehem_coshem_sineos_coseos_sinangle_to_vectorprincipal_eigenvectors_tstain_vectorss                            rD   r_  MacenkoNormalizer.fit(  s    15 )8==1=E(8~"A,qQRR --nBJJO++s~~-?
 	 %(IIm$<QR$@!jj**,-bc2!#!5!5l1c66JRTR\R\!] 66"&&/07:;;%'XX-.8/!3gXwG&&" -6/H zzad#ad#
 MM,>P8PQmmLE 66"34bff=N6O66+.{0C((w'7!34**
 $&#7#78N8P8PXZXbXb#c $
 }- &}a7GaZ^0_bi0i(jj 5B$4G-X\J]4]= cpqusuqucv rF   )r  rY  )c   )r  r  )r:   r   r  r  r   ra  )	rc  rd  re  rf  rg  r[  r_  rh  __classcell__)r  s   @rD   rT  rT  !  s    ?5 5Uw UwrF   rT  c                `    U S   S-  U S   S-  -   U S   S-  -   nX!:  nUR                  S5      $ )zGet tissue mask from image.

Args:
    img (np.ndarray): Input image
    threshold (float): Threshold for tissue detection. Default: 0.85

Returns:
    np.ndarray: Binary mask where True indicates tissue regions

r   gA`"?r  gbX9?r  gv/?rI   )r  )r:   rR   
luminosityr^   s       rD   get_tissue_maskr    sG     Vu$s6{U'::S[5=PPJ !D<<rF   c                   [        U 5      n[        R                  " U[        R                  S9nSnXR                  -  U[        R
                  " S5      -  -   nXR                  -  n [        R                  R                  Xx5      R                  n	U(       d'  [        U 5      R                  S5      n
X   U-  U-   X'   OX-  U-   n	X-  n[        R                  " U* 5      nUR                  U R                  5      $ ! [        R                  R                   a9    [        R                  R                  UR                  UUS9S   R                  n	 Nf = f)a  Apply HE stain augmentation to an image.

This function applies HE stain augmentation to an image.

Args:
    img (np.ndarray): Input image.
    stain_matrix (np.ndarray): Stain matrix.
    scale_factors (np.ndarray): Scale factors.
    shift_values (np.ndarray): Shift values.
    augment_background (bool): Whether to augment the background.

Returns:
    np.ndarray: Augmented image.

r'   rL  r$   )rcondr   rI   )rI  r1   r   r   r   r  r  solveLinAlgErrorlstsqr  r  r  rM   )r:   stain_matrixscale_factorsshift_valuesaugment_backgroundrp  regularizationstain_correlationdensity_projectionrs  tissue_maskoptical_density_result
rgb_results                rD   apply_he_stain_augmentationr    sB   2 -S1O ''BJJGL N$~~5PQ8RR%(9(99	!yy/@UWW %c*2226,@,MP],]`l,l)  4ClR 2@//0Jcii((+ 99   !yyNN   /  
 	  Q	 	s   ()C< <AEEc                D    [        [        R                  SUS9nU" U 5      $ )zConvolve an image with a kernel.

This function convolves an image with a kernel.

Args:
    img (np.ndarray): Input image.
    kernel (np.ndarray): Kernel.

Returns:
    np.ndarray: Convolved image.

rI   )ddepthkernel)r   r,   r  r:   r  conv_fns      rD   r'  r'    s      &cll2fMG3<rF   c                D    [        [        R                  SXS9nU" U 5      $ )zConvolve an image with a separable kernel.

This function convolves an image with a separable kernel.

Args:
    img (np.ndarray): Input image.
    kernel (np.ndarray): Kernel.

Returns:
    np.ndarray: Convolved image.

rI   )r  kernelXkernelY)r   r,   sepFilter2Dr  s      rD   separable_convolver    s      &coob&aG3<rF   )
r:   r   r;   r  r<   r  r=   r  r   r   )r:   r   rR   r  r   r   )r:   r   r\   zALiteral[1, 2, 3, 4, 5, 6, 7] | list[Literal[1, 2, 3, 4, 5, 6, 7]]r   r   r   )r:   r   r^   np.ndarray | Noner   r   )r:   r   r^   r  r{   boolr   ra  )r^   r  rT   
int | Noner   r  )NcvT)
r:   r   r^   r  r   zLiteral['cv', 'pil']r{   r  r   r   )r:   r   r   r   r   r   r   r   )r:   r   r   r   r   r   )r:   r   r   r  r   tuple[int, int]r   r   )r:   r   r   rL   r   zLiteral['.jpg', '.webp']r   r   )r:   r   r   r  r   r  r   r   )r   r  r   np.random.Generatorr   rw  )r:   r   r   r  r   r  r   r   r   r   r   r   )r:   r   r  r  r  rL   r	  rL   r
  tuple[int, int, int]r  rL   r  r  r  r   r   r   )
r   r  r  rL   r  r  r   r  r   	list[int])r:   r   r  r  r&  r  r'  zlist[tuple[int, int]]r(  r  r   r   )r:   r   r4  tuple[float, float]r5  rL   r6  tuple[int, ...]r7  	list[Any]r   r   )r:   r   r4  r  r5  rL   r6  r  r7  r  r   r   )r:   r   rY  zlist[np.ndarray]rZ  r   r   r   )r:   r   rb  r  r   r   )r:   r   r   r   )r:   r   ro  r  r   r   )rv  r   ro  Sequence[int]r   r   )ry  r   ro  r  r   r   )r:   r   r|  r  r   r   )
r  r   r  r  r  r  r   r  r   r   )r:   r   r  rL   r  zPLiteral['weighted_average', 'from_lab', 'desaturation', 'average', 'max', 'pca']r   r   )r   )r  r   r  rL   r   r   )
r:   r   rv   r  r  rL   r  rL   r   r   )r  r   r  r   r   r   )r:   r   r  r   r   r   )r:   r   r  r   r   r   )r:   r   r  r  r   r   )r   )r:   r   r  r  r|  r  r   r   )r  r   r  rL   r  zSequence[bool]r  r  r  rL   r   r   )r  r   r  rL   r  r  r-  r  rR   rL   r   r   )r  r   r  r   r  r   r   r   )r:   r   r
  r   r   r   )r:   r   r  r   r  r   r   r   )r:   r   r  r  r  r  r  r  r  r  r  rL   r   r   )r)  r   r  r   r*  r   r  rL   r  rL   r  rL   r   r   )r:   r   r@  rL   r   zLiteral['blackbody', 'cied']r   r   )r:   r   r  r   r   r   )g      $@r1  )
r  r   r  rL   r  r  rQ  rL   r   r   )r:   r   rv   r  r   r  r   r   )r-  r  rl  r  r   r  r   r  )rw  1Literal['uniform', 'gaussian', 'laplace', 'beta']rx  z*Literal['constant', 'per_pixel', 'shared']rM   r  r  zdict[str, Any] | Noner   r  ry  r  r   r  r   r   )rw  r  rM   r  r  dict[str, Any]r   r  r   r  r   r   )rw  r  r   r  r  r  r   r  r   r  r   r   )r   r  r  r  r   r  r   znp.ndarray | float)r   r  r  r  r   r  r   r   )
r:   r   r-  r  r  rL   r  r  r   r   )r:   r   r  r   r  r   r   r   )r  r  r  r  r   r  r   r   )
r:   r   r  r  r  r  r  r   r   r   )r:   r   r  r  r  r   r   r   )r  rL   r  rL   rQ  r  r   r   )r:   r   r  r  rQ  r  r   r   )r:   r   r  r  r  zLiteral[0, 1, 2, 3]r   r   )
r:   r   r  r  r  r  r  r  r   r   )
r:   r   r  r  r  r  r  Literal['cdf', 'pil']r   r   )r  r   r  rL   r  rL   r   rL   r  r  r   r   )r  r   r  r  r   r  )
rM   r  r  r  r  r  r   r  r   r   )rS  rL   r(   znp.dtyper   r  r   r   )rJ   r   r  z+float | Sequence[float] | np.ndarray | Noner   r  r   r   )r  r  r   r  )r(  r   r!  r   r  r  r   r  )r(  r   r!  r   r2  r  r  r  r  r  r   r  r   r  )rL  )r:   r   rG  r  r   r   )rM  r   r   r   )r  zLiteral['vahadane', 'macenko']r   rP  )rq  r   r   r  )r   )r:   r   r  r   r  r   r  r   r  r  r   r   )r:   r   r  r   r   r   )rg  
__future__r   r  collections.abcr   typingr   r   warningsr   r,   numpyr1   albucorer   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   1albumentations.augmentations.geometric.functionalaugmentations	geometric
functionalr  "albumentations.augmentations.utilsr   r   $albumentations.core.type_definitionsr    r!   r"   rE   rW   ra   ro   rx   r}   r   r   r   r   r   r   r   r   r   r  r  r/  rB  rT  r`  rh  rk  rr  rw  rz  r~  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r=  __annotations__rH  rJ  r  rj  ro  r~  rs  ru  r  r  r  r  r  rt  r  r  rJ   r   r  r  INITIAL_GRID_SIZEr  r  r  r  r  r  r  r  r  r  r  r  r  r  r-  r6  STAIN_MATRICESrI  rO  rU  rP  rj  r  rS  rT  r  r  r'  r  r   rF   rD   <module>r     s   #  $   
      2 G F  
;E	;E;E ;E 	;E
 ;E  
;E| 	: 	:> 
1 	 
1h* *("	"
" " 
	"" 
 " 
 #!%	<	<
< < 	<
 <  
<~ 
-	-- - 	- 
-` 	5	5%5 5 	5( 
,4	,4,4 $,4 	,4  
,4^ 
,5	,5,5 ),5 	,5  
,5^ 
H6	H6H6 H6 	H6 
H6V&&)& #&0 
j	jj j 	j
 j j 
jZ 
<	<< < 	<
 %< < "< < <  
<~aaa a *	a
 a2 
/0	/0/0 /0 2	/0
 %/0 /0  	 
/0d 
\	\%\ \ 	\
 \ \   
\~ 
v>	v>!v> v> $	v>
 v> v> 	 
v>r 
+	+#+ + 	+  
+\ 
6  	 
600$,QT * .5.5.5 .5 *	.5
 .5 	 .5b1. 
8 	 
8D 	M 	M(3D! H 	'Y 	'YT0B	0B0B0B 0Bj  !777 7: 	S	SS S 	S
 S 
 SB" F/  	 F/R 0 0, I I6 	 `	`` ` 	`  	`8000 
MkMkMk $Mk 	Mk
 Mk Mk  
Mk` 111 1 	1
 1 1  	 1h 333 3 	3 3& )  	 )  2  	 2" 
=C	=C!=C $=C #	=C
  %=C =C =C 	 
=C@ " 	
   4'' 	' 	'	
 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	(  	(!" 	(#$ )(((((((36'' 	' 	'	
 	' 	' 	' 	' 	' 	' 	' 	' 	( 	( 	(  	(!" 	(#$ )(((((/957 3 5p 	;	;; '; 	; 	;| 	* 	*& 	CCC C 	C
 CL C	CC *C 	C  CB!!
! ! 	!@SUASU<SU SU "	SU
 SU SU *SU SUlA  	
 * @PAPP P 	P
 *P P2:A:
: : 	:
 *: :D!:
!:!: *!: 	!:H"3
"3"3 *"3 	"3JE
EE *E 	E.%
%% *% 	%6!A!! ! 	!
 *! !H 	)	)) ) 	)
 )  	)>I	II I 	I8 
 ** 
 **  >!>> *> 	>B 	-	-- - 	-
 -  	-` 	&	&& & 	& 	&63l , ,> 	/+	/+/+  /+ 	/+ 	/+d 	1%	1%1%  1% 	1%
 1% 	1%h 
;	;; ; "	;
 ; 
;|1
11 1 	1
 "1 1h7(t';';'; '; *	';
 ';T444 *4 	4:PP6P *P 	P@9KKK K 	K\KKK K 	K
 K *K K` xx**	
 xx$$	
 	
 XX	
 xx	
 xx	
 HH	
 XX	
W1h! !& Q" "$S2 S2l&DC
 C
L\w \w~( 	9)	9)9) 9) 	9)
 9) 9)  	9)x 	  	" 	  	rF   