
    
3j|                     b    S SK Jr  S SKrS SKJr  S SKJr  SSKJr  \" SS9 " S	 S5      5       r	g)
    )AnyN)is_device_cpu)GrowMask   )
comfy_nodeLTXVPreprocessMasks)namec                       \ rS rSrSr\SS S\\\4   4S j5       r	Sr
SrSrS	rS
\R                  S\S\S\S\S\S\S\S\\R                     4S jrSrg)r   
   a  
Preprocesses masks for use with the LTXVideo model's latent masking.

This node handles temporal mask processing by:
1. Validating mask dimensions against VAE downscaling factors
2. Optionally inverting masks
3. Handling the first frame mask separately (can be ignored)
4. Combining temporal masks using max pooling
5. Growing/shrinking masks with morphological operations
6. Clamping mask values to ensure proper opacity

The output is a set of masks ready for latent-space masking operations.
clsreturnc                 z    SSSSSSS.4SSS	S.4/ S
QSSS.4SSSSSSS.4SSSS.4SSSSSSS.4SSSSSSS.4S.	0$ )NrequiredMASK)VAEBOOLEANFzTInvert the input masks before processing.Useful for masking vs unmasking operations.)defaulttooltipTzUZero out the first mask, typically used whenit corresponds to the conditioning frame.maxmeanminr   zoMethod to combine temporal masks. Max preserves strongest values, mean averages them, min takes weakest values.INTr   i i @  r   zVPixels to grow (positive) or shrink (negative) the mask.Uses morphological operations.)r   r   r   stepr   zHUse tapered corners in morphological operations for smoother mask edges.FLOATg      ?g              ?g{Gz?z:Minimum value to clamp masks to, ensuring minimum opacity.z Maximum value to clamp masks to.)	masksvaeinvert_input_masksignore_first_maskpooling_method	grow_masktapered_corners	clamp_min	clamp_max )r   s    C/home/wildlama/comfy/ComfyUI/custom_nodes/ComfyUI-LTXVideo/masks.pyINPUT_TYPESLTXVPreprocessMasks.INPUT_TYPES   s     "#($F' #'$D& +#($H# #$%$ !$9
 #'#m$ #&"" $#_	 #&"" $#E	oAC
 C	
    r   preprocess_maskszLightricks/mask_operationszFPreprocess masks to be used for masking latents in the LTXVideo model.r   r"   r#   r$   r%   r&   r!   r    c
                    UR                   S:w  a  [        S5      eUR                  u  pnUR                  u  pnU
S-
  U-  S:w  a  [        SU S35      eX-  S:w  a  [        SU S35      eX-  S:w  a  [        S	U S35      eU	(       a  S
U-
  nU(       a  USS2SS2SS24   S-  nOUSS2SS2SS24   nUSS R	                  SXU5      nS S S S.nUU   " U5      nUS:w  ai  [        5       nUR                  n[        U5      (       d  UR                  5       nUR                  UXES9u  n[        U5      (       d  UR                  U5      n[        R                  " U[        R                  " UXg5      /SS9nU4$ )a  
Preprocess masks for temporal latent masking.

Args:
    masks: Input masks tensor of shape (batch_size, H, W) where batch_size = 1 + (num_frames - 1)
           The first mask is for the conditioning frame, remaining masks are for generated frames
    vae: VAE model object containing downscale_index_formula attribute
    pooling_method: Method to combine temporal masks ('max', 'mean', 'min')
    grow_mask: Pixels to grow (positive) or shrink (negative) the mask
    tapered_corners: Whether to use tapered corners in morphological operations
    clamp_min: Minimum value to clamp masks to (ensures minimum opacity)
    clamp_max: Maximum value to clamp masks to
    ignore_first_mask: Whether to zero out the first mask (often used for conditioning)
    invert_input_masks: Whether to invert all input masks (1 - mask)

Returns:
    Tuple containing the processed masks tensor

Raises:
    ValueError: If mask dimensions are not compatible with VAE downscaling factors
   z*Masks must be of shape (batch_size, H, W).r   r   z)Masks batch size must have a multiple of z masks + 1.z%Masks height must have a multiple of .z$Masks width must have a multiple of r   Nc                 &    U R                  SS9S   $ Nr   dimr   )r   xs    r(   <lambda>6LTXVPreprocessMasks.preprocess_masks.<locals>.<lambda>       QUUqU\!_r+   c                      U R                  SS9$ )Nr   r3   )r   r5   s    r(   r7   r8      s    afffmr+   c                 &    U R                  SS9S   $ r2   )r   r5   s    r(   r7   r8      r9   r+   r   )maskexpandr$   r3   )ndim
ValueErrorshapedownscale_index_formulaviewr   devicer   cpuexpand_masktotorchcatclamp)selfr   r   r"   r#   r$   r%   r&   r!   r    
batch_sizeHWtime_scale_factorwidth_scale_factorheight_scale_factor
first_mask	remainingpooling_methodscombinedgrow_mask_nodeinput_deviceprocessed_maskss                          r(   r,   $LTXVPreprocessMasks.preprocess_masksh   s   D ::?IJJ ;;
q '' 	C/B N//14;<M;NkZ  "a'78K7LAN  !Q&67I6J!L 
 %KE qsAqy)A-JqsAqy)J !"INN2'8Q?	 -+,
 #>29= >%ZN $??L ..#<<>(44i 5 KX
 !..#;;|4  ))XyDE1
  !!r+   r'   N)__name__
__module____qualname____firstlineno____doc__classmethoddictstrr   r)   RETURN_TYPESFUNCTIONCATEGORYDESCRIPTIONrG   Tensorintboolfloattupler,   __static_attributes__r'   r+   r(   r   r   
   s     D
. D
4S> D
 D
L L!H+HP l"||l" 	l"
 l" l" l" l"  l" !l" 
u||	l"r+   )
typingr   rG   comfy.model_managementr   comfy_extras.nodes_maskr   nodes_registryr   r   r'   r+   r(   <module>ro      s6      0 , & &'I" I" (I"r+   