
    +j{#                     z   S SK r S SKrS SKJr  S\R                  S\\\4   4S jr    S.S\R                  S\S-  S\S-  S\S-  S	\S-  4
S
 jjr  S/SS.S\R                  S\\	\	4   S-  S\\	\	4   S-  S\
S\	4
S jjjrSS.S\R                  S\R                  S\	S\	S\
S\	4S jjr  S/SS.S\S\\	\	4   S-  S\\	\	4   S-  S\
S\	4
S jjjr    S.S\R                  S\S-  S\S-  S\S-  S	\S-  4
S jjr  S/S\R                  S\	S-  S\	S-  4S jjr  S/S\R                  S\S-  S\S-  4S  jjrS! r  S/S"\R(                  S\	S-  S\	S-  SS4S# jjr    S0S$\4S% jjrS\R                  SS4S& jrS'\\	\	4   S\	4S( jrSSSS).S*\	S\\	\	4   S-  S\\	\	4   S-  S\
SS4
S+ jjrS,\S\	4S- jrg)1    N)Inputimagereturnc                     [        U R                  5      S:X  a  U R                  S   U R                  S   4$ [        U R                  5      S:X  a  U R                  S   U R                  S   4$ [        S5      e)N            r   zInvalid image tensor shape.)lenshape
ValueError)r   s    E/home/wildlama/comfy/ComfyUI/comfy_api_nodes/util/validation_utils.pyget_image_dimensionsr      sf    
5;;1{{1~u{{1~--	U[[	Q	{{1~u{{1~--677    	min_width	max_width
min_height
max_heightc                     [        U 5      u  pVUb  Xa:  a  [        SU SU S35      eUb  Xb:  a  [        SU SU S35      eUb  XS:  a  [        SU SU S35      eUb  XT:  a  [        SU SU S35      eg g )NzImage width must be at least px, got pxzImage width must be at most zImage height must be at least zImage height must be at most )r   r   )r   r   r   r   r   heightwidths          r   validate_image_dimensionsr      s     )/MF!288E7RTUVV!27	{(5'QSTUU&"59*XfXUWXYY&"58HVHTVWXX #6r   T)strict	min_ratio	max_ratior   c                v    [        U 5      u  pEUS::  d  US::  a  [        SU SU 35      eXE-  n[        XaX#S9  U$ )zfValidates that image aspect ratio is within min and max. If a bound is None, that side is not checked.r   zInvalid image dimensions: xr   r   r   )r   r   _assert_ratio_bounds)r   r   r   r   whars          r   validate_image_aspect_ratior%   $   sL      &DAAva5aS!=>>	
BIUIr   Ffirst_imagesecond_imagemin_relmax_relc                (   [        U 5      u  pV[        U5      u  px[        XVXx5      S::  a  [        S5      eXV-  n	Xx-  n
[        X5      [        X5      -  n[        USU-  5      nU(       a  X:  a"  OX:  a  [        SX-  S SU SU SUS S	3	5      eU$ )
z
Validates that the two images' aspect ratios are 'close'.
The closeness factor is C = max(ar1, ar2) / min(ar1, ar2)  (C >= 1).
We require C <= limit, where limit = max(max_rel, 1.0 / min_rel).

Returns the computed closeness factor C.
r   zInvalid image dimensions      ?z%Aspect ratios must be close: ar1/ar2=.2gz, allowed range u   –z (limit z).)r   minr   max)r&   r'   r(   r)   r   w1h1w2h2ar1ar2	closenesslimits                r   &validate_images_aspect_ratio_closenessr7   4   s     "+.FB!,/FB
22a344
'C
'CCC-Iw'E%	I,=3CGC= A$IS	%BH
 	
 r   aspect_ratioc                0    [        U 5      n[        XAX#S9  U$ )zQParses 'X:Y' and validates it against optional bounds. Returns the numeric ratio.r    )_parse_aspect_ratio_stringr!   )r8   r   r   r   r$   s        r   validate_aspect_ratio_stringr;   S   s     
$L	1BIUIr   videoc                 X    U R                  5       u  pVUb  XQ:  a  [	        SU SU S35      eUb  XR:  a  [	        SU SU S35      eUb  Xc:  a  [	        SU SU S35      eUb  Xd:  a  [	        SU SU S35      eg g ! [         a!  n[        R                  " SU5         S nAg S nAff = f)Nz%Error getting dimensions of video: %szVideo width must be at least r   r   zVideo width must be at most zVideo height must be at least zVideo height must be at most )get_dimensions	Exceptionloggingerrorr   )r<   r   r   r   r   r   r   es           r   validate_video_dimensionsrC   `   s    ,,.
 !288E7RTUVV!27	{(5'QSTUU&"59*XfXUWXYY&"58HVHTVWXX #6  =qAs   A> >
B)B$$B)min_durationmax_durationc                      U R                  5       nSnUb  X-
  U:  a  [	        SU SU S35      eUb  X2U-   :  a  [	        SU SU S35      eg g ! [         a!  n[        R                  " SU5         S nAg S nAff = f)Nz#Error getting duration of video: %sg-C6?z Video duration must be at least s, got szVideo duration must be at most )get_durationr?   r@   rA   r   )r<   rD   rE   durationrB   epsilons         r   validate_video_durationrL   w   s    
%%'
 GL$:X$E;L>QYPZZ[\]]Hg/E$E:<.PXzYZ[\\ %F  ;Q?s   A 
A;A66A;min_frame_countmax_frame_countc                      U R                  5       nUb  X:  a  [	        SU SU 35      eUb  X2:  a  [	        SU SU 35      eg g ! [         a!  n[        R                  " SU5         S nAg S nAff = f)Nz&Error getting frame count of video: %sz#Video frame count must be at least z, got z"Video frame count must be at most )get_frame_countr?   r@   rA   r   )r<   rM   rN   frame_countrB   s        r   validate_video_frame_countrR      s    
++-
 "'D>>OvVaUbcdd"{'D=o=NfU`Tabcc (E"  >Bs   A 
A1A,,A1c                     [        U [        R                  5      (       a!  U R                  S:  a  U R                  S   $ S$ [        U 5      $ )Nr   r   r   )
isinstancetorchTensorndimr   r   )imagess    r   get_number_of_imagesrY      s;    &%,,''"(++"2v||A99v;r   audioc                     [        U S   5      n[        U S   R                  S   5      U-  nSU-  nUb  XE-   U:  a  [        SU SXE-   S S35      eUb  XE-
  U:  a  [        S	U SXE-
  S S35      eg g )
Nsample_ratewaveformr+   z Audio duration must be at least rG   z.2frH   zAudio duration must be at most )intr   r   )rZ   rD   rE   srdurepss         r   validate_audio_durationrc      s    
 
U=!	"B
eJ%%b)
*R
/C
(CCI$<;L>QTQZ[^P__`abbCI$<:<.PSPYZ]^_`aa %=r   stringc           
      (   U c  [        SU S35      eU(       a  U R                  5       n U(       a-  [        U 5      U:  a  [        SU SU S[        U 5       S35      eU(       a.  [        U 5      U:  a  [        SU SU S[        U 5       S35      eg g )NzField 'z' cannot be empty.z' cannot be shorter than z characters; was z characters long.z Field 'z cannot be longer than )r?   stripr   )rd   strip_whitespace
field_name
min_length
max_lengths        r   validate_stringrk      s     ~'*-?@AAc&kJ.j\!::,FWX[\bXcWdduv
 	
 c&kJ.zl"9*EVWZ[aWbVcctu
 	
 /zr   c                 L    U R                  5       nUS;  a  [        SU 35      eg)z(Validates video container format is MP4.)mp4zmov,mp4,m4a,3gp,3g2,mj2z*Only MP4 container format supported. Got: N)get_container_formatr   )r<   container_formats     r    validate_container_format_is_mp4rp      s5    113AAEFVEWXYY Br   rc                 N    U u  pUS::  d  US::  a  [        SU SU S35      eX-  $ )Nr   zRatios must be positive, got :.)r   )rq   abs      r   _ratio_from_tuplerw      s8    DAAva81QCqABB5Lr   r    r$   c          	      >   Ub  [        U5      OSnUb  [        U5      OSnUb
  Ub  XE:  a  XTpTUb4  U(       a  X::  a(  OX:  a"  U(       a  SOSn[        SU S SU SUS S35      eUb6  U(       a  X:  a)  OX:  a"  U(       a  SOSn[        SU S SU SUS S35      eggg)	zFValidate a numeric aspect ratio against optional min/max ratio bounds.N<u   ≤zAspect ratio `r,   z
` must be  rt   )rw   r   )r$   r   r   r   lohiops          r   r!   r!      s     *3)>	9	%DB)2)>	9	%DB	~".RWB	~BHbgEB~bXZt1RHANOO	~BHbgEB~bXZt1RHANOO '.H r   ar_strc                 Z   U R                  S5      n[        U5      S:w  a  [        SU  S35      e [        US   R	                  5       5      n[        US   R	                  5       5      nUS::  d  US::  a  [        S	U SU S
35      eX#-  $ ! [         a  n[        SU  S35      UeSnAff = f)z?Parse 'X:Y' with integer parts into a positive float ratio X/Y.rs   r	   z.Aspect ratio must be 'X:Y' (e.g., 16:9), got 'z'.r   r   z:Aspect ratio must contain integers separated by ':', got 'Nz2Aspect ratio parts must be positive integers, got rt   )splitr   r   r_   rf   )r~   partsru   rv   excs        r   r:   r:      s    LLE
5zQI&QSTUUka !a ! 	AvaMaSPQRSQTTUVWW5L	  kUV\U]]_`agjjks   8B 
B*B%%B*)NNNN)NN)TpromptNN)r@   rU   comfy_api.latestr   rV   tupler_   r   r   floatboolr%   r7   strr;   VideorC   rL   rR   rY   Audiorc   rk   rp   rw   r!   r:    r   r   <module>r      so     "8 8sCx 8 ! !!Y<<YTzY TzY d
	Y
 d
Y* -1,0
 <<UE\"T) UE\"T)
  , ,,  	  B -1,0

 

UE\"T)
 UE\"T)

 
 
 ! !!Y;;YTzY TzY d
	Y
 d
Y2 "&!%];;]$,] $,]( #'"&d;;d4Zd 4Zd" "&!%b;;b$,b $,b 
	b  

*ZEKK ZD Zue|,   -1,0PP UE\"T)P UE\"T)	P
 P 
P0s u r   