
    hwt                    Z   S SK Jr  S SKJr  S SKJrJrJr  S SKrS SK	r
S SKrS SKrS SKJrJr  S SKJrJrJrJrJrJrJrJrJrJr  \
R:                  \
R<                  \
R>                  S.r \R:                  \R<                  \RB                  S.r"SES jr#SFS	 jr$SGS
 jr%SGS jr&        SHS jr'\SISJS jj5       r(          SKS jr)        SLS jr*        SMS jr+        SNS jr,        SOS jr-SPS jr.\SQS j5       r/SRS jr0\SSS j5       r1\STS j5       r2\SFS j5       r3SUSVS jjr\SUSPS jj5       r4SRS jr5SPS jr6\SUSSS jj5       r7\SWS j5       r8\SUSWS jj5       r9SUSVS  jjrSXS! jr:\SXS" j5       r;\SXS# j5       r<SYS$ jr=SZS% jr>\SGS& j5       r?SUS[S' jjr@\SUS\S( jj5       rSES) jrA\SES* j5       rB\ SU           S]S+ jj5       rC\SES, j5       rDS^S- jrE\S^S. j5       rFS_S/ jrG\SUS_S0 jj5       rH\S`S1 j5       rI\S`S2 j5       rJ\S`S3 j5       rKS`S4 jrLSaSbS5 jjrM\SaSbS6 jj5       rN\SaSbS7 jj5       rOSaSbS8 jjrPSaScS9 jjrQ\SaScS: jj5       rRSaScS; jjrS\SdS< j5       rT\SdS= j5       rUSdS> jrV\SdS? j5       rW\SdS@ j5       rXSdSA jrYSeSB jrZSfSC jr[SfSD jr\g)g    )annotationswraps)AnyCallableLiteralN)
contiguouspreserve_channel_dim)
MAX_OPENCV_WORKING_CHANNELSMAX_VALUES_BY_DTYPEMONO_CHANNEL_DIMENSIONSNormalizationType	ValueTypeclipclippedconvert_valueget_max_valueget_num_channels)multiplyaddpowerc           
     L   U R                   nU R                  nUR                  U:w  a  [        UR                  USS9USS9n[        R
                  " [        R                  " U R                  S5      UR                  USS9R                  S5      XS9US9R                  U5      $ )NFcopyTinplace)alphabetadtype)	shaper!   r   astypenp
frombuffersswsumreshape)img1weight1img2weight2original_shapeoriginal_dtypes         L/var/www/fran/franai/venv/lib/python3.13/site-packages/albucore/functions.pyadd_weighted_simsimdr0      s    ZZNZZNzz^#DKKUK;^UYZ==
R $++n5+"I"Q"QRT"U]ds g    c                    [        U SUS5      $ N   )r0   imgvalues     r/   add_array_simsimdr8   -   s    Qq11r1   c                D    [        X[        R                  " U 5      S5      $ Nr   )r0   r$   
zeros_liker5   s     r/   multiply_by_constant_simsimdr<   1   s    BMM#,>BBr1   c           	     |    [        U S[        R                  " U 5      U-  R                  U R                  SS9S5      $ )Nr4   Fr   )r0   r$   	ones_liker#   r!   r5   s     r/   add_constant_simsimdr?   5   s7    Qc):U)B(J(J399[`(J(acdeer1   c                v   [         U    nU [        R                  :X  a  US:X  a  [        R                  " U5      n[        R                  " U[        R
                  S9R                  SS5      n[        R                  " SUS-   [        R
                  S9nU[        ;   a  [        U   " XA5      $ [        SU 35      e)Nr   r    r   r4   r   zUnsupported operation: )
r   r$   uint8truncarrayfloat32r(   arangenp_operations
ValueError)r!   r7   	operation	max_valueluts        r/   create_lut_arrayrK   9   s    
 $E*IY%/HHU"**-55b!<E
))Ay1}BJJ
7CM!Y'33
.yk:
;;r1   c                    U(       d  U R                  5       n [        R                  " [        U 5      [        U5      SS9  U $ NTr   )r   sz	translate
memoryview)r6   rJ   r   s      r/   sz_lutrQ   L   s-    hhjLLC*S/4@Jr1   c                r   U R                   n[        U[        [        45      (       a!  [	        XAU5      n[        U [        XTSS9S5      $ U R                  S   n[        [	        XAU5      USS9n[        R                  " [        U5       Vs/ s H  n[        U S S 2S S 2U4   Xx   U5      PM     sn5      $ s  snf )NFr   r   )r!   
isinstanceintfloatrK   rQ   r   r"   cv2mergerange)	r6   r7   rH   r   r!   rJ   num_channelslutsis	            r/   	apply_lutr\   U   s     IIE%#u&&uY7c4E:EBB99R=L y95%PD99eLFYZFYfSAq\47G<FYZ[[Zs   
#B4c                h    [        U[        [        45      (       a  [        XU5      $ [	        XU5      $ N)rS   rT   rU   _prepare_scalar_value_prepare_array_valuer6   r7   rH   s      r/   prepare_value_opencvrb   g   s7     ec5\** 	c)4 "#i8r1   c                   US:X  a)  U R                   [        R                  :X  a  [        U5      n[	        U 5      nU[
        :  a{  US:X  aA  US:  a  [        R                  OU R                   n[        R                  " U R                  XS9$ US:X  a.  [        R                  " U R                  U[        R                  S9$ U$ )Nr   r   r    r   )	r!   r$   rA   rT   r   r   rD   fullr"   )r6   r7   rH   rY   	cast_types        r/   r_   r_   s   s    
 Ecii2883E
#C(L11&+ai

SYYI77399e==
"77399e2::>>Lr1   c                   UR                   [        R                  :X  a  UR                  [        R                  SS9nUR
                  S:X  a  UR                  SSS5      n[        R                  " XR                  5      nUS:X  a  U R                   [        R                  :X  ai  [        R                  " US:  5      (       a  [        U[        R                  SS9$ [        R                  " U5      R                  [        R                  SS9$ U$ )NFr   r4   r   r   r   r   )r!   r$   float64r#   rD   ndimr(   broadcast_tor"   rA   allr   rB   ra   s      r/   r`   r`      s    
 {{bjj RZZe4zzQaB'OOE99-EEcii288366%1*rxx77xx%%bjju%==Lr1   c                    US:X  a4  U R                   [        R                  :X  a  [        R                  " U5      n[        U   " U R                  [        R                  SS9U5      $ )Nr   Fr   )r!   r$   rA   int16rF   r#   rD   ra   s      r/   apply_numpyrm      sK    
 Ecii2883#CJJrzzJ$FNNr1   c                    [        XSU5      $ Nr   r\   r6   r7   r   s      r/   multiply_lutrr      s    SW55r1   c                    [        XS5      nU R                  [        R                  :X  a3  [        R
                  " U R                  [        R                  SS9U5      $ [        R
                  " X5      $ )Nr   Fr   )rb   r!   r$   rA   rV   r   r#   rD   r5   s     r/   multiply_opencvrt      sO     Z8E
yyBHH||CJJrzzJ>FF<<##r1   c                    [        XS5      $ ro   rm   r5   s     r/   multiply_numpyrw      s    s:..r1   c                    U R                   [        R                  :X  a  [        XU5      $ U R                   [        R                  :X  a  [        X5      $ [        X5      $ r^   )r!   r$   rA   rr   rD   rw   rt   rq   s      r/   multiply_by_constantry      sE    
yyBHHC00
yyBJJc))3&&r1   c                    U R                   [        R                  :X  a  [        XU5      $ U[        :  a  [        X5      $ [        X5      $ r^   )r!   r$   rA   rr   r   rw   rt   )r6   r7   rY   r   s       r/   multiply_by_vectorr{      s@     yyBHHC0011c))3&&r1   c                    [        X5      $ r^   )rt   r5   s     r/   multiply_by_arrayr}      s    3&&r1   c                   [        U 5      n[        X5      n[        U[        [        45      (       a  [        XU5      $ [        U[        R                  5      (       a  UR                  S:X  a  [        XX25      $ [        X5      $ r3   )r   r   rS   rU   rT   ry   r$   ndarrayrh   r{   r}   r6   r7   r   rY   s       r/   r   r      sg    #C(L%.E%%&&#C88%$$q!#lDDS((r1   c                ^   [        XS5      nU R                  [        R                  :H  =(       ag    [	        U[
        [        45      =(       a    US:  =(       d>    [	        U[        R                  5      =(       a    UR                  [        R                  :g  nU(       ap  [        R                  " U R                  [        R                  SS9[	        U[
        [        45      (       a  U5      $ UR                  [        R                  SS95      $ U(       a  U OS n[        R                  " XUS9$ )Nr   r   Fr   )dst)rb   r!   r$   rA   rS   rT   rU   r   rV   r   r#   rD   )r6   r7   r   needs_floatr   s        r/   
add_opencvr      s     U3E
 ))rxx' 	EC<	(	6UQYvJubjj<Y<u^c^i^imomumu^u  wwJJrzzJ.U|44E
 	
:?,,rzzX],:^
 	
 #dC7733''r1   c                    [        XS5      $ Nr   rv   r5   s     r/   	add_numpyr      s    s5))r1   c                    [        XSU5      $ r   rp   rq   s      r/   add_lutr      s    S00r1   c                    [        XU5      $ r^   r   rq   s      r/   add_constantr          c'**r1   c                n    U R                   [        R                  :X  a  [        XU5      $ [	        XU5      $ r^   )r!   r$   rA   r   r   rq   s      r/   
add_vectorr      s,    
yyBHHs7++c'**r1   c                    [        XU5      $ r^   r   rq   s      r/   	add_arrayr     r   r1   c                .   [        U 5      n[        X5      n[        U[        [        45      (       a=  US:X  a  U $ U R
                  [        R                  :X  a  [	        U5      n[        XU5      $ UR                  S:X  a  [        XU5      $ [        XU5      $ )Nr   r4   )r   r   rS   rU   rT   r!   r$   rA   r   rh   r   r   r   s       r/   r   r     s}    #C(L%.E%%&&A:J99 JEC00.3jjAo:c'*a9SY`Caar1   c                N    U R                  [        R                  SS9n X-  n X-  $ NFr   r#   r$   rD   )r6   meandenominators      r/   normalize_numpyr     s(    
**RZZe*
,CKCr1   c                   U R                  [        R                  SS9n [        R                  " U [        R                  S9n[        R                  " U [        R                  S9n[	        U[
        [        45      (       a.  [        R                  " U R                  U[        R                  S9n[	        U[
        [        45      (       a.  [        R                  " U R                  U[        R                  S9nX1-   R                  [        R                  SS9nXB-   n[        R                  " X5      n[        R                  " XT[        R                  S9$ )NFr   r    )r#   r$   rD   r;   rS   rU   rT   rd   r"   rV   subtractr   CV_32F)r6   r   r   mean_imgdenominator_imgresults         r/   normalize_opencvr     s    
**RZZe*
,C}}S

3HmmCrzz:O $%%wwsyy$bjj9+s|,,ggciiBJJG ''

'?H%3O\\#(F<<szzBBr1   c                   U R                   n[        U   n[        U 5      n[        U[        [
        45      (       a^  [        U[        [
        45      (       aC  [        R                  " SUS-   [        R                  S9U-
  U-  n[        R                  " X5      $ [        U[        R                  5      (       a"  UR                  S:w  a  UR                  SS5      n[        U[        R                  5      (       a  UR                  SS5      n[        R                  " SUS-   [        R                  S9U-
  U-  n[        R                  " [        U5       Vs/ s H&  n[        R                  " U S S 2S S 2U4   Xx   5      PM(     sn5      $ s  snf )Nr   r4   r     r   )r!   r   r   rS   rU   rT   r$   rE   rD   rV   LUTr   r"   r(   rW   rX   )	r6   r   r   r!   rI   rY   rJ   rZ   r[   s	            r/   normalize_lutr   0  s'   IIE#E*I#C(L+s|,,D5#,1O1OyyIM<tC{Rwws  +rzz**{/@/@B/F!))"a0$

##||B"IIaQbjj9D@KOD99eL>QR>Qcggc!Q'lDG4>QRSSRs   -F
c                    [        U 5      n[        X#5      n[        X5      nU R                  [        R                  :X  a  [        XU5      $ [        XU5      $ r^   )r   r   r!   r$   rA   r   r   )r6   r   r   rY   s       r/   	normalizer   E  sJ    #C(L:K,D
yyBHHS44C{33r1   c                    [        XS5      $ Nr   rv   )r6   exponents     r/   power_numpyr   O  s    sg..r1   c                   U R                   [        R                  :X  a  [        R                  " X5      $ U R                   [        R
                  :X  a%  [        U5      U:X  a  [        R                  " X5      $ U R                   [        R
                  :X  aI  [        U[        5      (       a4  U R                  [        R                  SS9n[        R                  " X!5      $ [        SU R                    SU 35      e)z(Handle the 'power' operation for OpenCV.Fr   zUnsupported image type z  for power operation with value )r!   r$   rD   rV   powrA   rT   rS   rU   r#   rG   )r6   r7   	img_floats      r/   power_opencvr   S  s     yyBJJwws""
yyBHHUu!4wws""
yyBHHE5!9!9JJrzzJ6	wwy((
.syyk9YZ_Y`a
bbr1   c                    [        XSU5      $ r   rp   )r6   r   r   s      r/   	power_lutr   e  s    SGW55r1   c                    [        U 5      n[        X5      nU R                  [        R                  :X  a  [        XU5      $ [        U[        [        45      (       a  [        X5      $ [        X5      $ r^   )r   r   r!   r$   rA   r   rS   rU   rT   r   r   )r6   r   r   rY   s       r/   r   r   i  sZ    #C(LX4H
yyBHH00(UCL))C**s%%r1   c                    U R                  [        R                  SS9U-  UR                  [        R                  SS9U-  -   $ r   r   r)   r*   r+   r,   s       r/   add_weighted_numpyr   v  s;    ;;rzz;.84;;rzzX];;^ah;hhhr1   c                2    [         R                  " XX#S5      $ r:   )rV   addWeightedr   s       r/   add_weighted_opencvr   z  s    ??4$;;r1   c                   U R                   n[        U   nUS:X  a  US:X  a  U $ US:X  a  US:X  a  U$ US:X  a  US:X  a  [        R                  " U 5      $ US:X  a  US:X  a  [	        XU5      $ [        R
                  " SUS-   [        R                  S9U-  n[        R                  " X5      n[        R
                  " SUS-   [        R                  S9U-  n	[        R                  " X)5      n
[        XU5      $ )Nr4   r   r    )
r!   r   r$   r;   r   rE   rD   rV   r   r   )r)   r*   r+   r,   r   r!   rI   lut1result1lut2result2s              r/   add_weighted_lutr     s     JJE#E*I!|1!|1!|1}}T""!|1W--99Q	ARZZ87BDggd!G99Q	ARZZ87BDggd!Gg00r1   c                    U R                   UR                   :w  a&  [        SU R                    SUR                    S35      e[        XX#5      $ )Nz/The input images must have the same shape. Got z and .)r"   rG   r0   r   s       r/   add_weightedr     sH    zzTZZJ4::,V[\`\f\f[gghijjt==r1   c                b   [        U[        [        45      (       aE  US:X  a?  [        U[        [        45      (       a$  US:X  a  [        R                  " X R
                  S9$ US:w  a  [        R                  " X5      O[        R                  " U 5      nUS:X  a  U$ [        R                  " X25      $ )Nr   r    )rS   rT   rU   r$   r;   r!   r   r   r6   factorr7   r   s       r/   multiply_add_numpyr     s    %#u&&5A:*VcSX\:Z:Z_eij_j}}S		22)/1R[[%"--:LFaZ6:RVVF%::r1   c                2   [        U[        [        45      (       a=  US:X  a7  [        U[        [        45      (       a  US:X  a  [        R                  " U 5      $ U R                  [        R                  SS9nUS:w  a;  [        R                  " U[        R                  " U5      U-  [        R                  S9O[        R                  " X0R                  S9nUS:X  a  U$ [        R                  " U[        R                  " U5      U-  [        R                  S9$ )Nr   Fr   r    )rS   rT   rU   r$   r;   r#   rD   rV   r   r>   CV_64Fr!   r   r   s       r/   multiply_add_opencvr     s    %#u&&5A:*VcSX\:Z:Z_eij_j}}S!!ZZ

Z/F Q; 	VR\\&1F:#**M]]63 
 aZ6dSWWVR\\&5IE5QY\YcYc%ddr1   c                :   U R                   n[        U   n[        U 5      n[        U[        [
        45      (       a]  [        U[        [
        45      (       aB  [        [        R                  " SUS-   [        R                  S9U-  U-   USS9n[        XU5      $ [        U[        R                  5      (       a"  UR                  S:w  a  UR                  SS5      n[        U[        R                  5      (       a"  UR                  S:w  a  UR                  SS5      n[        [        R                  " SUS-   [        R                  S9U-  U-   USS9n[        R                  " [!        U5       V	s/ s H  n	[        U S S 2S S 2U	4   X   U5      PM     sn	5      $ s  sn	f )	Nr   r4   r    Fr   r   r   T)r!   r   r   rS   rU   rT   r   r$   rE   rD   rQ   r   r"   r(   rV   rW   rX   )
r6   r   r7   r   r!   rI   rY   rJ   rZ   r[   s
             r/   multiply_add_lutr     s@   IIE#E*I#C(L&5#,''Juucl,K,K299Q	ARZZ@6IEQSXbghc((&"**%%&,,"*<A&%$$):b!$		!Y]"**=FNPU_cdD99eLFYZFYfSAq\47G<FYZ[[Zs   .#Fc                    [        U 5      n[        X5      n[        X$5      nU R                  [        R                  :X  a  [        XX#5      $ [        XU5      $ r^   )r   r   r!   r$   rA   r   r   )r6   r   r7   r   rY   s        r/   multiply_addr     sJ    #C(L60F%.E
yyBHHU<<sE22r1   c           	     T   U R                  [        R                  SS9n SnU R                  [        :X  a  [        R
                  " U SS9n US:X  d  U R                  S   S:X  a  US:X  a  U R                  5       R                  5       nU R                  5       R                  5       U-   nU R                  S   [        :  a,  [        R                  " X5      n[        R                  " X5      n[        R                  " [        R                  " X5      U5      n[        R                  " US	S
US9$ US:X  a  [        R                   " U 5      u  p4US S 2S4   nUS S 2S4   nU R                  S   [        :  a,  [        R                  " X5      n[        R                  " X5      n[        R                  " [        R                  " X5      U[        R"                  S9n[        R                  " US	S
US9$ US:X  d  U R                  S   S:X  a[  US:X  aU  U R%                  5       nU R'                  5       n[        R(                  " U S SS[        R*                  [        R"                  S9$ US:X  a  U R%                  SS9nU R'                  SS9nU R                  S   [        :  a,  [        R                  " X5      n[        R                  " X5      n[        R                  " [        R                  " [        R                  " X5      Xv-
  U-   [        R"                  S9S	S
U S9$ [-        SU 35      e)NFr   -C6?r   axisimager4   image_per_channel   outr   r    min_maxmin_max_per_channel)r   r   	norm_typer!   r   r4   Unknown normalization method: )r#   r$   rD   rh   r   expand_dimsr"   r   itemstdr   	full_likerV   divider   r   
meanStdDevr   minmaxr   NORM_MINMAXrG   )r6   normalizationepsr   r   normalized_imgimg_minimg_maxs           r/   normalize_per_image_opencvr     sw   
**RZZe*
,C
C
xx**nnSr*CIIbMQ$6=L_;_xxz gginn$99R=66<<*D,,s(CCLL$;SAww~sBNCC++NN3'	AqDz!Q$i99R=66<<*D,,s(CCLL$;S

Sww~sBNCC	!ciimq&8]Nc=c'')'')}}S$aa3??Z]ZdZdee--''v'&''v'&99R=66ll30Gll30GwwJJs||C1G4E4KTWT^T^_	
 	
 5m_E
FFr1   c                   U R                  [        R                  SS9n SnU R                  [        :X  a  [        R
                  " U SS9n US:X  aA  U R                  5       nU R                  5       U-   nX-
  U-  n[        R                  " USSUS	9$ US
:X  a?  U R                  SS9nU R                  SS9U-   nX-
  U-  n[        R                  " USSUS	9$ US:X  aA  U R                  5       nU R                  5       n	[        R                  " X-
  X-
  U-   -  SSU S	9$ US:X  a?  U R                  SS9nU R                  SS9n	[        R                  " X-
  X-
  U-   -  SSU S	9$ [        SU 35      e)NFr   r   r   r   r   r   r   r   r   r   r   r   r   )r#   r$   rD   rh   r   r   r   r   r   r   r   rG   )
r6   r   r   r   r   r   
pixel_mean	pixel_stdr   r   s
             r/   normalize_per_image_numpyr     sq   
**RZZe*
,C
C
xx**nnSr*xxzggi#o*+ww~sBNCC++XX6X*
GGG(3.	*i7ww~sBNCC	!'')'')ww'*;c*ABCQTUU--''v'&''v'&ww'*;c*ABCQTUU
5m_E
FFr1   c                f   U R                   n[        U   nSn[        U 5      nU R                  [        :X  a  [
        R                  " U SS9n US:X  d  U R                  S   S:X  a|  US:X  av  U R                  5       nU R                  5       U-   n[
        R                  " SUS-   [
        R                  S9U-
  U-  n[        R                  " X5      R                  S	S
5      $ US:X  a  U R                  SS9n	U R                  SS9U-   n
[        U5       Vs/ s H4  n[
        R                  " SUS-   [
        R                  S9X   -
  X   -  PM6     nn[        R                   " [        U5       Vs/ s H6  n[        R                  " U S S 2S S 2U4   X   5      R                  S	S
5      PM8     sn5      $ US:X  d  U R                  S   S:X  a~  US:X  ax  U R#                  5       nU R%                  5       n[
        R                  " SUS-   [
        R                  S9U-
  X-
  U-   -  n[        R                  " X5      R                  S	S
5      $ US:X  a  U R#                  SS9nU R%                  SS9n[        U5       Vs/ s H<  n[
        R                  " SUS-   [
        R                  S9X   -
  X   X   -
  U-   -  PM>     nn[        R                   " [        U5       Vs/ s H&  n[        R                  " U S S 2S S 2U4   X   5      PM(     sn5      $ ['        SU 35      es  snf s  snf s  snf s  snf )Nr   r   r   r   r4   r   r   r    r   r   r   r   r   r   )r!   r   r   rh   r   r$   r   r"   r   r   rE   rD   rV   r   r   rX   rW   r   r   rG   )r6   r   r!   rI   r   rY   r   r   rJ   r   r   crZ   r[   r   r   s                   r/   normalize_per_image_lutr   3  s   IIE#E*I
C#C(L
xx**nnSr*CIIbMQ$6=L_;_xxzggi#oyyIM<tCsJwws %%c2..++XX6X*
GGG(3.	dijvdw
dw_`RYYq)a-rzz:Z]JilZdw 	 
 yyPUVbPcdPc1#''#aAg,8==c2FPcdee	!ciimq&8]Nc=c'')'')yyIM<wF7K\_bKbcwws %%c2..--''v'&''v'& <(
( YYq)a-rzz:WZGGJY`YcLcfiLij( 	 

 yy%BUVBUQ#''#aAg,8BUVWW
5m_E
FF+
 e

 Ws   ;L/=L$;AL)-L.c                v    U R                   [        R                  :X  a  US:w  a  [        X5      $ [	        X5      $ )Nper_image_per_channel)r!   r$   rA   r   r   )r6   r   s     r/   normalize_per_imager   ^  s0    
yyBHH2I!I&s::%c99r1   c                r    Uc  [        U R                  5      nX-  R                  [        R                  SS9$ r   )r   r!   r#   r$   rD   r6   rI   s     r/   to_float_numpyr   e  s2    !#)),	O##BJJU#;;r1   c                   Uc  [        U R                  5      nU R                  [        R                  SS9n[        U 5      nU[        :  a,  [        R                  " X!5      n[        R                  " X$5      $ [        R                  " X!5      $ r   )
r   r!   r#   r$   rD   r   r   r   rV   r   )r6   rI   r   rY   max_value_arrays        r/   to_float_opencvr   k  sm    !#)),	

2::E
2I#C(L11,,y<zz)55 ::i++r1   c                    U R                   [        R                  :w  a  [        S5      eUc  [        U R                      n[        R
                  " S[        R                  S9U-  n[        R                  " X5      $ )Nz.LUT method is only applicable for uint8 images   r    )	r!   r$   rA   rG   r   rE   rD   rV   r   )r6   rI   rJ   s      r/   to_float_lutr   }  sZ    
yyBHHIJJ'		2	
))Crzz
*Y
6C773r1   c                "   U R                   [        R                  :X  a  U R                  [        R                  SS9$ U R                   [        R                  :X  a  U $ U R                   [        R
                  :X  a  [        X5      $ [        X5      $ r   )r!   r$   rg   r#   rD   rA   r   r   r   s     r/   to_floatr     sd    
yyBJJzz"**5z11
yyBJJ

yyBHHC++#))r1   c                `    Uc  [        U5      n[        [        R                  " X-  5      USS9$ rM   )r   r   r$   rintr6   target_dtyperI   s      r/   from_float_numpyr    s,    !,/	(,EEr1   c                X   Uc  [        U5      nU R                  [        R                  SS9n[	        U 5      nU[
        :  aI  [        R                  " X25      n[        [        R                  " [        R                  " X55      5      USS9$ [        [        R                  " X-  5      USS9$ )NFr   r   )r   r#   r$   rD   r   r   r   r   r   rV   r   )r6   r  rI   r   rY   r   s         r/   from_float_opencvr    s    !,/	

2::E
2I#C(L11,,y<BGGCLLDE|]bcc (,FFr1   c                    U[         R                  :X  a  U $ U[         R                  :X  a  U R                  [         R                  SS9$ U R                  [         R                  :X  a  [        XU5      $ [        XU5      $ )a  Convert a floating-point image to the specified target data type.

This function converts an input floating-point image to the specified target data type,
scaling the values appropriately based on the max_value parameter or the maximum value
of the target data type.

Args:
    img (np.ndarray): Input floating-point image array.
    target_dtype (np.dtype): Target numpy data type for the output image.
    max_value (float | None, optional): Maximum value to use for scaling. If None,
        the maximum value of the target data type will be used. Defaults to None.

Returns:
    np.ndarray: Image converted to the target data type.

Notes:
    - If the input image is of type float32, the function uses OpenCV for faster processing.
    - For other input types, it falls back to a numpy-based implementation.
    - The function clips values to ensure they fit within the range of the target data type.
Fr   )r$   rD   rg   r#   r!   r  r  r   s      r/   
from_floatr    sb    * rzz!
rzz!zz"**5z11
yyBJJ I>>Cy99r1   c                    U S S 2S S S2S4   $ Nr   .r   r6   s    r/   hflip_numpyr
    s    q$B$|r1   c                    U R                   S:  a  U R                  S   S:  a
  [        U SS9$ [        R                  " U S5      $ )N      r4   	flip_coderh   r"   _flip_multichannelrV   flipr	  s    r/   	hflip_cv2r    <     xx!|		!s*!#3388Cr1   c                    [        U 5      $ r^   )r  r	  s    r/   hflipr        S>r1   c                    U R                   S:  a  U R                  S   S:  a
  [        U SS9$ [        R                  " U S5      $ )Nr  r  r   r  r  r	  s    r/   	vflip_cv2r    r  r1   c                    U S S S2S4   $ r  r   r	  s    r/   vflip_numpyr    s    ttSy>r1   c                    [        U 5      $ r^   )r  r	  s    r/   vflipr    r  r1   c                   U R                   SS u  p#U R                  S:X  a  SOU R                   S   nU R                  S:X  d  US::  a  [        R                  " X5      $ Sn/ n[	        SXE5       Hz  n[        Xu-   U5      nU SS2SS2Xx24   n	[        R                  " X5      n
U
R                  S:X  a%  U R                  S:X  a  [        R                  " U
SS9n
UR                  U
5        M|     [        R                  " USS9$ )a  Process images with more than 512 channels by splitting into chunks.

OpenCV's flip function has a limitation where it can only handle images with up to 512 channels.
This function works around that limitation by splitting the image into chunks of 512 channels,
flipping each chunk separately, and then concatenating the results.

Args:
    img: Input image with many channels
    flip_code: OpenCV flip code (0 for vertical, 1 for horizontal, -1 for both)

Returns:
    Flipped image with all channels preserved
Nr  r4   r  r      r   )
r"   rh   rV   r  rX   r   r$   r   appendconcatenate)r6   r  heightwidthrY   
chunk_sizeresult_chunksr[   end_idxchunkflipped_chunks              r/   r  r    s     IIbqMMFA1399Q<L xx1}+xx'' JM1l/anl3Aq!)O$2 "sxx1}NN=qAM]+ 0 >>-a00r1   c                4   ^  [        T 5      SU 4S jj5       nU$ )aB  Decorator to ensure float32 input/output for image processing functions.

This decorator converts the input image to float32 before passing it to the wrapped function,
and then converts the result back to the original dtype if it wasn't float32.

Args:
    func (Callable[..., np.ndarray]): The image processing function to be wrapped.

Returns:
    Callable[..., np.ndarray]: A wrapped function that handles float32 conversion.

Example:
    @float32_io
    def some_image_function(img: np.ndarray) -> np.ndarray:
        # Function implementation
        return processed_img
c                   > U R                   nU[        R                  :w  a  [        U 5      n T" U /UQ70 UD6nU[        R                  :w  a	  [	        XCS9$ U$ N)r  )r!   r$   rD   r   r  r6   argskwargsinput_dtyper   funcs        r/   float32_wrapper#float32_io.<locals>.float32_wrapper*  sQ    ii"**$3-Cc+D+F+?Jbjj?Xz&;d^ddr1   r6   
np.ndarrayr-  r   r.  r   returnr4  r   )r0  r1  s   ` r/   
float32_ior6    s%    & 4[e e r1   c                4   ^  [        T 5      SU 4S jj5       nU$ )a8  Decorator to ensure uint8 input/output for image processing functions.

This decorator converts the input image to uint8 before passing it to the wrapped function,
and then converts the result back to the original dtype if it wasn't uint8.

Args:
    func (Callable[..., np.ndarray]): The image processing function to be wrapped.

Returns:
    Callable[..., np.ndarray]: A wrapped function that handles uint8 conversion.

Example:
    @uint8_io
    def some_image_function(img: np.ndarray) -> np.ndarray:
        # Function implementation
        return processed_img
c                   > U R                   nU[        R                  :w  a  [        U [        R                  S9n T" U /UQ70 UD6nU[        R                  :w  a  [	        U5      $ U$ r+  )r!   r$   rA   r  r   r,  s        r/   uint8_wrapperuint8_io.<locals>.uint8_wrapperI  sW    ii"(("Srxx8Cc+D+F+#."((#:xFFr1   r3  r   )r0  r9  s   ` r/   uint8_ior;  6  s%    & 4[G G r1   )
r)   r4  r*   rU   r+   r4  r,   rU   r5  r4  )r6   r4  r7   r4  r5  r4  )r6   r4  r7   rU   r5  r4  )r!   ztype[np.number]r7   float | np.ndarrayrH   #Literal['add', 'multiply', 'power']r5  r4  )T)r6   r4  rJ   r4  r   boolr5  r4  )
r6   r4  r7   r<  rH   r=  r   r>  r5  r4  )r6   r4  r7   np.ndarray | floatrH   Literal['add', 'multiply']r5  r4  )r6   r4  r7   rU   rH   r@  r5  r?  )r6   r4  r7   r4  rH   r@  r5  r4  )r6   r4  r7   r<  rH   r=  r5  r4  )r6   r4  r7   r?  r   r>  r5  r4  )r6   r4  r7   r?  r5  r4  )r6   r4  r7   r<  r5  r4  )r6   r4  r7   rU   r   r>  r5  r4  )
r6   r4  r7   r4  rY   rT   r   r>  r5  r4  )F)r6   r4  r7   r   r   r>  r5  r4  )r6   r4  r7   r4  r   r>  r5  r4  )r6   r4  r   r<  r   r<  r5  r4  )r6   r4  r   r   r   r   r5  r4  )r6   r4  r   r<  r5  r4  )r6   r4  r   r<  r   r>  r5  r4  )r6   r4  r   r   r   r>  r5  r4  )r)   r4  r*   rU   r+   r4  r,   rU   r   r>  r5  r4  )r6   r4  r   r   r7   r   r5  r4  )
r6   r4  r   r   r7   r   r   r>  r5  r4  )r6   r4  r   r   r5  r4  r^   )r6   r4  rI   float | Noner5  r4  )r6   r4  r  znp.dtyperI   rA  r5  r4  )r6   r4  r5  r4  )r6   r4  r  rT   r5  r4  )r0  Callable[..., np.ndarray]r5  rB  )]
__future__r   	functoolsr   typingr   r   r   rV   numpyr$   simsimdr&   stringzillarN   albucore.decoratorsr	   r
   albucore.utilsr   r   r   r   r   r   r   r   r   r   r   r   r   rF   r   cv2_operationsr0   r8   r<   r?   rK   rQ   r\   rb   r_   r`   rm   rr   rt   rw   ry   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  r  r  r6  r;  r   r1   r/   <module>rL     s{   "  ) ) 
    @    [["((K!ll377SWWM2Cf<<< 3< 	<&  \	\\ 3\ 	\
 \$				 *	 			 * 	$	 * 	"O	OO 3O 	O6 $ $/ 	' 	' 	' 	' 	' 	'
) ( (**1 	+ 	+ 	+ 	+ 	+ 	+b  C C& T T(4/ c c"6 		& 		&i < <  1
11 1 	1
 1 1 1> 	> 	>; 
e 
e\( 	3 	3 0G 0Gf G G@ 'G 'GT:< , ,"  *F G G":B        '1T>r1   